Your goal in this exercise is to program your Turtlebot to perpetually spin in a circle.
Let's first create a new ROS2 package.
Within the package, create a python file called spin_in_circles.py
.
You can see all of the components of the Turtlebot 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 ROS2 topic you'll find useful is:
linear.x
direction sets forward velocity and angular.z
sets angular velocity.In one terminal, run:
$ ros2 run turtlesim turtlesim_node
Topics listing
$ ros2 topic list
Topics type in brackets
$ ros2 topic list -t
$ ros2 topic info /turtle1/cmd_vel
Structure of data the message expects
$ ros2 interface show geometry_msgs/msg/Twist
To see the data being published on a topic, use:
$ ros2 topic echo /turtle1/cmd_vel
At first, this command won’t return any data.$ ros2 topic pub --once /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
Now publish using
spin_in_circles.py
Once you finish, your robot should behave somewhat similar to what is pictured below:
A robot simulator allows one to interact with a robot and its environment virtually without requiring a real robot. Robot simulators often provide the following functionality,
gazebo_ros_demo.world
$ gazebo --verbose gazebo_ros_demo.world
To launch an instance of RViz, run the following command.
$ rviz2
While both RViz and Gazebo are graphical user interfaces that seem somewhat similar, their nature is quite different. Gazebo is a simulator. It places robots in a virtual world and allows users to give it commands and generates simulated sensor data. This sensor data is not very human friendly (e.g. some numbers and binaries). RViz simply visualizes this generated sensor data so that human users can understand what their robot observes in human terms.
Note that like any other ROS2 node, the robot simulator publishes and subscribe to topics.
/demo/cmd_demo
When you publish to this topic, you'll set the robot's velocity. The linear.x
direction sets forward velocity and angular.z
sets angular velocity.
/scan
The robot's LiDAR publishes measurements to this topic. LiDAR is used for measuring distance via lasers. The laser scan message consists of a number of attributes,
$ ros2 interface show sensor_msgs/msg/LaserScan
For initial purposes, the most important one is 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 array corresponds to what is in front of the robot.
Gazebo integrated the physics engine, rendering, and support code for sensor simulation and actuator control.
Next, launch Gazebo with the Turtlebot robot. In one terminal, run:
$ gazebo --verbose gazebo_ros_demo.world
In a second terminal, run:
$ ros2 topic pub /demo/cmd_demo geometry_msgs/Twist '{linear: {x: 1.0}}' -1
In Gazebo, you should see a Turtlebot robot some distance apart from a brick wall.
Your objective in this exercise is to move the robot forward and stop it just before it would collide with the wall. This will require using the laser scan data from the robot's LiDAR to make decisions about how to control the motors driving the two wheels of the robot.
Write your python ROS2 node in stop_at_wall.py