|
举个例(假设使用的是ROS2和PX4)
- import rclpy
- from rclpy.node import Node
- from mavros_msgs.srv import SetMode
- from mavros_msgs.msg import CommandBool, State
- class FixedWingController(Node):
- def __init__(self):
- super().__init__('fixed_wing_controller')
- self.set_mode_client = self.create_client(SetMode, '/mavros/set_mode')
- while not self.set_mode_client.wait_for_service(timeout_sec=1.0):
- self.get_logger().info('service not available, waiting again...')
- self.state_subscriber = self.create_subscription(State, '/mavros/state', self.state_callback, 10)
- def set_auto_takeoff(self):
- req = SetMode.Request()
- req.custom_mode = 'AUTO.TAKEOFF'
- self.set_mode_client.call_async(req)
- def state_callback(self, msg):
- self.get_logger().info(f"Current State: {msg.mode}")
- # 初始化ROS 2节点
- rclpy.init(args=None)
- node = FixedWingController()
- node.set_auto_takeoff()
- rclpy.spin(node)
- node.destroy_node()
- rclpy.shutdown()
复制代码 |
|