Skip to content

Commit 4e94fd0

Browse files
+ added alternative PID controller from http://www.multiwii.com/forum/viewtopic.php?f=8&t=3671
+ this is a per-profile setting, and PIDs CHANGE from default multiwii ones. check the above forum post for PID examples. set pid_controller = 0 for default multiwii, or 1 for new one. = went back to clearing clibuffer after each command git-svn-id: https://afrodevices.googlecode.com/svn/trunk/baseflight@341 7c89a4a9-59b9-e629-4cfe-3a2d53b20e61
1 parent 415600b commit 4e94fd0

File tree

6 files changed

+163
-60
lines changed

6 files changed

+163
-60
lines changed

baseflight.uvproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
<AdsLsun>1</AdsLsun>
198198
<AdsLven>1</AdsLven>
199199
<AdsLsxf>1</AdsLsxf>
200-
<RvctClst>0</RvctClst>
200+
<RvctClst>1</RvctClst>
201201
<GenPPlst>0</GenPPlst>
202202
<AdsCpuType>"Cortex-M3"</AdsCpuType>
203203
<RvctDeviceName></RvctDeviceName>
@@ -990,7 +990,7 @@
990990
<ldmm>1</ldmm>
991991
<ldXref>1</ldXref>
992992
<BigEnd>0</BigEnd>
993-
<AdsALst>0</AdsALst>
993+
<AdsALst>1</AdsALst>
994994
<AdsACrf>1</AdsACrf>
995995
<AdsANop>0</AdsANop>
996996
<AdsANot>0</AdsANot>
@@ -1003,7 +1003,7 @@
10031003
<AdsLsun>1</AdsLsun>
10041004
<AdsLven>1</AdsLven>
10051005
<AdsLsxf>1</AdsLsxf>
1006-
<RvctClst>0</RvctClst>
1006+
<RvctClst>1</RvctClst>
10071007
<GenPPlst>0</GenPPlst>
10081008
<AdsCpuType>"Cortex-M3"</AdsCpuType>
10091009
<RvctDeviceName></RvctDeviceName>

src/board.h

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ typedef void (* sensorReadFuncPtr)(int16_t *data); // sensor read and a
7575
typedef void (* baroCalculateFuncPtr)(int32_t *pressure, int32_t *temperature); // baro calculation (filled params are pressure and temperature)
7676
typedef void (* uartReceiveCallbackPtr)(uint16_t data); // used by uart2 driver to return frames to app
7777
typedef uint16_t (* rcReadRawDataPtr)(uint8_t chan); // used by receiver driver to return channel data
78+
typedef void (* pidControllerFuncPtr)(void); // pid controller function prototype
7879

7980
typedef struct sensor_t
8081
{

src/cli.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ const clivalue_t valueTable[] = {
133133
{ "gyro_cmpf_factor", VAR_UINT16, &mcfg.gyro_cmpf_factor, 100, 1000 },
134134
{ "gyro_cmpfm_factor", VAR_UINT16, &mcfg.gyro_cmpfm_factor, 100, 1000 },
135135
{ "gps_type", VAR_UINT8, &mcfg.gps_type, 0, 3 },
136+
{ "pid_controller", VAR_UINT8, &cfg.pidController, 0, 1 },
136137
{ "deadband", VAR_UINT8, &cfg.deadband, 0, 32 },
137138
{ "yawdeadband", VAR_UINT8, &cfg.yawdeadband, 0, 100 },
138139
{ "alt_hold_throttle_neutral", VAR_UINT8, &cfg.alt_hold_throttle_neutral, 1, 250 },
@@ -1013,7 +1014,7 @@ void cliProcess(void)
10131014
else
10141015
uartPrint("ERR: Unknown command, try 'help'");
10151016

1016-
*cliBuffer = '\0';
1017+
memset(cliBuffer, 0, sizeof(cliBuffer));
10171018
bufferIndex = 0;
10181019

10191020
// 'exit' will reset this flag, so we don't need to print prompt again

src/config.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ master_t mcfg; // master config struct with data independent from profiles
1313
config_t cfg; // profile config struct
1414
const char rcChannelLetters[] = "AERT1234";
1515

16-
static uint8_t EEPROM_CONF_VERSION = 47;
16+
static uint8_t EEPROM_CONF_VERSION = 48;
1717
static uint32_t enabledSensors = 0;
1818
static void resetConf(void);
1919

@@ -84,6 +84,7 @@ void readEEPROM(void)
8484
}
8585

8686
cfg.tri_yaw_middle = constrain(cfg.tri_yaw_middle, cfg.tri_yaw_min, cfg.tri_yaw_max); //REAR
87+
setPIDController(cfg.pidController);
8788
}
8889

8990
void writeEEPROM(uint8_t b, uint8_t updateProfile)
@@ -202,6 +203,7 @@ static void resetConf(void)
202203
mcfg.serial_baudrate = 115200;
203204
mcfg.looptime = 3500;
204205

206+
cfg.pidController = 0;
205207
cfg.P8[ROLL] = 40;
206208
cfg.I8[ROLL] = 30;
207209
cfg.D8[ROLL] = 23;

src/mw.c

+152-55
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "board.h"
22
#include "mw.h"
33

4-
// October 2012 V2.1-dev
4+
// June 2013 V2.2-dev
55

66
flags_t f;
77
int16_t debug[4];
@@ -24,6 +24,10 @@ int16_t lookupThrottleRC[11]; // lookup table for expo & mid THROTTLE
2424
uint16_t rssi; // range: [0;1023]
2525
rcReadRawDataPtr rcReadRawFunc = NULL; // receive data from default (pwm/ppm) or additional (spek/sbus/?? receiver drivers)
2626

27+
static void pidMultiWii(void);
28+
static void pidRewrite(void);
29+
pidControllerFuncPtr pid_controller = pidMultiWii; // which pid controller are we using, defaultMultiWii
30+
2731
uint8_t dynP8[3], dynI8[3], dynD8[3];
2832
uint8_t rcOptions[CHECKBOXITEMS];
2933

@@ -132,6 +136,7 @@ void annexCode(void)
132136
prop1 = 100 - (uint16_t) cfg.yawRate * tmp / 500;
133137
}
134138
dynP8[axis] = (uint16_t) cfg.P8[axis] * prop1 / 100;
139+
dynI8[axis] = (uint16_t) cfg.I8[axis] * prop1 / 100;
135140
dynD8[axis] = (uint16_t) cfg.D8[axis] * prop1 / 100;
136141
if (rcData[axis] < mcfg.midrc)
137142
rcCommand[axis] = -rcCommand[axis];
@@ -273,25 +278,160 @@ static void mwVario(void)
273278

274279
}
275280

276-
void loop(void)
281+
static int32_t errorGyroI[3] = { 0, 0, 0 };
282+
static int32_t errorAngleI[2] = { 0, 0 };
283+
284+
static void pidMultiWii(void)
277285
{
278-
static uint8_t rcDelayCommand; // this indicates the number of time (multiple of RC measurement at 50Hz) the sticks must be maintained to run or switch off motors
279-
static uint8_t rcSticks; // this hold sticks position for command combos
280-
uint8_t stTmp = 0;
281-
uint8_t axis, i;
286+
int axis, prop;
282287
int16_t error, errorAngle;
283288
int16_t PTerm, ITerm, PTermACC, ITermACC = 0, PTermGYRO = 0, ITermGYRO = 0, DTerm;
284-
static int16_t errorGyroI[3] = { 0, 0, 0 };
285-
static int16_t errorAngleI[2] = { 0, 0 };
286-
int16_t delta;
287289
static int16_t lastGyro[3] = { 0, 0, 0 };
288290
static int16_t delta1[3], delta2[3];
289291
int16_t deltaSum;
292+
int16_t delta;
293+
294+
// **** PITCH & ROLL & YAW PID ****
295+
prop = max(abs(rcCommand[PITCH]), abs(rcCommand[ROLL])); // range [0;500]
296+
for (axis = 0; axis < 3; axis++) {
297+
if ((f.ANGLE_MODE || f.HORIZON_MODE) && axis < 2) { // MODE relying on ACC
298+
// 50 degrees max inclination
299+
errorAngle = constrain(2 * rcCommand[axis] + GPS_angle[axis], -500, +500) - angle[axis] + cfg.angleTrim[axis];
300+
PTermACC = (int32_t)errorAngle * cfg.P8[PIDLEVEL] / 100; // 32 bits is needed for calculation: errorAngle*P8[PIDLEVEL] could exceed 32768 16 bits is ok for result
301+
PTermACC = constrain(PTermACC, -cfg.D8[PIDLEVEL] * 5, +cfg.D8[PIDLEVEL] * 5);
302+
303+
errorAngleI[axis] = constrain(errorAngleI[axis] + errorAngle, -10000, +10000); // WindUp
304+
ITermACC = ((int32_t)errorAngleI[axis] * cfg.I8[PIDLEVEL]) >> 12;
305+
}
306+
if (!f.ANGLE_MODE || f.HORIZON_MODE || axis == 2) { // MODE relying on GYRO or YAW axis
307+
error = (int32_t)rcCommand[axis] * 10 * 8 / cfg.P8[axis];
308+
error -= gyroData[axis];
309+
310+
PTermGYRO = rcCommand[axis];
311+
312+
errorGyroI[axis] = constrain(errorGyroI[axis] + error, -16000, +16000); // WindUp
313+
if (abs(gyroData[axis]) > 640)
314+
errorGyroI[axis] = 0;
315+
ITermGYRO = (errorGyroI[axis] / 125 * cfg.I8[axis]) >> 6;
316+
}
317+
if (f.HORIZON_MODE && axis < 2) {
318+
PTerm = ((int32_t)PTermACC * (500 - prop) + (int32_t)PTermGYRO * prop) / 500;
319+
ITerm = ((int32_t)ITermACC * (500 - prop) + (int32_t)ITermGYRO * prop) / 500;
320+
} else {
321+
if (f.ANGLE_MODE && axis < 2) {
322+
PTerm = PTermACC;
323+
ITerm = ITermACC;
324+
} else {
325+
PTerm = PTermGYRO;
326+
ITerm = ITermGYRO;
327+
}
328+
}
329+
330+
PTerm -= (int32_t)gyroData[axis] * dynP8[axis] / 10 / 8; // 32 bits is needed for calculation
331+
delta = gyroData[axis] - lastGyro[axis]; // 16 bits is ok here, the dif between 2 consecutive gyro reads is limited to 800
332+
lastGyro[axis] = gyroData[axis];
333+
deltaSum = delta1[axis] + delta2[axis] + delta;
334+
delta2[axis] = delta1[axis];
335+
delta1[axis] = delta;
336+
DTerm = ((int32_t)deltaSum * dynD8[axis]) >> 5; // 32 bits is needed for calculation
337+
axisPID[axis] = PTerm + ITerm - DTerm;
338+
}
339+
}
340+
341+
#define GYRO_I_MAX 256
342+
343+
static void pidRewrite(void)
344+
{
345+
int16_t errorAngle;
346+
int axis;
347+
int16_t delta, deltaSum;
348+
static int16_t delta1[3], delta2[3];
349+
int16_t PTerm, ITerm, DTerm;
350+
static int16_t lastError[3] = { 0, 0, 0 };
351+
int16_t AngleRateTmp, RateError;
352+
353+
// ----------PID controller----------
354+
for (axis = 0; axis < 3; axis++) {
355+
// -----Get the desired angle rate depending on flight mode
356+
if ((f.ANGLE_MODE || f.HORIZON_MODE) && axis < 2 ) { // MODE relying on ACC
357+
// calculate error and limit the angle to 50 degrees max inclination
358+
errorAngle = constrain((rcCommand[axis] << 1) + GPS_angle[axis], -500, +500) - angle[axis] + cfg.angleTrim[axis]; // 16 bits is ok here
359+
}
360+
if (axis == 2) { // YAW is always gyro-controlled (MAG correction is applied to rcCommand)
361+
AngleRateTmp = (((int32_t) (cfg.yawRate + 27) * rcCommand[2]) >> 5);
362+
} else {
363+
if (!f.ANGLE_MODE) { //control is GYRO based (ACRO and HORIZON - direct sticks control is applied to rate PID
364+
AngleRateTmp = ((int32_t) (cfg.rollPitchRate + 27) * rcCommand[axis]) >> 4;
365+
if (f.HORIZON_MODE) {
366+
// mix up angle error to desired AngleRateTmp to add a little auto-level feel
367+
AngleRateTmp += ((int32_t) errorAngle * cfg.I8[PIDLEVEL]) >> 8;
368+
}
369+
} else { // it's the ANGLE mode - control is angle based, so control loop is needed
370+
AngleRateTmp = ((int32_t) errorAngle * cfg.P8[PIDLEVEL]) >> 4;
371+
}
372+
}
373+
374+
// --------low-level gyro-based PID. ----------
375+
// Used in stand-alone mode for ACRO, controlled by higher level regulators in other modes
376+
// -----calculate scaled error.AngleRates
377+
// multiplication of rcCommand corresponds to changing the sticks scaling here
378+
RateError = AngleRateTmp - gyroData[axis];
379+
380+
// -----calculate P component
381+
PTerm = ((int32_t)RateError * cfg.P8[axis]) >> 7;
382+
// -----calculate I component
383+
// there should be no division before accumulating the error to integrator, because the precision would be reduced.
384+
// Precision is critical, as I prevents from long-time drift. Thus, 32 bits integrator is used.
385+
// Time correction (to avoid different I scaling for different builds based on average cycle time)
386+
// is normalized to cycle time = 2048.
387+
errorGyroI[axis] = errorGyroI[axis] + (((int32_t)RateError * cycleTime) >> 11) * cfg.I8[axis];
388+
389+
// limit maximum integrator value to prevent WindUp - accumulating extreme values when system is saturated.
390+
// I coefficient (I8) moved before integration to make limiting independent from PID settings
391+
errorGyroI[axis] = constrain(errorGyroI[axis], (int32_t)-GYRO_I_MAX << 13, (int32_t)+GYRO_I_MAX << 13);
392+
ITerm = errorGyroI[axis] >> 13;
393+
394+
//-----calculate D-term
395+
delta = RateError - lastError[axis]; // 16 bits is ok here, the dif between 2 consecutive gyro reads is limited to 800
396+
lastError[axis] = RateError;
397+
398+
// Correct difference by cycle time. Cycle time is jittery (can be different 2 times), so calculated difference
399+
// would be scaled by different dt each time. Division by dT fixes that.
400+
delta = ((int32_t) delta * ((uint16_t)0xFFFF / (cycleTime >> 4))) >> 6;
401+
// add moving average here to reduce noise
402+
deltaSum = delta1[axis] + delta2[axis] + delta;
403+
delta2[axis] = delta1[axis];
404+
delta1[axis] = delta;
405+
DTerm = (deltaSum * cfg.D8[axis]) >> 8;
406+
407+
// -----calculate total PID output
408+
axisPID[axis] = PTerm + ITerm + DTerm;
409+
}
410+
}
411+
412+
void setPIDController(int type)
413+
{
414+
switch (type) {
415+
case 0:
416+
default:
417+
pid_controller = pidMultiWii;
418+
break;
419+
case 1:
420+
pid_controller = pidRewrite;
421+
break;
422+
}
423+
}
424+
425+
void loop(void)
426+
{
427+
static uint8_t rcDelayCommand; // this indicates the number of time (multiple of RC measurement at 50Hz) the sticks must be maintained to run or switch off motors
428+
static uint8_t rcSticks; // this hold sticks position for command combos
429+
uint8_t stTmp = 0;
430+
int i;
290431
static uint32_t rcTime = 0;
291432
static int16_t initialThrottleHold;
292433
static uint32_t loopTime;
293434
uint16_t auxState = 0;
294-
int16_t prop;
295435
static uint8_t GPSNavReset = 1;
296436

297437
// this will return false if spektrum is disabled. shrug.
@@ -703,51 +843,8 @@ void loop(void)
703843
}
704844
}
705845

706-
// **** PITCH & ROLL & YAW PID ****
707-
prop = max(abs(rcCommand[PITCH]), abs(rcCommand[ROLL])); // range [0;500]
708-
for (axis = 0; axis < 3; axis++) {
709-
if ((f.ANGLE_MODE || f.HORIZON_MODE) && axis < 2) { // MODE relying on ACC
710-
// 50 degrees max inclination
711-
errorAngle = constrain(2 * rcCommand[axis] + GPS_angle[axis], -500, +500) - angle[axis] + cfg.angleTrim[axis];
712-
PTermACC = (int32_t)errorAngle * cfg.P8[PIDLEVEL] / 100; // 32 bits is needed for calculation: errorAngle*P8[PIDLEVEL] could exceed 32768 16 bits is ok for result
713-
PTermACC = constrain(PTermACC, -cfg.D8[PIDLEVEL] * 5, +cfg.D8[PIDLEVEL] * 5);
714-
715-
errorAngleI[axis] = constrain(errorAngleI[axis] + errorAngle, -10000, +10000); // WindUp
716-
ITermACC = ((int32_t)errorAngleI[axis] * cfg.I8[PIDLEVEL]) >> 12;
717-
}
718-
if (!f.ANGLE_MODE || f.HORIZON_MODE || axis == 2) { // MODE relying on GYRO or YAW axis
719-
error = (int32_t)rcCommand[axis] * 10 * 8 / cfg.P8[axis];
720-
error -= gyroData[axis];
721-
722-
PTermGYRO = rcCommand[axis];
723-
724-
errorGyroI[axis] = constrain(errorGyroI[axis] + error, -16000, +16000); // WindUp
725-
if (abs(gyroData[axis]) > 640)
726-
errorGyroI[axis] = 0;
727-
ITermGYRO = (errorGyroI[axis] / 125 * cfg.I8[axis]) >> 6;
728-
}
729-
if (f.HORIZON_MODE && axis < 2) {
730-
PTerm = ((int32_t)PTermACC * (500 - prop) + (int32_t)PTermGYRO * prop) / 500;
731-
ITerm = ((int32_t)ITermACC * (500 - prop) + (int32_t)ITermGYRO * prop) / 500;
732-
} else {
733-
if (f.ANGLE_MODE && axis < 2) {
734-
PTerm = PTermACC;
735-
ITerm = ITermACC;
736-
} else {
737-
PTerm = PTermGYRO;
738-
ITerm = ITermGYRO;
739-
}
740-
}
741-
742-
PTerm -= (int32_t)gyroData[axis] * dynP8[axis] / 10 / 8; // 32 bits is needed for calculation
743-
delta = gyroData[axis] - lastGyro[axis]; // 16 bits is ok here, the dif between 2 consecutive gyro reads is limited to 800
744-
lastGyro[axis] = gyroData[axis];
745-
deltaSum = delta1[axis] + delta2[axis] + delta;
746-
delta2[axis] = delta1[axis];
747-
delta1[axis] = delta;
748-
DTerm = ((int32_t)deltaSum * dynD8[axis]) >> 5; // 32 bits is needed for calculation
749-
axisPID[axis] = PTerm + ITerm - DTerm;
750-
}
846+
// PID - note this is function pointer set by setPIDController()
847+
pid_controller();
751848

752849
mixTable();
753850
writeServos();

src/mw.h

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ enum {
145145
};
146146

147147
typedef struct config_t {
148+
uint8_t pidController; // 0 = multiwii original, 1 = rewrite from http://www.multiwii.com/forum/viewtopic.php?f=8&t=3671
148149
uint8_t P8[PIDITEMS];
149150
uint8_t I8[PIDITEMS];
150151
uint8_t D8[PIDITEMS];
@@ -371,6 +372,7 @@ extern sensor_t gyro;
371372
extern baro_t baro;
372373

373374
// main
375+
void setPIDController(int type);
374376
void loop(void);
375377

376378
// IMU

0 commit comments

Comments
 (0)