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