ros2 hello

发布时间:2024年01月20日
import rclpy  
from rclpy.node import Node  
from geometry_msgs.msg import Twist  

def listener_callback( msg):  
    print("I heard: [%s]" % msg.linear.x)  

rclpy.init(args=None)  
node = Node('la')
subscription = node.create_subscription(  
            Twist,  
            '/myself',  
            listener_callback,  
            10)  
rclpy.spin(node)  
import rclpy  
from rclpy.node import Node  
from geometry_msgs.msg import Twist  
  
class MyPublisherNode(Node):  
    def __init__(self):  
        super().__init__('my_publisher_node')  
        self.publisher = self.create_publisher(Twist, '/myself', 10)  
        self.timer = self.create_timer(1.0, self.timer_callback)  
  
    def timer_callback(self):  
        twist = Twist()  
        twist.linear.x = 1.0  # 设置你想要发布的线性速度x分量值  
        twist.angular.z = 1.0  # 设置你想要发布的角速度z分量值  
        self.publisher.publish(twist)  
  
def main(args=None):  
    rclpy.init(args=args)  
    node = MyPublisherNode()  
    rclpy.spin(node)  
  
if __name__ == '__main__':  
    main()
import rclpy  
from rclpy.node import Node  
from geometry_msgs.msg import Twist  
  
class MyNode(Node):  
    def __init__(self):  
        super().__init__('my_subscriber_node')  
        self.subscription = self.create_subscription(  
            Twist,  
            '/myself',  
            self.listener_callback,  
            10)  
  
    def listener_callback(self, msg):  
        print("I heard: [%s]" % msg.linear.x)  
  
 
rclpy.init(args=args)  
node = MyNode()  
rclpy.spin(node)  

文章来源:https://blog.csdn.net/CAIYUNFREEDOM/article/details/135699458
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。