Rev 2053 | Rev 2058 | 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. |
1612 | dongfang | 53 | } |
54 | |||
55 | /* |
||
56 | * Update potentiometer values with limited slew rate. Could be made faster if desired. |
||
1775 | - | 57 | * TODO: It assumes R/C as source. Not necessarily true. |
1612 | dongfang | 58 | */ |
59 | void controlMixer_updateVariables(void) { |
||
1961 | - | 60 | uint8_t i; |
61 | int16_t targetvalue; |
||
62 | for (i=0; i < VARIABLE_COUNT; i++) { |
||
63 | targetvalue = RC_getVariable(i); |
||
64 | if (targetvalue < 0) |
||
65 | targetvalue = 0; |
||
66 | if (variables[i] < targetvalue && variables[i] < 255) |
||
67 | variables[i]++; |
||
68 | else if (variables[i] > 0 && variables[i] > targetvalue) |
||
69 | variables[i]--; |
||
70 | } |
||
1612 | dongfang | 71 | } |
72 | |||
73 | uint8_t controlMixer_getSignalQuality(void) { |
||
1961 | - | 74 | uint8_t rcQ = RC_getSignalQuality(); |
75 | uint8_t ecQ = EC_getSignalQuality(); |
||
1964 | - | 76 | |
1961 | - | 77 | // This needs not be the only correct solution... |
78 | return rcQ > ecQ ? rcQ : ecQ; |
||
1612 | dongfang | 79 | } |
80 | |||
1908 | - | 81 | void updateControlAndMeasureControlActivity(uint8_t index, int16_t newValue) { |
1961 | - | 82 | int16_t tmp = controls[index]; |
83 | controls[index] = newValue; |
||
2017 | - | 84 | |
1961 | - | 85 | tmp -= newValue; |
86 | tmp /= 2; |
||
87 | tmp = tmp * tmp; |
||
88 | // tmp += (newValue >= 0) ? newValue : -newValue; |
||
1986 | - | 89 | /* |
1961 | - | 90 | if (controlActivity + (uint16_t)tmp >= controlActivity) |
91 | controlActivity += tmp; |
||
92 | else controlActivity = 0xffff; |
||
1986 | - | 93 | */ |
2055 | - | 94 | if (controlActivity + (uint16_t)tmp < 0x8000) |
1986 | - | 95 | controlActivity += tmp; |
1908 | - | 96 | } |
97 | |||
98 | #define CADAMPING 10 |
||
99 | void dampenControlActivity(void) { |
||
1961 | - | 100 | uint32_t tmp = controlActivity; |
101 | tmp *= ((1<<CADAMPING)-1); |
||
102 | tmp >>= CADAMPING; |
||
103 | controlActivity = tmp; |
||
1908 | - | 104 | } |
105 | |||
1612 | dongfang | 106 | /* |
2048 | - | 107 | * Update the variables indicating stick position from the sum of R/C, GPS and external control |
108 | * and whatever other controls we invented in the meantime... |
||
109 | * Update variables. |
||
110 | * Decode commands but do not execute them. |
||
1612 | dongfang | 111 | */ |
2048 | - | 112 | |
2039 | - | 113 | void controlMixer_periodicTask(void) { |
2053 | - | 114 | int16_t tempPRTY[4] = { 0, 0, 0, 0 }; |
115 | |||
2049 | - | 116 | // Decode commands. |
117 | uint8_t rcCommand = (RC_getSignalQuality() >= SIGNAL_OK) ? RC_getCommand() |
||
118 | : COMMAND_NONE; |
||
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 | } |
||
1961 | - | 145 | |
2048 | - | 146 | // This will init the values (not just add to them). |
147 | RC_periodicTaskAndPRTY(tempPRTY); |
||
2045 | - | 148 | |
2048 | - | 149 | // Add external control to RC |
150 | EC_periodicTaskAndPRTY(tempPRTY); |
||
2039 | - | 151 | |
2052 | - | 152 | #ifdef USE_DIRECT_GPS |
2048 | - | 153 | // Add navigations control to RC and external control. |
154 | navigation_periodicTaskAndPRTY(tempPRTY); |
||
2052 | - | 155 | #endif |
2039 | - | 156 | |
2048 | - | 157 | // Add compass control (could also have been before navi, they are independent) |
158 | CC_periodicTaskAndPRTY(tempPRTY); |
||
159 | |||
2055 | - | 160 | FC_periodicTaskAndPRTY(tempPRTY); |
2048 | - | 161 | |
2055 | - | 162 | if (!MKFlags & MKFLAG_EMERGENCY_FLIGHT) { |
163 | // Add height control (could also have been before navi and/or compass, they are independent) |
||
164 | HC_periodicTaskAndPRTY(tempPRTY); |
||
2048 | - | 165 | |
2055 | - | 166 | // Add attitude control (could also have been before navi and/or compass, they are independent) |
167 | AC_getPRTY(tempPRTY); |
||
168 | } |
||
169 | |||
2048 | - | 170 | // Commit results to global variable and also measure control activity. |
2051 | - | 171 | controls[CONTROL_THROTTLE] = tempPRTY[CONTROL_THROTTLE]; |
2048 | - | 172 | updateControlAndMeasureControlActivity(CONTROL_PITCH, tempPRTY[CONTROL_PITCH]); |
173 | updateControlAndMeasureControlActivity(CONTROL_ROLL, tempPRTY[CONTROL_ROLL]); |
||
174 | updateControlAndMeasureControlActivity(CONTROL_YAW, tempPRTY[CONTROL_YAW]); |
||
1961 | - | 175 | dampenControlActivity(); |
2055 | - | 176 | //debugOut.analog[17] = controlActivity/10; |
1961 | - | 177 | |
1980 | - | 178 | // We can safely do this even with a bad signal - the variables will not have been updated then. |
179 | configuration_applyVariablesToParams(); |
||
1961 | - | 180 | |
181 | // part1a end. |
||
182 | |||
183 | /* This is not really necessary with the dead-band feature on all sticks (see rc.c) |
||
184 | if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
||
185 | if (controlYaw > 2) controlYaw-= 2; |
||
186 | else if (controlYaw< -2) controlYaw += 2; |
||
187 | else controlYaw = 0; |
||
188 | } |
||
189 | */ |
||
190 | |||
191 | /* |
||
2048 | - | 192 | * Record maxima. Predecessor of the control activity stuff. |
1961 | - | 193 | for (axis = PITCH; axis <= ROLL; axis++) { |
194 | if (abs(control[axis] / CONTROL_SCALING) > maxControl[axis]) { |
||
195 | maxControl[axis] = abs(control[axis]) / CONTROL_SCALING; |
||
196 | if (maxControl[axis] > 100) |
||
197 | maxControl[axis] = 100; |
||
198 | } else if (maxControl[axis]) |
||
199 | maxControl[axis]--; |
||
200 | } |
||
201 | */ |
||
1964 | - | 202 | } |