2015-11-16 00:09:37 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include "AP_InertialSensor_SITL.h"
|
|
|
|
#include <SITL/SITL.h>
|
2017-04-30 21:51:15 -03:00
|
|
|
#include <stdio.h>
|
2015-11-16 00:09:37 -04:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
|
|
|
|
const extern AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
AP_InertialSensor_SITL::AP_InertialSensor_SITL(AP_InertialSensor &imu) :
|
|
|
|
AP_InertialSensor_Backend(imu)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
detect the sensor
|
|
|
|
*/
|
|
|
|
AP_InertialSensor_Backend *AP_InertialSensor_SITL::detect(AP_InertialSensor &_imu)
|
|
|
|
{
|
|
|
|
AP_InertialSensor_SITL *sensor = new AP_InertialSensor_SITL(_imu);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (sensor == nullptr) {
|
|
|
|
return nullptr;
|
2015-11-16 00:09:37 -04:00
|
|
|
}
|
|
|
|
if (!sensor->init_sensor()) {
|
|
|
|
delete sensor;
|
2016-10-30 02:24:21 -03:00
|
|
|
return nullptr;
|
2015-11-16 00:09:37 -04:00
|
|
|
}
|
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_InertialSensor_SITL::init_sensor(void)
|
|
|
|
{
|
|
|
|
sitl = (SITL::SITL *)AP_Param::find_object("SIM_");
|
|
|
|
if (sitl == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// grab the used instances
|
|
|
|
for (uint8_t i=0; i<INS_SITL_INSTANCES; i++) {
|
2017-04-30 21:51:15 -03:00
|
|
|
gyro_instance[i] = _imu.register_gyro(gyro_sample_hz[i], i);
|
|
|
|
accel_instance[i] = _imu.register_accel(accel_sample_hz[i], i);
|
2015-11-16 00:09:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hal.scheduler->register_timer_process(FUNCTOR_BIND_MEMBER(&AP_InertialSensor_SITL::timer_update, void));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-30 21:51:15 -03:00
|
|
|
/*
|
|
|
|
generate an accelerometer sample
|
|
|
|
*/
|
|
|
|
void AP_InertialSensor_SITL::generate_accel(uint8_t instance)
|
2015-11-16 00:09:37 -04:00
|
|
|
{
|
|
|
|
// minimum noise levels are 2 bits, but averaged over many
|
|
|
|
// samples, giving around 0.01 m/s/s
|
|
|
|
float accel_noise = 0.01f;
|
|
|
|
|
|
|
|
if (sitl->motors_on) {
|
|
|
|
// add extra noise when the motors are on
|
2017-05-01 19:24:50 -03:00
|
|
|
accel_noise += instance==0?sitl->accel_noise:sitl->accel2_noise;
|
2015-11-16 00:09:37 -04:00
|
|
|
}
|
|
|
|
|
2016-12-18 00:53:57 -04:00
|
|
|
// add accel bias and noise
|
2017-04-30 21:51:15 -03:00
|
|
|
Vector3f accel_bias = instance==0?sitl->accel_bias.get():sitl->accel2_bias.get();
|
|
|
|
float xAccel = sitl->state.xAccel + accel_noise * rand_float() + accel_bias.x;
|
|
|
|
float yAccel = sitl->state.yAccel + accel_noise * rand_float() + accel_bias.y;
|
|
|
|
float zAccel = sitl->state.zAccel + accel_noise * rand_float() + accel_bias.z;
|
2015-11-16 00:09:37 -04:00
|
|
|
|
2016-10-18 17:41:26 -03:00
|
|
|
// correct for the acceleration due to the IMU position offset and angular acceleration
|
|
|
|
// correct for the centripetal acceleration
|
|
|
|
// only apply corrections to first accelerometer
|
|
|
|
Vector3f pos_offset = sitl->imu_pos_offset;
|
|
|
|
if (!pos_offset.is_zero()) {
|
|
|
|
// calculate sensed acceleration due to lever arm effect
|
|
|
|
// Note: the % operator has been overloaded to provide a cross product
|
|
|
|
Vector3f angular_accel = Vector3f(radians(sitl->state.angAccel.x) , radians(sitl->state.angAccel.y) , radians(sitl->state.angAccel.z));
|
|
|
|
Vector3f lever_arm_accel = angular_accel % pos_offset;
|
|
|
|
|
|
|
|
// calculate sensed acceleration due to centripetal acceleration
|
|
|
|
Vector3f angular_rate = Vector3f(radians(sitl->state.rollRate), radians(sitl->state.pitchRate), radians(sitl->state.yawRate));
|
|
|
|
Vector3f centripetal_accel = angular_rate % (angular_rate % pos_offset);
|
|
|
|
|
|
|
|
// apply corrections
|
2017-04-30 21:51:15 -03:00
|
|
|
xAccel += lever_arm_accel.x + centripetal_accel.x;
|
|
|
|
yAccel += lever_arm_accel.y + centripetal_accel.y;
|
|
|
|
zAccel += lever_arm_accel.z + centripetal_accel.z;
|
2016-10-18 17:41:26 -03:00
|
|
|
}
|
|
|
|
|
2015-11-16 00:09:37 -04:00
|
|
|
if (fabsf(sitl->accel_fail) > 1.0e-6f) {
|
2017-04-30 21:51:15 -03:00
|
|
|
xAccel = sitl->accel_fail;
|
|
|
|
yAccel = sitl->accel_fail;
|
|
|
|
zAccel = sitl->accel_fail;
|
2015-11-16 00:09:37 -04:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:03:55 -03:00
|
|
|
Vector3f accel = Vector3f(xAccel, yAccel, zAccel);
|
2017-04-30 21:51:15 -03:00
|
|
|
|
2017-06-05 02:37:07 -03:00
|
|
|
_rotate_and_correct_accel(accel_instance[instance], accel);
|
|
|
|
|
2017-04-30 21:51:15 -03:00
|
|
|
_notify_new_accel_raw_sample(accel_instance[instance], accel, AP_HAL::micros64());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
generate a gyro sample
|
|
|
|
*/
|
|
|
|
void AP_InertialSensor_SITL::generate_gyro(uint8_t instance)
|
|
|
|
{
|
|
|
|
// minimum gyro noise is less than 1 bit
|
|
|
|
float gyro_noise = ToRad(0.04f);
|
|
|
|
|
|
|
|
if (sitl->motors_on) {
|
|
|
|
// add extra noise when the motors are on
|
|
|
|
gyro_noise += ToRad(sitl->gyro_noise);
|
|
|
|
}
|
2015-11-16 00:09:37 -04:00
|
|
|
|
|
|
|
float p = radians(sitl->state.rollRate) + gyro_drift();
|
|
|
|
float q = radians(sitl->state.pitchRate) + gyro_drift();
|
|
|
|
float r = radians(sitl->state.yawRate) + gyro_drift();
|
|
|
|
|
2017-04-30 21:51:15 -03:00
|
|
|
p += gyro_noise * rand_float();
|
|
|
|
q += gyro_noise * rand_float();
|
|
|
|
r += gyro_noise * rand_float();
|
2015-11-16 00:09:37 -04:00
|
|
|
|
2017-11-01 03:03:55 -03:00
|
|
|
Vector3f gyro = Vector3f(p, q, r);
|
2015-11-16 00:09:37 -04:00
|
|
|
|
2016-01-19 00:29:08 -04:00
|
|
|
// add in gyro scaling
|
|
|
|
Vector3f scale = sitl->gyro_scale;
|
2017-04-30 21:51:15 -03:00
|
|
|
gyro.x *= (1 + scale.x*0.01);
|
|
|
|
gyro.y *= (1 + scale.y*0.01);
|
|
|
|
gyro.z *= (1 + scale.z*0.01);
|
2016-01-19 00:29:08 -04:00
|
|
|
|
2017-06-05 02:37:07 -03:00
|
|
|
_rotate_and_correct_gyro(gyro_instance[instance], gyro);
|
|
|
|
|
2017-04-30 21:51:15 -03:00
|
|
|
_notify_new_gyro_raw_sample(gyro_instance[instance], gyro, AP_HAL::micros64());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_InertialSensor_SITL::timer_update(void)
|
|
|
|
{
|
|
|
|
uint64_t now = AP_HAL::micros64();
|
2017-12-04 01:30:28 -04:00
|
|
|
#if 0
|
|
|
|
// insert a 1s pause in IMU data. This triggers a pause in EK2
|
|
|
|
// processing that leads to some interesting issues
|
|
|
|
if (now > 5e6 && now < 6e6) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-30 21:51:15 -03:00
|
|
|
for (uint8_t i=0; i<INS_SITL_INSTANCES; i++) {
|
|
|
|
if (now >= next_accel_sample[i]) {
|
|
|
|
generate_accel(i);
|
|
|
|
while (now >= next_accel_sample[i]) {
|
|
|
|
next_accel_sample[i] += 1000000UL / accel_sample_hz[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (now >= next_gyro_sample[i]) {
|
|
|
|
generate_gyro(i);
|
|
|
|
while (now >= next_gyro_sample[i]) {
|
|
|
|
next_gyro_sample[i] += 1000000UL / gyro_sample_hz[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-16 00:09:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
float AP_InertialSensor_SITL::gyro_drift(void)
|
|
|
|
{
|
|
|
|
if (sitl->drift_speed == 0.0f ||
|
|
|
|
sitl->drift_time == 0.0f) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
double period = sitl->drift_time * 2;
|
2015-11-19 23:11:52 -04:00
|
|
|
double minutes = fmod(AP_HAL::micros64() / 60.0e6, period);
|
2015-11-16 00:09:37 -04:00
|
|
|
if (minutes < period/2) {
|
|
|
|
return minutes * ToRad(sitl->drift_speed);
|
|
|
|
}
|
|
|
|
return (period - minutes) * ToRad(sitl->drift_speed);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AP_InertialSensor_SITL::update(void)
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<INS_SITL_INSTANCES; i++) {
|
|
|
|
update_accel(accel_instance[i]);
|
|
|
|
update_gyro(gyro_instance[i]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAL_BOARD_SITL
|