Subversion Repositories FlightCtrl

Rev

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

Rev 2019 Rev 2026
Line 53... Line 53...
53
#include <avr/io.h>
53
#include <avr/io.h>
54
#include "eeprom.h"
54
#include "eeprom.h"
55
#include "flight.h"
55
#include "flight.h"
56
#include "output.h"
56
#include "output.h"
Line 57... Line -...
57
 
-
 
58
// Only for debug. Remove.
-
 
59
//#include "analog.h"
-
 
60
//#include "rc.h"
-
 
61
 
57
 
62
// Necessary for external control and motor test
58
// Necessary for external control and motor test
63
#include "uart0.h"
59
#include "uart0.h"
64
#include "twimaster.h"
60
#include "twimaster.h"
65
#include "attitude.h"
61
#include "attitude.h"
Line 180... Line 176...
180
  /* This part could be abstracted, as having yet another control input   */
176
  /* This part could be abstracted, as having yet another control input   */
181
  /* to the control mixer: An emergency autopilot control.                */
177
  /* to the control mixer: An emergency autopilot control.                */
182
  /************************************************************************/
178
  /************************************************************************/
Line 183... Line 179...
183
 
179
 
184
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
180
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
185
    beepRCAlarm();
181
    if (controlMixer_didReceiveSignal) beepRCAlarm();
186
    if (emergencyFlightTime) {
182
    if (emergencyFlightTime) {
187
      // continue emergency flight
183
      // continue emergency flight
188
      emergencyFlightTime--;
184
      emergencyFlightTime--;
189
      if (isFlying > 256) {
185
      if (isFlying > 256) {
Line 218... Line 214...
218
     * Probably to avoid integration effects that will cause the copter to spin
214
     * Probably to avoid integration effects that will cause the copter to spin
219
     * or flip when taking off.
215
     * or flip when taking off.
220
     */
216
     */
221
      if (isFlying < 256) {
217
      if (isFlying < 256) {
222
        IPart[PITCH] = IPart[ROLL] = 0;
218
        IPart[PITCH] = IPart[ROLL] = 0;
223
        // TODO: Don't stomp on other modules' variables!!!
-
 
224
        // controlYaw = 0;
219
        PDPartYaw = 0;
225
        PDPartYaw = 0; // instead.
-
 
226
        if (isFlying == 250) {
220
        if (isFlying == 250) {
227
          // HC_setGround();
221
          // HC_setGround();
228
          updateCompassCourse = 1;
222
          updateCompassCourse = 1;
229
          yawAngleDiff = 0;
223
          yawAngleDiff = 0;
230
        }
224
        }
Line 283... Line 277...
283
  /* and angular velocity (gyro signal)                                   */
277
  /* and angular velocity (gyro signal)                                   */
284
  /************************************************************************/
278
  /************************************************************************/
285
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
279
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
286
  for (axis = PITCH; axis <= ROLL; axis++) {
280
  for (axis = PITCH; axis <= ROLL; axis++) {
287
    PDPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral
281
    PDPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral
288
   
-
 
289
    /*
-
 
290
     * Now blend in the D-part - proportional to the Differential of the integral = the rate.
-
 
291
     * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING
282
    PDPart[axis] += ((int32_t) rate_PID[axis] * gyroPFactor / (256L / CONTROL_SCALING));
292
     * where pfactor is in [0..1].
-
 
293
     */
-
 
294
    PDPart[axis] += (int32_t) ((int32_t) rate_PID[axis] * gyroPFactor / (256L / CONTROL_SCALING)) + (differential[axis] * (int16_t) dynamicParams.gyroD) / 16;
283
    PDPart[axis] += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16;
295
    CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT);
284
    CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT);
296
  }
285
  }
Line 297... Line 286...
297
 
286
 
298
  PDPartYaw = (int32_t) (yawAngleDiff * yawIFactor) / (2 * (44000 / CONTROL_SCALING));
287
  PDPartYaw = (int32_t) (yawAngleDiff * yawIFactor) / (2 * (44000 / CONTROL_SCALING));
Line 332... Line 321...
332
      yawTerm = -throttleTerm / 2;
321
      yawTerm = -throttleTerm / 2;
333
    } else if (yawTerm > throttleTerm / 2) {
322
    } else if (yawTerm > throttleTerm / 2) {
334
      debugOut.digital[0] |= DEBUG_CLIP;
323
      debugOut.digital[0] |= DEBUG_CLIP;
335
      yawTerm = throttleTerm / 2;
324
      yawTerm = throttleTerm / 2;
336
    }
325
    }
337
    //CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2));
-
 
338
  } else {
326
  } else {
339
    if (yawTerm < -MIN_YAWGAS / 2) {
327
    if (yawTerm < -MIN_YAWGAS / 2) {
340
      debugOut.digital[0] |= DEBUG_CLIP;
328
      debugOut.digital[0] |= DEBUG_CLIP;
341
      yawTerm = -MIN_YAWGAS / 2;
329
      yawTerm = -MIN_YAWGAS / 2;
342
    } else if (yawTerm > MIN_YAWGAS / 2) {
330
    } else if (yawTerm > MIN_YAWGAS / 2) {
343
      debugOut.digital[0] |= DEBUG_CLIP;
331
      debugOut.digital[0] |= DEBUG_CLIP;
344
      yawTerm = MIN_YAWGAS / 2;
332
      yawTerm = MIN_YAWGAS / 2;
345
    }
333
    }
346
    //CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2));
-
 
347
  }
334
  }
Line 348... Line -...
348
 
-
 
349
  // FIXME: Throttle may exceed maxThrottle (there is no check no more).
335
 
350
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
336
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
351
  if (yawTerm < -(tmp_int - throttleTerm)) {
337
  if (yawTerm < -(tmp_int - throttleTerm)) {
352
    yawTerm = -(tmp_int - throttleTerm);
338
    yawTerm = -(tmp_int - throttleTerm);
353
    debugOut.digital[0] |= DEBUG_CLIP;
339
    debugOut.digital[0] |= DEBUG_CLIP;
Line 390... Line 376...
390
    if (term[axis] < -tmp_int) {
376
    if (term[axis] < -tmp_int) {
391
      debugOut.digital[1] |= DEBUG_CLIP;
377
      debugOut.digital[1] |= DEBUG_CLIP;
392
    } else if (term[axis] > tmp_int) {
378
    } else if (term[axis] > tmp_int) {
393
      debugOut.digital[1] |= DEBUG_CLIP;
379
      debugOut.digital[1] |= DEBUG_CLIP;
394
    }
380
    }
395
    CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int);
-
 
396
  }
381
  }
Line 397... Line 382...
397
 
382
 
398
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
383
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
399
  // Universal Mixer
384
  // Universal Mixer