Skip to content

MilanM002005/ros_ws

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

ROS 2 Workspace (ros_ws)

What is ROS 2?

ROS 2 (Robot Operating System 2) is an open-source middleware framework used for building robotic applications.

ROS 2 provides:

  • Hardware abstraction
  • Device drivers
  • Communication between processes
  • Package management
  • Simulation tools
  • Visualization tools

Advantages of ROS 2

  • Real-time support
  • Multi-platform compatibility
  • DDS-based communication
  • Improved security
  • Better performance for distributed systems

ROS 2 Architecture

ROS 2 consists of:

1. Nodes

A node is an executable that performs a specific task.

Examples:

  • Camera Node
  • Motor Controller Node
  • Sensor Node

2. Topics

Topics enable asynchronous communication between nodes.

Example:

Camera Node ---> /image_raw ---> Image Processing Node

3. Publisher

A node that sends data to a topic.

4. Subscriber

A node that receives data from a topic.

5. Services

Request-response communication.

Example:

Client --> Request
Server --> Response

6. Actions

Long-running tasks with feedback.

Example:

  • Robot Navigation
  • Path Planning

Creating a ROS 2 Workspace

mkdir -p ~/ros_ws/src
cd ~/ros_ws

colcon build

source install/setup.bash

ROS 2 Publisher Example

Create a package:

cd ~/ros_ws/src

ros2 pkg create --build-type ament_python pub_sub_demo

publisher.py

import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class PublisherNode(Node):

    def __init__(self):
        super().__init__('publisher_node')

        self.publisher_ = self.create_publisher(
            String,
            'chatter',
            10
        )

        self.timer = self.create_timer(
            1.0,
            self.publish_message
        )

    def publish_message(self):
        msg = String()
        msg.data = "Hello from ROS 2 Publisher"
        self.publisher_.publish(msg)

        self.get_logger().info(
            f'Publishing: {msg.data}'
        )

def main(args=None):
    rclpy.init(args=args)

    node = PublisherNode()

    rclpy.spin(node)

    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

ROS 2 Subscriber Example

subscriber.py

import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class SubscriberNode(Node):

    def __init__(self):
        super().__init__('subscriber_node')

        self.subscription = self.create_subscription(
            String,
            'chatter',
            self.listener_callback,
            10
        )

    def listener_callback(self, msg):
        self.get_logger().info(
            f'Received: {msg.data}'
        )

def main(args=None):
    rclpy.init(args=args)

    node = SubscriberNode()

    rclpy.spin(node)

    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

Producer-Consumer Example

The ROS 2 Publisher-Subscriber model is a practical implementation of the Producer-Consumer pattern.

Producer

Produces data continuously.

msg.data = "Sensor Reading"
publisher.publish(msg)

Consumer

Consumes data whenever available.

def callback(msg):
    print(msg.data)

Workflow

Producer (Publisher)
          |
          v
       Topic
          |
          v
Consumer (Subscriber)

Benefits:

  • Loose coupling
  • Scalability
  • Real-time communication
  • Distributed processing

Useful ROS 2 Commands

List Nodes

ros2 node list

List Topics

ros2 topic list

Echo Topic Data

ros2 topic echo /chatter

View Topic Information

ros2 topic info /chatter

List Services

ros2 service list

TurtleSim Tutorial

TurtleSim is a lightweight simulator used to learn ROS concepts.


Step 1: Install TurtleSim

sudo apt update

sudo apt install ros-humble-turtlesim

Verify installation:

ros2 pkg executables turtlesim

Step 2: Start TurtleSim

Terminal 1:

ros2 run turtlesim turtlesim_node

You should see a window containing a turtle.


Step 3: Control Turtle Using Keyboard

Terminal 2:

ros2 run turtlesim turtle_teleop_key

Controls:

Arrow Up    -> Forward
Arrow Down  -> Backward
Arrow Left  -> Turn Left
Arrow Right -> Turn Right

Step 4: View Active Nodes

ros2 node list

Output:

/turtlesim
/teleop_turtle

Step 5: View Topics

ros2 topic list

Important Topics:

/turtle1/cmd_vel
/turtle1/pose
/turtle1/color_sensor

Step 6: Monitor Turtle Position

ros2 topic echo /turtle1/pose

This displays:

  • X coordinate
  • Y coordinate
  • Orientation
  • Velocity

Step 7: Move Turtle from Terminal

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

The turtle will move automatically.


Step 8: Spawn Another Turtle

ros2 service call /spawn turtlesim/srv/Spawn \
"{x: 5.0, y: 5.0, theta: 0.0, name: 'turtle2'}"

Step 9: Clear Screen

ros2 service call /clear std_srvs/srv/Empty "{}"

Step 10: Reset Simulator

ros2 service call /reset std_srvs/srv/Empty "{}"

ROS 2 Learning Roadmap

Beginner

  • ROS 2 Installation
  • Workspace Setup
  • Nodes
  • Topics
  • Publishers
  • Subscribers
  • TurtleSim

Intermediate

  • Services
  • Actions
  • Parameters
  • Launch Files
  • Custom Messages

Advanced

  • Navigation Stack
  • SLAM
  • Gazebo Simulation
  • MoveIt
  • Multi-Robot Systems
  • ROS 2 Control

Repository Structure

ros_ws/
│
├── src/
│   ├── pub_sub_demo/
│   ├── turtlesim_examples/
│   └── custom_packages/
│
├── build/
├── install/
├── log/
│
└── README.md

References

Official ROS 2 Documentation: https://docs.ros.org

TurtleSim Package: https://github.com/ros/ros_tutorials


Happy Learning ROS 2 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors