68,6 → 68,9 |
// For DebugOut.Digital |
#include "output.h" |
|
// set ADC enable & ADC Start Conversion & ADC Interrupt Enable bit |
#define startADC() (ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE)) |
|
/* |
* 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. |
76,6 → 79,7 |
* reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
* the offsets with the DAC. |
*/ |
volatile uint16_t sensorInputs[8]; |
volatile int16_t rawGyroSum[3]; |
volatile int16_t acc[3]; |
volatile int16_t filteredAcc[2] = { 0,0 }; |
203,18 → 207,18 |
// Disable digital input buffer for analog adc_channel pins |
DIDR0 = 0xFF; |
// external reference, adjust data to the right |
ADMUX &= ~((1 << REFS1) | (1 << REFS0) | (1 << ADLAR)); |
ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
// set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH; |
ADMUX = (ADMUX & 0xE0) | channelsForStates[0]; |
//Set ADC Control and Status Register A |
//Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
ADCSRA = (0 << ADEN) | (0 << ADSC) | (0 << ADATE) | (1 << ADPS2) | (1 |
<< ADPS1) | (1 << ADPS0) | (0 << ADIE); |
ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
//Set ADC Control and Status Register B |
//Trigger Source to Free Running Mode |
ADCSRB &= ~((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0)); |
// Start AD conversion |
analog_start(); |
ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
|
startAnalogConversionCycle(); |
|
// restore global interrupt flags |
SREG = sreg; |
} |
240,291 → 244,269 |
return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
} |
|
void startAnalogConversionCycle(void) { |
// Stop the sampling. Cycle is over. |
for (uint8_t i = 0; i < 8; i++) { |
sensorInputs[i] = 0; |
} |
ADMUX = (ADMUX & 0xE0) | channelsForStates[0]; |
startADC(); |
} |
|
/***************************************************** |
* 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. |
* processed further 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 }; |
static uint16_t pressureAutorangingWait = 25; |
uint16_t rawAirPressure; |
uint8_t i, axis; |
int16_t newrange; |
static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
sensorInputs[ad_channel] += ADC; |
// set up for next state. |
state++; |
if (state < 18) { |
ad_channel = pgm_read_byte(&channelsForStates[state]); |
// set adc muxer to next ad_channel |
ADMUX = (ADMUX & 0xE0) | ad_channel; |
// after full cycle stop further interrupts |
startADC(); |
} else { |
state = 0; |
ADCycleCount++; |
analogDataReady = 1; |
// do not restart ADC converter. |
} |
} |
|
// for various filters... |
int16_t tempOffsetGyro, tempGyro; |
void analog_updateGyros(void) { |
// for various filters... |
int16_t tempOffsetGyro, tempGyro; |
|
for (uint8_t axis=0; axis<2; axis++) { |
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) { |
DebugOut.Digital[0] |= DEBUG_SENSORLIMIT; |
tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
} else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
DebugOut.Digital[0] |= DEBUG_SENSORLIMIT; |
tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE |
+ SENSOR_MAX_PITCHROLL; |
} else { |
DebugOut.Digital[0] &= ~DEBUG_SENSORLIMIT; |
} |
} |
|
// 2) Apply sign and offset, scale before filtering. |
if (GYRO_REVERSED[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_PID_FILTER - 1) + tempOffsetGyro) / GYROS_PID_FILTER; |
|
// 4) Measure noise. |
measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
|
// 5) Differential measurement. |
gyroD[axis] = (gyroD[axis] * (GYROS_D_FILTER - 1) + (tempOffsetGyro - gyro_PID[axis])) / GYROS_D_FILTER; |
|
// 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 (GYRO_REVERSED[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_ATT_FILTER - 1) + tempOffsetGyro) / GYROS_ATT_FILTER; |
} |
|
// Yaw gyro. |
rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW]; |
if (GYRO_REVERSED[YAW]) |
yawGyro = gyroOffset[YAW] - sensorInputs[AD_GYRO_YAW]; |
else |
yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset[YAW]; |
} |
|
sensorInputs[ad_channel] += ADC; |
void analog_updateAccelerometers(void) { |
// Pitch and roll axis accelerations. |
for (uint8_t axis=0; axis<2; axis++) { |
if (ACC_REVERSED[axis]) |
acc[axis] = accOffset[axis] - sensorInputs[AD_ACC_PITCH-axis]; |
else |
acc[axis] = sensorInputs[AD_ACC_PITCH-axis] - accOffset[axis]; |
|
filteredAcc[axis] = (filteredAcc[axis] * (ACC_FILTER - 1) + acc[axis]) / ACC_FILTER; |
|
/* |
stronglyFilteredAcc[PITCH] = |
(stronglyFilteredAcc[PITCH] * 99 + acc[PITCH] * 10) / 100; |
*/ |
|
measureNoise(acc[axis], &accNoisePeak[axis], 1); |
} |
|
// Z acc. |
if (ACC_REVERSED[Z]) |
acc[Z] = accOffset[Z] - sensorInputs[AD_ACC_Z]; |
else |
acc[Z] = sensorInputs[AD_ACC_Z] - accOffset[Z]; |
|
/* |
* Actually we don't need this "switch". We could do all the sampling into the |
* sensorInputs array first, and all the processing after the last sample. |
*/ |
switch (state++) { |
/* |
stronglyFilteredAcc[Z] = |
(stronglyFilteredAcc[Z] * 99 + acc[Z] * 10) / 100; |
*/ |
} |
|
case 8: // Z acc |
if (ACC_REVERSED[Z]) |
acc[Z] = accOffset[Z] - sensorInputs[AD_ACC_Z]; |
else |
acc[Z] = sensorInputs[AD_ACC_Z] - accOffset[Z]; |
|
/* |
stronglyFilteredAcc[Z] = |
(stronglyFilteredAcc[Z] * 99 + acc[Z] * 10) / 100; |
*/ |
|
break; |
|
case 11: // yaw gyro |
rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW]; |
if (GYRO_REVERSED[YAW]) |
yawGyro = gyroOffset[YAW] - sensorInputs[AD_GYRO_YAW]; |
else |
yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset[YAW]; |
break; |
|
case 12: // pitch axis acc. |
if (ACC_REVERSED[PITCH]) |
acc[PITCH] = accOffset[PITCH] - sensorInputs[AD_ACC_PITCH]; |
else |
acc[PITCH] = sensorInputs[AD_ACC_PITCH] - accOffset[PITCH]; |
|
filteredAcc[PITCH] = |
(filteredAcc[PITCH] * (ACC_FILTER - 1) + acc[PITCH]) / ACC_FILTER; |
|
/* |
stronglyFilteredAcc[PITCH] = |
(stronglyFilteredAcc[PITCH] * 99 + acc[PITCH] * 10) / 100; |
*/ |
|
measureNoise(acc[PITCH], &accNoisePeak[PITCH], 1); |
break; |
|
case 13: // roll axis acc. |
if (ACC_REVERSED[ROLL]) |
acc[ROLL] = accOffset[ROLL] - sensorInputs[AD_ACC_ROLL]; |
else |
acc[ROLL] = sensorInputs[AD_ACC_ROLL] - accOffset[ROLL]; |
filteredAcc[ROLL] = |
(filteredAcc[ROLL] * (ACC_FILTER - 1) + acc[ROLL]) / ACC_FILTER; |
|
/* |
stronglyFilteredAcc[ROLL] = |
(stronglyFilteredAcc[ROLL] * 99 + acc[ROLL] * 10) / 100; |
*/ |
|
measureNoise(acc[ROLL], &accNoisePeak[ROLL], 1); |
break; |
|
case 14: // air pressure |
if (pressureAutorangingWait) { |
//A range switch was done recently. Wait for steadying. |
pressureAutorangingWait--; |
DebugOut.Analog[27] = (uint16_t) OCR0A; |
DebugOut.Analog[31] = simpleAirPressure; |
break; |
} |
|
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. |
newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
if (newrange > MIN_RANGES_EXTRAPOLATION) { |
pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
OCR0A = newrange; |
} else { |
if (OCR0A) { |
OCR0A--; |
pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
} |
} |
} else if (rawAirPressure > MAX_RAWPRESSURE) { |
// value is too high, so increase voltage on the op amp minus input, making the value lower. |
// If near the end, make a limited increase |
newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
if (newrange < MAX_RANGES_EXTRAPOLATION) { |
pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
OCR0A = newrange; |
} else { |
if (OCR0A < 254) { |
OCR0A++; |
pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
} |
} |
} |
|
// Even if the sample is off-range, use it. |
simpleAirPressure = getSimplePressure(rawAirPressure); |
DebugOut.Analog[27] = (uint16_t) OCR0A; |
DebugOut.Analog[31] = simpleAirPressure; |
|
if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
// Danger: pressure near lower end of range. If the measurement saturates, the |
// copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
DebugOut.Digital[1] |= DEBUG_SENSORLIMIT; |
airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
+ (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
* rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
} else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
// Danger: pressure near upper end of range. If the measurement saturates, the |
// copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
DebugOut.Digital[1] |= DEBUG_SENSORLIMIT; |
airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
+ (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
* rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
} else { |
// normal case. |
// If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample. |
// The 2 cases above (end of range) are ignored for this. |
DebugOut.Digital[1] &= ~DEBUG_SENSORLIMIT; |
if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1) |
airPressureSum += simpleAirPressure / 2; |
else |
airPressureSum += simpleAirPressure; |
} |
|
// 2 samples were added. |
pressureMeasurementCount += 2; |
if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) { |
filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1) |
+ airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER; |
pressureMeasurementCount = airPressureSum = 0; |
} |
|
break; |
|
case 15: |
case 16: // pitch or roll gyro. |
axis = state - 16; |
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) { |
DebugOut.Digital[0] |= DEBUG_SENSORLIMIT; |
tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
} else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
DebugOut.Digital[0] |= DEBUG_SENSORLIMIT; |
tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE |
+ SENSOR_MAX_PITCHROLL; |
} else { |
DebugOut.Digital[0] &= ~DEBUG_SENSORLIMIT; |
} |
} |
|
// 2) Apply sign and offset, scale before filtering. |
if (GYRO_REVERSED[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_PID_FILTER - 1) + tempOffsetGyro) |
/ GYROS_PID_FILTER; |
|
// 4) Measure noise. |
measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], |
GYRO_NOISE_MEASUREMENT_DAMPING); |
|
// 5) Differential measurement. |
gyroD[axis] = (gyroD[axis] * (GYROS_D_FILTER - 1) + (tempOffsetGyro |
- gyro_PID[axis])) / GYROS_D_FILTER; |
|
// 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 (GYRO_REVERSED[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_ATT_FILTER - 1) + tempOffsetGyro) |
/ GYROS_ATT_FILTER; |
break; |
|
case 17: |
// Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
// This is divided by 3 --> 10.34 counts per volt. |
UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
DebugOut.Analog[11] = UBat; |
analogDataReady = 1; // mark |
ADCycleCount++; |
// Stop the sampling. Cycle is over. |
state = 0; |
for (i = 0; i < 8; i++) { |
sensorInputs[i] = 0; |
} |
break; |
default: { |
} // do nothing. |
void analog_updateAirPressure(void) { |
static uint16_t pressureAutorangingWait = 25; |
uint16_t rawAirPressure; |
int16_t newrange; |
// air pressure |
if (pressureAutorangingWait) { |
//A range switch was done recently. Wait for steadying. |
pressureAutorangingWait--; |
DebugOut.Analog[27] = (uint16_t) OCR0A; |
DebugOut.Analog[31] = simpleAirPressure; |
} else { |
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. |
newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
if (newrange > MIN_RANGES_EXTRAPOLATION) { |
pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
OCR0A = newrange; |
} else { |
if (OCR0A) { |
OCR0A--; |
pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
} |
} |
} else if (rawAirPressure > MAX_RAWPRESSURE) { |
// value is too high, so increase voltage on the op amp minus input, making the value lower. |
// If near the end, make a limited increase |
newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
if (newrange < MAX_RANGES_EXTRAPOLATION) { |
pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
OCR0A = newrange; |
} else { |
if (OCR0A < 254) { |
OCR0A++; |
pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
} |
} |
} |
|
// Even if the sample is off-range, use it. |
simpleAirPressure = getSimplePressure(rawAirPressure); |
DebugOut.Analog[27] = (uint16_t) OCR0A; |
DebugOut.Analog[31] = simpleAirPressure; |
|
if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
// Danger: pressure near lower end of range. If the measurement saturates, the |
// copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
DebugOut.Digital[1] |= DEBUG_SENSORLIMIT; |
airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
+ (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
* rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
} else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
// Danger: pressure near upper end of range. If the measurement saturates, the |
// copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
DebugOut.Digital[1] |= DEBUG_SENSORLIMIT; |
airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
+ (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
* rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
} else { |
// normal case. |
// If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample. |
// The 2 cases above (end of range) are ignored for this. |
DebugOut.Digital[1] &= ~DEBUG_SENSORLIMIT; |
if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1) |
airPressureSum += simpleAirPressure / 2; |
else |
airPressureSum += simpleAirPressure; |
} |
|
// 2 samples were added. |
pressureMeasurementCount += 2; |
if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) { |
filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1) |
+ airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER; |
pressureMeasurementCount = airPressureSum = 0; |
} |
} |
} |
|
// set up for next state. |
ad_channel = pgm_read_byte(&channelsForStates[state]); |
// ad_channel = channelsForStates[state]; |
void analog_updateBatteryVoltage(void) { |
// Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
// This is divided by 3 --> 10.34 counts per volt. |
UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
DebugOut.Analog[11] = UBat; |
} |
|
// set adc muxer to next ad_channel |
ADMUX = (ADMUX & 0xE0) | ad_channel; |
// after full cycle stop further interrupts |
if (state) |
analog_start(); |
void analog_update(void) { |
analog_updateGyros(); |
analog_updateAccelerometers(); |
analog_updateAirPressure(); |
analog_updateBatteryVoltage(); |
} |
|
void analog_calibrate(void) { |
#define GYRO_OFFSET_CYCLES 32 |
uint8_t i, axis; |
int32_t deltaOffsets[3] = { 0, 0, 0 }; |
|
// Set the filters... to be removed again, once some good settings are found. |
GYROS_PID_FILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1; |
GYROS_ATT_FILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1; |
GYROS_D_FILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1; |
ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1; |
|
gyro_calibrate(); |
|
// 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(20); |
for (axis = PITCH; axis <= YAW; axis++) { |
deltaOffsets[axis] += rawGyroSum[axis]; |
} |
} |
|
for (axis = PITCH; axis <= YAW; axis++) { |
gyroOffset[axis] = (deltaOffsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
// DebugOut.Analog[20 + axis] = gyroOffset[axis]; |
} |
|
// Noise is relativ to offset. So, reset noise measurements when changing offsets. |
gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
|
accOffset[PITCH] = GetParamWord(PID_ACC_PITCH); |
accOffset[ROLL] = GetParamWord(PID_ACC_ROLL); |
accOffset[Z] = GetParamWord(PID_ACC_Z); |
|
// Rough estimate. Hmm no nothing happens at calibration anyway. |
// airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2); |
// pressureMeasurementCount = 0; |
|
delay_ms_Mess(100); |
uint8_t i, axis; |
int32_t deltaOffsets[3] = { 0, 0, 0 }; |
|
// Set the filters... to be removed again, once some good settings are found. |
GYROS_PID_FILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1; |
GYROS_ATT_FILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1; |
GYROS_D_FILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1; |
ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1; |
|
gyro_calibrate(); |
|
// 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(20); |
for (axis = PITCH; axis <= YAW; axis++) { |
deltaOffsets[axis] += rawGyroSum[axis]; |
} |
} |
|
for (axis = PITCH; axis <= YAW; axis++) { |
gyroOffset[axis] = (deltaOffsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
// DebugOut.Analog[20 + axis] = gyroOffset[axis]; |
} |
|
// Noise is relativ to offset. So, reset noise measurements when changing offsets. |
gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
|
accOffset[PITCH] = GetParamWord(PID_ACC_PITCH); |
accOffset[ROLL] = GetParamWord(PID_ACC_ROLL); |
accOffset[Z] = GetParamWord(PID_ACC_Z); |
|
// Rough estimate. Hmm no nothing happens at calibration anyway. |
// airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2); |
// pressureMeasurementCount = 0; |
|
delay_ms_Mess(100); |
} |
|
/* |