Skip to content

Instantly share code, notes, and snippets.

@danic85
Last active January 23, 2023 20:52
Show Gist options
  • Save danic85/d0494d5ed881fe0a787718a9fbb15ce6 to your computer and use it in GitHub Desktop.
Save danic85/d0494d5ed881fe0a787718a9fbb15ce6 to your computer and use it in GitHub Desktop.
ROS2 on MacOS reference commands

Following config described here: https://www.kevsrobots.com/learn/learn_ros/00_intro.html

There is also a more detailed cheet sheet available here: https://www.theconstructsim.com/wp-content/uploads/2021/10/ROS2-Command-Cheat-Sheets-updated.pdf

Configure docker for display output

(to use GUI components)

References:

https://www.kevsrobots.com/learn/learn_ros/15_rviz.html

https://gist.github.com/cschiewek/246a244ba23da8b9f0e7b11a68bf3285

https://stackoverflow.com/questions/70722222/how-to-run-tkinter-inside-a-docker-container-on-macbook-pro

https://stackoverflow.com/questions/72586838/xquartz-cant-open-display-mac-os

docker-compose.yml

version: "3.9"
services:
  ros2:
    build: .
    network_mode: "host"
    ipc: host
    tty: true
    environment:
    - DISPLAY=192.168.1.135:0 # your IP (value of `/usr/sbin/ipconfig getifaddr en0`)
    volumes:
    - /tmp/.X11-unix:/tmp/.X11-unix # may not be needed

dockerfile

# This is an auto generated Dockerfile for ros:ros-base
# generated from docker_images_ros2/create_ros_image.Dockerfile.em
FROM ros:humble-ros-core-jammy

# install bootstrap tools
RUN apt-get update && apt-get install --no-install-recommends -y \
    build-essential \
    git \
    python3-colcon-common-extensions \
    python3-colcon-mixin \
    python3-rosdep \
    python3-vcstool \
    && rm -rf /var/lib/apt/lists/*

# bootstrap rosdep
RUN rosdep init && \
  rosdep update --rosdistro $ROS_DISTRO

# setup colcon mixin and metadata
RUN colcon mixin add default \
      https://raw.githubusercontent.com/colcon/colcon-mixin-repository/master/index.yaml && \
    colcon mixin update && \
    colcon metadata add default \
      https://raw.githubusercontent.com/colcon/colcon-metadata-repository/master/index.yaml && \
    colcon metadata update

# install ros2 packages
RUN apt-get update && apt-get install -y --no-install-recommends \
    ros-humble-desktop=0.10.0-1* 
RUN apt-get update
RUN apt-get install -y --no-install-recommends ros-humble-navigation2 ros-humble-nav2-bringup
RUN apt-get install nano -y --no-install-recommends
RUN sudo echo "source /opt/ros/humble/setup.bash" >> /root/.bashrc

Commands

Prerequisites

brew install --cask xquartz # reinstall if necessary

Rebuild and start container (from docker directory)

docker-compose build --no-cache
docker-compose up -d

Configuring Xquarts

Run Xquarts

Set your Mac IP address

IP=$(/usr/sbin/ipconfig getifaddr en0)

Allow connections from Mac to XQuartz

/opt/X11/bin/xhost + "$IP"

Run container

docker run -it -e DISPLAY="${IP}:0" --name docker-ros2 docker-ros2

Connect with additional terminals

docker exec -it docker-ros2 bash


Source ros files

source /opt/ros/humble/setup.bash only if dockerfile does not output this to .bashrc

Create workspace

cd /ros2
mkdir first_app
cd first_app
mkdir src
colcon build
ls

Create package 'my_py_pkg' (from ros2/ dir)

ros2 pkg create my_py_pkg --build-type ament_python --dependencies rclpy

Build package (from package directory)

colcon build

Update permissions on package files

chmod 777 -R my_py_pkg/

Source package files

source install/setup.bash

Run node 'pub' in package 'my_py_pkg'

ros2 run my_py_pkg pub

Echo a topic 'Hello'

ros2 topic echo Hello

RQT

rqt rqt_graph ros2 run rqt_console rqt_console

Nodes

List running nodes

ros2 node list

Node info

ros2 node info <node_name> ros2 node info /my_turtle

Topics

List topics

ros2 topic list [-t]

View topic messages

ros2 topic echo <topic_name>

View topic info

ros2 topic info <topic_name>

View message / interface type details (view structure of arguments)

ros2 interface show <msg type>

Messages

View frequency of messages published to topic

ros2 topic hz <topic_name>

Publish message to topic

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

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}}" Once

ros2 topic pub --rate 1 /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}}" Every second

Services

Service type info

ros2 service type <service_name>

List services

ros2 service list -t

Find services of type

ros2 service find <type_name>

Call service

ros2 service call <service_name> <service_type> <arguments>

ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: ''}"

Params

Type and current value of param

ros2 param get <node_name> <parameter_name>

Set param at runtime

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

ros2 param set /turtlesim background_r 150

Dump all node params to file / load

ros2 param dump <node_name> > <file_name>

ros2 param load <node_name> <file_name>

ros2 run <package_name> <executable_name> --ros-args --params-file <file_name> Load on startup

Actions

List / info

ros2 action list -t

ros2 action info <action_name>

Send goal

ros2 action send_goal <action_name> <action_type> <values> [--feedback]

Bags (record and play back messages)

ros2 bag record [-o <bag_file_name>] <topic(s)>

ros2 bag info <bag_file_name>

ros2 bag play <bag_file_name>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment