2011-11-16 21:49:56 -04:00
|
|
|
/*
|
|
|
|
ADS7844 register emulation
|
|
|
|
Code by Andrew Tridgell November 2011
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _SITL_ADC_H
|
|
|
|
#define _SITL_ADC_H
|
|
|
|
|
2011-11-28 01:01:23 -04:00
|
|
|
#include <stdlib.h>
|
2012-02-27 06:58:58 -04:00
|
|
|
#include <math.h>
|
2011-11-28 01:01:23 -04:00
|
|
|
|
2012-03-02 03:32:25 -04:00
|
|
|
static const float vibration_level = 2.0;
|
2012-03-02 20:54:19 -04:00
|
|
|
static const float drift_level = 1.0;
|
2012-03-02 03:29:27 -04:00
|
|
|
// order zgyro, xgyro, ygyro, temp, xacc, yacc, zacc, aspd
|
|
|
|
static const float noise_scale[8] = { 150, 150, 150, 0, 400, 400, 400, 0 };
|
|
|
|
static const float noise_offset[8]= { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
static const float drift_rate[8] = { 0.7, 1.0, 0.5, 0, 0, 0, 0, 0 };
|
|
|
|
static const float base_noise = 2;
|
2012-02-18 02:45:56 -04:00
|
|
|
|
2012-02-27 06:58:58 -04:00
|
|
|
// generate a random float between -1 and 1
|
|
|
|
static double rand_float(void)
|
2012-02-18 02:45:56 -04:00
|
|
|
{
|
2012-02-27 06:58:58 -04:00
|
|
|
float ret = ((unsigned)random()) % 2000000;
|
|
|
|
return (ret - 1.0e6) / 1.0e6;
|
|
|
|
}
|
|
|
|
|
2012-03-02 03:29:27 -04:00
|
|
|
static inline float gyro_drift(uint8_t chan)
|
|
|
|
{
|
2012-03-02 20:54:19 -04:00
|
|
|
if (drift_rate[chan] * drift_level == 0.0) {
|
2012-03-02 03:29:27 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
extern long unsigned int micros(void);
|
2012-03-02 20:54:19 -04:00
|
|
|
double period = 10*drift_rate[chan] * drift_level;
|
2012-03-02 03:29:27 -04:00
|
|
|
double minutes = fmod(micros() / 60.0e6, period);
|
|
|
|
if (minutes < period/2) {
|
|
|
|
return minutes;
|
|
|
|
}
|
|
|
|
return period - minutes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-27 06:58:58 -04:00
|
|
|
static inline float noise_generator(uint8_t chan)
|
|
|
|
{
|
|
|
|
extern float sitl_motor_speed[4];
|
|
|
|
uint8_t i;
|
|
|
|
float noise = 0;
|
|
|
|
uint8_t noise_count=0;
|
2012-03-02 03:29:27 -04:00
|
|
|
extern long unsigned int micros(void);
|
2012-02-27 06:58:58 -04:00
|
|
|
for (i=0; i<4; i++) {
|
|
|
|
if (sitl_motor_speed[i] > 0.0) {
|
2012-03-02 03:29:27 -04:00
|
|
|
float n = rand_float() * noise_scale[chan] * vibration_level;
|
|
|
|
//double t = micros() / 1.0e6;
|
|
|
|
//float freq = (rand_float() + 1.0) * sitl_motor_speed[i];
|
|
|
|
//noise += sin(fmod(t * freq * 2 * M_PI + i,
|
|
|
|
//2*M_PI)) * n;
|
|
|
|
noise += n + noise_offset[chan];
|
2012-02-27 06:58:58 -04:00
|
|
|
noise_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (noise_count == 0) {
|
2012-03-02 20:54:19 -04:00
|
|
|
return rand_float() * base_noise * vibration_level;
|
2012-02-27 04:29:45 -04:00
|
|
|
}
|
2012-03-02 03:29:27 -04:00
|
|
|
return gyro_drift(chan) + noise/noise_count;
|
2012-02-18 02:45:56 -04:00
|
|
|
}
|
|
|
|
|
2011-11-16 21:49:56 -04:00
|
|
|
// this implements the UDR2 register
|
|
|
|
struct ADC_UDR2 {
|
|
|
|
uint16_t value, next_value;
|
|
|
|
uint8_t idx;
|
2011-11-28 01:01:23 -04:00
|
|
|
float channels[8];
|
2011-11-16 21:49:56 -04:00
|
|
|
|
|
|
|
ADC_UDR2() {
|
|
|
|
// constructor
|
|
|
|
for (uint8_t i=0; i<8; i++) {
|
|
|
|
channels[i] = 0xFFFF;
|
|
|
|
}
|
|
|
|
value = next_value = 0;
|
|
|
|
idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
assignment of UDR2 selects which ADC channel
|
|
|
|
to output next
|
|
|
|
*/
|
|
|
|
ADC_UDR2& operator=(uint8_t cmd) {
|
2011-11-28 01:01:23 -04:00
|
|
|
float next_analog;
|
2012-02-27 06:58:58 -04:00
|
|
|
uint8_t chan;
|
2011-11-16 21:49:56 -04:00
|
|
|
switch (cmd) {
|
2012-02-27 06:58:58 -04:00
|
|
|
case 0x87: chan = 0; break;
|
|
|
|
case 0xC7: chan = 1; break;
|
|
|
|
case 0x97: chan = 2; break;
|
|
|
|
case 0xD7: chan = 3; break;
|
|
|
|
case 0xA7: chan = 4; break;
|
|
|
|
case 0xE7: chan = 5; break;
|
|
|
|
case 0xB7: chan = 6; break;
|
|
|
|
case 0xF7: chan = 7; break;
|
2011-11-28 01:01:23 -04:00
|
|
|
case 0:
|
|
|
|
default: return *this;
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|
2012-02-27 06:58:58 -04:00
|
|
|
next_analog = channels[chan];
|
2011-11-28 01:01:23 -04:00
|
|
|
idx = 1;
|
2012-03-02 03:29:27 -04:00
|
|
|
next_analog += noise_generator(chan) + 0.5;
|
|
|
|
if (next_analog > 0xFFF) next_analog = 0xFFF;
|
|
|
|
if (next_analog < 0) next_analog = 0;
|
|
|
|
next_value = ((unsigned)next_analog) << 3;
|
2011-11-16 21:49:56 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
read from UDR2 fetches a byte from the channel
|
|
|
|
*/
|
|
|
|
operator int() {
|
2011-11-25 00:48:52 -04:00
|
|
|
uint8_t ret;
|
|
|
|
if (idx & 1) {
|
|
|
|
ret = (value&0xFF);
|
|
|
|
value = next_value;
|
|
|
|
} else {
|
|
|
|
ret = (value>>8);
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|
2011-11-25 00:48:52 -04:00
|
|
|
idx ^= 1;
|
|
|
|
return ret;
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
interface to set a channel value from SITL
|
|
|
|
*/
|
2011-11-28 01:01:23 -04:00
|
|
|
void set(uint8_t i, float v) {
|
2011-11-16 21:49:56 -04:00
|
|
|
channels[i] = v;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _SITL_ADC_H
|