Skip to content

Instantly share code, notes, and snippets.

@tejashah88
Last active March 12, 2024 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tejashah88/dd8959686da1715d0854525149cdf811 to your computer and use it in GitHub Desktop.
Save tejashah88/dd8959686da1715d0854525149cdf811 to your computer and use it in GitHub Desktop.

Part 1 - Preparing the workspace

  1. Install needed tools
sudo apt install python3-colcon-common-extensions -y
sudo apt install python3-rosdep2 -y

# !!! NEW Needed Tools !!!
sudo apt install python3-pip
pip install setuptools==58.2.0
  1. Create a new directory for your workspace and "change directory" (cd) to it in terminal
  • NOTE: The name of the source directory can be anything but we're making it ros2_ws for this tutorial.
mkdir -p ~/ros2_helloworld_ws/src
cd ~/ros2_helloworld_ws/src
  1. Download the example starter package
git clone https://github.com/ros/ros_tutorials.git -b humble
  1. Resolve dependencies
cd ..
rosdep update
rosdep install -i --from-path src --rosdistro humble -y
  1. Build the workspace
colcon build

You'll see a message along the lines of "All required rosdeps installed successfully"

  1. In a brand new terminal window, run the following to start up the TurtleSim
cd ~/ros2_helloworld_ws
source install/local_setup.bash
ros2 run turtlesim turtlesim_node
  1. In a brand new terminal window, run the following to control the turtle in TurtleSim
cd ~/ros2_helloworld_ws
source install/local_setup.bash
ros2 run turtlesim turtle_teleop_key

Part 2 - Setting up the project

  1. Open a new terminal and source the workspace
cd ~/ros2_helloworld_ws
source install/local_setup.bash
  1. Create a new package
cd src
ros2 pkg create --build-type ament_python ros2_hello_world
  1. Create two files, one for the publisher and one for the subscriber
cd ros2_hello_world
touch ros2_hello_world/talker_node.py
touch ros2_hello_world/listener_node.py
  1. Open VSCode within the working directory
code .
  1. In talker_node.py, copy and paste the following template code. Fill out both functions below.
import rclpy
from rclpy.node import Node

from std_msgs.msg import String


class TalkerNode(Node):
    def __init__(self):
        super().__init__('talker_node')
        self.publisher = self.create_publisher(String, 'chatter', 10)
        
        timer_period = 1.0
        self.timer = self.create_timer(timer_period, self.timer_callback)

        self.i = 0

    def timer_callback(self):
        message = String()
        message.data = f'Hello World: {self.i}'

        self.publisher.publish(message)
        self.i += 1


def main(args=None):
    rclpy.init(args=args)
    
    # Initialize the node and start it up
    talker_node = TalkerNode()
    rclpy.spin(talker_node)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically when the garbage collector destroys the node object)
    talker_node.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
  1. In listener_node.py, copy and paste the following template code. Fill out both functions below.
import rclpy
from rclpy.node import Node

from std_msgs.msg import String


class ListenerNode(Node):
    def __init__(self):
        super().__init__('listener_node')
        self.subscription = self.create_subscription(String, 'chatter', self.listener_callback, 10)
    

    def listener_callback(self, msg):
        print(msg.data)


def main(args=None):
    rclpy.init(args=args)
    
    # Initialize the node and start it up
    listener_node = ListenerNode()
    rclpy.spin(listener_node)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically when the garbage collector destroys the node object)
    listener_node.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
  1. In ros2_hello_world/package.xml, add the following lines underneath <build_type>ament_python</build_type>
<exec_depend>rclpy</exec_depend>
<exec_depend>std_msgs</exec_depend>

image

  1. In ros2_hello_world/setup.py, edit the following lines.
entry_points={
        'console_scripts': [
                'talker = ros2_hello_world.talker_node:main',
                'listener = ros2_hello_world.listener_node:main',
        ],
},

image

  1. Update the ROS package dependencies from your workspace
cd ~/ros2_helloworld_ws
rosdep install -i --from-path src --rosdistro humble -y
  1. Build your package
colcon build --packages-select ros2_hello_world
  1. In a brand new terminal window, run the following to start up the talker node
cd ~/ros2_helloworld_ws
source install/local_setup.bash
ros2 run ros2_hello_world talker
  1. In a brand new terminal window, run the following to start up the listener node
cd ~/ros2_helloworld_ws
source install/local_setup.bash
ros2 run ros2_hello_world listener
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment