Skip to content

3_ROS_Tips

Arseni1919 edited this page Jul 31, 2025 · 3 revisions

🏠 Home

ROS 2 Tips

A collection of useful commands and tips for working with ROS 2.

Core Concepts

Topics vs. Services vs. Actions

  • Topics: Use for continuous data streams, like sensor readings or robot state.
  • Services: Use for quick request/response communication, like triggering a calculation or changing a setting.
  • Actions: Use for long-running tasks that have a clear beginning and end, and provide feedback along the way, like navigating to a goal.

ROS 2 Architecture

ROS 2 uses DDS (Data Distribution Service) as its middleware. This provides:

  • Reduced coupling between nodes
  • Increased scalability
  • Improved performance, reliability, security, and flexibility

Workspace and Packages

Creating a Workspace

To build a ROS 2 workspace, run the following command in the workspace folder:

colcon build

For faster development, use --symlink-install to create symbolic links to your source files. This allows you to edit files without needing to rebuild.

colcon build --symlink-install

Navigating Packages

Quickly change your directory to a specific package:

colcon_cd <some_ros_package>

Workspace Folders

When you build your workspace, these folders are created automatically:

  • build: Temporary files used during the build process.
  • install: The finished, ready-to-use parts of your software.
  • log: Logs of the build process, useful for debugging.

Creating a Package

To create a new Python package:

ros2 pkg create --build-type ament_python --license Apache-2.0 --node-name my_node my_package
  • It's best practice to create packages within a src folder in your workspace.

Checking Dependencies

Before building, check for and install any missing dependencies:

rosdep install -i --from-path src --rosdistro jazzy -y

Introspection and Debugging

Listing ROS 2 Entities

To see all active nodes, topics, services, or actions:

ros2 node list
ros2 topic list
ros2 service list
ros2 action list

Getting More Information

  • Node Info: Get a list of a node's subscribers, publishers, services, and actions.
    ros2 node info <node_name>
  • Topic Info: See the message type and number of publishers/subscribers for a topic.
    ros2 topic info /turtle1/cmd_vel
  • Service Info: See the type of a service.
    ros2 service info <service_name>
  • Action Info: Get details about an action.
    ros2 action info /turtle1/rotate_absolute

RQT Tools

  • RQT Graph: Visualize the connections between nodes and topics.
    rqt_graph
  • RQT Console: A GUI for viewing and filtering log messages.
    ros2 run rqt_console rqt_console

Topics

Listing Topics with Types

ros2 topic list -t

Finding Topics by Type

ros2 topic find geometry_msgs/msg/Twist

Viewing Message Structure

ros2 interface show geometry_msgs/msg/Twist

Publishing to a Topic

Publish a message to a topic from the command line:

ros2 topic pub <topic_name> <msg_type> '<args>'

Example:

ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0}, angular: {z: 1.8}}"

To publish a single message and then exit:

ros2 topic pub --once -w 2 /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}}"
  • --once: Publish one message and exit.
  • -w 2: Wait for two matching subscriptions before publishing.

Monitoring Topics

  • Frequency: Check the rate at which messages are published.
    ros2 topic hz /turtle1/pose
  • Bandwidth: Check the bandwidth used by a topic.
    ros2 topic bw /turtle1/pose

Parameters

Listing Parameters

To see the parameters for all active nodes:

ros2 param list

Getting a Parameter's Value

ros2 param get <node_name> <parameter_name>

Example:

ros2 param get /turtlesim background_g

Setting a Parameter's Value

ros2 param set <node_name> <parameter_name> <value>

Example:

ros2 param set /turtlesim background_r 150

Saving and Loading Parameters

  • Save: Dump all of a node's parameters to a YAML file.
    ros2 param dump <node_name> > <file_name>.yaml
  • Load: Load parameters from a file to a running node.
    ros2 param load <node_name> <file_name>.yaml
  • Load on Startup: Start a node with parameters from a file.
    ros2 run <package_name> <executable_name> --ros-args --params-file <file_name>.yaml

Actions

Listing Actions with Types

ros2 action list -t

Viewing Action Interface

ros2 interface show turtlesim/action/RotateAbsolute

Which will return:

# The desired heading in radians
float32 theta
---
# The angular displacement in radians to the starting position
float32 delta
---
# The remaining rotation in radians
float32 remaining

Sending an Action Goal

ros2 action send_goal <action_name> <action_type> "<goal>"

Example:

ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}"

To get feedback while the goal is running:

ros2 action send_goal --feedback <action_name> <action_type> "<goal>"

Rosbag

Recording Data

Record all data on a specific topic:

ros2 bag record <topic_name>

To record services:

ros2 bag record --all-services

Inspecting a Bag File

ros2 bag info <bag_file_name>

Replaying Data

ros2 bag play <bag_file_name>

To replay service requests:

ros2 bag play --publish-service-requests <bag_file_name>

URDF and SDF

Converting Between Formats

  • XACRO to URDF:
    xacro your_robot.xacro > your_robot.urdf
  • URDF to SDF:
    gz sdf -p your_model.urdf > your_model.sdf

TF (Transformations)

Visualizing the TF Tree

  • Generate a PDF:
    ros2 run tf2_tools view_frames
  • Use RQT:
    ros2 run rqt_tf_tree rqt_tf_tree

🏠 Home