This repository serves as an introduction to ROS2 via turtlesim.
Turtlesim simulates a robot via a turtle that can move around a canvas. The turtle can be commanded to move forward, backward, and rotate by setting its velocity. The turtle can also draw on the canvas by setting the pen properties.
The repository contains a Dockerfile and a GitHub Codespaces configuration file to create a development environment for ROS2. The repository also contains a custom node with starter code to move the turtle forward in a straight line.
Tip: To view this README in a more readable format while in VS Code or GitHub Codespaces, right-click on this file's name and select "Open Preview".
You can work with this repository in two ways: locally or in GitHub Codespaces. Follow the instructions below based on your preference.
- Install the following items:
- Clone the repository.
- Open a terminal and change your directory to the repository.
- Run the following command to build the Docker image and start the container:
docker-compose up -d
- SSH into the container:
Password:
ssh -XY -p 2201 root@localhost
robotics
- Make sure you are in the following directory.
/root/dev/turtlesim
- Build the workspace.
- Perform steps 5-6 again for each new terminal you open.
- After you have finished working, run the following command in a terminal on your local machine (not the container) to stop the container. Make sure you are in the repository directory.
docker-compose down
- Open this repository on GitHub.
- To create the codespace for the first time (skip to step 3 if you've already created the codespace):
- Click the green "Code" button.
- Switch to the "Codespaces" tab.
- Click the "+" button to create a new codespace.
- A new tab will open with the codespace. It will take approximately five minutes to build the container and prepare the codespace.
- Once the codespace is ready, you will see the terminal in the bottom of the window, and you will be in the
/workspaces/{repo-name}
directory. - If you are prompted to install the recommended extensions, click "Install All" to install the recommended extensions. If you are not prompted, you can install the recommended extensions by clicking the extensions icon on the left sidebar, searching for "@recommended" in the search bar, and clicking the cloud icon next to "Workspace Recommendations".
- To open the codespace after it has been created:
- Click the green "Code" button.
- Switch to the "Codespaces" tab.
- Click on the codespace you want to open.
- Optional: If you would like to open the codespace in VS Code on your local machine, perform steps 1-2, click the three dots on the right side of the codespace and select "Open with Visual Studio Code".
- Make sure you are in the following directory:
Replace
cd /workspaces/{repo-name}
{repo-name}
with the name of the repository you created the codespace from. - Build the workspace.
- After you have finished working, close the tab with the codespace or close the VS Code window if you opened the codespace in VS Code.
- To stop the codespace:
- Go back to the repository on GitHub.
- Click the "Code" button.
- Switch to the "Codespaces" tab.
- Click the three dots on the right side of the codespace and select "Stop codespace".
Once you've made sure you're in the correct directory, run the following command to build the workspace:
source build.sh
Do not run ./build.sh
or bash build.sh
as it will not have the intended effect. You must use source build.sh
or its shorthand . build.sh
.
Lastly, run the following command to configure your shell:
source ~/.bashrc
The two commands above need to be run only once after you start the container or codespace. These commands need to be run again only if you kill the container or create a new codespace.
To run the turtlesim node, run the following command:
ros2 run turtlesim turtlesim_node
If you are developing locally, the turtlesim GUI will open automatically in a separate window through XQuartz/MobaXTerm. If you're using GitHub Codespaces, you need to view the Desktop to see the turtlesim GUI. See the Desktop Viewer section below.
To run the teleop node to move the turtle with your keyboard, run the following command:
ros2 run turtlesim turtle_teleop_key
To run your custom node, run the following command:
ros2 run my_turtlesim move_turtle
If you're using GitHub Codespaces, to view the turtlesim GUI, perform the following:
- Press Ctrl/Cmd + Shift + P to open the command palette.
- Type ">Ports" and select "Ports: Focus on Ports View".
- In the "Ports" view, right-click on
6080
in the first column and select "Open in Browser" to open the desktop in a new tab. Alternatively, select "Preview in Editor" to view the desktop in the codespace. - You may need to wait a few minutes for the desktop to load.
- Once you see the "noVNC" logo, click the "Connect" button to open the desktop.
- The turtlesim GUI will be visible in the desktop (if you've started the turtlesim node).
- Type:
geometry_msgs/msg/Twist
- Description: The velocity command for the turtle. The turtle will move forward/backward if X linear velocity is positive/negative and rotate counterclockwise/clockwise if the Z angular velocity is positive/negative.
- Type:
turtlesim/msg/Pose
- Description: The pose of the turtle. The pose includes the X and Y position of the turtle, the orientation of the turtle, and the linear and angular velocities of the turtle.
- Type:
std_srvs/srv/Empty
- Description: Clears all drawings. Does not affect the turtle's position.
- Type:
std_srvs/srv/Empty
- Description: Clears all drawings, resets the turtle to its initial position and orientation (in the center of the screen facing right), resets the pen color, and changes the sprite of the turtle.
- Type:
turtlesim/srv/SetPen
- Description: Sets the pen properties for the turtle. You can specify the color, width, and whether the pen is on or off.
- Type:
turtlesim/srv/SetParameters
- Description: Sets the parameters for the turtle. You can specify the background r, g, and b values. Used only for setting the background color via code. To set the background color using the terminal, see the parameters section below.
- Type:
int
- Description: The red, green, and blue values of the background color of the turtlesim GUI.
You can append the -h
flag to any command to view a message describing the command and its options.
- To list all nodes:
ros2 node list
- To view information about a node:
Replace
ros2 node info /node_name
/node_name
with the name of the node you want to view information about.
-
To list all topics:
ros2 topic list
-
To view information about a topic:
ros2 topic info /topic_name
Replace
/topic_name
with the name of the topic you want to view information about. -
To view the messages being published on a topic:
ros2 topic echo /topic_name
Replace
/topic_name
with the name of the topic you want to view messages from. -
To publish a message on a topic:
ros2 topic pub /topic_name message_type '{data: value}'
Replace
/topic_name
with the name of the topic you want to publish a message to,message_type
with the type of message you want to publish, and{data: value}
with the values you want to publish. The values must be in YAML format and must match the message type.- Add the
-1
flag to publish only once:ros2 topic pub -1 /topic_name message_type '{data: value}'
- Add the
-r rate
flag to publish at a specific rate in Hz:Replaceros2 topic pub -r rate /topic_name message_type '{data: value}'
rate
with the rate you want to publish at.
Tip: After typing the message type, type a space, quote, and the first letter of the message type, then press Tab to auto-complete the message type.
- Add the
-
To view the rate at which messages are being published on a topic:
ros2 topic hz /topic_name
Replace
/topic_name
with the name of the topic you want to view the rate of messages being published on.
- To list all services:
ros2 service list
- To view information about a service:
Replace
ros2 service info /service_name
/service_name
with the name of the service you want to view information about. - To call a service:
Replace
ros2 service call /service_name service_type '{data: value}'
/service_name
with the name of the service you want to call,service_type
with the type of service you want to call, and{data: value}
with the values you want to call the service with. The values must be in YAML format and must match the service type.
- To list all parameters:
ros2 param list
- To get the value of a parameter:
Replace
ros2 param get /node_name parameter_name
/node_name
with the name of the node the parameter is in andparameter_name
with the name of the parameter you want to get the value of. - To set the value of a parameter:
Replace
ros2 param set /node_name parameter_name value
/node_name
with the name of the node the parameter is in,parameter_name
with the name of the parameter you want to set the value of, andvalue
with the value you want to set the parameter to.
- To view the definition of a message:
Replace
ros2 interface show package_name/(msg or srv)/message_type
package_name
with the name of the package the message is in,(msg or srv)
with eithermsg
for messages orsrv
for services, andmessage_type
with the type of message you want to view the definition of.