Sample for the ROS2 101 tutorial

Home   »   Sample for the ROS2 101 tutorial

from std_srvs.srv import Trigger
from std_msgs.msg import String

import rclpy
from rclpy.node import Node


class FancyService(Node):

	def __init__(self):
    	super().__init__('fancy_service')
    	self._service = self.create_service(Trigger, 'trigger_the_print', self.trigger_callback)

    	self._string_sub = self.create_subscription(String, 'thing_to_print', self.string_callback, 1)

    	self._last_string = "Nothing received!"

	def trigger_callback(self, request, response):
    	self.get_logger().info("Last received string was {}".format(self._last_string))

    	if self._last_string == 'fail':
        	response.success = False

    	return response

	def string_callback(self, string_msg):
    	self._last_string = string_msg.data


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

	fancy_service = FancyService()

	rclpy.spin(fancy_service)

	rclpy.shutdown()


if __name__ == '__main__':
	main()

Leave a Reply

Your email address will not be published. Required fields are marked *