2013-05-29 20:51:11 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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>
|
2014-06-26 23:57:28 -03:00
|
|
|
#include "RangeFinder.h"
|
2013-02-28 20:19:02 -04:00
|
|
|
#include "AP_RangeFinder_analog.h"
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
AP_RangeFinder_analog::AP_RangeFinder_analog(RangeFinder &_ranger, uint8_t instance, RangeFinder::RangeFinder_State &_state) :
|
|
|
|
AP_RangeFinder_Backend(_ranger, instance, _state)
|
2013-02-28 20:19:02 -04:00
|
|
|
{
|
2014-06-26 23:57:28 -03:00
|
|
|
source = hal.analogin->channel(ranger._pin[instance]);
|
|
|
|
if (source == NULL) {
|
|
|
|
// failed to allocate a ADC channel? This shouldn't happen
|
2015-04-13 03:08:19 -03:00
|
|
|
set_status(RangeFinder::RangeFinder_NotConnected);
|
2014-06-26 23:57:28 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
source->set_stop_pin((uint8_t)ranger._stop_pin[instance]);
|
|
|
|
source->set_settle_time((uint16_t)ranger._settle_time_ms[instance]);
|
2015-04-13 03:08:19 -03:00
|
|
|
set_status(RangeFinder::RangeFinder_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
|
|
|
*/
|
2014-06-26 23:57:28 -03:00
|
|
|
bool AP_RangeFinder_analog::detect(RangeFinder &_ranger, uint8_t instance)
|
2013-02-28 20:19:02 -04:00
|
|
|
{
|
2014-06-26 23:57:28 -03:00
|
|
|
if (_ranger._pin[instance] != -1) {
|
|
|
|
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
|
|
|
{
|
2014-06-26 23:57:28 -03:00
|
|
|
if (source == NULL) {
|
|
|
|
state.voltage_mv = 0;
|
|
|
|
return;
|
2013-02-28 20:19:02 -04:00
|
|
|
}
|
2013-04-26 01:53:15 -03:00
|
|
|
// cope with changed settings
|
2014-06-26 23:57:28 -03:00
|
|
|
source->set_pin(ranger._pin[state.instance]);
|
|
|
|
source->set_stop_pin((uint8_t)ranger._stop_pin[state.instance]);
|
|
|
|
source->set_settle_time((uint16_t)ranger._settle_time_ms[state.instance]);
|
2014-06-27 00:04:25 -03:00
|
|
|
if (ranger._ratiometric[state.instance]) {
|
|
|
|
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;
|
|
|
|
float scaling = ranger._scaling[state.instance];
|
|
|
|
float offset = ranger._offset[state.instance];
|
|
|
|
RangeFinder::RangeFinder_Function function = (RangeFinder::RangeFinder_Function)ranger._function[state.instance].get();
|
|
|
|
int16_t max_distance_cm = ranger._max_distance_cm[state.instance];
|
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
case RangeFinder::FUNCTION_LINEAR:
|
|
|
|
dist_m = (v - offset) * scaling;
|
|
|
|
break;
|
2013-02-28 20:19:02 -04:00
|
|
|
|
2014-06-26 23:57:28 -03:00
|
|
|
case RangeFinder::FUNCTION_INVERTED:
|
|
|
|
dist_m = (offset - v) * scaling;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RangeFinder::FUNCTION_HYPERBOLA:
|
|
|
|
if (v <= offset) {
|
|
|
|
dist_m = 0;
|
|
|
|
}
|
|
|
|
dist_m = scaling / (v - offset);
|
2015-04-29 22:26:36 -03:00
|
|
|
if (isinf(dist_m) || dist_m > max_distance_cm * 0.01f) {
|
|
|
|
dist_m = max_distance_cm * 0.01f;
|
2014-06-26 23:57:28 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (dist_m < 0) {
|
|
|
|
dist_m = 0;
|
|
|
|
}
|
|
|
|
state.distance_cm = dist_m * 100.0f;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|