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
use crate::act::SyncDriveFuture;
use crate::data::MicroSteps;
use crate::math::movements::DefinedActuator;
use crate::prelude::StepperActuator;
use crate::{SyncActuator, Setup, StepperConfig, AsyncActuator};

use syunit::*;

use super::Interruptible;

pub trait ActuatorParent {
    type Child;

    fn child(&self) -> &Self::Child;

    fn child_mut(&mut self) -> &mut Self::Child; 
}

// Relationships
    /// A parent that relates to its child through a constant `ratio`
    pub trait RatioActuatorParent : ActuatorParent {
        /// The linear ratio that defines the relation between the child and the parent component
        /// 
        /// # Ratio
        /// 
        /// For each radian/mm the child moves, the parent moves this distances *times the `ratio`*
        fn ratio(&self) -> f32;

        // Automatic implementations
            fn gamma_for_child(&self, parent_gamma : Gamma) -> Gamma {
                parent_gamma / self.ratio()
            }

            fn gamma_for_parent(&self, child_gamma : Gamma) -> Gamma {
                child_gamma * self.ratio()
            }

            fn delta_for_chlid(&self, parent_delta : Delta) -> Delta {
                parent_delta / self.ratio()
            }

            fn delta_for_parent(&self, child_delta : Delta) -> Delta {
                child_delta * self.ratio()
            }

            fn velocity_for_child(&self, parent_velocity : Velocity) -> Velocity {
                parent_velocity / self.ratio()
            }

            fn velocity_for_parent(&self, child_velocity : Velocity) -> Velocity {
                child_velocity * self.ratio()
            }

            fn alpha_for_child(&self, parent_alpha : Acceleration) -> Acceleration {
                parent_alpha / self.ratio()
            }

            fn alpha_for_parent(&self, child_alpha : Acceleration) -> Acceleration {
                child_alpha * self.ratio()
            }

            // Implementation for Newtonmeter to Newtonmeter conversion
            fn force_for_child(&self, parent_force : Force) -> Force {
                parent_force * self.ratio()
            }

            fn force_for_parent(&self, child_force : Force) -> Force {
                child_force / self.ratio()
            }

            // Implementation for Newtonmeter to Newtonmeter conversion
            fn inertia_for_child(&self, parent_inertia : Inertia) -> Inertia {
                parent_inertia * self.ratio() * self.ratio()
            }

            fn inertia_for_parent(&self, child_intertia : Inertia) -> Inertia {
                child_intertia / self.ratio() / self.ratio()
            }
        // 
    }
// 

// ##################################
// #    AUTOMATIC IMPLEMENTATION    #
// ##################################
// 
// Automatically implements `SyncActor` for every component
    impl<T : RatioActuatorParent + Setup> SyncActuator for T 
    where
        T::Child : SyncActuator
    {
        // Drive
            fn drive_rel(&mut self, mut delta : Delta, speed : Factor) -> SyncDriveFuture {
                delta = self.delta_for_chlid(delta);
                self.child_mut().drive_rel(delta, speed)
            }
        //  

        // Position
            fn gamma(&self) -> Gamma {
                self.gamma_for_parent(self.child().gamma())
            }

            fn set_gamma(&mut self, mut gamma : Gamma) {
                gamma = self.gamma_for_child(gamma);
                self.child_mut().set_gamma(gamma)
            }

            fn velocity_max(&self) -> Velocity {
                self.velocity_for_parent(self.child().velocity_max())
            }

            fn set_velocity_max(&mut self, mut velocity_max : Velocity) {
                velocity_max = self.velocity_for_child(velocity_max);
                self.child_mut().set_velocity_max(velocity_max)
            }

            fn limits_for_gamma(&self, gamma : Gamma) -> Delta {
                self.delta_for_parent(self.child().limits_for_gamma(
                    self.gamma_for_child(gamma)
                ))
            }

            fn set_limits(&mut self, mut min : Option<Gamma>, mut max : Option<Gamma>) {
                min = min.map(|g| self.gamma_for_child(g));
                max = max.map(|g| self.gamma_for_child(g));
                self.child_mut().set_limits(min, max)
            }

            fn set_end(&mut self, mut set_gamma : Gamma) {
                set_gamma = self.gamma_for_child(set_gamma);
                self.child_mut().set_end(set_gamma)
            }

            fn overwrite_limits(&mut self, mut min : Option<Gamma>, mut max : Option<Gamma>) {
                min = min.map(|g| self.gamma_for_child(g));
                max = max.map(|g| self.gamma_for_child(g));
                self.child_mut().overwrite_limits(min, max)
            }
        // 

        // Loads
            fn force_gen(&self) -> Force {
                self.force_for_parent(self.child().force_gen())
            }

            fn force_dir(&self) -> Force {
                self.force_for_parent(self.child().force_dir())
            }

            fn apply_gen_force(&mut self, mut force : Force) -> Result<(), crate::Error> {
                force = self.force_for_child(force);
                self.child_mut().apply_gen_force(force)
            }

            fn apply_dir_force(&mut self, mut force : Force) -> Result<(), crate::Error> {
                force = self.force_for_child(force);
                self.child_mut().apply_dir_force(force)
            }

            fn inertia(&self) -> Inertia {
                self.inertia_for_parent(self.child().inertia())
            }

            fn apply_inertia(&mut self, mut inertia : Inertia) {
                inertia = self.inertia_for_child(inertia);
                self.child_mut().apply_inertia(inertia)
            }
        // 
    }

    impl<T : RatioActuatorParent + Setup> StepperActuator for T 
    where
        T::Child : StepperActuator
    {
        fn consts(&self) -> &crate::StepperConst {
            self.child().consts()
        }

        // Config
            fn config(&self) -> &crate::StepperConfig {
                self.child().config()
            }

            fn set_config(&mut self, config : StepperConfig) {
                self.child_mut().set_config(config)
            }
        //

        // Microsteps
            fn microsteps(&self) -> MicroSteps {
                self.child().microsteps()
            }

            fn set_microsteps(&mut self, micro : MicroSteps) {
                self.child_mut().set_microsteps(micro)
            }
        // 

        fn step_ang(&self) -> Delta {
            self.delta_for_parent(self.child().step_ang())
        }
    }

    impl<T : ActuatorParent> Interruptible for T 
    where
        T::Child : Interruptible
    {
        fn add_interruptor(&mut self, interruptor : Box<dyn super::Interruptor + Send>) {
            self.child_mut().add_interruptor(interruptor)
        }

        fn intr_reason(&mut self) -> Option<super::InterruptReason> {
            self.child_mut().intr_reason()
        }
    }

    impl<T : ActuatorParent + Setup> AsyncActuator for T
    where 
        T::Child : AsyncActuator 
    {
        fn drive(&mut self, dir : Direction, speed : Factor) -> Result<(), crate::Error> {
            self.child_mut().drive(dir, speed)
        }

        fn dir(&self) -> Direction {
            self.child().dir()
        }

        fn speed(&self) -> Factor {
            self.child().speed()
        }
    }

    // Movements
    impl<T : ActuatorParent> DefinedActuator for T 
    where
        T::Child : DefinedActuator
    {
        fn ptp_time_for_distance(&self, gamma_0 : Gamma, gamma_t : Gamma) -> Time {
            self.child().ptp_time_for_distance(gamma_0, gamma_t)
        }
    }
//