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