Subversion Repositories FlightCtrl

Rev

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
 
701 killagreg 18
volatile int16_t Current_Pitch = 0, Current_Roll = 0, Current_Yaw = 0;
683 killagreg 19
volatile int16_t Current_AccX = 0, Current_AccY = 0, Current_AccZ = 0;
20
volatile int16_t UBat = 100;
701 killagreg 21
volatile int16_t AdValueGyrPitch = 0, AdValueGyrRoll = 0, AdValueGyrYaw = 0;
22
volatile int16_t AdValueAccRoll = 0, AdValueAccPitch = 0, AdValueAccTop = 0;
23
volatile int32_t AirPressure = 32000;
24
volatile int16_t StartAirPressure;
25
volatile uint16_t ReadingAirPressure = 1023;
711 killagreg 26
uint8_t PressureSensorOffset;
703 killagreg 27
volatile int16_t HightD = 0;
701 killagreg 28
volatile int16_t tmpAirPressure;
711 killagreg 29
volatile uint16_t MeasurementCounter = 0;
1 ingob 30
 
683 killagreg 31
/*****************************************************/
32
/*     Initialize Analog Digital Converter           */
33
/*****************************************************/
1 ingob 34
void ADC_Init(void)
683 killagreg 35
{
36
        uint8_t sreg = SREG;
37
        // disable all interrupts before reconfiguration
38
        cli();
39
        //ADC0 ... ADC7 is connected to PortA pin 0 ... 7
40
        DDRA = 0x00;
41
        PORTA = 0x00;
42
        // Digital Input Disable Register 0
43
        // Disable digital input buffer for analog adc_channel pins
44
        DIDR0 = 0xFF;
45
        // external reference, adjust data to the right
46
    ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR));
47
    // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
48
    ADMUX = (ADMUX & 0xE0) | 0x00;
49
    //Set ADC Control and Status Register A
50
    //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz
51
    ADCSRA = (1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
52
        //Set ADC Control and Status Register B
53
        //Trigger Source to Free Running Mode
54
        ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0));
55
        // Enable AD conversion
56
        ADC_Enable();
57
    // restore global interrupt flags
58
    SREG = sreg;
1 ingob 59
}
60
 
701 killagreg 61
void SearchAirPressureOffset(void)
1 ingob 62
{
687 killagreg 63
 uint8_t off;
64
 off = GetParamByte(PID_LAST_OFFSET);
173 holgerb 65
 if(off > 20) off -= 10;
66
 OCR0A = off;
380 hbuss 67
 Delay_ms_Mess(100);
701 killagreg 68
 if(ReadingAirPressure < 850) off = 0;
173 holgerb 69
 for(; off < 250;off++)
1 ingob 70
  {
71
  OCR0A = off;
380 hbuss 72
  Delay_ms_Mess(50);
683 killagreg 73
  printf(".");
701 killagreg 74
  if(ReadingAirPressure < 900) break;
1 ingob 75
  }
687 killagreg 76
 SetParamByte(PID_LAST_OFFSET, off);
711 killagreg 77
 PressureSensorOffset = off;
380 hbuss 78
 Delay_ms_Mess(300);
1 ingob 79
}
80
 
81
 
683 killagreg 82
/*****************************************************/
83
/*     Interrupt Service Routine for ADC             */
84
/*****************************************************/
85
ISR(ADC_vect)
1 ingob 86
{
683 killagreg 87
    static uint8_t adc_channel = 0, state = 0;
701 killagreg 88
    static uint16_t yaw1, roll1, pitch1;
703 killagreg 89
    static uint8_t average_pressure = 0;
683 killagreg 90
    // disable further AD conversion
91
    ADC_Disable();
92
    // state machine
1 ingob 93
    switch(state++)
94
        {
95
        case 0:
701 killagreg 96
            yaw1 = ADC; // get Gyro Yaw Voltage 1st sample
683 killagreg 97
            adc_channel = 1; // set next channel to ADC1 = ROLL GYRO
711 killagreg 98
            MeasurementCounter++; // increment total measurement counter
1 ingob 99
            break;
100
        case 1:
683 killagreg 101
            roll1 = ADC; // get Gyro Roll Voltage 1st sample
701 killagreg 102
            adc_channel = 2; // set next channel to ADC2 = PITCH GYRO
1 ingob 103
            break;
104
        case 2:
701 killagreg 105
            pitch1 = ADC; // get Gyro Pitch Voltage 1st sample
683 killagreg 106
            adc_channel = 4; // set next channel to ADC4 = UBAT
1 ingob 107
            break;
108
        case 3:
683 killagreg 109
                // get actual UBat (Volts*10) is ADC*30V/1024*10 = ADC/3
110
            UBat = (3 * UBat + ADC / 3) / 4; // low pass filter updates UBat only to 1 quater with actual ADC value
111
            adc_channel = 6; // set next channel to ADC6 = ACC_Y
1 ingob 112
            break;
113
        case 4:
683 killagreg 114
            Current_AccY = NeutralAccY - ADC; // get acceleration in Y direction
115
            AdValueAccRoll = Current_AccY;
116
            adc_channel = 7; // set next channel to ADC7 = ACC_X
1 ingob 117
            break;
118
        case 5:
683 killagreg 119
            Current_AccX = ADC - NeutralAccX; // get acceleration in X direction
701 killagreg 120
            AdValueAccPitch =  Current_AccX;
121
                    adc_channel = 0; // set next channel to ADC7 = YAW GYRO
1 ingob 122
            break;
123
        case 6:
701 killagreg 124
                // average over two samples to create current AdValueGyrYaw
125
            if(BoardRelease == 10)  AdValueGyrYaw = (ADC + yaw1) / 2;
126
                        else                                       AdValueGyrYaw = ADC + yaw1; // gain is 2 times lower on FC 1.1
683 killagreg 127
            adc_channel = 1; // set next channel to ADC7 = ROLL GYRO
401 hbuss 128
            break;
129
        case 7:
701 killagreg 130
                // average over two samples to create current ADValueGyrRoll
685 killagreg 131
            if(BoardRelease == 10)  AdValueGyrRoll = (ADC + roll1) / 2;
683 killagreg 132
                        else                                       AdValueGyrRoll = ADC + roll1; // gain is 2 times lower on FC 1.1
701 killagreg 133
            adc_channel = 2; // set next channel to ADC2 = PITCH GYRO
401 hbuss 134
            break;
135
        case 8:
701 killagreg 136
                // average over two samples to create current ADValuePitch
137
            if(BoardRelease == 10)  AdValueGyrPitch = (ADC + pitch1) / 2;
138
                        else                                       AdValueGyrPitch = ADC + pitch1; // gain is 2 times lower on FC 1.1
683 killagreg 139
            adc_channel = 5; // set next channel to ADC5 = ACC_Z
401 hbuss 140
            break;
141
       case 9:
683 killagreg 142
                // get z acceleration
143
            AdValueAccTop =  (int16_t) ADC - NeutralAccZ; // get plain acceleration in Z direction
144
            AdValueAccTop += abs(Current_AccY) / 4 + abs(Current_AccX) / 4;
145
            if(AdValueAccTop > 1)
1 ingob 146
             {
683 killagreg 147
              if(NeutralAccZ < 800) NeutralAccZ+= 0.02;
148
             }
149
             else if(AdValueAccTop < -1)
1 ingob 150
             {
151
              if(NeutralAccZ > 600) NeutralAccZ-= 0.02;
683 killagreg 152
             }
153
            Current_AccZ = ADC;
701 killagreg 154
            Reading_Integral_Top += AdValueAccTop;      // Integrieren
155
            Reading_Integral_Top -= Reading_Integral_Top / 1024; // dämfen
683 killagreg 156
                adc_channel = 3; // set next channel to ADC3 = air pressure
1 ingob 157
            break;
401 hbuss 158
        case 10:
701 killagreg 159
            tmpAirPressure += ADC; // sum vadc values
703 killagreg 160
            if(++average_pressure >= 5) // if 5 values are summerized for averaging
1 ingob 161
                {
701 killagreg 162
                ReadingAirPressure = ADC; // update measured air pressure
703 killagreg 163
                average_pressure = 0; // reset air pressure measurement counter
164
                                HightD = (int16_t)(StartAirPressure - tmpAirPressure - ReadingHight);  // D-Anteil = neuerWert - AlterWert
701 killagreg 165
                AirPressure = (tmpAirPressure + 3 * AirPressure) / 4; // averaging using history
166
                ReadingHight = StartAirPressure - AirPressure;
167
                tmpAirPressure = 0;
683 killagreg 168
                }
169
            adc_channel = 0; // set next channel to ADC0 = GIER GYRO
170
            state = 0; // reset state
1 ingob 171
            break;
683 killagreg 172
        default:
173
            adc_channel = 0;
1 ingob 174
            state = 0;
175
            break;
683 killagreg 176
        }
177
    // set adc muxer to next adc_channel
178
    ADMUX = (ADMUX & 0xE0) | adc_channel;
179
    // ??
180
    if(state != 0) ADC_Enable();
1 ingob 181
}