Skip to content

NSW Frame

NSW places X toward nadir, Y toward the Sun as nearly as possible while normal to X, and \(Z = X \times Y\). Each function takes the Sun's state, relative to the same center as the spacecraft state, as the argument after the spacecraft states. The rate follows from the time derivatives of the axes and includes the Sun's velocity. When the Sun lies on the nadir line, Y falls back to the along-track direction \(\hat{h} \times \hat{r}\). The standard definition of NSW gives only the axes away from that case; the fallback and the rate are specific to brahe. The frame graph takes the Sun's state from the SPICE registry.

import numpy as np

import brahe as bh

bh.initialize_eop()

oe = np.array([bh.R_EARTH + 700e3, 0.05, 97.8, 15.0, 30.0, 45.0])
x_sc = bh.state_koe_to_eci(oe, bh.AngleFormat.DEGREES)

epc = bh.Epoch.from_datetime(2024, 3, 1, 0, 0, 0.0, 0.0, bh.UTC)
r_sun = bh.sun_position(epc)
v_sun = (bh.sun_position(epc + 0.5) - bh.sun_position(epc - 0.5)) / 1.0
x_sun = np.concatenate([r_sun, v_sun])

r_nsw = bh.rotation_nsw_to_eci(x_sc, x_sun)
x_axis, y_axis = r_nsw[:, 0], r_nsw[:, 1]
sun_dir = (x_sun[:3] - x_sc[:3]) / np.linalg.norm(x_sun[:3] - x_sc[:3])
print("Y . sun_direction > 0:", y_axis.dot(sun_dir) > 0.0)
print("|Y . X| < 1e-12:", abs(y_axis.dot(x_axis)) < 1e-12)

# Fixed-Sun approximation: a zero-velocity Sun state omits the Sun's own motion
x_sun_fixed = np.concatenate([r_sun, np.zeros(3)])
omega_moving = bh.omega_nsw(x_sc, x_sun)
omega_fixed = bh.omega_nsw(x_sc, x_sun_fixed)
rate_diff = np.linalg.norm(omega_moving - omega_fixed)
print(
    "Fixed-Sun approximation error within (1e-8, 1e-6) rad/s:",
    1e-8 < rate_diff < 1e-6,
)

# Relative state round trip: 1 km along Y, 200 m along Z
x_rel = np.array([0.0, 1000.0, 200.0, 0.0, 0.0, 0.0])
x_deputy = bh.state_nsw_to_eci(x_sc, x_rel, x_sun)
print(
    f"Round trip error: {np.linalg.norm(bh.state_eci_to_nsw(x_sc, x_deputy, x_sun) - x_rel):.2e}"
)

# Frame graph: the Sun's state comes from the SPICE registry
bh.load_spice_kernel("de440s")
x_sun_spk = bh.spk_state(bh.NAIFId.SUN, bh.NAIFId.EARTH, epc)
bh.register_object("SC", lambda epoch: x_sc, bh.CelestialFrame.GCRF)
r_graph = bh.rotation_frame_to_frame(
    bh.CelestialFrame.GCRF, bh.ReferenceFrame.NSW("SC"), epc
)
print(
    "Frame graph matches rotation_eci_to_nsw:",
    np.allclose(r_graph, bh.rotation_eci_to_nsw(x_sc, x_sun_spk), atol=1e-9),
)
bh.clear_object_registry()
use brahe as bh;
use brahe::frames::{CelestialFrame, ReferenceFrame};
use brahe::spice::NAIFId;
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.05, 97.8, 15.0, 30.0, 45.0);
    let x_sc = bh::state_koe_to_eci(oe, bh::AngleFormat::Degrees);

    let epc = Epoch::from_datetime(2024, 3, 1, 0, 0, 0.0, 0.0, TimeSystem::UTC);
    let r_sun = bh::sun_position(epc);
    let v_sun = (bh::sun_position(epc + 0.5) - bh::sun_position(epc - 0.5)) / 1.0;
    let x_sun = Vector6::new(
        r_sun[0], r_sun[1], r_sun[2], v_sun[0], v_sun[1], v_sun[2],
    );

    let r_nsw = bh::rotation_nsw_to_eci(x_sc, x_sun);
    let x_axis = r_nsw.column(0).into_owned();
    let y_axis = r_nsw.column(1).into_owned();
    let r_sc = x_sc.fixed_rows::<3>(0).into_owned();
    let sun_dir = (x_sun.fixed_rows::<3>(0).into_owned() - r_sc).normalize();
    println!("Y . sun_direction > 0: {}", y_axis.dot(&sun_dir) > 0.0);
    println!("|Y . X| < 1e-12: {}", y_axis.dot(&x_axis).abs() < 1e-12);

    // Fixed-Sun approximation: a zero-velocity Sun state omits the Sun's own motion
    let x_sun_fixed = Vector6::new(r_sun[0], r_sun[1], r_sun[2], 0.0, 0.0, 0.0);
    let omega_moving = bh::omega_nsw(x_sc, x_sun);
    let omega_fixed = bh::omega_nsw(x_sc, x_sun_fixed);
    let rate_diff = (omega_moving - omega_fixed).norm();
    println!(
        "Fixed-Sun approximation error within (1e-8, 1e-6) rad/s: {}",
        (1e-8..1e-6).contains(&rate_diff)
    );

    // Relative state round trip: 1 km along Y, 200 m along Z
    let x_rel = Vector6::new(0.0, 1000.0, 200.0, 0.0, 0.0, 0.0);
    let x_deputy = bh::state_nsw_to_eci(x_sc, x_rel, x_sun);
    println!(
        "Round trip error: {:.2e}",
        (bh::state_eci_to_nsw(x_sc, x_deputy, x_sun) - x_rel).norm()
    );

    // Frame graph: the Sun's state comes from the SPICE registry
    bh::spice::load_spice_kernel("de440s").unwrap();
    let x_sun_spk = bh::spice::spk_state(NAIFId::Sun, NAIFId::Earth, epc).unwrap();
    bh::register_object("SC", ConstantProvider(x_sc), CelestialFrame::GCRF).unwrap();
    let r_graph =
        bh::rotation_frame_to_frame(CelestialFrame::GCRF, ReferenceFrame::NSW("SC"), epc)
            .unwrap();
    println!(
        "Frame graph matches rotation_eci_to_nsw: {}",
        (r_graph - bh::rotation_eci_to_nsw(x_sc, x_sun_spk)).norm() < 1e-9
    );
    bh::clear_object_registry();
}
Output
1
2
3
4
5
Y . sun_direction > 0: True
|Y . X| < 1e-12: True
Fixed-Sun approximation error within (1e-8, 1e-6) rad/s: True
Round trip error: 2.22e-10
Frame graph matches rotation_eci_to_nsw: True
1
2
3
4
5
Y . sun_direction > 0: true
|Y . X| < 1e-12: true
Fixed-Sun approximation error within (1e-8, 1e-6) rad/s: true
Round trip error: 2.22e-10
Frame graph matches rotation_eci_to_nsw: true

See Also