nyx_space/dynamics/
deltavctrl.rs

1/*
2    Nyx, blazing fast astrodynamics
3    Copyright (C) 2018-onwards Christopher Rabotin <christopher.rabotin@gmail.com>
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU Affero General Public License for more details.
14
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19use crate::cosmic::Orbit;
20use crate::linalg::Vector3;
21
22pub use super::guidance::Maneuver;
23
24/// The `DeltaVctrl` trait handles control laws, optimizations, and other such methods for
25/// controlling the change in velocity of a point mass during a mission arc (`MissionArc`).
26pub trait DeltaVctrl
27where
28    Self: Clone + Sized,
29{
30    /// Returns the control vector corresponding to the change in velocity direction in the inertial frame.
31    fn ctrl_vector(&self, state: &Orbit) -> Vector3<f64>;
32
33    /// Prepares the controller for the next maneuver (called from set_state of the dynamics).
34    fn next(&mut self, state: &Orbit);
35}
36
37#[derive(Clone, Debug)]
38pub struct ImpulsiveBurns {
39    /// Maneuvers should be provided in chronological order, first maneuver first in the list
40    pub mnvrs: Vec<Maneuver>,
41    pub mnvr_no: usize,
42}
43
44impl ImpulsiveBurns {
45    /// Builds a schedule from the vector of maneuvers, must be provided in chronological order.
46    pub fn from_mnvrs(mnvrs: Vec<Maneuver>) -> Self {
47        Self { mnvrs, mnvr_no: 0 }
48    }
49}
50
51impl DeltaVctrl for ImpulsiveBurns {
52    fn ctrl_vector(&self, state: &Orbit) -> Vector3<f64> {
53        if self.mnvr_no >= self.mnvrs.len() {
54            Vector3::zeros()
55        } else {
56            let next_mnvr = self.mnvrs[self.mnvr_no];
57            if next_mnvr.start <= state.epoch && next_mnvr.end >= state.epoch {
58                state.dcm_from_vnc_to_inertial().unwrap() * next_mnvr.vector(state.epoch)
59            } else {
60                Vector3::zeros()
61            }
62        }
63    }
64
65    fn next(&mut self, state: &Orbit) {
66        if self.mnvr_no < self.mnvrs.len() {
67            let cur_mnvr = self.mnvrs[self.mnvr_no];
68            if state.epoch >= cur_mnvr.end {
69                self.mnvr_no += 1;
70            }
71        }
72    }
73}