Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + only for non-profit use
4
// + www.MikroKopter.com
5
// + see the File "License.txt" for further Informations
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
687 killagreg 7
#include <stdlib.h>
8
#include <avr/io.h>
9
#include <avr/interrupt.h>
10
 
685 killagreg 11
#include "analog.h"
1 ingob 12
#include "main.h"
685 killagreg 13
#include "timer0.h"
14
#include "fc.h"
15
#include "printf_P.h"
687 killagreg 16
#include "eeprom.h"
1 ingob 17
 
750 killagreg 18
volatile int16_t Current_AccZ = 0;
683 killagreg 19
volatile int16_t UBat = 100;
750 killagreg 20
volatile int16_t AdValueGyrPitch = 0, AdValueGyrRoll = 0,  AdValueGyrYaw = 0;
21
volatile int16_t AdValueAccRoll = 0,  AdValueAccPitch = 0, AdValueAccTop = 0;
701 killagreg 22
volatile int32_t AirPressure = 32000;
23
volatile int16_t StartAirPressure;
24
volatile uint16_t ReadingAirPressure = 1023;
711 killagreg 25
uint8_t PressureSensorOffset;
761 killagreg 26
volatile int16_t HeightD = 0;
711 killagreg 27
volatile uint16_t MeasurementCounter = 0;
1 ingob 28
 
683 killagreg 29
/*****************************************************/
30
/*     Initialize Analog Digital Converter           */
31
/*****************************************************/
1 ingob 32
void ADC_Init(void)
683 killagreg 33
{
34
        uint8_t sreg = SREG;
35
        // disable all interrupts before reconfiguration
36
        cli();
37
        //ADC0 ... ADC7 is connected to PortA pin 0 ... 7
38
        DDRA = 0x00;
39
        PORTA = 0x00;
40
        // Digital Input Disable Register 0
41
        // Disable digital input buffer for analog adc_channel pins
42
        DIDR0 = 0xFF;
43
        // external reference, adjust data to the right
44
    ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR));
45
    // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
46
    ADMUX = (ADMUX & 0xE0) | 0x00;
47
    //Set ADC Control and Status Register A
48
    //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz
49
    ADCSRA = (1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
50
        //Set ADC Control and Status Register B
51
        //Trigger Source to Free Running Mode
52
        ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0));
53
        // Enable AD conversion
54
        ADC_Enable();
55
    // restore global interrupt flags
56
    SREG = sreg;
1 ingob 57
}
58
 
701 killagreg 59
void SearchAirPressureOffset(void)
1 ingob 60
{
750 killagreg 61
        uint8_t off;
62
        off = GetParamByte(PID_PRESSURE_OFFSET);
63
        if(off > 20) off -= 10;
64
        OCR0A = off;
65
        Delay_ms_Mess(100);
66
        if(ReadingAirPressure < 850) off = 0;
67
        for(; off < 250;off++)
68
        {
69
                OCR0A = off;
70
                Delay_ms_Mess(50);
71
                printf(".");
72
                if(ReadingAirPressure < 900) break;
73
        }
74
        SetParamByte(PID_PRESSURE_OFFSET, off);
75
        PressureSensorOffset = off;
76
        Delay_ms_Mess(300);
1 ingob 77
}
78
 
79
 
683 killagreg 80
/*****************************************************/
81
/*     Interrupt Service Routine for ADC             */
82
/*****************************************************/
826 killagreg 83
// runs at 156.25 kHz or 6.4 µs
84
// if after (70.4µs) all 11 states are processed the interrupt is disabled
85
// and the update of further ads is stopped
750 killagreg 86
// The routine changes the ADC input muxer running
87
// thru the state machine by the following order.
88
// state 0: ch0 (yaw gyro)
89
// state 1: ch1 (roll gyro)
90
// state 2: ch2 (pitch gyro)
91
// state 3: ch4 (battery voltage -> UBat)
92
// state 4: ch6 (acc y -> Current_AccY)
93
// state 5: ch7 (acc x -> Current_AccX)
94
// state 6: ch0 (yaw gyro average with first reading   -> AdValueGyrYaw)
95
// state 7: ch1 (roll gyro average with first reading  -> AdValueGyrRoll)
96
// state 8: ch2 (pitch gyro average with first reading -> AdValueGyrPitch)
97
// state 9: ch5 (acc z add also 4th part of acc x and acc y to reading)
98
// state10: ch3 (air pressure averaging over 5 single readings -> tmpAirPressure)
99
 
683 killagreg 100
ISR(ADC_vect)
1 ingob 101
{
683 killagreg 102
    static uint8_t adc_channel = 0, state = 0;
701 killagreg 103
    static uint16_t yaw1, roll1, pitch1;
703 killagreg 104
    static uint8_t average_pressure = 0;
750 killagreg 105
    static int16_t tmpAirPressure = 0;
683 killagreg 106
    // disable further AD conversion
107
    ADC_Disable();
108
    // state machine
1 ingob 109
    switch(state++)
110
        {
111
        case 0:
701 killagreg 112
            yaw1 = ADC; // get Gyro Yaw Voltage 1st sample
683 killagreg 113
            adc_channel = 1; // set next channel to ADC1 = ROLL GYRO
711 killagreg 114
            MeasurementCounter++; // increment total measurement counter
1 ingob 115
            break;
116
        case 1:
683 killagreg 117
            roll1 = ADC; // get Gyro Roll Voltage 1st sample
701 killagreg 118
            adc_channel = 2; // set next channel to ADC2 = PITCH GYRO
1 ingob 119
            break;
120
        case 2:
701 killagreg 121
            pitch1 = ADC; // get Gyro Pitch Voltage 1st sample
683 killagreg 122
            adc_channel = 4; // set next channel to ADC4 = UBAT
1 ingob 123
            break;
124
        case 3:
683 killagreg 125
                // get actual UBat (Volts*10) is ADC*30V/1024*10 = ADC/3
126
            UBat = (3 * UBat + ADC / 3) / 4; // low pass filter updates UBat only to 1 quater with actual ADC value
127
            adc_channel = 6; // set next channel to ADC6 = ACC_Y
1 ingob 128
            break;
129
        case 4:
750 killagreg 130
            AdValueAccRoll = NeutralAccY - ADC; // get acceleration in Y direction
683 killagreg 131
            adc_channel = 7; // set next channel to ADC7 = ACC_X
1 ingob 132
            break;
133
        case 5:
750 killagreg 134
            AdValueAccPitch = ADC - NeutralAccX; // get acceleration in X direction
701 killagreg 135
                    adc_channel = 0; // set next channel to ADC7 = YAW GYRO
1 ingob 136
            break;
137
        case 6:
701 killagreg 138
                // average over two samples to create current AdValueGyrYaw
139
            if(BoardRelease == 10)  AdValueGyrYaw = (ADC + yaw1) / 2;
750 killagreg 140
                        else                                    AdValueGyrYaw = ADC + yaw1; // gain is 2 times lower on FC 1.1
683 killagreg 141
            adc_channel = 1; // set next channel to ADC7 = ROLL GYRO
401 hbuss 142
            break;
143
        case 7:
701 killagreg 144
                // average over two samples to create current ADValueGyrRoll
685 killagreg 145
            if(BoardRelease == 10)  AdValueGyrRoll = (ADC + roll1) / 2;
750 killagreg 146
                        else                                    AdValueGyrRoll = ADC + roll1; // gain is 2 times lower on FC 1.1
701 killagreg 147
            adc_channel = 2; // set next channel to ADC2 = PITCH GYRO
401 hbuss 148
            break;
149
        case 8:
701 killagreg 150
                // average over two samples to create current ADValuePitch
151
            if(BoardRelease == 10)  AdValueGyrPitch = (ADC + pitch1) / 2;
750 killagreg 152
                        else                                    AdValueGyrPitch = ADC + pitch1; // gain is 2 times lower on FC 1.1
683 killagreg 153
            adc_channel = 5; // set next channel to ADC5 = ACC_Z
401 hbuss 154
            break;
155
       case 9:
683 killagreg 156
                // get z acceleration
157
            AdValueAccTop =  (int16_t) ADC - NeutralAccZ; // get plain acceleration in Z direction
750 killagreg 158
            AdValueAccTop += abs(AdValueAccPitch) / 4 + abs(AdValueAccRoll) / 4;
683 killagreg 159
            if(AdValueAccTop > 1)
1 ingob 160
             {
750 killagreg 161
                if(NeutralAccZ < 800) NeutralAccZ+= 0.02;
683 killagreg 162
             }
163
             else if(AdValueAccTop < -1)
1 ingob 164
             {
750 killagreg 165
                if(NeutralAccZ > 600) NeutralAccZ-= 0.02;
683 killagreg 166
             }
167
            Current_AccZ = ADC;
701 killagreg 168
            Reading_Integral_Top += AdValueAccTop;      // Integrieren
169
            Reading_Integral_Top -= Reading_Integral_Top / 1024; // dämfen
683 killagreg 170
                adc_channel = 3; // set next channel to ADC3 = air pressure
1 ingob 171
            break;
401 hbuss 172
        case 10:
701 killagreg 173
            tmpAirPressure += ADC; // sum vadc values
703 killagreg 174
            if(++average_pressure >= 5) // if 5 values are summerized for averaging
750 killagreg 175
            {
701 killagreg 176
                ReadingAirPressure = ADC; // update measured air pressure
761 killagreg 177
                                HeightD = (int16_t)(StartAirPressure - tmpAirPressure - ReadingHeight);  // D-Anteil = neuerWert - AlterWert
701 killagreg 178
                AirPressure = (tmpAirPressure + 3 * AirPressure) / 4; // averaging using history
761 killagreg 179
                ReadingHeight = StartAirPressure - AirPressure;
750 killagreg 180
                average_pressure = 0; // reset air pressure measurement counter
701 killagreg 181
                tmpAirPressure = 0;
750 killagreg 182
            }
683 killagreg 183
            adc_channel = 0; // set next channel to ADC0 = GIER GYRO
750 killagreg 184
            state = 0; // reset state machine
1 ingob 185
            break;
683 killagreg 186
        default:
187
            adc_channel = 0;
1 ingob 188
            state = 0;
189
            break;
683 killagreg 190
        }
191
    // set adc muxer to next adc_channel
192
    ADMUX = (ADMUX & 0xE0) | adc_channel;
750 killagreg 193
    // after full cycle stop further interrupts
683 killagreg 194
    if(state != 0) ADC_Enable();
1 ingob 195
}