CPS_Lab1
CPS_Lab1
2. April 2024
Page 1
backup of your work, e.g., using an external storage medium or the TUHH cloud, is
recommended.
Page 2
Exercise 1: Functional Description of the Robot
In this exercise sheet the robot shall be programmed so that it can drive towards an
object autonomously and can follow a moving object.
Initially the robot shall be in the state Standby. After pressing the touch sensor, the
robot shall enter the state Active. Another push and the robot re-enters the Standby
state.
The ultrasonic sensor evaluates the surroundings in front of the robot and checks for
objects. The object recognition is only activated in the Active state. The environment
is scanned by turning the ultrasonic sensor in an arc to the front of the robot.
In case the robot is in the Active state, it shall drive towards the nearest object. If no
object is in reach, the robot shall stop. The robot shall also stop if the distance towards
the nearest object is too small.
The following ports are used by the robot:
• A: Left Wheel
• B: Right Wheel
• D: Motor for the ultrasonic sensor
• 1: Touch sensor
• 4: Ultrasonic Distance sensor
Page 3
Important classes and their member functions for the programming of the EV3 in the
C++ language:
motor(PORT, MOTOR_TYPE) Class representing an EV3 motor.
PORT: The port which the motor is connected to
(e.g., OUTPUT_A).
MOTOR_TYPE: Determines the motor type
(motor_large or motor_medium).
→ set_duty_cycle_sp(SPEED) Set the setpoint of the motor’s Duty Cycle to
SPEED (integer-value between -100 .. +100).
The setting of the setpoint itself does not drive
the motor.
→ run_direct() Run the motor at the saved setpoint value of
SPEED constantly. A change of the setpoint value
has a direct effect on an active motor.
→ set_position_sp(ANGLE) Set the setpoint of a rotation of the motor in
degree (ANGLE is an integer-value in degree.).
→ run_to_abs_pos() Run the motor until the given setpoint degree.
→ set_speed_sp(SPEED) Set the speed in degrees per second for
run_to_abs_pos().
→ stop() Stop the motor.
→ position() Read the current angle of the motor.
→ set_position(ANGLE) Set the „internal” angle of the motor (not the
setpoint!) to ANGLE.
touch_sensor(PORT) Class representing an EV3 touch sensor.
PORT: The port which the touch sensor is con-
nected to (e.g., INPUT_1).
→ is_pressed() Boolean value of the touch sensor (0 = b not
pressed, 1 =
b pressed).
ultrasonic_sensor(PORT) Class representing an EV3 ultrasonic sensor.
PORT: The port which the ultrasonic sensor is
connected to (e.g., INPUT_1).
→ distance_centimeters(): Read out the value of the ultrasonic sensor in
centimeters (as a floating point value)
You can read the complete API here:
http://ev3dev-lang.readthedocs.org/en/latest/spec.html
Page 4
Exercise 1-1: Preparing the EV3
In this task the connection between the EV3 and the computer will be prepared. You
will also compile an example source code file to an executable binary and transfer it to
the EV3.
a) Make sure that the MicroSD card is inserted into the EV3-brick and power-on the
brick by pressing the center button. The booting process can take several minutes.
Connect it to the WLAN cps_wifi (or cps_wifi_2). In order to do so, navigate
to “Wireless and Networks” → “Wi-Fi” → “Start Scan”. If necessary, turn on the
WiFi-stick via “Powered”. After the brick successfully connected to the WLAN, the
network address will be shown in the title bar of the EV3, e.g., 192.168.2.101.
Connect your pool computer to the WLAN as well, using the large, white WLAN
stick.
b) Download the file Exercise1.tar.gz from StudIP. It contains a VSCode project that
allows you to easily cross-compile your source code for the robot. 1
Open the hello_world.cpp file in VSCode and install the C++ extension when
prompted. Have a look at the source code file and consider, what the program does.
Compile the program by pressing Ctrl-Shift-B or clicking Terminal → Run Build
Task. This creates a file hello_world.elf that the robot can execute.
c) The next step is to transfer the executable binary onto the EV3-brick and start it.
For transferring the binary, execute the following command:
1 scp hello_world.elf robot@IP_ADDRESS:~/
IP_ADDRESS has to be replaced by the IP address of your EV3. robot is the username.
The password is maker.
Alternatively, you can use the graphical program Krusader to copy the file to
the robot. You can connect to the robot using the fish:// protocol, i.e.,
fish://[email protected].
d) Finally, establish an SSH connection the EV3 robot and execute the example binary.
Execute the following command (a second terminal window is recommended for an
improved overview):
1 ssh robot@IP_ADDRESS
The content of the home directory can be listed via the command ls. Run the
previously transferred binary via ./hello_world.elf. Watch the terminal as well as
the display of the EV3. Programs can be started via the file browser of the EV3 as
1
Cross-compilation refers to the process of translating the source code into an executable format for
a different instruction-set architecture. The robot uses an ARM instruction set, whereas the PC
operates using x86 instructions.
Page 5
well. Now start the program through the file browser and observe again the display
of the EV3 and the terminal.
2
The “seemingly” simultaneous sequence of actions, due to the high processor frequency, is meant here,
not the actual parallel execution (the ARM926 processor used is a single-core processor).
Page 6
Exercise 1-4: Object Identification
The robot shall now learn to find the nearest object and determine its distance and
direction.
a) Create a program which implements the combined state machine from exercise 1-4.
The actual control for the driving motors shall not be implemented yet.
b) Read out the values of the ultrasonic sensor and the current angle of the motor,
on which the sensor is placed. Display these values on the display of the EV3 (or
STDOUT).
Familiarize yourself with the sensor values of the ultrasonic sensor and the sensor
itself.
c) Extend the program that after each rotation of the ultrasonic sensor a calculation of
the driving motors’ speed is done, but without implementing the actual calculation
yet – Only write the function head and the control structures to the appropriate
locations in the code.
Page 7