|
| 1 | +"""Handles all communication with the PX4 autopilot using px4_msgs.""" |
| 2 | +from px4_msgs.msg import OffboardControlMode, TrajectorySetpoint, VehicleCommand, VehicleStatus |
| 3 | + |
| 4 | +from rclpy.qos import DurabilityPolicy, HistoryPolicy, QoSProfile, ReliabilityPolicy |
| 5 | + |
| 6 | + |
| 7 | +class PX4Communication: |
| 8 | + """PX4Communication.""" |
| 9 | + |
| 10 | + def __init__(self, node): |
| 11 | + """ |
| 12 | + Initialize the PX4 communication handler. |
| 13 | +
|
| 14 | + Args: |
| 15 | + node: The ROS2 node that will handle the publishers and subscribers |
| 16 | + """ |
| 17 | + self.node = node |
| 18 | + |
| 19 | + # Initialize variables |
| 20 | + self.vehicle_status = None |
| 21 | + self._last_nav_state = None |
| 22 | + |
| 23 | + px4_qos = QoSProfile( |
| 24 | + reliability=ReliabilityPolicy.BEST_EFFORT, |
| 25 | + durability=DurabilityPolicy.TRANSIENT_LOCAL, |
| 26 | + history=HistoryPolicy.KEEP_LAST, |
| 27 | + depth=1 |
| 28 | + ) |
| 29 | + |
| 30 | + self.vehicle_command_publisher = self.node.create_publisher( |
| 31 | + VehicleCommand, 'fmu/in/vehicle_command', px4_qos) |
| 32 | + self.offboard_control_mode_publisher = self.node.create_publisher( |
| 33 | + OffboardControlMode, 'fmu/in/offboard_control_mode', px4_qos) |
| 34 | + self.trajectory_setpoint_publisher = self.node.create_publisher( |
| 35 | + TrajectorySetpoint, 'fmu/in/trajectory_setpoint', px4_qos) |
| 36 | + |
| 37 | + self.vehicle_status_subscriber = self.node.create_subscription( |
| 38 | + VehicleStatus, 'fmu/out/vehicle_status_v1', self.vehicle_status_callback, |
| 39 | + px4_qos) |
| 40 | + |
| 41 | + def vehicle_status_callback(self, msg): |
| 42 | + """Update vehicle status.""" |
| 43 | + self.vehicle_status = msg |
| 44 | + # Log navigation state changes |
| 45 | + if self._last_nav_state is None or self._last_nav_state != msg.nav_state: |
| 46 | + nav_states = { |
| 47 | + 0: 'MANUAL', |
| 48 | + 1: 'ALTCTL', |
| 49 | + 2: 'POSCTL', |
| 50 | + 3: 'AUTO_MISSION', |
| 51 | + 4: 'AUTO_LOITER', |
| 52 | + 5: 'AUTO_RTL', |
| 53 | + 14: 'OFFBOARD', |
| 54 | + 17: 'AUTO_TAKEOFF', |
| 55 | + 18: 'AUTO_LAND' |
| 56 | + } |
| 57 | + current_state = nav_states.get(msg.nav_state, f'UNKNOWN({msg.nav_state})') |
| 58 | + self.node.get_logger().info(f'Navigation state changed to: {current_state}') |
| 59 | + self._last_nav_state = msg.nav_state |
| 60 | + |
| 61 | + # pylint: disable=unknown-option-value too-many-positional-arguments too-many-arguments |
| 62 | + def send_command(self, command, |
| 63 | + param1=float('nan'), param2=float('nan'), param3=float('nan'), param4=float('nan'), |
| 64 | + param5=float('nan'), param6=float('nan'), param7=float('nan')): |
| 65 | + """Send a vehicle command.""" |
| 66 | + vehicle_command = VehicleCommand() |
| 67 | + vehicle_command.timestamp = int(self.node.get_clock().now().nanoseconds / 1000) |
| 68 | + vehicle_command.command = command |
| 69 | + vehicle_command.param1 = param1 |
| 70 | + vehicle_command.param2 = param2 |
| 71 | + vehicle_command.param3 = param3 |
| 72 | + vehicle_command.param4 = param4 |
| 73 | + vehicle_command.param5 = param5 |
| 74 | + vehicle_command.param6 = param6 |
| 75 | + vehicle_command.param7 = param7 |
| 76 | + vehicle_command.target_system = 1 |
| 77 | + vehicle_command.target_component = 1 |
| 78 | + vehicle_command.source_system = 1 |
| 79 | + vehicle_command.source_component = 1 |
| 80 | + vehicle_command.from_external = True |
| 81 | + |
| 82 | + self.vehicle_command_publisher.publish(vehicle_command) |
| 83 | + |
| 84 | + def arm_vehicle(self): |
| 85 | + """Send arm command to vehicle.""" |
| 86 | + self.send_command(VehicleCommand.VEHICLE_CMD_COMPONENT_ARM_DISARM, param1=1.0) |
| 87 | + |
| 88 | + def set_offboard_mode(self): |
| 89 | + """Enable offboard control mode by publishing OffboardControlMode message.""" |
| 90 | + # Send offboard control mode with velocity control enabled |
| 91 | + offboard_control_mode = OffboardControlMode() |
| 92 | + offboard_control_mode.timestamp = int(self.node.get_clock().now().nanoseconds / 1000) |
| 93 | + offboard_control_mode.position = False |
| 94 | + offboard_control_mode.velocity = True # We want to control velocity |
| 95 | + offboard_control_mode.acceleration = False |
| 96 | + offboard_control_mode.attitude = False |
| 97 | + offboard_control_mode.body_rate = False |
| 98 | + offboard_control_mode.thrust_and_torque = False |
| 99 | + offboard_control_mode.direct_actuator = False |
| 100 | + |
| 101 | + self.offboard_control_mode_publisher.publish(offboard_control_mode) |
| 102 | + |
| 103 | + def send_offboard_mode_command(self): |
| 104 | + """Send VEHICLE_CMD_DO_SET_MODE command to switch to offboard mode.""" |
| 105 | + # Send command to set offboard mode (Base mode: 4, Custom main mode: 6) |
| 106 | + # Base mode: 4 = MAV_MODE_FLAG_CUSTOM_MODE_ENABLED |
| 107 | + # Custom main mode: 6 = PX4_CUSTOM_MAIN_MODE_OFFBOARD |
| 108 | + self.send_command( |
| 109 | + VehicleCommand.VEHICLE_CMD_DO_SET_MODE, |
| 110 | + param1=1.0, # Base mode (1=enable custom, 0=use system defaults) |
| 111 | + param2=6.0, # Custom main mode (6 = OFFBOARD) |
| 112 | + param3=0.0 # Custom sub mode (not used for offboard) |
| 113 | + ) |
| 114 | + |
| 115 | + def send_takeoff_command(self, altitude): |
| 116 | + """Send takeoff command to vehicle.""" |
| 117 | + self.send_command(VehicleCommand.VEHICLE_CMD_NAV_TAKEOFF, param7=altitude) |
| 118 | + |
| 119 | + def send_zero_velocity_setpoint(self): |
| 120 | + """Send zero velocity setpoint.""" |
| 121 | + trajectory_setpoint = TrajectorySetpoint() |
| 122 | + trajectory_setpoint.timestamp = int(self.node.get_clock().now().nanoseconds / 1000) |
| 123 | + |
| 124 | + # Set zero velocity |
| 125 | + trajectory_setpoint.velocity[0] = 0.0 |
| 126 | + trajectory_setpoint.velocity[1] = 0.0 |
| 127 | + trajectory_setpoint.velocity[2] = 0.0 |
| 128 | + |
| 129 | + # Keep position and acceleration as NaN to not control them |
| 130 | + trajectory_setpoint.position[0] = float('nan') |
| 131 | + trajectory_setpoint.position[1] = float('nan') |
| 132 | + trajectory_setpoint.position[2] = float('nan') |
| 133 | + |
| 134 | + trajectory_setpoint.acceleration[0] = float('nan') |
| 135 | + trajectory_setpoint.acceleration[1] = float('nan') |
| 136 | + trajectory_setpoint.acceleration[2] = float('nan') |
| 137 | + |
| 138 | + trajectory_setpoint.yaw = float('nan') # Don't control yaw |
| 139 | + trajectory_setpoint.yawspeed = float('nan') |
| 140 | + |
| 141 | + self.trajectory_setpoint_publisher.publish(trajectory_setpoint) |
| 142 | + |
| 143 | + def send_velocity_setpoint(self, velocity): |
| 144 | + """Send constant velocity setpoint.""" |
| 145 | + trajectory_setpoint = TrajectorySetpoint() |
| 146 | + trajectory_setpoint.timestamp = int(self.node.get_clock().now().nanoseconds / 1000) |
| 147 | + |
| 148 | + # Set constant velocity |
| 149 | + trajectory_setpoint.velocity[0] = float(velocity[0]) |
| 150 | + trajectory_setpoint.velocity[1] = float(velocity[1]) |
| 151 | + trajectory_setpoint.velocity[2] = float(velocity[2]) |
| 152 | + |
| 153 | + # Keep position and acceleration as NaN to not control them |
| 154 | + trajectory_setpoint.position[0] = float('nan') |
| 155 | + trajectory_setpoint.position[1] = float('nan') |
| 156 | + trajectory_setpoint.position[2] = float('nan') |
| 157 | + |
| 158 | + trajectory_setpoint.acceleration[0] = float('nan') |
| 159 | + trajectory_setpoint.acceleration[1] = float('nan') |
| 160 | + trajectory_setpoint.acceleration[2] = float('nan') |
| 161 | + |
| 162 | + trajectory_setpoint.yaw = float('nan') # Don't control yaw |
| 163 | + trajectory_setpoint.yawspeed = float('nan') |
| 164 | + |
| 165 | + self.trajectory_setpoint_publisher.publish(trajectory_setpoint) |
0 commit comments