gmt_dos-systems_m1 0.1.0

GMT DOS M1 System
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::fmt::Display;
use std::sync::Arc;

use gmt_dos_actors::framework::network::{ActorOutput, AddActorInput};
use gmt_dos_actors::subsystem::{gateway, GetField};
use gmt_dos_actors::system::System;

use gmt_dos_actors::{framework::model::Check, prelude::*};
use gmt_dos_clients_io::gmt_m1::assembly::{
    M1ActuatorAppliedForces, M1ActuatorCommandForces, M1HardpointsMotion, M1RigidBodyMotions,
};
use gmt_dos_clients_io::gmt_m1::segment::{
    ActuatorAppliedForces, ActuatorCommandForces, HardpointsForces, HardpointsMotion, RBM,
};
use gmt_dos_clients_io::Assembly;
use interface::UniqueIdentifier;

use crate::{
    subsystems::{Segment, SegmentControl},
    Actuators, Calibration, Hardpoints,
};

use super::dispatch::{DispatchIn, DispatchOut};

#[derive(Clone)]
pub enum SegmentControls<const R: usize> {
    S1(SegmentControl<1, R>),
    S2(SegmentControl<2, R>),
    S3(SegmentControl<3, R>),
    S4(SegmentControl<4, R>),
    S5(SegmentControl<5, R>),
    S6(SegmentControl<6, R>),
    S7(SegmentControl<7, R>),
}

impl<const R: usize> SegmentControls<R> {
    pub fn new(id: u8, calibration: &Calibration) -> anyhow::Result<Self> {
        Ok(match id {
            1 => Self::S1(SegmentControl::<1, R>::new(calibration)),
            2 => Self::S2(SegmentControl::<2, R>::new(calibration)),
            3 => Self::S3(SegmentControl::<3, R>::new(calibration)),
            4 => Self::S4(SegmentControl::<4, R>::new(calibration)),
            5 => Self::S5(SegmentControl::<5, R>::new(calibration)),
            6 => Self::S6(SegmentControl::<6, R>::new(calibration)),
            7 => Self::S7(SegmentControl::<7, R>::new(calibration)),
            _ => todo!(),
        })
    }
    pub fn get(&self) -> Option<&dyn Check> {
        match self {
            Self::S1(actor) => Some(actor as &dyn Check),
            Self::S2(actor) => Some(actor as &dyn Check),
            Self::S3(actor) => Some(actor as &dyn Check),
            Self::S4(actor) => Some(actor as &dyn Check),
            Self::S5(actor) => Some(actor as &dyn Check),
            Self::S6(actor) => Some(actor as &dyn Check),
            Self::S7(actor) => Some(actor as &dyn Check),
        }
    }
    pub fn into_model(self) -> Model<Unknown> {
        match self {
            Self::S1(actor) => model!(actor),
            Self::S2(actor) => model!(actor),
            Self::S3(actor) => model!(actor),
            Self::S4(actor) => model!(actor),
            Self::S5(actor) => model!(actor),
            Self::S6(actor) => model!(actor),
            Self::S7(actor) => model!(actor),
        }
    }
    pub fn m1_rigid_body_motions(
        &mut self,
        dispatch: &mut Actor<DispatchIn>,
    ) -> anyhow::Result<()> {
        match self {
            Self::S1(actor) => dispatch.add_output::<RBM<1>>().build().into_input(actor)?,
            Self::S2(actor) => dispatch.add_output::<RBM<2>>().build().into_input(actor)?,
            Self::S3(actor) => dispatch.add_output::<RBM<3>>().build().into_input(actor)?,
            Self::S4(actor) => dispatch.add_output::<RBM<4>>().build().into_input(actor)?,
            Self::S5(actor) => dispatch.add_output::<RBM<5>>().build().into_input(actor)?,
            Self::S6(actor) => dispatch.add_output::<RBM<6>>().build().into_input(actor)?,
            Self::S7(actor) => dispatch.add_output::<RBM<7>>().build().into_input(actor)?,
        };
        Ok(())
    }
    pub fn m1_actuator_command_forces(
        &mut self,
        dispatch: &mut Actor<DispatchIn>,
    ) -> anyhow::Result<()> {
        match self {
            Self::S1(actor) => dispatch
                .add_output::<ActuatorCommandForces<1>>()
                .build()
                .into_input(actor)?,
            Self::S2(actor) => dispatch
                .add_output::<ActuatorCommandForces<2>>()
                .build()
                .into_input(actor)?,
            Self::S3(actor) => dispatch
                .add_output::<ActuatorCommandForces<3>>()
                .build()
                .into_input(actor)?,
            Self::S4(actor) => dispatch
                .add_output::<ActuatorCommandForces<4>>()
                .build()
                .into_input(actor)?,
            Self::S5(actor) => dispatch
                .add_output::<ActuatorCommandForces<5>>()
                .build()
                .into_input(actor)?,
            Self::S6(actor) => dispatch
                .add_output::<ActuatorCommandForces<6>>()
                .build()
                .into_input(actor)?,
            Self::S7(actor) => dispatch
                .add_output::<ActuatorCommandForces<7>>()
                .build()
                .into_input(actor)?,
        };
        Ok(())
    }
    pub fn m1_hardpoints_motion(&mut self, dispatch: &mut Actor<DispatchIn>) -> anyhow::Result<()> {
        match self {
            Self::S1(actor) => dispatch
                .add_output::<HardpointsMotion<1>>()
                .build()
                .into_input(actor)?,
            Self::S2(actor) => dispatch
                .add_output::<HardpointsMotion<2>>()
                .build()
                .into_input(actor)?,
            Self::S3(actor) => dispatch
                .add_output::<HardpointsMotion<3>>()
                .build()
                .into_input(actor)?,
            Self::S4(actor) => dispatch
                .add_output::<HardpointsMotion<4>>()
                .build()
                .into_input(actor)?,
            Self::S5(actor) => dispatch
                .add_output::<HardpointsMotion<5>>()
                .build()
                .into_input(actor)?,
            Self::S6(actor) => dispatch
                .add_output::<HardpointsMotion<6>>()
                .build()
                .into_input(actor)?,
            Self::S7(actor) => dispatch
                .add_output::<HardpointsMotion<7>>()
                .build()
                .into_input(actor)?,
        };
        Ok(())
    }
    pub fn m1_actuator_applied_forces(
        &mut self,
        dispatch: &mut Actor<DispatchOut>,
    ) -> anyhow::Result<()> {
        match self {
            Self::S1(actor) => {
                <SegmentControl<1, R> as AddActorOutput<'_, Actuators<1>, R, 1>>::add_output::<
                    ActuatorAppliedForces<1>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S2(actor) => {
                <SegmentControl<2, R> as AddActorOutput<'_, Actuators<2>, R, 1>>::add_output::<
                    ActuatorAppliedForces<2>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S3(actor) => {
                <SegmentControl<3, R> as AddActorOutput<'_, Actuators<3>, R, 1>>::add_output::<
                    ActuatorAppliedForces<3>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S4(actor) => {
                <SegmentControl<4, R> as AddActorOutput<'_, Actuators<4>, R, 1>>::add_output::<
                    ActuatorAppliedForces<4>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S5(actor) => {
                <SegmentControl<5, R> as AddActorOutput<'_, Actuators<5>, R, 1>>::add_output::<
                    ActuatorAppliedForces<5>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S6(actor) => {
                <SegmentControl<6, R> as AddActorOutput<'_, Actuators<6>, R, 1>>::add_output::<
                    ActuatorAppliedForces<6>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S7(actor) => {
                <SegmentControl<7, R> as AddActorOutput<'_, Actuators<7>, R, 1>>::add_output::<
                    ActuatorAppliedForces<7>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
        };
        Ok(())
    }
    pub fn m1_hardpoints_forces(
        &mut self,
        dispatch: &mut Actor<DispatchOut>,
    ) -> anyhow::Result<()> {
        match self {
            Self::S1(actor) => {
                <SegmentControl<1, R> as AddActorOutput<'_, Hardpoints, 1, 1>>::add_output::<
                    HardpointsForces<1>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S2(actor) => {
                <SegmentControl<2, R> as AddActorOutput<'_, Hardpoints, 1, 1>>::add_output::<
                    HardpointsForces<2>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S3(actor) => {
                <SegmentControl<3, R> as AddActorOutput<'_, Hardpoints, 1, 1>>::add_output::<
                    HardpointsForces<3>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S4(actor) => {
                <SegmentControl<4, R> as AddActorOutput<'_, Hardpoints, 1, 1>>::add_output::<
                    HardpointsForces<4>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S5(actor) => {
                <SegmentControl<5, R> as AddActorOutput<'_, Hardpoints, 1, 1>>::add_output::<
                    HardpointsForces<5>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S6(actor) => {
                <SegmentControl<6, R> as AddActorOutput<'_, Hardpoints, 1, 1>>::add_output::<
                    HardpointsForces<6>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
            Self::S7(actor) => {
                <SegmentControl<7, R> as AddActorOutput<'_, Hardpoints, 1, 1>>::add_output::<
                    HardpointsForces<7>,
                >(actor)
                .build()
                .into_input(dispatch)?
            }
        };
        Ok(())
    }
}

impl<const R: usize> Assembly for M1<R> {}

#[derive(Clone)]
pub struct M1<const R: usize>
where
    Self: Assembly,
{
    segments: Vec<SegmentControls<R>>,
    dispatch_in: Actor<DispatchIn>,
    dispatch_out: Actor<DispatchOut>,
}

impl<const R: usize> System for M1<R> {
    fn output<C: interface::Update, const NI: usize, const NO: usize>(
        &mut self,
    ) -> &mut Actor<C, NI, NO> {
        todo!()
    }

    fn input<CI: interface::Update, const NI: usize, const NO: usize>(
        &mut self,
    ) -> &mut Actor<CI, NI, NO> {
        todo!()
    }

    fn name(&self) -> Option<String> {
        todo!()
    }

    fn build(&mut self) -> anyhow::Result<()> {
        /*         gateway_in
            .add_output()
            .build::<M1RigidBodyMotions>()
            .into_input(&mut self.dispatch_in)?;
        gateway_in
            .add_output()
            .build::<M1ActuatorCommandForces>()
            .into_input(&mut self.dispatch_in)?;
        gateway_in
            .add_output()
            .build::<M1HardpointsMotion>()
            .into_input(&mut self.dispatch_in)?;*/

        self.segments
            .iter_mut()
            .map(|segment| segment.m1_rigid_body_motions(&mut self.dispatch_in))
            .collect::<anyhow::Result<Vec<()>>>()?;
        self.segments
            .iter_mut()
            .map(|segment| segment.m1_actuator_command_forces(&mut self.dispatch_in))
            .collect::<anyhow::Result<Vec<()>>>()?;
        self.segments
            .iter_mut()
            .map(|segment| segment.m1_hardpoints_motion(&mut self.dispatch_in))
            .collect::<anyhow::Result<Vec<()>>>()?;

        self.segments
            .iter_mut()
            .map(|segment| segment.m1_actuator_applied_forces(&mut self.dispatch_out))
            .collect::<anyhow::Result<Vec<()>>>()?;
        self.segments
            .iter_mut()
            .map(|segment| segment.m1_hardpoints_forces(&mut self.dispatch_out))
            .collect::<anyhow::Result<Vec<()>>>()?;

        /*         self.dispatch_out
            .add_output()
            .build::<M1ActuatorAppliedForces>()
            .into_input(gateway_out)?;
        self.dispatch_out
            .add_output()
            .build::<M1HardpointsForces>()
            .into_input(gateway_out)?; */
        Ok(())
    }
}

impl<const R: usize> GetField for M1<R> {
    fn get_field(&self, idx: usize) -> Option<&dyn Check> {
        match idx {
            n if n < <M1<R> as Assembly>::N => {
                self.segments.get(idx).and_then(|segment| segment.get())
            }
            n if n == <M1<R> as Assembly>::N => Some(&self.dispatch_in as &dyn Check),
            n if n == <M1<R> as Assembly>::N + 1 => Some(&self.dispatch_out as &dyn Check),
            _ => None,
        }
    }
}

impl<const R: usize> From<M1<R>> for Model<Unknown> {
    fn from(m1: M1<R>) -> Self {
        m1.segments
            .into_iter()
            .fold(Model::default(), |model, segment| {
                model + segment.into_model()
            })
            + m1.dispatch_in
            + m1.dispatch_out
    }
}

impl<const R: usize> gateway::Gateways for M1<R> {
    type DataType = Vec<Arc<Vec<f64>>>;

    const N_IN: usize = 3;

    const N_OUT: usize = 2;
}

impl<const R: usize> Display for M1<R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "SegmentControls")
    }
}

impl<const R: usize> GetName for M1<R> {
    fn get_name(&self) -> String {
        "integrated_model".into()
    }
}

impl<const R: usize> AddActorInput<M1RigidBodyMotions, DispatchIn, 1, 1> for M1<R> {
    fn add_input(&mut self, rx: flume::Receiver<interface::Data<M1RigidBodyMotions>>, hash: u64) {
        AddActorInput::add_input(&mut self.dispatch_in, rx, hash)
    }
}

impl<const R: usize> AddActorInput<M1ActuatorCommandForces, DispatchIn, 1, 1> for M1<R> {
    fn add_input(
        &mut self,
        rx: flume::Receiver<interface::Data<M1ActuatorCommandForces>>,
        hash: u64,
    ) {
        AddActorInput::add_input(&mut self.dispatch_in, rx, hash)
    }
}

impl<const R: usize> AddActorInput<M1HardpointsMotion, DispatchIn, 1, 1> for M1<R> {
    fn add_input(&mut self, rx: flume::Receiver<interface::Data<M1HardpointsMotion>>, hash: u64) {
        AddActorInput::add_input(&mut self.dispatch_in, rx, hash)
    }
}

impl<'a, const R: usize> AddActorOutput<'a, DispatchOut, 1, 1> for M1<R> {
    fn add_output<M1HardpointsForces: UniqueIdentifier>(
        &'a mut self,
    ) -> ActorOutput<'a, Actor<DispatchOut, 1, 1>> {
        // AddActorOutput::add_output::<M1HardpointsForces>(&mut self.dispatch_out)
        todo!()
    }
}