Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 1635 → Rev 1645

/branches/dongfang_FC_rewrite/analog.c
65,41 → 65,34
#include "eeprom.h"
 
/*
* Arrays could have been used for the 2 * 3 axes, but despite some repetition,
* the code is easier to read without.
*
* For each A/D conversion cycle, each channel (eg. the yaw gyro, or the Z axis
* accelerometer) is sampled a number of times (see array channelsForStates), and
* the results for each channel are summed. Here are those for the gyros and the
* acc. meters. They are not zero-offset.
* For each A/D conversion cycle, each analog channel is sampled a number of times
* (see array channelsForStates), and the results for each channel are summed.
* Here are those for the gyros and the acc. meters. They are not zero-offset.
* They are exported in the analog.h file - but please do not use them! The only
* reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating
* the offsets with the DAC.
*/
volatile int16_t rawPitchGyroSum, rawRollGyroSum, rawYawGyroSum;
volatile int16_t pitchAxisAcc = 0, rollAxisAcc = 0, ZAxisAcc = 0;
volatile int16_t filteredPitchAxisAcc = 0, filteredRollAxisAcc = 0;
volatile int16_t rawGyroSum[2], rawYawGyroSum;
volatile int16_t acc[2] = {0,0}, ZAcc = 0;
volatile int16_t filteredAcc[2] = {0,0};
 
// that float one - "Top" - is missing.
 
/*
* These 4 exported variables are zero-offset. The "filtered" ones are
* (if configured to with the GYROS_SECONDORDERFILTER define) low pass
* filtered versions of the other 2.
* They are derived from the "raw" values above, by zero-offsetting.
* These 4 exported variables are zero-offset. The "PID" ones are used
* in the attitude control as rotation rates. The "ATT" ones are for
* integration to angles.
*/
volatile int16_t hiResPitchGyro = 0, hiResRollGyro = 0;
volatile int16_t filteredHiResPitchGyro = 0, filteredHiResRollGyro = 0;
volatile int16_t pitchGyroD = 0, rollGyroD = 0;
volatile int16_t gyro_PID[2];
volatile int16_t gyro_ATT[2];
volatile int16_t gyroD[2];
volatile int16_t yawGyro = 0;
 
/*
* Offset values. These are the raw gyro and acc. meter sums when the copter is
* standing still. They are used for adjusting the gyro and acc. meter values
* to be zero when the copter stands still.
* to be centered on zero.
*/
volatile int16_t pitchOffset, rollOffset, yawOffset;
volatile int16_t pitchAxisAccOffset, rollAxisAccOffset, ZAxisAccOffset;
volatile int16_t gyroOffset[2], yawGyroOffset;
volatile int16_t accOffset[2], ZAccOffset;
 
/*
* This allows some experimentation with the gyro filters.
110,12 → 103,14
volatile uint8_t GYROS_DFILTER;
volatile uint8_t ACC_FILTER;
 
// Air pressure (no support right now).
// volatile int32_t AirPressure = 32000;
// volatile uint8_t average_pressure = 0;
// volatile int16_t StartAirPressure;
// volatile uint16_t ReadingAirPressure = 1023;
// volatile int16_t HeightD = 0;
/*
* Air pressure measurement.
*/
#define MIN_RAWPRESSURE 200
#define MAX_RAWPRESSURE (1023-MIN_RAWPRESSURE)
volatile uint8_t rangewidth = 53;
volatile uint16_t rawAirPressure;
volatile uint16_t filteredAirPressure;
 
/*
* Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt.
124,8 → 119,6
*/
volatile int16_t UBat = 100;
 
volatile int16_t filteredAirPressure;
 
/*
* Control and status.
*/
135,18 → 128,18
/*
* Experiment: Measuring vibration-induced sensor noise.
*/
volatile uint16_t pitchGyroNoisePeak, rollGyroNoisePeak;
volatile uint16_t pitchAccNoisePeak, rollAccNoisePeak;
volatile uint16_t gyroNoisePeak[2];
volatile uint16_t accNoisePeak[2];
 
// ADC channels
#define AD_GYRO_YAW 0
#define AD_GYRO_ROLL 1
#define AD_GYRO_YAW 0
#define AD_GYRO_ROLL 1
#define AD_GYRO_PITCH 2
#define AD_AIRPRESSURE 3
#define AD_UBAT 4
#define AD_ACC_Z 5
#define AD_ACC_ROLL 6
#define AD_ACC_PITCH 7
#define AD_UBAT 4
#define AD_ACC_Z 5
#define AD_ACC_ROLL 6
#define AD_ACC_PITCH 7
 
/*
* Table of AD converter inputs for each state.
228,13 → 221,8
}
}
 
 
#define ADCENTER (1023/2)
#define HALFRANGE 400
uint8_t stepsize = 53;
 
uint16_t getAbsPressure(int advalue) {
return (uint16_t)OCR0A * (uint16_t)stepsize + advalue;
return (uint16_t)OCR0A * (uint16_t)rangewidth + advalue;
}
 
uint16_t filterAirPressure(uint16_t rawpressure) {
241,22 → 229,21
return rawpressure;
}
 
/*****************************************************/
/* Interrupt Service Routine for ADC */
/*****************************************************/
// Runs at 312.5 kHz or 3.2 µs
// When all states are processed the interrupt is disabled
// and the update of further AD conversions is stopped.
 
/*****************************************************
* Interrupt Service Routine for ADC
* Runs at 312.5 kHz or 3.2 µs. When all states are
* processed the interrupt is disabled and further
* AD conversions are stopped.
*****************************************************/
ISR(ADC_vect) {
static uint8_t ad_channel = AD_GYRO_PITCH, state = 0;
static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0};
 
uint8_t i;
int16_t step = OCR0A;
static uint8_t pressure_wait = 10;
uint8_t i, axis;
int16_t range;
// for various filters...
static int16_t pitchGyroFilter, rollGyroFilter, tempOffsetGyro;
int16_t tempOffsetGyro, tempGyro;
sensorInputs[ad_channel] += ADC;
 
267,9 → 254,9
switch(state++) {
case 7: // Z acc
#ifdef ACC_REVERSE_ZAXIS
ZAxisAcc = -ZAxisAccOffset - sensorInputs[AD_ACC_Z];
ZAcc = -ZAccOffset - sensorInputs[AD_ACC_Z];
#else
ZAxisAcc = sensorInputs[AD_ACC_Z] - ZAxisAccOffset;
ZAcc = sensorInputs[AD_ACC_Z] - ZAccOffset;
#endif
break;
276,84 → 263,116
case 10: // yaw gyro
rawYawGyroSum = sensorInputs[AD_GYRO_YAW];
#ifdef GYRO_REVERSE_YAW
yawGyro = rawYawGyroSum - yawOffset;
yawGyro = rawYawGyroSum - yawGyroOffset;
#else
yawGyro = yawOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3).
yawGyro = yawGyroOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3).
#endif
break;
case 11: // pitch axis acc.
#ifdef ACC_REVERSE_PITCHAXIS
pitchAxisAcc = -pitchAxisAccOffset - sensorInputs[AD_ACC_PITCH];
acc[PITCH] = -accOffset[PITCH] - sensorInputs[AD_ACC_PITCH];
#else
pitchAxisAcc = sensorInputs[AD_ACC_PITCH] - pitchAxisAccOffset;
acc[PITCH] = sensorInputs[AD_ACC_PITCH] - accOffset[PITCH];
#endif
filteredPitchAxisAcc = (filteredPitchAxisAcc * (ACC_FILTER-1) + pitchAxisAcc) / ACC_FILTER;
filteredAcc[PITCH] = (filteredAcc[PITCH] * (ACC_FILTER-1) + acc[PITCH]) / ACC_FILTER;
 
measureNoise(pitchAxisAcc, &pitchAccNoisePeak, 1);
measureNoise(acc[PITCH], &accNoisePeak[PITCH], 1);
break;
case 12: // roll axis acc.
#ifdef ACC_REVERSE_ROLLAXIS
rollAxisAcc = sensorInputs[AD_ACC_ROLL] - rollAxisAccOffset;
acc[ROLL] = sensorInputs[AD_ACC_ROLL] - accOffset[ROLL];
#else
rollAxisAcc = -rollAxisAccOffset - sensorInputs[AD_ACC_ROLL];
acc[ROLL] = -accOffset[ROLL] - sensorInputs[AD_ACC_ROLL];
#endif
filteredRollAxisAcc = (filteredRollAxisAcc * (ACC_FILTER-1) + rollAxisAcc) / ACC_FILTER;
measureNoise(rollAxisAcc, &rollAccNoisePeak, 1);
filteredAcc[ROLL] = (filteredAcc[ROLL] * (ACC_FILTER-1) + acc[ROLL]) / ACC_FILTER;
measureNoise(acc[ROLL], &accNoisePeak[ROLL], 1);
break;
 
case 13: // air pressure
if (sensorInputs[AD_AIRPRESSURE] < ADCENTER-HALFRANGE) {
if (pressure_wait) {
// A range switch was done recently. Wait for steadying.
pressure_wait--;
break;
}
range = OCR0A;
rawAirPressure = sensorInputs[AD_AIRPRESSURE];
if (rawAirPressure < MIN_RAWPRESSURE) {
// value is too low, so decrease voltage on the op amp minus input, making the value higher.
step -= ((HALFRANGE-sensorInputs[AD_AIRPRESSURE]) / stepsize + 1);
if (step<0) step = 0;
OCR0A = step;
// wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off)
} else if (sensorInputs[AD_AIRPRESSURE] > ADCENTER+HALFRANGE) {
range -= (MAX_RAWPRESSURE - rawAirPressure) / rangewidth - 1;
if (range < 0) range = 0;
pressure_wait = (OCR0A - range) * 4;
OCR0A = range;
} else if (rawAirPressure > MAX_RAWPRESSURE) {
// value is too high, so increase voltage on the op amp minus input, making the value lower.
step += ((sensorInputs[AD_AIRPRESSURE] - HALFRANGE)/stepsize + 1);
if (step>254) step = 254;
OCR0A = step;
// wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off)
range += (rawAirPressure - MIN_RAWPRESSURE) / rangewidth - 1;
if (range > 254) range = 254;
pressure_wait = (range - OCR0A) * 4;
OCR0A = range;
} else {
filteredAirPressure = filterAirPressure(getAbsPressure(sensorInputs[AD_AIRPRESSURE]));
filteredAirPressure = filterAirPressure(getAbsPressure(rawAirPressure));
}
DebugOut.Analog[12] = range;
DebugOut.Analog[13] = rawAirPressure;
DebugOut.Analog[14] = filteredAirPressure;
break;
 
case 14: // pitch gyro
rawPitchGyroSum = sensorInputs[AD_GYRO_PITCH];
// Filter already before offsetting. The offsetting resolution improvement obtained by divding by
// GYROS_FIRSTORDERFILTER _after_ offsetting is too small to be worth pursuing.
pitchGyroFilter = (pitchGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawPitchGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER;
// Offset to 0.
#ifdef GYROS_REVERSE_PITCH
tempOffsetGyro = pitchOffset - pitchGyroFilter;
#else
tempOffsetGyro = pitchGyroFilter - pitchOffset;
#endif
// Calculate the delta from last shot and filter it.
pitchGyroD = (pitchGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResPitchGyro)) / GYROS_DFILTER;
// How we can overwrite the last value. This value is used for the D part of the PID controller.
hiResPitchGyro = tempOffsetGyro;
// Filter a little more. This value is used in integration to angles.
filteredHiResPitchGyro = (filteredHiResPitchGyro * (GYROS_SECONDORDERFILTER-1) + hiResPitchGyro) / GYROS_SECONDORDERFILTER;
measureNoise(hiResPitchGyro, &pitchGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING);
case 14:
case 15: // pitch or roll gyro.
axis = state - 15;
tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH - axis];
// DebugOut.Analog[6 + 3 * axis ] = tempGyro;
/*
* Process the gyro data for the PID controller.
*/
// 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a
// gyro with a wider range, and helps counter saturation at full control.
 
if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) {
if (tempGyro < SENSOR_MIN_PITCHROLL) {
tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT;
}
else if (tempGyro > SENSOR_MAX_PITCHROLL) {
tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL;
}
}
 
// 2) Apply sign and offset, scale before filtering.
if (GYROS_REVERSE[axis]) {
tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
} else {
tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
}
 
// 3) Scale and filter.
tempOffsetGyro = (gyro_PID[axis] * (GYROS_PIDFILTER-1) + tempOffsetGyro) / GYROS_PIDFILTER;
 
// 4) Measure noise.
measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING);
 
// 5) Differential measurement.
gyroD[axis] = (gyroD[axis] * (GYROS_DFILTER-1) + (tempOffsetGyro - gyro_PID[axis])) / GYROS_DFILTER;
 
// 6) Done.
gyro_PID[axis] = tempOffsetGyro;
 
/*
* Now process the data for attitude angles.
*/
tempGyro = rawGyroSum[axis];
// 1) Apply sign and offset, scale before filtering.
if (GYROS_REVERSE[axis]) {
tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
} else {
tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
}
// 2) Filter.
gyro_ATT[axis] = (gyro_ATT[axis] * (GYROS_INTEGRALFILTER-1) + tempOffsetGyro) / GYROS_INTEGRALFILTER;
break;
case 15: // Roll gyro. Works the same as pitch.
rawRollGyroSum = sensorInputs[AD_GYRO_ROLL];
rollGyroFilter = (rollGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawRollGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER;
#ifdef GYRO_REVERSE_ROLL
tempOffsetGyro = rollOffset - rollGyroFilter;
#else
tempOffsetGyro = rollGyroFilter - rollOffset;
#endif
rollGyroD = (rollGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResRollGyro)) / GYROS_DFILTER;
hiResRollGyro = tempOffsetGyro;
filteredHiResRollGyro = (filteredHiResRollGyro * (GYROS_SECONDORDERFILTER-1) + hiResRollGyro) / GYROS_SECONDORDERFILTER;
measureNoise(hiResRollGyro, &rollGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING);
break;
case 16:
// battery
390,7 → 409,7
GYROS_DFILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1;
ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1;
 
pitchOffset = rollOffset = yawOffset = 0;
gyroOffset[PITCH] = gyroOffset[ROLL] = yawGyroOffset = 0;
 
gyro_calibrate();
 
397,28 → 416,25
// determine gyro bias by averaging (requires that the copter does not rotate around any axis!)
for(i=0; i < GYRO_OFFSET_CYCLES; i++) {
Delay_ms_Mess(10);
_pitchOffset += rawPitchGyroSum * GYRO_FACTOR_PITCHROLL;
_rollOffset += rawRollGyroSum * GYRO_FACTOR_PITCHROLL;
_pitchOffset += rawGyroSum[PITCH];
_rollOffset += rawGyroSum[ROLL];
_yawOffset += rawYawGyroSum;
}
pitchOffset = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
rollOffset = (_rollOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
yawOffset = (_yawOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
gyroOffset[PITCH] = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
gyroOffset[ROLL] = (_rollOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
yawGyroOffset = (_yawOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
filteredHiResPitchGyro = filteredHiResRollGyro = 0;
 
pitchAxisAccOffset = (int16_t)GetParamWord(PID_ACC_NICK);
rollAxisAccOffset = (int16_t)GetParamWord(PID_ACC_ROLL);
ZAxisAccOffset = (int16_t)GetParamWord(PID_ACC_TOP);
gyro_PID[PITCH] = gyro_PID[ROLL] = 0;
gyro_ATT[PITCH] = gyro_ATT[ROLL] = 0;
// Noise is relative to offset. So, reset noise measurements when
// changing offsets.
pitchGyroNoisePeak = rollGyroNoisePeak = 0;
gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0;
 
// Setting offset values has an influence in the analog.c ISR
// Therefore run measurement for 100ms to achive stable readings
Delay_ms_Mess(100);
accOffset[PITCH] = (int16_t)GetParamWord(PID_ACC_PITCH);
accOffset[ROLL] = (int16_t)GetParamWord(PID_ACC_ROLL);
ZAccOffset = (int16_t)GetParamWord(PID_ACC_TOP);
}
 
/*
432,22 → 448,54
#define ACC_OFFSET_CYCLES 10
uint8_t i;
int32_t _pitchAxisOffset = 0, _rollAxisOffset = 0, _ZAxisOffset = 0;
// int16_t pressureDiff, savedRawAirPressure;
pitchAxisAccOffset = rollAxisAccOffset = ZAxisAccOffset = 0;
accOffset[PITCH] = accOffset[ROLL] = ZAccOffset = 0;
 
for(i=0; i < ACC_OFFSET_CYCLES; i++) {
Delay_ms_Mess(10);
_pitchAxisOffset += pitchAxisAcc;
_rollAxisOffset += rollAxisAcc;
_ZAxisOffset += ZAxisAcc;
_pitchAxisOffset += acc[PITCH];
_rollAxisOffset += acc[ROLL];
_ZAxisOffset += ZAcc;
}
 
// Save ACC neutral settings to eeprom
SetParamWord(PID_ACC_NICK, (uint16_t)((_pitchAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
SetParamWord(PID_ACC_PITCH, (uint16_t)((_pitchAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
SetParamWord(PID_ACC_ROLL, (uint16_t)((_rollAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
SetParamWord(PID_ACC_TOP, (uint16_t)((_ZAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES));
 
// Noise is relative to offset. So, reset noise measurements when
// changing offsets.
pitchAccNoisePeak = rollAccNoisePeak = 0;
accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0;
// Setting offset values has an influence in the analog.c ISR
// Therefore run measurement for 100ms to achive stable readings
// Delay_ms_Mess(100);
// Set the feedback so that air pressure ends up in the middle of the range.
// (raw pressure high --> OCR0A also high...)
// OCR0A += (rawAirPressure - 512) / rangewidth;
// Delay_ms_Mess(500);
 
/*
pressureDiff = 0;
DebugOut.Analog[16] = rawAirPressure;
 
#define PRESSURE_CAL_CYCLE_COUNT 2
for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) {
savedRawAirPressure = rawAirPressure;
OCR0A++;
Delay_ms_Mess(200);
// raw pressure will decrease.
pressureDiff += (savedRawAirPressure - rawAirPressure);
 
savedRawAirPressure = rawAirPressure;
OCR0A--;
Delay_ms_Mess(200);
// raw pressure will increase.
pressureDiff += (rawAirPressure - savedRawAirPressure);
}
 
DebugOut.Analog[15] = rangewidth =
(pressureDiff + PRESSURE_CAL_CYCLE_COUNT * 2 - 1) / (PRESSURE_CAL_CYCLE_COUNT * 2);
*/
}