summaryrefslogtreecommitdiff
path: root/src/math.rs
blob: 4f0558cc3e108cd524b2c8ce4ca63a33816e5995 (plain)
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
extern crate rand;

use self::rand::distributions::Alphanumeric;
use self::rand::Rng;
use crate::constants;
use crate::modules::types::ModuleType;
use std::iter::repeat;

pub fn rand_name() -> String {
    repeat(())
        .map(|()| rand::thread_rng().sample(Alphanumeric))
        .take(8)
        .collect()
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ControlSystem {
    previous_error: f64,
    integral: f64,
    kp: f64,
    ki: f64,
    kd: f64,
    dt: f64,
}

impl ControlSystem {
    pub fn new(module: ModuleType) -> ControlSystem {
        let previous_error = 0.0;
        let integral = 0.0;
        match module {
            ModuleType::Tractorbeam => ControlSystem {
                kp: constants::SHIP_TRACTORBEAM_CONTROLSYSTEM_KP,
                ki: constants::SHIP_TRACTORBEAM_CONTROLSYSTEM_KI,
                kd: constants::SHIP_TRACTORBEAM_CONTROLSYSTEM_KD,
                dt: constants::SHIP_TRACTORBEAM_CONTROLSYSTEM_DT,
                previous_error,
                integral,
            },
            _ => ControlSystem {
                kp: 1.0,
                ki: 1.0,
                kd: 1.0,
                dt: 1.0,
                previous_error,
                integral,
            },
        }
    }

    pub fn compute(&mut self, strength: f64, distance: f64, desired_distance: f64) -> f64 {
        let error = desired_distance - distance;
        self.integral += error * self.dt;
        let derivative = (error - self.previous_error) / self.dt;
        let output = self.kp * error + self.ki * self.integral + self.kd * derivative;
        self.previous_error = error;

        if output.abs() > strength {
            if output.is_sign_positive() {
                strength
            } else {
                strength * -1.0
            }
        } else {
            output
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub struct Vector {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Vector {
    pub fn new(x: f64, y: f64, z: f64) -> Vector {
        Vector { x, y, z }
    }

    pub fn distance_from(&self, other: Vector) -> f64 {
        ((self.x - other.x).powf(2.0) + (self.y - other.y).powf(2.0) + (self.z - other.z).powf(2.0))
            .sqrt()
    }

    pub fn unitize(&self) -> Vector {
        let denominator = self.magnitude();
        Vector {
            x: self.x / denominator,
            y: self.y / denominator,
            z: self.z / denominator,
        }
    }

    pub fn magnitude(&self) -> f64 {
        (self.x.powf(2.0) + self.y.powf(2.0) + self.z.powf(2.0)).sqrt()
    }
}

impl std::fmt::Display for Vector {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "({:.2}, {:.2}, {:.2})", self.x, self.y, self.z)
    }
}

impl std::ops::Add for Vector {
    type Output = Vector;

    fn add(self, other: Vector) -> Vector {
        Vector {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
        }
    }
}

impl std::ops::Sub for Vector {
    type Output = Vector;

    fn sub(self, other: Vector) -> Vector {
        Vector {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
        }
    }
}

impl std::ops::Mul<f64> for Vector {
    type Output = Vector;

    fn mul(self, other: f64) -> Vector {
        Vector {
            x: self.x * other,
            y: self.y * other,
            z: self.z * other,
        }
    }
}

impl std::ops::AddAssign for Vector {
    fn add_assign(&mut self, other: Vector) {
        *self = Vector {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
        }
    }
}