Blog Post 5
Published:
title: “ROS2 humble” excerpt: “ Docker file and docker build. “ —
mkdir ~/docker_rosHum
cd ~/docker_rosHum
touch Dockerfile
echo ‘FROM osrf/ros:humble-desktop-full\nARG USER=user\nARG DEBIAN_FRONTEND=noninteractive\nRUN apt-get update && apt-get install -y \\n && apt-get update \\n && apt-get upgrade -y \\n && apt-get install -y python3-pip \\n && apt-get install -y terminator \n\nENV SHELL /bin/bash\nUSER $USERNAME’ > Dockerfile
docker build ~/docker_rosHum ros-humble-container
mkdir ~/Volumes
docker run -it –privileged –rm –user=$(id -u $USER):$(id -g $USER) –env=”DISPLAY” –volume=”/etc/group:/etc/group:ro” –volume=”/etc/passwd:/etc/passwd:ro” –volume=”/etc/shadow:/etc/shadow:ro” –volume=”/etc/sudoers.d:/etc/sudoers.d:ro” –net host -v /home:/home -v ~/Volumes:/home/usr/ ros-humble-container
mkdir -p robot_ws/src
cd robot_ws
colcon build #(build log install)
cd src ros2 pkg create –build-type ament_python robot_py_codes ros2 pkg create –build-type ament_cmake robot_cpp_codes
cd ..
colcon build
#in a new terminal to source the environment to recognize our packages . install/setup.bash
Simple Publisher Python
create a new file simple_publisher.py inside src/robot_py_codes/robot_py_codes
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class SimplePublisher(Node):
def __init__(self):
super().__init__('simple_publisher')
self.pub_ = self.create_publisher(String , "chatter", 10)
self.counter_ = 0
self.frequency_ = 1.0
self.get_logger().info("Publishing at %d Hz" % self.frequency_)
self.timer = self.create_timer(self.frequency_, self.timerCallback)
def timerCallback(self):
msg = String()
msg.data = "Hello ROS 2 - counter: %d" % self.counter_
self.pub_.publish(msg)
self.counter_ += 1
def main():
rclpy.init()
simple_publisher = SimplePublisher()
rclpy.spin(simple_publisher)
simple_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
To execute the code
inside src/robot_py_codes/setup.py change
entry_points={ ‘console_scripts’: [ ], }, to entry_points={ ‘console_scripts’: [ ‘simple_publisher = bumperbot_py_examples.simple_publisher:main’ ], },
and
add the following lines to package.xml
then in terminal cd /robot_ws colcon build
new terminal . install/setup.bash
then ros2 run robot_py_codes simple_publisher
on another terminal ros2 topic echo /chatter