2016-09-13 00:24:41 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-11-01 03:28:22 -03:00
|
|
|
#include "AP_RangeFinder_LeddarOne.h"
|
|
|
|
|
2016-09-13 00:24:41 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2019-04-19 07:46:37 -03:00
|
|
|
#include <AP_Math/crc.h>
|
2016-09-13 00:24:41 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
// read - return last value measured by sensor
|
|
|
|
bool AP_RangeFinder_LeddarOne::get_reading(uint16_t &reading_cm)
|
|
|
|
{
|
2016-11-04 11:15:31 -03:00
|
|
|
uint8_t number_detections;
|
|
|
|
LeddarOne_Status leddarone_status;
|
|
|
|
|
2016-09-13 00:24:41 -03:00
|
|
|
if (uart == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-04 11:15:31 -03:00
|
|
|
switch (modbus_status) {
|
2016-11-22 10:27:36 -04:00
|
|
|
case LEDDARONE_MODBUS_STATE_INIT: {
|
2020-05-22 21:24:32 -03:00
|
|
|
uart->discard_input();
|
|
|
|
|
2016-11-04 23:26:13 -03:00
|
|
|
// clear buffer and buffer_len
|
|
|
|
memset(read_buffer, 0, sizeof(read_buffer));
|
|
|
|
read_len = 0;
|
2016-11-06 06:02:14 -04:00
|
|
|
modbus_status = LEDDARONE_MODBUS_STATE_PRE_SEND_REQUEST;
|
2016-12-12 08:37:52 -04:00
|
|
|
}
|
2017-08-22 14:28:10 -03:00
|
|
|
|
|
|
|
// fall through to next state LEDDARONE_MODBUS_STATE_PRE_SEND_REQUEST
|
|
|
|
// immediately
|
|
|
|
FALLTHROUGH;
|
2016-11-05 21:27:00 -03:00
|
|
|
|
2016-11-06 06:02:14 -04:00
|
|
|
case LEDDARONE_MODBUS_STATE_PRE_SEND_REQUEST:
|
2016-11-04 23:26:13 -03:00
|
|
|
// send a request message for Modbus function 4
|
2016-11-22 17:40:37 -04:00
|
|
|
uart->write(send_request_buffer, sizeof(send_request_buffer));
|
2016-11-06 06:02:14 -04:00
|
|
|
modbus_status = LEDDARONE_MODBUS_STATE_SENT_REQUEST;
|
2016-11-04 23:26:13 -03:00
|
|
|
last_sending_request_ms = AP_HAL::millis();
|
2017-08-22 14:28:10 -03:00
|
|
|
FALLTHROUGH;
|
2016-11-04 23:26:13 -03:00
|
|
|
|
2016-11-06 06:02:14 -04:00
|
|
|
case LEDDARONE_MODBUS_STATE_SENT_REQUEST:
|
2016-11-04 23:26:13 -03:00
|
|
|
if (uart->available()) {
|
|
|
|
// change mod_bus status to read available buffer
|
2016-11-06 06:02:14 -04:00
|
|
|
modbus_status = LEDDARONE_MODBUS_STATE_AVAILABLE;
|
2016-11-22 10:27:36 -04:00
|
|
|
last_available_ms = AP_HAL::millis();
|
2016-11-04 23:26:13 -03:00
|
|
|
} else {
|
|
|
|
if (AP_HAL::millis() - last_sending_request_ms > 200) {
|
2016-11-04 11:15:31 -03:00
|
|
|
// reset mod_bus status to read new buffer
|
2016-11-04 14:20:43 -03:00
|
|
|
// if read_len is zero, send request without initialize
|
2016-11-06 06:02:14 -04:00
|
|
|
modbus_status = (read_len == 0) ? LEDDARONE_MODBUS_STATE_PRE_SEND_REQUEST : LEDDARONE_MODBUS_STATE_INIT;
|
2016-11-04 11:15:31 -03:00
|
|
|
}
|
2016-11-04 23:26:13 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-11-06 06:02:14 -04:00
|
|
|
case LEDDARONE_MODBUS_STATE_AVAILABLE:
|
2016-11-04 23:26:13 -03:00
|
|
|
// parse a response message, set number_detections, detections and sum_distance
|
|
|
|
leddarone_status = parse_response(number_detections);
|
|
|
|
|
2016-11-06 06:02:14 -04:00
|
|
|
if (leddarone_status == LEDDARONE_STATE_OK) {
|
2016-11-04 23:26:13 -03:00
|
|
|
reading_cm = sum_distance / number_detections;
|
|
|
|
|
|
|
|
// reset mod_bus status to read new buffer
|
2016-11-06 06:02:14 -04:00
|
|
|
modbus_status = LEDDARONE_MODBUS_STATE_INIT;
|
2016-11-04 23:26:13 -03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// if status is not reading buffer, reset mod_bus status to read new buffer
|
2016-11-22 10:27:36 -04:00
|
|
|
else if (leddarone_status != LEDDARONE_STATE_READING_BUFFER || AP_HAL::millis() - last_available_ms > 200) {
|
2016-11-04 23:26:13 -03:00
|
|
|
// if read_len is zero, send request without initialize
|
2016-11-06 06:02:14 -04:00
|
|
|
modbus_status = (read_len == 0) ? LEDDARONE_MODBUS_STATE_PRE_SEND_REQUEST : LEDDARONE_MODBUS_STATE_INIT;
|
2016-11-04 23:26:13 -03:00
|
|
|
}
|
|
|
|
break;
|
2016-11-04 11:15:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-09-13 00:24:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CRC16
|
|
|
|
CRC-16-IBM(x16+x15+x2+1)
|
|
|
|
*/
|
|
|
|
bool AP_RangeFinder_LeddarOne::CRC16(uint8_t *aBuffer, uint8_t aLength, bool aCheck)
|
|
|
|
{
|
2019-04-19 07:46:37 -03:00
|
|
|
uint16_t crc = calc_crc_modbus(aBuffer, aLength);
|
2016-09-13 00:24:41 -03:00
|
|
|
|
2016-09-21 20:32:17 -03:00
|
|
|
uint8_t lCRCLo = LOWBYTE(crc);
|
|
|
|
uint8_t lCRCHi = HIGHBYTE(crc);
|
2016-09-13 00:24:41 -03:00
|
|
|
|
|
|
|
if (aCheck) {
|
|
|
|
return (aBuffer[aLength] == lCRCLo) && (aBuffer[aLength+1] == lCRCHi);
|
|
|
|
} else {
|
|
|
|
aBuffer[aLength] = lCRCLo;
|
|
|
|
aBuffer[aLength+1] = lCRCHi;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
parse a response message from Modbus
|
2016-11-04 14:20:43 -03:00
|
|
|
-----------------------------------------------
|
|
|
|
[ read buffer packet ]
|
|
|
|
-----------------------------------------------
|
|
|
|
0: slave address (LEDDARONE_DEFAULT_ADDRESS)
|
|
|
|
1: functions code
|
2017-11-27 09:14:17 -04:00
|
|
|
2: byte count
|
|
|
|
5-6-3-4: timestamp
|
2016-11-04 14:20:43 -03:00
|
|
|
7-8: internal temperature
|
2017-11-27 09:14:17 -04:00
|
|
|
9-10: number of detections
|
2016-11-04 14:20:43 -03:00
|
|
|
11-12: first distance
|
|
|
|
13-14: first amplitude
|
|
|
|
15-16: second distance
|
|
|
|
17-18: second amplitude
|
|
|
|
19-20: third distances
|
|
|
|
21-22: third amplitude
|
|
|
|
23: CRC Low
|
|
|
|
24: CRC High
|
|
|
|
-----------------------------------------------
|
2016-09-13 00:24:41 -03:00
|
|
|
*/
|
2016-10-10 05:14:39 -03:00
|
|
|
LeddarOne_Status AP_RangeFinder_LeddarOne::parse_response(uint8_t &number_detections)
|
2016-09-13 00:24:41 -03:00
|
|
|
{
|
2016-11-04 11:15:31 -03:00
|
|
|
uint8_t index;
|
2016-11-04 22:19:32 -03:00
|
|
|
uint8_t index_offset = LEDDARONE_DETECTION_DATA_INDEX_OFFSET;
|
2016-09-13 00:24:41 -03:00
|
|
|
|
|
|
|
// read serial
|
2016-11-04 11:15:31 -03:00
|
|
|
uint32_t nbytes = uart->available();
|
|
|
|
|
|
|
|
if (nbytes != 0) {
|
|
|
|
for (index=read_len; index<nbytes+read_len; index++) {
|
2016-11-04 22:19:32 -03:00
|
|
|
if (index >= LEDDARONE_READ_BUFFER_SIZE) {
|
2016-11-06 06:02:14 -04:00
|
|
|
return LEDDARONE_STATE_ERR_BAD_RESPONSE;
|
2016-09-13 00:24:41 -03:00
|
|
|
}
|
2016-11-04 11:15:31 -03:00
|
|
|
read_buffer[index] = uart->read();
|
|
|
|
}
|
|
|
|
|
|
|
|
read_len += nbytes;
|
|
|
|
|
2016-11-04 22:19:32 -03:00
|
|
|
if (read_len < LEDDARONE_READ_BUFFER_SIZE) {
|
2016-11-06 06:02:14 -04:00
|
|
|
return LEDDARONE_STATE_READING_BUFFER;
|
2016-09-13 00:24:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 08:10:26 -04:00
|
|
|
// read_len is not 25 byte or function code is not 0x04
|
2016-11-04 22:19:32 -03:00
|
|
|
if (read_len != LEDDARONE_READ_BUFFER_SIZE || read_buffer[1] != LEDDARONE_MODOBUS_FUNCTION_CODE) {
|
2016-11-06 06:02:14 -04:00
|
|
|
return LEDDARONE_STATE_ERR_BAD_RESPONSE;
|
2016-09-13 00:24:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CRC16
|
2016-11-04 11:15:31 -03:00
|
|
|
if (!CRC16(read_buffer, read_len-2, true)) {
|
2016-11-06 06:02:14 -04:00
|
|
|
return LEDDARONE_STATE_ERR_BAD_CRC;
|
2016-09-13 00:24:41 -03:00
|
|
|
}
|
|
|
|
|
2016-11-04 22:19:32 -03:00
|
|
|
// number of detections (index:10)
|
|
|
|
number_detections = read_buffer[LEDDARONE_DETECTION_DATA_NUMBER_INDEX];
|
2016-09-13 00:24:41 -03:00
|
|
|
|
2016-10-08 08:58:05 -03:00
|
|
|
// if the number of detection is over or zero , it is false
|
2016-10-10 05:14:39 -03:00
|
|
|
if (number_detections > LEDDARONE_DETECTIONS_MAX || number_detections == 0) {
|
2016-11-06 06:02:14 -04:00
|
|
|
return LEDDARONE_STATE_ERR_NUMBER_DETECTIONS;
|
2016-09-13 00:24:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(detections, 0, sizeof(detections));
|
|
|
|
sum_distance = 0;
|
2016-11-04 11:15:31 -03:00
|
|
|
for (index=0; index<number_detections; index++) {
|
2016-09-19 06:45:56 -03:00
|
|
|
// construct data word from two bytes and convert mm to cm
|
2016-11-04 11:15:31 -03:00
|
|
|
detections[index] = (static_cast<uint16_t>(read_buffer[index_offset])*256 + read_buffer[index_offset+1]) / 10;
|
|
|
|
sum_distance += detections[index];
|
2016-11-04 22:19:32 -03:00
|
|
|
|
|
|
|
// add index offset (4) to read next detection data
|
|
|
|
index_offset += LEDDARONE_DETECTION_DATA_OFFSET;
|
2016-09-13 00:24:41 -03:00
|
|
|
}
|
|
|
|
|
2016-11-06 06:02:14 -04:00
|
|
|
return LEDDARONE_STATE_OK;
|
2016-09-13 00:24:41 -03:00
|
|
|
}
|