Subversion Repositories FlightCtrl

Rev

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

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