Skip to content

PQW Frame

The perifocal frame PQW places P toward periapsis, W along the orbit normal, and \(Q = W \times P\), so a satellite's own position is \(r[\cos f, \sin f, 0]\) with \(f\) the true anomaly. The periapsis direction comes from the eccentricity vector, which needs the central body's gravitational parameter, so the rotation has an Earth form and a _for_body form. On a circular orbit P is taken along the ascending node, and on a circular equatorial orbit along the inertial x axis projected into the orbit plane.

import numpy as np

import brahe as bh

bh.initialize_eop()

oe = np.array([bh.R_EARTH + 700e3, 0.1, 97.8, 15.0, 30.0, 45.0])
x_ecc = bh.state_koe_to_eci(oe, bh.AngleFormat.DEGREES)
r_pqw = bh.rotation_eci_to_pqw(x_ecc) @ x_ecc[:3]
r = np.linalg.norm(x_ecc[:3])
f = np.radians(bh.anomaly_mean_to_true(45.0, 0.1, angle_format=bh.AngleFormat.DEGREES))
print(
    "Own position in PQW equals r [cos f, sin f, 0]:",
    np.allclose(r_pqw, r * np.array([np.cos(f), np.sin(f), 0.0]), atol=1e-6),
)

# Circular orbit: periapsis is undefined, P falls back to the ascending node
oe_circ = np.array([bh.R_EARTH + 700e3, 0.0, 97.8, 15.0, 30.0, 45.0])
x_circ = bh.state_koe_to_eci(oe_circ, bh.AngleFormat.DEGREES)
p_circ = bh.rotation_pqw_to_eci(x_circ)[:, 0]
raan = np.radians(15.0)
print(
    "Circular fallback: P equals [cos Omega, sin Omega, 0]:",
    np.allclose(p_circ, np.array([np.cos(raan), np.sin(raan), 0.0])),
)

# About Mars, the periapsis direction is the position at mean anomaly zero
oe_mars = np.array([bh.R_MARS + 400e3, 0.05, 92.6, 45.0, 270.0, 10.0])
x_mars = bh.state_koe_to_inertial_for_body(
    oe_mars, bh.CentralBody.Mars, bh.AngleFormat.DEGREES
)
oe_mars_periapsis = oe_mars.copy()
oe_mars_periapsis[5] = 0.0
x_mars_periapsis = bh.state_koe_to_inertial_for_body(
    oe_mars_periapsis, bh.CentralBody.Mars, bh.AngleFormat.DEGREES
)
p_mars = bh.rotation_pqw_to_inertial_for_body(x_mars, bh.GM_MARS)[:, 0]
r_periapsis_hat = x_mars_periapsis[:3] / np.linalg.norm(x_mars_periapsis[:3])
print(
    "Mars periapsis direction matches the position at M = 0:",
    np.allclose(p_mars, r_periapsis_hat),
)

# Frame graph
epc = bh.Epoch.from_datetime(2024, 3, 1, 0, 0, 0.0, 0.0, bh.UTC)
bh.register_object("SC", lambda epoch: x_ecc, bh.CelestialFrame.GCRF)
r_graph = bh.rotation_frame_to_frame(
    bh.CelestialFrame.GCRF, bh.ReferenceFrame.PQW("SC"), epc
)
print(
    "Frame graph matches rotation_eci_to_pqw:",
    np.allclose(r_graph, bh.rotation_eci_to_pqw(x_ecc)),
)
bh.clear_object_registry()
use brahe as bh;
use brahe::frames::{CelestialFrame, ReferenceFrame};
use brahe::time::{Epoch, TimeSystem};
use brahe::utils::BraheError;
use brahe::utils::state_providers::SStateProvider;
use nalgebra::Vector6;

/// A state provider returning a fixed state at every epoch, for registering
/// the spacecraft as an object.
struct ConstantProvider(Vector6<f64>);
impl SStateProvider for ConstantProvider {
    fn state(&self, _epoch: Epoch) -> Result<Vector6<f64>, BraheError> {
        Ok(self.0)
    }
}

fn main() {
    bh::initialize_eop().unwrap();

    let oe = Vector6::new(bh::R_EARTH + 700e3, 0.1, 97.8, 15.0, 30.0, 45.0);
    let x_ecc = bh::state_koe_to_eci(oe, bh::AngleFormat::Degrees);
    let r_pqw = bh::rotation_eci_to_pqw(x_ecc) * x_ecc.fixed_rows::<3>(0).into_owned();
    let r = x_ecc.fixed_rows::<3>(0).norm();
    let f = bh::anomaly_mean_to_true(45.0, 0.1, bh::AngleFormat::Degrees)
        .unwrap()
        .to_radians();
    let r_true_anomaly = r * nalgebra::Vector3::new(f.cos(), f.sin(), 0.0);
    println!(
        "Own position in PQW equals r [cos f, sin f, 0]: {}",
        (r_pqw - r_true_anomaly).norm() < 1e-6
    );

    // Circular orbit: periapsis is undefined, P falls back to the ascending node
    let oe_circ = Vector6::new(bh::R_EARTH + 700e3, 0.0, 97.8, 15.0, 30.0, 45.0);
    let x_circ = bh::state_koe_to_eci(oe_circ, bh::AngleFormat::Degrees);
    let p_circ = bh::rotation_pqw_to_eci(x_circ).column(0).into_owned();
    let raan = 15.0_f64.to_radians();
    println!(
        "Circular fallback: P equals [cos Omega, sin Omega, 0]: {}",
        (p_circ - nalgebra::Vector3::new(raan.cos(), raan.sin(), 0.0)).norm() < 1e-6
    );

    // About Mars, the periapsis direction is the position at mean anomaly zero
    let oe_mars = Vector6::new(bh::R_MARS + 400e3, 0.05, 92.6, 45.0, 270.0, 10.0);
    let x_mars =
        bh::state_koe_to_inertial_for_body(oe_mars, &bh::CentralBody::Mars, bh::AngleFormat::Degrees)
            .unwrap();
    let mut oe_mars_periapsis = oe_mars;
    oe_mars_periapsis[5] = 0.0;
    let x_mars_periapsis = bh::state_koe_to_inertial_for_body(
        oe_mars_periapsis,
        &bh::CentralBody::Mars,
        bh::AngleFormat::Degrees,
    )
    .unwrap();
    let p_mars = bh::rotation_pqw_to_inertial_for_body(x_mars, bh::GM_MARS)
        .column(0)
        .into_owned();
    let r_periapsis = x_mars_periapsis.fixed_rows::<3>(0);
    let r_periapsis_hat = r_periapsis / r_periapsis.norm();
    println!(
        "Mars periapsis direction matches the position at M = 0: {}",
        (p_mars - r_periapsis_hat).norm() < 1e-6
    );

    // Frame graph
    let epc = Epoch::from_datetime(2024, 3, 1, 0, 0, 0.0, 0.0, TimeSystem::UTC);
    bh::register_object("SC", ConstantProvider(x_ecc), CelestialFrame::GCRF).unwrap();
    let r_graph =
        bh::rotation_frame_to_frame(CelestialFrame::GCRF, ReferenceFrame::PQW("SC"), epc)
            .unwrap();
    println!(
        "Frame graph matches rotation_eci_to_pqw: {}",
        (r_graph - bh::rotation_eci_to_pqw(x_ecc)).norm() < 1e-12
    );
    bh::clear_object_registry();
}
Output
1
2
3
4
Own position in PQW equals r [cos f, sin f, 0]: True
Circular fallback: P equals [cos Omega, sin Omega, 0]: True
Mars periapsis direction matches the position at M = 0: True
Frame graph matches rotation_eci_to_pqw: True
1
2
3
4
Own position in PQW equals r [cos f, sin f, 0]: true
Circular fallback: P equals [cos Omega, sin Omega, 0]: true
Mars periapsis direction matches the position at M = 0: true
Frame graph matches rotation_eci_to_pqw: true

See Also