nyx_space/dynamics/guidance/
mnvr.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
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
204
205
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
    Nyx, blazing fast astrodynamics
    Copyright (C) 2018-onwards Christopher Rabotin <christopher.rabotin@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

use super::{
    ra_dec_from_unit_vector, GuidanceError, GuidanceLaw, GuidancePhysicsSnafu, LocalFrame,
};
use crate::cosmic::{GuidanceMode, Spacecraft};
use crate::dynamics::guidance::unit_vector_from_ra_dec;
use crate::linalg::Vector3;
use crate::polyfit::CommonPolynomial;
use crate::time::{Epoch, Unit};
use crate::State;
use anise::prelude::Almanac;
use hifitime::{Duration, TimeUnits};
use serde::{Deserialize, Serialize};
use snafu::ResultExt;
use std::fmt;
use std::sync::Arc;

/// Mnvr defined a single maneuver. Direction MUST be in the VNC frame (Velocity / Normal / Cross).
/// It may be used with a maneuver scheduler.
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Maneuver {
    /// Start epoch of the maneuver
    pub start: Epoch,
    /// End epoch of the maneuver
    pub end: Epoch,
    /// TODO: Add a thruster group set to specify which set of thrusters to use for this maneuver, should be a key to a thruster (maybe change thruster to a hashmap actually now that I don't care about embedded stuff).
    /// Thrust level, if 1.0 use all thruster available at full power
    /// TODO: Convert this to a common polynomial as well to optimize throttle, throttle rate (and accel?)
    pub thrust_prct: f64,
    /// The representation of this maneuver.
    pub representation: MnvrRepr,
    /// The frame in which the maneuvers are defined.
    pub frame: LocalFrame,
}

impl fmt::Display for Maneuver {
    /// Prints the polynomial with the least significant coefficients first
    #[allow(clippy::identity_op)]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.end != self.start {
            let start_vec = self.vector(self.start);
            let end_vec = self.vector(self.end);
            write!(
                f,
                "Finite burn maneuver @ {:.2}% on {} for {} (ending on {})",
                100.0 * self.thrust_prct,
                self.start,
                self.end - self.start,
                self.end,
            )?;
            write!(f, "\n{}", self.representation)?;
            write!(
                f,
                "\n\tinitial dir: [{:.6}, {:.6}, {:.6}]\n\tfinal dir  : [{:.6}, {:.6}, {:.6}]",
                start_vec[0], start_vec[1], start_vec[2], end_vec[0], end_vec[1], end_vec[2]
            )
        } else {
            write!(
                f,
                "Impulsive maneuver @ {}\n{}",
                self.start, self.representation
            )
        }
    }
}

/// Defines the available maneuver representations.
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum MnvrRepr {
    /// Represents the maneuver as a fixed vector in the local frame.
    Vector(Vector3<f64>),
    /// Represents the maneuver as a polynominal of azimuth (right ascension / in-plane) and elevation (declination / out of plane)
    Angles {
        azimuth: CommonPolynomial,
        elevation: CommonPolynomial,
    },
}

impl fmt::Display for MnvrRepr {
    /// Prints the polynomial with the least significant coefficients first
    #[allow(clippy::identity_op)]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            MnvrRepr::Vector(vector) => write!(f, "{vector}"),
            MnvrRepr::Angles { azimuth, elevation } => write!(
                f,
                "\tazimuth (in-plane) α: {}\n\televation (out-of-plane) β: {}",
                azimuth, elevation
            ),
        }
    }
}

impl Maneuver {
    /// Creates an impulsive maneuver whose vector is the deltaV.
    /// TODO: This should use William's algorithm
    pub fn from_impulsive(dt: Epoch, vector: Vector3<f64>, frame: LocalFrame) -> Self {
        Self::from_time_invariant(dt, dt + Unit::Millisecond, 1.0, vector, frame)
    }

    /// Creates a maneuver from the provided time-invariant delta-v, in km/s
    pub fn from_time_invariant(
        start: Epoch,
        end: Epoch,
        thrust_lvl: f64,
        vector: Vector3<f64>,
        frame: LocalFrame,
    ) -> Self {
        Self {
            start,
            end,
            thrust_prct: thrust_lvl,
            representation: MnvrRepr::Vector(vector),
            frame,
        }
    }

    /// Return the thrust vector computed at the provided epoch
    pub fn vector(&self, epoch: Epoch) -> Vector3<f64> {
        match self.representation {
            MnvrRepr::Vector(vector) => vector,
            MnvrRepr::Angles { azimuth, elevation } => {
                let t = (epoch - self.start).to_seconds();
                let alpha = azimuth.eval(t);
                let delta = elevation.eval(t);
                unit_vector_from_ra_dec(alpha, delta)
            }
        }
    }

    /// Return the duration of this maneuver
    pub fn duration(&self) -> Duration {
        self.end - self.start
    }

    /// Return whether this is an antichronological maneuver
    pub fn antichronological(&self) -> bool {
        self.duration().abs() > 1.microseconds() && self.duration() < 1.microseconds()
    }

    /// Returns the direction of the burn at the start of the burn, useful for setting new angles
    pub fn direction(&self) -> Vector3<f64> {
        match self.representation {
            MnvrRepr::Vector(vector) => vector / vector.norm(),
            MnvrRepr::Angles { azimuth, elevation } => {
                let alpha = azimuth.coeff_in_order(0).unwrap();
                let delta = elevation.coeff_in_order(0).unwrap();
                unit_vector_from_ra_dec(alpha, delta)
            }
        }
    }

    /// Set the time-invariant direction for this finite burn while keeping the other components as they are
    pub fn set_direction(&mut self, vector: Vector3<f64>) -> Result<(), GuidanceError> {
        self.set_direction_and_rates(vector, self.rate(), self.accel())
    }

    /// Returns the rate of direction of the burn at the start of the burn, useful for setting new angles
    pub fn rate(&self) -> Vector3<f64> {
        match self.representation {
            MnvrRepr::Vector(_) => Vector3::zeros(),
            MnvrRepr::Angles { azimuth, elevation } => match azimuth.coeff_in_order(1) {
                Ok(alpha) => {
                    let delta = elevation.coeff_in_order(1).unwrap();
                    unit_vector_from_ra_dec(alpha, delta)
                }
                Err(_) => Vector3::zeros(),
            },
        }
    }

    /// Set the rate of direction for this finite burn while keeping the other components as they are
    pub fn set_rate(&mut self, rate: Vector3<f64>) -> Result<(), GuidanceError> {
        self.set_direction_and_rates(self.direction(), rate, self.accel())
    }

    /// Returns the acceleration of the burn at the start of the burn, useful for setting new angles
    pub fn accel(&self) -> Vector3<f64> {
        match self.representation {
            MnvrRepr::Vector(_) => Vector3::zeros(),
            MnvrRepr::Angles { azimuth, elevation } => match azimuth.coeff_in_order(2) {
                Ok(alpha) => {
                    let delta = elevation.coeff_in_order(2).unwrap();
                    unit_vector_from_ra_dec(alpha, delta)
                }
                Err(_) => Vector3::zeros(),
            },
        }
    }

    /// Set the acceleration of the direction of this finite burn while keeping the other components as they are
    pub fn set_accel(&mut self, accel: Vector3<f64>) -> Result<(), GuidanceError> {
        self.set_direction_and_rates(self.direction(), self.rate(), accel)
    }

    /// Set the initial direction, direction rate, and direction acceleration for this finite burn
    pub fn set_direction_and_rates(
        &mut self,
        dir: Vector3<f64>,
        rate: Vector3<f64>,
        accel: Vector3<f64>,
    ) -> Result<(), GuidanceError> {
        if rate.norm() < f64::EPSILON && accel.norm() < f64::EPSILON {
            // Set as a vector
            self.representation = MnvrRepr::Vector(dir)
        } else {
            let (alpha, delta) = ra_dec_from_unit_vector(dir);
            if alpha.is_nan() || delta.is_nan() {
                return Err(GuidanceError::InvalidDirection {
                    x: dir[0],
                    y: dir[1],
                    z: dir[2],
                    in_plane_deg: alpha.to_degrees(),
                    out_of_plane_deg: delta.to_degrees(),
                });
            }
            if rate.norm() < f64::EPSILON && accel.norm() < f64::EPSILON {
                self.representation = MnvrRepr::Angles {
                    azimuth: CommonPolynomial::Constant(alpha),
                    elevation: CommonPolynomial::Constant(delta),
                };
            } else {
                let (alpha_dt, delta_dt) = ra_dec_from_unit_vector(rate);
                if alpha_dt.is_nan() || delta_dt.is_nan() {
                    return Err(GuidanceError::InvalidRate {
                        x: rate[0],
                        y: rate[1],
                        z: rate[2],
                        in_plane_deg_s: alpha_dt.to_degrees(),
                        out_of_plane_deg_s: delta_dt.to_degrees(),
                    });
                }
                if accel.norm() < f64::EPSILON {
                    self.representation = MnvrRepr::Angles {
                        azimuth: CommonPolynomial::Linear(alpha_dt, alpha),
                        elevation: CommonPolynomial::Linear(delta_dt, delta),
                    };
                } else {
                    let (alpha_ddt, delta_ddt) = ra_dec_from_unit_vector(accel);
                    if alpha_ddt.is_nan() || delta_ddt.is_nan() {
                        return Err(GuidanceError::InvalidAcceleration {
                            x: accel[0],
                            y: accel[1],
                            z: accel[2],
                            in_plane_deg_s2: alpha_ddt.to_degrees(),
                            out_of_plane_deg_s2: delta_ddt.to_degrees(),
                        });
                    }

                    self.representation = MnvrRepr::Angles {
                        azimuth: CommonPolynomial::Quadratic(alpha_ddt, alpha_dt, alpha),
                        elevation: CommonPolynomial::Quadratic(delta_ddt, delta_dt, delta),
                    };
                }
            }
        }
        Ok(())
    }
}

impl GuidanceLaw for Maneuver {
    fn direction(&self, osc: &Spacecraft) -> Result<Vector3<f64>, GuidanceError> {
        match osc.mode() {
            GuidanceMode::Thrust => match self.frame {
                LocalFrame::Inertial => Ok(self.vector(osc.epoch())),
                _ => Ok(self.frame.dcm_to_inertial(osc.orbit).context({
                    GuidancePhysicsSnafu {
                        action: "computing RCN frame",
                    }
                })? * self.vector(osc.epoch())),
            },
            _ => Ok(Vector3::zeros()),
        }
    }

    fn throttle(&self, osc: &Spacecraft) -> Result<f64, GuidanceError> {
        // match self.next(osc) {
        match osc.mode() {
            GuidanceMode::Thrust => Ok(self.thrust_prct),
            _ => {
                // We aren't in maneuver mode, so return 0% throttle
                Ok(0.0)
            }
        }
    }

    fn next(&self, sc: &mut Spacecraft, _almanac: Arc<Almanac>) {
        let next_mode = if sc.epoch() >= self.start && sc.epoch() < self.end {
            GuidanceMode::Thrust
        } else {
            GuidanceMode::Coast
        };
        sc.mut_mode(next_mode);
    }
}

#[cfg(test)]
mod ut_mnvr {
    use hifitime::Epoch;
    use nalgebra::Vector3;

    use crate::dynamics::guidance::LocalFrame;

    use super::Maneuver;

    #[test]
    fn serde_mnvr() {
        let epoch = Epoch::from_gregorian_utc_at_midnight(2012, 2, 29);
        let mnvr = Maneuver::from_impulsive(epoch, Vector3::new(1.0, 1.0, 0.0), LocalFrame::RCN);

        let mnvr_yml = serde_yml::to_string(&mnvr).unwrap();
        println!("{mnvr_yml}");

        let mnvr2 = serde_yml::from_str(&mnvr_yml).unwrap();
        assert_eq!(mnvr, mnvr2);
    }
}