Skip to content

Commit cda36c0

Browse files
committed
added parsed state enum
1 parent cfcfd67 commit cda36c0

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

src/drivers/distance_sensor/lightware_sf45_serial/lightware_sf45_serial.cpp

+27-30
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
using namespace time_literals;
4545

4646
/* Configuration Constants */
47-
#define SF45_SCALE_FACTOR 0.01f
47+
4848

4949
SF45LaserSerial::SF45LaserSerial(const char *port) :
5050
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
346346

347347
while (restart_flag != true) {
348348
switch (_parsed_state) {
349-
case 0: {
349+
case SF45_PARSED_STATE::START: {
350350
if (_linebuf[index] == 0xAA) {
351351
// start of frame is valid, continue
352352
_sop_valid = true;
353353
_calc_crc = sf45_format_crc(_calc_crc, _start_of_frame);
354-
_parsed_state = 1;
354+
_parsed_state = SF45_PARSED_STATE::FLG_LOW;
355355
break;
356356

357357
} else {
358358
_sop_valid = false;
359359
_crc_valid = false;
360-
_parsed_state = 0;
360+
_parsed_state = SF45_PARSED_STATE::START;
361361
restart_flag = true;
362362
_calc_crc = 0;
363363
PX4_DEBUG("Start of packet not valid: %d", _sensor_state);
364364
break;
365-
} // end else
366-
} // end case 0
365+
}
366+
}
367367

368-
case 1: {
368+
case SF45_PARSED_STATE::FLG_LOW: {
369369
rx_field.flags_lo = _linebuf[index + 1];
370370
_calc_crc = sf45_format_crc(_calc_crc, rx_field.flags_lo);
371-
_parsed_state = 2;
371+
_parsed_state = SF45_PARSED_STATE::FLG_HIGH;
372372
break;
373373
}
374374

375-
case 2: {
375+
case SF45_PARSED_STATE::FLG_HIGH: {
376376
rx_field.flags_hi = _linebuf[index + 2];
377377
rx_field.data_len = (rx_field.flags_hi << 2) | (rx_field.flags_lo >> 6);
378378
_calc_crc = sf45_format_crc(_calc_crc, rx_field.flags_hi);
379379

380380
// Check payload length against known max value
381381
if (rx_field.data_len > 17) {
382-
_parsed_state = 0;
382+
_parsed_state = SF45_PARSED_STATE::START;
383383
restart_flag = true;
384384
_calc_crc = 0;
385385
PX4_DEBUG("Payload length error: %d", _sensor_state);
386386
break;
387387

388388
} else {
389-
_parsed_state = 3;
389+
_parsed_state = SF45_PARSED_STATE::ID;
390390
break;
391391
}
392392
}
393393

394-
case 3: {
394+
case SF45_PARSED_STATE::ID: {
395395
rx_field.msg_id = _linebuf[index + 3];
396396

397397
if (rx_field.msg_id == msg_id) {
398398
_calc_crc = sf45_format_crc(_calc_crc, rx_field.msg_id);
399-
_parsed_state = 4;
399+
_parsed_state = SF45_PARSED_STATE::DATA;
400400
break;
401401
}
402402

403403
// Ignore message ID's that aren't searched
404404
else {
405-
_parsed_state = 0;
405+
_parsed_state = SF45_PARSED_STATE::START;
406406
_calc_crc = 0;
407407
restart_flag = true;
408408
PX4_DEBUG("Non needed message ID: %d", _sensor_state);
409409
break;
410410
}
411411
}
412412

413-
// Data
414-
case 4: {
413+
case SF45_PARSED_STATE::DATA: {
415414
// Process commands with & w/out data bytes
416415
if (rx_field.data_len > 1) {
417416
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
420419
_calc_crc = sf45_format_crc(_calc_crc, rx_field.data[_data_bytes_recv]);
421420
_data_bytes_recv = _data_bytes_recv + 1;
422421

423-
} // end for
424-
} //end if
422+
}
423+
}
425424

426425
else {
427426

428-
_parsed_state = 5;
427+
_parsed_state = SF45_PARSED_STATE::CRC_LOW;
429428
_data_bytes_recv = 0;
430429
_calc_crc = sf45_format_crc(_calc_crc, _data_bytes_recv);
431430
}
432431

433-
_parsed_state = 5;
432+
_parsed_state = SF45_PARSED_STATE::CRC_LOW;
434433
_data_bytes_recv = 0;
435434
break;
436435
}
437436

438-
// CRC low byte
439-
case 5: {
437+
case SF45_PARSED_STATE::CRC_LOW: {
440438
rx_field.crc[0] = _linebuf[index + 3 + rx_field.data_len];
441-
_parsed_state = 6;
439+
_parsed_state = SF45_PARSED_STATE::CRC_HIGH;
442440
break;
443441
}
444442

445-
// CRC high byte
446-
case 6: {
443+
case SF45_PARSED_STATE::CRC_HIGH: {
447444
rx_field.crc[1] = _linebuf[index + 4 + rx_field.data_len];
448445
uint16_t recv_crc = (rx_field.crc[1] << 8) | rx_field.crc[0];
449446

@@ -452,24 +449,24 @@ void SF45LaserSerial::sf45_get_and_handle_request(const int payload_length, cons
452449
// Only if crc match is valid and sensor ready (transmitting distance data) do we flag _init_complete
453450
if (recv_crc == _calc_crc) {
454451
_crc_valid = true;
455-
_parsed_state = 0;
452+
_parsed_state = SF45_PARSED_STATE::START;
456453
_calc_crc = 0;
457454
restart_flag = true;
458455
break;
459456

460457
} else {
461458

462459
_crc_valid = false;
463-
_parsed_state = 0;
460+
_parsed_state = SF45_PARSED_STATE::START;
464461
_calc_crc = 0;
465462
restart_flag = true;
466463
perf_count(_comms_errors);
467464
PX4_DEBUG("CRC mismatch: %d", _sensor_state);
468465
break;
469466
}
470467
}
471-
} // end switch
472-
} //end while
468+
}
469+
}
473470

474471
index++;
475472
}
@@ -678,7 +675,7 @@ void SF45LaserSerial::_publish_obstacle_msg(hrt_abstime now)
678675
{
679676
// This whole logic is in place because CollisionPrevention, just reads in the last published message on the topic, and not every message,
680677
// 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++) {
682679
if (now - _data_timestamps[i] > SF45_MEAS_TIMEOUT) {
683680
// If the data is older than SF45_MEAS_TIMEOUT, we discard the value (UINT16_MAX means no valid data)
684681
_obstacle_distance.distances[i] = UINT16_MAX;

src/drivers/distance_sensor/lightware_sf45_serial/lightware_sf45_serial.hpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050

5151
#include <uORB/Publication.hpp>
5252
#include <uORB/topics/obstacle_distance.h>
53-
#include <uORB/topics/distance_sensor.h>
5453

5554
#include "sf45_commands.h"
5655

@@ -62,6 +61,15 @@ enum SF_SERIAL_STATE {
6261
STATE_SEND_STREAM = 4,
6362
};
6463

64+
enum SF45_PARSED_STATE {
65+
START = 0,
66+
FLG_LOW,
67+
FLG_HIGH,
68+
ID,
69+
DATA,
70+
CRC_LOW,
71+
CRC_HIGH
72+
};
6573

6674
enum SensorOrientation { // Direction the sensor faces from MAV_SENSOR_ORIENTATION enum
6775
ROTATION_FORWARD_FACING = 0, // MAV_SENSOR_ROTATION_NONE
@@ -91,8 +99,10 @@ class SF45LaserSerial : public px4::ScheduledWorkItem
9199
private:
92100
obstacle_distance_s _obstacle_distance{};
93101
uORB::Publication<obstacle_distance_s> _obstacle_distance_pub{ORB_ID(obstacle_distance)}; /**< obstacle_distance publication */
94-
static constexpr int BIN_COUNT = sizeof(obstacle_distance_s::distances) / sizeof(obstacle_distance_s::distances[0]);
95-
static constexpr uint64_t SF45_MEAS_TIMEOUT{100_ms};
102+
static constexpr uint8_t BIN_COUNT = sizeof(obstacle_distance_s::distances) / sizeof(
103+
obstacle_distance_s::distances[0]);
104+
static constexpr uint64_t SF45_MEAS_TIMEOUT{100_ms};
105+
static constexpr float SF45_SCALE_FACTOR = 0.01f;
96106

97107
void start();
98108
void stop();

0 commit comments

Comments
 (0)