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)