Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 749 → Rev 750

/branches/V0.68d Code Redesign killagreg/analog.c
15,17 → 15,15
#include "printf_P.h"
#include "eeprom.h"
 
volatile int16_t Current_Pitch = 0, Current_Roll = 0, Current_Yaw = 0;
volatile int16_t Current_AccX = 0, Current_AccY = 0, Current_AccZ = 0;
volatile int16_t Current_AccZ = 0;
volatile int16_t UBat = 100;
volatile int16_t AdValueGyrPitch = 0, AdValueGyrRoll = 0, AdValueGyrYaw = 0;
volatile int16_t AdValueAccRoll = 0, AdValueAccPitch = 0, AdValueAccTop = 0;
volatile int16_t AdValueGyrPitch = 0, AdValueGyrRoll = 0, AdValueGyrYaw = 0;
volatile int16_t AdValueAccRoll = 0, AdValueAccPitch = 0, AdValueAccTop = 0;
volatile int32_t AirPressure = 32000;
volatile int16_t StartAirPressure;
volatile uint16_t ReadingAirPressure = 1023;
uint8_t PressureSensorOffset;
volatile int16_t HightD = 0;
volatile int16_t tmpAirPressure;
volatile uint16_t MeasurementCounter = 0;
 
/*****************************************************/
60,22 → 58,22
 
void SearchAirPressureOffset(void)
{
uint8_t off;
off = GetParamByte(PID_LAST_OFFSET);
if(off > 20) off -= 10;
OCR0A = off;
Delay_ms_Mess(100);
if(ReadingAirPressure < 850) off = 0;
for(; off < 250;off++)
{
OCR0A = off;
Delay_ms_Mess(50);
printf(".");
if(ReadingAirPressure < 900) break;
}
SetParamByte(PID_LAST_OFFSET, off);
PressureSensorOffset = off;
Delay_ms_Mess(300);
uint8_t off;
off = GetParamByte(PID_PRESSURE_OFFSET);
if(off > 20) off -= 10;
OCR0A = off;
Delay_ms_Mess(100);
if(ReadingAirPressure < 850) off = 0;
for(; off < 250;off++)
{
OCR0A = off;
Delay_ms_Mess(50);
printf(".");
if(ReadingAirPressure < 900) break;
}
SetParamByte(PID_PRESSURE_OFFSET, off);
PressureSensorOffset = off;
Delay_ms_Mess(300);
}
 
 
82,11 → 80,26
/*****************************************************/
/* Interrupt Service Routine for ADC */
/*****************************************************/
// The routine changes the ADC input muxer running
// thru the state machine by the following order.
// state 0: ch0 (yaw gyro)
// state 1: ch1 (roll gyro)
// state 2: ch2 (pitch gyro)
// state 3: ch4 (battery voltage -> UBat)
// state 4: ch6 (acc y -> Current_AccY)
// state 5: ch7 (acc x -> Current_AccX)
// state 6: ch0 (yaw gyro average with first reading -> AdValueGyrYaw)
// state 7: ch1 (roll gyro average with first reading -> AdValueGyrRoll)
// state 8: ch2 (pitch gyro average with first reading -> AdValueGyrPitch)
// state 9: ch5 (acc z add also 4th part of acc x and acc y to reading)
// state10: ch3 (air pressure averaging over 5 single readings -> tmpAirPressure)
 
ISR(ADC_vect)
{
static uint8_t adc_channel = 0, state = 0;
static uint16_t yaw1, roll1, pitch1;
static uint8_t average_pressure = 0;
static int16_t tmpAirPressure = 0;
// disable further AD conversion
ADC_Disable();
// state machine
111,44 → 124,42
adc_channel = 6; // set next channel to ADC6 = ACC_Y
break;
case 4:
Current_AccY = NeutralAccY - ADC; // get acceleration in Y direction
AdValueAccRoll = Current_AccY;
AdValueAccRoll = NeutralAccY - ADC; // get acceleration in Y direction
adc_channel = 7; // set next channel to ADC7 = ACC_X
break;
case 5:
Current_AccX = ADC - NeutralAccX; // get acceleration in X direction
AdValueAccPitch = Current_AccX;
AdValueAccPitch = ADC - NeutralAccX; // get acceleration in X direction
adc_channel = 0; // set next channel to ADC7 = YAW GYRO
break;
case 6:
// average over two samples to create current AdValueGyrYaw
if(BoardRelease == 10) AdValueGyrYaw = (ADC + yaw1) / 2;
else AdValueGyrYaw = ADC + yaw1; // gain is 2 times lower on FC 1.1
else AdValueGyrYaw = ADC + yaw1; // gain is 2 times lower on FC 1.1
adc_channel = 1; // set next channel to ADC7 = ROLL GYRO
break;
case 7:
// average over two samples to create current ADValueGyrRoll
if(BoardRelease == 10) AdValueGyrRoll = (ADC + roll1) / 2;
else AdValueGyrRoll = ADC + roll1; // gain is 2 times lower on FC 1.1
else AdValueGyrRoll = ADC + roll1; // gain is 2 times lower on FC 1.1
adc_channel = 2; // set next channel to ADC2 = PITCH GYRO
break;
case 8:
// average over two samples to create current ADValuePitch
if(BoardRelease == 10) AdValueGyrPitch = (ADC + pitch1) / 2;
else AdValueGyrPitch = ADC + pitch1; // gain is 2 times lower on FC 1.1
else AdValueGyrPitch = ADC + pitch1; // gain is 2 times lower on FC 1.1
adc_channel = 5; // set next channel to ADC5 = ACC_Z
break;
case 9:
// get z acceleration
AdValueAccTop = (int16_t) ADC - NeutralAccZ; // get plain acceleration in Z direction
AdValueAccTop += abs(Current_AccY) / 4 + abs(Current_AccX) / 4;
AdValueAccTop += abs(AdValueAccPitch) / 4 + abs(AdValueAccRoll) / 4;
if(AdValueAccTop > 1)
{
if(NeutralAccZ < 800) NeutralAccZ+= 0.02;
if(NeutralAccZ < 800) NeutralAccZ+= 0.02;
}
else if(AdValueAccTop < -1)
{
if(NeutralAccZ > 600) NeutralAccZ-= 0.02;
if(NeutralAccZ > 600) NeutralAccZ-= 0.02;
}
Current_AccZ = ADC;
Reading_Integral_Top += AdValueAccTop; // Integrieren
158,16 → 169,16
case 10:
tmpAirPressure += ADC; // sum vadc values
if(++average_pressure >= 5) // if 5 values are summerized for averaging
{
{
ReadingAirPressure = ADC; // update measured air pressure
average_pressure = 0; // reset air pressure measurement counter
HightD = (int16_t)(StartAirPressure - tmpAirPressure - ReadingHight); // D-Anteil = neuerWert - AlterWert
AirPressure = (tmpAirPressure + 3 * AirPressure) / 4; // averaging using history
ReadingHight = StartAirPressure - AirPressure;
average_pressure = 0; // reset air pressure measurement counter
tmpAirPressure = 0;
}
}
adc_channel = 0; // set next channel to ADC0 = GIER GYRO
state = 0; // reset state
state = 0; // reset state machine
break;
default:
adc_channel = 0;
176,6 → 187,6
}
// set adc muxer to next adc_channel
ADMUX = (ADMUX & 0xE0) | adc_channel;
// ??
// after full cycle stop further interrupts
if(state != 0) ADC_Enable();
}