pub struct VehicleState {
pub position_m: Vec3,
pub velocity_mps: Vec3,
pub acceleration_mps2: Vec3,
pub attitude: UnitQuaternion<f32>,
pub angular_velocity_radps: Vec3,
pub roll_rad: f32,
pub pitch_rad: f32,
pub heading_rad: f32,
pub airspeed_mps: Option<f32>,
}Expand description
Represents the physical state of a vehicle in 3D space.
This struct encapsulates all the key state information needed for flight control,
including position, velocity, acceleration, attitude, and angular velocity.
Quantities are plain SI f32s; field names carry the unit (position_m, velocity_mps, …).
§Frame contract
This table is the reference for how every StateSource reports state; a backend
converts from its native frames at its boundary, and testing::conformance (testing
feature) checks it.
Two frames are involved. Attitude follows aerospace convention: the NED world frame
(x north, y east, z down) and the FRD body frame (x forward, y right, z down).
Translation uses the same north and east axes but flips z to point up, so altitude
and climb rate are positive: position_m/velocity_mps/acceleration_mps2 z is NED z
negated.
| Quantity | Frame | Units | Positive |
|---|---|---|---|
position_m | x north, y east, z up | m | z is altitude above the backend’s datum (e.g. sea level) |
velocity_mps, acceleration_mps2 | as position_m | m/s, m/s² | z climbing |
attitude | NED world → FRD body, ZYX (yaw, pitch, roll); UnitQuaternion::from_euler_angles(roll, pitch, heading) | — | euler_angles of it match the fields below |
roll_rad | about body x | rad | right wing down |
pitch_rad | about body y | rad | nose up |
heading_rad | about NED z | rad | clockwise from north (east = π/2); any range, e.g. [0, 2π) or [-π, π) |
angular_velocity_radps | FRD body: x roll, y pitch, z yaw | rad/s | roll right, nose up, nose right |
airspeed_mps | speed relative to the air mass (any frame) | m/s | non-negative; None without an air-data source |
Near ±90° pitch the Euler fields are degenerate: roll and heading turn about the same axis,
so only their sum (or difference) means anything, and euler_angles picks one split.
attitude and angular_velocity_radps stay well defined, so code that must work there (a
tailsitter’s nose-up hover) uses them, not the Euler fields.
§Examples
Creating a default vehicle state:
use stable_flight::VehicleState;
let state = VehicleState::default();Fields§
§position_m: Vec3World position (north, east, up); see the frame contract.
velocity_mps: Vec3World velocity (north, east, up).
acceleration_mps2: Vec3World acceleration (north, east, up).
attitude: UnitQuaternion<f32>Orientation quaternion representing the vehicle’s attitude. The quaternion represents the rotation from world frame to body frame.
angular_velocity_radps: Vec3Body angular velocity in roll (x), pitch (y), and yaw (z), in radians per second.
roll_rad: f32Current roll angle in radians, right wing down positive.
pitch_rad: f32Current pitch angle in radians, nose up positive.
heading_rad: f32Current heading (yaw) angle in radians. 0 is north, with positive values rotating clockwise.
airspeed_mps: Option<f32>Speed relative to the air mass in meters per second, or None without an air-data
source (e.g. no pitot). A magnitude, so it doesn’t depend on the body frame. Consumers
read it through valid_airspeed_mps, which treats a NaN
or negative value as None (input policy).
Implementations§
Source§impl VehicleState
impl VehicleState
Sourcepub fn valid_airspeed_mps(&self) -> Option<f32>
pub fn valid_airspeed_mps(&self) -> Option<f32>
airspeed_mps when it is usable: None without air data, and
for a NaN or negative reading (input policy).
§Examples
use stable_flight::VehicleState;
// (reported, usable)
let cases = [
(Some(12.0), Some(12.0)),
(Some(0.0), Some(0.0)),
(None, None),
(Some(f32::NAN), None),
(Some(-1.0), None),
];
for (airspeed_mps, usable) in cases {
let state = VehicleState { airspeed_mps, ..Default::default() };
assert_eq!(state.valid_airspeed_mps(), usable, "{airspeed_mps:?}");
}Sourcepub fn rotate_body(&self, rotation: &UnitQuaternion<f32>) -> Self
pub fn rotate_body(&self, rotation: &UnitQuaternion<f32>) -> Self
This state in another body frame: rotation is the new body frame expressed in the old
one (a constant mount or view rotation).
The attitude becomes attitude · rotation, the body rates rotation⁻¹ · ω (the same
rotation seen in the new axes), and the Euler fields are recomputed from the new
attitude. World position, velocity, acceleration, and airspeed don’t depend on the body
frame and are unchanged.
§Examples
use core::f32::consts::FRAC_PI_2;
use stable_flight::nalgebra::UnitQuaternion;
use stable_flight::VehicleState;
// A tailsitter hovering nose up, seen in a frame rotated −90° in pitch, reads level.
let nose_up = VehicleState {
attitude: UnitQuaternion::from_euler_angles(0.0, FRAC_PI_2, 0.0),
..Default::default()
};
let view = nose_up.rotate_body(&UnitQuaternion::from_euler_angles(0.0, -FRAC_PI_2, 0.0));
assert!(view.pitch_rad.abs() < 1e-6);Trait Implementations§
Source§impl Clone for VehicleState
impl Clone for VehicleState
Source§impl Debug for VehicleState
impl Debug for VehicleState
Source§impl Default for VehicleState
impl Default for VehicleState
Source§impl<'de> Deserialize<'de> for VehicleState
impl<'de> Deserialize<'de> for VehicleState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for VehicleState
impl PartialEq for VehicleState
Source§impl Serialize for VehicleState
impl Serialize for VehicleState
impl StructuralPartialEq for VehicleState
Auto Trait Implementations§
impl Forget for VehicleState
impl Freeze for VehicleState
impl Move for VehicleState
impl RefUnwindSafe for VehicleState
impl Send for VehicleState
impl Sync for VehicleState
impl Unpin for VehicleState
impl UnsafeUnpin for VehicleState
impl UnwindSafe for VehicleState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
impl<T> Scalar for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.