Rev 2079 | Rev 2088 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2079 | Rev 2086 | ||
---|---|---|---|
1 | #include <inttypes.h> |
1 | #include <inttypes.h> |
2 | #include "analog.h" |
2 | #include "analog.h" |
3 | #include "attitude.h" |
3 | #include "attitude.h" |
4 | #include "configuration.h" |
4 | #include "configuration.h" |
5 | #include "controlMixer.h" |
5 | #include "controlMixer.h" |
6 | 6 | ||
7 | // for digital / LED debug. |
7 | // for digital / LED debug. |
8 | #include "output.h" |
8 | #include "output.h" |
9 | 9 | ||
10 | // For scope debugging only! |
10 | // For scope debugging only! |
11 | #include "rc.h" |
11 | #include "rc.h" |
12 | 12 | ||
13 | #define INTEGRAL_LIMIT 100000 |
13 | #define INTEGRAL_LIMIT 100000 |
14 | 14 | ||
15 | #define LATCH_TIME 3 |
15 | #define LATCH_TIME 3 |
16 | 16 | ||
17 | int32_t setHeight; |
17 | int32_t setHeight; |
18 | int32_t targetHeight; |
18 | int32_t targetHeight; |
19 | 19 | ||
20 | uint16_t testOscPrescaler = 0; |
20 | uint16_t hc_testOscPrescaler = 0; |
21 | uint8_t testOscTimer = 0; |
21 | uint8_t hc_testOscTimer = 0; |
22 | 22 | ||
23 | int32_t maxHeightThisFlight; |
23 | int32_t maxHeightThisFlight; |
24 | int32_t iHeight; |
24 | int32_t iHeight; |
25 | 25 | ||
26 | void HC_setGround(void) { |
26 | void HC_setGround(void) { |
27 | analog_setGround(); |
27 | analog_setGround(); |
28 | // This should also happen when height control is enabled in-flight. |
28 | // This should also happen when height control is enabled in-flight. |
29 | setHeight = targetHeight = analog_getHeight(); |
29 | setHeight = targetHeight = analog_getHeight(); |
30 | testOscTimer = 0; |
30 | hc_testOscTimer = 0; |
31 | maxHeightThisFlight = 0; |
31 | maxHeightThisFlight = 0; |
32 | iHeight = 0; |
32 | iHeight = 0; |
33 | } |
33 | } |
34 | 34 | ||
35 | uint8_t HC_isSwitchOn(void) { |
35 | uint8_t HC_isSwitchOn(void) { |
36 | return (dynamicParams.heightSetting >= 255/3); |
36 | return (dynamicParams.heightSetting >= 255/3); |
37 | } |
37 | } |
38 | 38 | ||
39 | void HC_periodicTask(void) { |
39 | void HC_periodicTask(void) { |
40 | int32_t height = analog_getHeight(); |
40 | int32_t height = analog_getHeight(); |
41 | static uint8_t setHeightLatch = 0; |
41 | static uint8_t setHeightLatch = 0; |
42 | 42 | ||
43 | if (height > maxHeightThisFlight) |
43 | if (height > maxHeightThisFlight) |
44 | maxHeightThisFlight = height; |
44 | maxHeightThisFlight = height; |
45 | 45 | ||
46 | // debugOut.analog[25] = dynamicParams.heightSetting; |
46 | // debugOut.analog[25] = dynamicParams.heightSetting; |
47 | 47 | ||
48 | if (staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) { |
48 | if (staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) { |
49 | if (HC_isSwitchOn()) { |
49 | if (HC_isSwitchOn()) { |
50 | // Switch is ON |
50 | // Switch is ON |
51 | if (setHeightLatch <= LATCH_TIME) { |
51 | if (setHeightLatch <= LATCH_TIME) { |
52 | if (setHeightLatch == LATCH_TIME) { |
52 | if (setHeightLatch == LATCH_TIME) { |
53 | // Freeze the height as target. We want to do this exactly once each time the switch is thrown ON. |
53 | // Freeze the height as target. We want to do this exactly once each time the switch is thrown ON. |
54 | setHeight = height; |
54 | setHeight = height; |
55 | testOscTimer = 0; |
55 | hc_testOscTimer = 0; |
56 | iHeight = 0; |
56 | iHeight = 0; |
57 | } |
57 | } |
58 | // Time not yet reached. |
58 | // Time not yet reached. |
59 | setHeightLatch++; |
59 | setHeightLatch++; |
60 | } |
60 | } |
61 | } else { |
61 | } else { |
62 | // Switch is OFF. |
62 | // Switch is OFF. |
63 | setHeightLatch = 0; |
63 | setHeightLatch = 0; |
64 | } |
64 | } |
65 | } else { |
65 | } else { |
66 | // Switch is not activated; take the "max-height" as the target height. |
66 | // Switch is not activated; take the "max-height" as the target height. |
67 | setHeight = (uint16_t) dynamicParams.heightSetting * 25L - 500L; // should be: 100 (or make a param out of it) |
67 | setHeight = (uint16_t) dynamicParams.heightSetting * 25L - 500L; // should be: 100 (or make a param out of it) |
68 | } |
68 | } |
69 | 69 | ||
70 | /* |
70 | /* |
71 | if (++heightRampingTimer == INTEGRATION_FREQUENCY / 10) { |
71 | if (++heightRampingTimer == INTEGRATION_FREQUENCY / 10) { |
72 | heightRampingTimer = 0; |
72 | heightRampingTimer = 0; |
73 | if (rampedTargetHeight < targetHeight) { |
73 | if (rampedTargetHeight < targetHeight) { |
74 | // climbing |
74 | // climbing |
75 | if (rampedTargetHeight < targetHeight - staticParams.heightSlewRate) { |
75 | if (rampedTargetHeight < targetHeight - staticParams.heightSlewRate) { |
76 | rampedTargetHeight += staticParams.heightSlewRate; |
76 | rampedTargetHeight += staticParams.heightSlewRate; |
77 | } else { |
77 | } else { |
78 | rampedTargetHeight = targetHeight; |
78 | rampedTargetHeight = targetHeight; |
79 | } |
79 | } |
80 | } else { |
80 | } else { |
81 | // descending |
81 | // descending |
82 | if (rampedTargetHeight > targetHeight + staticParams.heightSlewRate) { |
82 | if (rampedTargetHeight > targetHeight + staticParams.heightSlewRate) { |
83 | rampedTargetHeight -= staticParams.heightSlewRate; |
83 | rampedTargetHeight -= staticParams.heightSlewRate; |
84 | } else { |
84 | } else { |
85 | rampedTargetHeight = targetHeight; |
85 | rampedTargetHeight = targetHeight; |
86 | } |
86 | } |
87 | } |
87 | } |
88 | } |
88 | } |
89 | */ |
89 | */ |
90 | // uint8_t heightControlTestOscPeriod; |
90 | // uint8_t heightControlTestOscPeriod; |
91 | // uint8_t heightControlTestOscAmplitude; |
91 | // uint8_t heightControlTestOscAmplitude; |
92 | 92 | ||
93 | testOscPrescaler++; |
93 | hc_testOscPrescaler++; |
94 | if (testOscPrescaler == 488) { |
94 | if (hc_testOscPrescaler == 488) { |
95 | testOscPrescaler = 0; |
95 | hc_testOscPrescaler = 0; |
96 | testOscTimer++; |
96 | hc_testOscTimer++; |
97 | if (testOscTimer == staticParams.heightControlTestOscPeriod) { |
97 | if (hc_testOscTimer == staticParams.heightControlTestOscPeriod) { |
98 | testOscTimer = 0; |
98 | hc_testOscTimer = 0; |
99 | if (staticParams.heightControlTestOscAmplitude) |
99 | if (staticParams.heightControlTestOscAmplitude) |
100 | iHeight = 0; |
100 | iHeight = 0; |
101 | } else if (testOscTimer == staticParams.heightControlTestOscPeriod/2) { |
101 | } else if (hc_testOscTimer == staticParams.heightControlTestOscPeriod/2) { |
102 | if (staticParams.heightControlTestOscAmplitude) |
102 | if (staticParams.heightControlTestOscAmplitude) |
103 | iHeight = 0; |
103 | iHeight = 0; |
104 | } |
104 | } |
105 | } |
105 | } |
106 | 106 | ||
107 | if (testOscTimer < staticParams.heightControlTestOscPeriod/2) |
107 | if (hc_testOscTimer < staticParams.heightControlTestOscPeriod/2) |
108 | targetHeight = setHeight; |
108 | targetHeight = setHeight; |
109 | else |
109 | else |
110 | targetHeight = setHeight + (uint16_t)staticParams.heightControlTestOscAmplitude * 25L; |
110 | targetHeight = setHeight + (uint16_t)staticParams.heightControlTestOscAmplitude * 25L; |
111 | 111 | ||
112 | //if (staticParams.) |
112 | //if (staticParams.) |
113 | // height, in meters (so the division factor is: 100) |
113 | // height, in meters (so the division factor is: 100) |
114 | // debugOut.analog[24] = (117100 - filteredAirPressure) / 100; |
114 | // debugOut.analog[24] = (117100 - filteredAirPressure) / 100; |
115 | // Calculated 0 alt number: 108205 |
115 | // Calculated 0 alt number: 108205 |
116 | // Experimental 0 alt number: 117100 |
116 | // Experimental 0 alt number: 117100 |
117 | } |
117 | } |
118 | 118 | ||
119 | #define LOG_PHEIGHT_SCALE 10 |
119 | #define LOG_PHEIGHT_SCALE 10 |
120 | #define LOG_IHEIGHT_SCALE 24 |
120 | #define LOG_IHEIGHT_SCALE 24 |
121 | #define LOG_DHEIGHT_SCALE 6 |
121 | #define LOG_DHEIGHT_SCALE 6 |
122 | 122 | ||
123 | // takes 180-200 usec (with integral term). That is too heavy!!! |
123 | // takes 180-200 usec (with integral term). That is too heavy!!! |
124 | // takes 100 usec without integral term. |
124 | // takes 100 usec without integral term. |
125 | void HC_periodicTaskAndPRTY(int16_t* PRTY) { |
125 | void HC_periodicTaskAndPRTY(int16_t* PRTY) { |
126 | HC_periodicTask(); |
126 | HC_periodicTask(); |
127 | int16_t throttle = PRTY[CONTROL_THROTTLE]; |
127 | int16_t throttle = PRTY[CONTROL_THROTTLE]; |
128 | int32_t height = analog_getHeight(); |
128 | int32_t height = analog_getHeight(); |
129 | int32_t heightError = targetHeight - height; |
129 | int32_t heightError = targetHeight - height; |
130 | int16_t dHeight = analog_getDHeight(); |
130 | int16_t dHeight = analog_getDHeight(); |
131 | 131 | ||
132 | debugOut.analog[22] = height/10L; |
132 | debugOut.analog[22] = height/10L; |
133 | debugOut.analog[23] = dHeight; |
133 | debugOut.analog[23] = dHeight; |
134 | 134 | ||
135 | if (heightError > 0) { |
135 | if (heightError > 0) { |
136 | debugOut.digital[0] |= DEBUG_HEIGHT_DIFF; |
136 | debugOut.digital[0] |= DEBUG_HEIGHT_DIFF; |
137 | } else { |
137 | } else { |
138 | debugOut.digital[0] &= ~DEBUG_HEIGHT_DIFF; |
138 | debugOut.digital[0] &= ~DEBUG_HEIGHT_DIFF; |
139 | } |
139 | } |
140 | 140 | ||
141 | if (dHeight > 0) { |
141 | if (dHeight > 0) { |
142 | debugOut.digital[1] |= DEBUG_HEIGHT_DIFF; |
142 | debugOut.digital[1] |= DEBUG_HEIGHT_DIFF; |
143 | } else { |
143 | } else { |
144 | debugOut.digital[1] &= ~DEBUG_HEIGHT_DIFF; |
144 | debugOut.digital[1] &= ~DEBUG_HEIGHT_DIFF; |
145 | } |
145 | } |
146 | 146 | ||
147 | int32_t heightErrorForIntegral = heightError; |
147 | int32_t heightErrorForIntegral = heightError; |
148 | int32_t heightErrorForIntegralLimit = staticParams.heightControlMaxIntegralIn << LOG_PHEIGHT_SCALE; |
148 | int32_t heightErrorForIntegralLimit = staticParams.heightControlMaxIntegralIn << LOG_PHEIGHT_SCALE; |
149 | 149 | ||
150 | if (heightErrorForIntegral > heightErrorForIntegralLimit) { |
150 | if (heightErrorForIntegral > heightErrorForIntegralLimit) { |
151 | heightErrorForIntegral = heightErrorForIntegralLimit; |
151 | heightErrorForIntegral = heightErrorForIntegralLimit; |
152 | } else if (heightErrorForIntegral < -heightErrorForIntegralLimit) { |
152 | } else if (heightErrorForIntegral < -heightErrorForIntegralLimit) { |
153 | heightErrorForIntegral =- heightErrorForIntegralLimit; |
153 | heightErrorForIntegral =- heightErrorForIntegralLimit; |
154 | } |
154 | } |
155 | 155 | ||
156 | // iHeight, at a difference of 5 meters and a freq. of 488 Hz, will grow with 244000 / sec.... |
156 | // iHeight, at a difference of 5 meters and a freq. of 488 Hz, will grow with 244000 / sec.... |
157 | iHeight += heightErrorForIntegral; |
157 | iHeight += heightErrorForIntegral; |
158 | 158 | ||
159 | #define IHEIGHT_SCALE 24 |
159 | #define IHEIGHT_SCALE 24 |
160 | // dThrottle is in the range between +/- 1<<(IHEIGHT_SCALE+8)>>(IHEIGHT_SCALE) = +/- 256 |
160 | // dThrottle is in the range between +/- 1<<(IHEIGHT_SCALE+8)>>(IHEIGHT_SCALE) = +/- 256 |
161 | int16_t dThrottleI = (iHeight * (int32_t)dynamicParams.heightI) >> (IHEIGHT_SCALE); |
161 | int16_t dThrottleI = (iHeight * (int32_t)dynamicParams.heightI) >> (IHEIGHT_SCALE); |
162 | 162 | ||
163 | if (dThrottleI > staticParams.heightControlMaxIntegralOut) { |
163 | if (dThrottleI > staticParams.heightControlMaxIntegralOut) { |
164 | dThrottleI = staticParams.heightControlMaxIntegralOut; |
164 | dThrottleI = staticParams.heightControlMaxIntegralOut; |
165 | iHeight = ((int32_t)staticParams.heightControlMaxIntegralOut << IHEIGHT_SCALE) / dynamicParams.heightI; |
165 | iHeight = ((int32_t)staticParams.heightControlMaxIntegralOut << IHEIGHT_SCALE) / dynamicParams.heightI; |
166 | } else if (dThrottleI < -staticParams.heightControlMaxIntegralOut) { |
166 | } else if (dThrottleI < -staticParams.heightControlMaxIntegralOut) { |
167 | dThrottleI = -staticParams.heightControlMaxIntegralOut; |
167 | dThrottleI = -staticParams.heightControlMaxIntegralOut; |
168 | iHeight = -((int32_t)staticParams.heightControlMaxIntegralOut << IHEIGHT_SCALE) / dynamicParams.heightI; |
168 | iHeight = -((int32_t)staticParams.heightControlMaxIntegralOut << IHEIGHT_SCALE) / dynamicParams.heightI; |
169 | } |
169 | } |
170 | 170 | ||
171 | int16_t dThrottleP = (heightError * dynamicParams.heightP) >> LOG_PHEIGHT_SCALE; |
171 | int16_t dThrottleP = (heightError * dynamicParams.heightP) >> LOG_PHEIGHT_SCALE; |
172 | int16_t dThrottleD = (dHeight * dynamicParams.heightD) >> LOG_DHEIGHT_SCALE; |
172 | int16_t dThrottleD = (dHeight * dynamicParams.heightD) >> LOG_DHEIGHT_SCALE; |
173 | 173 | ||
174 | debugOut.analog[10] = dThrottleP; |
174 | debugOut.analog[10] = dThrottleP; |
175 | debugOut.analog[11] = dThrottleI; |
175 | debugOut.analog[11] = dThrottleI; |
176 | debugOut.analog[12] = dThrottleD; |
176 | debugOut.analog[12] = dThrottleD; |
177 | debugOut.analog[13] = heightError/10; |
177 | debugOut.analog[13] = heightError/10; |
178 | 178 | ||
179 | //debugOut.analog[27] = dynamicParams.heightP; |
179 | //debugOut.analog[27] = dynamicParams.heightP; |
180 | //debugOut.analog[28] = dynamicParams.heightI; |
180 | //debugOut.analog[28] = dynamicParams.heightI; |
181 | //debugOut.analog[29] = dynamicParams.heightD; |
181 | //debugOut.analog[29] = dynamicParams.heightD; |
182 | 182 | ||
183 | int16_t dThrottle = dThrottleI + dThrottleP - dThrottleD; |
183 | int16_t dThrottle = dThrottleI + dThrottleP - dThrottleD; |
184 | 184 | ||
185 | if (dThrottle > staticParams.heightControlMaxThrottleChange) |
185 | if (dThrottle > staticParams.heightControlMaxThrottleChange) |
186 | dThrottle = staticParams.heightControlMaxThrottleChange; |
186 | dThrottle = staticParams.heightControlMaxThrottleChange; |
187 | else if (dThrottle < -staticParams.heightControlMaxThrottleChange) |
187 | else if (dThrottle < -staticParams.heightControlMaxThrottleChange) |
188 | dThrottle = -staticParams.heightControlMaxThrottleChange; |
188 | dThrottle = -staticParams.heightControlMaxThrottleChange; |
189 | 189 | ||
190 | /* |
190 | /* |
191 | debugOut.analog[19] = throttle; |
191 | debugOut.analog[19] = throttle; |
192 | debugOut.analog[20] = dThrottle; |
192 | debugOut.analog[20] = dThrottle; |
193 | debugOut.analog[21] = height; |
193 | debugOut.analog[21] = height; |
194 | debugOut.analog[22] = rampedTargetHeight; |
194 | debugOut.analog[22] = rampedTargetHeight; |
195 | debugOut.analog[23] = heightError; |
195 | debugOut.analog[23] = heightError; |
196 | */ |
196 | */ |
197 | 197 | ||
198 | if (staticParams.bitConfig & CFG_SIMPLE_HEIGHT_CONTROL) { |
198 | if (staticParams.bitConfig & CFG_SIMPLE_HEIGHT_CONTROL) { |
199 | if (!(staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) || HC_isSwitchOn()) { |
199 | if (!(staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) || HC_isSwitchOn()) { |
200 | // If switch is not in use --> Just apply height control. |
200 | // If switch is not in use --> Just apply height control. |
201 | // If switch is in use --> only apply height control when switch is also ON. |
201 | // If switch is in use --> only apply height control when switch is also ON. |
202 | throttle += dThrottle; |
202 | throttle += dThrottle; |
203 | } |
203 | } |
204 | } |
204 | } |
205 | 205 | ||
206 | /* Experiment: Find hover-throttle */ |
206 | /* Experiment: Find hover-throttle */ |
207 | 207 | ||
208 | #define DEFAULT_HOVERTHROTTLE 50 |
208 | #define DEFAULT_HOVERTHROTTLE 50 |
209 | int32_t stronglyFilteredHeightDiff = 0; |
209 | int32_t stronglyFilteredHeightDiff = 0; |
210 | // uint16_t hoverThrottle = 0; // DEFAULT_HOVERTHROTTLE; |
210 | // uint16_t hoverThrottle = 0; // DEFAULT_HOVERTHROTTLE; |
211 | uint16_t stronglyFilteredThrottle = DEFAULT_HOVERTHROTTLE; |
211 | uint16_t stronglyFilteredThrottle = DEFAULT_HOVERTHROTTLE; |
212 | #define HOVERTHROTTLEFILTER 25 |
212 | #define HOVERTHROTTLEFILTER 25 |
213 | 213 | ||
214 | stronglyFilteredHeightDiff = (stronglyFilteredHeightDiff |
214 | stronglyFilteredHeightDiff = (stronglyFilteredHeightDiff |
215 | * (HOVERTHROTTLEFILTER - 1) + dHeight) / HOVERTHROTTLEFILTER; |
215 | * (HOVERTHROTTLEFILTER - 1) + dHeight) / HOVERTHROTTLEFILTER; |
216 | stronglyFilteredThrottle = (stronglyFilteredThrottle * (HOVERTHROTTLEFILTER |
216 | stronglyFilteredThrottle = (stronglyFilteredThrottle * (HOVERTHROTTLEFILTER |
217 | - 1) + throttle) / HOVERTHROTTLEFILTER; |
217 | - 1) + throttle) / HOVERTHROTTLEFILTER; |
218 | 218 | ||
219 | /* |
219 | /* |
220 | if (isFlying >= 1000 && stronglyFilteredHeightDiff < 3 |
220 | if (isFlying >= 1000 && stronglyFilteredHeightDiff < 3 |
221 | && stronglyFilteredHeightDiff > -3) { |
221 | && stronglyFilteredHeightDiff > -3) { |
222 | hoverThrottle = stronglyFilteredThrottle; |
222 | hoverThrottle = stronglyFilteredThrottle; |
223 | debugOut.digital[0] |= DEBUG_HOVERTHROTTLE; |
223 | debugOut.digital[0] |= DEBUG_HOVERTHROTTLE; |
224 | } else |
224 | } else |
225 | debugOut.digital[0] &= ~DEBUG_HOVERTHROTTLE; |
225 | debugOut.digital[0] &= ~DEBUG_HOVERTHROTTLE; |
226 | 226 | ||
227 | */ |
227 | */ |
228 | 228 | ||
229 | PRTY[CONTROL_THROTTLE] = throttle; |
229 | PRTY[CONTROL_THROTTLE] = throttle; |
230 | } |
230 | } |
231 | 231 | ||
232 | /* |
232 | /* |
233 | For a variometer thingy: |
233 | For a variometer thingy: |
234 | When switch is thrown on, freeze throttle (capture it into variable) |
234 | When switch is thrown on, freeze throttle (capture it into variable) |
235 | For each iter., add (throttle - frozen throttle) to target height. Maybe don't do ramping. |
235 | For each iter., add (throttle - frozen throttle) to target height. Maybe don't do ramping. |
236 | Output = frozen throttle + whatever is computed +/-. Integral? |
236 | Output = frozen throttle + whatever is computed +/-. Integral? |
237 | */ |
237 | */ |
238 | 238 |