Rev 1986 | Rev 2027 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1775 | - | 1 | #include <inttypes.h> |
2 | #include "attitude.h" |
||
3 | #include "uart0.h" |
||
4 | #include "configuration.h" |
||
5 | |||
6 | // for digital / LED debug. |
||
7 | #include "output.h" |
||
8 | |||
9 | // For scope debugging only! |
||
10 | #include "rc.h" |
||
11 | |||
12 | #define INTEGRAL_LIMIT 100000 |
||
13 | |||
14 | /* |
||
1821 | - | 15 | #define DEBUGINTEGRAL 0 |
16 | #define DEBUGDIFFERENTIAL 0 |
||
17 | #define DEBUGHOVERTHROTTLE 0 |
||
18 | #define DEBUGHEIGHTSWITCH 0 |
||
19 | */ |
||
1775 | - | 20 | |
21 | #define LATCH_TIME 40 |
||
22 | |||
23 | int32_t groundPressure; |
||
24 | int32_t targetHeight; |
||
25 | int32_t rampedTargetHeight; |
||
26 | |||
27 | uint8_t heightRampingTimer = 0; |
||
1961 | - | 28 | int32_t maxHeightThisFlight; |
1775 | - | 29 | int32_t iHeight; |
30 | |||
31 | int32_t getHeight(void) { |
||
1960 | - | 32 | return groundPressure - filteredAirPressure; |
1775 | - | 33 | } |
34 | |||
35 | void HC_setGround(void) { |
||
1960 | - | 36 | groundPressure = filteredAirPressure; |
37 | // This should also happen when height control is enabled in-flight. |
||
38 | rampedTargetHeight = getHeight(); |
||
1961 | - | 39 | maxHeightThisFlight = 0; |
1960 | - | 40 | iHeight = 0; |
1775 | - | 41 | } |
42 | |||
43 | void HC_update(void) { |
||
1960 | - | 44 | int32_t height = getHeight(); |
45 | static uint8_t setHeightLatch = 0; |
||
46 | |||
1961 | - | 47 | if (height > maxHeightThisFlight) |
48 | maxHeightThisFlight = height; |
||
1960 | - | 49 | |
1961 | - | 50 | if (staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) { |
1960 | - | 51 | // If switch is activated in config, the MaxHeight parameter is a switch value: ON in both ends of the range; OFF in the middle. |
52 | if (dynamicParams.heightSetting < 40 || dynamicParams.heightSetting > 255 - 40) { |
||
53 | // Switch is ON |
||
54 | if (setHeightLatch <= LATCH_TIME) { |
||
55 | if (setHeightLatch == LATCH_TIME) { |
||
56 | // Freeze the height as target. We want to do this exactly once each time the switch is thrown ON. |
||
57 | targetHeight = height; |
||
1775 | - | 58 | } |
1960 | - | 59 | // Time not yet reached. |
60 | setHeightLatch++; |
||
61 | } |
||
62 | } else { |
||
63 | // Switch is OFF. |
||
64 | setHeightLatch = 0; |
||
65 | } |
||
66 | } else { |
||
67 | // Switch is not activated; take the "max-height" as the target height. |
||
2026 | - | 68 | targetHeight = (uint16_t) dynamicParams.heightSetting * 5L - 500L; // should be: 100 (or make a param out of it) |
1960 | - | 69 | } |
70 | |||
71 | if (++heightRampingTimer == INTEGRATION_FREQUENCY / 10) { |
||
72 | heightRampingTimer = 0; |
||
1961 | - | 73 | if (rampedTargetHeight < targetHeight) { |
74 | // climbing |
||
75 | if (rampedTargetHeight < targetHeight - staticParams.heightSlewRate) { |
||
76 | rampedTargetHeight += staticParams.heightSlewRate; |
||
77 | } else { |
||
78 | rampedTargetHeight = targetHeight; |
||
79 | } |
||
2026 | - | 80 | } else { |
1961 | - | 81 | // descending |
82 | if (rampedTargetHeight > targetHeight + staticParams.heightSlewRate) { |
||
83 | rampedTargetHeight -= staticParams.heightSlewRate; |
||
84 | } else { |
||
85 | rampedTargetHeight = targetHeight; |
||
86 | } |
||
1960 | - | 87 | } |
88 | } |
||
1970 | - | 89 | |
1960 | - | 90 | // height, in meters (so the division factor is: 100) |
1970 | - | 91 | // debugOut.analog[30] = filteredAirPressure / 10; |
1980 | - | 92 | debugOut.analog[30] = (117100 - filteredAirPressure) / 100; |
1971 | - | 93 | // Calculated 0 alt number: 108205 |
1980 | - | 94 | // Experimental 0 alt number: 117100 |
1775 | - | 95 | } |
96 | |||
97 | // takes 180-200 usec (with integral term). That is too heavy!!! |
||
98 | // takes 100 usec without integral term. |
||
99 | uint16_t HC_getThrottle(uint16_t throttle) { |
||
1960 | - | 100 | int32_t height = getHeight(); |
101 | int32_t heightError = rampedTargetHeight - height; |
||
2026 | - | 102 | |
1960 | - | 103 | static int32_t lastHeight; |
104 | |||
105 | int16_t dHeight = height - lastHeight; |
||
106 | lastHeight = height; |
||
107 | |||
108 | // DebugOut.Analog[20] = dHeight; |
||
109 | // DebugOut.Analog[21] = dynamicParams.MaxHeight; |
||
110 | |||
111 | // iHeight, at a difference of 5 meters and a freq. of 488 Hz, will grow with 244000 / sec.... |
||
112 | // iHeight += heightError; |
||
113 | |||
2026 | - | 114 | if (heightError > 0) { |
1960 | - | 115 | debugOut.digital[0] |= DEBUG_HEIGHT_DIFF; |
116 | debugOut.digital[1] &= ~DEBUG_HEIGHT_DIFF; |
||
2026 | - | 117 | } else if (heightError < 0) { |
1961 | - | 118 | debugOut.digital[0] &= ~DEBUG_HEIGHT_DIFF; |
1960 | - | 119 | debugOut.digital[1] |= DEBUG_HEIGHT_DIFF; |
120 | } |
||
121 | |||
122 | /* |
||
123 | if (iHeight > INTEGRAL_LIMIT) { iHeight = INTEGRAL_LIMIT; if (DEBUGINTEGRAL) {DebugOut.Digital[0] = 1; DebugOut.Digital[1] = 1;}} |
||
124 | else if (iHeight < -INTEGRAL_LIMIT) { iHeight = -INTEGRAL_LIMIT; if (DEBUGINTEGRAL) {DebugOut.Digital[0] = 0; DebugOut.Digital[1] = 0; }} |
||
125 | else if (iHeight > 0) { if (DEBUGINTEGRAL) DebugOut.Digital[0] = 1;} |
||
126 | else if (iHeight < 0) { if (DEBUGINTEGRAL) DebugOut.Digital[1] = 1;} |
||
127 | */ |
||
128 | |||
2026 | - | 129 | int16_t dThrottle = ((heightError * staticParams.heightP) >> 9) |
130 | /*+ iHeight / 10000L * staticParams.Height_ACC_Effect */-((dHeight * staticParams.heightD) >> 7); |
||
1960 | - | 131 | |
1961 | - | 132 | if (dThrottle > staticParams.heightControlMaxThrottleChange) |
133 | dThrottle = staticParams.heightControlMaxThrottleChange; |
||
134 | else if (dThrottle < -staticParams.heightControlMaxThrottleChange) |
||
135 | dThrottle = -staticParams.heightControlMaxThrottleChange; |
||
2026 | - | 136 | |
137 | debugOut.analog[19] = rampedTargetHeight; |
||
138 | debugOut.analog[21] = dThrottle; |
||
139 | debugOut.analog[26] = height; |
||
140 | |||
1961 | - | 141 | if (staticParams.bitConfig & CFG_SIMPLE_HEIGHT_CONTROL) { |
142 | if (!(staticParams.bitConfig & CFG_SIMPLE_HC_HOLD_SWITCH) |
||
1960 | - | 143 | || (dynamicParams.heightSetting < 40 || dynamicParams.heightSetting > 255 - 40)) { |
144 | // If switch is not in use --> Just apply height control. |
||
145 | // If switch is in use --> only apply height control when switch is also ON. |
||
146 | throttle += dThrottle; |
||
147 | } |
||
148 | } |
||
1961 | - | 149 | |
150 | /* Experiment: Find hover-throttle */ |
||
151 | |||
152 | #define DEFAULT_HOVERTHROTTLE 50 |
||
153 | int32_t stronglyFilteredHeightDiff = 0; |
||
154 | uint16_t hoverThrottle = 0; // DEFAULT_HOVERTHROTTLE; |
||
155 | uint16_t stronglyFilteredThrottle = DEFAULT_HOVERTHROTTLE; |
||
156 | #define HOVERTHROTTLEFILTER 25 |
||
1960 | - | 157 | |
158 | stronglyFilteredHeightDiff = (stronglyFilteredHeightDiff |
||
159 | * (HOVERTHROTTLEFILTER - 1) + dHeight) / HOVERTHROTTLEFILTER; |
||
160 | stronglyFilteredThrottle = (stronglyFilteredThrottle * (HOVERTHROTTLEFILTER |
||
161 | - 1) + throttle) / HOVERTHROTTLEFILTER; |
||
162 | |||
163 | if (isFlying >= 1000 && stronglyFilteredHeightDiff < 3 |
||
164 | && stronglyFilteredHeightDiff > -3) { |
||
165 | hoverThrottle = stronglyFilteredThrottle; |
||
166 | debugOut.digital[0] |= DEBUG_HOVERTHROTTLE; |
||
167 | // DebugOut.Analog[18] = hoverThrottle; |
||
168 | } else |
||
169 | debugOut.digital[0] &= ~DEBUG_HOVERTHROTTLE; |
||
170 | |||
1986 | - | 171 | //debugOut.analog[20] = dThrottle; |
1960 | - | 172 | |
173 | return throttle; |
||
1775 | - | 174 | } |
175 | |||
176 | /* |
||
1821 | - | 177 | For a variometer thingy: |
178 | When switch is thrown on, freeze throttle (capture it into variable) |
||
179 | For each iter., add (throttle - frozen throttle) to target height. Maybe don't do ramping. |
||
180 | Output = frozen throttle + whatever is computed +/-. Integral? |
||
181 | */ |