|
39 | 39 | #define SAMPLING_TIMEOUT_US 1000 // us
|
40 | 40 | #define RESAMPLING_THRESHOLD 2500 // us, 400Hz freq = 2500us period
|
41 | 41 | #define RESAMPLING_SWITCHING_THRESHOLD 200 // us
|
42 |
| -#define MODE_CHANGE_DELAY 100 // ms |
| 42 | +#define MODE_CHANGE_DELAY 50 // ms |
43 | 43 |
|
44 | 44 | static const stm32_usart_t fsUSART = {
|
45 | 45 | .USARTx = FLYSKY_HALL_SERIAL_USART,
|
@@ -72,13 +72,14 @@ static void* _fs_usart_ctx = nullptr;
|
72 | 72 |
|
73 | 73 | static volatile bool _fs_gimbal_detected;
|
74 | 74 | static uint8_t _fs_gimbal_version;
|
75 |
| -static uint8_t _fs_gimbal_mode; |
76 |
| -static uint8_t _fs_gimbal_mode_change; |
| 75 | +static V2_GIMBAL_MODE _fs_gimbal_mode; |
| 76 | +static V2_GIMBAL_MODE _fs_gimbal_mode_change; |
| 77 | +static V2_GIMBAL_MODE _fs_gimbal_mode_detected; |
77 | 78 | static uint32_t _fs_gimbal_last_mode_change_tick;
|
78 | 79 | static bool _fs_gimbal_cmd_finished;
|
79 | 80 | static uint32_t _fs_gimbal_lastReadTick;
|
80 | 81 | static uint32_t _fs_gimbal_readTick;
|
81 |
| -static volatile uint32_t _fs_gimbal_sync_period; |
| 82 | +static uint32_t _fs_gimbal_sync_period; |
82 | 83 |
|
83 | 84 | static int _fs_get_byte(uint8_t* data)
|
84 | 85 | {
|
@@ -186,11 +187,6 @@ void _fs_cmd_start_read()
|
186 | 187 | _fs_send_cmd(0xc1, 0x00);
|
187 | 188 | }
|
188 | 189 |
|
189 |
| -bool _fs_sync_enabled() |
190 |
| -{ |
191 |
| - return _fs_gimbal_detected && _fs_gimbal_version > GIMBAL_V1 && _fs_gimbal_mode != V1_MODE; |
192 |
| -} |
193 |
| - |
194 | 190 | static void flysky_gimbal_loop(void*)
|
195 | 191 | {
|
196 | 192 | uint8_t byte;
|
@@ -219,8 +215,6 @@ static void flysky_gimbal_loop(void*)
|
219 | 215 | uint16_t majorVersion = p_values[7];
|
220 | 216 | if (majorVersion == 2 && minorVersion >= 1) {
|
221 | 217 | _fs_gimbal_version = GIMBAL_V2;
|
222 |
| - // Enable sync mode |
223 |
| - _fs_cmd_set_mode(SYNC_RESAMPLING); |
224 | 218 | }
|
225 | 219 | } else if (HallProtocol.hallID.hall_Id.packetID == FLYSKY_PACKET_MODE_ID) {
|
226 | 220 | _fs_gimbal_cmd_finished = true;
|
@@ -252,6 +246,7 @@ bool flysky_gimbal_init()
|
252 | 246 | _fs_gimbal_detected = false;
|
253 | 247 | _fs_gimbal_version = GIMBAL_V1;
|
254 | 248 | _fs_gimbal_mode = V1_MODE;
|
| 249 | + _fs_gimbal_mode_detected = V1_MODE; |
255 | 250 | _fs_gimbal_last_mode_change_tick = 0;
|
256 | 251 | _fs_gimbal_cmd_finished = true;
|
257 | 252 | _fs_gimbal_lastReadTick = 0;
|
@@ -279,30 +274,42 @@ bool flysky_gimbal_init()
|
279 | 274 |
|
280 | 275 | void flysky_gimbal_start_read()
|
281 | 276 | {
|
282 |
| - if(_fs_sync_enabled()) { |
| 277 | + if(_fs_gimbal_detected && _fs_gimbal_version > GIMBAL_V1) { |
283 | 278 | _fs_gimbal_lastReadTick = _fs_gimbal_readTick;
|
284 | 279 | _fs_gimbal_readTick = timersGetUsTick();
|
285 | 280 | if (_fs_gimbal_lastReadTick != 0) {
|
286 | 281 | _fs_gimbal_sync_period = _fs_gimbal_readTick - _fs_gimbal_lastReadTick;
|
287 |
| - uint32_t tick = timersGetMsTick(); |
288 |
| - if (tick - _fs_gimbal_last_mode_change_tick >= MODE_CHANGE_DELAY) { |
289 |
| - // Prevent mode change too often |
290 |
| - _fs_gimbal_last_mode_change_tick = tick; |
291 |
| - if (_fs_gimbal_mode == SYNC_SAMPLING && _fs_gimbal_sync_period < RESAMPLING_THRESHOLD) { |
292 |
| - _fs_cmd_set_mode(SYNC_RESAMPLING); |
293 |
| - } else if(_fs_gimbal_mode == SYNC_RESAMPLING && _fs_gimbal_sync_period >= RESAMPLING_THRESHOLD + RESAMPLING_SWITCHING_THRESHOLD) { |
294 |
| - _fs_cmd_set_mode(SYNC_SAMPLING); |
| 282 | + V2_GIMBAL_MODE newMode = _fs_gimbal_mode_detected; |
| 283 | + switch (_fs_gimbal_mode_detected) { |
| 284 | + case V1_MODE: |
| 285 | + case SYNC_SAMPLING: |
| 286 | + newMode = _fs_gimbal_sync_period < RESAMPLING_THRESHOLD ? SYNC_RESAMPLING : SYNC_SAMPLING; |
| 287 | + break; |
| 288 | + case SYNC_RESAMPLING: |
| 289 | + newMode = _fs_gimbal_sync_period >= RESAMPLING_THRESHOLD + RESAMPLING_SWITCHING_THRESHOLD ? SYNC_SAMPLING : SYNC_RESAMPLING; |
| 290 | + break; |
| 291 | + } |
| 292 | + if (_fs_gimbal_mode_detected != newMode) { |
| 293 | + _fs_gimbal_mode_detected = newMode; |
| 294 | + _fs_gimbal_last_mode_change_tick = timersGetMsTick(); |
| 295 | + } else if (_fs_gimbal_mode != _fs_gimbal_mode_detected) { |
| 296 | + uint32_t tick = timersGetMsTick(); |
| 297 | + if (tick - _fs_gimbal_last_mode_change_tick >= MODE_CHANGE_DELAY) { |
| 298 | + // Update mode when mode is stable |
| 299 | + _fs_cmd_set_mode(_fs_gimbal_mode_detected); |
295 | 300 | }
|
296 | 301 | }
|
297 | 302 | }
|
298 |
| - _fs_cmd_start_read(); |
| 303 | + if (_fs_gimbal_mode != V1_MODE) { |
| 304 | + _fs_cmd_start_read(); |
| 305 | + } |
299 | 306 | }
|
300 | 307 | }
|
301 | 308 |
|
302 | 309 | void flysky_gimbal_wait_completion()
|
303 | 310 | {
|
304 |
| - if(_fs_sync_enabled()) { |
305 |
| - auto timeout = timersGetUsTick(); |
| 311 | + if(_fs_gimbal_detected && _fs_gimbal_version > GIMBAL_V1 && _fs_gimbal_mode != V1_MODE) { |
| 312 | + auto timeout = timersGetUsTick(); |
306 | 313 | while(!_fs_gimbal_cmd_finished) {
|
307 | 314 | // busy wait
|
308 | 315 | if ((uint32_t)(timersGetUsTick() - timeout) >= SAMPLING_TIMEOUT_US) {
|
|
0 commit comments