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