2017-01-09 05:16:13 -04:00
|
|
|
/*
|
|
|
|
SITL handling
|
|
|
|
|
|
|
|
This simulates a rangefinder
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2020-09-12 16:04:38 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL && !defined(HAL_BUILD_AP_PERIPH)
|
2017-01-09 05:16:13 -04:00
|
|
|
|
|
|
|
#include "AP_HAL_SITL.h"
|
|
|
|
#include "AP_HAL_SITL_Namespace.h"
|
|
|
|
#include "HAL_SITL_Class.h"
|
|
|
|
#include "SITL_State.h"
|
|
|
|
#include <SITL/SITL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
using namespace HALSITL;
|
|
|
|
|
2021-12-07 21:49:53 -04:00
|
|
|
// returns a voltage between 0V to 5V which should appear as the
|
|
|
|
// voltage from the sensor
|
|
|
|
float SITL_State::_sonar_pin_voltage() const
|
2017-01-09 05:16:13 -04:00
|
|
|
{
|
2021-12-07 21:49:53 -04:00
|
|
|
// Use glitch defines as the probablility between 0-1 that any
|
|
|
|
// given sonar sample will read as max distance
|
|
|
|
if (!is_zero(_sitl->sonar_glitch) &&
|
|
|
|
_sitl->sonar_glitch >= (rand_float() + 1.0f) / 2.0f) {
|
|
|
|
// glitched
|
|
|
|
return 5.0f;
|
2017-01-09 05:16:13 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 21:49:53 -04:00
|
|
|
const float altitude = sitl_model->rangefinder_range();
|
2021-12-11 17:48:30 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
2021-12-07 21:49:53 -04:00
|
|
|
if (altitude == INFINITY) {
|
|
|
|
return 5.0f;
|
2017-01-09 05:16:13 -04:00
|
|
|
}
|
2021-12-11 17:48:30 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2017-01-09 05:16:13 -04:00
|
|
|
|
2021-12-07 21:49:53 -04:00
|
|
|
// Altitude in in m, scaler in meters/volt
|
|
|
|
const float voltage = altitude / _sitl->sonar_scale;
|
2017-01-09 05:16:13 -04:00
|
|
|
|
2021-12-07 21:49:53 -04:00
|
|
|
// constrain to 0-5V
|
|
|
|
return constrain_float(voltage, 0.0f, 5.0f);
|
|
|
|
}
|
2020-07-29 05:26:36 -03:00
|
|
|
|
2021-12-07 21:49:53 -04:00
|
|
|
/*
|
|
|
|
setup the rangefinder with new input
|
|
|
|
*/
|
|
|
|
void SITL_State::_update_rangefinder()
|
|
|
|
{
|
|
|
|
sonar_pin_value = 1023 * (_sonar_pin_voltage() / 5.0f);
|
2017-01-09 05:16:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|