Practical 1 (Assessed 20OCT) - RobotTeaching/COMP4034 GitHub Wiki
Practical 1 is all about moving your robot! You will learn how to use Twist messages to drive your robot in a square, then how to use odometry information to refine your solution.
[!IMPORTANT] These practicals work on simulation first! Your turtlebot must be OFF before you run any
turtlebot3_gazebocommand. Once you can show us your code working in simulation, please ask us to give you a battery.
Preparation
Make sure you complete the ROS2 basics. In addition please go over the ROS2 Humble tutorials. We recommend completing the First steps with ROS - learning path, going through Beginner: CLI tools (you can skip services, actions, and parameters) and Beginner: Client libraries until the creating interfaces tutorial (please go over Python tutorials only.)
Before you begin
Make sure you have cloned the git repo so you have the practical_1 skeleton code (refer back to Git basics if you need to) into the /home/ros2_ws/src folder on the lab machine containers:
- Bring up the Docker container on the B85 lab machine (follow ROS2 Basics if you need a reminder, or check the readme in the
~/COMP4034/srcfolder on the lab machine) - In a VSCode terminal,
cd srcthen rungit clone https://github.com/RobotTeaching/COMP4034.git - In the same VSCode terminal, once the clone has completed, try running
ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py.
[!IMPORTANT] There's a strangeness with Gazebo where it tries to use services before it's started them, so the "spawn service" may fail. If it does,
ctrl+cin the terminal and launch Gazebo again (this time, it should have already started the service from the last time!)
Goals
By the end of this practical, you should be able to:
- Control the movement of your robot by publishing Twist messages, and drive in a square
- Subscribe to odometry information to refine the movement of your robot (and drive in a better square!)
- Start the physical robot, and run your code on it
Move your TurtleBot (publishing Twist messages)
Step 1: understanding the skeleton code
The skeleton code you downloaded contains some very basic ROS2 code for moving your robot forwards. Find the practical_1.py file in the folder structure and open it in a code or text editor. You should recall from ROS2 Basics that robots move when Twist messages are published on the /cmd_vel topic, and that Twist messages contain a linear and angular component.
❓ Find where the Twist message is defined in the code
❓ Find out which component of the angular vector (x, y, or z) you need to change to make your robot turn
❓ Finally, comment the code (ignoring lines 47-49 for now) to ensure you have a good understanding of what it does. You can use the resources at the bottom of the page
Step 2: running the code in simulation
You can follow the instructions in ROS2 Basics to start the TurtleBot simulation:
- In your terminal, navigate to your workspace using
cd ros2_ws - Run the following command:
ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py
Then, in a new terminal tab, you need to build the skeleton code. Since this is the first time you're using ROS2, start by running colcon build in the ros2_ws directory (this make take a while, start thinking about your strategy for driving in a square while you're waiting!). When the build has finished run source install/setup.bash.
Now you can run your code: ros2 run practical_1 practical_1
❓ Does the code do what you expected?
Stop the code running (ctrl + c in the terminal running the code).
Step 3: driving around in a square 🤖
Now, make some changes to the code so that your robot drives around in a square with a side of 1m. You'll need to think about how you'll continuously drive forwards for a distance, and how you'll turn.
[!TIP] Linear velocity is in metres/second, angular velocity is in radians/second.
Whenever you make changes to the code, you'll need to rebuild. You can build only your practical_1 package using colcon build --packages-select practical_1. Make sure you re-source setup.bash from the install folder as well (source install/setup.bash).
We call this type of movement open-loop control, which you will have learnt about in the lectures.
Using odometry to refine your TurtleBot's movement (subscribing to the /odom topic)
You'll notice that your robot's square starts off well, but then gets a bit wobbly! This is because open-loop control doesn't incorporate any feedback about the robot's actual location, and over time tiny inaccuracies in the way the robot measures its wheels turning begin to accumulate.
Step 1: understanding the /odom subscriber
Let's look at the odom_callback method in practical_1.py. You'll have noticed some information from this method being printed when you run the code.
❓ Use the resources below to comment this piece of code, so you understand how the subscriber works
❓ Don't forget you can use RQT to find out more about the message type!
[!NOTE] Unlike
Twist, the message typePoseuses quaternions to represent 3D rotation. You don't need a mathematical understanding of quarternions for this module; instead you will want to translate them (x,y,z,w) into Euler angles (x,y,z) and vice versa as necessary. You could try implement this yourself, or use functions from the internet. You'll need aquat_to_euler()function.
Step 2: driving around in a (better) square 🤖
- Save a copy of your old code for driving in a square. You will need to submit this file to GitLab.
- Edit the code you are running to use the odometry information. The goal is to get the robot to drive in the neatest square possible: use the odometry information to correct any drift
This type of movement is called closed-loop control, as you will have learnt about in the lecture.
❗Show a lab assistant your closed-loop control running in simulation when you're happy with it.
Resources
Generative AI policy
You should not copy and paste large chunks of code from any AI tool, as this constitutes false authorship and is an academic offence. You must be able to explain all code you submit. Failure to do so may result with a zero mark.