2014-06-09 14:14:50 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2012-11-27 21:39:15 -04:00
|
|
|
#ifndef __RANGEFINDER_H__
|
|
|
|
#define __RANGEFINDER_H__
|
2012-01-10 19:44:04 -04:00
|
|
|
|
2012-11-11 21:40:19 -04:00
|
|
|
#include <AP_Common.h>
|
2012-11-27 21:39:15 -04:00
|
|
|
#include <AP_HAL.h>
|
2014-06-26 23:56:50 -03:00
|
|
|
#include <AP_Param.h>
|
2012-01-10 19:44:04 -04:00
|
|
|
|
2014-06-09 14:14:50 -03:00
|
|
|
// Maximum number of range finder instances available on this platform
|
2014-04-30 14:43:03 -03:00
|
|
|
#define RANGEFINDER_MAX_INSTANCES 2
|
2014-06-26 23:56:50 -03:00
|
|
|
|
|
|
|
class AP_RangeFinder_Backend;
|
2014-06-09 14:14:50 -03:00
|
|
|
|
2012-01-10 19:44:04 -04:00
|
|
|
class RangeFinder
|
|
|
|
{
|
2014-06-09 14:14:50 -03:00
|
|
|
public:
|
2015-02-21 06:55:21 -04:00
|
|
|
friend class AP_RangeFinder_Backend;
|
2014-06-26 23:56:50 -03:00
|
|
|
RangeFinder(void) :
|
2015-01-07 03:20:29 -04:00
|
|
|
primary_instance(0),
|
2015-02-21 06:55:21 -04:00
|
|
|
num_instances(0),
|
|
|
|
estimated_terrain_height(0)
|
2014-06-26 23:56:50 -03:00
|
|
|
{
|
2014-06-09 14:14:50 -03:00
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
2012-08-17 03:21:04 -03:00
|
|
|
}
|
2014-06-09 14:14:50 -03:00
|
|
|
|
|
|
|
// RangeFinder driver types
|
|
|
|
enum RangeFinder_Type {
|
2014-06-26 23:56:50 -03:00
|
|
|
RangeFinder_TYPE_NONE = 0,
|
2014-07-16 10:52:46 -03:00
|
|
|
RangeFinder_TYPE_ANALOG = 1,
|
|
|
|
RangeFinder_TYPE_MBI2C = 2,
|
|
|
|
RangeFinder_TYPE_PLI2C = 3,
|
2015-02-21 06:55:21 -04:00
|
|
|
RangeFinder_TYPE_PX4 = 4,
|
|
|
|
RangeFinder_TYPE_PX4_PWM= 5
|
2014-06-09 14:14:50 -03:00
|
|
|
};
|
|
|
|
|
2014-06-26 23:56:50 -03:00
|
|
|
enum RangeFinder_Function {
|
|
|
|
FUNCTION_LINEAR = 0,
|
|
|
|
FUNCTION_INVERTED = 1,
|
|
|
|
FUNCTION_HYPERBOLA = 2
|
2014-06-09 14:14:50 -03:00
|
|
|
};
|
2014-06-26 23:56:50 -03:00
|
|
|
|
|
|
|
|
2014-06-09 14:14:50 -03:00
|
|
|
// The RangeFinder_State structure is filled in by the backend driver
|
|
|
|
struct RangeFinder_State {
|
2014-06-26 23:56:50 -03:00
|
|
|
uint8_t instance; // the instance number of this RangeFinder
|
|
|
|
uint16_t distance_cm; // distance: in cm
|
|
|
|
uint16_t voltage_mv; // voltage in millivolts,
|
|
|
|
// if applicable, otherwise 0
|
|
|
|
bool healthy; // sensor is communicating correctly
|
2014-06-09 14:14:50 -03:00
|
|
|
};
|
2012-01-10 19:44:04 -04:00
|
|
|
|
2014-06-26 23:56:50 -03:00
|
|
|
// parameters for each instance
|
|
|
|
AP_Int8 _type[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
AP_Int8 _pin[RANGEFINDER_MAX_INSTANCES];
|
2014-06-27 00:04:25 -03:00
|
|
|
AP_Int8 _ratiometric[RANGEFINDER_MAX_INSTANCES];
|
2014-06-26 23:56:50 -03:00
|
|
|
AP_Int8 _stop_pin[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
AP_Int16 _settle_time_ms[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
AP_Float _scaling[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
AP_Float _offset[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
AP_Int8 _function[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
AP_Int16 _min_distance_cm[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
AP_Int16 _max_distance_cm[RANGEFINDER_MAX_INSTANCES];
|
2015-02-21 06:55:21 -04:00
|
|
|
AP_Int16 _powersave_range;
|
2014-06-26 23:56:50 -03:00
|
|
|
|
2014-06-09 14:14:50 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
// Return the number of range finder instances
|
|
|
|
uint8_t num_sensors(void) const {
|
|
|
|
return num_instances;
|
2012-11-27 21:39:15 -04:00
|
|
|
}
|
2012-01-10 19:44:04 -04:00
|
|
|
|
2014-06-26 23:56:50 -03:00
|
|
|
// detect and initialise any available rangefinders
|
2014-06-09 14:14:50 -03:00
|
|
|
void init(void);
|
2014-06-26 23:56:50 -03:00
|
|
|
|
|
|
|
// update state of all rangefinders. Should be called at around
|
|
|
|
// 10Hz from main loop
|
2014-06-09 14:14:50 -03:00
|
|
|
void update(void);
|
2014-04-30 14:43:03 -03:00
|
|
|
|
2014-06-27 00:18:54 -03:00
|
|
|
#define _RangeFinder_STATE(instance) state[instance]
|
2014-04-30 14:43:03 -03:00
|
|
|
|
2014-06-26 23:56:50 -03:00
|
|
|
uint16_t distance_cm(uint8_t instance) const {
|
|
|
|
return _RangeFinder_STATE(instance).distance_cm;
|
|
|
|
}
|
|
|
|
uint16_t distance_cm() const {
|
|
|
|
return distance_cm(primary_instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t voltage_mv(uint8_t instance) const {
|
|
|
|
return _RangeFinder_STATE(instance).voltage_mv;
|
2014-06-09 14:14:50 -03:00
|
|
|
}
|
2014-06-26 23:56:50 -03:00
|
|
|
uint16_t voltage_mv() const {
|
|
|
|
return voltage_mv(primary_instance);
|
2014-06-09 14:14:50 -03:00
|
|
|
}
|
|
|
|
|
2014-06-26 23:56:50 -03:00
|
|
|
int16_t max_distance_cm(uint8_t instance) const {
|
|
|
|
return _max_distance_cm[instance];
|
2014-06-09 14:14:50 -03:00
|
|
|
}
|
2014-06-26 23:56:50 -03:00
|
|
|
int16_t max_distance_cm() const {
|
|
|
|
return max_distance_cm(primary_instance);
|
2014-06-09 14:14:50 -03:00
|
|
|
}
|
|
|
|
|
2014-06-26 23:56:50 -03:00
|
|
|
int16_t min_distance_cm(uint8_t instance) const {
|
|
|
|
return _min_distance_cm[instance];
|
2014-06-09 14:14:50 -03:00
|
|
|
}
|
2014-06-26 23:56:50 -03:00
|
|
|
int16_t min_distance_cm() const {
|
|
|
|
return min_distance_cm(primary_instance);
|
2014-06-09 14:14:50 -03:00
|
|
|
}
|
2014-04-30 14:43:03 -03:00
|
|
|
|
2014-06-09 14:14:50 -03:00
|
|
|
bool healthy(uint8_t instance) const {
|
2014-06-27 00:18:54 -03:00
|
|
|
return instance < num_instances && _RangeFinder_STATE(instance).healthy;
|
2014-06-09 14:14:50 -03:00
|
|
|
}
|
|
|
|
bool healthy() const {
|
|
|
|
return healthy(primary_instance);
|
|
|
|
}
|
2015-02-21 06:55:21 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
set an externally estimated terrain height. Used to enable power
|
|
|
|
saving (where available) at high altitudes.
|
|
|
|
*/
|
|
|
|
void set_estimated_terrain_height(float height) {
|
|
|
|
estimated_terrain_height = height;
|
|
|
|
}
|
2014-06-09 14:14:50 -03:00
|
|
|
|
|
|
|
private:
|
|
|
|
RangeFinder_State state[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
AP_RangeFinder_Backend *drivers[RANGEFINDER_MAX_INSTANCES];
|
|
|
|
uint8_t primary_instance:2;
|
|
|
|
uint8_t num_instances:2;
|
2015-02-21 06:55:21 -04:00
|
|
|
float estimated_terrain_height;
|
2014-06-09 14:14:50 -03:00
|
|
|
|
|
|
|
void detect_instance(uint8_t instance);
|
|
|
|
void update_instance(uint8_t instance);
|
2012-01-10 19:44:04 -04:00
|
|
|
};
|
2012-11-27 21:39:15 -04:00
|
|
|
#endif // __RANGEFINDER_H__
|