Rev 2059 | Rev 2073 | 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 }; |
16 | uint16_t controlActivity = 0; |
||
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 | } |
||
2067 | - | 74 | |
75 | debugOut.analog[31] = variables[0]; |
||
1612 | dongfang | 76 | } |
77 | |||
78 | uint8_t controlMixer_getSignalQuality(void) { |
||
1961 | - | 79 | uint8_t rcQ = RC_getSignalQuality(); |
80 | uint8_t ecQ = EC_getSignalQuality(); |
||
1964 | - | 81 | |
1961 | - | 82 | // This needs not be the only correct solution... |
83 | return rcQ > ecQ ? rcQ : ecQ; |
||
1612 | dongfang | 84 | } |
85 | |||
1908 | - | 86 | void updateControlAndMeasureControlActivity(uint8_t index, int16_t newValue) { |
1961 | - | 87 | int16_t tmp = controls[index]; |
88 | controls[index] = newValue; |
||
2017 | - | 89 | |
1961 | - | 90 | tmp -= newValue; |
91 | tmp /= 2; |
||
92 | tmp = tmp * tmp; |
||
93 | // tmp += (newValue >= 0) ? newValue : -newValue; |
||
1986 | - | 94 | /* |
1961 | - | 95 | if (controlActivity + (uint16_t)tmp >= controlActivity) |
96 | controlActivity += tmp; |
||
97 | else controlActivity = 0xffff; |
||
1986 | - | 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 | } |
110 | |||
1612 | dongfang | 111 | /* |
2048 | - | 112 | * Update the variables indicating stick position from the sum of R/C, GPS and external control |
113 | * and whatever other controls we invented in the meantime... |
||
114 | * Update variables. |
||
115 | * Decode commands but do not execute them. |
||
1612 | dongfang | 116 | */ |
2048 | - | 117 | |
2039 | - | 118 | void controlMixer_periodicTask(void) { |
2053 | - | 119 | int16_t tempPRTY[4] = { 0, 0, 0, 0 }; |
120 | |||
2049 | - | 121 | // Decode commands. |
122 | uint8_t rcCommand = (RC_getSignalQuality() >= SIGNAL_OK) ? RC_getCommand() |
||
123 | : COMMAND_NONE; |
||
124 | uint8_t ecCommand = (EC_getSignalQuality() >= SIGNAL_OK) ? EC_getCommand() |
||
125 | : COMMAND_NONE; |
||
126 | |||
127 | // Update variables ("potis"). |
||
128 | if (controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
||
129 | controlMixer_updateVariables(); |
||
130 | controlMixer_didReceiveSignal = 1; |
||
131 | } else { // Signal is not OK |
||
132 | // Could handle switch to emergency flight here. |
||
133 | // throttle is handled elsewhere. |
||
134 | } |
||
135 | |||
136 | if (rcCommand != COMMAND_NONE) { |
||
137 | isCommandRepeated = (lastCommand == rcCommand); |
||
138 | lastCommand = rcCommand; |
||
139 | lastArgument = RC_getArgument(); |
||
140 | } else if (ecCommand != COMMAND_NONE) { |
||
141 | isCommandRepeated = (lastCommand == ecCommand); |
||
142 | lastCommand = ecCommand; |
||
143 | lastArgument = EC_getArgument(); |
||
144 | } else { |
||
145 | // Both sources have no command, or one or both are out. |
||
146 | // Just set to false. There is no reason to check if the none-command was repeated anyway. |
||
147 | isCommandRepeated = 0; |
||
148 | lastCommand = COMMAND_NONE; |
||
149 | } |
||
1961 | - | 150 | |
2048 | - | 151 | // This will init the values (not just add to them). |
152 | RC_periodicTaskAndPRTY(tempPRTY); |
||
2045 | - | 153 | |
2058 | - | 154 | debugOut.analog[16] = tempPRTY[CONTROL_PITCH]; |
155 | debugOut.analog[17] = tempPRTY[CONTROL_ROLL]; |
||
2059 | - | 156 | debugOut.analog[18] = tempPRTY[CONTROL_THROTTLE]; |
157 | debugOut.analog[19] = tempPRTY[CONTROL_YAW]; |
||
2058 | - | 158 | |
2048 | - | 159 | // Add external control to RC |
160 | EC_periodicTaskAndPRTY(tempPRTY); |
||
2039 | - | 161 | |
2052 | - | 162 | #ifdef USE_DIRECT_GPS |
2058 | - | 163 | if (staticParams.bitConfig & (CFG_COMPASS_ENABLED)) |
164 | navigation_periodicTaskAndPRTY(tempPRTY); |
||
2052 | - | 165 | #endif |
2039 | - | 166 | |
2048 | - | 167 | // Add compass control (could also have been before navi, they are independent) |
168 | CC_periodicTaskAndPRTY(tempPRTY); |
||
169 | |||
2055 | - | 170 | FC_periodicTaskAndPRTY(tempPRTY); |
2048 | - | 171 | |
2058 | - | 172 | // This is temporary. There might be some emergency height control also. |
173 | if (!(MKFlags & MKFLAG_EMERGENCY_FLIGHT)) { |
||
2055 | - | 174 | // Add height control (could also have been before navi and/or compass, they are independent) |
175 | HC_periodicTaskAndPRTY(tempPRTY); |
||
2048 | - | 176 | |
2055 | - | 177 | // Add attitude control (could also have been before navi and/or compass, they are independent) |
178 | AC_getPRTY(tempPRTY); |
||
179 | } |
||
180 | |||
2048 | - | 181 | // Commit results to global variable and also measure control activity. |
2051 | - | 182 | controls[CONTROL_THROTTLE] = tempPRTY[CONTROL_THROTTLE]; |
2048 | - | 183 | updateControlAndMeasureControlActivity(CONTROL_PITCH, tempPRTY[CONTROL_PITCH]); |
184 | updateControlAndMeasureControlActivity(CONTROL_ROLL, tempPRTY[CONTROL_ROLL]); |
||
185 | updateControlAndMeasureControlActivity(CONTROL_YAW, tempPRTY[CONTROL_YAW]); |
||
1961 | - | 186 | dampenControlActivity(); |
2055 | - | 187 | //debugOut.analog[17] = controlActivity/10; |
1961 | - | 188 | |
1980 | - | 189 | // We can safely do this even with a bad signal - the variables will not have been updated then. |
190 | configuration_applyVariablesToParams(); |
||
1961 | - | 191 | |
192 | // part1a end. |
||
193 | |||
194 | /* This is not really necessary with the dead-band feature on all sticks (see rc.c) |
||
195 | if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
||
196 | if (controlYaw > 2) controlYaw-= 2; |
||
197 | else if (controlYaw< -2) controlYaw += 2; |
||
198 | else controlYaw = 0; |
||
199 | } |
||
200 | */ |
||
201 | |||
202 | /* |
||
2048 | - | 203 | * Record maxima. Predecessor of the control activity stuff. |
1961 | - | 204 | for (axis = PITCH; axis <= ROLL; axis++) { |
205 | if (abs(control[axis] / CONTROL_SCALING) > maxControl[axis]) { |
||
206 | maxControl[axis] = abs(control[axis]) / CONTROL_SCALING; |
||
207 | if (maxControl[axis] > 100) |
||
208 | maxControl[axis] = 100; |
||
209 | } else if (maxControl[axis]) |
||
210 | maxControl[axis]--; |
||
211 | } |
||
212 | */ |
||
1964 | - | 213 | } |