ROS2 Publisher Node (Python) - Ros-ros2 Typing CST Test
Loading…
ROS2 Publisher Node (Python) — Ros-ros2 Code
A simple ROS2 publisher node that sends messages to a topic.
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(String, 'topic', 10)
self.timer = self.create_timer(1.0, self.timer_callback)
def timer_callback(self):
msg = String()
msg.data = 'Hello ROS2'
self.publisher_.publish(msg)
self.get_logger().info('Publishing: %s' % msg.data)
def main(args=None):
rclpy.init(args=args)
node = MinimalPublisher()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()Ros-ros2 Language Guide
ROS (Robot Operating System) and ROS2 are open-source frameworks for robot software development. They provide libraries, tools, and conventions to simplify programming, communication, and control in robotics, supporting modularity, real-time systems, and hardware abstraction.
Primary Use Cases
- ▸Robot perception, navigation, and control
- ▸Multi-robot coordination and communication
- ▸Simulation and testing of robotic systems
- ▸Integration with sensors, actuators, and middleware
- ▸Development of autonomous systems and AI robotics
Notable Features
- ▸Node-based modular architecture
- ▸Communication via topics, services, and actions
- ▸Hardware abstraction for diverse robot platforms
- ▸Simulation and visualization integration
- ▸Cross-platform support in ROS2 (Linux, Windows, macOS)
Origin & Creator
ROS was created by the Stanford AI Lab and Willow Garage around 2007-2009. ROS2 development started by Open Robotics in 2014 to address ROS1 limitations in real-time, security, and cross-platform support.
Industrial Note
ROS2 is increasingly adopted in industrial automation, autonomous vehicles, and robotics middleware due to its support for real-time deterministic systems and secure communication.
Quick Explain
- ▸ROS uses a node-based architecture where each node performs a specific task, communicating via topics, services, and actions.
- ▸ROS2 improves on ROS1 by adding real-time capabilities, DDS-based communication, and better security and multi-robot support.
- ▸Both ROS and ROS2 abstract hardware through device drivers and hardware interfaces, allowing platform-independent programming.
- ▸Supports simulation, visualization, and testing through tools like Gazebo, RViz, and rqt.
- ▸Widely used in academic research, industrial robotics, autonomous vehicles, and robotic middleware development.
Core Features
- ▸Pub-sub messaging system for asynchronous communication
- ▸Service calls for synchronous operations
- ▸Action interfaces for long-running tasks
- ▸Parameter server and configuration management
- ▸Integration with Gazebo, RViz, and other visualization/simulation tools
Learning Path
- ▸Learn ROS1 basics: nodes, topics, services
- ▸Transition to ROS2: DDS, QoS, real-time concepts
- ▸Understand message types and parameter management
- ▸Work with simulation (Gazebo) and visualization (RViz)
- ▸Build multi-robot and hardware-integrated applications
Practical Examples
- ▸Simulate a robot in Gazebo and control it via ROS2 nodes
- ▸Subscribe to a camera topic and process images
- ▸Use LIDAR data for obstacle avoidance
- ▸Implement a navigation stack for autonomous movement
- ▸Coordinate multiple robots using ROS2 DDS communication
Comparisons
- ▸ROS1 vs ROS2: ROS2 adds real-time support, DDS communication, and security
- ▸ROS vs custom C++ robotics frameworks: ROS provides standard middleware and ecosystem
- ▸ROS2 vs microcontroller firmware: ROS2 handles high-level logic; low-level drivers run on embedded firmware
- ▸ROS2 vs proprietary robot SDKs: ROS2 is open-source, modular, and flexible
- ▸ROS2 vs MATLAB Robotics Toolbox: ROS2 is runtime, open-source middleware; MATLAB is simulation and prototyping
Strengths
- ▸Rapid prototyping of robotic applications
- ▸Hardware-independent software development
- ▸Large ecosystem of packages and drivers
- ▸Community support and documentation
- ▸Scalable from research to industrial-grade robotics
Limitations
- ▸ROS1 is not fully real-time and lacks security features
- ▸ROS2 requires familiarity with DDS and real-time concepts
- ▸Steeper learning curve for beginners in robotics
- ▸Debugging distributed systems can be complex
- ▸Some packages may not be fully migrated from ROS1 to ROS2
When NOT to Use
- ▸For extremely resource-limited microcontrollers without OS support
- ▸When minimal latency is required and middleware overhead is unacceptable
- ▸If project requires only single-node, simple control without modularity
- ▸When legacy codebase is incompatible and migration is infeasible
- ▸For hobby projects with no network or multi-device communication needs
Cheat Sheet
- ▸ros2 run <package> <node> - run a ROS2 node
- ▸ros2 topic list - list active topics
- ▸ros2 topic echo <topic> - view messages on a topic
- ▸ros2 service call <service> <args> - call a service
- ▸colcon build - build workspace
FAQ
- ▸Do I need a license for ROS? -> ROS is open-source under BSD licenses.
- ▸Can ROS2 run on Windows? -> Yes, ROS2 supports Linux, Windows, and macOS.
- ▸Is ROS2 real-time? -> It can be, with proper DDS QoS settings and RTOS usage.
- ▸Can ROS2 communicate with ROS1 nodes? -> Yes, via ROS1-ROS2 bridges.
- ▸Which languages can I use with ROS2? -> Primarily Python and C++.
30-Day Skill Plan
- ▸Week 1: ROS1 nodes and topic communication
- ▸Week 2: ROS2 node migration and DDS understanding
- ▸Week 3: Sensor integration and driver usage
- ▸Week 4: Simulation and visualization tools
- ▸Week 5: Multi-robot coordination and deployment
Final Summary
- ▸ROS and ROS2 provide modular, hardware-independent frameworks for robotic software development.
- ▸ROS2 extends ROS1 with real-time, security, and multi-platform support.
- ▸They enable easy simulation, visualization, and deployment across platforms.
- ▸Large ecosystem of packages and drivers simplifies robotics development.
- ▸Ideal for research, prototyping, and industrial robotic applications.
Project Structure
- ▸src/ - source code of nodes
- ▸msg/ - custom message definitions
- ▸srv/ - custom service definitions
- ▸launch/ - launch files for starting multiple nodes
- ▸CMakeLists.txt & package.xml - build configuration and dependencies
Monetization
- ▸Industrial automation solutions with ROS2
- ▸Robotics consulting and prototyping
- ▸Autonomous vehicle software development
- ▸ROS2 training and certification
- ▸Simulation and testing services for robotic systems
Productivity Tips
- ▸Use launch files to run multiple nodes efficiently
- ▸Leverage ROS2 lifecycle nodes for clean startup/shutdown
- ▸Use ROS2 composition for reduced overhead
- ▸Containerize workspaces for reproducibility
- ▸Document and modularize packages for easier collaboration
Basic Concepts
- ▸Nodes - independent processes performing tasks
- ▸Topics - asynchronous message communication
- ▸Services - synchronous request/response calls
- ▸Actions - preemptable long-running tasks
- ▸Parameters - runtime configuration values