use crate::frame::ItrfPositionM;
pub use crate::constants::HALF_WEEK_S;
pub use crate::constants::SECONDS_PER_WEEK;
pub const KEPLER_TOL: f64 = 1.0e-12;
pub const KEPLER_MAX_ITER: usize = 30;
pub const CLOCK_MAX_ITER: usize = 2;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ConstellationConstants {
pub gm_m3_s2: f64,
pub omega_e_rad_s: f64,
pub dtr_f: f64,
}
impl ConstellationConstants {
pub const GPS: Self = Self {
gm_m3_s2: 3.9860050E14,
omega_e_rad_s: 7.2921151467E-5,
dtr_f: -0.000000000444280763339306,
};
pub const GALILEO: Self = Self {
gm_m3_s2: 3.986004418E14,
omega_e_rad_s: 7.2921151467E-5,
dtr_f: -0.00000000044428073090439775,
};
pub const BEIDOU: Self = Self {
gm_m3_s2: 3.986004418E14,
omega_e_rad_s: 7.292115E-5,
dtr_f: -0.00000000044428073090439775,
};
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct KeplerianElements {
pub sqrt_a: f64,
pub e: f64,
pub m0: f64,
pub delta_n: f64,
pub omega0: f64,
pub i0: f64,
pub omega: f64,
pub omega_dot: f64,
pub idot: f64,
pub cuc: f64,
pub cus: f64,
pub crc: f64,
pub crs: f64,
pub cic: f64,
pub cis: f64,
pub toe_sow: f64,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ClockPolynomial {
pub af0: f64,
pub af1: f64,
pub af2: f64,
pub toc_sow: f64,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EccentricAnomaly {
pub value: f64,
pub iterations: usize,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OrbitState {
pub a: f64,
pub n0: f64,
pub n: f64,
pub tk: f64,
pub mk: f64,
pub eccentric_anomaly: f64,
pub kepler_iterations: usize,
pub sin_e: f64,
pub cos_e: f64,
pub nu: f64,
pub phi: f64,
pub s2: f64,
pub c2: f64,
pub du: f64,
pub dr: f64,
pub di: f64,
pub u: f64,
pub r: f64,
pub i: f64,
pub xp: f64,
pub yp: f64,
pub omega_k: f64,
pub x_m: f64,
pub y_m: f64,
pub z_m: f64,
}
impl OrbitState {
pub const fn position(&self) -> ItrfPositionM {
ItrfPositionM::new(self.x_m, self.y_m, self.z_m)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ClockOffset {
pub dt_clock_poly_s: f64,
pub dt_rel_s: f64,
pub tgd_s: f64,
pub dt_clock_total_s: f64,
}
pub fn time_from_reference_s(t_sow_s: f64, t_ref_sow_s: f64) -> f64 {
let mut dt = t_sow_s - t_ref_sow_s;
if dt > HALF_WEEK_S {
dt -= SECONDS_PER_WEEK;
}
if dt < -HALF_WEEK_S {
dt += SECONDS_PER_WEEK;
}
dt
}
pub fn eccentric_anomaly(mean_anomaly_rad: f64, eccentricity: f64) -> EccentricAnomaly {
let mut e_k = mean_anomaly_rad;
let mut iterations = 0usize;
while iterations < KEPLER_MAX_ITER {
let e_prev = e_k;
e_k = mean_anomaly_rad + eccentricity * e_prev.sin();
iterations += 1;
let delta = (e_k - e_prev).abs();
if delta <= KEPLER_TOL {
break;
}
}
EccentricAnomaly {
value: e_k,
iterations,
}
}
pub fn satellite_position_ecef(
elements: &KeplerianElements,
consts: &ConstellationConstants,
t_sow_s: f64,
is_geo: bool,
) -> OrbitState {
let sqrt_a = elements.sqrt_a;
let e = elements.e;
let gm = consts.gm_m3_s2;
let omega_e = consts.omega_e_rad_s;
let a = sqrt_a * sqrt_a;
let n0 = (gm / (a * a * a)).sqrt();
let n = n0 + elements.delta_n;
let tk = time_from_reference_s(t_sow_s, elements.toe_sow);
let mk = elements.m0 + n * tk;
let kepler = eccentric_anomaly(mk, e);
let ecc_anom = kepler.value;
let sin_e = ecc_anom.sin();
let cos_e = ecc_anom.cos();
let e2 = e * e;
let nu = ((1.0 - e2).sqrt() * sin_e).atan2(cos_e - e);
let phi = nu + elements.omega;
let two_phi = 2.0 * phi;
let s2 = two_phi.sin();
let c2 = two_phi.cos();
let du = elements.cus * s2 + elements.cuc * c2;
let dr = elements.crs * s2 + elements.crc * c2;
let di = elements.cis * s2 + elements.cic * c2;
let u = phi + du;
let r = a * (1.0 - e * cos_e) + dr;
let i = elements.i0 + di + elements.idot * tk;
let xp = r * u.cos();
let yp = r * u.sin();
let omega_k = if is_geo {
elements.omega0 + elements.omega_dot * tk - omega_e * elements.toe_sow
} else {
elements.omega0 + (elements.omega_dot - omega_e) * tk - omega_e * elements.toe_sow
};
let sin_o = omega_k.sin();
let cos_o = omega_k.cos();
let sin_i = i.sin();
let cos_i = i.cos();
let xg = xp * cos_o - yp * cos_i * sin_o;
let yg = xp * sin_o + yp * cos_i * cos_o;
let zg = yp * sin_i;
let (x, y, z) = if is_geo {
let deg5 = 5.0_f64.to_radians();
let cos_phi = deg5.cos();
let sin_phi = -deg5.sin();
let z_ang = omega_e * tk;
let cos_z = z_ang.cos();
let sin_z = z_ang.sin();
let yr = yg * cos_phi + zg * sin_phi;
let zr = -yg * sin_phi + zg * cos_phi;
(xg * cos_z + yr * sin_z, -xg * sin_z + yr * cos_z, zr)
} else {
(xg, yg, zg)
};
OrbitState {
a,
n0,
n,
tk,
mk,
eccentric_anomaly: ecc_anom,
kepler_iterations: kepler.iterations,
sin_e,
cos_e,
nu,
phi,
s2,
c2,
du,
dr,
di,
u,
r,
i,
xp,
yp,
omega_k,
x_m: x,
y_m: y,
z_m: z,
}
}
pub fn satellite_clock_offset_s(
clock: &ClockPolynomial,
consts: &ConstellationConstants,
elements: &KeplerianElements,
sin_e: f64,
t_sow_s: f64,
tgd_s: f64,
) -> ClockOffset {
let af0 = clock.af0;
let af1 = clock.af1;
let af2 = clock.af2;
let dt0 = time_from_reference_s(t_sow_s, clock.toc_sow);
let mut dt = dt0;
let mut refine = 0usize;
while refine < CLOCK_MAX_ITER {
dt = dt0 - (af0 + af1 * dt + af2 * dt * dt);
refine += 1;
}
let dt_poly = af0 + af1 * dt + af2 * dt * dt;
let dt_rel = consts.dtr_f * elements.e * elements.sqrt_a * sin_e;
let dt_total = dt_poly + dt_rel - tgd_s;
ClockOffset {
dt_clock_poly_s: dt_poly,
dt_rel_s: dt_rel,
tgd_s,
dt_clock_total_s: dt_total,
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SatelliteState {
pub orbit: OrbitState,
pub clock: ClockOffset,
}
pub fn satellite_state(
elements: &KeplerianElements,
clock: &ClockPolynomial,
consts: &ConstellationConstants,
t_sow_s: f64,
tgd_s: f64,
is_geo: bool,
) -> SatelliteState {
let orbit = satellite_position_ecef(elements, consts, t_sow_s, is_geo);
let clock = satellite_clock_offset_s(clock, consts, elements, orbit.sin_e, t_sow_s, tgd_s);
SatelliteState { orbit, clock }
}
#[cfg(test)]
mod tests;