Skip to main content

ROS1 Example (Turtlesim)

Experience the basic concepts and functionalities of ROS1 quickly using the Turtlesim example.

tip

This tutorial is based on the ROS1 Noetic Docker image but is not limited to the Docker environment. It can be performed in any ROS1 environment.

If you are using a Docker environment, it is recommended to first learn how to use Docker.

Start the Docker Script

Use the following command to start the Docker script: ros_noetic.sh is a script created during the ROS1 environment setup.

radxa@device$
bash ros_noetic.sh

It is recommended to use multiple terminals to access the same Docker container for easier subsequent operations.

Start the Master Node

ROS1 requires starting the Master node before launching the Turtlesim node.

radxa@docker$
roscore

Start Turtlesim

Use the following command to start Turtlesim:

radxa@docker$
rosrun turtlesim turtlesim_node

This command will create the /turtlesim node and display a window containing a turtle.

Control Turtlesim

Use the following command to control the turtle's movement:

radxa@docker$
rosrun turtlesim turtle_teleop_key

This command will create the /teleop_turtle node.

tip

Click and focus on the terminal window running this command. You can control the turtle's movement using the arrow keys on your keyboard.

ROS1 Basic Concepts

Through the Turtlesim example, you can further understand and use the basic concepts and commands of ROS1.

tip

To stop a running ROS1 command, use the Ctrl + C keyboard shortcut.

ROS1 Nodes

Nodes are the basic communication units in ROS1. They interact with other nodes through publishers and subscribers.

tip

Key Components:

  • Publisher: Interface for publishing messages to a topic.
  • Subscriber: Interface for receiving messages from a topic.

View Nodes

Use the rosnode list command to view nodes. Currently, only the command to start Turtlesim is running.

radxa@docker$
rosnode list

ROS1 Topics

Topics are the publish/subscribe communication mechanism in ROS1, enabling asynchronous data transfer between nodes.

tip

Communication Roles:

  • Publisher: Sends messages to a specific topic.
  • Subscriber: Subscribes to a topic to receive messages.

View Topics

Use the rostopic list command to view topics:

radxa@docker$
rostopic list

Publish a Topic

Use the rostopic pub command to publish a velocity control message to move the turtle:

rostopic pub -r 10 /turtle1/cmd_vel geometry_msgs/Twist "{linear: {x: 0.5, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.5}}"

This command will move the turtle at a linear velocity of 0.5 and an angular velocity of 0.5 at a frequency of 10 Hz.

Subscribe to a Topic

Use the rostopic echo command to subscribe to a topic and view its messages:

radxa@docker$
rostopic echo -n 1 /turtle1/pose

This command will print the turtle's position once in the terminal.

ROS1 Services

Services are the request/response communication mechanism in ROS1, used for synchronous calls between nodes (e.g., remote function calls).

tip

Interaction Flow:

  • Client: Sends a request.
  • Server: Processes the request and returns a response.

View Services

Use the rosservice list command to view services:

radxa@docker$
rosservice list

Call a Service

Use the rosservice call command to call a service and clear the screen:

radxa@docker$
rosservice call /clear

This command will clear the background in the Turtlesim window.