px4 offboard位置模式速度
时间: 2025-06-28 19:23:57 浏览: 20
### PX4 Offboard Mode Position and Velocity Control
In the context of PX4, offboard mode allows external systems to send setpoints (such as position, velocity, attitude) directly to the flight controller. For setting up or controlling position and velocity specifically under this mode, several key points must be considered.
To enable offboard control over position and velocity within PX4, one needs to ensure that MAVLink messages are correctly configured and sent from an external computer running a companion application like QGroundControl or custom scripts using libraries such as pymavlink[^1]. The process involves transitioning into offboard mode after establishing communication between ground station software and the vehicle's autopilot system through MAVLink protocol.
For precise manipulation of these parameters:
- **Position Setpoint**: Utilize `SET_POSITION_TARGET_LOCAL_NED` message type which includes fields for local North-East-Down coordinates along with yaw angle.
- **Velocity Setpoint**: Similarly, use `SET_POSITION_TARGET_LOCAL_NED`, but populate only the velocities components (`vx`, `vy`, `vz`) while leaving coordinate positions unset.
A Python script utilizing pymavlink can demonstrate sending both types of commands effectively by connecting to the drone via UDP/TCP connection established on port 14550 typically used for telemetry data exchange.
```python
from pymavlink import mavutil
# Connect to Vehicle
vehicle = mavutil.mavlink_connection('udp:localhost:14550')
def arm_and_takeoff(aTargetAltitude):
"""
Arms vehicle and fly to aTargetAltitude.
"""
print("Basic pre-arm checks")
# Don't let the user try to arm until autopilot is ready
while not vehicle.wait_heartbeat().base_mode & mavutil.mavlink.MAV_MODE_FLAG_SAFETY_ARMED:
print(" Waiting for vehicle to initialise...")
time.sleep(1)
print("Arming motors")
# Copter should arm in GUIDED mode
vehicle.set_mode('GUIDED')
vehicle.arming_check()
print("Taking off!")
vehicle.simple_takeoff(aTargetAltitude)
# Wait until altitude has been reached
while True:
alt = vehicle.location.global_relative_frame.alt
if abs(alt - aTargetAltitude) < 0.2:
print(f"Reached target altitude {aTargetAltitude}")
break
time.sleep(1)
print("Starting the mission")
arm_and_takeoff(10)
while True:
msg = vehicle.message_factory.set_position_target_local_ned_encode(
0,
0, 0,
mavutil.mavlink.MAV_FRAME_BODY_OFFSET_NED,
int(0b0000111111111000), # Use position setpoints
1, 0, 0, # X,Y,Z position NED(meters)
0, 0, 0, # Velocities m/s
0, 0, 0, # Accelerations m/s^2
0, 0 # Yaw, yaw rate(degrees/radians)
)
vehicle.send_mavlink(msg)
time.sleep(0.1)
```
This example demonstrates how to establish a simple take-off sequence followed by continuous transmission of position-based navigation instructions during offboard operation. Adjustments would need to be made depending upon specific requirements regarding speed limits, direction changes etc., all adhering closely to guidelines provided officially.
阅读全文
相关推荐


















