You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.5 KiB
83 lines
2.5 KiB
11 years ago
|
/*
|
||
|
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/>.
|
||
|
*/
|
||
|
|
||
10 years ago
|
#include <AP_Common/AP_Common.h>
|
||
|
#include <AP_HAL/AP_HAL.h>
|
||
11 years ago
|
#include "RangeFinder.h"
|
||
|
#include "RangeFinder_Backend.h"
|
||
|
|
||
8 years ago
|
extern const AP_HAL::HAL& hal;
|
||
|
|
||
11 years ago
|
/*
|
||
|
base class constructor.
|
||
|
This incorporates initialisation as well.
|
||
|
*/
|
||
7 years ago
|
AP_RangeFinder_Backend::AP_RangeFinder_Backend(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params) :
|
||
|
state(_state),
|
||
|
params(_params)
|
||
11 years ago
|
{
|
||
5 years ago
|
_backend_type = type();
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
7 years ago
|
MAV_DISTANCE_SENSOR AP_RangeFinder_Backend::get_mav_distance_sensor_type() const {
|
||
5 years ago
|
if (type() == RangeFinder::Type::NONE) {
|
||
7 years ago
|
return MAV_DISTANCE_SENSOR_UNKNOWN;
|
||
|
}
|
||
|
return _get_mav_distance_sensor_type();
|
||
|
}
|
||
|
|
||
5 years ago
|
RangeFinder::Status AP_RangeFinder_Backend::status() const {
|
||
5 years ago
|
if (type() == RangeFinder::Type::NONE) {
|
||
7 years ago
|
// turned off at runtime?
|
||
5 years ago
|
return RangeFinder::Status::NotConnected;
|
||
7 years ago
|
}
|
||
|
return state.status;
|
||
|
}
|
||
|
|
||
|
// true if sensor is returning data
|
||
|
bool AP_RangeFinder_Backend::has_data() const {
|
||
5 years ago
|
return ((state.status != RangeFinder::Status::NotConnected) &&
|
||
|
(state.status != RangeFinder::Status::NoData));
|
||
7 years ago
|
}
|
||
|
|
||
10 years ago
|
// update status based on distance measurement
|
||
|
void AP_RangeFinder_Backend::update_status()
|
||
|
{
|
||
|
// check distance
|
||
7 years ago
|
if ((int16_t)state.distance_cm > params.max_distance_cm) {
|
||
5 years ago
|
set_status(RangeFinder::Status::OutOfRangeHigh);
|
||
7 years ago
|
} else if ((int16_t)state.distance_cm < params.min_distance_cm) {
|
||
5 years ago
|
set_status(RangeFinder::Status::OutOfRangeLow);
|
||
10 years ago
|
} else {
|
||
5 years ago
|
set_status(RangeFinder::Status::Good);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// set status and update valid count
|
||
5 years ago
|
void AP_RangeFinder_Backend::set_status(RangeFinder::Status _status)
|
||
10 years ago
|
{
|
||
8 years ago
|
state.status = _status;
|
||
10 years ago
|
|
||
|
// update valid count
|
||
5 years ago
|
if (_status == RangeFinder::Status::Good) {
|
||
10 years ago
|
if (state.range_valid_count < 10) {
|
||
|
state.range_valid_count++;
|
||
|
}
|
||
|
} else {
|
||
|
state.range_valid_count = 0;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|