2011-11-16 21:49:56 -04:00
|
|
|
/*
|
|
|
|
SITL handling
|
|
|
|
|
|
|
|
This emulates the ADS7844 ADC
|
|
|
|
|
|
|
|
Andrew Tridgell November 2011
|
|
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <AP_ADC.h>
|
2012-06-29 02:06:28 -03:00
|
|
|
#include <SITL.h>
|
2012-03-04 05:37:10 -04:00
|
|
|
#include <avr/interrupt.h>
|
2011-11-16 21:49:56 -04:00
|
|
|
#include "wiring.h"
|
|
|
|
#include "sitl_adc.h"
|
|
|
|
#include "desktop.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2012-06-29 02:06:28 -03:00
|
|
|
extern SITL sitl;
|
|
|
|
|
2011-11-16 21:49:56 -04:00
|
|
|
/*
|
|
|
|
convert airspeed in m/s to an airspeed sensor value
|
|
|
|
*/
|
|
|
|
static uint16_t airspeed_sensor(float airspeed)
|
|
|
|
{
|
|
|
|
const float airspeed_ratio = 1.9936;
|
|
|
|
const float airspeed_offset = 2820;
|
|
|
|
float airspeed_pressure, airspeed_raw;
|
|
|
|
|
|
|
|
airspeed_pressure = sqr(airspeed) / airspeed_ratio;
|
|
|
|
airspeed_raw = airspeed_pressure + airspeed_offset;
|
|
|
|
return airspeed_raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-29 02:06:28 -03:00
|
|
|
static float gyro_drift(void)
|
|
|
|
{
|
|
|
|
if (sitl.drift_speed == 0.0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
double period = sitl.drift_time * 2;
|
|
|
|
double minutes = fmod(micros() / 60.0e6, period);
|
|
|
|
if (minutes < period/2) {
|
|
|
|
return minutes * ToRad(sitl.drift_speed);
|
|
|
|
}
|
|
|
|
return (period - minutes) * ToRad(sitl.drift_speed);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-16 21:49:56 -04:00
|
|
|
/*
|
|
|
|
setup the ADC channels with new input
|
|
|
|
|
|
|
|
Note that this uses roll, pitch and yaw only as inputs. The
|
|
|
|
simulator rollrates are instantaneous, whereas we need to use
|
|
|
|
average rates to cope with slow update rates.
|
|
|
|
|
|
|
|
inputs are in degrees
|
2012-01-11 06:30:48 -04:00
|
|
|
|
|
|
|
phi - roll
|
|
|
|
theta - pitch
|
|
|
|
psi - true heading
|
|
|
|
alpha - angle of attack
|
|
|
|
beta - side slip
|
|
|
|
phidot - roll rate
|
|
|
|
thetadot - pitch rate
|
|
|
|
psidot - yaw rate
|
|
|
|
v_north - north velocity in local/body frame
|
|
|
|
v_east - east velocity in local/body frame
|
|
|
|
v_down - down velocity in local/body frame
|
|
|
|
A_X_pilot - X accel in body frame
|
|
|
|
A_Y_pilot - Y accel in body frame
|
|
|
|
A_Z_pilot - Z accel in body frame
|
|
|
|
|
|
|
|
Note: doubles on high prec. stuff are preserved until the last moment
|
|
|
|
|
2011-11-16 21:49:56 -04:00
|
|
|
*/
|
2012-01-11 06:30:48 -04:00
|
|
|
void sitl_update_adc(float roll, float pitch, float yaw, // Relative to earth
|
|
|
|
double rollRate, double pitchRate,double yawRate, // Local to plane
|
|
|
|
double xAccel, double yAccel, double zAccel, // Local to plane
|
2011-12-02 00:15:12 -04:00
|
|
|
float airspeed)
|
2011-11-16 21:49:56 -04:00
|
|
|
{
|
|
|
|
static const uint8_t sensor_map[6] = { 1, 2, 0, 4, 5, 6 };
|
|
|
|
static const float _sensor_signs[6] = { 1, -1, -1, 1, -1, -1 };
|
|
|
|
const float accel_offset = 2041;
|
|
|
|
const float gyro_offset = 1658;
|
2011-12-02 00:15:12 -04:00
|
|
|
const float _gyro_gain_x = ToRad(0.4);
|
|
|
|
const float _gyro_gain_y = ToRad(0.41);
|
|
|
|
const float _gyro_gain_z = ToRad(0.41);
|
2011-11-16 21:49:56 -04:00
|
|
|
const float _accel_scale = 9.80665 / 423.8;
|
2012-01-11 06:30:48 -04:00
|
|
|
double adc[7];
|
|
|
|
double p, q, r;
|
2012-06-29 02:06:28 -03:00
|
|
|
extern float sitl_motor_speed[4];
|
|
|
|
bool motors_on = false;
|
|
|
|
|
|
|
|
SITL::convert_body_frame(roll, pitch,
|
|
|
|
rollRate, pitchRate, yawRate,
|
|
|
|
&p, &q, &r);
|
|
|
|
|
|
|
|
for (uint8_t i=0; i<4; i++) {
|
|
|
|
if (sitl_motor_speed[i] > 0.0) {
|
|
|
|
motors_on = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 22:04:54 -03:00
|
|
|
// minimum noise levels are 2 bits
|
|
|
|
float accel_noise = _accel_scale*2;
|
|
|
|
float gyro_noise = _gyro_gain_y*2;
|
2012-06-29 02:06:28 -03:00
|
|
|
if (motors_on) {
|
2012-07-04 22:04:54 -03:00
|
|
|
// add extra noise when the motors are on
|
|
|
|
accel_noise += sitl.accel_noise;
|
|
|
|
gyro_noise += ToRad(sitl.gyro_noise);
|
2012-06-29 02:06:28 -03:00
|
|
|
}
|
2012-07-04 22:04:54 -03:00
|
|
|
xAccel += accel_noise * rand_float();
|
|
|
|
yAccel += accel_noise * rand_float();
|
|
|
|
zAccel += accel_noise * rand_float();
|
|
|
|
|
|
|
|
p += gyro_noise * rand_float();
|
|
|
|
q += gyro_noise * rand_float();
|
|
|
|
r += gyro_noise * rand_float();
|
2011-12-02 00:15:12 -04:00
|
|
|
|
2012-06-29 02:06:28 -03:00
|
|
|
p += gyro_drift();
|
|
|
|
q += gyro_drift();
|
|
|
|
r += gyro_drift();
|
2011-11-16 21:49:56 -04:00
|
|
|
|
|
|
|
/* work out the ADC channel values */
|
2011-12-02 00:15:12 -04:00
|
|
|
adc[0] = (p / (_gyro_gain_x * _sensor_signs[0])) + gyro_offset;
|
|
|
|
adc[1] = (q / (_gyro_gain_y * _sensor_signs[1])) + gyro_offset;
|
|
|
|
adc[2] = (r / (_gyro_gain_z * _sensor_signs[2])) + gyro_offset;
|
2011-11-16 21:49:56 -04:00
|
|
|
|
|
|
|
adc[3] = (xAccel / (_accel_scale * _sensor_signs[3])) + accel_offset;
|
|
|
|
adc[4] = (yAccel / (_accel_scale * _sensor_signs[4])) + accel_offset;
|
|
|
|
adc[5] = (zAccel / (_accel_scale * _sensor_signs[5])) + accel_offset;
|
|
|
|
|
|
|
|
/* tell the UDR2 register emulation what values to give to the driver */
|
|
|
|
for (uint8_t i=0; i<6; i++) {
|
2011-11-28 01:01:23 -04:00
|
|
|
UDR2.set(sensor_map[i], adc[i]);
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|
|
|
|
// set the airspeed sensor input
|
2011-11-28 01:01:23 -04:00
|
|
|
UDR2.set(7, airspeed_sensor(airspeed));
|
2011-11-16 21:49:56 -04:00
|
|
|
|
|
|
|
/* FIX: rubbish value for temperature for now */
|
2011-11-28 01:01:23 -04:00
|
|
|
UDR2.set(3, 2000);
|
2012-03-04 05:37:10 -04:00
|
|
|
|
|
|
|
runInterrupt(6);
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
setup ADC emulation
|
|
|
|
*/
|
|
|
|
void sitl_setup_adc(void)
|
|
|
|
{
|
|
|
|
// mark it always ready. This is the register
|
|
|
|
// the ADC driver uses to tell if there is new data pending
|
|
|
|
UCSR2A = (1 << RXC2);
|
|
|
|
}
|