AerialVehicleControl.jl
cont_main.c
Go to the documentation of this file.
1 #include "cont_main.h"
2 
3 /*******************************************************************************
4  * This is an example of how the attitude controllers could be implemented
5  * used mainly for checking of memory leaks using valgrind.
6  ******************************************************************************/
7 int main(int argc, char *argv[]) {
8  /* Run the FSF attitude cotroller in closed loop for 100 time-steps */
10  /* Run the FOF attitude cotroller in closed loop for 100 time-steps */
12  return 0;
13 }
14 
16 
17  int i;
18  double dt;
19  matrix_double_t Jm, commandm, filtermemm, dutycyclem;
20  dyn_state_qw_t * state;
21  con_state_qw_fsf_t * controller;
22  ref_state_qw_t * reference;
23 
24  printf("Allocate memory for the reference, state and controller structs...\n");
25  state = (dyn_state_qw_t*) calloc(1, sizeof(dyn_state_qw_t));
26  controller = (con_state_qw_fsf_t*) calloc(1, sizeof(con_state_qw_fsf_t));
27  reference = (ref_state_qw_t*) calloc(1, sizeof(ref_state_qw_t));
28 
29  matrix_allocate(&commandm, 4, 1);
30  matrix_allocate(&filtermemm, 12, 1);
31  matrix_allocate(&dutycyclem, 4, 1);
32 
33  printf("Set some controller parameters, define dynamics and reference\n");
34  /* Initialize filter reference smoother state, commands and set a timestep */
35  for (i = 0; i < 4 ; i++) commandm.pData[i] = 0.0;
36  for (i = 0; i < 12 ; i++) filtermemm.pData[i] = 0.0;
37  dt = 0.1;
38 
39  controller->status = 1; /* Controller status - 0 for idle 1 for active */
40  controller->gain_kR = 4.0; /* Gain relating to the attitude error */
41  controller->gain_kc = 0.1; /* Gain relating to the cross-terms */
42  controller->gain_kw = 2.0; /* Gain relating to the attitude rate error */
43  controller->gain_eps = 0.1; /* Gain relating to the cross-terms */
44  controller->gain_L = 2.0; /* Gain relating to the attitude rate error */
45  controller->param_a = 0.35; /* Constant related to the PWM-thrust map */
46  controller->param_b = 0.26; /* Constant related to the PWM-thrust map */
47  controller->param_c = 0.1; /* Constant related to rotor motor torque */
48  controller->param_d = 0.2; /* Distance from rotors to the center of mass */
49 
50  /* Controller inertia */
51  matrix_define(&Jm, 3, 3, controller->inertia);
52  matrix_set(&Jm, 0, 0, 1.0);
53  matrix_set(&Jm, 0, 2, 0.2);
54  matrix_set(&Jm, 0, 1, 0.1);
55  matrix_set(&Jm, 1, 0, 0.1);
56  matrix_set(&Jm, 1, 1, 1.0);
57  matrix_set(&Jm, 1, 2, 0.3);
58  matrix_set(&Jm, 2, 0, 0.2);
59  matrix_set(&Jm, 2, 1, 0.3);
60  matrix_set(&Jm, 2, 2, 1.0);
61  /*matrix_print(&Jm, "Defined inertia");*/
62 
63  for (i = 0; i < 100; i++){
64  /* 1. Update the system state in "state" and update user commands in "comamndm"*/
65  /* state->quaternion = ... */
66  /* state->omega = ... */
67  /* commandm->pData[0] = ... */ /* lateral thrust force reference */
68  /* commandm->pData[1] = ... */ /* pitch ref (rotation about the e1 axis)*/
69  /* commandm->pData[2] = ... */ /* roll ref (rotation about the e2 axis)*/
70  /* commandm->pData[3] = ... */ /* yaw ref (rotation about the e3 axis)*/
71  /* dt = ... */ /* time-step since last update */
72 
73  /* 2. Update the reference trajectory */
74  assert(1==update_attitude_references(&commandm, &filtermemm, reference, dt));
75 
76  /* 3. Compute the rigid-body forces and torques in the controller, we can
77  use any of the controllers below, but all are called for debugging purposes
78  to check that there are no memory leaks */
79  assert(1==update_attitude_FSF_SO3_continuous(reference, state, controller));
80  assert(1==update_attitude_FSF_SO3_robust(reference, state, controller));
81  assert(1==update_attitude_FSF_SU2_continuous(reference, state, controller));
82  assert(1==update_attitude_FSF_SU2_discontinuous(reference, state, controller));
83  assert(1==update_attitude_FSF_SU2_robust(reference, state, controller));
84 
85  /* 4. Compute the desired thrust - now just setting the reference directly
86  here the force could be projected so as to be constant in the z-direction if
87  unchanged by the uer. */
88  controller->thrust = reference->thrust;
89 
90  /* 5. Externalize the moments and forces in the controller to duty cycles */
91  assert(1 == compute_power_distribution(controller, &dutycyclem));
92  }
93 
94  printf("Free allocated memory...\n");
95  free(state);
96  free(controller);
97  free(reference);
98 
99  free(commandm.pData);
100  free(filtermemm.pData);
101  free(dutycyclem.pData);
102 
103 }
104 
106 
107  int i;
108  double dt;
109  matrix_double_t Jm, iJm, Kwm, Cwm, Vim, y0m, y1m, y2m, y3m, commandm, filtermemm, dutycyclem;
110  dyn_state_qw_t * state;
111  con_state_qw_fof_t * controller;
112  ref_state_qw_t * reference;
113 
114  printf("Allocate memory for the reference, state and controller structs...\n");
115  state = (dyn_state_qw_t*) calloc(1, sizeof(dyn_state_qw_t));
116  controller = (con_state_qw_fof_t*) calloc(1, sizeof(con_state_qw_fof_t));
117  reference = (ref_state_qw_t*) calloc(1, sizeof(ref_state_qw_t));
118 
119  matrix_allocate(&commandm, 4, 1);
120  matrix_allocate(&filtermemm, 12, 1);
121  matrix_allocate(&dutycyclem, 4, 1);
122 
123  printf("Set some controller parameters, define dynamics and reference\n");
124  /* Initialize filter reference smoother state, commands and set a timestep */
125  for (i = 0; i < 4 ; i++) commandm.pData[i] = 0.0;
126  for (i = 0; i < 12 ; i++) filtermemm.pData[i] = 0.0;
127  dt = 0.1;
128 
129  /* Scalar controller parameters */
130  controller->status = 1; /* Controller status - 0 for idle 1 for active */
131  controller->gain_cw = 1.0; /* Gain relating to the attitude rate error */
132  controller->gain_cR = 1.0; /* Gain relating to the attitude rate error */
133  controller->param_a = 0.35; /* Constant related to the PWM-thrust map */
134  controller->param_b = 0.26; /* Constant related to the PWM-thrust map */
135  controller->param_c = 0.1; /* Constant related to rotor motor torque */
136  controller->param_d = 0.2; /* Distance from rotors to the center of mass */
137 
138  /* Controller inertia */
139  matrix_define(&Jm, 3, 3, controller->inertia);
140  matrix_zero(&Jm);
141  matrix_set(&Jm, 0, 0, 0.1);
142  matrix_set(&Jm, 1, 1, 0.1);
143  matrix_set(&Jm, 2, 2, 0.1);
144 
145  /* Controller inertia inverse */
146  matrix_define(&iJm, 3, 3, controller->invinertia);
147  matrix_zero(&iJm);
148  matrix_set(&iJm, 0, 0, 1.0/0.1);
149  matrix_set(&iJm, 1, 1, 1.0/0.1);
150  matrix_set(&iJm, 2, 2, 1.0/0.1);
151 
152  /* Controller gain Kw */
153  matrix_define(&Kwm, 3, 3, controller->gain_Kw);
154  matrix_identity(&Kwm);
155 
156  /* Controller gain ki */
157  for ( i = 0; i < 3; i++ ) controller->gain_ki[i] = 1.0;
158 
159  /* Controller gain Cw */
160  matrix_define(&Cwm, 3, 3, controller->gain_Cw);
161  matrix_identity(&Cwm);
162 
163  /* Controller global directions Cw */
164  matrix_define(&Vim, 3, 3, controller->globalDirections);
165  matrix_identity(&Vim);
166 
167  /* Matrices pointing to the measurements */
168  matrix_allocate(&y0m, 3, 1);
169  matrix_allocate(&y1m, 3, 1);
170  matrix_allocate(&y2m, 3, 1);
171  matrix_allocate(&y3m, 3, 1);
172 
173  for (i = 0; i < 100; i++){
174  /* 1. Update user commands in "comamndm"*/
175  /* commandm->pData[0] = ... */ /* lateral thrust force reference */
176  /* commandm->pData[1] = ... */ /* pitch ref (rotation about the e1 axis)*/
177  /* commandm->pData[2] = ... */ /* roll ref (rotation about the e2 axis)*/
178  /* commandm->pData[3] = ... */ /* yaw ref (rotation about the e3 axis)*/
179  /* dt = ... */ /* time-step since last update */
180 
181  /* 2. Update the reference trajectory*/
182  assert(1==update_attitude_references(&commandm, &filtermemm, reference, dt));
183 
184  /* 3. Update the measurements in the controller struct - gyro rates in y0,
185  and the accelerometer directions in y1, with mg- directions in y2
186  and a virtual measurements taken as the cross-product of the two */
187  matrix_set(&y0m, 0, 0, +0.1);
188  matrix_set(&y0m, 1, 0, -0.1);
189  matrix_set(&y0m, 2, 0, +0.2);
190  matrix_set(&y1m, 0, 0, +0.3);
191  matrix_set(&y1m, 1, 0, -0.1);
192  matrix_set(&y1m, 2, 0, -0.1);
193  matrix_set(&y2m, 0, 0, -0.1);
194  matrix_set(&y2m, 1, 0, -0.1);
195  matrix_set(&y2m, 2, 0, +0.3);
196  cont_normalize(&y1m); /* Normalize the dir. meas. */
197  cont_normalize(&y2m); /* Normalize the dir. meas. */
198  cont_cross_product(&y1m, &y2m, &y3m); /* Construct virtual measurement */
199  cont_normalize(&y3m); /* Normalize virtual measurement */
200  assert(1==update_attitude_FOF_SO3_continuous_measurements(controller, &y0m, &y1m, &y2m, &y3m));
201 
202  /* 4. Compute the rigid-body forces and torques in the controller, we can
203  use any of the controllers below, but all are called for debugging purposes
204  to check that there are no memory leaks */
205  assert(1==update_attitude_FOF_SO3_continuous(reference, state, controller, dt));
206 
207  /* 5. Compute the desired thrust - now just setting the reference directly
208  here the force could be projected so as to be constant in the z-direction if
209  unchanged by the uer. */
210 
211  /* TODO fix the rotation of the thrust
212  controller->thrust = reference->thrust;
213  */
214 
215  /* 6. Externalize the moments and forces in the controller to duty cycles
216  assert(1 == compute_power_distribution(controller, &dutycyclem));
217  TODO, write the power distribution controller independent
218  */
219  }
220 
221  printf("Free allocated memory...\n");
222  free(state);
223  free(controller);
224  free(reference);
225 
226  free(commandm.pData);
227  free(filtermemm.pData);
228  free(dutycyclem.pData);
229  free(y0m.pData);
230  free(y1m.pData);
231  free(y2m.pData);
232  free(y3m.pData);
233 
234 }
con_state_qw_fsf_s::status
int status
Definition: cont_types.h:68
matrix_double_s::pData
double * pData
Definition: cont_types.h:59
con_state_qw_fof_s::param_c
double param_c
Definition: cont_types.h:101
update_attitude_FSF_SU2_robust
int update_attitude_FSF_SU2_robust(ref_state_qw_t *reference, dyn_state_qw_t *state, con_state_qw_fsf_t *controller)
Update using the continuous full state feedback (FSF) on SU(2)
Definition: cont_attitude_FSF_SU2_robust.c:10
con_state_qw_fsf_s::gain_kc
double gain_kc
Definition: cont_types.h:73
matrix_allocate
void matrix_allocate(matrix_double_t *matrix, int numRows, int numCols)
Allocate memory for a matrix struct of given dimensions.
Definition: cont_types.c:65
con_state_qw_fof_s::gain_Cw
double gain_Cw[9]
Definition: cont_types.h:96
ref_state_qw_s::thrust
double thrust
Definition: cont_types.h:115
matrix_zero
void matrix_zero(matrix_double_t *matrix)
Set all antries of a matrix to zero.
Definition: cont_types.c:49
cont_cross_product
void cont_cross_product(matrix_double_t *inAm, matrix_double_t *inBm, matrix_double_t *outm)
Cross product operation, To be migrated to the cont_math stack.
Definition: cont_math.c:359
con_state_qw_fof_s::gain_cR
double gain_cR
Definition: cont_types.h:98
main
int main(int argc, char *argv[])
Main program, facilitating memory checks and analysis of the code.
Definition: cont_main.c:7
con_state_qw_fof_s::param_b
double param_b
Definition: cont_types.h:100
con_state_qw_fsf_s::gain_L
double gain_L
Definition: cont_types.h:76
matrix_double_s
Matrix object used for all matrix manipulation.
Definition: cont_types.h:50
con_state_qw_fsf_s::gain_kw
double gain_kw
Definition: cont_types.h:74
con_state_qw_fsf_s::param_c
double param_c
Definition: cont_types.h:79
con_state_qw_fof_s::gain_ki
double gain_ki[3]
Definition: cont_types.h:95
matrix_identity
void matrix_identity(matrix_double_t *matrix)
Set a square matrix to the identity matrix.
Definition: cont_types.c:59
ref_state_qw_s
Reference signal structure for the attitude FSF on S(3) or SU(2)
Definition: cont_types.h:111
con_state_qw_fsf_s::param_b
double param_b
Definition: cont_types.h:78
con_state_qw_fsf_s
Complete state of the attitude FSF on SO(3) or SU(2)
Definition: cont_types.h:67
matrix_set
void matrix_set(matrix_double_t *mat, int row, int column, double value)
Set an entry of a matrix (irrespective of the used memory layout)
Definition: cont_types.c:40
con_state_qw_fsf_s::gain_eps
double gain_eps
Definition: cont_types.h:75
matrix_define
void matrix_define(matrix_double_t *matrix, int numRows, int numCols, double *data)
Define a matrix to use already allocated memory.
Definition: cont_types.c:75
update_attitude_references
int update_attitude_references(matrix_double_t *commands, matrix_double_t *filterMemory, ref_state_qw_t *reference, double h)
Update the reference trajectory based on input commands.
Definition: cont_attitude_reference_generator.c:10
con_state_qw_fof_s
Complete state of the attitude FOF on SO(3) or SU(2)
Definition: cont_types.h:89
update_attitude_FSF_SU2_discontinuous
int update_attitude_FSF_SU2_discontinuous(ref_state_qw_t *reference, dyn_state_qw_t *state, con_state_qw_fsf_t *controller)
Update using the discontinuous full state feedback (FSF) on SU(2)
Definition: cont_attitude_FSF_SU2_discontinuous.c:10
dyn_state_qw_s
Dynamical states, assumed known in the attitude FSF on SO(3) or SU(2)
Definition: cont_types.h:59
example_attitude_FSF
void example_attitude_FSF(void)
Example of how the attitude FSF controllers can be implemented.
Definition: cont_main.c:15
update_attitude_FSF_SU2_continuous
int update_attitude_FSF_SU2_continuous(ref_state_qw_t *reference, dyn_state_qw_t *state, con_state_qw_fsf_t *controller)
Update using the continuous full state feedback (FSF) on SU(2)
Definition: cont_attitude_FSF_SU2_continuous.c:10
update_attitude_FSF_SO3_robust
int update_attitude_FSF_SO3_robust(ref_state_qw_t *reference, dyn_state_qw_t *state, con_state_qw_fsf_t *controller)
Update using the robust full state feedback (FSF) on SO(3)
Definition: cont_attitude_FSF_SO3_robust.c:10
con_state_qw_fsf_s::param_d
double param_d
Definition: cont_types.h:80
con_state_qw_fsf_s::thrust
double thrust
Definition: cont_types.h:69
con_state_qw_fsf_s::param_a
double param_a
Definition: cont_types.h:77
con_state_qw_fof_s::gain_cw
double gain_cw
Definition: cont_types.h:97
con_state_qw_fof_s::invinertia
double invinertia[9]
Definition: cont_types.h:93
compute_power_distribution
int compute_power_distribution(con_state_qw_fsf_t *controller, matrix_double_t *dutyCycles)
Compute the desired rotor thrusts.
Definition: cont_power_distribution.c:10
con_state_qw_fsf_s::gain_kR
double gain_kR
Definition: cont_types.h:72
con_state_qw_fof_s::globalDirections
double globalDirections[9]
Definition: cont_types.h:105
con_state_qw_fsf_s::inertia
double inertia[9]
Definition: cont_types.h:71
con_state_qw_fof_s::param_a
double param_a
Definition: cont_types.h:99
example_attitude_FOF
void example_attitude_FOF(void)
Example of how the attitude FOF controllers can be implemented.
Definition: cont_main.c:105
con_state_qw_fof_s::inertia
double inertia[9]
Definition: cont_types.h:92
cont_normalize
int cont_normalize(matrix_double_t *vec)
Normalizes the vector.
Definition: cont_math.c:378
update_attitude_FOF_SO3_continuous_measurements
int update_attitude_FOF_SO3_continuous_measurements(con_state_qw_fof_t *controller, matrix_double_t *y0m, matrix_double_t *y1m, matrix_double_t *y2m, matrix_double_t *y3m)
Help function used to update the measurements in the controller struct.
Definition: cont_attitude_FOF_SO3_continuous.c:10
cont_main.h
update_attitude_FOF_SO3_continuous
int update_attitude_FOF_SO3_continuous(ref_state_qw_t *reference, dyn_state_qw_t *state, con_state_qw_fof_t *controller, double dt)
Update using the continuous filtered outpot feedback (FOF) on SO(3)
Definition: cont_attitude_FOF_SO3_continuous.c:44
con_state_qw_fof_s::param_d
double param_d
Definition: cont_types.h:102
con_state_qw_fof_s::gain_Kw
double gain_Kw[9]
Definition: cont_types.h:94
con_state_qw_fof_s::status
int status
Definition: cont_types.h:90
update_attitude_FSF_SO3_continuous
int update_attitude_FSF_SO3_continuous(ref_state_qw_t *reference, dyn_state_qw_t *state, con_state_qw_fsf_t *controller)
Update using the continuous full state feedback (FSF) on SO(3)
Definition: cont_attitude_FSF_SO3_continuous.c:10