AutoMavCmd Interface Documentation¶
Introduction¶
Overview: This file provides MAVLink command encapsulation and control capabilities required for PX4 hardware-in-the-loop simulation mission planning, enabling delay waiting and custom timing management of drone mission commands.
In autonomous drone mission simulation, various MAVLink control commands must be sent to the flight controller in a specific sequence. This module is specifically designed for mission planning scenarios, encapsulating commonly used mission execution control logic and command-sending functionality into directly callable classes. It is suitable for users writing custom autonomous mission scripts, helping them conveniently orchestrate mission workflows, control command transmission timing, and achieve stable MAVLink command interaction with the PX4 flight controller.
Quick Start¶
Minimal runnable example; copy and modify minimal configuration to run.
from RflySimVision import VisionClient # Import vision client to obtain drone's mav object
from RflySimSDK.phm.AutoMavCmd import Command # Import main command class from this module
# 1. Connect to PX4 drone's mav object (connect to drone in RflySim simulation environment using default configuration)
vision = VisionClient()
mav = vision.get_vehicle() # Obtain the mav object of the first drone in the simulation
# 2. Initialize command control object
cmd = Command(mav=mav)
# 3. Arm the drone
cmd.Arm()
print("Drone armed")
# 4. Control quadcopter to fly to target position (x=0m, y=0m, z=2m)
target_position = [0, 0, 2]
cmd.QuadPos(pos=target_position)
print(f"Position command sent to target position {target_position}")
# Wait 10 seconds, then descend and disarm
import time
time.sleep(10)
# Optionally send position to ground and disarm
cmd.QuadPos([0, 0, 0])
time.sleep(5)
cmd.DisArm()
print("Drone disarmed; mission completed")
Environment and Dependencies¶
- Python Environment:
>= 3.8.10 - Dependencies:
UE4CtrlAPI,math,numpy,time - Prerequisites: Before calling this interface, RflySimSDK must be initialized and the simulation environment and flight controller must be properly connected.
Core Interface Description¶
The module AutoMavCmd.py includes configuration variables, helper functions, and core business classes.
Global Constants and Enumerations¶
This section lists all globally accessible constants and enumerations defined in the module.
Standalone Constants¶
None
Global/Standalone Functions¶
None
Sleep Class¶
Used to add waiting-type tasks in drone mission path planning, supporting both general time-based waiting and fixed-wing reset waiting, and used in conjunction with MAVLink command task execution workflows.
__init__(mav)¶
Function Description: Initializes a Sleep class instance and binds the MAV connection object.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
mav |
MavlinkInterface |
Yes | - | MAVLink communication interface object used for command interaction with the drone flight controller |
Return Value (Returns):
Sleepinstance object
Exceptions (Raises): - None
Wait(times)¶
Function Description: Adds a task command to wait for a specified duration, pausing execution for the set time before proceeding to subsequent tasks.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
times |
float |
Yes | - | Duration to wait, in seconds |
Return Value (Returns):
- None
Exceptions (Raises): - None
Example:
from RflySimSDK.phm.AutoMavCmd import Sleep
from RflySimSDK.communication import MavlinkInterface
# Initialize MAV connection
mav = MavlinkInterface()
# Create Sleep instance and add a 10-second wait task
sleep_task = Sleep(mav)
sleep_task.Wait(10)
WaitResetForFixwing(targetPos)¶
Function Description: Adds a task command to wait for position reset for fixed-wing drones, resetting the fixed-wing drone to a specified target position before proceeding to subsequent tasks.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
targetPos |
list[float] |
Yes | - | Target reset position, formatted as [N, E, D] in meters, corresponding to the North-East-Down (NED) coordinate system |
Return Value (Returns):
- None
Exceptions (Raises): - None
Example:
from RflySimSDK.phm.AutoMavCmd import Sleep
from RflySimSDK.communication import MavlinkInterface
# Initialize MAV connection
mav = MavlinkInterface()
# Create Sleep instance and add a fixed-wing position reset wait task, resetting to 100m above origin
sleep_task = Sleep(mav)
target_pos = [0, 0, -100]
sleep_task.WaitResetForFixwing(target_pos)
Command Class¶
This class provides MAVLink command encapsulation for different vehicles (multi-rotor drones, fixed-wing drones, unmanned surface vehicles, etc.) in the RflySim simulation environment, supporting common flight and navigation control operations such as arming, disarming, position control, and velocity control.
__init__(self, mav)¶
Function Description: Initializes a Command class instance and binds it to the MAVLink connection object.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
mav |
mavutil.mavlink_connection |
Yes | - | MAVLink connection object for the PX4 drone |
Return Value (Returns):
Commandinstance object
Exceptions (Raises): - None
Arm()¶
Function Description: Sends the vehicle arming command to enable the power system.
Parameters (Args):
- None
Return Value (Returns):
- None
Exceptions (Raises):
- None
DisArm()¶
Function Description: Sends the vehicle disarming command to disable the power system.
Parameters (Args):
- None
Return Value (Returns):
- None
Exceptions (Raises):
- None
QuadPos(pos)¶
Function Description: Sends a position control command for multi-rotor drones to fly to a specified target position.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
pos |
list[float] |
Yes | - | Target position array in format [N, E, D], unit: meters |
Return Value (Returns):
- None
Exceptions (Raises):
- None
FixWingPos(pos)¶
Function Description: Sends a navigation command for fixed-wing drones to fly toward a specified waypoint.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
pos |
list[float] |
Yes | - | Target waypoint position array in format [N, E, D], unit: meters |
Return Value (Returns):
- None
Exceptions (Raises):
- None
USVPos(pos)¶
Function Description: Sends a position control command for unmanned surface vehicles (USVs) to navigate to a specified target position.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
pos |
list[float] |
Yes | - | Target position array in format [N, E], unit: meters |
Return Value (Returns):
- None
Exceptions (Raises):
- None
QuadVel(vel)¶
Function Description: Sends a velocity control command for multi-rotor drones to fly at a specified velocity.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
vel |
list[float] |
Yes | - | Target velocity array in format [vN, vE, vD], unit: m/s |
Return Value (Returns):
- None
Exceptions (Raises):
- None
USVVel(vel)¶
Function Description: Sends a velocity control command for unmanned surface vehicles (USVs) to navigate at a specified velocity.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
vel |
list[float] |
Yes | - | Target velocity array in format [vN, vE], unit: m/s |
Return Value (Returns):
- None
Exceptions (Raises):
- None
USVGroundSpeed(vel)¶
Function Description: Sets the ground speed control target for an unmanned surface vehicle (USV).
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
vel |
float |
Yes | - | Target ground speed magnitude, unit: m/s |
Return Value (Returns):
- None
Exceptions (Raises):
- None
UAVLand(pos)¶
Function Description: Sends an autonomous landing command for drones to land at a specified position.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
pos |
list[float] |
Yes | - | Landing position array in format [N, E], unit: meters |
Return Value (Returns):
- None
Exceptions (Raises):
- None
FixWingTakeOff(targetpos)¶
Function Description: Sends a takeoff command for fixed-wing drones to take off from the current position to a target altitude.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
targetpos |
list[float] |
Yes | - | Takeoff target position array in format [N, E, D], unit: meters |
Return Value (Returns):
- None
Exceptions (Raises):
- None
FixWingSetCruiseRadius(radius)¶
Function Description: Sets the radius for the fixed-wing drone’s orbit patrol mode.
Parameters (Args):
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
radius |
float |
Yes | - | Orbit patrol radius, unit: meters |
Return Value (Returns):
- None
Exceptions (Raises):
- None
FaultInject(param)¶
Function Description: Injects simulation faults to test vehicle fault-handling logic.
Parameters (Args):
| Parameter Name | Type | Required | Default Value | Description |
|---|---|---|---|---|
param |
any |
Yes | - | Fault injection parameter, defining the fault type and fault parameters |
Returns: None
Raises: None
Example:
from RflySimSDK.phm.AutoMavCmd import Command
# Initialize the command object after binding the MAV connection
cmd = Command(mav)
# Arm the vehicle
cmd.Arm()
# Control the multirotor to fly to a specified position
cmd.QuadPos([10, 0, -2])
# Control the UAV to land
cmd.UAVLand([0, 0])
# Disarm the vehicle
cmd.DisArm()
---
### `CmdCtrl` Class
Used for controlling and processing MAVLink command sequences in the RflySim simulation environment, in conjunction with the PX4 flight controller to handle and schedule automatic mission commands.
#### `__init__(mav, frame)`
**Function Description**: Initializes an instance of the `CmdCtrl` class, binding the MAVLink interface and coordinate frame.
**Parameters (Args)**:
| Parameter Name | Type | Required | Default | Description |
| :--- | :--- | :---: | :--- | :--- |
| `mav` | | Yes | - | MAVLink communication interface object, used for command interaction with the flight controller |
| `frame` | | Yes | - | Coordinate frame object, defining the reference frame for command coordinates |
**Return Value (Returns)**:
- `CmdCtrl` instance object
**Exceptions (Raises)**:
- None
---
#### `GetWaitseq()`
**Function Description**: Retrieves the sequence number of the command currently waiting to be executed.
**Return Value (Returns)**:
- Sequence number of the command currently waiting for execution.
**Exceptions (Raises)**:
- None
---
#### `GetCmdseq()`
**Function Description**: Retrieves the sequence number of the command currently being executed.
**Return Value (Returns)**:
- Sequence number of the command currently in execution.
**Exceptions (Raises)**:
- None
---
#### `FIDPro(cmdCID)`
**Function Description**: Processes the flight command task identified by the specified ID.
**Parameters (Args)**:
| Parameter Name | Type | Required | Default | Description |
| :--- | :---: | :---: | :--- | :--- |
| `cmdCID` | | Yes | - | ID of the flight command to be processed, identifying the specific command task to execute |
**Return Value (Returns)**:
- Result of command processing.
**Exceptions (Raises)**:
- None
**Example**:
```python
from RflySimSDK.phm.AutoMavCmd import CmdCtrl
# Bind an already-initialized MAV interface and coordinate frame
cmd_ctrl = CmdCtrl(mav, frame)
# Get the sequence number of the command currently being executed
current_seq = cmd_ctrl.GetCmdseq()
# Process the flight command with the specified ID
cmd_ctrl.FIDPro(current_seq)
Advanced Usage Example¶
Demonstrates complex collaborative scenarios (e.g., multi-platform coordination, asynchronous control, batch operations)
The following example implements asynchronous planning and control for a collaborative mission involving three platforms: fixed-wing UAV, multi-rotor UAV, and an unmanned surface vehicle (USV). It first executes takeoff and waypoint path planning for each vehicle in sequence, then uses FIDPro to batch issue and synchronously wait for multiple command frames:
from RflySimSDK.phm.AutoMavCmd import Command, CmdCtrl, Sleep
# 1. Initialize command and synchronization objects for each vehicle
cmd = Command()
ctrl = CmdCtrl()
# 2. Batch arm and prepare each vehicle for takeoff
cmd.Arm(vehID=1) # Arm fixed-wing UAV for takeoff
cmd.FixWingTakeOff(vehID=1, targetAlt=100)
cmd.Arm(vehID=2) # Arm multi-rotor UAV
cmd.Arm(vehID=3) # Arm USV
# 3. Asynchronously plan position waypoints for different vehicles
waypoints_quad = [(31.2, 121.5, 80), (31.21, 121.51, 80)]
waypoints_fw = [(31.19, 121.49, 100), (31.22, 121.52, 100)]
waypoints_usv = [(31.205, 121.495, 0), (31.215, 121.505, 0)]
for wp in waypoints_quad:
cmd.QuadPos(vehID=2, lat=wp[0], lon=wp[1], alt=wp[2])
for wp in waypoints_fw:
cmd.FixWingPos(vehID=1, lat=wp[0], lon=wp[1], alt=wp[2])
for wp in waypoints_usv:
cmd.USVPos(vehID=3, lat=wp[0], lon=wp[1], alt=wp[2])
# 4. Add synchronization wait: wait for the fixed-wing UAV to complete alignment with the waypoint before batch execution
ctrl.FIDPro()
Sleep.WaitResetForFixwing(vehID=1, waitTime=10)
# 5. Batch send commands and wait for all tasks to complete
cmdSeq = ctrl.GetCmdseq()
waitSeq = ctrl.GetWaitseq()
ctrl.sendCmdAndWait(cmdSeq, waitSeq)
# 6. Unified landing after mission completion
cmd.UAVLand(vehID=1)
cmd.UAVLand(vehID=2)
cmd.USVVel(vehID=3, velX=0, velY=0)
ctrl.sendCmdAndWait(ctrl.GetCmdseq(), ctrl.GetWaitseq())
Notes and Pitfall Avoidance Guide¶
- Vehicle ID Validity: When issuing commands, ensure the
vehIDparameter matches the vehicle ID configured in the RflySim visualization configuration interface. If the ID does not exist, the command will be silently dropped without throwing an explicit error. It is recommended to test individual vehicle command responses before performing batch operations. - Wait Command Binding Requirement:
Sleepclass wait methods must be bound withGetWaitseq()to take effect. Directly adding wait commands into the standard command sequence obtained viaGetCmdseq()will cause the wait logic to fail, resulting in all commands executing concurrently. - Fixed-Wing Takeoff Wait Requirement: After calling
FixWingTakeOff, sufficient wait time must be added orWaitResetForFixwingmust be invoked to allow the fixed-wing UAV to complete its takeoff roll and heading alignment. Otherwise, subsequent waypoint commands may cause the UAV to lose control and deviate from the runway during takeoff. - Memory Management for Multi-Frame Command Issuance: Each call to
GetCmdseq()andGetWaitseq()clears the original command buffer. If existing commands need to be retained, they must be stored separately in advance to avoid unintentional deletion of already-planned mission commands.
Changelog¶
2024-08-02: chore: Added code comments for generating HTML-format API documentation.2024-07-18: fix: Updated API homepage index.2023-11-09: fix: Added interface class file for automated safety assessment.