Rev 1796 | Rev 1867 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1796 | Rev 1821 | ||
---|---|---|---|
Line 58... | Line 58... | ||
58 | #include "configuration.h" |
58 | #include "configuration.h" |
59 | #include "attitude.h" |
59 | #include "attitude.h" |
60 | #include "commands.h" |
60 | #include "commands.h" |
61 | #include "output.h" |
61 | #include "output.h" |
Line 62... | Line 62... | ||
62 | 62 | ||
63 | uint16_t maxControl[2] = {0,0}; |
63 | uint16_t maxControl[2] = { 0, 0 }; |
64 | int16_t control[2] = {0,0}; |
64 | int16_t control[2] = { 0, 0 }; |
65 | int16_t controlYaw = 0, controlThrottle = 0; |
65 | int16_t controlYaw = 0, controlThrottle = 0; |
Line 66... | Line 66... | ||
66 | uint8_t looping = 0; |
66 | uint8_t looping = 0; |
67 | 67 | ||
68 | // Internal variables for reading commands made with an R/C stick. |
68 | // Internal variables for reading commands made with an R/C stick. |
Line 69... | Line 69... | ||
69 | uint8_t lastCommand = COMMAND_NONE; |
69 | uint8_t lastCommand = COMMAND_NONE; |
Line 70... | Line 70... | ||
70 | uint8_t lastArgument; |
70 | uint8_t lastArgument; |
Line 78... | Line 78... | ||
78 | /* |
78 | /* |
79 | * This could be expanded to take arguments from ohter sources than the RC |
79 | * This could be expanded to take arguments from ohter sources than the RC |
80 | * (read: Custom MK RC project) |
80 | * (read: Custom MK RC project) |
81 | */ |
81 | */ |
82 | uint8_t controlMixer_getArgument(void) { |
82 | uint8_t controlMixer_getArgument(void) { |
83 | return lastArgument; |
83 | return lastArgument; |
84 | } |
84 | } |
Line 85... | Line 85... | ||
85 | 85 | ||
86 | /* |
86 | /* |
87 | * This could be expanded to take calibrate / start / stop commands from ohter sources |
87 | * This could be expanded to take calibrate / start / stop commands from ohter sources |
88 | * than the R/C (read: Custom MK R/C project) |
88 | * than the R/C (read: Custom MK R/C project) |
89 | */ |
89 | */ |
90 | uint8_t controlMixer_getCommand(void) { |
90 | uint8_t controlMixer_getCommand(void) { |
91 | return lastCommand; |
91 | return lastCommand; |
Line 92... | Line 92... | ||
92 | } |
92 | } |
93 | 93 | ||
94 | uint8_t controlMixer_isCommandRepeated(void) { |
94 | uint8_t controlMixer_isCommandRepeated(void) { |
Line 95... | Line 95... | ||
95 | return isCommandRepeated; |
95 | return isCommandRepeated; |
96 | } |
96 | } |
97 | 97 | ||
98 | void controlMixer_setNeutral() { |
98 | void controlMixer_setNeutral() { |
Line 99... | Line 99... | ||
99 | EC_setNeutral(); |
99 | EC_setNeutral(); |
100 | HC_setGround(); |
100 | HC_setGround(); |
101 | } |
101 | } |
102 | 102 | ||
103 | /* |
103 | /* |
104 | * Set the potientiometer values to the momentary values of the respective R/C channels. |
104 | * Set the potientiometer values to the momentary values of the respective R/C channels. |
105 | * No slew rate limitation. |
105 | * No slew rate limitation. |
106 | */ |
106 | */ |
107 | void controlMixer_initVariables(void) { |
107 | void controlMixer_initVariables(void) { |
108 | uint8_t i; |
108 | uint8_t i; |
Line 109... | Line 109... | ||
109 | for (i=0; i<8; i++) { |
109 | for (i = 0; i < 8; i++) { |
110 | variables[i] = RC_getVariable(i); |
110 | variables[i] = RC_getVariable(i); |
111 | } |
111 | } |
112 | } |
112 | } |
113 | 113 | ||
114 | /* |
114 | /* |
115 | * Update potentiometer values with limited slew rate. Could be made faster if desired. |
115 | * Update potentiometer values with limited slew rate. Could be made faster if desired. |
116 | * TODO: It assumes R/C as source. Not necessarily true. |
116 | * TODO: It assumes R/C as source. Not necessarily true. |
117 | */ |
117 | */ |
118 | void controlMixer_updateVariables(void) { |
118 | void controlMixer_updateVariables(void) { |
- | 119 | uint8_t i; |
|
- | 120 | int16_t targetvalue; |
|
- | 121 | for (i = 0; i < 8; i++) { |
|
119 | uint8_t i; |
122 | targetvalue = RC_getVariable(i); |
- | 123 | if (targetvalue < 0) |
|
120 | int16_t targetvalue; |
124 | targetvalue = 0; |
121 | for (i=0; i<8; i++) { |
125 | if (variables[i] < targetvalue && variables[i] < 255) |
Line 122... | Line 126... | ||
122 | targetvalue = RC_getVariable(i); |
126 | variables[i]++; |
123 | if (targetvalue < 0) targetvalue = 0; |
127 | else if (variables[i] > 0 && variables[i] > targetvalue) |
124 | if (variables[i] < targetvalue && variables[i] < 255) variables[i]++; else if(variables[i] > 0 && variables[i] > targetvalue) variables[i]--; |
128 | variables[i]--; |
125 | } |
129 | } |
126 | } |
130 | } |
127 | 131 | ||
Line 128... | Line 132... | ||
128 | uint8_t controlMixer_getSignalQuality(void) { |
132 | uint8_t controlMixer_getSignalQuality(void) { |
129 | uint8_t rcQ = RC_getSignalQuality(); |
133 | uint8_t rcQ = RC_getSignalQuality(); |
130 | uint8_t ecQ = EC_getSignalQuality(); |
134 | uint8_t ecQ = EC_getSignalQuality(); |
131 | // This needs not be the only correct solution... |
135 | // This needs not be the only correct solution... |
132 | return rcQ > ecQ ? rcQ : ecQ; |
136 | return rcQ > ecQ ? rcQ : ecQ; |
133 | } |
137 | } |
134 | 138 | ||
135 | /* |
139 | /* |
136 | * Update the variables indicating stick position from the sum of R/C, GPS and external control. |
140 | * Update the variables indicating stick position from the sum of R/C, GPS and external control. |
137 | */ |
141 | */ |
138 | void controlMixer_update(void) { |
142 | void controlMixer_update(void) { |
139 | // calculate Stick inputs by rc channels (P) and changing of rc channels (D) |
143 | // calculate Stick inputs by rc channels (P) and changing of rc channels (D) |
140 | // TODO: If no signal --> zero. |
144 | // TODO: If no signal --> zero. |
141 | uint8_t axis; |
145 | uint8_t axis; |
142 | 146 | ||
143 | // takes almost no time... |
147 | // takes almost no time... |
144 | RC_update(); |
148 | RC_update(); |
145 | 149 | ||
146 | // takes almost no time... |
150 | // takes almost no time... |
147 | EC_update(); |
151 | EC_update(); |
148 | 152 | ||
149 | // takes about 80 usec. |
153 | // takes about 80 usec. |
150 | HC_update(); |
154 | HC_update(); |
151 | 155 | ||
- | 156 | int16_t* RC_PRTY = RC_getPRTY(); |
|
152 | int16_t* RC_PRTY = RC_getPRTY(); |
157 | int16_t* EC_PRTY = EC_getPRTY(); |
153 | int16_t* EC_PRTY = EC_getPRTY(); |
158 | |
154 | 159 | control[PITCH] = RC_PRTY[CONTROL_PITCH] + EC_PRTY[CONTROL_PITCH]; |
|
155 | control[PITCH] = RC_PRTY[CONTROL_PITCH] + EC_PRTY[CONTROL_PITCH]; |
160 | control[ROLL] = RC_PRTY[CONTROL_ROLL] + EC_PRTY[CONTROL_ROLL]; |
156 | control[ROLL] = RC_PRTY[CONTROL_ROLL] + EC_PRTY[CONTROL_ROLL]; |
161 | // This can be a CPU time killer if the function implementations are inefficient. |
157 | // This can be a CPU time killer if the function implementations are inefficient. |
162 | controlThrottle = HC_getThrottle(AC_getThrottle(RC_PRTY[CONTROL_THROTTLE] |
158 | controlThrottle = HC_getThrottle(AC_getThrottle(RC_PRTY[CONTROL_THROTTLE] + EC_PRTY[CONTROL_THROTTLE])); |
163 | + EC_PRTY[CONTROL_THROTTLE])); |
159 | controlYaw = RC_PRTY[CONTROL_YAW] + EC_PRTY[CONTROL_YAW]; |
164 | controlYaw = RC_PRTY[CONTROL_YAW] + EC_PRTY[CONTROL_YAW]; |
160 | 165 | ||
161 | DebugOut.Analog[12] = control[PITCH]; |
166 | DebugOut.Analog[12] = control[PITCH]; |
162 | DebugOut.Analog[13] = control[ROLL]; |
167 | DebugOut.Analog[13] = control[ROLL]; |
163 | //DebugOut.Analog[26] = controlYaw; |
168 | //DebugOut.Analog[26] = controlYaw; |
164 | 169 | ||
165 | if (controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
170 | if (controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
166 | controlMixer_updateVariables(); |
171 | controlMixer_updateVariables(); |
167 | configuration_applyVariablesToParams(); |
172 | configuration_applyVariablesToParams(); |
168 | looping = RC_getLooping(looping); |
173 | looping = RC_getLooping(looping); |
169 | } else { // Signal is not OK |
174 | } else { // Signal is not OK |
170 | // Could handle switch to emergency flight here. |
175 | // Could handle switch to emergency flight here. |
171 | // throttle is handled elsewhere. |
176 | // throttle is handled elsewhere. |
172 | looping = 0; |
177 | looping = 0; |
173 | } |
178 | } |
174 | 179 | ||
175 | // part1a end. |
180 | // part1a end. |
176 | 181 | ||
177 | /* This is not really necessary with the dead-gap feature on all sticks (see rc.c) |
182 | /* This is not really necessary with the dead-gap feature on all sticks (see rc.c) |
178 | if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
183 | if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
179 | if (controlYaw > 2) controlYaw-= 2; |
184 | if (controlYaw > 2) controlYaw-= 2; |
180 | else if (controlYaw< -2) controlYaw += 2; |
185 | else if (controlYaw< -2) controlYaw += 2; |
181 | else controlYaw = 0; |
186 | else controlYaw = 0; |
182 | } |
187 | } |
183 | */ |
188 | */ |
184 | 189 | ||
- | 190 | /* |
|
185 | /* |
191 | * Record maxima |
- | 192 | */ |
|
186 | * Record maxima |
193 | for (axis = PITCH; axis <= ROLL; axis++) { |
187 | */ |
194 | if (abs(control[axis] / CONTROL_SCALING) > maxControl[axis]) { |
188 | for (axis=PITCH; axis<=ROLL; axis++) { |
195 | maxControl[axis] = abs(control[axis]) / CONTROL_SCALING; |
- | 196 | if (maxControl[axis] > 100) |
|
189 | if(abs(control[axis] / CONTROL_SCALING) > maxControl[axis]) { |
197 | maxControl[axis] = 100; |
- | 198 | } else if (maxControl[axis]) |
|
190 | maxControl[axis] = abs(control[axis]) / CONTROL_SCALING; |
199 | maxControl[axis]--; |
191 | if(maxControl[axis] > 100) maxControl[axis] = 100; |
200 | } |
192 | } else if (maxControl[axis]) maxControl[axis]--; |
201 | |
193 | } |
202 | uint8_t rcCommand = (RC_getSignalQuality() >= SIGNAL_OK) ? RC_getCommand() |
194 | 203 | : COMMAND_NONE; |
|
195 | uint8_t rcCommand = (RC_getSignalQuality() >= SIGNAL_OK) ? RC_getCommand() : COMMAND_NONE; |
204 | uint8_t ecCommand = (EC_getSignalQuality() >= SIGNAL_OK) ? EC_getCommand() |
196 | uint8_t ecCommand = (EC_getSignalQuality() >= SIGNAL_OK) ? EC_getCommand() : COMMAND_NONE; |
205 | : COMMAND_NONE; |
197 | 206 | ||
198 | if (rcCommand != COMMAND_NONE) { |
207 | if (rcCommand != COMMAND_NONE) { |
199 | isCommandRepeated = (lastCommand == rcCommand); |
208 | isCommandRepeated = (lastCommand == rcCommand); |
200 | lastCommand = rcCommand; |
209 | lastCommand = rcCommand; |
201 | lastArgument = RC_getArgument(); |
210 | lastArgument = RC_getArgument(); |
202 | } else if (ecCommand != COMMAND_NONE) { |
211 | } else if (ecCommand != COMMAND_NONE) { |
203 | isCommandRepeated = (lastCommand == ecCommand); |
212 | isCommandRepeated = (lastCommand == ecCommand); |
204 | lastCommand = ecCommand; |
213 | lastCommand = ecCommand; |
- | 214 | lastArgument = EC_getArgument(); |
|
- | 215 | } else { |
|
- | 216 | // Both sources have no command, or one or both are out. |
|
- | 217 | // Just set to false. There is no reason to check if the none-command was repeated anyway. |
|
- | 218 | isCommandRepeated = 0; |
|
- | 219 | lastCommand = COMMAND_NONE; |
|
- | 220 | } |
|
- | 221 | ||
- | 222 | if (isCommandRepeated) |
|
Line 205... | Line -... | ||
205 | lastArgument = EC_getArgument(); |
- | |
206 | } else { |
- | |
207 | // Both sources have no command, or one or both are out. |
- | |
208 | // Just set to false. There is no reason to check if the none-command was repeated anyway. |
223 | DebugOut.Digital[0] |= DEBUG_COMMANDREPEATED; |
209 | isCommandRepeated = 0; |
224 | else |
Line 210... | Line 225... | ||
210 | lastCommand = COMMAND_NONE; |
225 | DebugOut.Digital[0] &= ~DEBUG_COMMANDREPEATED; |
211 | } |
226 | if (rcCommand) |
212 | 227 | DebugOut.Digital[1] |= DEBUG_COMMANDREPEATED; |
|
213 | if (isCommandRepeated) DebugOut.Digital[0] |= DEBUG_COMMANDREPEATED; else DebugOut.Digital[0] &= ~DEBUG_COMMANDREPEATED; |
228 | else |