Subversion Repositories FlightCtrl

Rev

Rev 2160 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2160 Rev 2189
Line 7... Line 7...
7
#include "configuration.h"
7
#include "configuration.h"
8
#include "controlMixer.h"
8
#include "controlMixer.h"
9
#include "output.h"
9
#include "output.h"
10
#include "isqrt.h"
10
#include "isqrt.h"
11
#include "attitude.h"
11
#include "attitude.h"
12
#include "dongfangMath.h"
-
 
13
#include "attitude.h"
12
#include "attitude.h"
Line 14... Line 13...
14
 
13
 
15
typedef enum {
14
typedef enum {
16
  NAVI_FLIGHT_MODE_UNDEF,
15
  NAVI_FLIGHT_MODE_UNDEF,
17
  NAVI_FLIGHT_MODE_FREE,
16
  NAVI_FLIGHT_MODE_FREE,
18
  NAVI_FLIGHT_MODE_AID,
17
  NAVI_FLIGHT_MODE_AID,
19
  NAVI_FLIGHT_MODE_HOME,
18
  NAVI_FLIGHT_MODE_HOME,
Line 20... Line 19...
20
} FlightMode_t;
19
} NaviMode_t;
21
 
20
 
22
/*
21
/*
23
 * This is not read from internally. It only serves to let a pilot/debugger
22
 * This is not read from internally. It only serves to let a pilot/debugger
Line 51... Line 50...
51
// GPS coordinates for hold position
50
// GPS coordinates for hold position
52
GPS_Pos_t holdPosition = { 0, 0, 0, INVALID };
51
GPS_Pos_t holdPosition = { 0, 0, 0, INVALID };
53
// GPS coordinates for home position
52
// GPS coordinates for home position
54
GPS_Pos_t homePosition = { 0, 0, 0, INVALID };
53
GPS_Pos_t homePosition = { 0, 0, 0, INVALID };
55
// the current flight mode
54
// the current flight mode
56
FlightMode_t flightMode = NAVI_FLIGHT_MODE_UNDEF;
55
NaviMode_t naviMode = NAVI_FLIGHT_MODE_UNDEF;
57
int16_t naviSticks[2] = {0,0};
56
int16_t naviSticks[2] = {0,0};
Line 58... Line 57...
58
 
57
 
Line 59... Line 58...
59
int8_t naviStatus;
58
int8_t naviStatus;
60
 
59
 
61
// ---------------------------------------------------------------------------------
60
// ---------------------------------------------------------------------------------
Line 62... Line 61...
62
void navi_updateFlightMode(void) {
61
void navi_updateFlightMode(void) {
63
  static FlightMode_t flightModeOld = NAVI_FLIGHT_MODE_UNDEF;
62
  static NaviMode_t naviModeOld = NAVI_FLIGHT_MODE_UNDEF;
64
 
63
 
65
  if (MKFlags & MKFLAG_EMERGENCY_FLIGHT) {
64
  if (MKFlags & MKFLAG_EMERGENCY_FLIGHT) {
66
    flightMode = NAVI_FLIGHT_MODE_FREE;
65
    naviMode = NAVI_FLIGHT_MODE_FREE;
67
  } else {
66
  } else {
68
    if (dynamicParams.naviMode < 50)
67
    if (dynamicParams.naviMode < 50)
69
      flightMode = NAVI_FLIGHT_MODE_FREE;
68
      naviMode = NAVI_FLIGHT_MODE_FREE;
70
    else if (dynamicParams.naviMode < 180)
69
    else if (dynamicParams.naviMode < 180)
71
      flightMode = NAVI_FLIGHT_MODE_AID;
70
      naviMode = NAVI_FLIGHT_MODE_AID;
Line 72... Line 71...
72
    else
71
    else
73
      flightMode = NAVI_FLIGHT_MODE_HOME;
72
      naviMode = NAVI_FLIGHT_MODE_HOME;
74
  }
73
  }
75
 
74
 
76
  if (flightMode != flightModeOld) {
75
  if (naviMode != naviModeOld) {
Line 77... Line 76...
77
    beep(100);
76
    beep(100);
78
    flightModeOld = flightMode;
77
    naviModeOld = naviMode;
Line 105... Line 104...
105
  }
104
  }
106
  return 0;
105
  return 0;
107
}
106
}
Line 108... Line 107...
108
 
107
 
109
// checks nick and roll sticks for manual control
108
// checks nick and roll sticks for manual control
110
uint8_t navi_isManuallyControlled(int16_t* PRTY) {
-
 
111
  debugOut.analog[26] = PRTY[CONTROL_PITCH];
-
 
112
  debugOut.analog[27] = PRTY[CONTROL_ROLL];
109
uint8_t navi_isManuallyControlled(int16_t* RPTY) {
113
  if (abs(PRTY[CONTROL_PITCH]) < staticParams.naviStickThreshold
110
  if (abs(RPTY[CONTROL_PITCH]) < staticParams.naviStickThreshold
114
      && abs(PRTY[CONTROL_ROLL]) < staticParams.naviStickThreshold)
111
      && abs(RPTY[CONTROL_ROLL]) < staticParams.naviStickThreshold)
115
    return 0;
112
    return 0;
116
  else
113
  else
117
    return 1;
114
    return 1;
Line 266... Line 263...
266
    GPSPosDevIntegral_North = 0;
263
    GPSPosDevIntegral_North = 0;
267
    GPSPosDevIntegral_East = 0;
264
    GPSPosDevIntegral_East = 0;
268
  }
265
  }
269
}
266
}
Line 270... Line 267...
270
 
267
 
271
void navigation_periodicTaskAndPRTY(int16_t* PRTY) {
268
void navigation_periodicTaskAndRPTY(int16_t* RPTY) {
272
  static uint8_t GPS_P_Delay = 0;
269
  static uint8_t GPS_P_Delay = 0;
273
  static uint16_t beep_rythm = 0;
270
  static uint16_t beep_rythm = 0;
274
  static uint16_t navi_testOscPrescaler = 0;
271
  static uint16_t navi_testOscPrescaler = 0;
Line 278... Line 275...
278
    navi_setNeutral();
275
    navi_setNeutral();
279
    naviStatus = NAVI_STATUS_NO_COMPASS;
276
    naviStatus = NAVI_STATUS_NO_COMPASS;
280
    return;
277
    return;
281
  }
278
  }
Line 282... Line 279...
282
 
279
 
Line 283... Line 280...
283
  navi_updateFlightMode();
280
  navi_updateNaviMode();
284
 
281
 
285
  navi_testOscPrescaler++;
282
  navi_testOscPrescaler++;
286
  if (navi_testOscPrescaler == 488) {
283
  if (navi_testOscPrescaler == 488) {
Line 312... Line 309...
312
 
309
 
313
  switch (GPSInfo.status) {
310
  switch (GPSInfo.status) {
314
  case INVALID: // invalid gps data
311
  case INVALID: // invalid gps data
315
    navi_setNeutral();
312
    navi_setNeutral();
316
    naviStatus = NAVI_STATUS_INVALID_GPS;
313
    naviStatus = NAVI_STATUS_INVALID_GPS;
317
    if (flightMode != NAVI_FLIGHT_MODE_FREE) {
314
    if (naviMode != NAVI_FLIGHT_MODE_FREE) {
318
      beep(1); // beep if signal is neccesary
315
      beep(1); // beep if signal is neccesary
319
    }
316
    }
320
    break;
317
    break;
321
  case PROCESSED: // if gps data are already processed do nothing
318
  case PROCESSED: // if gps data are already processed do nothing
Line 332... Line 329...
332
    break;
329
    break;
333
  case NEWDATA: // new valid data from gps device
330
  case NEWDATA: // new valid data from gps device
334
    // if the gps data quality is good
331
    // if the gps data quality is good
335
    beep_rythm++;
332
    beep_rythm++;
336
    if (navi_isGPSSignalOK()) {
333
    if (navi_isGPSSignalOK()) {
337
      switch (flightMode) { // check what's to do
334
      switch (naviMode) { // check what's to do
338
      case NAVI_FLIGHT_MODE_FREE:
335
      case NAVI_FLIGHT_MODE_FREE:
339
        // update hold position to current gps position
336
        // update hold position to current gps position
340
        navi_writeCurrPositionTo(&holdPosition); // can get invalid if gps signal is bad
337
        navi_writeCurrPositionTo(&holdPosition); // can get invalid if gps signal is bad
341
        // disable gps control
338
        // disable gps control
342
        navi_setNeutral();
339
        navi_setNeutral();
343
        naviStatus = NAVI_STATUS_FREEFLIGHT;
340
        naviStatus = NAVI_STATUS_FREEFLIGHT;
344
        break;
341
        break;
Line 345... Line 342...
345
 
342
 
346
      case NAVI_FLIGHT_MODE_AID:
343
      case NAVI_FLIGHT_MODE_AID:
347
        if (holdPosition.status != INVALID) {
344
        if (holdPosition.status != INVALID) {
348
          if (navi_isManuallyControlled(PRTY)) { // MK controlled by user
345
          if (navi_isManuallyControlled(RPTY)) { // MK controlled by user
349
            // update hold point to current gps position
346
            // update hold point to current gps position
350
            navi_writeCurrPositionTo(&holdPosition);
347
            navi_writeCurrPositionTo(&holdPosition);
351
            // disable gps control
348
            // disable gps control
352
            navi_setNeutral();
349
            navi_setNeutral();
Line 375... Line 372...
375
      case NAVI_FLIGHT_MODE_HOME:
372
      case NAVI_FLIGHT_MODE_HOME:
376
        if (homePosition.status != INVALID) {
373
        if (homePosition.status != INVALID) {
377
          // update hold point to current gps position
374
          // update hold point to current gps position
378
          // to avoid a flight back if home comming is deactivated
375
          // to avoid a flight back if home comming is deactivated
379
          navi_writeCurrPositionTo(&holdPosition);
376
          navi_writeCurrPositionTo(&holdPosition);
380
          if (navi_isManuallyControlled(PRTY)) { // MK controlled by user
377
          if (navi_isManuallyControlled(RPTY)) { // MK controlled by user
381
            navi_setNeutral();
378
            navi_setNeutral();
382
            naviStatus = NAVI_STATUS_MANUAL_OVERRIDE;
379
            naviStatus = NAVI_STATUS_MANUAL_OVERRIDE;
383
          } else {// GPS control active
380
          } else {// GPS control active
384
            navi_PIDController(&homePosition);
381
            navi_PIDController(&homePosition);
385
          }
382
          }
Line 387... Line 384...
387
        } else {
384
        } else {
388
          // bad home position
385
          // bad home position
389
          beep(50); // signal invalid home position
386
          beep(50); // signal invalid home position
390
          // try to hold at least the position as a fallback option
387
          // try to hold at least the position as a fallback option
391
          if (holdPosition.status != INVALID) {
388
          if (holdPosition.status != INVALID) {
392
            if (navi_isManuallyControlled(PRTY)) {
389
            if (navi_isManuallyControlled(RPTY)) {
393
              // MK controlled by user
390
              // MK controlled by user
394
              navi_setNeutral();
391
              navi_setNeutral();
395
              naviStatus = NAVI_STATUS_MANUAL_OVERRIDE;
392
              naviStatus = NAVI_STATUS_MANUAL_OVERRIDE;
396
            } else {
393
            } else {
397
              // GPS control active
394
              // GPS control active
Line 412... Line 409...
412
    } // eof gps data quality is good
409
    } // eof gps data quality is good
413
    else { // gps data quality is bad
410
    else { // gps data quality is bad
414
      // disable gps control
411
      // disable gps control
415
      navi_setNeutral();
412
      navi_setNeutral();
416
      naviStatus = NAVI_STATUS_BAD_GPS_SIGNAL;
413
      naviStatus = NAVI_STATUS_BAD_GPS_SIGNAL;
417
      if (flightMode != NAVI_FLIGHT_MODE_FREE) {
414
      if (naviMode != NAVI_FLIGHT_MODE_FREE) {
418
        // beep if signal is not sufficient
415
        // beep if signal is not sufficient
419
        if (!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5))
416
        if (!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5))
420
          beep(100);
417
          beep(100);
421
        else if (GPSInfo.satnum < staticParams.GPSMininumSatellites
418
        else if (GPSInfo.satnum < staticParams.GPSMininumSatellites
422
            && !(beep_rythm % 5))
419
            && !(beep_rythm % 5))
Line 426... Line 423...
426
    // set current data as processed to avoid further calculations on the same gps data
423
    // set current data as processed to avoid further calculations on the same gps data
427
    GPSInfo.status = PROCESSED;
424
    GPSInfo.status = PROCESSED;
428
    break;
425
    break;
429
  } // eof GPSInfo.status
426
  } // eof GPSInfo.status
Line 430... Line 427...
430
 
427
 
431
  PRTY[CONTROL_PITCH] += naviSticks[CONTROL_PITCH];
428
  RPTY[CONTROL_PITCH] += naviSticks[CONTROL_PITCH];
432
  PRTY[CONTROL_ROLL] += naviSticks[CONTROL_ROLL];
-
 
433
 
-
 
434
  //debugOut.analog[16] = flightMode;
-
 
435
  //debugOut.analog[17] = naviStatus;
-
 
436
 
-
 
437
  /*
-
 
438
  debugOut.analog[18] = naviSticks[CONTROL_PITCH];
-
 
439
  debugOut.analog[19] = naviSticks[CONTROL_ROLL];
-
 
440
  */
429
  RPTY[CONTROL_ROLL] += naviSticks[CONTROL_ROLL];