You'll need to follow the instructions for connecting to a Turtlebot3 before beginning this exercise.
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.
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:
linear.x
direction sets forward velocity and angular.z
sets angular velocity.To run your code, you will need to do three things:
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
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
Once you finish, your robot should behave somewhat similar to what is pictured below:
$ 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.
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
.
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.
The relevant ROS topics you'll find useful are:
linear.x
direction sets forward velocity and angular.z
sets angular velocity.ranges
, a list of 360 numbers where each number corresponds to the distance to
the closest obstacle from the LiDAR at various angles. Each measurement is 1 degree apart. The first entry in
the ranges
list corresponds with what's directly in front of the robot, and the remaining entries
are in counterclockwise order. The LiDAR has a maximum range of ~4 meters on the physical robot, and angles
where nothing is detected within that range will have entries of 0.
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:
If your group finishes both coding exercises #1 and #2, you can either leave or spend time working on the Warmup Project.