2016-04-19 18:32:17 -03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file 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 file 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:29:03 -03:00
|
|
|
#include "AP_RangeFinder_MaxsonarSerialLV.h"
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#if AP_RANGEFINDER_MAXBOTIX_SERIAL_ENABLED
|
|
|
|
|
2016-04-19 18:32:17 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#define MAXSONAR_SERIAL_LV_BAUD_RATE 9600
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2022-01-12 22:48:24 -04:00
|
|
|
AP_RangeFinder_MaxsonarSerialLV::AP_RangeFinder_MaxsonarSerialLV(
|
|
|
|
RangeFinder::RangeFinder_State &_state,
|
|
|
|
AP_RangeFinder_Params &_params):
|
|
|
|
AP_RangeFinder_Backend_Serial(_state, _params)
|
|
|
|
{
|
|
|
|
params.scaling.set_default(0.0254f);
|
|
|
|
}
|
|
|
|
|
2016-04-19 18:32:17 -03:00
|
|
|
// read - return last value measured by sensor
|
2021-10-18 02:45:33 -03:00
|
|
|
bool AP_RangeFinder_MaxsonarSerialLV::get_reading(float &reading_m)
|
2016-04-19 18:32:17 -03:00
|
|
|
{
|
|
|
|
if (uart == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t sum = 0;
|
|
|
|
uint16_t count = 0;
|
|
|
|
|
2023-11-28 18:16:22 -04:00
|
|
|
for (auto i=0; i<8192; i++) {
|
|
|
|
uint8_t c;
|
|
|
|
if (!uart->read(c)) {
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 18:32:17 -03:00
|
|
|
if (c == '\r') {
|
|
|
|
linebuf[linebuf_len] = 0;
|
|
|
|
sum += (int)atoi(linebuf);
|
|
|
|
count++;
|
|
|
|
linebuf_len = 0;
|
|
|
|
} else if (isdigit(c)) {
|
|
|
|
linebuf[linebuf_len++] = c;
|
|
|
|
if (linebuf_len == sizeof(linebuf)) {
|
|
|
|
// too long, discard the line
|
|
|
|
linebuf_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-18 02:45:33 -03:00
|
|
|
// This sonar gives the metrics in inches, so we have to transform this to meters
|
2022-01-12 22:48:24 -04:00
|
|
|
reading_m = params.scaling * (float(sum) / count);
|
2016-04-19 18:32:17 -03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-12 06:37:29 -04:00
|
|
|
|
|
|
|
#endif // AP_RANGEFINDER_MAXBOTIX_SERIAL_ENABLED
|