|
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;
|
@@ -255,6 +249,7 @@ bool flysky_gimbal_init(bool force)
|
255 | 249 | _fs_gimbal_detected = force;
|
256 | 250 | _fs_gimbal_version = GIMBAL_V1;
|
257 | 251 | _fs_gimbal_mode = V1_MODE;
|
| 252 | + _fs_gimbal_mode_detected = V1_MODE; |
258 | 253 | _fs_gimbal_last_mode_change_tick = 0;
|
259 | 254 | _fs_gimbal_cmd_finished = true;
|
260 | 255 | _fs_gimbal_lastReadTick = 0;
|
@@ -282,30 +277,42 @@ bool flysky_gimbal_init(bool force)
|
282 | 277 |
|
283 | 278 | void flysky_gimbal_start_read()
|
284 | 279 | {
|
285 |
| - if(_fs_sync_enabled()) { |
| 280 | + if(_fs_gimbal_detected && _fs_gimbal_version > GIMBAL_V1) { |
286 | 281 | _fs_gimbal_lastReadTick = _fs_gimbal_readTick;
|
287 | 282 | _fs_gimbal_readTick = timersGetUsTick();
|
288 | 283 | if (_fs_gimbal_lastReadTick != 0) {
|
289 | 284 | _fs_gimbal_sync_period = _fs_gimbal_readTick - _fs_gimbal_lastReadTick;
|
290 |
| - uint32_t tick = timersGetMsTick(); |
291 |
| - if (tick - _fs_gimbal_last_mode_change_tick >= MODE_CHANGE_DELAY) { |
292 |
| - // Prevent mode change too often |
293 |
| - _fs_gimbal_last_mode_change_tick = tick; |
294 |
| - if (_fs_gimbal_mode == SYNC_SAMPLING && _fs_gimbal_sync_period < RESAMPLING_THRESHOLD) { |
295 |
| - _fs_cmd_set_mode(SYNC_RESAMPLING); |
296 |
| - } else if(_fs_gimbal_mode == SYNC_RESAMPLING && _fs_gimbal_sync_period >= RESAMPLING_THRESHOLD + RESAMPLING_SWITCHING_THRESHOLD) { |
297 |
| - _fs_cmd_set_mode(SYNC_SAMPLING); |
| 285 | + V2_GIMBAL_MODE newMode = _fs_gimbal_mode_detected; |
| 286 | + switch (_fs_gimbal_mode_detected) { |
| 287 | + case V1_MODE: |
| 288 | + case SYNC_SAMPLING: |
| 289 | + newMode = _fs_gimbal_sync_period < RESAMPLING_THRESHOLD ? SYNC_RESAMPLING : SYNC_SAMPLING; |
| 290 | + break; |
| 291 | + case SYNC_RESAMPLING: |
| 292 | + newMode = _fs_gimbal_sync_period >= RESAMPLING_THRESHOLD + RESAMPLING_SWITCHING_THRESHOLD ? SYNC_SAMPLING : SYNC_RESAMPLING; |
| 293 | + break; |
| 294 | + } |
| 295 | + if (_fs_gimbal_mode_detected != newMode) { |
| 296 | + _fs_gimbal_mode_detected = newMode; |
| 297 | + _fs_gimbal_last_mode_change_tick = timersGetMsTick(); |
| 298 | + } else if (_fs_gimbal_mode != _fs_gimbal_mode_detected) { |
| 299 | + uint32_t tick = timersGetMsTick(); |
| 300 | + if (tick - _fs_gimbal_last_mode_change_tick >= MODE_CHANGE_DELAY) { |
| 301 | + // Update mode when mode is stable |
| 302 | + _fs_cmd_set_mode(_fs_gimbal_mode_detected); |
298 | 303 | }
|
299 | 304 | }
|
300 | 305 | }
|
301 |
| - _fs_cmd_start_read(); |
| 306 | + if (_fs_gimbal_mode != V1_MODE) { |
| 307 | + _fs_cmd_start_read(); |
| 308 | + } |
302 | 309 | }
|
303 | 310 | }
|
304 | 311 |
|
305 | 312 | void flysky_gimbal_wait_completion()
|
306 | 313 | {
|
307 |
| - if(_fs_sync_enabled()) { |
308 |
| - auto timeout = timersGetUsTick(); |
| 314 | + if(_fs_gimbal_detected && _fs_gimbal_version > GIMBAL_V1 && _fs_gimbal_mode != V1_MODE) { |
| 315 | + auto timeout = timersGetUsTick(); |
309 | 316 | while(!_fs_gimbal_cmd_finished) {
|
310 | 317 | // busy wait
|
311 | 318 | if ((uint32_t)(timersGetUsTick() - timeout) >= SAMPLING_TIMEOUT_US) {
|
|
0 commit comments