Subversion Repositories FlightCtrl

Rev

Rev 2109 | Rev 2135 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2109 Rev 2132
Line 43... Line 43...
43
        target[YAW] = attitude[YAW];
43
        target[YAW] = attitude[YAW];
44
}
44
}
Line 45... Line 45...
45
 
45
 
46
void flight_updateFlightParametersToFlightMode(void) {
46
void flight_updateFlightParametersToFlightMode(void) {
47
        debugOut.analog[16] = currentFlightMode;
47
        debugOut.analog[16] = currentFlightMode;
48
        reverse[PITCH] = staticParams.controlServosReverse & CONTROL_SERVO_REVERSE_ELEVATOR;
48
        reverse[PITCH] = staticParams.servosReverse & CONTROL_SERVO_REVERSE_ELEVATOR;
49
        reverse[ROLL] = staticParams.controlServosReverse & CONTROL_SERVO_REVERSE_AILERONS;
49
        reverse[ROLL] = staticParams.servosReverse & CONTROL_SERVO_REVERSE_AILERONS;
Line 50... Line 50...
50
        reverse[YAW] = staticParams.controlServosReverse & CONTROL_SERVO_REVERSE_RUDDER;
50
        reverse[YAW] = staticParams.servosReverse & CONTROL_SERVO_REVERSE_RUDDER;
51
 
51
 
52
        // At a switch to angles, we want to kill errors first.
52
        // At a switch to angles, we want to kill errors first.
53
        // This should be triggered only once per mode change!
53
        // This should be triggered only once per mode change!
Line 62... Line 62...
62
        }
62
        }
63
}
63
}
Line 64... Line 64...
64
 
64
 
65
// Normal at airspeed = 10.
65
// Normal at airspeed = 10.
66
uint8_t calcAirspeedPID(uint8_t pid) {
66
uint8_t calcAirspeedPID(uint8_t pid) {
67
        if (staticParams.bitConfig & CFG_USE_AIRSPEED_PID) {
67
        //if (!(staticParams.bitConfig & CFG_USE_AIRSPEED_PID)) {
68
                return pid;
68
                return pid;
69
        }
-
 
70
 
-
 
71
        uint16_t result = (pid * 10) / airspeedVelocity;
-
 
72
 
-
 
73
        if (result > 240 || airspeedVelocity == 0) {
-
 
74
                result = 240;
69
        //}
75
        }
-
 
76
 
-
 
77
        return result;
-
 
Line 78... Line 70...
78
}
70
                        }
79
 
71
 
80
void setAirspeedPIDs(void) {
72
void setAirspeedPIDs(void) {
81
        for (uint8_t axis = 0; axis<3; axis++) {
73
        for (uint8_t axis = 0; axis<3; axis++) {
82
                airspeedPID[axis].P = calcAirspeedPID(dynamicParams.gyroPID[axis].P);
74
                airspeedPID[axis].P = calcAirspeedPID(dynamicParams.gyroPID[axis].P);
83
                airspeedPID[axis].I = calcAirspeedPID(dynamicParams.gyroPID[axis].I); // Should this be???
75
                airspeedPID[axis].I = calcAirspeedPID(dynamicParams.gyroPID[axis].I); // Should this be???
84
                airspeedPID[axis].D = dynamicParams.gyroPID[axis].D;
76
                airspeedPID[axis].D = dynamicParams.gyroPID[axis].D;
Line -... Line 77...
-
 
77
        }
-
 
78
}
-
 
79
 
-
 
80
#define LOG_STICK_SCALE 8
-
 
81
#define LOG_P_SCALE 6
85
        }
82
#define LOG_I_SCALE 10
86
}
83
#define LOG_D_SCALE 6
87
 
84
 
88
/************************************************************************/
85
/************************************************************************/
89
/*  Main Flight Control                                                 */
86
/*  Main Flight Control                                                 */
Line 105... Line 102...
105
        setAirspeedPIDs();
102
        setAirspeedPIDs();
Line 106... Line 103...
106
 
103
 
Line 107... Line 104...
107
        term[CONTROL_THROTTLE] = controls[CONTROL_THROTTLE];
104
        term[CONTROL_THROTTLE] = controls[CONTROL_THROTTLE];
-
 
105
 
-
 
106
        // These params are just left the same in all modes. In MANUAL and RATE the results are ignored anyway.
108
 
107
        int32_t tmp;
-
 
108
 
-
 
109
        tmp = ((int32_t)controls[CONTROL_ELEVATOR] * staticParams.stickIElevator) >> LOG_STICK_SCALE;
109
        // These params are just left the same in all modes. In MANUAL and RATE the results are ignored anyway.
110
        if (reverse[PITCH]) target[PITCH] += tmp; else target[PITCH] -= tmp;
-
 
111
 
-
 
112
        tmp = ((int32_t)controls[CONTROL_AILERONS] * staticParams.stickIAilerons) >> LOG_STICK_SCALE;
110
        target[PITCH] += (controls[CONTROL_ELEVATOR] * staticParams.stickIElevator) >> 7;
113
        if (reverse[ROLL]) target[ROLL] += tmp; else target[ROLL] -= tmp;
-
 
114
 
Line 111... Line 115...
111
        target[ROLL] += (controls[CONTROL_AILERONS] * staticParams.stickIAilerons) >> 7;
115
        tmp = ((int32_t)controls[CONTROL_RUDDER] * staticParams.stickIRudder) >> LOG_STICK_SCALE;
112
        target[YAW] += (controls[CONTROL_RUDDER] * staticParams.stickIRudder) >> 7;
116
        if (reverse[YAW]) target[YAW] += tmp; else target[YAW] -= tmp;
113
 
117
 
114
        for (axis = PITCH; axis <= YAW; axis++) {
118
        for (axis = PITCH; axis <= YAW; axis++) {
115
                if (target[axis] > OVER180) {
119
                if (target[axis] > OVER180) {
116
                        target[axis] -= OVER360;
120
                        target[axis] -= OVER360;
Line 117... Line -...
117
                } else if (target[axis] <= -OVER180) {
-
 
118
                        target[axis] += OVER360;
121
                } else if (target[axis] <= -OVER180) {
119
                }
122
                        target[axis] += OVER360;
-
 
123
                }
120
 
124
 
Line -... Line 125...
-
 
125
                error[axis] = attitude[axis] - target[axis];
-
 
126
 
-
 
127
#define ROTATETARGET 1
-
 
128
// #define TRUNCATEERROR 1
-
 
129
 
-
 
130
#ifdef ROTATETARGET
-
 
131
                if(abs(error[axis]) > OVER180) {
-
 
132
                  // The shortest way from attitude to target crosses -180.
-
 
133
                  // Well there are 2 possibilities: A is >0 and T is < 0, that makes E a (too) large positive number. It should be wrapped to negative.
-
 
134
                  // Or A is <0 and T is >0, that makes E a (too) large negative number. It should be wrapped to positive.
-
 
135
                  if (error[axis] > 0) {
-
 
136
                    if (error[axis] < OVER360 - maxError[axis]) {
-
 
137
                      // too much err.
-
 
138
                      error[axis] = -maxError[axis];
-
 
139
                      target[axis] = attitude[axis] + maxError[axis];
-
 
140
                      if (target[axis] > OVER180) target[axis] -= OVER360;
-
 
141
                    } else {
-
 
142
                      // Normal case, we just need to correct for the wrap. Error will be negative.
-
 
143
                      error[axis] -= OVER360;
-
 
144
                    }
-
 
145
                  } else {
-
 
146
            if (error[axis] > maxError[axis] - OVER360) {
-
 
147
              // too much err.
-
 
148
              error[axis] = maxError[axis];
-
 
149
              target[axis] = attitude[axis] - maxError[axis];
-
 
150
              if (target[axis] < -OVER180) target[axis] += OVER360;
-
 
151
            } else {
-
 
152
              // Normal case, we just need to correct for the wrap. Error will be negative.
-
 
153
              error[axis] += OVER360;
-
 
154
            }
-
 
155
                  }
-
 
156
                } else {
-
 
157
                  // Simple case, linear range.
-
 
158
                if (error[axis] > maxError[axis]) {
-
 
159
                  error[axis] = maxError[axis];
-
 
160
                  target[axis] = attitude[axis] - maxError[axis];
-
 
161
                } else if (error[axis] < -maxError[axis]) {
-
 
162
              error[axis] = -maxError[axis];
121
                if (reverse[axis])
163
              target[axis] = attitude[axis] + maxError[axis];
122
                        error[axis] = attitude[axis] + target[axis];
164
            }
123
                else
165
                }
124
                        error[axis] = attitude[axis] - target[axis];
166
#endif
-
 
167
#ifdef TUNCATEERROR
-
 
168
                if (error[axis] > maxError[axis]) {
125
 
169
                  error[axis] = maxError[axis];
126
                if (error[axis] > maxError[axis]) {
-
 
127
                        error[axis] = maxError[axis];
-
 
128
                } else if (error[axis] < -maxError[axis]) {
-
 
129
                        error[axis] =- maxError[axis];
170
                } else if (error[axis] < -maxError[axis]) {
Line 130... Line 171...
130
                }
171
                  error[axis] = -maxError[axis];
131
 
172
                } else {
132
#define LOG_P_SCALE 6
173
                        // update I parts here for angles mode. I parts in rate mode is something different.
133
#define LOG_I_SCALE 6
174
                }
Line 141... Line 182...
141
                                == FLIGHT_MODE_RATE) {
182
                                == FLIGHT_MODE_RATE) {
142
                        PDPart[axis] = (((int32_t) gyro_PID[axis]
183
                        PDPart[axis] = (((int32_t) gyro_PID[axis]
143
                                        * (int16_t) airspeedPID[axis].P) >> LOG_P_SCALE)
184
                                        * (int16_t) airspeedPID[axis].P) >> LOG_P_SCALE)
144
                                        + ((gyroD[axis] * (int16_t) airspeedPID[axis].D)
185
                                        + ((gyroD[axis] * (int16_t) airspeedPID[axis].D)
145
                                                        >> LOG_D_SCALE);
186
                                                        >> LOG_D_SCALE);
146
                        if (reverse[axis])
-
 
147
                                PDPart[axis] = -PDPart[axis];
-
 
148
                } else {
187
                } else {
149
                        PDPart[axis] = 0;
188
                        PDPart[axis] = 0;
150
                }
189
                }
Line 151... Line 190...
151
 
190
 
152
                if (currentFlightMode == FLIGHT_MODE_ANGLES) {
-
 
153
                        int16_t anglePart = (int32_t)(
191
                if (currentFlightMode == FLIGHT_MODE_ANGLES) {
154
                                        error[axis] * (int32_t) airspeedPID[axis].I)
-
 
155
                                        >> LOG_I_SCALE;
-
 
156
                        if (reverse[axis])
192
                        int16_t anglePart = (int32_t)(error[axis] * (int32_t) airspeedPID[axis].I) >> LOG_I_SCALE;
157
                                PDPart[axis] += anglePart;
-
 
158
                        else
-
 
159
                                PDPart[axis] -= anglePart;
193
                        PDPart[axis] += anglePart;
160
                }
-
 
161
                // Add I parts here... these are integrated errors.
-
 
Line -... Line 194...
-
 
194
                }
-
 
195
 
-
 
196
                // Add I parts here... these are integrated errors.
-
 
197
                if (reverse[axis])
162
                // When an error wraps, actually its I part should be negated or something...
198
                  term[axis] = controls[axis] - PDPart[axis]; // + IPart[axis];
163
 
199
                else
Line 164... Line 200...
164
                term[axis] = controls[axis] + PDPart[axis] + IPart[axis];
200
                  term[axis] = controls[axis] + PDPart[axis]; // + IPart[axis];
165
        }
201
        }
166
 
202
 
167
        debugOut.analog[12] = term[PITCH];
203
        debugOut.analog[12] = term[PITCH];
Line 168... Line 204...
168
        debugOut.analog[13] = term[ROLL];
204
        debugOut.analog[13] = term[ROLL];
169
        debugOut.analog[14] = term[YAW];
205
        debugOut.analog[14] = term[YAW];
170
        debugOut.analog[15] = term[THROTTLE];
206
        debugOut.analog[15] = term[THROTTLE];
171
 
207
 
172
        for (uint8_t i = 0; i < NUM_CONTROL_SERVOS; i++) {
208
        for (uint8_t i = 0; i < NUM_CONTROL_SERVOS; i++) {
173
                int16_t tmp;
209
                int16_t tmp;
174
                if (servoTestActive) {
210
                if (servoTestActive) {
175
                        controlServos[i] = ((int16_t) servoTest[i] - 128) * 4;
211
                        controlServos[i] = ((int16_t) servoTest[i] - 128) * 8;
176
                } else {
212
                } else {