1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #include <AP_HAL/AP_HAL.h>
- #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
- #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;
- void SITL_State::_update_rangefinder(float range_value)
- {
- float altitude = range_value;
- if (is_equal(range_value, -1.0f)) {
- altitude = _sitl->height_agl;
- }
-
- const Vector3f relPosSensorBF = _sitl->rngfnd_pos_offset;
-
- if (!relPosSensorBF.is_zero()) {
-
- Matrix3f rotmat;
- _sitl->state.quaternion.rotation_matrix(rotmat);
-
- const Vector3f relPosSensorEF = rotmat * relPosSensorBF;
-
- altitude -= relPosSensorEF.z;
- }
- float voltage = 5.0f;
-
-
- 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)) {
-
- altitude /= cosf(radians(_sitl->state.rollDeg)) * cosf(radians(_sitl->state.pitchDeg));
- }
-
- altitude += _sitl->sonar_noise * rand_float();
-
- voltage = altitude / _sitl->sonar_scale;
-
- voltage = constrain_float(voltage, 0.0f, 5.0f);
-
- if (!is_zero(_sitl->sonar_glitch) && _sitl->sonar_glitch >= (rand_float() + 1.0f) / 2.0f) {
- voltage = 5.0f;
- }
- }
- sonar_pin_value = 1023 * (voltage / 5.0f);
- }
- #endif
|