ROS 2 Python Lab

Introduction

The goals of this activity are to:

Resources

Controlling a Turtlebot using Python

Create a Python file named forward.py. Your script should create a node that moves the Turtlebot directly forward at a fixed, constant rate of .2 meters per second. (This node will need a publisher, but it won’t need to create any subscriptions.)

Start the simulator or robot according to the instructions from the earlier lab. Don’t start a teleop node, or it will interfere with your controller node.

You should be able to test your script by starting it in the terminal using Python:

python3 forward.py

Using the /scan Topic

Create a new file named wander.py that subscribes to the /scan topic. This node should halt forward progress as soon as an object is observed less than one meter in front of the Turtlebot.

You’ll need to make use of the ROS2 command line tools to determine the appropriate message type and message format associated with the /scan topic. Even then, it will probably require some experimentation to figure out how to access the range values that correspond to the front of the robot.

Note that, on the physical robot, the data on the /scan topic is provided by a “Best effort” publisher. This is not compatible with the default QoS subscriber settings. (You can read more about QoS compatibility here) You will need to include this import at the top of your file:

from rclpy.qos import qos_profile_sensor_data

You can then pass qos_profile_sensor_data as the fourth argument when you call create_subscription to set up your subscription. The resulting code should run correctly on both the simulator and the robot. Without the qos_profile_sensor_data the code will work on the simulator, but not on the physical robot.

Improved Wandering

Modify your wander node so that, instead of stopping, the robot to rotates to avoid looming obstacles. Your finished node should be able to wander around while avoiding obstacles in the robot’s path.

If you developed your code in the simulator, make sure to test it on one of the actual robots and vice versa.

Submitting

Submit forward.py and wander.py through Canvas. Make sure that your submission contains the names of all group members.


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.