#!/usr/bin/env pythonimportrclpyfromrclpy.nodeimportNodefromexample_interfaces.msgimportStringclassSmartphoneNode(Node):def__init__(self):super().__init__("smartphone")self.subscriber_=self.create_subscription(String,'robot_news',self.callback_robot_news,10)self.get_logger().info("Smartphone has been starte.")defcallback_robot_news(self,msg):self.get_logger().info(msg.data)defmain(args=None):rclpy.init(args=args)node=SmartphoneNode()rclpy.spin(node)rclpy.shutdown()if__name__=='__main__':main()
#include "rclcpp/rclcpp.hpp"#include "example_interfaces/msg/string.hpp"classSmartphoneNode:publicrclcpp::Node{public:SmartphoneNode():Node("smartphone"){subscriber_=this->create_subscription<example_interfaces::msg::String>("robot_news",10,std::bind(&SmartphoneNode::callbackRobotNews,this,std::placeholders::_1));RCLCPP_INFO(this->get_logger(),"Smartphone has been started.");}private:voidcallbackRobotNews(constexample_interfaces::msg::String::SharedPtrmsg){RCLCPP_INFO(this->get_logger(),"%s",msg->data.c_str());}rclcpp::Subscription<example_interfaces::msg::String>::SharedPtrsubscriber_;};intmain(intargc,char**argv){rclcpp::init(argc,argv);autonode=std::make_shared<SmartphoneNode>();rclcpp::spin(node);rclcpp::shutdown();return0;}