44
44
using namespace time_literals ;
45
45
46
46
/* Configuration Constants */
47
- # define SF45_SCALE_FACTOR 0 . 01f
47
+
48
48
49
49
SF45LaserSerial::SF45LaserSerial (const char *port) :
50
50
ScheduledWorkItem(MODULE_NAME, px4::serial_port_to_wq(port)),
@@ -346,72 +346,71 @@ void SF45LaserSerial::sf45_get_and_handle_request(const int payload_length, cons
346
346
347
347
while (restart_flag != true ) {
348
348
switch (_parsed_state) {
349
- case 0 : {
349
+ case SF45_PARSED_STATE::START : {
350
350
if (_linebuf[index ] == 0xAA ) {
351
351
// start of frame is valid, continue
352
352
_sop_valid = true ;
353
353
_calc_crc = sf45_format_crc (_calc_crc, _start_of_frame);
354
- _parsed_state = 1 ;
354
+ _parsed_state = SF45_PARSED_STATE::FLG_LOW ;
355
355
break ;
356
356
357
357
} else {
358
358
_sop_valid = false ;
359
359
_crc_valid = false ;
360
- _parsed_state = 0 ;
360
+ _parsed_state = SF45_PARSED_STATE::START ;
361
361
restart_flag = true ;
362
362
_calc_crc = 0 ;
363
363
PX4_DEBUG (" Start of packet not valid: %d" , _sensor_state);
364
364
break ;
365
- } // end else
366
- } // end case 0
365
+ }
366
+ }
367
367
368
- case 1 : {
368
+ case SF45_PARSED_STATE::FLG_LOW : {
369
369
rx_field.flags_lo = _linebuf[index + 1 ];
370
370
_calc_crc = sf45_format_crc (_calc_crc, rx_field.flags_lo );
371
- _parsed_state = 2 ;
371
+ _parsed_state = SF45_PARSED_STATE::FLG_HIGH ;
372
372
break ;
373
373
}
374
374
375
- case 2 : {
375
+ case SF45_PARSED_STATE::FLG_HIGH : {
376
376
rx_field.flags_hi = _linebuf[index + 2 ];
377
377
rx_field.data_len = (rx_field.flags_hi << 2 ) | (rx_field.flags_lo >> 6 );
378
378
_calc_crc = sf45_format_crc (_calc_crc, rx_field.flags_hi );
379
379
380
380
// Check payload length against known max value
381
381
if (rx_field.data_len > 17 ) {
382
- _parsed_state = 0 ;
382
+ _parsed_state = SF45_PARSED_STATE::START ;
383
383
restart_flag = true ;
384
384
_calc_crc = 0 ;
385
385
PX4_DEBUG (" Payload length error: %d" , _sensor_state);
386
386
break ;
387
387
388
388
} else {
389
- _parsed_state = 3 ;
389
+ _parsed_state = SF45_PARSED_STATE::ID ;
390
390
break ;
391
391
}
392
392
}
393
393
394
- case 3 : {
394
+ case SF45_PARSED_STATE::ID : {
395
395
rx_field.msg_id = _linebuf[index + 3 ];
396
396
397
397
if (rx_field.msg_id == msg_id) {
398
398
_calc_crc = sf45_format_crc (_calc_crc, rx_field.msg_id );
399
- _parsed_state = 4 ;
399
+ _parsed_state = SF45_PARSED_STATE::DATA ;
400
400
break ;
401
401
}
402
402
403
403
// Ignore message ID's that aren't searched
404
404
else {
405
- _parsed_state = 0 ;
405
+ _parsed_state = SF45_PARSED_STATE::START ;
406
406
_calc_crc = 0 ;
407
407
restart_flag = true ;
408
408
PX4_DEBUG (" Non needed message ID: %d" , _sensor_state);
409
409
break ;
410
410
}
411
411
}
412
412
413
- // Data
414
- case 4 : {
413
+ case SF45_PARSED_STATE::DATA: {
415
414
// Process commands with & w/out data bytes
416
415
if (rx_field.data_len > 1 ) {
417
416
for (uint8_t i = 4 ; i < 3 + rx_field.data_len ; ++i) {
@@ -420,30 +419,28 @@ void SF45LaserSerial::sf45_get_and_handle_request(const int payload_length, cons
420
419
_calc_crc = sf45_format_crc (_calc_crc, rx_field.data [_data_bytes_recv]);
421
420
_data_bytes_recv = _data_bytes_recv + 1 ;
422
421
423
- } // end for
424
- } // end if
422
+ }
423
+ }
425
424
426
425
else {
427
426
428
- _parsed_state = 5 ;
427
+ _parsed_state = SF45_PARSED_STATE::CRC_LOW ;
429
428
_data_bytes_recv = 0 ;
430
429
_calc_crc = sf45_format_crc (_calc_crc, _data_bytes_recv);
431
430
}
432
431
433
- _parsed_state = 5 ;
432
+ _parsed_state = SF45_PARSED_STATE::CRC_LOW ;
434
433
_data_bytes_recv = 0 ;
435
434
break ;
436
435
}
437
436
438
- // CRC low byte
439
- case 5 : {
437
+ case SF45_PARSED_STATE::CRC_LOW: {
440
438
rx_field.crc [0 ] = _linebuf[index + 3 + rx_field.data_len ];
441
- _parsed_state = 6 ;
439
+ _parsed_state = SF45_PARSED_STATE::CRC_HIGH ;
442
440
break ;
443
441
}
444
442
445
- // CRC high byte
446
- case 6 : {
443
+ case SF45_PARSED_STATE::CRC_HIGH: {
447
444
rx_field.crc [1 ] = _linebuf[index + 4 + rx_field.data_len ];
448
445
uint16_t recv_crc = (rx_field.crc [1 ] << 8 ) | rx_field.crc [0 ];
449
446
@@ -452,24 +449,24 @@ void SF45LaserSerial::sf45_get_and_handle_request(const int payload_length, cons
452
449
// Only if crc match is valid and sensor ready (transmitting distance data) do we flag _init_complete
453
450
if (recv_crc == _calc_crc) {
454
451
_crc_valid = true ;
455
- _parsed_state = 0 ;
452
+ _parsed_state = SF45_PARSED_STATE::START ;
456
453
_calc_crc = 0 ;
457
454
restart_flag = true ;
458
455
break ;
459
456
460
457
} else {
461
458
462
459
_crc_valid = false ;
463
- _parsed_state = 0 ;
460
+ _parsed_state = SF45_PARSED_STATE::START ;
464
461
_calc_crc = 0 ;
465
462
restart_flag = true ;
466
463
perf_count (_comms_errors);
467
464
PX4_DEBUG (" CRC mismatch: %d" , _sensor_state);
468
465
break ;
469
466
}
470
467
}
471
- } // end switch
472
- } // end while
468
+ }
469
+ }
473
470
474
471
index ++;
475
472
}
@@ -678,7 +675,7 @@ void SF45LaserSerial::_publish_obstacle_msg(hrt_abstime now)
678
675
{
679
676
// This whole logic is in place because CollisionPrevention, just reads in the last published message on the topic, and not every message,
680
677
// This can result in messages being misssed if the sensor is publishing at a faster rate than the CollisionPrevention module is reading.
681
- for (int i = 0 ; i < BIN_COUNT; i++) {
678
+ for (uint8_t i = 0 ; i < BIN_COUNT; i++) {
682
679
if (now - _data_timestamps[i] > SF45_MEAS_TIMEOUT) {
683
680
// If the data is older than SF45_MEAS_TIMEOUT, we discard the value (UINT16_MAX means no valid data)
684
681
_obstacle_distance.distances [i] = UINT16_MAX;
0 commit comments