Learn Ros-ros2 - 3 Code Examples & CST Typing Practice Test
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.
View all 3 Ros-ros2 code examples →
Learn ROS-ROS2 with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
ROS2 Publisher Node (Python)
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()
A simple ROS2 publisher node that sends messages to a topic.
ROS2 Subscriber Node (C++)
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>
class MinimalSubscriber : public rclcpp::Node {
public:
MinimalSubscriber() : Node("minimal_subscriber") {
subscription_ = this->create_subscription<std_msgs::msg::String>(
"topic", 10,
std::bind(&MinimalSubscriber::topic_callback, this, std::placeholders::_1));
}
private:
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const {
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
int main(int argc, char * argv[]) {
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalSubscriber>());
rclcpp::shutdown();
return 0;
}
A simple ROS2 subscriber node that listens to messages from a topic.
ROS2 Service Node (Python)
import rclpy
from rclpy.node import Node
from example_interfaces.srv import AddTwoInts
class MinimalService(Node):
def __init__(self):
super().__init__('minimal_service')
self.srv = self.create_service(AddTwoInts, 'add_two_ints', self.add_two_ints_callback)
def add_two_ints_callback(self, request, response):
response.sum = request.a + request.b
self.get_logger().info('Incoming request: a=%d, b=%d' % (request.a, request.b))
return response
def main(args=None):
rclpy.init(args=args)
node = MinimalService()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
A ROS2 service that responds to requests with a greeting message.
Frequently Asked Questions about Ros-ros2
What is Ros-ros2?
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.
What are the primary use cases for Ros-ros2?
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
What are the strengths of Ros-ros2?
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
What are the limitations of Ros-ros2?
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
How can I practice Ros-ros2 typing speed?
CodeSpeedTest offers 3+ real Ros-ros2 code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.