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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
setup the rangefinder with new input
|
|
|
|
*/
|
|
|
|
void SITL_State::_update_rangefinder(float range_value)
|
|
|
|
{
|
|
|
|
float altitude = range_value;
|
2017-03-01 11:54:44 -04:00
|
|
|
if (is_equal(range_value, -1.0f)) { // Use SITL altitude as reading by default
|
2017-01-09 05:16:13 -04:00
|
|
|
altitude = _sitl->height_agl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sensor position offset in body frame
|
2017-03-03 06:19:18 -04:00
|
|
|
const Vector3f relPosSensorBF = _sitl->rngfnd_pos_offset;
|
2017-01-09 05:16:13 -04:00
|
|
|
|
|
|
|
// adjust altitude for position of the sensor on the vehicle if position offset is non-zero
|
|
|
|
if (!relPosSensorBF.is_zero()) {
|
|
|
|
// get a rotation matrix following DCM conventions (body to earth)
|
|
|
|
Matrix3f rotmat;
|
|
|
|
_sitl->state.quaternion.rotation_matrix(rotmat);
|
|
|
|
// rotate the offset into earth frame
|
2017-03-03 06:19:18 -04:00
|
|
|
const Vector3f relPosSensorEF = rotmat * relPosSensorBF;
|
2017-01-09 05:16:13 -04:00
|
|
|
// correct the altitude at the sensor
|
|
|
|
altitude -= relPosSensorEF.z;
|
|
|
|
}
|
|
|
|
|
2017-03-01 11:54:44 -04:00
|
|
|
float voltage = 5.0f; // Start the reading at max value = 5V
|
|
|
|
// If the attidude is non reversed for SITL OR we are using rangefinder from external simulator,
|
|
|
|
// We adjust the reading with noise, glitch and scaler as the reading is on analog port.
|
|
|
|
if ((fabs(_sitl->state.rollDeg) < 90.0 && fabs(_sitl->state.pitchDeg) < 90.0) || !is_equal(range_value, -1.0f)) {
|
|
|
|
if (is_equal(range_value, -1.0f)) { // disable for external reading that already handle this
|
|
|
|
// adjust for apparent altitude with roll
|
|
|
|
altitude /= cosf(radians(_sitl->state.rollDeg)) * cosf(radians(_sitl->state.pitchDeg));
|
|
|
|
}
|
|
|
|
// Add some noise on reading
|
2017-01-09 05:16:13 -04:00
|
|
|
altitude += _sitl->sonar_noise * rand_float();
|
|
|
|
|
|
|
|
// Altitude in in m, scaler in meters/volt
|
|
|
|
voltage = altitude / _sitl->sonar_scale;
|
2017-03-01 11:54:44 -04:00
|
|
|
// constrain to 0-5V
|
|
|
|
voltage = constrain_float(voltage, 0.0f, 5.0f);
|
2017-01-09 05:16:13 -04:00
|
|
|
|
2017-03-01 11:54:44 -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) {
|
2017-01-09 05:16:13 -04:00
|
|
|
voltage = 5.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 05:26:36 -03:00
|
|
|
// if not populated also fill out the STIL rangefinder array
|
|
|
|
if (is_equal(_sitl->state.rangefinder_m[0],-1.0f)) {
|
|
|
|
_sitl->state.rangefinder_m[0] = altitude;
|
|
|
|
}
|
|
|
|
|
2017-03-01 11:54:44 -04:00
|
|
|
sonar_pin_value = 1023 * (voltage / 5.0f);
|
2017-01-09 05:16:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|