Subversion Repositories FlightCtrl

Rev

Rev 2088 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1612 dongfang 1
#include <stdlib.h>
2
#include "controlMixer.h"
3
#include "rc.h"
1775 - 4
#include "heightControl.h"
5
#include "attitudeControl.h"
1612 dongfang 6
#include "externalControl.h"
2048 - 7
#include "compassControl.h"
2055 - 8
#include "failsafeControl.h"
2039 - 9
#include "naviControl.h"
1612 dongfang 10
#include "configuration.h"
11
#include "attitude.h"
1775 - 12
#include "commands.h"
13
#include "output.h"
1612 dongfang 14
 
1908 - 15
// uint16_t maxControl[2] = { 0, 0 };
2089 - 16
// uint16_t controlActivity = 0;
1908 - 17
int16_t controls[4] = { 0, 0, 0, 0 };
1612 dongfang 18
 
19
// Internal variables for reading commands made with an R/C stick.
1821 - 20
uint8_t lastCommand = COMMAND_NONE;
1775 - 21
uint8_t lastArgument;
1612 dongfang 22
 
1775 - 23
uint8_t isCommandRepeated = 0;
1963 - 24
uint8_t controlMixer_didReceiveSignal = 0;
1775 - 25
 
1612 dongfang 26
/*
27
 * This could be expanded to take arguments from ohter sources than the RC
28
 * (read: Custom MK RC project)
29
 */
30
uint8_t controlMixer_getArgument(void) {
1961 - 31
  return lastArgument;
1612 dongfang 32
}
33
 
34
/*
35
 * This could be expanded to take calibrate / start / stop commands from ohter sources
36
 * than the R/C (read: Custom MK R/C project)
37
 */
38
uint8_t controlMixer_getCommand(void) {
1961 - 39
  return lastCommand;
1612 dongfang 40
}
41
 
42
uint8_t controlMixer_isCommandRepeated(void) {
1961 - 43
  return isCommandRepeated;
1612 dongfang 44
}
45
 
1775 - 46
void controlMixer_setNeutral() {
1961 - 47
  for (uint8_t i=0; i<VARIABLE_COUNT; i++) {
2055 - 48
    variables[i] = RC_getVariable(i);
1961 - 49
  }
50
  EC_setNeutral();
51
  HC_setGround();
2055 - 52
  FC_setNeutral();  // FC is FailsafeControl, not FlightCtrl.
2058 - 53
 
54
  // This is to set the home pos in navi.
55
  // MKFlags |= MKFLAG_CALIBRATE;
1612 dongfang 56
}
57
 
58
/*
59
 * Update potentiometer values with limited slew rate. Could be made faster if desired.
1775 - 60
 * TODO: It assumes R/C as source. Not necessarily true.
1612 dongfang 61
 */
62
void controlMixer_updateVariables(void) {
1961 - 63
  uint8_t i;
64
  int16_t targetvalue;
65
  for (i=0; i < VARIABLE_COUNT; i++) {
66
    targetvalue = RC_getVariable(i);
67
    if (targetvalue < 0)
68
      targetvalue = 0;
69
    if (variables[i] < targetvalue && variables[i] < 255)
70
      variables[i]++;
71
    else if (variables[i] > 0 && variables[i] > targetvalue)
72
      variables[i]--;
73
  }
1612 dongfang 74
}
75
 
76
uint8_t controlMixer_getSignalQuality(void) {
1961 - 77
  uint8_t rcQ = RC_getSignalQuality();
78
  uint8_t ecQ = EC_getSignalQuality();
1964 - 79
 
1961 - 80
  // This needs not be the only correct solution...
81
  return rcQ > ecQ ? rcQ : ecQ;
1612 dongfang 82
}
83
 
2089 - 84
/*
1908 - 85
void updateControlAndMeasureControlActivity(uint8_t index, int16_t newValue) {
1961 - 86
  int16_t tmp = controls[index];
87
  controls[index] = newValue;
2017 - 88
 
1961 - 89
  tmp -= newValue;
90
  tmp /= 2;
91
  tmp = tmp * tmp;
92
  // tmp += (newValue >= 0) ? newValue : -newValue;
2089 - 93
 
94
  / *
1961 - 95
  if (controlActivity + (uint16_t)tmp >= controlActivity)
96
    controlActivity += tmp;
97
  else controlActivity = 0xffff;
2089 - 98
  * /
2055 - 99
  if (controlActivity + (uint16_t)tmp < 0x8000)
1986 - 100
    controlActivity += tmp;
1908 - 101
}
102
 
103
#define CADAMPING 10
104
void dampenControlActivity(void) {
1961 - 105
  uint32_t tmp = controlActivity;
106
  tmp *= ((1<<CADAMPING)-1);
107
  tmp >>= CADAMPING;
108
  controlActivity = tmp;
1908 - 109
}
2089 - 110
*/
1908 - 111
 
1612 dongfang 112
/*
2048 - 113
 * Update the variables indicating stick position from the sum of R/C, GPS and external control
114
 * and whatever other controls we invented in the meantime...
115
 * Update variables.
116
 * Decode commands but do not execute them.
1612 dongfang 117
 */
2048 - 118
 
2039 - 119
void controlMixer_periodicTask(void) {
2053 - 120
  int16_t tempPRTY[4] = { 0, 0, 0, 0 };
121
 
2049 - 122
  // Decode commands.
123
  uint8_t rcCommand = (RC_getSignalQuality() >= SIGNAL_OK) ? RC_getCommand()
124
    : COMMAND_NONE;
2073 - 125
 
2049 - 126
  uint8_t ecCommand = (EC_getSignalQuality() >= SIGNAL_OK) ? EC_getCommand()
127
    : COMMAND_NONE;
128
 
129
  // Update variables ("potis").
130
  if (controlMixer_getSignalQuality() >= SIGNAL_GOOD) {
131
    controlMixer_updateVariables();
132
    controlMixer_didReceiveSignal = 1;
133
  } else { // Signal is not OK
134
    // Could handle switch to emergency flight here.
135
    // throttle is handled elsewhere.
136
  }
137
 
138
  if (rcCommand != COMMAND_NONE) {
139
    isCommandRepeated = (lastCommand == rcCommand);
140
    lastCommand = rcCommand;
141
    lastArgument = RC_getArgument();
142
  } else if (ecCommand != COMMAND_NONE) {
143
    isCommandRepeated = (lastCommand == ecCommand);
144
    lastCommand = ecCommand;
145
    lastArgument = EC_getArgument();
146
  } else {
147
    // Both sources have no command, or one or both are out.
148
    // Just set to false. There is no reason to check if the none-command was repeated anyway.
149
    isCommandRepeated = 0;
150
    lastCommand = COMMAND_NONE;
151
  }
1961 - 152
 
2048 - 153
  // This will init the values (not just add to them).
154
  RC_periodicTaskAndPRTY(tempPRTY);
2045 - 155
 
2048 - 156
  // Add external control to RC
157
  EC_periodicTaskAndPRTY(tempPRTY);
2039 - 158
 
2052 - 159
#ifdef USE_DIRECT_GPS
2088 - 160
  if (staticParams.bitConfig & (CFG_NAVI_ENABLED))
2058 - 161
    navigation_periodicTaskAndPRTY(tempPRTY);
2052 - 162
#endif
2039 - 163
 
2048 - 164
  // Add compass control (could also have been before navi, they are independent)
165
  CC_periodicTaskAndPRTY(tempPRTY);
166
 
2055 - 167
  FC_periodicTaskAndPRTY(tempPRTY);
2048 - 168
 
2058 - 169
  // This is temporary. There might be some emergency height control also.
170
  if (!(MKFlags & MKFLAG_EMERGENCY_FLIGHT)) {
2055 - 171
    // Add height control (could also have been before navi and/or compass, they are independent)
172
    HC_periodicTaskAndPRTY(tempPRTY);
2048 - 173
 
2055 - 174
    // Add attitude control (could also have been before navi and/or compass, they are independent)
175
    AC_getPRTY(tempPRTY);
176
  }
177
 
2048 - 178
  // Commit results to global variable and also measure control activity.
2051 - 179
  controls[CONTROL_THROTTLE] = tempPRTY[CONTROL_THROTTLE];
2089 - 180
  controls[CONTROL_PITCH] = tempPRTY[CONTROL_PITCH];
181
  controls[CONTROL_ROLL] = tempPRTY[CONTROL_ROLL];
182
  controls[CONTROL_YAW] = tempPRTY[CONTROL_YAW];
183
  // dampenControlActivity();
1961 - 184
 
1980 - 185
  // We can safely do this even with a bad signal - the variables will not have been updated then.
186
  configuration_applyVariablesToParams();
1964 - 187
 }