Assignment 2. Automatic Path
Info
To complete this assignment, you should have completed:
- Lab 3.1. ROS 2 Topics in C++
- Lab 3.2. ROS 2 Services in C++
- Lab 3.3. ROS 2 Package, msg, srv
Motivation
Objective: Control a simulated robot to follow a predefined path
- In M2, one of the programmer tasks each year is to program the robot such that it can follow a path on the game field automatically.
- In this assignment, you will write a node to control the movement of a simple simulated robot (the turtle) after a ROS service is called to trigger the start of the path.
Background. What is the turtle?
This section is exactly the same as that in assignment 1. If you have read that before, you can skip this part.
The "turtle" is the name for the simulated robot. This simulator is maintained by the ROS organization, the documentation is here: https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Introducing-Turtlesim/Introducing-Turtlesim.html
The above interface is running inside a simulator node called turtlesim_node.
Important
If you are trying to play with turtlesim on a remote machine (e.g. standalone Raspberry Pi) that are incapable of displaying GUI contents, you can use X11-Forwarding method to visualize the GUI contents on your local machine.
You can set up X11-Forwarding in your mac or windows according to the following guide:
- Download and install MobaXterm from here.
- Launch MobaXterm, and create a new session by clicking "Session" at the menu bar. Select "SSH" as Session Type. Then, enter remote machine's IP address and username. No need to change the port value.
- Select the "Advanced SSH settings" tab and then make sure the "X11-Forwarding" box is checked. Leave the rest of the settings at their defaults. Select OK.
- You have just logged into the remote machine, you can run command:
rqt
in the terminal, to verify if GUI content can be displayed on your windows.
- Download and install XQuartz from here.
- Remote access into the remote machine by using SSH with this command:
ssh -X <username>@<hostname>
. Note that, you need to replace the<username>
and<hostname>
with the actual username and hostname of remote machine. You can ask our team member about it. The-X
option here enables X11-forwarding. - Run command:
rqt
to launch rqt GUI application, and check if the GUI can be display on your mac.
Running the turtle
To run the turtlesim_node, run ros2 run turtlesim turtlesim_node
, and the above window should pop up.
The turtlesim package also includes a keyboard-based controller. To run it, in a new terminal, run ros2 run turtlesim turtle_teleop_key
.
In summary, you have to run the following two commands in separate terminals.
# on a separate terminal
$ ros2 run turtlesim turtlesim_node
# on a separate terminal
$ ros2 run turtlesim turtle_teleop_key
...(omitted)
Reading from keyboard
---------------------------
Use arrow keys to move the turtle.
- Now you can use the arrow keys of the keyboard to drive the turtle around. If you cannot drive the turtle, select the terminal window of the turtle_teleop_key to make sure that the keys that you type are recorded.
Topics provided by turtlesim
The turtlesim_node receives velocity instructions via the topic /turtle1/cmd_vel
. Try to use ros2 topic echo
on a new terminal to see how the messages are interpreted when you are using the keyboard controller.
$ ros2 topic echo /turtle1/cmd_vel
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
- The topic type can be obtained via
ros2 topic type [topic]
orros2 topic info [topic]
.
$ ros2 topic type /turtle1/cmd_vel
geometry_msgs/msg/Twist
Position and velocity feedback from the turtle
When the turtle is moving, the turtlesim_node
actually will also publish a message containing the current position and velocity of the turtle under /turtle1/pose
.
- In a real robot, we often call this the "position feedback" and "velocity feedback".
- Note that the msg type of
/turtle1/pose
isturtlesim/msg/Pose
.
To test this behavior, you can try to drive the turtle and check the topic being published:
- Run the nodes (you can skip this if they are already running):
$ ros2 run turtlesim turtlesim_node $ ros2 run turtlesim turtle_teleop_key
- Drive the robot around using your keyboard. At the same time, you can use
ros2 topic echo
to display the position published:
$ ros2 topic echo /turtle1/pose
x: 0.746215641499
y: 0.0329808667302
theta: 2.94571447372
linear_velocity: 0.0
angular_velocity: 0.0
---
x: 0.746215641499
y: 0.0329808667302
theta: 2.94571447372
linear_velocity: 0.0
angular_velocity: 0.0
---
...(omitted)
The turtlesim_node
also provides the /turtle1/teleport_absolute
service that allows you to teleport the turtle directly to a position on the field.
Try to call the service in the terminal!
$ ros2 service call /turtle1/teleport_absolute turtlesim/srv/TeleportAbsolute "{x: 0.0, y: 0.0, theta: 0.0}"
- You should see that the turtle has moved to the lower-left corner (origin) of the field.
Required functionalities of your node
Important
Since you will be creating some custom messages and services in the same package, you can reference ROS 2 tutorial about how to implement custom interfaces. Note that we generally call msg, srv as interfaces.
You should write a node, which via service servers, provides the following features.
-
Turn to a specified global orientation
- Your node should provide a service named
/set_orientation
. - When this service is called, the turtle should start turning (in the direction which leads to a decrease in the absolute difference to the target orientation) until the turtle has reached the target orientation. You can check the current orientation from the
/turtle1/Pose
topic stated previously. - The
srv
type of this service should beSetOrientation.srv
. You should create this file by yourself. The required request and responses are:- Request: There should be a
float64
field namedorientation
.- This field specifies the target orientation (in radians) that the turtle should turn to.
- Request: There should be a
- Response: There should be a
bool
field namedsuccess
.- When the turtle has finished turning, the service should return with
success: True
- (ignore if you don’t understand) To avoid floating point precision issues, the turtle will be considered "at the correct orientation" if the absolute difference between the final orientation and the target orientation is less than 0.05 .
- When the turtle has finished turning, the service should return with
- Your node should provide a service named
-
Move forward a specified distance
- Your node should provide a service named
/walk_distance
. - When this service is called, the turtle should start moving forward (positive velocity
linear.x
) until the turtle has moved forward the specified units. - The
srv
type of this service should beWalkDistance.srv
. You should create this file by yourself. The required request and responses are:- Request: There should be a
float64
field nameddistance
.- If distance is
positive
, this field specifies the distance the turtle should move. - The boundaries of the turtle map is \([0, 11]\) for both \(x\) and \(y\). If moving
distance
will result in the turtle moving out of the field (i.e. hit the wall), the turtle should not move.
- If distance is
- Request: There should be a
- If distance is negative, the turtle should not move.
- Response: There should be a
bool
field namedsuccess
.- If distance is
positive
, then the turtle has finished moving, the service should return withsuccess: True
- If the turtle does not move due to
distance
being too large (and beyond the range of the map) ordistance
being negative, the service should return withsuccess: False
- If distance is
- Response: There should be a
- Precision issues will be handled the same as the previous task.
- Your node should provide a service named
Template package
Where do I get even started with this assignment… there are so many tasks…
To help you, we have provided you with a template package, and you can work on the controller node directly!
- Inside
m2cs_ros_tutorial
, you should see a package namedturtle_path
. - The node that you should be working on is
turtle_path/src/path_manager.cpp
. - If you are lost, there are some "blanks" and hints in the file that you can fill in to complete the assignment.
- Note that apart from path_manager.cpp, you should also modify CMakeLists.txt, package.xml and create appropriate srv files by yourself!
Assignment evaluation/testing procedures
This part describes how your assignment submission will be evaluated by the trainers, which also gives hints on how you can test your program while developing it.
- The trainer's machine will have
turtlesim
and yourturtle_path
installed. - The trainers will run the command
ros2 launch turtle_path test.launch.xml
. This command runsturtlesim_node
and runs your filepath_manager.cpp
. - The trainers will test the features stated in "Required functionalities of your node", by driving the turtle to a random location then using
ros2 service call
. - The trainers will read your source code to check your implementation skills and coding style. You are encouraged (but not required) to discuss with other trainees, but we require you to write your own code (no plagiarism!).
Submission instructions
Please refer to the submission instructions on the teaching PPT slides.
Warning
We will start marking your assignments after the deadline, so make sure it is pushed!