prinThor 0.0.3

The highly reliable but not necessarily functional 3D Printer firmware
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
use crate::hwa;
#[cfg(feature = "native")]
use core::fmt::Display;
#[cfg(feature = "native")]
use core::fmt::Formatter;
use core::ops::{Div, Neg};
use core::ops::Mul;
//use embassy_time::{Instant, Duration};
#[cfg(all(feature = "native", feature = "plot-motion-plan"))]
use num_traits::float::FloatCore;
use crate::{math::Real, math::RealInclusiveRange};
#[cfg(all(feature = "native", feature = "plot-motion-plan"))]
use rust_decimal::prelude::ToPrimitive;
use crate::math::{FOUR, HALF, ONE, SIX, THREE, TWO, ZERO};
use crate::control::planner::CodeExecutionFailure;

#[derive(Clone, Default)]
pub struct Boundaries {
    /// Final position
    pub q_1: Real,
    /// Start speed
    pub v_0: Real,
    /// Final speed
    pub v_1: Real,
}

#[cfg(feature = "native")]
impl Display for Boundaries {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f,
               "q_{{1}}={} v_{{0}}={} v_{{1}}={}", self.q_1, self.v_0, self.v_1
        )
    }
}

#[derive(Clone, Default)]
pub struct Constraints {
    /// Max velocity
    pub v_max: Real,
    /// Maximum acceleration
    pub a_max: Real,
    /// Maxium jerk
    pub j_max: Real,
}

#[cfg(feature = "native")]
impl Display for Constraints {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f,
               "v_{{max}}={} a_{{max}}={} j_{{max}}={}", self.v_max, self.a_max, self.j_max
        )
    }
}

#[derive(Clone, Copy, Default)]
pub struct SCurveMotionProfile {
    pub t_j1: Real,
    pub t_a: Real,
    pub t_v: Real,
    pub t_d: Real,
    pub t_j2: Real,

    pub t: Real,

    pub v_0: Real,
    pub v_1: Real,
    pub j_max: Real,
    pub j_min: Real,
    pub a_lim_a: Real,
    pub a_lim_d: Real,
    pub v_lim: Real,
    pub q1: Real,
}

#[allow(unused)]
impl SCurveMotionProfile {
    pub fn compute(q_1: Real, v_0: Real, v_1:Real, constraints: &Constraints) -> Result<SCurveMotionProfile, CodeExecutionFailure>  {

        let _t0 = embassy_time::Instant::now();

        let t_jmax = constraints.a_max / constraints.j_max;
        let t_jstar = Real::min(
            Some((((v_1 - v_0).abs()) / constraints.j_max).sqrt().unwrap()),
            Some(t_jmax),
        ).unwrap();

        hwa::trace!("t_jstar = {} t_jmax = {}", t_jstar, t_jmax);

        let feasible = if t_jstar < t_jmax {
            q_1 > t_jstar * (v_0 + v_1)
        }
        else if t_jstar == t_jmax {
            q_1 > ((v_0 + v_1) / TWO)
                * (t_jstar + ((v_1 - v_0).abs() / constraints.a_max))
        }
        else {
            hwa::error!("unhandled situation");
            return Err(CodeExecutionFailure::ERR)
        };
        if !feasible {
            hwa::error!("Movement NOT FEASIBLE");
            return Err(CodeExecutionFailure::ERR)
        }

        let amax_squared = constraints.a_max * constraints.a_max;

        let (t_j1, t_a) = if (constraints.v_max - v_0) * constraints.j_max < amax_squared {
            let t_j1 = ( ( constraints.v_max - v_0 ) / constraints.j_max ).sqrt().ok_or(CodeExecutionFailure::NumericalError)?;
            (t_j1, t_j1 * TWO)
        } else {
            let t_j1 = constraints.a_max / constraints.j_max;
            (t_j1, t_j1 + (constraints.v_max - v_0) / constraints.a_max)
        };

        let (t_j2, t_d) = if (constraints.v_max - v_1) * constraints.j_max < amax_squared {
            let t_j1 = ( ( constraints.v_max - v_1 ) / constraints.j_max ).sqrt().ok_or(CodeExecutionFailure::NumericalError)?;
            (t_j1, t_j1 * TWO)
        } else {
            let t_j1 = constraints.a_max / constraints.j_max;
            (t_j1, t_j1 + (constraints.v_max - v_1) / constraints.a_max)
        };

        let t_v = (q_1 / constraints.v_max )
            - ((t_a / TWO) * ( ONE + (v_0 / constraints.v_max)))
            - ((t_d / TWO) * ( ONE + (v_1 / constraints.v_max)));

        let (intervals, _j_max) = if t_v >= ZERO {
            let a_lim_a = constraints.j_max * t_j1;
            let a_lim_d = constraints.j_max * t_j2;
            let v_lim = v_0 + (t_a - t_j1) * a_lim_a;
            let t = t_a + t_v + t_d;
            (SCurveMotionProfile {
                t_j1,
                t_a,
                t_v,
                t_d,
                t_j2,
                t,
                v_0: v_0,
                v_1: v_1,
                j_max: constraints.j_max,
                j_min: constraints.j_max.neg(),
                a_lim_a,
                a_lim_d,
                v_lim,
                q1: q_1,
            }, constraints.j_max)
        }
        else {

            let mut gamma = ONE;
            let mut t_a_2 = ZERO;
            let mut t_d_2 = ZERO;
            let mut t_j = ZERO;
            #[allow(unused_assignments)]
                let mut t_j1_2 = ZERO;
            #[allow(unused_assignments)]
                let mut t_j2_2 = ZERO;
            #[allow(unused_variables)]
                let mut v_lim = ZERO;

            let mut a_max_2 = constraints.a_max;

            for _i in 0..10 {
                // TODO: lagrange multipliers
                //println!("Trying with gamma={}", gamma.rdp(4));

                t_j = a_max_2 / constraints.j_max;

                let sqrt_delta = (((a_max_2 * a_max_2 * a_max_2 * a_max_2) / (constraints.j_max * constraints.j_max))
                    + (TWO * ((v_0 * v_0) + (v_1 * v_1))
                    + (a_max_2 * ((FOUR * q_1)
                    - (TWO * (a_max_2 / constraints.j_max) * (v_0 + v_1)))))).sqrt().unwrap();
                let aj = (a_max_2 * a_max_2) / constraints.j_max;
                t_a_2 = (aj - (TWO * v_0) + sqrt_delta) / (TWO * a_max_2);
                t_d_2 = (aj - (TWO * v_1) + sqrt_delta) / (TWO * a_max_2);

                let a_lim_a = constraints.j_max * t_j;
                v_lim = v_0 + (t_a_2 - t_j) * a_lim_a;

                if t_a_2 > (TWO * t_j) && t_d_2 > (TWO * t_j) {
                    break;
                }
                gamma = gamma * HALF;
                a_max_2 = constraints.a_max * gamma;
            }
            t_j1_2 = t_j;
            t_j2_2 = t_j;
            if t_a_2 < Real::zero() {
                t_a_2 = Real::zero();
                t_j1_2 = Real::zero();
                t_d_2 = (q_1.mul(TWO)).div(v_1 + v_0);
                t_j2_2 = (constraints.j_max.mul(q_1) - (constraints.j_max.mul(
                    (constraints.j_max.mul(q_1.powi(2))) + ((v_1 + v_0).powi(2)).mul(v_1 - v_0)
                )
                ).sqrt().unwrap()).div(
                    constraints.j_max.mul(v_1 + v_0)
                )
            }
            else if t_d_2 < Real::zero() {
                t_d_2 = Real::zero();
                t_j2_2 = Real::zero();
                t_a_2 = (q_1.mul(TWO)).div(v_1 + v_0);
                t_j1_2 = (constraints.j_max.mul(q_1) - (constraints.j_max.mul(
                    (constraints.j_max.mul(q_1.powi(2))) + ((v_1 + v_0).powi(2)).mul(v_0 - v_1)
                )
                ).sqrt().unwrap()).div(
                    constraints.j_max.mul(v_1 + v_0)
                )
            }
            let a_lim_a = constraints.j_max * t_j1_2;
            let a_lim_d = constraints.j_max * t_j2_2;
            let t = t_a_2 + t_d_2;
            (SCurveMotionProfile {
                t_j1: t_j1_2,
                t_a: t_a_2,
                t_v: ZERO,
                t_d: t_d_2,
                t_j2: t_j2_2,
                t,
                v_0,
                v_1,
                j_max: constraints.j_max,
                j_min: constraints.j_max.neg(),
                a_lim_a,
                a_lim_d,
                v_lim,
                q1: q_1,
            }, a_max_2)
        };

        let _t = intervals.t_a + intervals.t_v + intervals.t_d;

        #[allow(unused)]
            let a_lim_a = constraints.j_max * intervals.t_j1;
        #[allow(unused)]
            let a_lim_d = constraints.j_max.neg() * intervals.t_j2;
        #[allow(unused)]
            let v_lim = v_0 + ((intervals.t_a - intervals.t_j1) * a_lim_a);

        hwa::debug!("Motion plan computed in {} us", _t0.elapsed().as_micros());
        Ok(intervals)
    }

    pub fn recalculate(&mut self) {

    }

    #[inline]
    fn t1(&self) -> Real {
        self.t_j1
    }
    #[inline]
    fn t2(&self) -> Real {
        self.t_a - self.t_j1
    }

    #[inline]
    fn t3(&self) -> Real {
        self.t_a
    }

    #[inline]
    fn t4(&self) -> Real {
        self.t_a + self.t_v
    }

    #[inline]
    fn t5(&self) -> Real {
        self.t - self.t_d + self.t_j2
    }

    #[inline]
    fn t6(&self) -> Real {
        self.t - self.t_j2
    }

    #[inline]
    fn t7(&self) -> Real {
        self.t
    }

    /// Computes the position (steps) in given timestamp (uS)
    pub fn eval_position(&self, t_i: Real) -> Option<Real> {
        let v0 =  self.v_0;
        let v1 = self.v_1;

        if t_i <= self.t1() {
            Some((v0 * t_i) + (self.j_max * t_i.powi(3) / SIX))
        } else if t_i <= self.t2() {
            Some((v0 * t_i) + ((self.a_lim_a * ((THREE * t_i.powi(2)) - (THREE * self.t_j1 * t_i) + self.t_j1.powi(2))) / SIX))
        } else if t_i <= self.t3() {
            Some((self.v_lim + v0) * self.t_a / TWO - self.v_lim * (self.t_a - t_i) - self.j_min * (self.t_a - t_i).powi(3) / SIX)
        } else if t_i <= self.t4() {
            Some((self.v_lim + v0) * self.t_a / TWO + self.v_lim * (t_i - self.t_a))
        } else if t_i <= self.t5() {
            Some(self.q1 - (self.v_lim + v1) * self.t_d / TWO
                + self.v_lim * (t_i - self.t + self.t_d)
                - self.j_max * (t_i - self.t + self.t_d).powi(3) / SIX)
        } else if t_i <= self.t6() {
            Some(self.q1 - (self.v_lim + v1) * self.t_d / TWO
                + self.v_lim * (t_i - self.t + self.t_d)
                - (self.a_lim_d / SIX) * (
                    (THREE * ((t_i - self.t + self.t_d).powi(2)))
                        - (THREE * self.t_j2 * (t_i - self.t + self.t_d))
                        + self.t_j2.powi(2)
                )
            )
        } else if t_i <= self.t7() {
            Some(self.q1 - v1 * (self.t - t_i) - self.j_max * (self.t - t_i).powi(3) / SIX)
        } else {
            None
        }
    }

    /*
    pub fn iterate(&self, ref_time: Instant, offset: Duration) -> SCurveRealTimeIterator {
        SCurveRealTimeIterator::new(self, ref_time, offset)
    }
     */
}

/*
pub struct SCurveRealTimeIterator<'a> {
    profile: &'a SCurveMotionProfile,
    ref_time: Instant,
    offset: Duration,
    exhausted: bool,
}
impl<'a> SCurveRealTimeIterator<'a> {
    pub const fn new(profile: &'a SCurveMotionProfile, ref_time: Instant, offset: Duration) -> Self {
        Self {
            profile,
            ref_time,
            offset,
            exhausted: false,
        }
    }
}

impl<'a> Iterator for SCurveRealTimeIterator<'a> {
    type Item = Real;

    fn next(&mut self) -> Option<Self::Item> {
        todo!("WIP")
    }
}
*/

#[cfg(feature = "native")]
impl Display for SCurveMotionProfile {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f,
               "\n\tt_j1={}",
               self.t_j1.rdp(4)
        )
    }
}

//#[test]
#[allow(unused)]
pub fn test() -> Result<(),()>{
    /// TODO
    Ok(())
}

pub struct PlanProfile {
    plan: SCurveMotionProfile,
    lin_space: RealInclusiveRange,
    #[allow(unused)]
    advanced: Real,
}


#[allow(unused)]
impl PlanProfile
{
    pub fn new(intervals: SCurveMotionProfile, step_size: Real) -> Self {

        let lin_space = RealInclusiveRange::new(
            ZERO,
            intervals.t,
            step_size);
        Self{
            plan: intervals,
            lin_space,
            advanced: Real::zero(),
        }
    }

    #[allow(unused)]
    #[inline]
    pub fn reset(&mut self) {
        self.advanced = Real::zero();
        self.lin_space.reset();
    }

    #[allow(unused)]
    #[inline]
    pub fn total_duration(&self) -> Real {
        self.lin_space.width()

    }

    #[allow(unused)]
    #[allow(dead_code)]
    #[cfg(feature = "native")]
    pub fn plot(&mut self, plot_pos: bool, plot_vel: bool, plot_accel: bool, plot_jerk: bool) {

        use gnuplot::{AxesCommon, Figure};
        use gnuplot::{AutoOption, Tick, MultiplotFillOrder::RowsFirst, MultiplotFillDirection::{Downwards}};
        use gnuplot::{DashType, PlotOption};

        let p = &(self.plan);

        let t :Vec<Tick<f64, std::string::String>> = vec!(
            Tick::Major(p.t1().to_f64(), AutoOption::Fix(std::string::String::from("t1"))),
            Tick::Major(p.t2().to_f64(), AutoOption::Fix(std::string::String::from("t2"))),
            Tick::Major(p.t3().to_f64(), AutoOption::Fix(std::string::String::from("t3"))),
            Tick::Major(p.t4().to_f64(), AutoOption::Fix(std::string::String::from("t4"))),
            Tick::Major(p.t5().to_f64(), AutoOption::Fix(std::string::String::from("t5"))),
            Tick::Major(p.t6().to_f64(), AutoOption::Fix(std::string::String::from("t6"))),
            Tick::Major(p.t7().to_f64(), AutoOption::Fix(std::string::String::from("t7"))),

        );

        let steps_per_unit = Real::from_lit(5, 0);

        let mut time: Vec<f64> = Vec::with_capacity(1000);
        let mut time_step_by_pulse: Vec<f64> = Vec::with_capacity(1000);
        let mut step_by_pulse: Vec<f64> = Vec::with_capacity(1000);
        let mut pos: Vec<f64> = Vec::with_capacity(1000);

        let mut spd: Vec<f64> = Vec::with_capacity(1000);
        let mut acc: Vec<f64> = Vec::with_capacity(1000);
        let mut jerk: Vec<f64> = Vec::with_capacity(1000);

        let mut time_ref = &mut time;
        let mut pos_ref = &mut pos;
        let mut spd_ref = &mut spd;
        let mut acc_ref = &mut acc;
        let mut jerk_ref = &mut jerk;
        let mut cnt = 0;
        let mut last_t = 0.;
        let mut last_p = 0.;
        let mut last_s = 0.;
        let mut last_a = 0.;
        let mut last_j = 0.;
        let mut real_advanced = Real::zero();
        let mut abs_advanced = 0u32;
        let mut abs_steps_advanced = 0u32;

        time_step_by_pulse.push(last_t);
        step_by_pulse.push(f64::nan());

        let pv0 = self.plan.v_0.rdp(4);
        let pv1 = self.plan.v_1.rdp(4);
        let pvm = self.plan.v_lim.rdp(4);
        let pdisp = self.plan.q1.rdp(4);
        let pres = self.lin_space.step_size().rdp(4);

        for (_v, ti, pos) in self.into_iter() {
            //info!("T[{}] {} P {}", _v, ti, pos);

            real_advanced = pos;

            let ti_v = ti.inner();
            let real_pos = pos.inner();
            let c_ti = ti_v.to_f64().unwrap();
            let c_pos = real_pos.to_f64().unwrap();

            let dt = c_ti.clone() - last_t;

            let pos_abs = real_pos.to_i32().unwrap() as u32;

            let real_steps = (pos * steps_per_unit).ceil();

            let abs_steps = real_steps.to_i64().unwrap() as u32;
            let steps = core::cmp::max(abs_steps, abs_steps_advanced) - abs_steps_advanced;
            abs_steps_advanced = abs_steps;

            if steps > 0 {
                let dt_step = dt / (steps as f64);
                let mut dt_acc = ti_v.to_f64().unwrap();
                for _i in 0..steps {
                    time_step_by_pulse.push(dt_acc);
                    step_by_pulse.push(abs_steps_advanced as f64);
                    abs_steps_advanced += 1;
                    time_step_by_pulse.push(dt_acc);
                    step_by_pulse.push(abs_steps_advanced as f64);
                    dt_acc += dt_step;
                }
            }

            abs_advanced = pos_abs;

            let s = (c_pos - last_p) / dt.clone();
            let a = ((s.clone() - last_s) / dt.clone());
            let j = ((a.clone() - last_a) / dt.clone());

            time_ref.push(c_ti.clone());
            pos_ref.push(c_pos.clone());

            if plot_vel {
                spd_ref.push(s.clone());
            }
            if plot_accel {
                acc_ref.push(a.clone());
            }
            if plot_jerk {
                jerk_ref.push(j.clone());
            }
            cnt += 1;
            last_t = c_ti;
            last_p = c_pos;
            last_s = if s.is_nan() {0.} else {s};
            last_a = if a.is_nan() {0.} else {a};
            last_j = if j.is_nan() {0.} else {j};
        }
        time_step_by_pulse.push(last_t);
        step_by_pulse.push(abs_steps_advanced as f64);
        println!("D; Advanced: {} mm {} steps", real_advanced.rdp(4), abs_steps_advanced);
        let mut fg = Figure::new();

        fg.set_multiplot_layout(5, 1)
            .set_title(
                format!(
                    "FixedPoint Double S-Curve velocity profile\n[vin={} mm/s, vmax={} mm/s, vout={} mm/s, displacement={} mm, sample\\_period={} s]",
                    pv0, pvm, pv1, pdisp, pres,
                ).as_str()
            )
            .set_scale(1.0, 1.0)
            .set_offset(0.0, 0.0)
            .set_multiplot_fill_order(RowsFirst, Downwards);

        fg.axes2d()
            .set_y_label("Position (mm)", &[])
            .lines(time.clone(), pos.clone(), &[PlotOption::Color("blue")])
            .set_x2_ticks_custom(t.clone(), &[], &[]);
        ;
        fg.axes2d()
            .set_y_label("Velocity (mm/s)", &[])
            .lines(time.clone(), spd.clone(), &[PlotOption::Color("green")])
        ;
        fg.axes2d()
            .set_y_label("Acceleration (mm/s²)", &[])
            .lines(time.clone(), acc.clone(), &[PlotOption::Color("red")])
        ;
        fg.axes2d()
            .set_y_label("Jerk (m/s³)", &[])
            .lines(time.clone(), jerk.clone(), &[PlotOption::Color("orange")])
        ;
        fg.axes2d()
            .set_x_label("Time in seconds", &[])
            .set_y_label("Discrete\nsteps", &[])
            .set_x2_ticks_custom(t, &[], &[])
            .set_x2_grid(true)
            .set_x2_minor_grid(true)
            .set_grid_options(true, &[PlotOption::Color("gray"),PlotOption::LineStyle(DashType::Dash)])
            .lines(time_step_by_pulse.clone(), step_by_pulse.clone(), &[PlotOption::Color("black")])
        ;
        fg.show_and_keep_running().unwrap();
    }

}

impl Iterator for PlanProfile
{
    type Item = (u8, Real, Real);

    fn next(&mut self) -> Option<Self::Item> {
        match self.lin_space.next() {
            None => {
                None
            },
            Some(tp) => {
                Some((1, tp, self.plan.eval_position(tp)?))
            }
        }
    }
}


#[test]
pub fn plan_test() -> Result<(),()>{
    /*

    println!("3.9: Constant velocity phase is present");
    // Boundaries
    let q_1 = Real::from_fixed(dec!(10.0));
    let v_0 = Real::from_fixed(dec!(1.0));
    let v_1 = Real::from_fixed(dec!(0.0));

    // Constraints
    let v_max = Real::from_fixed(dec!(5.0));
    let a_max = Real::from_fixed(dec!(10.0));
    let j_max = Real::from_fixed(dec!(30.0));

    let boundaries = Boundaries { q_1, v_0, v_1, };
    let constraints = Constraints { v_max, a_max, j_max, };
    println!("boundaries: {}, constraints: {}", boundaries, constraints);
    let intervals = SCurveMotionProfile::new(&boundaries, &constraints);
    println!("expected  = T_{{j1}}=0.3333 T_{{a}}=0.7333 T_{{v}}=1.1433 T_{{d}}=0.8333 T_{{j2}}=0.3333");
    println!("intervals = {}", intervals);
    println!("--");

    println!("3.10: Constant velocity phase is NOT present");
    // Boundaries
    let q_1 = Real::from_fixed(dec!(10.0));
    let v_0 = Real::from_fixed(dec!(1.0));
    let v_1 = Real::from_fixed(dec!(0.0));

    // Constraints
    let v_max = Real::from_fixed(dec!(10.0));
    let a_max = Real::from_fixed(dec!(10.0));
    let j_max = Real::from_fixed(dec!(30.0));

    let boundaries = Boundaries { q_1, v_0, v_1, };
    let constraints = Constraints { v_max, a_max, j_max, };
    println!("boundaries: {}, constraints: {}", boundaries, constraints);
    let intervals = SCurveMotionProfile::new(&boundaries, &constraints);
    println!("expected  = T_{{j1}}=0.3333 T_{{a}}=1.0747 T_{{v}}=0.0 T_{{d}}=1.1747 T_{{j2}}=0.3333");
    println!("intervals = {}", intervals);
    println!("--");


    println!("3.11: Constant velocity phase is NOT present and maximum acceleration is not reached");
    // Boundaries
    let q_1 = Real::from_fixed(dec!(10.0));
    let v_0 = Real::from_fixed(dec!(7.0));
    let v_1 = Real::from_fixed(dec!(0.0));

    // Constraints
    let v_max = Real::from_fixed(dec!(10.0));
    let a_max = Real::from_fixed(dec!(10.0));
    let j_max = Real::from_fixed(dec!(30.0));

    let boundaries = Boundaries { q_1, v_0, v_1, };
    let constraints = Constraints { v_max, a_max, j_max, };
    println!("boundaries: {}, constraints: {}", boundaries, constraints);
    let intervals = SCurveMotionProfile::new(&boundaries, &constraints);
    println!("expected  = T_{{j1}}=0.2321 T_{{a}}=0.4666 T_{{v}}=0.0 T_{{d}}=1.4718 T_{{j2}}=0.2321");
    println!("intervals = {}", intervals);
    println!("--");

    println!("3.12: Constant velocity phase is NOT present, maximum acceleration is not reached and T_{{a}} becomes negative");

    // Boundaries
    let q_1 = Real::from_fixed(dec!(10.0));
    let v_0 = Real::from_fixed(dec!(7.5));
    let v_1 = Real::from_fixed(dec!(0.0));

    // Constraints
    let v_max = Real::from_fixed(dec!(10.0));
    let a_max = Real::from_fixed(dec!(10.0));
    let j_max = Real::from_fixed(dec!(30.0));

    let boundaries = Boundaries { q_1, v_0, v_1, };
    let constraints = Constraints { v_max, a_max, j_max, };
    println!("boundaries: {}, constraints: {}", boundaries, constraints);
    let intervals = SCurveMotionProfile::new(&boundaries, &constraints);
    // TBD
    println!("expected  = T_{{j1}}=0.0 T_{{a}}=0.0 T_{{v}}=0.0 T_{{d}}=2.6667 T_{{j2}}=0.0973");
    println!("intervals = {}", intervals);
    println!("--");

    let mut profile = PlanProfile::new(intervals,Real::from_fixed(dec!(0.01)));
    profile.plot();

    //////////////

    println!("3.12 bis: Constant velocity phase is NOT present, maximum acceleration is not reached and T_{{a}} becomes negative");

    // Boundaries
    let q_1 = Real::from_fixed(dec!(10.0));
    let v_0 = Real::from_fixed(dec!(0.0));
    let v_1 = Real::from_fixed(dec!(7.5));

    // Constraints
    let v_max = Real::from_fixed(dec!(10.0));
    let a_max = Real::from_fixed(dec!(10.0));
    let j_max = Real::from_fixed(dec!(30.0));

    let boundaries = Boundaries { q_1, v_0, v_1, };
    let constraints = Constraints { v_max, a_max, j_max, };
    println!("boundaries: {}, constraints: {}", boundaries, constraints);
    let intervals = SCurveMotionProfile::new(&boundaries, &constraints);
    // TBD
    println!("expected  = T_{{j1}}=0.0 T_{{a}}=0.0 T_{{v}}=0.0 T_{{d}}=2.6667 T_{{j2}}=0.0973");
    println!("intervals = {}", intervals);
    println!("--");

    let mut profile = PlanProfile::new(intervals,
                                       Real::from_fixed(dec!(0.01)));
    profile.plot();
    */

    Ok(())
}