小
控制测试
#!/usr/bin/env python3
from std_msgs.msg import Float32
from std_msgs.msg import Int32
import rclpy
from rclpy.node import Node
class NodeSubscribe02(Node):
def __init__(self,name):
super().__init__(name)
self.get_logger().info("大家好,我是%s!" % name)
# 创建订阅者
self.command_subscribe_ = self.create_subscription(Float32,"distance",self.command_callback,10)
self.command_publisher_ = self.create_publisher(Int32,"servo_angle", 10)
def command_callback(self,msg):
angle = 0.0
if msg.data>0.20:
angle = 0
else:
angle = 90
self.get_logger().info(f'收到距离:[{msg.data}],发送角度:{angle}')
pub_msg = Int32()
pub_msg.data = angle
self.command_publisher_.publish(pub_msg)
def main(args=None):
rclpy.init(args=args) # 初始化rclpy
node = NodeSubscribe02("topic_subscribe_02") # 新建一个节点
rclpy.spin(node) # 保持节点运行,检测是否收到退出指令(Ctrl+C)
rclpy.shutdown() # 关闭rclpy
main()