0% found this document useful (0 votes)
3 views

CPS_Lab1

The document outlines the structure and requirements for the 'Cyber-Physical Systems' laboratory, focusing on programming Lego Mindstorms EV3 robots using C++, LabView, and MATLAB/SIMULINK. It details the exercises to be completed, including programming the robot to autonomously drive towards and track objects, and emphasizes the importance of written reports and group collaboration for evaluation. Additionally, it provides instructions for setting up the EV3, compiling code, and transferring it to the robot.

Uploaded by

hikmatnaim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CPS_Lab1

The document outlines the structure and requirements for the 'Cyber-Physical Systems' laboratory, focusing on programming Lego Mindstorms EV3 robots using C++, LabView, and MATLAB/SIMULINK. It details the exercises to be completed, including programming the robot to autonomously drive towards and track objects, and emphasizes the importance of written reports and group collaboration for evaluation. Additionally, it provides instructions for setting up the EV3, compiling code, and transferring it to the robot.

Uploaded by

hikmatnaim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab "Cyber Physical Systems" Exercise 1

2. April 2024

H. Falk, T. Fischer, W. Brandt

Discussion: 2. April 2024


Due to: Week 17, 2024

General and Organizational Information


In the laboratory "Cyber-Physical Systems" you will get an insight into the programming
of systems that interact with their environment via sensors and actuators. For this
purpose Lego Mindstorms EV3-robot kits are available and will be programmed in
three different ways.
In each exercise sheet you will solve a task and get to know a specification language as
well as its programming environment, like it is partially also used in the industry. Each
of these approaches has some advantages and drawbacks. You will discover them over
the course of the lab.
In the first laboratory you are going to program the robot in C++. C++ allows
programming close to the hardware in an object-oriented manner.
Later on you will program the robot via the LabView software. LabView was originally
designed to create and control measurement systems. It allows programming via dataflow
graphs in a graphical programming environment. It also enables the user to measure
data on the robot during the runtime and analyze them on the PC. LabView is also
used in combination with so-called hardware-in-the-loop-systems (which are used as test
environments for real systems) at the creation of controllers.
In the final laboratory you will be using the simulation- and developing-environment
MATLAB/SIMULINK. MATLAB with its addition SIMULINK (a graphical pro-
gramming environment) is used for modeling and simulation of mathematical and physi-
cal systems. A variety of available toolboxes offer the possibility to create C and VHDL
Code, which makes it a powerful and popular developing tool among engineers.

Usage of the Computer Pool


You will get a special username and password for the computer. You can save your data
in a subdirectory of your choice inside your home directory. However, an additional

Page 1
backup of your work, e.g., using an external storage medium or the TUHH cloud, is
recommended.

Participation and Evaluation of the Laboratory


There are three exercise sheets in the laboratory which contain exercises to be solved.
The applied and theoretical exercises should be solved in groups of 2. For each exercise
sheet, a specified number of appointments is available. There will one appointment per
week with assistance. During the final appointment for an exercise sheet you have to
present your results to the supervisor.
All theoretical exercises shall be solved and documented in writing. For programming
exercises, save the current state of the program after each exercise into a separate file.
Upload these files with a significant filename into the file directory of your group in the
Stud.IP-system.
The examination consists of the results of all applied and theoretical exercises of all
exercise sheets, as well as of a written report to each exercise sheet. The content of the
report is specified in the corresponding exercise sheet. The written reports are to be done
in the chosen groups of 2. After each exercise sheet you have the possibility to submit a
preliminary version, for which the tutor will give you hints to improve the report once.
The submission of the preliminary version must be done at latest inside the week after
the last regular appointment of a laboratory. The final reports of all laboratories must
be submitted as printed papers to the tutor until the official date of the examination.
The final grade will consist of the functionality of the programmed robot (which has to
be presented to the tutor), as well as the written reports.
The module is passed if all exercises of all exercise sheets are solved successfully and all
reports meet the minimum requirements individually.
Remember to sign up in time for the examination!

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

Programming the EV3 in C++


The ev3dev-project is used in order to program the robot in the C++ language. ev3dev
is a Debian Linux-based OS for the EV3. It contains suitable libraries for the EV3,
including its peripherals, e.g., actuators and sensors. The operating system is placed
on the MicroSD-card and is automatically booted when starting up the EV3. In the
following table a small selection of the most important classes and their member functions
is shown.

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.

Exercise 1-2: Drive on Command


The two states described in the functional description shall now be implemented piece-
wise. Create a program which reads the value of the touch sensor and turns on (or
turns off) both main motors when the touch sensor is pressed. The program should be
implemented in a way, such that the program execution is not blocked while waiting for
the touch sensor being pressed, but other actions could be potentially executed “simul-
taneously”.2 In case the robot collides with an object, a pressed touch sensor is supposed
to stop the robot.
Hint: The change in state between a pressed and an unpressed touch sensor should only
be detected once for each press. How can this change be detected?

Exercise 1-3: Modeling


Inside this exercise, you shall prepare the architecture of your software. The subexercises
b) - d) should be used as a theoretical base for the following exercises. As a basic
structure and orientation, use the functional description from page 3. Solve this exercise
in a written form.
a) Draw a state machine which represents the previous exercise.
b) Draw a flow diagram which represents the object identification. The object identi-
fication should use the ultrasonic sensor and the third motor (on top of which the
ultrasonic sensor is placed). The aim of the object identification is to determine the
nearest object, its distance as well as its direction. The sensor shall be rotated alter-
natively to the right and to the left. After each turn, the driving speed of the motors
shall be calculated. The actual algorithm to determine the speed does not need to
be considered here yet.
c) Convert the flow diagram into a state machine.
d) Combine both state machines into one. The robot shall be activated by pressing
the touch sensor and execute the object identification. If the touch sensor is pressed
again, the robot shall re-enter the inactive state. Consider what type of variable is
reasonable to implement a state machine in C or C++.

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.

Exercise 1-5: Tracking a Moving Object


In the last exercise the robot shall be completed in its functional description. The
robot shall now learn to drive towards the nearest object with an appropriate speed.
Therefor determine the angle and distance of the nearest object (Reminder: Use the
global minimum). Calculate appropriate speed values for both driving motors as a
function of the distance and angle. The aim is to implement a constant tracking of a
moving object, which shall be as smooth as possible.
Hint: Computing a precise angle for the wheels to turn is difficult and error-prone, due
to physical imperfections. Instead, try to make continuous adjustments to the motor
speeds to control the orientation of the robot.

Evaluation of the Laboratory Experiment


Write a report about the exercises 1-2 to 1-5 for the evaluation.
Add your written code to the corresponding exercise. Shortly explain the important
parts of your implementation.
The results of exercise 1-3 shall be part of the report as well. Explain your thoughts,
which led to the results of exercise 1-3.
Note: Remember that the report is a major component for your final grade!

Page 7

You might also like