balance_gazebo_control.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Copyright (c) 2015 Robosavvy Ltd.
3  * Author: Vitor Matos
4  *
5  *********************************************************************/
7 
8 namespace balance_control
9 {
10 
12 
19 {
20  t = 0;
21  for (int i=0; i < 4; i++)
22  {
23  x_hat[i] = 0;
24  x_adjust[i] = 0;
25  x_r[i] = 0;
26  }
27  u_output[0] = 0.0;
28  u_output[1] = 0.0;
29 }
30 
39 void BalanceControl::stepControl(double dt, const double (&x_desired)[4], const double (&y_fbk)[4])
40 {
41  t += dt;
42  //**************************************************************
43  // Set reference state
44  //**************************************************************
45  x_reference[theta] = x_desired[theta] + x_adjust[theta];
46  x_reference[dx] = x_desired[dx] + x_adjust[dx];
47  x_reference[dphi] = x_desired[dphi] + x_adjust[dphi];
48  x_reference[dtheta] = x_desired[dtheta] + x_adjust[dtheta];
49 
50  //**************************************************************
51  // State estimation
52  //**************************************************************
53  // x_hat - x_ref
54  float x_hat_x_ref[4];
55  x_hat_x_ref[theta] = x_hat[theta] - x_reference[theta];
56  x_hat_x_ref[dx] = x_hat[dx] - x_reference[dx];
57  x_hat_x_ref[dphi] = x_hat[dphi] - x_reference[dphi];
58  x_hat_x_ref[dtheta] = x_hat[dtheta] - x_reference[dtheta];
59 
60  // A*(x_hat-x_ref)
61  dx_hat[theta] = A[0][0]*x_hat_x_ref[theta] + A[0][1]*x_hat_x_ref[dx]
62  + A[0][2]*x_hat_x_ref[dphi] + A[0][3]*x_hat_x_ref[dtheta];
63  dx_hat[dx] = A[1][0]*x_hat_x_ref[theta] + A[1][1]*x_hat_x_ref[dx]
64  + A[1][2]*x_hat_x_ref[dphi] + A[1][3]*x_hat_x_ref[dtheta];
65  dx_hat[dphi] = A[2][0]*x_hat_x_ref[theta] + A[2][1]*x_hat_x_ref[dx]
66  + A[2][2]*x_hat_x_ref[dphi] + A[2][3]*x_hat_x_ref[dtheta];
67  dx_hat[dtheta] = A[3][0]*x_hat_x_ref[theta] + A[3][1]*x_hat_x_ref[dx]
68  + A[3][2]*x_hat_x_ref[dphi] + A[3][3]*x_hat_x_ref[dtheta];
69 
70  // + B*u_output
71  dx_hat[theta] += B[0][0]*u_output[tauL] + B[0][1]*u_output[tauR];
72  dx_hat[dx] += B[1][0]*u_output[tauL] + B[1][1]*u_output[tauR];
73  dx_hat[dphi] += B[2][0]*u_output[tauL] + B[2][1]*u_output[tauR];
74  dx_hat[dtheta] += B[3][0]*u_output[tauL] + B[3][1]*u_output[tauR];
75 
76  // y - C*x_hat - x_desired
77  float yC[4];
78  yC[0] = y_fbk[theta] - (C[0][0]*x_hat[theta] + C[0][1]*x_hat[dx] + C[0][2]*x_hat[dphi] + C[0][3]*x_hat[dtheta]);
79  yC[1] = y_fbk[dx] - (C[1][0]*x_hat[theta] + C[1][1]*x_hat[dx] + C[1][2]*x_hat[dphi] + C[1][3]*x_hat[dtheta]);
80  yC[2] = y_fbk[dphi] - (C[2][0]*x_hat[theta] + C[2][1]*x_hat[dx] + C[2][2]*x_hat[dphi] + C[2][3]*x_hat[dtheta]);
81  yC[3] = y_fbk[dtheta] - (C[3][0]*x_hat[theta] + C[3][1]*x_hat[dx] + C[3][2]*x_hat[dphi] + C[3][3]*x_hat[dtheta]);
82 
83  // L*(y-C*x_hat)
84  dx_hat[theta] += L[0][0]*yC[0] + L[0][1]*yC[1] + L[0][2]*yC[2] + L[0][3]*yC[3];
85  dx_hat[dx] += L[1][0]*yC[0] + L[1][1]*yC[1] + L[1][2]*yC[2] + L[1][3]*yC[3];
86  dx_hat[dphi] += L[2][0]*yC[0] + L[2][1]*yC[1] + L[2][2]*yC[2] + L[2][3]*yC[3];
87  dx_hat[dtheta] += L[3][0]*yC[0] + L[3][1]*yC[1] + L[3][2]*yC[2] + L[3][3]*yC[3];
88 
89  // Integrate Observer, Euler method:
90  x_hat[theta] += dx_hat[theta]*dt;
91  x_hat[dx] += dx_hat[dx]*dt;
92  x_hat[dphi] += dx_hat[dphi]*dt;
93  x_hat[dtheta] += dx_hat[dtheta]*dt;
94 
95  //**************************************************************
96  // Reference model
97  //**************************************************************
98  float x_r_x_ref[4];
99  x_r_x_ref[theta] = x_r[theta] - x_reference[theta];
100  x_r_x_ref[dx] = x_r[dx] - x_reference[dx];
101  x_r_x_ref[dphi] = x_r[dphi] - x_reference[dphi];
102  x_r_x_ref[dtheta] = x_r[dtheta] - x_reference[dtheta];
103  x_r[theta] += (A_BK[0][0]*x_r_x_ref[theta] + A_BK[0][1]*x_r_x_ref[dx]
104  + A_BK[0][2]*x_r_x_ref[dphi] + A_BK[0][3]*x_r_x_ref[dtheta])*dt;
105  x_r[dx] += (A_BK[1][0]*x_r_x_ref[theta] + A_BK[1][1]*x_r_x_ref[dx]
106  + A_BK[1][2]*x_r_x_ref[dphi] + A_BK[1][3]*x_r_x_ref[dtheta])*dt;
107  x_r[dphi] += (A_BK[2][0]*x_r_x_ref[theta] + A_BK[2][1]*x_r_x_ref[dx]
108  + A_BK[2][2]*x_r_x_ref[dphi] + A_BK[2][3]*x_r_x_ref[dtheta])*dt;
109  x_r[dtheta] += (A_BK[3][0]*x_r_x_ref[theta] + A_BK[3][1]*x_r_x_ref[dx]
110  + A_BK[3][2]*x_r_x_ref[dphi] + A_BK[3][3]*x_r_x_ref[dtheta])*dt;
111 
112  // Adjust theta based on reference model
113  float e_r_dx;
114  // e_r_dx = x_r[dx] - x_hat[dx];
115  e_r_dx = x_r[dx] - y_fbk[dx];
116  x_adjust[theta] += (.025*e_r_dx)*dt;
117 
118  //**************************************************************
119  // Feedback control
120  //**************************************************************
121  u_output[tauL] = -(K[0][0]*(x_hat[theta]-x_reference[theta]) + K[0][1]*(x_hat[dx]-x_reference[dx])
122  + K[0][2]*(x_hat[dphi]-x_reference[dphi]) + K[0][3]*x_hat[dtheta]);
123  u_output[tauR] = -(K[1][0]*(x_hat[theta]-x_reference[theta]) + K[1][1]*(x_hat[dx]-x_reference[dx])
124  + K[1][2]*(x_hat[dphi]-x_reference[dphi]) + K[1][3]*x_hat[dtheta]);
125 }
126 
133 {
134  return u_output;
135 }
136 } // namespace balance_control
double * getControl()
Set up the output array.
const float C[4][4]
Definition: control.cpp:24
const float A_BK[4][4]
Definition: control.cpp:40
void stepControl(double dt, const double(&x_desired)[4], const double(&y_fbk)[4])
Integrates control and models.
void resetControl()
Resets all state and control variables.
const float L[4][4]
Definition: control.cpp:30
const float A[4][4]
Definition: control.cpp:12
const float B[4][2]
Definition: control.cpp:18
const float K[2][4]
Definition: control.cpp:36


rsv_balance_gazebo_control
Author(s): Vitor Matos
autogenerated on Mon Jun 10 2019 15:06:39