PX4¶
nexus_uav.px4 holds PX4's own names: what a script that commands PX4 imports. OffboardClient is the
host end of the offboard link of PX4 Software In The Loop (SITL). The run owns that link's address
and names it in its port map, sim.ports, so the script connects the client
itself once the sim has started. OffboardClient.connect(sim, timeout) listens on that address and
waits for PX4's heartbeat. PX4 runs on the sim's clock, so the wait steps the sim, and timeout
counts sim seconds. When no heartbeat comes, it raises RunError, which names the
port, and so does any wait once the run has lost PX4.
import nexus_uav as nx
from nexus_uav.px4 import OffboardClient
with nx.Sim("astro_max_base", scene="empty") as sim:
sim.start()
with OffboardClient.connect(sim, 10.0) as op: # sim.ports["offboard"]: UDP 14540 on instance 0
op.takeoff(2.0)
sim.wait_until(op.at_target, sim_timeout=60.0)
A run against the fake PX4 lists no offboard link, so sim.ports["offboard"] raises and names the
fake. The mission plan types describe what upload_mission hands PX4:
from nexus_uav.px4 import MissionItem, Plan, read_plan
OffboardClient
¶
OffboardClient(
conn: str = f"udpin:0.0.0.0:{OFFBOARD_PORT}",
*,
system_id: int = 1,
arrive_m: float = 2.0,
yaw_tol_rad: float = radians(5.0),
alt_tol_m: float = 1.0,
wait_clear_s: float = 5.0,
)
Configure the offboard link; nothing opens a connection until entered.
| PARAMETER | DESCRIPTION |
|---|---|
conn
|
pymavlink connection string for the PX4 offboard link. The default
listens on
TYPE:
|
system_id
|
PX4's MAVLink system id,
TYPE:
|
arrive_m
|
3D arrival radius for :meth:
TYPE:
|
yaw_tol_rad
|
Heading tolerance for :meth:
TYPE:
|
alt_tol_m
|
Altitude tolerance for :meth:
TYPE:
|
wait_clear_s
|
Seconds the vehicle must be failure-free, and the initial settle period, before the pump sends an arm command.
TYPE:
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
connected
property
¶
connected: bool
Whether PX4 has answered on the offboard link: a heartbeat has arrived and the pump has learned the peer's system id, so every verb has somewhere to send.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
connect
classmethod
¶
connect(sim: Sim, timeout: float) -> OffboardClient
Open a client on the run's offboard link and wait for PX4's heartbeat, stepping the sim.
The client listens on the address the run's port map names, sim.ports["offboard"], and
addresses the system it names. PX4 runs on the sim's clock, so the wait steps the sim, and
timeout counts sim seconds. Call it once the sim has started; the client returned is open,
and entering it as a context manager closes it on the way out::
sim.start()
with OffboardClient.connect(sim, 10.0) as op:
op.takeoff(2.0)
sim.wait_until(op.at_target, sim_timeout=60.0)
| PARAMETER | DESCRIPTION |
|---|---|
sim
|
The started run.
TYPE:
|
timeout
|
Sim seconds to wait for PX4's first heartbeat.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OffboardClient
|
The open client, with PX4's heartbeat: :attr: |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
The run has no offboard link; the message says why, such as a run on the fake PX4. |
RunError
|
No heartbeat came within |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
open
¶
open() -> OffboardClient
Open the MAVLink link and start the pump, returning at once, with no wait for PX4.
The caller owns the waiting, because PX4's clock is the sim's under lockstep: the sim has
to keep stepping or no heartbeat ever arrives, so the wait has to be a stepping loop rather
than a sleep, sim.wait_until(lambda: op.connected, sim_timeout=…). Poll
:attr:connected to know when PX4 has answered. Entering the client as a context manager
calls this, and leaving it calls :meth:close.
| RETURNS | DESCRIPTION |
|---|---|
OffboardClient
|
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
close
¶
close() -> None
Stop the background pump thread and close the MAVLink connection.
Idempotent and safe to call after a partial start: signals the pump to stop,
joins it, for up to 5 s, then closes the connection if open made one.
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
284 285 286 287 288 289 290 291 292 293 294 295 296 | |
set_mode
¶
set_mode(mode: str) -> None
Request a flight mode. Returns at once; the pump commands it until telemetry confirms.
Entering a manual mode requires a live manual-control source, so a neutral MANUAL_CONTROL
stream is auto-started first. Observe the result with :meth:mode.
| PARAMETER | DESCRIPTION |
|---|---|
mode
|
Friendly mode name; one of the keys of the mode table, for example
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
arm
¶
arm(*, gesture: bool = False) -> None
Request arming. Returns at once; the pump arms once the vehicle is armable.
Armable means telemetry has confirmed any pending mode change, PX4 is publishing a position
estimate, so the EKF has converged, and it has been failure-free for the configured settle
period. Observe the result with :meth:is_armed.
| PARAMETER | DESCRIPTION |
|---|---|
gesture
|
Arm with the pilot's throttle-down plus yaw-right stick gesture rather than
TYPE:
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
disarm
¶
disarm() -> bool
Send a single disarm command, fire-and-forget and not verified.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
573 574 575 576 577 578 579 580 581 582 | |
set_rc
¶
set_rc(
*,
roll: float = 0.0,
pitch: float = 0.0,
throttle: float = 0.5,
yaw: float = 0.0,
) -> None
Set the streamed MANUAL_CONTROL setpoint. roll/pitch/yaw in [-1,1]; throttle in [0,1].
Stores the setpoint; the background pump streams it at ~20 Hz until changed or
cleared with stop_rc. This clamps the values to range, then scales them to the
MANUAL_CONTROL integer fields.
| PARAMETER | DESCRIPTION |
|---|---|
roll
|
Roll stick, clamped to [-1, 1].
TYPE:
|
pitch
|
Pitch stick, clamped to [-1, 1].
TYPE:
|
throttle
|
Throttle stick, clamped to [0, 1]; 0.5 is mid-stick.
TYPE:
|
yaw
|
Yaw stick, clamped to [-1, 1].
TYPE:
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | |
stop_rc
¶
stop_rc() -> None
Stop streaming the MANUAL_CONTROL setpoint; the pump goes quiet until set_rc.
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
607 608 609 | |
param_set
¶
param_set(name: str, value: float) -> None
Set a REAL32 PX4 parameter: a fire-and-forget PARAM_SET that awaits no acknowledgement.
Float-only on purpose: an integer parameter doesn't ride the wire as a float, so it gets
its own verb rather than a type argument this one can't honour; see :meth:param_set_int.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Parameter name, for example
TYPE:
|
value
|
New value, sent as a float.
TYPE:
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
611 612 613 614 615 616 617 618 619 620 621 622 623 624 | |
param_set_int
¶
param_set_int(name: str, value: int) -> None
Set an INT32 PX4 parameter: a fire-and-forget PARAM_SET that awaits no acknowledgement.
PARAM_SET carries every value in one float field, and PX4 reads an integer parameter out of
that field's bits rather than its numeric value. So this reinterprets the int rather than
casting it: sending float(1) would have PX4 read 1065353216.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Parameter name, for example
TYPE:
|
value
|
New value, sent as the INT32 bit pattern of the float field.
TYPE:
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
takeoff
¶
takeoff(alt_m: float) -> None
Set the takeoff altitude, request Takeoff mode, then request arming. Returns at once.
The ordering is load-bearing and this method owns it: telemetry confirms
the DO_SET_MODE for Takeoff mode before the arm command, which is why an autonomous script never
calls :meth:arm itself. PX4 climbs on arming; wait for the climb with :meth:at_target.
| PARAMETER | DESCRIPTION |
|---|---|
alt_m
|
Target takeoff altitude in meters, written to
TYPE:
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
land
¶
land() -> None
Request Land mode to land the vehicle in place. Returns at once.
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
659 660 661 662 663 | |
upload_mission
¶
upload_mission(
plan: Plan | Sequence[MissionItem] | str | PathLike,
) -> None
Upload a mission to PX4. Returns at once; wait for it with :meth:mission_uploaded.
The items go on the wire verbatim, in the geodetic frame the .plan authored them in;
nothing converts through the sim's world frame, so a plan flies where QGroundControl drew it.
Upload is a handshake rather than a send: this lodges the items, the pump puts
MISSION_COUNT on the wire, PX4 then asks for each item by seq, and it ends with
PX4's MISSION_ACK. Re-uploading replaces the mission and resets the progress counters,
so a second mission never reports the first one's arrivals.
| PARAMETER | DESCRIPTION |
|---|---|
plan
|
A :class:
TYPE:
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | |
mission_uploaded
¶
mission_uploaded() -> bool
Report whether PX4 has accepted the uploaded mission.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
bool
|
is in flight, and also when PX4 rejected the mission; check :meth: |
bool
|
to tell a "not yet" from a "refused" answer. |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
691 692 693 694 695 696 697 698 699 | |
mission_ack
¶
mission_ack() -> int | None
Report PX4's MISSION_ACK result code.
| RETURNS | DESCRIPTION |
|---|---|
int | None
|
The MAV_MISSION_RESULT value, where |
int | None
|
acknowledged an upload yet. |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
701 702 703 704 705 706 707 708 | |
mission_count
¶
mission_count() -> int
Report how many items are in the uploaded mission.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The item count, or |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
710 711 712 713 714 715 716 | |
mission_reached
¶
mission_reached() -> int
Report the highest mission item PX4 says it has reached.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The sequence number from the most recent |
int
|
arrived yet. Never decreases within one mission. |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
718 719 720 721 722 723 724 725 | |
mission_current
¶
mission_current() -> int
Report the item PX4 is flying to.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The sequence number from the most recent |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
727 728 729 730 731 732 733 | |
mission_complete
¶
mission_complete() -> bool
Report whether the vehicle has reached the mission's last waypoint.
Measured on the last NAV_WAYPOINT, not the last item. A mission that ends in
NAV_RETURN_TO_LAUNCH hands the flight to Return To Launch (RTL) instead of reporting an
arrival there, so waiting on the final item could never return. Wait out the RTL tail with
:meth:landed_state instead.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
bool
|
mission, or one that carries no waypoint at all. |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
735 736 737 738 739 740 741 742 743 744 745 746 747 748 | |
start_mission
¶
start_mission() -> None
Request the mission's first item, Mission mode, then arming. Returns at once.
The ordering is load-bearing and this method owns it, exactly as :meth:takeoff does:
telemetry confirms the DO_SET_MODE for Mission mode before the arm command, which is the proven
headless recipe. PX4 flies the mission on arming, from the item it holds as current, so the
arm also waits until PX4 reports item 0 current after MISSION_SET_CURRENT: PX4 skips an
upload that matches the mission it already holds, which after a finished flight sits at its
last item, and would end the new flight on the ground.
Upload the mission first and wait for :meth:mission_uploaded: PX4 refuses Mission mode
while it has no valid mission, so engaging early just burns retries.
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | |
goto
¶
goto(
setpoint: Setpoint, *, yaw: float | None = None
) -> None
Fly to a world-frame position. Returns at once; wait for it with :meth:at_target.
Commanded as DO_REPOSITION: the same guidance PX4 runs for a GCS "fly here" request, and
what it latches until the next command, so this goes out once rather than streamed. The
position is in world axes, Newton FLU and Z-up, the same tuples a mission in the loop
takes, resolved against an anchor captured on the first call; see :meth:_anchor_now.
Yaw is in world axes too, and this reflects it on the way out for the same reason it reflects the position: a right-handed yaw about world +z turns +x toward +y, which is north toward west, while PX4's North-East-Down (NED) heading is positive north toward east. Leaving the position in one frame and the heading in the other is the trap #61 was.
| PARAMETER | DESCRIPTION |
|---|---|
setpoint
|
The target: a
TYPE:
|
yaw
|
Optional world heading in radians, right-handed about +z, so +90° faces +y =
west; overrides a
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
No position has arrived on the offboard link yet. |
TypeError
|
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | |
at_target
¶
at_target() -> bool
Report whether the vehicle has reached what the last verb commanded.
Measured against what PX4 reports, the cached GLOBAL_POSITION_INT and ATTITUDE,
never the sim's ground truth. The client is a remote GCS; reading sim state here would
let it see a position the autopilot doesn't have.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
bool
|
target, because no verb commanded one or the last verb was :meth: |
bool
|
first position report. |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | |
is_armed
¶
is_armed() -> bool
Report the cached arming state from the most recent heartbeat.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
844 845 846 847 848 849 850 | |
mode
¶
mode() -> str
Report the cached flight mode decoded from the most recent heartbeat.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The friendly mode name, for example |
str
|
string if the mode isn't in the known table. |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
852 853 854 855 856 857 858 859 | |
landed_state
¶
landed_state() -> str
Report the cached landed state from EXTENDED_SYS_STATE.
| RETURNS | DESCRIPTION |
|---|---|
str
|
|
str
|
|
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
861 862 863 864 865 866 867 868 | |
relative_altitude
¶
relative_altitude() -> float | None
Report the cached altitude over the launch point, in meters.
| RETURNS | DESCRIPTION |
|---|---|
float | None
|
Relative altitude in meters from GLOBAL_POSITION_INT, or |
float | None
|
position has arrived yet. |
Source code in nexus_uav/_src/peers/px4_sitl/offboard.py
870 871 872 873 874 875 876 877 | |
Plan
dataclass
¶
Plan(
home: tuple[float, float, float],
items: tuple[MissionItem, ...],
)
A parsed .plan: where the vehicle starts, and the items it flies.
| ATTRIBUTE | DESCRIPTION |
|---|---|
home |
TYPE:
|
items |
The mission items in file order, with
TYPE:
|
MissionItem
dataclass
¶
MissionItem(
seq: int,
frame: int,
command: int,
autocontinue: bool,
params: tuple[float, float, float, float],
x: int,
y: int,
z: float,
)
One mission item, in the shape MISSION_ITEM_INT carries it.
| ATTRIBUTE | DESCRIPTION |
|---|---|
seq |
Position in the mission, assigned from the item's index in the file.
TYPE:
|
frame |
MAV_FRAME for this item;
TYPE:
|
command |
MAV_CMD for this item, for example
TYPE:
|
autocontinue |
Whether PX4 proceeds to the next item on arrival.
TYPE:
|
params |
TYPE:
|
x |
Latitude in degrees times 1e7: the integer field, which is why the
TYPE:
|
y |
Longitude in degrees times 1e7.
TYPE:
|
z |
Altitude in metres, in this item's
TYPE:
|
read_plan
¶
read_plan(path: str | PathLike) -> Plan
Read a QGroundControl .plan file.
The items keep their file order and get a seq from their index, so the mission uploads as
0..N-1 with the first real item at seq 0. There is no implicit home item: PX4 takes the
mission as sent, and this is what MAVSDK puts on the wire too.
This reads the mission and nothing else: it skips geoFence and rallyPoints.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to the
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Plan
|
The parsed :class: |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
The file isn't a plan, meaning |
Source code in nexus_uav/_src/peers/px4_sitl/qgc_plan.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |