Minitask 1 - RobotTeaching/COMP4034 GitHub Wiki

Minitask 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.

Before you begin

Make sure you have cloned the git repo so you have the minitask1 skeleton code (refer back to Git basics if you need to) into the /home/ros2_ws/src folder on the lab machine containers:

  1. 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/src folder on the lab machine)
  2. In a VSCode terminal, cd src then run git clone https://github.com/RobotTeaching/COMP4034.git
  3. In the same VSCode terminal, once the clone has completed, try running ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py. 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+c in 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 minitask, 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 minitask1.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 director (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 minitask_1 minitask1

❓ 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. You'll need to think about how you'll continuously drive forwards for a distance, and how you'll turn.

Hint: linear velocity is in metres/second, angular velocity is in radians/second.

When you make changes to the code, you'll need to rebuild. You can build only your minitask_1 package using colcon build --packages-select minitask_1. Make sure you re-source setup.bash from the install folder as well!

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 minitask_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 about quaternions: you'll see that the message type Pose uses quaternions to represent rotation. You don't need to understand what a quaternion is or how it works (unless you want to!) and instead you'll want to translate the quaternion into Euler angles (x, y, z). You can figure this out yourself if you really want to, or you can find the standard method to do this online (just make sure you reference it!)

Step 2: driving around in a (better) square 🤖

  1. Save a copy of your old code for driving in a square. You will need to submit this file to GitLab
  2. 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, by using 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

Writing a publisher node

Writing a subscriber node