AerialVehicleControl.jl
cont_power_distribution.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) Marcus Greiff 2020
3 *
4 * @file cont_power_distribution.c
5 * @author Marcus Greiff
6 * @date June 2020
7 *******************************************************************************/
9 
11  con_state_qw_fsf_t * controller,
12  matrix_double_t * dutyCycles
13 ){
14  int i;
15  double a = controller->param_a;
16  double b = controller->param_b;
17  double c = controller->param_c;
18  double dx = controller->param_d;
19  double dy = controller->param_d;
20  double adtb;
21  double f[4];
22 
23  /* Input error check*/
24  if (controller->thrust < 0.0) {
25  TRACE(5, ("The target thrust cannot be negative in the power distribution\n"));
26  return 0;
27  }
28  if (dutyCycles->numCols != 1 || dutyCycles->numRows != 4) {
29  TRACE(5, ("The dutyCycle vector is not of the correct dimensions\n"));
30  return 0;
31  }
32  if ((a <= 0.0) || (b <= 0.0) || (c <= 0.0)) {
33  TRACE(5, ("The input parameters must be positive\n"));
34  return 0;
35  }
36 
37  /* Compute the individual rotor forces */
38  /*
39  f[0] = controller->thrust +tid*controller->torque[1] -controller->torque[2]/c;
40  f[1] = controller->thrust -tid*controller->torque[0] +controller->torque[2]/c;
41  f[2] = controller->thrust -tid*controller->torque[1] -controller->torque[2]/c;
42  f[3] = controller->thrust +tid*controller->torque[0] +controller->torque[2]/c;
43  */
44 
45  /*
46  for the razor drone, with
47 
48  A = [ 1, 1, 1, 1]
49  [ -dy, -dy, dy, dy]
50  [ -dx, dx, dx, -dx]
51  [ c, -c, c, -c]
52 
53  we instead get
54 
55  inv(A) = [ 1/4, -1/(4*dy), -1/(4*dx), 1/(4*c)]
56  [ 1/4, -1/(4*dy), 1/(4*dx), -1/(4*c)]
57  [ 1/4, 1/(4*dy), 1/(4*dx), 1/(4*c)]
58  [ 1/4, 1/(4*dy), -1/(4*dx), -1/(4*c)]
59  */
60  /* Todo remove hard coded coefficients
61  dx = 0.09/2.0;
62  dy = 0.152/2.0;*/
63 
64  f[0] = controller->thrust -(1/dy)*controller->torque[0] -(1/dx)*controller->torque[1] +(1/c)*controller->torque[2];
65  f[1] = controller->thrust -(1/dy)*controller->torque[0] +(1/dx)*controller->torque[1] -(1/c)*controller->torque[2];
66  f[2] = controller->thrust +(1/dy)*controller->torque[0] +(1/dx)*controller->torque[1] +(1/c)*controller->torque[2];
67  f[3] = controller->thrust +(1/dy)*controller->torque[0] -(1/dx)*controller->torque[1] -(1/c)*controller->torque[2];
68  for (i = 0; i < 4; i++) f[i] /= 4.0;
69 
70  /* TODO make this saturation continuous using a hyperbolic tan function*/
71  for (i = 0; i < 4; i++){
72  if(f[i] < 0.0){
73  TRACE(5, ("Got a negative force, f[%i]=%f, rounding to zero.\nThis should not generally happen - your controller might be too agressively tuned.\n", i, f[i]));
74  f[i] = 0.0;
75  }
76  }
77 
78  /* Precomputation of parameters*/
79  adtb = a/(2.0*b);
80 
81  /* Compute the duty cycles */
82  for (i = 0; i < 4; i++ ) dutyCycles->pData[i] = - adtb + sqrt(adtb*adtb + f[i]/b);
83 
84  /* Assert that the duty cycles are positive*/
85  for (i = 0; i < 4; i++ ){
86  if (dutyCycles->pData[i] < 0.0){
87  TRACE(5, ("The duty cycles must be positive\n"));
88  return 0;
89  }
90  if (dutyCycles->pData[i] > 1.0){
91  dutyCycles->pData[i] = 1.0;
92  TRACE(5, ("Warning, attemting to set a duty cycle > 1 - should never happen\n"));
93  }
94  }
95  return 1;
96 }
matrix_double_s::pData
double * pData
Definition: cont_types.h:59
con_state_qw_fsf_s::torque
double torque[3]
Definition: cont_types.h:70
matrix_double_s::numRows
int numRows
Definition: cont_types.h:57
matrix_double_s
Matrix object used for all matrix manipulation.
Definition: cont_types.h:50
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
cont_power_distribution.h
con_state_qw_fsf_s::param_c
double param_c
Definition: cont_types.h:79
matrix_double_s::numCols
int numCols
Definition: cont_types.h:58
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
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