Subversion Repositories FlightCtrl

Rev

Rev 2059 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2059 Rev 2069
1
#include "controlMixer.h"
1
#include "controlMixer.h"
2
#include "timer0.h"
2
#include "timer0.h"
3
#include "configuration.h"
3
#include "configuration.h"
4
#include "twimaster.h"
4
#include "twimaster.h"
5
#include "flight.h"
5
#include "flight.h"
6
#include "output.h"
6
#include "output.h"
7
 
7
 
8
uint16_t emergencyFlightTime;
8
uint16_t emergencyFlightTime;
9
 
9
 
10
void FC_setNeutral(void) {
10
void FC_setNeutral(void) {
11
  configuration_setNormalFlightParameters();
11
  configuration_setNormalFlightParameters();
12
}
12
}
13
 
13
 
14
void FC_periodicTaskAndPRTY(uint16_t* PRTY) {
14
void FC_periodicTaskAndPRTY(uint16_t* PRTY) {
15
 
15
 
16
  debugOut.analog[25] = emergencyFlightTime;
16
  debugOut.analog[25] = emergencyFlightTime;
-
 
17
 
-
 
18
  debugOut.analog[30] = controlMixer_getSignalQuality();
17
 
19
 
18
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
20
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
19
    if (controlMixer_didReceiveSignal)
21
    if (controlMixer_didReceiveSignal)
20
      beepRCAlarm(); // Only make alarm if a control signal was received before the signal loss.
22
      beepRCAlarm(); // Only make alarm if a control signal was received before the signal loss.
21
 
23
 
22
    // There are the possibilities: We are not yet in emergency flight, we are already in emergency flight.
24
    // There are the possibilities: We are not yet in emergency flight, we are already in emergency flight.
23
    if (!(MKFlags & MKFLAG_EMERGENCY_FLIGHT)) {
25
    if (!(MKFlags & MKFLAG_EMERGENCY_FLIGHT)) {
24
      if (isFlying > 256) {
26
      if (isFlying > 256) {
25
        MKFlags |= MKFLAG_EMERGENCY_FLIGHT; // Set flag for emergency landing
27
        MKFlags |= MKFLAG_EMERGENCY_FLIGHT; // Set flag for emergency landing
26
        configuration_setFailsafeFlightParameters();
28
        configuration_setFailsafeFlightParameters();
27
        // Set the time in whole seconds.
29
        // Set the time in whole seconds.
28
        if (staticParams.emergencyFlightDuration > (65535 - (uint16_t)F_MAINLOOP) / (uint16_t)F_MAINLOOP)
30
        if (staticParams.emergencyFlightDuration > (65535 - (uint16_t)F_MAINLOOP) / (uint16_t)F_MAINLOOP)
29
          emergencyFlightTime = 0xffff;
31
          emergencyFlightTime = 0xffff;
30
        else
32
        else
31
          emergencyFlightTime = (uint16_t)staticParams.emergencyFlightDuration * (uint16_t)F_MAINLOOP;
33
          emergencyFlightTime = (uint16_t)staticParams.emergencyFlightDuration * (uint16_t)F_MAINLOOP;
32
      }
34
      }
33
    } else {
35
    } else {
34
      if (emergencyFlightTime) {
36
      if (emergencyFlightTime) {
35
        emergencyFlightTime--;
37
        emergencyFlightTime--;
36
      } else {
38
      } else {
37
        // stop motors but stay in emergency flight.
39
        // stop motors but stay in emergency flight.
38
        MKFlags &= ~(MKFLAG_MOTOR_RUN);
40
        MKFlags &= ~(MKFLAG_MOTOR_RUN);
39
      }
41
      }
40
    }
42
    }
41
 
43
 
42
    // In either case, use e. throttle and neutral controls. TODO: If there is supposed to be a navi come-home, this should affect RC control only and not navi.
44
    // In either case, use e. throttle and neutral controls. TODO: If there is supposed to be a navi come-home, this should affect RC control only and not navi.
43
    PRTY[CONTROL_THROTTLE] = staticParams.emergencyThrottle; // Set emergency throttle
45
    PRTY[CONTROL_THROTTLE] = staticParams.emergencyThrottle; // Set emergency throttle
44
    PRTY[CONTROL_PITCH] = PRTY[CONTROL_ROLL] = PRTY[CONTROL_YAW] = 0;
46
    PRTY[CONTROL_PITCH] = PRTY[CONTROL_ROLL] = PRTY[CONTROL_YAW] = 0;
45
  } else {
47
  } else {
46
    // Signal is OK.
48
    // Signal is OK.
47
    if (MKFlags & MKFLAG_EMERGENCY_FLIGHT) {
49
    if (MKFlags & MKFLAG_EMERGENCY_FLIGHT) {
48
      MKFlags &= ~MKFLAG_EMERGENCY_FLIGHT; // Clear flag for emergency landing
50
      MKFlags &= ~MKFLAG_EMERGENCY_FLIGHT; // Clear flag for emergency landing
49
      configuration_setNormalFlightParameters();
51
      configuration_setNormalFlightParameters();
50
    }
52
    }
51
  }
53
  }
52
 
54
 
53
  /*
55
  /*
54
   * If a Bl-Ctrl is missing, prevent takeoff.
56
   * If a Bl-Ctrl is missing, prevent takeoff.
55
   */
57
   */
56
  if (missingMotor) {
58
  if (missingMotor) {
57
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
59
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
58
    if (isFlying > 1 && isFlying < 50 && PRTY[CONTROL_THROTTLE] > 0) isFlying = 1; // keep within lift off condition
60
    if (isFlying > 1 && isFlying < 50 && PRTY[CONTROL_THROTTLE] > 0) isFlying = 1; // keep within lift off condition
59
    PRTY[CONTROL_THROTTLE] = staticParams.minThrottle; // reduce throttle to min to prevent takeoff
61
    PRTY[CONTROL_THROTTLE] = staticParams.minThrottle; // reduce throttle to min to prevent takeoff
60
  }
62
  }
61
}
63
}
62
 
64