2013-08-29 02:34:34 -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/>.
|
|
|
|
*/
|
|
|
|
|
2013-02-28 20:19:02 -04:00
|
|
|
/*
|
|
|
|
* AP_RangeFinder_analog.cpp - rangefinder for analog source
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#include "AP_RangeFinder_analog.h"
|
|
|
|
|
|
|
|
#if AP_RANGEFINDER_ANALOG_ENABLED
|
|
|
|
|
2015-08-11 03:28:45 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2019-11-10 22:47:38 -04:00
|
|
|
#include "AP_RangeFinder.h"
|
2018-07-04 11:22:17 -03:00
|
|
|
#include "AP_RangeFinder_Params.h"
|
2013-02-28 20:19:02 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2014-06-26 23:57:28 -03:00
|
|
|
/*
|
|
|
|
The constructor also initialises the rangefinder. Note that this
|
|
|
|
constructor is not called until detect() returns true, so we
|
|
|
|
already know that we should setup the rangefinder
|
|
|
|
*/
|
2018-07-04 11:22:17 -03:00
|
|
|
AP_RangeFinder_analog::AP_RangeFinder_analog(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params) :
|
|
|
|
AP_RangeFinder_Backend(_state, _params)
|
2013-02-28 20:19:02 -04:00
|
|
|
{
|
2018-07-04 11:22:17 -03:00
|
|
|
source = hal.analogin->channel(_params.pin);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (source == nullptr) {
|
2014-06-26 23:57:28 -03:00
|
|
|
// failed to allocate a ADC channel? This shouldn't happen
|
2019-11-01 02:10:52 -03:00
|
|
|
set_status(RangeFinder::Status::NotConnected);
|
2014-06-26 23:57:28 -03:00
|
|
|
return;
|
|
|
|
}
|
2019-11-01 02:10:52 -03:00
|
|
|
set_status(RangeFinder::Status::NoData);
|
2013-02-28 20:19:02 -04:00
|
|
|
}
|
|
|
|
|
2014-06-26 23:57:28 -03:00
|
|
|
/*
|
|
|
|
detect if an analog rangefinder is connected. The only thing we
|
|
|
|
can do is check if the pin number is valid. If it is, then assume
|
|
|
|
that the device is connected
|
2013-02-28 20:19:02 -04:00
|
|
|
*/
|
2018-07-04 11:22:17 -03:00
|
|
|
bool AP_RangeFinder_analog::detect(AP_RangeFinder_Params &_params)
|
2013-02-28 20:19:02 -04:00
|
|
|
{
|
2018-07-04 11:22:17 -03:00
|
|
|
if (_params.pin != -1) {
|
2014-06-26 23:57:28 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-02-28 20:19:02 -04:00
|
|
|
}
|
|
|
|
|
2014-06-26 23:57:28 -03:00
|
|
|
|
2013-02-28 20:19:02 -04:00
|
|
|
/*
|
2014-06-26 23:57:28 -03:00
|
|
|
update raw voltage state
|
2013-02-28 20:19:02 -04:00
|
|
|
*/
|
2014-06-26 23:57:28 -03:00
|
|
|
void AP_RangeFinder_analog::update_voltage(void)
|
2013-02-28 20:19:02 -04:00
|
|
|
{
|
2021-09-22 16:49:58 -03:00
|
|
|
if (source == nullptr || !source->set_pin(params.pin)) {
|
2014-06-26 23:57:28 -03:00
|
|
|
state.voltage_mv = 0;
|
2021-09-22 16:49:58 -03:00
|
|
|
set_status(RangeFinder::Status::NotConnected);
|
2014-06-26 23:57:28 -03:00
|
|
|
return;
|
2013-02-28 20:19:02 -04:00
|
|
|
}
|
2018-07-04 11:22:17 -03:00
|
|
|
if (params.ratiometric) {
|
2014-06-27 00:04:25 -03:00
|
|
|
state.voltage_mv = source->voltage_average_ratiometric() * 1000U;
|
|
|
|
} else {
|
|
|
|
state.voltage_mv = source->voltage_average() * 1000U;
|
|
|
|
}
|
2013-02-28 20:19:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-06-26 23:57:28 -03:00
|
|
|
update distance_cm
|
2013-02-28 20:19:02 -04:00
|
|
|
*/
|
2014-06-26 23:57:28 -03:00
|
|
|
void AP_RangeFinder_analog::update(void)
|
2013-02-28 20:19:02 -04:00
|
|
|
{
|
2014-06-26 23:57:28 -03:00
|
|
|
update_voltage();
|
|
|
|
float v = state.voltage_mv * 0.001f;
|
|
|
|
float dist_m = 0;
|
2018-07-04 11:22:17 -03:00
|
|
|
float scaling = params.scaling;
|
|
|
|
float offset = params.offset;
|
2019-11-01 00:06:21 -03:00
|
|
|
RangeFinder::Function function = (RangeFinder::Function)params.function.get();
|
2018-07-04 11:22:17 -03:00
|
|
|
int16_t _max_distance_cm = params.max_distance_cm;
|
2014-06-26 23:57:28 -03:00
|
|
|
|
|
|
|
switch (function) {
|
2019-11-01 00:06:21 -03:00
|
|
|
case RangeFinder::Function::LINEAR:
|
2014-06-26 23:57:28 -03:00
|
|
|
dist_m = (v - offset) * scaling;
|
|
|
|
break;
|
2013-02-28 20:19:02 -04:00
|
|
|
|
2019-11-01 00:06:21 -03:00
|
|
|
case RangeFinder::Function::INVERTED:
|
2014-06-26 23:57:28 -03:00
|
|
|
dist_m = (offset - v) * scaling;
|
|
|
|
break;
|
|
|
|
|
2019-11-01 00:06:21 -03:00
|
|
|
case RangeFinder::Function::HYPERBOLA:
|
2014-06-26 23:57:28 -03:00
|
|
|
if (v <= offset) {
|
|
|
|
dist_m = 0;
|
2018-03-31 02:25:03 -03:00
|
|
|
} else {
|
|
|
|
dist_m = scaling / (v - offset);
|
2014-06-26 23:57:28 -03:00
|
|
|
}
|
2018-03-31 21:49:44 -03:00
|
|
|
if (dist_m > _max_distance_cm * 0.01f) {
|
2017-08-08 02:54:09 -03:00
|
|
|
dist_m = _max_distance_cm * 0.01f;
|
2014-06-26 23:57:28 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (dist_m < 0) {
|
|
|
|
dist_m = 0;
|
|
|
|
}
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = dist_m;
|
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
|
|
|
state.last_reading_ms = AP_HAL::millis();
|
2014-06-26 23:57:28 -03:00
|
|
|
|
2015-04-13 03:08:19 -03:00
|
|
|
// update range_valid state based on distance measured
|
|
|
|
update_status();
|
2013-02-28 20:19:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#endif // AP_RANGEFINDER_ANALOG_ENABLED
|