-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuadControl.cpp
294 lines (234 loc) · 10.4 KB
/
QuadControl.cpp
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
#include "Common.h"
#include "QuadControl.h"
#include "Utility/SimpleConfig.h"
#include "Utility/StringUtils.h"
#include "Trajectory.h"
#include "BaseController.h"
#include "Math/Mat3x3F.h"
#ifdef __PX4_NUTTX
#include <systemlib/param/param.h>
#endif
void QuadControl::Init()
{
BaseController::Init();
// variables needed for integral control
integratedAltitudeError = 0;
#ifndef __PX4_NUTTX
// Load params from simulator parameter system
ParamsHandle config = SimpleConfig::GetInstance();
// Load parameters (default to 0)
kpPosXY = config->Get(_config+".kpPosXY", 0);
kpPosZ = config->Get(_config + ".kpPosZ", 0);
KiPosZ = config->Get(_config + ".KiPosZ", 0);
kpVelXY = config->Get(_config + ".kpVelXY", 0);
kpVelZ = config->Get(_config + ".kpVelZ", 0);
kpBank = config->Get(_config + ".kpBank", 0);
kpYaw = config->Get(_config + ".kpYaw", 0);
kpPQR = config->Get(_config + ".kpPQR", V3F());
maxDescentRate = config->Get(_config + ".maxDescentRate", 100);
maxAscentRate = config->Get(_config + ".maxAscentRate", 100);
maxSpeedXY = config->Get(_config + ".maxSpeedXY", 100);
maxAccelXY = config->Get(_config + ".maxHorizAccel", 100);
maxTiltAngle = config->Get(_config + ".maxTiltAngle", 100);
minMotorThrust = config->Get(_config + ".minMotorThrust", 0);
maxMotorThrust = config->Get(_config + ".maxMotorThrust", 100);
#else
// load params from PX4 parameter system
//TODO
param_get(param_find("MC_PITCH_P"), &Kp_bank);
param_get(param_find("MC_YAW_P"), &Kp_yaw);
#endif
}
VehicleCommand QuadControl::GenerateMotorCommands(float collThrustCmd, V3F momentCmd)
{
// Convert a desired 3-axis moment and collective thrust command to
// individual motor thrust commands
// INPUTS:
// desCollectiveThrust: desired collective thrust [N]
// desMoment: desired rotation moment about each axis [N m]
// OUTPUT:
// set class member variable cmd (class variable for graphing) where
// cmd.desiredThrustsN[0..3]: motor commands, in [N]
// HINTS:
// - you can access parts of desMoment via e.g. desMoment.x
// You'll need the arm length parameter L, and the drag/thrust ratio kappa
////////////////////////////// BEGIN STUDENT CODE ///////////////////////////
const float l = L / sqrt(2.0F);
float Ft = collThrustCmd;
float Fp = momentCmd.x / l;
float Fq = momentCmd.y / l;
float Fr = momentCmd.z / kappa;
// F1, F2, F3, F4 are desired thrust for each motor
float F1 = (Ft + Fp + Fq - Fr)/4;
float F2 = F1 - (Fp - Fr)/2;
float F4 = (Ft - Fp)/2 - F2;
float F3 = Ft - F1 - F2 - F4;
cmd.desiredThrustsN[0] = CONSTRAIN(F1, minMotorThrust, maxMotorThrust); // front left
cmd.desiredThrustsN[1] = CONSTRAIN(F2, minMotorThrust, maxMotorThrust); // front right
cmd.desiredThrustsN[2] = CONSTRAIN(F3, minMotorThrust, maxMotorThrust); // rear left
cmd.desiredThrustsN[3] = CONSTRAIN(F4, minMotorThrust, maxMotorThrust); // rear right
/////////////////////////////// END STUDENT CODE ////////////////////////////
return cmd;
}
V3F QuadControl::BodyRateControl(V3F pqrCmd, V3F pqr)
{
// Calculate a desired 3-axis moment given a desired and current body rate
// INPUTS:
// pqrCmd: desired body rates [rad/s]
// pqr: current or estimated body rates [rad/s]
// OUTPUT:
// return a V3F containing the desired moments for each of the 3 axes
// HINTS:
// - you can use V3Fs just like scalars: V3F a(1,1,1), b(2,3,4), c; c=a-b;
// - you'll need parameters for moments of inertia Ixx, Iyy, Izz
// - you'll also need the gain parameter kpPQR (it's a V3F)
V3F momentCmd;
////////////////////////////// BEGIN STUDENT CODE ///////////////////////////
V3F I = V3F(Ixx, Iyy, Izz);
momentCmd = I * kpPQR * (pqrCmd - pqr);
/////////////////////////////// END STUDENT CODE ////////////////////////////
return momentCmd;
}
// returns a desired roll and pitch rate
V3F QuadControl::RollPitchControl(V3F accelCmd, Quaternion<float> attitude, float collThrustCmd)
{
// Calculate a desired pitch and roll angle rates based on a desired global
// lateral acceleration, the current attitude of the quad, and desired
// collective thrust command
// INPUTS:
// accelCmd: desired acceleration in global XY coordinates [m/s2]
// attitude: current or estimated attitude of the vehicle
// collThrustCmd: desired collective thrust of the quad [N]
// OUTPUT:
// return a V3F containing the desired pitch and roll rates. The Z
// element of the V3F should be left at its default value (0)
// HINTS:
// - we already provide rotation matrix R: to get element R[1,2] (python) use R(1,2) (C++)
// - you'll need the roll/pitch gain kpBank
// - collThrustCmd is a force in Newtons! You'll likely want to convert it to acceleration first
V3F pqrCmd;
Mat3x3F R = attitude.RotationMatrix_IwrtB();
////////////////////////////// BEGIN STUDENT CODE ///////////////////////////
float R11 = R(0, 0);
float R12 = R(0, 1);
float R13 = R(0, 2);
float R21 = R(1, 0);
float R22 = R(1, 1);
float R23 = R(1, 2);
float R33 = R(2, 2);
float c = collThrustCmd / mass;
V3F b = V3F(R(0, 2), R(1, 2), 0.f);
V3F b_c = V3F(accelCmd.x / -c, accelCmd.y / -c, 0.f);
b_c.constrain(-maxTiltAngle, maxTiltAngle);
V3F b_error = b_c - b;
V3F b_c_dot = kpBank * b_error;
pqrCmd.x = (R21 * b_c_dot.x - R11 * b_c_dot.y) / R33;
pqrCmd.y = (R22 * b_c_dot.x - R12 * b_c_dot.y) / R33;
pqrCmd.z = 0.f;
/////////////////////////////// END STUDENT CODE ////////////////////////////
return pqrCmd;
}
float QuadControl::AltitudeControl(float posZCmd, float velZCmd, float posZ, float velZ, Quaternion<float> attitude, float accelZCmd, float dt)
{
// Calculate desired quad thrust based on altitude setpoint, actual altitude,
// vertical velocity setpoint, actual vertical velocity, and a vertical
// acceleration feed-forward command
// INPUTS:
// posZCmd, velZCmd: desired vertical position and velocity in NED [m]
// posZ, velZ: current vertical position and velocity in NED [m]
// accelZCmd: feed-forward vertical acceleration in NED [m/s2]
// dt: the time step of the measurements [seconds]
// OUTPUT:
// return a collective thrust command in [N]
// HINTS:
// - we already provide rotation matrix R: to get element R[1,2] (python) use R(1,2) (C++)
// - you'll need the gain parameters kpPosZ and kpVelZ
// - maxAscentRate and maxDescentRate are maximum vertical speeds. Note they're both >=0!
// - make sure to return a force, not an acceleration
// - remember that for an upright quad in NED, thrust should be HIGHER if the desired Z acceleration is LOWER
Mat3x3F R = attitude.RotationMatrix_IwrtB();
float thrust = 0;
////////////////////////////// BEGIN STUDENT CODE ///////////////////////////
float z_err = posZCmd - posZ;
velZCmd += kpPosZ * z_err;
velZCmd = CONSTRAIN(velZCmd, -maxAscentRate, maxDescentRate);
integratedAltitudeError += z_err * dt;
float z_err_dot = velZCmd - velZ;
accelZCmd += KiPosZ * integratedAltitudeError + kpVelZ * z_err_dot;
float b_z = R(2, 2);
thrust = mass * ((float)CONST_GRAVITY - accelZCmd) / b_z;
/////////////////////////////// END STUDENT CODE ////////////////////////////
return thrust;
}
// returns a desired acceleration in global frame
V3F QuadControl::LateralPositionControl(V3F posCmd, V3F velCmd, V3F pos, V3F vel, V3F accelCmdFF)
{
// Calculate a desired horizontal acceleration based on
// desired lateral position/velocity/acceleration and current pose
// INPUTS:
// posCmd: desired position, in NED [m]
// velCmd: desired velocity, in NED [m/s]
// pos: current position, NED [m]
// vel: current velocity, NED [m/s]
// accelCmdFF: feed-forward acceleration, NED [m/s2]
// OUTPUT:
// return a V3F with desired horizontal accelerations.
// the Z component should be 0
// HINTS:
// - use the gain parameters kpPosXY and kpVelXY
// - make sure you limit the maximum horizontal velocity and acceleration
// to maxSpeedXY and maxAccelXY
// make sure we don't have any incoming z-component
accelCmdFF.z = 0;
velCmd.z = 0;
posCmd.z = pos.z;
// we initialize the returned desired acceleration to the feed-forward value.
// Make sure to _add_, not simply replace, the result of your controller
// to this variable
V3F accelCmd = accelCmdFF;
////////////////////////////// BEGIN STUDENT CODE ///////////////////////////
const V3F error = posCmd - pos;
velCmd += kpPosXY * error;
velCmd.x = CONSTRAIN(velCmd.x, -maxSpeedXY, maxSpeedXY);
velCmd.y = CONSTRAIN(velCmd.y, -maxSpeedXY, maxSpeedXY);
const V3F error_dot = velCmd - vel;
accelCmd += kpVelXY * error_dot;
accelCmd.x = CONSTRAIN(accelCmd.x, -maxAccelXY, maxAccelXY);
accelCmd.y = CONSTRAIN(accelCmd.y, -maxAccelXY, maxAccelXY);
accelCmd.z = 0.0F;
/////////////////////////////// END STUDENT CODE ////////////////////////////
return accelCmd;
}
// returns desired yaw rate
float QuadControl::YawControl(float yawCmd, float yaw)
{
// Calculate a desired yaw rate to control yaw to yawCmd
// INPUTS:
// yawCmd: commanded yaw [rad]
// yaw: current yaw [rad]
// OUTPUT:
// return a desired yaw rate [rad/s]
// HINTS:
// - use fmodf(foo,b) to unwrap a radian angle measure float foo to range [0,b].
// - use the yaw control gain parameter kpYaw
float yawRateCmd=0;
////////////////////////////// BEGIN STUDENT CODE ///////////////////////////
float error = yawCmd - yaw;
error = fmodf(error, 2.*F_PI);
yawRateCmd = kpYaw * error;
/////////////////////////////// END STUDENT CODE ////////////////////////////
return yawRateCmd;
}
VehicleCommand QuadControl::RunControl(float dt, float simTime)
{
curTrajPoint = GetNextTrajectoryPoint(simTime);
float collThrustCmd = AltitudeControl(curTrajPoint.position.z, curTrajPoint.velocity.z, estPos.z, estVel.z, estAtt, curTrajPoint.accel.z, dt);
// reserve some thrust margin for angle control
float thrustMargin = .1f*(maxMotorThrust - minMotorThrust);
collThrustCmd = CONSTRAIN(collThrustCmd, (minMotorThrust+ thrustMargin)*4.f, (maxMotorThrust-thrustMargin)*4.f);
V3F desAcc = LateralPositionControl(curTrajPoint.position, curTrajPoint.velocity, estPos, estVel, curTrajPoint.accel);
V3F desOmega = RollPitchControl(desAcc, estAtt, collThrustCmd);
desOmega.z = YawControl(curTrajPoint.attitude.Yaw(), estAtt.Yaw());
V3F desMoment = BodyRateControl(desOmega, estOmega);
return GenerateMotorCommands(collThrustCmd, desMoment);
}