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
//! Mathematical equations for Step 2 in third-order velocity interface: Time synchronization

use crate::profile::{ControlSigns, Profile, ReachedLimits};

pub struct VelocityThirdOrderStep2 {
    a0: f64,
    tf: f64,
    af: f64,
    _a_max: f64,
    _a_min: f64,
    _j_max: f64,
    vd: f64,
    ad: f64,
}

impl VelocityThirdOrderStep2 {
    pub fn new(
        tf: f64,
        v0: f64,
        a0: f64,
        vf: f64,
        af: f64,
        a_max: f64,
        a_min: f64,
        j_max: f64,
    ) -> Self {
        Self {
            a0,
            tf,
            af,
            _a_max: a_max,
            _a_min: a_min,
            _j_max: j_max,
            vd: vf - v0,
            ad: af - a0,
        }
    }

    fn time_acc0(&mut self, profile: &mut Profile, a_max: f64, a_min: f64, j_max: f64) -> bool {
        // UD Solution 1/2
        {
            let h1 = f64::sqrt(
                (-self.ad * self.ad
                    + 2.0 * j_max * ((self.a0 + self.af) * self.tf - 2.0 * self.vd))
                    / (j_max * j_max)
                    + self.tf * self.tf,
            );

            profile.t[0] = self.ad / (2.0 * j_max) + (self.tf - h1) / 2.0;
            profile.t[1] = h1;
            profile.t[2] = self.tf - (profile.t[0] + h1);
            profile.t[3] = 0.0;
            profile.t[4] = 0.0;
            profile.t[5] = 0.0;
            profile.t[6] = 0.0;

            if profile.check_for_velocity(
                ControlSigns::UDDU,
                ReachedLimits::Acc0,
                j_max,
                a_max,
                a_min,
            ) {
                profile.pf = *profile.p.last().unwrap();
                return true;
            }
        }

        // UU Solution
        {
            let h1 = -self.ad + j_max * self.tf;

            profile.t[0] =
                -self.ad * self.ad / (2.0 * j_max * h1) + (self.vd - self.a0 * self.tf) / h1;
            profile.t[1] = -self.ad / j_max + self.tf;
            profile.t[2] = 0.0;
            profile.t[3] = 0.0;
            profile.t[4] = 0.0;
            profile.t[5] = 0.0;
            profile.t[6] = self.tf - (profile.t[0] + profile.t[1]);

            if profile.check_for_velocity(
                ControlSigns::UDDU,
                ReachedLimits::Acc0,
                j_max,
                a_max,
                a_min,
            ) {
                profile.pf = *profile.p.last().unwrap();
                return true;
            }
        }

        // UU Solution - 2 step
        {
            profile.t[0] = 0.0;
            profile.t[1] = -self.ad / j_max + self.tf;
            profile.t[2] = 0.0;
            profile.t[3] = 0.0;
            profile.t[4] = 0.0;
            profile.t[5] = 0.0;
            profile.t[6] = self.ad / j_max;

            if profile.check_for_velocity(
                ControlSigns::UDDU,
                ReachedLimits::Acc0,
                j_max,
                a_max,
                a_min,
            ) {
                profile.pf = *profile.p.last().unwrap();
                return true;
            }
        }

        false
    }

    fn time_none(&mut self, profile: &mut Profile, a_max: f64, a_min: f64, j_max: f64) -> bool {
        if f64::abs(self.a0) < f64::EPSILON
            && f64::abs(self.af) < f64::EPSILON
            && f64::abs(self.vd) < f64::EPSILON
        {
            profile.t[0] = 0.0;
            profile.t[1] = self.tf;
            profile.t[2] = 0.0;
            profile.t[3] = 0.0;
            profile.t[4] = 0.0;
            profile.t[5] = 0.0;
            profile.t[6] = 0.0;

            if profile.check_for_velocity(
                ControlSigns::UDDU,
                ReachedLimits::None,
                j_max,
                a_max,
                a_min,
            ) {
                profile.pf = *profile.p.last().unwrap();
                return true;
            }
        }

        // UD Solution 1/2
        {
            let h1 = 2.0 * (self.af * self.tf - self.vd);

            profile.t[0] = h1 / self.ad;
            profile.t[1] = self.tf - profile.t[0];
            profile.t[2] = 0.0;
            profile.t[3] = 0.0;
            profile.t[4] = 0.0;
            profile.t[5] = 0.0;
            profile.t[6] = 0.0;

            let jf = self.ad * self.ad / h1;

            if profile.check_for_velocity(
                ControlSigns::UDDU,
                ReachedLimits::None,
                jf,
                a_max,
                a_min,
            ) {
                profile.pf = *profile.p.last().unwrap();
                return true;
            }
        }

        false
    }

    fn check_all(&mut self, profile: &mut Profile, a_max: f64, a_min: f64, j_max: f64) -> bool {
        self.time_acc0(profile, a_max, a_min, j_max)
            || self.time_none(profile, a_max, a_min, j_max)
    }

    pub fn get_profile(&mut self, profile: &mut Profile) -> bool {
        // Test all cases to get ones that match
        // However we should guess which one is correct and try them first...
        if self.vd > 0.0 {
            return self.check_all(profile, self._a_max, self._a_min, self._j_max)
                || self.check_all(profile, self._a_min, self._a_max, -self._j_max);
        }

        self.check_all(profile, self._a_min, self._a_max, -self._j_max)
            || self.check_all(profile, self._a_max, self._a_min, self._j_max)
    }
}