-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathode_def.h
2148 lines (1839 loc) · 105 KB
/
ode_def.h
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ============================================================================
File : ode_def.h
Author : Sylvie Putot, Ecole Polytechnique (France)
Part of the RINO package for Inner and Outer Reachability Analysis.
The place to declare the systems of ODEs or DDEs on which he/she wants to perform reachability
Setup of dimension of the system (sysdim), initial conditions and parameters is in ode_def.cpp
============================================================================ */
#ifndef ODE_DEF_H
#define ODE_DEF_H
#include "utils.h"
#include "filib_interval.h"
#include "tadiff.h"
#include "fadiff.h"
#include "network_handler.h"
#include "fadbad_aa.h"
#include <math.h>
#include <cstring>
#include <gsl/gsl_math.h>
#include <gsl/gsl_sf_gamma.h>
#include <algorithm>
using namespace std;
#define innerapprox 1
extern double t_end; // ending time of integration
extern double t_begin; // starting time of initialization
extern double tau; // integration time step (fixed step for now)
extern double control_period;
extern int Taylor_order;
extern double delay; // = 1; // delay in DDE
extern int nb_subdiv_delay; // = 10; // number of Taylor models on [0,delay]
extern vector<interval> params; // constant params of the ODE (don't appear in the Jacobian)
extern vector<AAF> params_aff; // constant params of the ODE (don't appear in the Jacobian) - same as params but aff form
extern vector<AAF> nncontrol;
extern vector<vector<AAF>> Jac_params; // (\partial u) / (partial x)
extern vector<vector<AAF>> Jac_params_order2; // (\partial u) / (partial x)
extern vector<interval> initial_values; // initial values
extern vector<AAF> initial_values_aff; // uncertain initial conditions
extern vector<interval> center_initial_values;
extern vector<AAF> center_initial_values_aff;
extern vector<interval> inputs; // uncertain inputs and parameters
extern vector<AAF> inputs_aff; // uncertain inputs and parameters
extern vector<interval> fullinputs; // uncertain inputs and parameters
extern vector<AAF> fullinputs_aff; // uncertain inputs and parameters
extern vector<int> nb_inputs; // piecewise constant input changes value every t_end/nb_inputs[i] seconds
extern vector<interval> center_fullinputs;
extern vector<AAF> center_fullinputs_aff;
extern vector<int> index_param;
extern vector<int> index_param_inv;
extern vector<interval> eps;
extern vector<vector<interval>> Jac_param_inputs; // for inputs defined as g(x1,...xn): we give the jacobian
// to save initial_values and fullinputs when subdivisions
extern vector<interval> initial_values_save;
extern vector<interval> fullinputs_save;
// extern vector<F<AAF>> nn_outputs; // result of NN evaluation
void define_system_dim(); // define the dimensions of your system (ODE or DDE)
// for DDEs : functions that initialize the DDE on first time period
vector<T<AAF>> Initfunc(const T<AAF>& t, vector<AAF> &beta_initial, vector<AAF> &beta_inputs);
vector <T<F<AAF>>> Initfunc(const T<F<AAF>> &t, vector<T<F<AAF>>> &beta_initial, vector<T<F<AAF>>> &beta_inputs);
vector<interval> AnalyticalSol(vector<interval> &initial_values, double d0, double t);
// Obsolete version but kept in case we want to pust subdivisions back
// defining analytical solution if any for comparison
void AnalyticalSol(int current_iteration, double d0);
// d0 and t_begin and nb_subdiv are for DDEs only, rest are common to ODE and DDE
void read_parameters(const char * params_filename);
//vector<F<AAF>> syst_to_nn(vector<F<AAF>> &sysval);
//template <class C> vector<C> syst_to_nn(vector<C> &sysval);
template <class C> vector<C> syst_to_nn(vector<C> &sysval) {
vector<C> res; //= vector<AAF>(NH.n_inputs);
if ((syschoice == 491) || (syschoice == 492)) // Ex ACC de Verisig (avec nn obtenu a partir du yaml)
{
res = vector<C>(NH.n_inputs);
res[0] = 30.0;
res[1] = 1.4;
res[2] = sysval[4]; // v_ego
res[3] = sysval[0]-sysval[3]; // x_lead-x_ego
res[4] = sysval[1]-sysval[4]; // v_lead-v_ego
}
else
res = sysval;
return res;
}
template <class C> vector<C> nn_to_control(vector<C> nnoutput);
template<class ForwardIterator>
inline size_t argmax(ForwardIterator first, ForwardIterator last)
{
return std::distance(first, std::max_element(first, last));
}
// for ODEs and DDEs: define bounds for parameters and inputs, value of delay d0 if any, and parameters of integration (timestep, order of TM)
void init_system();
void init_utils_inputs();
// specific to subdivisions
void init_subdiv(int current_subdiv, vector<interval> initial_values_save, vector<interval> inputs_save, int param_to_subdivide);
// maps the ODE sys coordinates to the neural network inputs
//vector<AAF> sys_to_nn(vector<AAF> &sysval);
// define here your ODE system yp = \dot y = f(y)
class OdeFunc {
public:
template <class C>
void operator()(vector<C> &yp, vector<C> params, vector<C> param_inputs, vector<C> control_inputs, vector<C> y) {
if (syschoice == 12345)
{
// biological model
// dx(1,1) = -0.4*x(1) - x(1)*x(6);
// dx(2,1) = 0.4*x(1) - x(2);
// dx(3,1) = x(2) - 5*x(3)*x(4);
// dx(4,1) = 5*x(5)*x(6) - 5*x(3)*x(4);
// dx(5,1) = -5*x(5)*x(6) + 5*x(3)*x(4);
// dx(6,1) = 0.5*x(7) - 5*x(5)*x(6);
// dx(7,1) = -0.5*x(7) + 5*x(5)*x(6);
yp[0] = -0.4*y[0] - y[0]*y[5];
yp[1] = 0.4*y[0] - y[1];
yp[2] = y[1] - 5*y[2]*y[3];
yp[3] = 5*y[4]*y[5] - 5*y[2]*y[3];
yp[4] = -5*y[4]*y[5] + 5*y[2]*y[3];
yp[5] = 0.5*y[6] - 5*y[4]*y[5];
yp[6] = -0.5*y[6] + 5*y[4]*y[5];
}
else if (syschoice == 1) // running example
yp[0] = (1+params[0]*y[0])*(1+params[0]*y[0]);
else if (syschoice == 2)
// Bruss corresponds to the Brusselator system
{
yp[0] = 1 - (params[1]+1)*y[0] + params[0]*y[0]*y[0]*y[1];
yp[1] = params[1]*y[0] - params[0]*y[0]*y[0]*y[1];
// yp[0] = 1 - (1.5+1)*y[0] + y[0]*y[0]*y[1];
// yp[1] = 1.5*y[0] - y[0]*y[0]*y[1];
}
else if (syschoice == 3)
/******************* ballistic ****************************/
{
double g = 9.81; // gravity in m/s^2
double rho = 1204.4; // air density in g/m3
double a = 0.000126677; // cross section du bullet d=12.7mm, cross section=pi R^2
double d = 0.45; // drag coefficient
double m = 14.3; // mass du bullet in g
yp[0] = - g*sin(y[1])-rho*y[0]*y[0]*a*d/(2.0*m); // velocity v
yp[1] = - g*cos(y[1])/y[0]; // angle gamma with respect to the x axis
yp[2] = y[0]*cos(y[1]); // position x
yp[3] = y[0]*sin(y[1]); // position y
}
else if (syschoice == 4)
/**** ballistic linearise ... *****/
{
double g = 9.81; // gravity in m/s^2
double rho = 1204.4; // air density in g/m3
double a = 0.000126677; // cross section du bullet d=12.7mm, cross section=pi R^2
double d = 0.45; // drag coefficient
double m = 14.3; // mass du bullet in g
// yp[0] = - g*(y[1])-rho*y[0]*y[0]*a*d/(2.0*m); // velocity v
yp[0] = - g*(y[1])-rho*y[0]*y[0]*a*d/(2.0*param_inputs[0]); // velocity v
yp[1] = - g*(1-y[1]*y[1]/2)/y[0]; // angle gamma with respect to the x axis
yp[2] = y[0]*(1-y[1]*y[1]/2); // position x
yp[3] = y[0]*(y[1]); // position y
// yp[4] = 0;
}
/******************* end ballistic ****************************/
else if (syschoice == 5) // self-driving car
{
yp[0] = y[1];
yp[1] = -params[0] *(y[0] - 1.0) - params[1]*y[1]; // pr = 1 is the reference position
}
else if (syschoice == 6) // self-driving car with constant parameters
{
yp[0] = y[1];
yp[1] = -param_inputs[0] *(y[0] - 1.0) - param_inputs[1]*y[1]; // pr = 1 is the reference position
}
else if (syschoice == 7) // self-driving car with time-varying parameters
{ // attention ici j'ai remplace intervalle par params (pour pouvoir notamment appeler avec un double), mais verifier que je traite correctement pour le time-varying et n'exploite pas de relations indues
yp[0] = y[1];
yp[1] = -y[2] *(y[0] - 1.0) - y[3]*y[1]; // pr = 1 is the reference position
yp[2] = params[0]; // interval(-2.,2.); //AAF(interval(-2.,2.)) + y[4]; // parameter Kp
yp[3] = params[1]; // interval(-2.,2.); // 0; // parameter Kd
}
else if (syschoice == 8)
{
yp[0] = - y[0]*y[0]*y[0];
}
else if (syschoice == 9) // acrobatic quadcopter
{
double Cv = 0.25; // transitional drag
double Cphi = 0.02255; // rotational drag
double g = 9.81;
double m = 1.25; // mass
double l = 0.5; // length of quadrotor"s center to an edge
double Iyy = 0.03; // moment of inertia
yp[0] = -y[1]; // px
yp[1] = -(-Cv/m*y[1] - (param_inputs[0]+param_inputs[1])/m*sin(y[4])); // vx
yp[2] = -y[3]; // py
yp[3] = -(-(g+Cv*y[2]/m) + (param_inputs[0]+param_inputs[1])/m*cos(y[4])); // vy
yp[4] = -y[5]; // phi
yp[5] = -(-Cphi/Iyy*y[5] + l/Iyy*(param_inputs[1]-param_inputs[0])); // omega
}
else if (syschoice == 99) // acrobatic quadcopter with m et Iyy as disturbances
{
double Cv = 0.25; // transitional drag
double Cphi = 0.02255; // rotational drag
double g = 9.81;
// double m = 1.25; // mass : param_inputs[2]
double l = 0.5; // length of quadrotor"s center to an edge
// double Iyy = 0.03; // moment of inertia : param_inputs[3]
yp[0] = -y[1]; // px
yp[1] = -(-Cv/param_inputs[2]*y[1] - (param_inputs[0]+param_inputs[1])/param_inputs[2]*sin(y[4])); // vx
yp[2] = -y[3]; // py
yp[3] = -(-(g+Cv*y[2]/param_inputs[2]) + (param_inputs[0]+param_inputs[1])/param_inputs[2]*cos(y[4])); // vy
yp[4] = -y[5]; // phi
yp[5] = -(-Cphi/param_inputs[3]*y[5] + l/param_inputs[3]*(param_inputs[1]-param_inputs[0])); // omega
}
else if (syschoice == 10) // 10-D near-hover quadrotor
{
double n0 = 10;
double d0 = 10;
double d1 = 8;
double kT = 0.91;
double m = 1.3;
double g = 9.81;
// param_inputs[0] .. param_inputs[2] : disturbances dx, dy, dz
// param_inputs[3] .. param_inputs[5] : control inputs Sx, Sy, Tz
yp[0] = -(y[1] + param_inputs[0]); // px' = vx + dx
yp[1] = -(g*tan(y[2])); // vx' = g.tan(thetax)
yp[2] = -(-d1*y[2] +y[3]); // thetax' = -d1.thetax + omegax
yp[3] = -(-d0*y[2] + n0*param_inputs[3]); // omegax' = -d0.thetax + no.Sx
yp[4] = -(y[5] + param_inputs[1]); // py' = vy + dy
yp[5] = -(g*tan(y[6])); // vy' = g.tan(thetay)
yp[6] = -(-d1*y[6] +y[7]); // thetay' = -d1.thetay + omegay
yp[7] = -(-d0*y[6] + n0*param_inputs[4]); // omegay' = -d0.thetay + no.Sy
yp[8] = -(y[9] + param_inputs[2]); // pz' = vz + dz
yp[9] = -(kT*param_inputs[5] - g); // vz' = kT.Tz - g
}
else if (syschoice == 11) // Dubbins vehicle
{
double v = 5; // constant velocity
// param_inputs[0] .. param_inputs[2] : disturbances b1, b2, b3
// param_inputs[3] : control input a : angular control
yp[0] = -(v*cos(y[2]) + param_inputs[0]); // px' = v.cos(theta) + b1
yp[1] = -(v*sin(y[2]) + param_inputs[1]); // py' = v.sin(theta) + b2
yp[2] = -(param_inputs[3] + param_inputs[2]); // theta' = a + b3
}
else if (syschoice == 12) // academic example to investigate time-varying parameters
{
yp[0] = (param_inputs[0] + param_inputs[1]*y[1])*y[0];
yp[1] = 1;
}
else if (syschoice == 13) // Laub-Loomis Benchmark [Arch 2019]
{
yp[0] = 1.4*y[2] - 0.9*y[0];
yp[1] = 2.5*y[4] - 1.5*y[1];
yp[2] = 0.6*y[6] - 0.8*y[1]*y[2];
yp[3] = 2 - 1.3*y[2]*y[3];
yp[4] = 0.7*y[0] - y[3]*y[4];
yp[5] = 0.3*y[0] - 3.1*y[5];
yp[6] = 1.8*y[5]-1.5*y[1]*y[6];
}
else if (syschoice == 14) // Van der Pol oscillator [Arch 2019]
{
double mu = 1.;
yp[0] = y[1];
yp[1] = mu*(1-y[0]*y[0])*y[1]-y[0];
}
else if (syschoice == 15) // Van der Pol oscillator [Arch 2018 and Sparse Polynomial zonotopes]
{
yp[0] = y[1];
yp[1] = (1-y[0]*y[0])*y[1]-y[0];
}
else if(syschoice == 17) // quadrotor model [Arch 2019]
{
double g = 9.81;
double R = 0.1;
double l = 0.5;
double Mmotor = 0.5;
double M = 1;
double m = M + 4*Mmotor;
double Jx = 2./5*M*R*R + 2*l*l*Mmotor;
double Jy = Jx;
double Jz = 2./5*M*R*R + 4*l*l*Mmotor;
double u1 = 1.;
double u2 = 0;
double u3 = 0;
auto F = m*g - 10*(y[2]-u1)+3*y[5]; // height control
auto tau_phi = -(y[6]-u2)-y[9]; // roll control
auto tau_theta = -(y[7]-u3)-y[10]; // pitch control
auto tau_psi = 0; // heading uncontrolled
yp[0] = cos(y[7])*cos(y[8])*y[3] + (sin(y[6])*sin(y[7])*cos(y[8])-cos(y[6]*sin(y[8])))*y[4] + (cos(y[6])*sin(y[7])*cos(y[8])+sin(y[6])*sin(y[8]))*y[5];
yp[1] = cos(y[7])*sin(y[8])*y[3] + (sin(y[6])*sin(y[7])*cos(y[8])+cos(y[6]*sin(y[8])))*y[4] + (cos(y[6])*sin(y[7])*sin(y[8])-sin(y[6])*cos(y[8]))*y[5];
yp[2] = sin(y[7])*y[3] - sin(y[6])*cos(y[7])*y[4] - cos(y[6])*cos(y[7])*y[5];
yp[3] = y[11]*y[4] - y[10]*y[5] - g*sin(y[7]);
yp[4] = y[9]*y[5] - y[11]*y[3] + g*cos(y[7])*sin(y[6]);
yp[5] = y[10]*y[3] - y[9]*y[4] + g*cos(y[7])*cos(y[6]) - F/m;
yp[6] = y[9] + sin(y[6])*tan(y[7])*y[10] + cos(y[6])*tan(y[7])*y[11];
yp[7] = cos(y[6])*y[10] - sin(y[6])*y[11];
yp[8] = sin(y[6])/cos(y[7])*y[10] + cos(y[6])/cos(y[7])*y[11];
yp[9] = (Jy-Jz)/Jx*y[10]*y[11] + tau_phi/Jx;
yp[10] = (Jz-Jx)/Jy*y[9]*y[11] + tau_theta/Jy;
yp[11] = (Jx-Jy)/Jz*y[9]*y[10] + tau_psi/Jz;
}
else if(syschoice == 18) // HSCC 2019 paper crazyflie example
{
static const double p_sp = -0.1; //1.0*M_PI/180.0; // angular speed of 1 degree / sec
static const double q_sp = 0.1;
static const double r_sp = 0.1;
static const double z_sp = 0.1;
static const double C1 = 0.04076521;
static const double C2 = 380.8359;
static const double d = 0.046/sqrt(2.0);
static const double Ixx = 1.657171e-5;
static const double Iyy = 1.6655602e-5;
static const double Izz = 2.9261652e-5;
static const double Ct = 1.285e-8;//interval(1.28e-8 , 1.29e-8);
static const double Cd = 7.645e-11;//interval(7.64e-11 , 7.65e-11);
static const double Kp_Z = 2.0;
static const double Kp_VZ = 25.0;
static const double Ki_VZ = 15.0;
static const double Kp_rr = 250.0;
static const double Kp_pr = 250.0;
static const double Kp_yr = 120.0;
static const double Ki_rr = 500.0;
static const double Ki_pr = 500.0;
static const double Ki_yr = 16.7;
/* Crazyflie trajectory tracking article*/
static const double Ip_qr = (Iyy-Izz)/Ixx;//interval(-1.04880447793, -1.03580464787);
static const double Iq_pr = (Izz-Ixx)/Iyy;//interval(1.03470095927, 1.04749270535);
static const double Ir_pq = (Ixx-Iyy)/Izz;//interval(-0.0162919189567, -0.0120891632629);
static const double Im_xx = 1.0/Ixx;//interval(71484.0524534, 71885.7226787);
static const double Im_yy = 1.0/Iyy;//interval(69441.6509547, 69834.7034512);
static const double Im_zz = 1.0/Izz;//interval(34492.4780616, 34712.0265858);
static const double g = 9.8;
static const double m = 0.028;
auto cosRoll = cos(y[0]);
auto sinRoll = sin(y[0]);
auto cosPitch = cos(y[1]);
auto sinPitch = sin(y[1]);
auto tanPitch = tan(y[1]);
auto err_z = z_sp - y[12];
auto velZ_sp = Kp_Z * err_z;
//Z derivative coordinate
yp[12] = cosPitch*cosRoll*y[11] - sinPitch*y[9] + cosPitch*sinRoll*y[10];
// Z integrale for thrust setpoint calculation
yp[13] = velZ_sp - yp[12];
//auto thrust_Raw = Kp_VZ * (velZ_sp - y[11]) + Ki_VZ * y[13];
auto thrust_Raw = Kp_VZ * (yp[13]) + Ki_VZ * y[13];
auto thrust = 1000.0*thrust_Raw + 36000;
auto err_p = p_sp - y[3];
auto err_q = q_sp - y[4];
auto err_r = r_sp - y[5];
auto cmd_r = y[6]*Ki_rr + err_p*Kp_rr;
auto cmd_p = y[7]*Ki_pr + err_q*Kp_pr;
auto cmd_y = y[8]*Ki_yr + err_r*Kp_yr;
//std:cout << getAAF(cmd_p).convert_int() << std::endl;
auto Mx = ((4*Ct*d*thrust*C1*C1 + 4*C2*Ct*d*C1)*cmd_r + (-4*C1*C1*Ct*d)*cmd_p*cmd_y);
auto My = (-4*C1*C1*Ct*d*cmd_r*cmd_y + (4*Ct*d*thrust*C1*C1 + 4*C2*Ct*d*C1)*cmd_p);
auto Mz = (-2*C1*C1*Cd*cmd_r*cmd_p + (8*Cd*thrust*C1*C1 + 8*C2*Cd*C1)*cmd_y);
auto F = Ct*C1*C1 *(cmd_p*cmd_p + cmd_r*cmd_r + 4.0*cmd_y*cmd_y + 4.0*thrust*thrust) + 8*Ct*C1*C2*thrust + 4*Ct*C2*C2;
// Roll , pitch , yaw derivatives
yp[0] = y[3] + (y[5]*cosRoll + y[4]*sinRoll)*tanPitch;
yp[1] = y[4]*cosRoll - y[5]*sinRoll;
yp[2] = (y[5]*cosRoll + y[4]*sinRoll)/cosPitch;
// p , q and r derivatives
yp[3] = Ip_qr * y[4] * y[5] + Im_xx * Mx ;
yp[4] = Iq_pr * y[3] * y[5] + Im_yy * My ;
yp[5] = Ir_pq * y[3] * y[4] + Im_zz * Mz ;
// integrale of error in p , q and r
yp[6] = err_p;//err_p;
yp[7] = err_q;//err_q;
yp[8] = err_r;//err_r;
// derivatives of body speed u , v and w
yp[9] = y[5]*y[10] - y[4]*y[11] + g*sinPitch;
yp[10] = y[3]*y[11] - y[5]*y[9] - g*cosPitch*sinRoll;
yp[11] = y[4]*y[9] - y[3]*y[10] + F/m - g*cosPitch*cosRoll;
// //Z derivative coordinate
// yp[12] = cosPitch*cosRoll*y[11] - sinPitch*y[9] + cosPitch*sinRoll*y[10];
// // Z integrale for thrust setpoint calculation
// yp[13] = velZ_sp - y[11];
}
else if (syschoice == 181) // HSCC 2019 paper crazyflie example now controlled by neural network
{
static const double p_sp = -0.1; //1.0*M_PI/180.0; // angular speed of 1 degree / sec
static const double q_sp = 0.1;
static const double r_sp = 0.1;
static const double z_sp = 0.1;
static const double C1 = 0.04076521;
static const double C2 = 380.8359;
static const double d = 0.046/sqrt(2.0);
static const double Ixx = 1.657171e-5;
static const double Iyy = 1.6655602e-5;
static const double Izz = 2.9261652e-5;
static const double Ct = 1.285e-8;//interval(1.28e-8 , 1.29e-8);
static const double Cd = 7.645e-11;//interval(7.64e-11 , 7.65e-11);
static const double Kp_Z = 2.0;
static const double Kp_VZ = 25.0;
static const double Ki_VZ = 15.0;
static const double Kp_rr = 250.0;
static const double Kp_pr = 250.0;
static const double Kp_yr = 120.0;
static const double Ki_rr = 500.0;
static const double Ki_pr = 500.0;
static const double Ki_yr = 16.7;
/* Crazyflie trajectory tracking article*/
static const double Ip_qr = (Iyy-Izz)/Ixx;//interval(-1.04880447793, -1.03580464787);
static const double Iq_pr = (Izz-Ixx)/Iyy;//interval(1.03470095927, 1.04749270535);
static const double Ir_pq = (Ixx-Iyy)/Izz;//interval(-0.0162919189567, -0.0120891632629);
static const double Im_xx = 1.0/Ixx;//interval(71484.0524534, 71885.7226787);
static const double Im_yy = 1.0/Iyy;//interval(69441.6509547, 69834.7034512);
static const double Im_zz = 1.0/Izz;//interval(34492.4780616, 34712.0265858);
static const double g = 9.8;
static const double m = 0.028;
auto cosRoll = cos(y[0]);
auto sinRoll = sin(y[0]);
auto cosPitch = cos(y[1]);
auto sinPitch = sin(y[1]);
auto cosYaw = cos(y[2]);
auto sinYaw = sin(y[2]);
auto tanPitch = tan(y[1]);
auto err_z = z_sp - y[12];
auto velZ_sp = Kp_Z * err_z;
//Z derivative coordinate
yp[12] = cosPitch*cosRoll*y[11] - sinPitch*y[9] + cosPitch*sinRoll*y[10];
// Z integrale for thrust setpoint calculation
yp[13] = velZ_sp - yp[12];
//auto thrust_Raw = Kp_VZ * (velZ_sp - y[11]) + Ki_VZ * y[13];
auto thrust_Raw = Kp_VZ * (yp[13]) + Ki_VZ * y[13];
auto thrust = 1000.0*thrust_Raw + 36000;
auto err_p = p_sp - y[3];
auto err_q = q_sp - y[4];
auto err_r = r_sp - y[5];
auto sqp0 = param_inputs[0]*param_inputs[0]; // exp(2.0*param_inputs[0]);
auto sqp1 = param_inputs[1]*param_inputs[1]; // exp(2.0*param_inputs[1]);
auto sqp2 = param_inputs[2]*param_inputs[2]; // exp(2.0*param_inputs[2]);
auto cmd_r = 400*param_inputs[0]*(10+sqp0)*(60+sqp0)/(600+sqp0*(270+sqp0*(11+sqp0/24))); // 800.*(expp0-1.0)/(expp0+1.0); // cmd_phi = 800*tanh(param_inputs[0]) // y[6]*Ki_rr + err_p*Kp_rr;
auto cmd_p = 400*param_inputs[1]*(10+sqp1)*(60+sqp1)/(600+sqp1*(270+sqp1*(11+sqp1/24))); // 800.0*(expp1-1.0)/(expp1+1.0); // cmd_theta // y[7]*Ki_pr + err_q*Kp_pr;
auto cmd_y = 1000*param_inputs[2]*(10+sqp2)*(60+sqp2)/(600+sqp2*(270+sqp2*(11+sqp2/24))); // 3000.0*(expp2-1.0)/(expp2+1.0); // cmd_psi // y[8]*Ki_yr + err_r*Kp_yr;
//std:cout << getAAF(cmd_p).convert_int() << std::endl;
auto Mx = ((4*Ct*d*thrust*C1*C1 + 4*C2*Ct*d*C1)*cmd_r + (-4*C1*C1*Ct*d)*cmd_p*cmd_y);
auto My = (-4*C1*C1*Ct*d*cmd_r*cmd_y + (4*Ct*d*thrust*C1*C1 + 4*C2*Ct*d*C1)*cmd_p);
auto Mz = (-2*C1*C1*Cd*cmd_r*cmd_p + (8*Cd*thrust*C1*C1 + 8*C2*Cd*C1)*cmd_y);
auto F = Ct*C1*C1 *(cmd_p*cmd_p + cmd_r*cmd_r + 4.0*cmd_y*cmd_y + 4.0*thrust*thrust) + 8*Ct*C1*C2*thrust + 4*Ct*C2*C2;
// Roll , pitch , yaw derivatives
yp[0] = y[3] + (y[5]*cosRoll + y[4]*sinRoll)*tanPitch;
yp[1] = y[4]*cosRoll - y[5]*sinRoll;
yp[2] = (y[5]*cosRoll + y[4]*sinRoll)/cosPitch;
// p , q and r derivatives
yp[3] = Ip_qr * y[4] * y[5] + Im_xx * Mx ;
yp[4] = Iq_pr * y[3] * y[5] + Im_yy * My ;
yp[5] = Ir_pq * y[3] * y[4] + Im_zz * Mz ;
// integrale of error in p , q and r
yp[6] = err_p;//err_p;
yp[7] = err_q;//err_q;
yp[8] = err_r;//err_r;
// derivatives of body speed u , v and w
yp[9] = y[5]*y[10] - y[4]*y[11] + g*sinPitch;
yp[10] = y[3]*y[11] - y[5]*y[9] - g*cosPitch*sinRoll;
yp[11] = y[4]*y[9] - y[3]*y[10] + F/m - g*cosPitch*cosRoll;
// //Z derivative coordinate
// yp[12] = cosPitch*cosRoll*y[11] - sinPitch*y[9] + cosPitch*sinRoll*y[10];
// // Z integrale for thrust setpoint calculation
// yp[13] = velZ_sp - y[11];
}
else if (syschoice == 182) // HSCC 2019 paper crazyflie example but with a different altitude controller - and now controlled by neural network
{
static const double p_sp = -0.1; // angular speed of 1 degree / sec
static const double q_sp = 0.1;
static const double r_sp = 0.1; // prefer yaw command
static const double z_sp = 0.1; // just stabilize at 0
static const double C1 = 0.04076521;
static const double C2 = 380.8359;
static const double d = 0.046/sqrt(2.0);
static const double Ixx = 1.657171e-5;
static const double Iyy = 1.6655602e-5;
static const double Izz = 2.9261652e-5;
static const double Ct = 1.285e-8;//interval(1.28e-8 , 1.29e-8);
static const double Cd = 7.645e-11;//interval(7.64e-11 , 7.65e-11);
//static const double Kp_Z = 2.0;
//static const double Kp_VZ = 25.0;
//static const double Ki_VZ = 15.0;
static const double Kp_Z = 3000.0;
static const double Kp_VZ = 500.0;
static const double Ki_VZ = 300.0;
static const double Kp_rr = 250.0;
static const double Kp_pr = 250.0;
static const double Kp_yr = 120.0;
static const double Ki_rr = 500.0;
static const double Ki_pr = 500.0;
static const double Ki_yr = 16.7;
/* Crazyflie trajectory tracking article*/
static const double Ip_qr = (Iyy-Izz)/Ixx;//interval(-1.04880447793, -1.03580464787);
static const double Iq_pr = (Izz-Ixx)/Iyy;//interval(1.03470095927, 1.04749270535);
static const double Ir_pq = (Ixx-Iyy)/Izz;//interval(-0.0162919189567, -0.0120891632629);
static const double Im_xx = 1.0/Ixx;//interval(71484.0524534, 71885.7226787);
static const double Im_yy = 1.0/Iyy;//interval(69441.6509547, 69834.7034512);
static const double Im_zz = 1.0/Izz;//interval(34492.4780616, 34712.0265858);
static const double g = 9.8;
static const double m = 0.028;
auto cosRoll = cos(y[0]);
auto sinRoll = sin(y[0]);
auto cosPitch = cos(y[1]);
auto sinPitch = sin(y[1]);
auto cosYaw = cos(y[2]);
auto sinYaw = sin(y[2]);
auto tanPitch = tan(y[1]);
auto err_z = z_sp - y[12];
//Z derivative coordinate
yp[12] = cosPitch*cosRoll*y[11] - sinPitch*y[9] + cosPitch*sinRoll*y[10];
// Z integrale for thrust setpoint calculation
yp[13] = err_z;
//auto thrust_Raw = Kp_VZ * (velZ_sp - y[11]) + Ki_VZ * y[13];
//auto thrust_Raw = Kp_VZ * (yp[13]) + Ki_VZ * y[13];
auto thrust = Kp_Z * err_z + Kp_VZ * (-yp[12]) + Ki_VZ * y[13] + 48500;
auto err_p = p_sp - y[3];
auto err_q = q_sp - y[4];
auto err_r = r_sp - y[5];
auto sqp0 = param_inputs[0]*param_inputs[0]; // exp(2.0*param_inputs[0]);
auto sqp1 = param_inputs[1]*param_inputs[1]; // exp(2.0*param_inputs[1]);
auto sqp2 = param_inputs[2]*param_inputs[2]; // exp(2.0*param_inputs[2]);
auto cmd_r = 400*param_inputs[0]*(10+sqp0)*(60+sqp0)/(600+sqp0*(270+sqp0*(11+sqp0/24))); // 800.*(expp0-1.0)/(expp0+1.0); // cmd_phi = 800*tanh(param_inputs[0]) // y[6]*Ki_rr + err_p*Kp_rr;
auto cmd_p = 400*param_inputs[1]*(10+sqp1)*(60+sqp1)/(600+sqp1*(270+sqp1*(11+sqp1/24))); // 800.0*(expp1-1.0)/(expp1+1.0); // cmd_theta // y[7]*Ki_pr + err_q*Kp_pr;
auto cmd_y = 1000*param_inputs[2]*(10+sqp2)*(60+sqp2)/(600+sqp2*(270+sqp2*(11+sqp2/24))); // 3000.0*(expp2-1.0)/(expp2+1.0); // cmd_psi // y[8]*Ki_yr + err_r*Kp_yr;
//std:cout << getAAF(cmd_p).convert_int() << std::endl;
auto Mx = ((4*Ct*d*thrust*C1*C1 + 4*C2*Ct*d*C1)*cmd_r + (-4*C1*C1*Ct*d)*cmd_p*cmd_y);
auto My = (-4*C1*C1*Ct*d*cmd_r*cmd_y + (4*Ct*d*thrust*C1*C1 + 4*C2*Ct*d*C1)*cmd_p);
auto Mz = (-2*C1*C1*Cd*cmd_r*cmd_p + (8*Cd*thrust*C1*C1 + 8*C2*Cd*C1)*cmd_y);
auto F = Ct*C1*C1 *(cmd_p*cmd_p + cmd_r*cmd_r + 4.0*cmd_y*cmd_y + 4.0*thrust*thrust) + 8*Ct*C1*C2*thrust + 4*Ct*C2*C2;
// Roll , pitch , yaw derivatives
yp[0] = y[3] + (y[5]*cosRoll + y[4]*sinRoll)*tanPitch;
yp[1] = y[4]*cosRoll - y[5]*sinRoll;
yp[2] = (y[5]*cosRoll + y[4]*sinRoll)/cosPitch;
// p , q and r derivatives
yp[3] = Ip_qr * y[4] * y[5] + Im_xx * Mx ;
yp[4] = Iq_pr * y[3] * y[5] + Im_yy * My ;
yp[5] = Ir_pq * y[3] * y[4] + Im_zz * Mz ;
// integrale of error in p , q and r
yp[6] = err_p;//err_p;
yp[7] = err_q;//err_q;
yp[8] = err_r;//err_r;
// derivatives of body speed u , v and w
yp[9] = y[5]*y[10] - y[4]*y[11] + g*sinPitch;
yp[10] = y[3]*y[11] - y[5]*y[9] - g*cosPitch*sinRoll;
yp[11] = y[4]*y[9] - y[3]*y[10] + F/m - g*cosPitch*cosRoll;
// //Z derivative coordinate
// yp[12] = cosPitch*cosRoll*y[11] - sinPitch*y[9] + cosPitch*sinRoll*y[10];
// // Z integrale for thrust setpoint calculation
// yp[13] = velZ_sp - y[11];
}
else if (syschoice == 183) // HSCC 2019 paper crazyflie example but with a different altitude controller plus aero effects - and now controlled by neural network
{
static const double p_sp = -0.1; // angular speed of 1 degree / sec
static const double q_sp = 0.1;
static const double r_sp = 0.1; // prefer yaw command
static const double z_sp = 0.1; // just stabilize at 0
static const double C1 = 0.04076521;
static const double C2 = 380.8359;
static const double d = 0.046/sqrt(2.0);
static const double h = 0.005;
/* Cross-model for aerodynamical systems from Forster */
/*
static const double K11 = -10.2506e-7; // e-8 and below?
static const double K12 = -0.3177e-7;
static const double K13 = -0.4332e-7;
static const double K21 = -0.3177e-7;
static const double K22 = -10.2506e-7;
static const double K23 = -0.4332e-7;
static const double K31 = -7.7050e-7;
static const double K32 = -7.7050e-7;
static const double K33 = -7.5530e-7;
*/
/* Simplified model */
static const double K11 = -9.1785e-7;
static const double K12 = 0;
static const double K13 = 0;
static const double K21 = 0;
static const double K22 = -9.1785e-7;
static const double K23 = 0;
static const double K31 = 0;
static const double K32 = 0;
static const double K33 = -10.311e-7;
static const double Ixx = 1.657171e-5;
static const double Iyy = 1.6655602e-5;
static const double Izz = 2.9261652e-5;
static const double Ct = 1.285e-8;//interval(1.28e-8 , 1.29e-8);
static const double Cd = 7.645e-11;//interval(7.64e-11 , 7.65e-11);
//static const double Kp_Z = 2.0;
//static const double Kp_VZ = 25.0;
//static const double Ki_VZ = 15.0;
static const double Kp_Z = 3000.0;
static const double Kp_VZ = 500.0;
static const double Ki_VZ = 300.0;
static const double Kp_rr = 250.0;
static const double Kp_pr = 250.0;
static const double Kp_yr = 120.0;
static const double Ki_rr = 500.0;
static const double Ki_pr = 500.0;
static const double Ki_yr = 16.7;
/* Crazyflie trajectory tracking article*/
static const double Ip_qr = (Iyy-Izz)/Ixx;//interval(-1.04880447793, -1.03580464787);
static const double Iq_pr = (Izz-Ixx)/Iyy;//interval(1.03470095927, 1.04749270535);
static const double Ir_pq = (Ixx-Iyy)/Izz;//interval(-0.0162919189567, -0.0120891632629);
static const double Im_xx = 1.0/Ixx;//interval(71484.0524534, 71885.7226787);
static const double Im_yy = 1.0/Iyy;//interval(69441.6509547, 69834.7034512);
static const double Im_zz = 1.0/Izz;//interval(34492.4780616, 34712.0265858);
static const double g = 9.8;
static const double m = 0.028;
auto cosRoll = cos(y[0]);
auto sinRoll = sin(y[0]);
auto cosPitch = cos(y[1]);
auto sinPitch = sin(y[1]);
auto cosYaw = cos(y[2]);
auto sinYaw = sin(y[2]);
auto tanPitch = tan(y[1]);
auto err_z = z_sp - y[12];
//Z derivative coordinate
yp[12] = cosPitch*cosRoll*y[11] - sinPitch*y[9] + cosPitch*sinRoll*y[10];
// Z integrale for thrust setpoint calculation
yp[13] = err_z;
//auto thrust_Raw = Kp_VZ * (velZ_sp - y[11]) + Ki_VZ * y[13];
//auto thrust_Raw = Kp_VZ * (yp[13]) + Ki_VZ * y[13];
auto thrust = Kp_Z * err_z + Kp_VZ * (-yp[12]) + Ki_VZ * y[13] + 48500;
auto err_p = p_sp - y[3];
auto err_q = q_sp - y[4];
auto err_r = r_sp - y[5];
auto sqp0 = param_inputs[0]*param_inputs[0]; // exp(2.0*param_inputs[0]);
auto sqp1 = param_inputs[1]*param_inputs[1]; // exp(2.0*param_inputs[1]);
auto sqp2 = param_inputs[2]*param_inputs[2]; // exp(2.0*param_inputs[2]);
auto cmd_r = 400*param_inputs[0]*(10+sqp0)*(60+sqp0)/(600+sqp0*(270+sqp0*(11+sqp0/24))); // 800.*(expp0-1.0)/(expp0+1.0); // cmd_phi = 800*tanh(param_inputs[0]) // y[6]*Ki_rr + err_p*Kp_rr;
auto cmd_p = 400*param_inputs[1]*(10+sqp1)*(60+sqp1)/(600+sqp1*(270+sqp1*(11+sqp1/24))); // 800.0*(expp1-1.0)/(expp1+1.0); // cmd_theta // y[7]*Ki_pr + err_q*Kp_pr;
auto cmd_y = 1000*param_inputs[2]*(10+sqp2)*(60+sqp2)/(600+sqp2*(270+sqp2*(11+sqp2/24))); // 3000.0*(expp2-1.0)/(expp2+1.0); // cmd_psi // y[8]*Ki_yr + err_r*Kp_yr;
//std:cout << getAAF(cmd_p).convert_int() << std::endl;
// computation of the PWMs
auto PWM1 = thrust-cmd_r/2-cmd_p/2-cmd_y;
auto PWM2 = thrust-cmd_r/2+cmd_p/2+cmd_y;
auto PWM3 = thrust+cmd_r/2+cmd_p/2-cmd_y;
auto PWM4 = thrust+cmd_r/2-cmd_p/2+cmd_y;
// computation of the angular velocities of the four rotors
auto Om1 = C1*PWM1+C2;
auto Om2 = C1*PWM2+C2;
auto Om3 = C1*PWM3+C2;
auto Om4 = C1*PWM4+C2;
// R transpose, from inertial to body frame phi=Roll, theta=Pitch, psi=Yaw
// first line
auto RT11 = cosYaw*cosPitch;
auto RT12 = cosPitch*sinYaw;
auto RT13 = -sinPitch;
// second line
auto RT21 = cosYaw*sinPitch*sinRoll-cosRoll*sinYaw;
auto RT22 = cosYaw*cosRoll+sinYaw*sinPitch*sinRoll;
auto RT23 = cosPitch*sinRoll;
// third line
auto RT31 = sinYaw*sinRoll+cosYaw*cosRoll*sinPitch;
auto RT32 = cosRoll*sinYaw*sinPitch-cosYaw*sinPitch;
auto RT33 = cosPitch*cosRoll;
// linear velocities of the four rotors
auto u1 = y[9]+y[4]*h+y[5]*d*sqrt(2)/2;
auto u2 = y[9]+y[4]*h+y[5]*d*sqrt(2)/2;
auto u3 = y[9]+y[4]*h-y[5]*d*sqrt(2)/2;
auto u4 = y[9]+y[4]*h-y[5]*d*sqrt(2)/2;
auto v1 = y[10]-y[3]*h+y[5]*d*sqrt(2)/2;
auto v2 = y[10]-y[3]*h-y[5]*d*sqrt(2)/2;
auto v3 = y[10]-y[3]*h-y[5]*d*sqrt(2)/2;
auto v4 = y[10]-y[3]*h+y[5]*d*sqrt(2)/2;
auto w1 = y[11]-y[3]*d*sqrt(2)/2-y[4]*d*sqrt(2)/2;
auto w2 = y[11]-y[3]*d*sqrt(2)/2+y[4]*d*sqrt(2)/2;
auto w3 = y[11]+y[3]*d*sqrt(2)/2+y[4]*d*sqrt(2)/2;
auto w4 = y[11]+y[3]*d*sqrt(2)/2-y[4]*d*sqrt(2)/2;
// computation of the four aerodynamical forces in the body frame
// F_i^a=Om_i*K*((u_i,v_i,w_i)-RT*Wa
// calcul de RT*Wa d'abord
// for now Wa = 0
static const double Wa1 = 0;
static const double Wa2 = 0;
static const double Wa3 = 0;
auto RTWax = RT11*Wa1+RT12*Wa2+RT13*Wa3;
auto RTWay = RT21*Wa1+RT22*Wa2+RT23*Wa3;
auto RTWaz = RT31*Wa1+RT32*Wa2+RT33*Wa3;
auto F1x = Om1*(K11*(u1-RTWax)+K12*(v1-RTWay)+K13*(w1-RTWaz));
auto F1y = Om1*(K21*(u1-RTWax)+K22*(v1-RTWay)+K23*(w1-RTWaz));
auto F1z = Om1*(K31*(u1-RTWax)+K32*(v1-RTWay)+K33*(w1-RTWaz));
auto F2x = Om2*(K11*(u2-RTWax)+K12*(v2-RTWay)+K13*(w2-RTWaz));
auto F2y = Om2*(K21*(u2-RTWax)+K22*(v2-RTWay)+K23*(w2-RTWaz));
auto F2z = Om2*(K31*(u2-RTWax)+K32*(v2-RTWay)+K33*(w2-RTWaz));
auto F3x = Om3*(K11*(u3-RTWax)+K12*(v3-RTWay)+K13*(w3-RTWaz));
auto F3y = Om3*(K21*(u3-RTWax)+K22*(v3-RTWay)+K23*(w3-RTWaz));
auto F3z = Om3*(K31*(u3-RTWax)+K32*(v3-RTWay)+K33*(w3-RTWaz));
auto F4x = Om4*(K11*(u4-RTWax)+K12*(v4-RTWay)+K13*(w4-RTWaz));
auto F4y = Om4*(K21*(u4-RTWax)+K22*(v4-RTWay)+K23*(w4-RTWaz));
auto F4z = Om4*(K31*(u4-RTWax)+K32*(v4-RTWay)+K33*(w4-RTWaz));
// computation of the three aerodynamical moments in the body frame
auto Max = -d*sqrt(2)/2*F1z-h*F1y-d*sqrt(2)/2*F2z-h*F2y+d*sqrt(2)/2*F3z-h*F3y+d*sqrt(2)/2*F4z-h*F4y;
auto May = h*F1x-d*sqrt(2)/2*F1z+h*F2x+d*sqrt(2)/2*F2z+h*F3x+d*sqrt(2)/2*F3z+h*F4x-d*sqrt(2)/2*F4z;
auto Maz = d*sqrt(2)/2*F1y+d*sqrt(2)/2*F1x-d*sqrt(2)/2*F2y+d*sqrt(2)/2*F2x-d*sqrt(2)/2*F3y-d*sqrt(2)/2*F3x+d*sqrt(2)/2*F4y-d*sqrt(2)/2*F4x;
auto Mx = ((4*Ct*d*thrust*C1*C1 + 4*C2*Ct*d*C1)*cmd_r + (-4*C1*C1*Ct*d)*cmd_p*cmd_y);
auto My = (-4*C1*C1*Ct*d*cmd_r*cmd_y + (4*Ct*d*thrust*C1*C1 + 4*C2*Ct*d*C1)*cmd_p);
auto Mz = (-2*C1*C1*Cd*cmd_r*cmd_p + (8*Cd*thrust*C1*C1 + 8*C2*Cd*C1)*cmd_y);
auto F = Ct*C1*C1 *(cmd_p*cmd_p + cmd_r*cmd_r + 4.0*cmd_y*cmd_y + 4.0*thrust*thrust) + 8*Ct*C1*C2*thrust + 4*Ct*C2*C2;
Mx = Mx + Max;
My = My + May;
Mz = Mz + Maz;
F = F + F1z + F2z + F3z + F4z;
// Roll , pitch , yaw derivatives
yp[0] = y[3] + (y[5]*cosRoll + y[4]*sinRoll)*tanPitch;
yp[1] = y[4]*cosRoll - y[5]*sinRoll;
yp[2] = (y[5]*cosRoll + y[4]*sinRoll)/cosPitch;
// p , q and r derivatives
yp[3] = Ip_qr * y[4] * y[5] + Im_xx * Mx ;
yp[4] = Iq_pr * y[3] * y[5] + Im_yy * My ;
yp[5] = Ir_pq * y[3] * y[4] + Im_zz * Mz ;
// integrale of error in p , q and r
yp[6] = err_p;//err_p;
yp[7] = err_q;//err_q;
yp[8] = err_r;//err_r;
// derivatives of body speed u , v and w
yp[9] = y[5]*y[10] - y[4]*y[11] + g*sinPitch+(F1x+F2x+F3x+F4x)/m;
yp[10] = y[3]*y[11] - y[5]*y[9] - g*cosPitch*sinRoll+(F1y+F2y+F3y+F4y)/m;
yp[11] = y[4]*y[9] - y[3]*y[10] + F/m - g*cosPitch*cosRoll;
// //Z derivative coordinate
// yp[12] = cosPitch*cosRoll*y[11] - sinPitch*y[9] + cosPitch*sinRoll*y[10];
// // Z integrale for thrust setpoint calculation
// yp[13] = velZ_sp - y[11];
}
else if (syschoice == 19) { // academic example, time-varying (piecewise constant) parameters
yp[0] = 2 + 2*param_inputs[0]*(1-y[1]) + y[1];
yp[1] = 1; // time
}
else if (syschoice == 21) { // academic example, time-varying (piecewise constant) parameters
yp[0] = 2 + 2*param_inputs[0]*(1-y[1]) + y[1];
yp[1] = 1; // time
}
else if (syschoice == 22) { // academic example, time-varying (piecewise constant) parameters
yp[0] = 2 + 2*(param_inputs[0]+param_inputs[0]*param_inputs[0])*(1-y[1]) + y[1];
yp[1] = 1; // time
}
else if (syschoice == 23) { // pursuer-evader example Mitchell
double v = 5.0; // linear velocity
// param_inputs[0] : angular velocity a of the evader (control)
// param_inputs[1] : angular velocity b of the pursuer (disturbance)
yp[0] = -v + v*cos(y[2]) + param_inputs[0]*y[1];
yp[1] = v*sin(y[2]) - param_inputs[0]*y[1];
yp[2] = param_inputs[1] - param_inputs[0];
}
else if (syschoice == 24) { // [Franzle et al.]
yp[0] = -(-0.5*y[0] - (0.5+param_inputs[0])*y[1] + 0.5);
yp[1] = - (-0.5*y[1] + 1.0);
}
else if (syschoice == 25) { // [Franzle et al.] reversed time van der pol oscillator with uncertainty
yp[0] = y[1];
yp[1] = - (0.4*y[0] + 5.0*(y[0]*y[0] - (param_inputs[0] + 0.2))*y[1]);
}
else if (syschoice == 26) { // [Franzle et al.] 7-d biological system
yp[0] = -(-0.4*y[0] + 5.0*y[2]*y[3] + param_inputs[0]);
yp[1] = -(0.4*y[0] - y[1]);
yp[2] = -(y[1] - 5.0*y[2]*y[3]);
yp[3] = -(5*y[4]*y[5] - 5.0*y[2]*y[3]);
yp[4] = -(-5*y[4]*y[5] + 5.0*y[2]*y[3]);
yp[5] = -(0.5*y[6] - 5.0*y[4]*y[5]);
yp[6] = -(-0.5*y[6] + 5.0*y[4]*y[5]);
}
else if (syschoice == 27) { // [Franzle et al.] 7-d biological system but with sharing
C y2y3 = y[2]*y[3];
C y4y5 = y[4]*y[5];
yp[0] = -(-0.4*y[0] + 5.0*y2y3 + param_inputs[0]);
yp[1] = -(0.4*y[0] - y[1]);
yp[2] = -(y[1] - 5.0*y2y3);
yp[3] = -(5*y4y5 - 5.0*y2y3);
yp[4] = -(-5*y4y5 + 5.0*y2y3);
yp[5] = -(0.5*y[6] - 5.0*y4y5);
yp[6] = -(-0.5*y[6] + 5.0*y4y5);
}
else if (syschoice == 28) { // [Franzle et al.] 7-d biological system without disturbance
C y2y3 = y[2]*y[3];
C y4y5 = y[4]*y[5];
yp[0] = -(-0.4*y[0] + 5.0*y2y3);
yp[1] = -(0.4*y[0] - y[1]);
yp[2] = -(y[1] - 5.0*y2y3);
yp[3] = -(5*y4y5 - 5.0*y2y3);
yp[4] = -(-5*y4y5 + 5.0*y2y3);
yp[5] = -(0.5*y[6] - 5.0*y4y5);
yp[6] = -(-0.5*y[6] + 5.0*y4y5);
}
else if (syschoice == 29) { // EX_10 Reachability for Neural Feedback Systems using Regressive Polynomial Rule Inference
yp[0] = y[3]*cos(y[2]);
yp[1] = y[3]*sin(y[2]);
yp[2] = param_inputs[0];
yp[3] = param_inputs[1]; //+ interval(-0.0001,0.0001);
}
else if (syschoice == 30) { // EX_1 Reachability for Neural Feedback Systems using Regressive Polynomial Rule Inference
yp[0] = y[1]-y[0]*y[0]*y[0];
yp[1] = param_inputs[0];
}
else if (syschoice == 301) { // EX_1 Reachability for Neural Feedback Systems using Regressive Polynomial Rule Inference
/* vector<vector<C>> net_outputs(NH.n_hidden_layers+2);
net_outputs[0] = y;
for (int i=0 ; i<NH.n_hidden_layers+1 ; i++ )
net_outputs[i+1] = eval_layer(L[i],net_outputs[i]);
vector<C> u = net_outputs[NH.n_hidden_layers+1]; // 0.0; // NN control */
vector<C> u = NH.eval_network(y);
yp[0] = y[1]-y[0]*y[0]*y[0];
yp[1] = u[0];
}
else if (syschoice == 31) { // Quadcopter Mikhail Bessa avec 3 composantes de plus
// y[0]=z ; y[1]=u ; y[2]=v ; y[3]=w ; y[4]=phi ; y[5]=theta ;
// y[6]=psi ; y[7]=p ; y[8]=q ; y[9]=r ; y[10]=zI ;
// param_inputs[0]=cmd_phi ; param_inputs[1]=cmd_theta ;
// param_inputs[2]=cmd_psi ;
yp[0] = (-sin(y[5]))*y[1] + cos(y[5])*sin(y[4])*y[2] + cos(y[5])*cos(y[4])*y[3]; // yp[0] = z
yp[1] = y[9]*y[2] - y[8]*y[3] + sin(y[5])*9.81; // yp[1] = u
yp[2] = -y[9]*y[1] + y[7]*y[3] - cos(y[5])*sin(y[4])*9.81; // yp[2] = v
yp[3] = y[8]*y[1] - y[7]*y[2] - 9.81*cos(y[5])*cos(y[4])+(7.62648576804e-10)*param_inputs[1]*param_inputs[1] + (7.62648576804e-10)*param_inputs[0]*param_inputs[0] + (3.05059430721e-9)*param_inputs[2]*param_inputs[2] + 7.62648576804*y[0]*y[0] + 7.62648576804*y[3]*y[0] - 4.57589146082*y[0]*y[10] + 1.90662144201*y[3]*y[3] - 2.28794573041*y[3]*y[10] + 0.686383719125*y[10]*y[10] - 29.0850309334*y[0] - 14.5425154667*y[3] + 8.72550928*y[10] + 27.7303023346; // yp[3] = w
yp[4] = y[7] + sin(y[5])/cos(y[5])*(cos(y[4])*y[9] + sin(y[4])*y[8]); // yp[4] = phi
yp[5] = cos(y[4])*y[8] - sin(y[4])*y[9]; // yp[5] = theta
yp[6] = cos(y[4]) / cos(y[5]) * y[9] + sin(y[4]) / cos(y[5]) * y[8]; // yp[6] = psi
yp[7] = -0.760697* y[8] * y[9] + (-8.38277868313e-3)*y[0]*param_inputs[0] - (4.19138934156e-3)*y[3]*param_inputs[0] + (2.51483360494e-3)*y[10]*param_inputs[0] + 0.0159846477606*param_inputs[0] - (1.67655573663e-7)*param_inputs[1]*param_inputs[2]; // yp[7] = p
yp[8] = 0.761902* y[7] * y[9] + (-8.34055576802e-3)*y[0]*param_inputs[1] - (4.17027788401e-3)*y[3]*param_inputs[1] + (2.50216673041e-3)*y[10]*param_inputs[1] + 0.0159041352657*param_inputs[1] - (1.66811115361e-7)*param_inputs[0]*param_inputs[2]; // yp[8] = q
yp[9] = -0.002867* y[7] * y[8] + (-1.73667282186e-3)*y[0]*param_inputs[2] - (8.68336410928e-4)*y[3]*param_inputs[2] + (5.21001846557e-4)*y[10]*param_inputs[2] + (3.31156343047e-3)*param_inputs[2] - (8.68336410931e-9)*param_inputs[1]*param_inputs[0]; // yp[9] = r
yp[10] = 2*(1-y[0])-y[3]; // yp[10] = zI
yp[11] = 0;
yp[12] = 0;
yp[13] = 0;
}
else if (syschoice == 311) { // Quadcopter Mikhail Bessa
// y[0]=z ; y[1]=u ; y[2]=v ; y[3]=w ; y[4]=phi ; y[5]=theta ;
// y[6]=psi ; y[7]=p ; y[8]=q ; y[9]=r ; y[10]=zI ;
// param_inputs[0]=cmd_phi ; param_inputs[1]=cmd_theta ;
// param_inputs[2]=cmd_psi ;
yp[0] = (-sin(y[5]))*y[1] + cos(y[5])*sin(y[4])*y[2] + cos(y[5])*cos(y[4])*y[3]; // yp[0] = z
yp[1] = y[9]*y[2] - y[8]*y[3] + sin(y[5])*9.81; // yp[1] = u
yp[2] = -y[9]*y[1] + y[7]*y[3] - cos(y[5])*sin(y[4])*9.81; // yp[2] = v
yp[3] = y[8]*y[1] - y[7]*y[2] - 9.81*cos(y[5])*cos(y[4])+(7.62648576804e-10)*param_inputs[1]*param_inputs[1] + (7.62648576804e-10)*param_inputs[0]*param_inputs[0] + (3.05059430721e-9)*param_inputs[2]*param_inputs[2] + 7.62648576804*y[0]*y[0] + 7.62648576804*y[3]*y[0] - 4.57589146082*y[0]*y[10] + 1.90662144201*y[3]*y[3] - 2.28794573041*y[3]*y[10] + 0.686383719125*y[10]*y[10] - 29.0850309334*y[0] - 14.5425154667*y[3] + 8.72550928*y[10] + 27.7303023346; // yp[3] = w
yp[4] = y[7] + sin(y[5])/cos(y[5])*(cos(y[4])*y[9] + sin(y[4])*y[8]); // yp[4] = phi
yp[5] = cos(y[4])*y[8] - sin(y[4])*y[9]; // yp[5] = theta
yp[6] = cos(y[4]) / cos(y[5]) * y[9] + sin(y[4]) / cos(y[5]) * y[8]; // yp[6] = psi
yp[7] = -0.760697* y[8] * y[9] + (-8.38277868313e-3)*y[0]*param_inputs[0] - (4.19138934156e-3)*y[3]*param_inputs[0] + (2.51483360494e-3)*y[10]*param_inputs[0] + 0.0159846477606*param_inputs[0] - (1.67655573663e-7)*param_inputs[1]*param_inputs[2]; // yp[7] = p
yp[8] = 0.761902* y[7] * y[9] + (-8.34055576802e-3)*y[0]*param_inputs[1] - (4.17027788401e-3)*y[3]*param_inputs[1] + (2.50216673041e-3)*y[10]*param_inputs[1] + 0.0159041352657*param_inputs[1] - (1.66811115361e-7)*param_inputs[0]*param_inputs[2]; // yp[8] = q