1. Home
  2. /
  3. Ros-ros2
  4. /
  5. ROS2 Service Node (Python)

ROS2 Service Node (Python) - Ros-ros2 Typing CST Test

Loading…

ROS2 Service Node (Python) — Ros-ros2 Code

A ROS2 service that responds to requests with a greeting message.

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()

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.

More Ros-ros2 Typing Exercises

ROS2 Publisher Node (Python)ROS2 Subscriber Node (C++)

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher