Lab A: First Turtlebot3 Programming


Today's Lab



What You'll Need for Today's Class



Connecting to a Turtlebot3


You'll need to follow the instructions for connecting to a Turtlebot3 before beginning this exercise.


Coding Exercise #1: Turtlebot3 Spinning in Circles


For this exercise you'll work with one partner, where you and your partner will share one robot. Both you and your partner will individually complete both exercises (each of you should have a working solution on your computer). Even though you each will be working on your own solutions, feel free to collaborate with one another, talk through your code, and/or share your screen in order to help each other out.

Your goal in this exercise is to program your Turtlebot3 to perpetually spin in a circle. Let's first create a new ROS package called lab_a_spin_circles:

$ cd ~/catkin_ws/src/intro_robo
$ catkin_create_pkg lab_a_spin_circles rospy std_msgs geometry_msgs
$ cd ~/catkin_ws && catkin_make
$ source devel/setup.bash

Within the lab_a_spin_circles package, create a scripts directory, and within the scripts directory create a new python file called spin_in_circles.py. This is the Python file we'll use to program our robot to spin in circles.

Before going any further, it's helpful to get familiarized with our friend, the Turtlebot3. You can see all of the components of the Turtlebot3 highlighted in the figure below.

Turtlebot3

In order to program the robot to spin in circles, you'll want to be controlling the two motors that are connected to the robot's two wheels. The relevant ROS topic you'll find useful is:

Note: Here are some commands you might find useful: For more info on these commands, and for more handy ROS commands, please check out the ROS Resources page of the course site. Additionally, please utilize Google or your favorite search engine to learn more about these ROS topics and message types (e.g., here's the documentation for the PointStamped message).

To run your code, you will need to do three things:

  1. Start up the ROS Master, which will enable communication between your ROS nodes.
  2. Launch the Turtlebot3's bringup node, which will allow it to respond to ROS messages that you publish.
  3. Run your Python script.

Before we connect to the robot, you must be on the same wifi network as the robot (intro-robo), otherwise you won't be able to connect to the robot.

In the first terminal tab, run:

$ roscore

In a second terminal tab, SSH into the Turtlebot (specifically the Raspberry Pi on the Turtlebot) before running bringup using the bringup alias (more details on why here) AND set the ROS_MASTER_URI to your IP address (if you need to find your IP address use the command ifconfig) using the alias set_ip. Run:

$ ssh pi@IP_OF_TURTLEBOT
$ set_ip LAST_THREE_DIGITS
Note: In case you forgot, the password is turtlebot.

Next, we will run the bringup node on the Turtlebot by either running:

$ roslaunch turtlebot3_bringup turtlebot3_robot.launch

or we can use the handy alias bringup to do the same thing:

$ bringup

Finally, in a third terminal tab, run your ROS node:

$ rosrun lab_a_spin_circles spin_in_circles.py
Note: Don't forget to run chmod u+x spin_in_circles.py!

Once you finish, your robot should behave somewhat similar to what is pictured below:

Turtlebot3 Spin Circles
Note: You may find it handy to be able to run a single command to stop the Turtlebot. You can do this by either running
$ rostopic pub -1 /cmd_vel geometry_msgs/Twist  '{linear:  {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0,y: 0.0,z: 0.0}}'
or by executing that same command via the alias stop_bot on the robot.

Once you and your group finish this coding exercise, please move onto the next exercise.


Coding Exercise #2: Turtlebot3 Stopping in Front of a Wall


To get started on this exercise, create a new ROS package inside the intro_robo directory called lab_a_stop_at_wall. You can use the same commands to create this ROS package as you did for lab_a_spin_circles.

Turtlebot3 in front of wall

Your objective in this exercise is to move the robot forward and stop it just before it would collide with a wall. This will require positioning the robot in front of a wall and using the laser scan data from the robot's LiDAR (see robot diagram again, below) to make decisions about how to control the motors driving the two wheels of the robot.

When finding a wall for your robot, ensure that it is opaque (i.e. not a glass window). Remember, glass is transparent to the LiDAR.

Turtlebot3

The relevant ROS topics you'll find useful are:

Note: You might want to consider possible edge cases by putting the robot at various distances from very close to the wall to very far away from the wall to ensure that your code is robust to the minimum and maximum ranges of the Turtlebot's LiDAR sensor.

Create a scripts directory within the lab_a_stop_at_wall directory and write your python ROS node (e.g., stop_at_wall.py), putting it within the scripts directory. After ensuring that your script is executable (chmod u+x stop_at_wall.py), you can run it with the following command in a third terminal:

$ rosrun lab_a_stop_at_wall stop_at_wall.py

Success looks like what you can see in the following image where the robot stops just before it would collide with the wall:

Turtlebot3 moving to wall

If your group finishes both coding exercises #1 and #2, you can either leave or spend time working on the Warmup Project.