AerialVehicleControl.jl
cont_attitude_reference_generator.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) Marcus Greiff 2020
3 *
4 * @file cont_attitude_reference_generator.c
5 * @author Marcus Greiff
6 * @date June 2020
7 *******************************************************************************/
9 
11  matrix_double_t * commands,
12  matrix_double_t * mem,
13  ref_state_qw_t * reference,
14  double h
15 ){
16  int i, j;
17  double tmp[3], Bm[3];
18  matrix_double_t tmp31m, tmp41Am, tmp41Bm, tmp41Cm, tmp91m, Xm, Am, Qrm, Wrm, Arm, dQrm, ddQrm;
19 
20  double p = 4.0;
21  double psq = p*p;
22  double pcb = psq*p;
23  double hp = h*p;
24  double hsq = h*h;
25  double a = exp(-h * p);
26 
27  matrix_allocate(&Am, 3, 3);
28  matrix_allocate(&tmp41Am, 4, 1);
29  matrix_allocate(&tmp41Bm, 4, 1);
30  matrix_allocate(&tmp41Cm, 4, 1);
31  matrix_allocate(&dQrm, 4, 1);
32  matrix_allocate(&ddQrm, 4, 1);
33 
34  matrix_set(&Am, 0, 0, (a*(hsq*psq + 2.0*hp + 2.0))/2.0);
35  matrix_set(&Am, 0, 1, h*a*(hp + 1.0));
36  matrix_set(&Am, 0, 2, (hsq*a)/2.0);
37  matrix_set(&Am, 1, 0, -(hp*hp*p*a)/2.0);
38  matrix_set(&Am, 1, 1, a*(- hsq*psq + hp + 1.0));
39  matrix_set(&Am, 1, 2, -(h*a*(hp - 2.0))/2.0);
40  matrix_set(&Am, 2, 0, (hp*psq*a*(hp - 2.0))/2.0);
41  matrix_set(&Am, 2, 1, h*psq*a*(hp - 3));
42  matrix_set(&Am, 2, 2, (a*(hsq*psq - 4.0*h*p + 2.0))/2.0);
43  Bm[0] = 1.0 - (hsq*psq*a)/2.0 - hp*a - a;
44  Bm[1] = (hsq*pcb*a)/2.0;
45  Bm[2] = -(hp*psq*a*(hp - 2.0))/2.0;
46 
47  for (i = 0; i < 4; i++){
48  matrix_define(&Xm, 3, 1, &mem->pData[i*3]);
49  matrix_define(&tmp31m, 3, 1, tmp);
50  mat_mul(&Am, &Xm, &tmp31m);
51  for (j = 0; j < 3; j++ ) mem->pData[i*3 + j] = tmp[j] + Bm[j] * commands->pData[i];
52  }
53 
54  matrix_define(&Qrm, 4, 1, reference->quaternion);
55  matrix_define(&Wrm, 3, 1, reference->omega);
56  matrix_define(&Arm, 3, 1, reference->alpha);
57 
58  matrix_define(&tmp91m, 9, 1, &mem->pData[3]);
59 
60  /* Compute the reference quaternion */
61  cont_SU2_triple_derivatives(&tmp91m, 0, 0, 0, &Qrm);
62 
63  /* Compute the reference quaternion time terivative */
64  cont_SU2_triple_derivatives(&tmp91m, 1, 0, 0, &dQrm);
65  cont_SU2_triple_derivatives(&tmp91m, 0, 1, 0, &tmp41Am);
66  mat_add_inplace(&dQrm, &tmp41Am);
67  cont_SU2_triple_derivatives(&tmp91m, 0, 0, 1, &tmp41Am);
68  mat_add_inplace(&dQrm, &tmp41Am);
69 
70  /* Compute the reference quaternion second time derivative */
71  cont_SU2_triple_derivatives(&tmp91m, 2, 0, 0, &ddQrm);
72  cont_SU2_triple_derivatives(&tmp91m, 0, 2, 0, &tmp41Am);
73  mat_add_inplace(&ddQrm, &tmp41Am);
74  cont_SU2_triple_derivatives(&tmp91m, 0, 0, 2, &tmp41Am);
75  mat_add_inplace(&ddQrm, &tmp41Am);
76  cont_SU2_triple_derivatives(&tmp91m, 1, 1, 0, &tmp41Am);
77  mat_add_inplace(&ddQrm, &tmp41Am);
78  mat_add_inplace(&ddQrm, &tmp41Am);
79  cont_SU2_triple_derivatives(&tmp91m, 1, 0, 1, &tmp41Am);
80  mat_add_inplace(&ddQrm, &tmp41Am);
81  mat_add_inplace(&ddQrm, &tmp41Am);
82  cont_SU2_triple_derivatives(&tmp91m, 0, 1, 1, &tmp41Am);
83  mat_add_inplace(&ddQrm, &tmp41Am);
84  mat_add_inplace(&ddQrm, &tmp41Am);
85 
86  /* Ang. rate ref. w = 2 * [conj(q) * dq]^{vee} */
87  cont_SU2_conjugate(&Qrm, &tmp41Am);
88  cont_SU2_product(&tmp41Am, &dQrm, &tmp41Bm);
89  for (i = 0; i < 3; i++ ) Wrm.pData[i] = 2.0*tmp41Bm.pData[i+1];
90 
91  /* Ang. acc. ref. a = [conj(q) * (2*ddq - dq * [w]^{hat})]^{vee} */
92  cont_SU2_hat(&Wrm, &tmp41Am);
93  cont_SU2_product(&dQrm, &tmp41Am, &tmp41Bm);
94  for (i = 0; i < 4; i++ ) tmp41Am.pData[i] = 2.0*ddQrm.pData[i] - tmp41Bm.pData[i];
95  cont_SU2_conjugate(&Qrm, &tmp41Bm);
96  cont_SU2_product(&tmp41Bm, &tmp41Am, &tmp41Cm);
97  cont_SU2_vee(&tmp41Cm, &Arm);
98 
99  /* Set the reference thrust to the filtered thrust */
100  reference->thrust = CONT_MASS * CONT_GRAVACC * (mem->pData[0] / 2.0 + 0.5);
101 
102  /* Free malloced memory */
103  free(Am.pData);
104  free(tmp41Am.pData);
105  free(tmp41Bm.pData);
106  free(tmp41Cm.pData);
107  free(dQrm.pData);
108  free(ddQrm.pData);
109  return 1;
110 }
111 
113 /* TODO refactor this */
114 int cont_SU2_triple_derivatives(
115  matrix_double_t * in,
116  int derivA,
117  int derivB,
118  int derivC,
119  matrix_double_t * out
120 ){
121  double ca, cb, cc, sa, sb, sc;
122  matrix_double_t tmp31m;
123 
124  if((in->numCols != 1) ||
125  (in->numRows != 9) ||
126  (out->numCols != 1) ||
127  (out->numRows != 4)){
128  TRACE(5, ("Bad input dimensions\n"));
129  return 0;
130  }
131 
132  matrix_define(&tmp31m, 3, 1, &in->pData[0]);
133  cont_get_cossin(&tmp31m, derivA, &ca, &sa);
134  matrix_define(&tmp31m, 3, 1, &in->pData[3]);
135  cont_get_cossin(&tmp31m, derivB, &cb, &sb);
136  matrix_define(&tmp31m, 3, 1, &in->pData[6]);
137  cont_get_cossin(&tmp31m, derivC, &cc, &sc);
138 
139  out->pData[0] = ca*cb*cc - sa*sb*sc;
140  out->pData[1] = cb*cc*sa + ca*sb*sc;
141  out->pData[2] = ca*cc*sb - cb*sa*sc;
142  out->pData[3] = ca*cb*sc + cc*sa*sb;
143 
144  return 1;
145 }
146 
147 /* TODO refactor this */
148 int cont_get_cossin(matrix_double_t * input, int deriv, double * ca, double * sa) {
149  double a = input->pData[0] / 2.0;
150  double da = input->pData[1] / 2.0;
151  double dda = input->pData[2] / 2.0;
152 
153  *ca = 0.0;
154  *sa = 0.0;
155  if (deriv == 0) {
156  *ca = cos(a);
157  *sa = sin(a);
158  } else if (deriv == 1){
159  *ca = -sin(a)*da;
160  *sa = +cos(a)*da;
161  } else if (deriv == 2){
162  *ca = -cos(a)*da*da - sin(a)*dda;
163  *sa = -sin(a)*da*da + cos(a)*dda ;
164  } else {
165  TRACE(5, ("Bad input derivative order - must be {0,1,2}\n"));
166  return 0;
167  }
168  return 1;
169 }
matrix_double_s::pData
double * pData
Definition: cont_types.h:59
CONT_GRAVACC
#define CONT_GRAVACC
Definition: cont_math.h:15
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
ref_state_qw_s::alpha
double alpha[3]
Definition: cont_types.h:114
ref_state_qw_s::omega
double omega[3]
Definition: cont_types.h:113
matrix_double_s::numRows
int numRows
Definition: cont_types.h:57
ref_state_qw_s::thrust
double thrust
Definition: cont_types.h:115
update_attitude_references
int update_attitude_references(matrix_double_t *commands, matrix_double_t *mem, ref_state_qw_t *reference, double h)
Update the reference trajectory based on input commands.
Definition: cont_attitude_reference_generator.c:10
matrix_double_s
Matrix object used for all matrix manipulation.
Definition: cont_types.h:50
mat_add_inplace
void mat_add_inplace(matrix_double_t *Amat, matrix_double_t *Bmat)
In-place matrix addition, A <– A + B, asserts that this is done correctly.
Definition: cont_matrix_math.c:249
cont_SU2_hat
int cont_SU2_hat(matrix_double_t *in, matrix_double_t *out)
The hat map on SU(2)
Definition: cont_math.c:69
ref_state_qw_s
Reference signal structure for the attitude FSF on S(3) or SU(2)
Definition: cont_types.h:111
matrix_double_s::numCols
int numCols
Definition: cont_types.h:58
cont_SU2_product
int cont_SU2_product(matrix_double_t *q, matrix_double_t *p, matrix_double_t *out)
Product of two elements of SU(2)
Definition: cont_math.c:28
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
cont_attitude_reference_generator.h
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
mat_mul
void mat_mul(matrix_double_t *Amat, matrix_double_t *Bmat, matrix_double_t *Cmat)
and asserts that this is done correctly
Definition: cont_matrix_math.c:258
cont_SU2_vee
int cont_SU2_vee(matrix_double_t *in, matrix_double_t *out)
The vee map on SU(2)
Definition: cont_math.c:53
cont_SU2_conjugate
int cont_SU2_conjugate(matrix_double_t *qin, matrix_double_t *qout)
Conjugate an element of SU(2)
Definition: cont_math.c:10
CONT_MASS
#define CONT_MASS
Definition: cont_math.h:14
ref_state_qw_s::quaternion
double quaternion[4]
Definition: cont_types.h:112