Subversion Repositories FlightCtrl

Rev

Rev 624 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 624 Rev 683
Line 5... Line 5...
5
// + see the File "License.txt" for further Informations
5
// + see the File "License.txt" for further Informations
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 7... Line 7...
7
 
7
 
Line 8... Line 8...
8
#include "main.h"
8
#include "main.h"
9
 
9
 
-
 
10
volatile int16_t Current_Nick = 0, Current_Roll = 0, Current_Gier = 0;
10
volatile int  Aktuell_Nick,Aktuell_Roll,Aktuell_Gier,Aktuell_ax, Aktuell_ay,Aktuell_az, UBat = 100;
11
volatile int16_t Current_AccX = 0, Current_AccY = 0, Current_AccZ = 0;
11
volatile int  AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0;
12
volatile int16_t UBat = 100;
12
volatile int  AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0;
13
volatile int16_t AdValueGyrNick = 0, AdValueGyrRoll = 0, AdValueGyrGier = 0;
13
volatile char MessanzahlNick = 0, MessanzahlRoll = 0, MessanzahlGier = 0;
14
volatile int16_t AdValueAccRoll = 0, AdValueAccNick = 0, AdValueAccTop = 0;
14
volatile char messanzahl_AccNick = 0, messanzahl_AccRoll = 0, messanzahl_AccHoch = 0;
15
volatile uint8_t messanzahl_AccHoch = 0;
15
volatile long Luftdruck = 32000;
16
volatile int32_t Luftdruck = 32000;
16
volatile int  StartLuftdruck;
17
volatile int16_t StartLuftdruck;
17
volatile unsigned int  MessLuftdruck = 1023;
18
volatile uint16_t MessLuftdruck = 1023;
18
unsigned char DruckOffsetSetting;
-
 
19
volatile int HoeheD = 0;
19
uint8_t DruckOffsetSetting;
20
volatile char messanzahl_Druck;
20
volatile int16_t HoeheD = 0;
21
volatile int  tmpLuftdruck;
21
volatile int16_t  tmpLuftdruck;
22
volatile unsigned int ZaehlMessungen = 0;
22
volatile uint16_t ZaehlMessungen = 0;
23
 
-
 
-
 
23
 
-
 
24
/*****************************************************/
24
//#######################################################################################
25
/*     Initialize Analog Digital Converter           */
25
//
-
 
26
void ADC_Init(void)
26
/*****************************************************/
-
 
27
void ADC_Init(void)
-
 
28
{
-
 
29
        uint8_t sreg = SREG;
-
 
30
        // disable all interrupts before reconfiguration
-
 
31
        cli();
-
 
32
        //ADC0 ... ADC7 is connected to PortA pin 0 ... 7
-
 
33
        DDRA = 0x00;
-
 
34
        PORTA = 0x00;
-
 
35
        // Digital Input Disable Register 0
-
 
36
        // Disable digital input buffer for analog adc_channel pins
-
 
37
        DIDR0 = 0xFF;
-
 
38
        // external reference, adjust data to the right
27
//#######################################################################################
39
    ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR));
-
 
40
    // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
-
 
41
    ADMUX = (ADMUX & 0xE0) | 0x00;
28
{
42
    //Set ADC Control and Status Register A
-
 
43
    //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz
-
 
44
    ADCSRA = (1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
-
 
45
        //Set ADC Control and Status Register B
-
 
46
        //Trigger Source to Free Running Mode
-
 
47
        ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0));
29
    ADMUX = 0;//Referenz ist extern
48
        // Enable AD conversion
-
 
49
        ADC_Enable();
30
    ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADIE);
50
    // restore global interrupt flags
Line 31... Line 51...
31
    //Free Running Mode, Division Factor 128, Interrupt on
51
    SREG = sreg;
32
}
52
}
33
 
53
 
Line 41... Line 61...
41
 if(MessLuftdruck < 850) off = 0;
61
 if(MessLuftdruck < 850) off = 0;
42
 for(; off < 250;off++)
62
 for(; off < 250;off++)
43
  {
63
  {
44
  OCR0A = off;
64
  OCR0A = off;
45
  Delay_ms_Mess(50);
65
  Delay_ms_Mess(50);
46
  printf(".");  
66
  printf(".");
47
  if(MessLuftdruck < 900) break;
67
  if(MessLuftdruck < 900) break;
48
  }
68
  }
49
 eeprom_write_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET], off);
69
 eeprom_write_byte(&EEPromArray[EEPROM_ADR_LAST_OFFSET], off);
50
 DruckOffsetSetting = off;
70
 DruckOffsetSetting = off;
51
 Delay_ms_Mess(300);
71
 Delay_ms_Mess(300);
52
}
72
}
Line -... Line 73...
-
 
73
 
-
 
74
 
53
 
75
/*****************************************************/
54
 
-
 
55
//#######################################################################################
76
/*     Interrupt Service Routine for ADC             */
56
//
-
 
57
SIGNAL(SIG_ADC)
77
/*****************************************************/
58
//#######################################################################################
78
ISR(ADC_vect)
59
{
79
{
-
 
80
    static uint8_t adc_channel = 0, state = 0;
-
 
81
    static uint16_t gier1, roll1, nick1;
60
    static unsigned char kanal=0,state = 0;
82
    static uint8_t messanzahl_Druck = 0;
-
 
83
    // disable further AD conversion
61
    static unsigned int gier1, roll1, nick1;
84
    ADC_Disable();
62
    ANALOG_OFF;
85
    // state machine
63
    switch(state++)
86
    switch(state++)
64
        {
87
        {
65
        case 0:
88
        case 0:
66
            gier1 = ADC;
89
            gier1 = ADC; // get Gyro Gier Voltage 1st sample
67
            kanal = 1;
90
            adc_channel = 1; // set next channel to ADC1 = ROLL GYRO
68
            ZaehlMessungen++;
91
            ZaehlMessungen++; // increment total measurement counter
69
            break;
92
            break;
70
        case 1:
93
        case 1:
71
            roll1 = ADC;
94
            roll1 = ADC; // get Gyro Roll Voltage 1st sample
72
            kanal = 2;
95
            adc_channel = 2; // set next channel to ADC2 = NICK GYRO
73
            break;
96
            break;
74
        case 2:
97
        case 2:
75
            nick1 = ADC;
98
            nick1 = ADC; // get Gyro Nick Voltage 1st sample
76
            kanal = 4;
99
            adc_channel = 4; // set next channel to ADC4 = UBAT
-
 
100
            break;
77
            break;
101
        case 3:
78
        case 3:
102
                // get actual UBat (Volts*10) is ADC*30V/1024*10 = ADC/3
79
            UBat = (3 * UBat + ADC / 3) / 4;//(UBat + ((ADC * 39) / 256) + 19)  / 2;
103
            UBat = (3 * UBat + ADC / 3) / 4; // low pass filter updates UBat only to 1 quater with actual ADC value
80
            kanal = 6;
104
            adc_channel = 6; // set next channel to ADC6 = ACC_Y
81
            break;
105
            break;
82
        case 4:
106
        case 4:
83
            Aktuell_ay = NeutralAccY - ADC;
107
            Current_AccY = NeutralAccY - ADC; // get acceleration in Y direction
84
            AdWertAccRoll = Aktuell_ay;
108
            AdValueAccRoll = Current_AccY;
85
            kanal = 7;
109
            adc_channel = 7; // set next channel to ADC7 = ACC_X
86
            break;
110
            break;
87
        case 5:
111
        case 5:
88
            Aktuell_ax = ADC - NeutralAccX;
112
            Current_AccX = ADC - NeutralAccX; // get acceleration in X direction
89
            AdWertAccNick =  Aktuell_ax;
113
            AdValueAccNick =  Current_AccX;
90
                    kanal = 0;
114
                    adc_channel = 0; // set next channel to ADC7 = GIER GYRO
-
 
115
            break;
91
            break;
116
        case 6:
92
        case 6:
117
                // average over two samples to create current ADValueGier
93
            if(PlatinenVersion == 10)  AdWertGier = (ADC + gier1) / 2;
118
            if(PlatinenVersion == 10)  AdValueGyrGier = (ADC + gier1) / 2;
94
                        else                                       AdWertGier = ADC + gier1;
119
                        else                                       AdValueGyrGier = ADC + gier1; // gain is 2 times lower on FC 1.1
95
            kanal = 1;
120
            adc_channel = 1; // set next channel to ADC7 = ROLL GYRO
-
 
121
            break;
96
            break;
122
        case 7:
97
        case 7:
123
                // average over two samples to create current ADValueRoll
98
            if(PlatinenVersion == 10)  AdWertRoll = (ADC + roll1) / 2;
124
            if(PlatinenVersion == 10)  AdValueGyrRoll = (ADC + roll1) / 2;
99
                        else                                       AdWertRoll = ADC + roll1;
125
                        else                                       AdValueGyrRoll = ADC + roll1; // gain is 2 times lower on FC 1.1
100
            kanal = 2;
126
            adc_channel = 2; // set next channel to ADC2 = NICK GYRO
-
 
127
            break;
101
            break;
128
        case 8:
102
        case 8:
129
                // average over two samples to create current ADValueNick
103
            if(PlatinenVersion == 10)  AdWertNick = (ADC + nick1) / 2;
-
 
104
                        else                                       AdWertNick = ADC + nick1;
-
 
105
//AdWertNick = 0;
130
            if(PlatinenVersion == 10)  AdValueGyrNick = (ADC + nick1) / 2;
106
//AdWertNick += Poti2;            
131
                        else                                       AdValueGyrNick = ADC + nick1; // gain is 2 times lower on FC 1.1
107
            kanal = 5;
132
            adc_channel = 5; // set next channel to ADC5 = ACC_Z
-
 
133
            break;
108
            break;
134
       case 9:
109
       case 9:
135
                // get z acceleration
110
            AdWertAccHoch =  (signed int) ADC - NeutralAccZ;
136
            AdValueAccTop =  (int16_t) ADC - NeutralAccZ; // get plain acceleration in Z direction
111
            AdWertAccHoch += abs(Aktuell_ay) / 4 + abs(Aktuell_ax) / 4;
137
            AdValueAccTop += abs(Current_AccY) / 4 + abs(Current_AccX) / 4;
112
            if(AdWertAccHoch > 1)
138
            if(AdValueAccTop > 1)
113
             {
139
             {
114
              if(NeutralAccZ < 800) NeutralAccZ+= 0.02;
140
              if(NeutralAccZ < 800) NeutralAccZ+= 0.02;
115
             }  
141
             }
116
             else if(AdWertAccHoch < -1)
142
             else if(AdValueAccTop < -1)
117
             {
143
             {
118
              if(NeutralAccZ > 600) NeutralAccZ-= 0.02;
144
              if(NeutralAccZ > 600) NeutralAccZ-= 0.02;
119
             }
145
             }
120
            messanzahl_AccHoch = 1;
146
            messanzahl_AccHoch = 1;
121
            Aktuell_az = ADC;
147
            Current_AccZ = ADC;
122
            Mess_Integral_Hoch += AdWertAccHoch;      // Integrieren
148
            Mess_Integral_Hoch += AdValueAccTop;      // Integrieren
123
            Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen
149
            Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen
124
                kanal = 3;
150
                adc_channel = 3; // set next channel to ADC3 = air pressure
125
            break;
151
            break;
126
        case 10:
152
        case 10:
127
            tmpLuftdruck += ADC;
153
            tmpLuftdruck += ADC; // sum vadc values
128
            if(++messanzahl_Druck >= 5)
154
            if(++messanzahl_Druck >= 5) // if 5 values are summerized for averaging
129
                {
155
                {
130
                MessLuftdruck = ADC;
156
                MessLuftdruck = ADC; // update measured air pressure
131
                messanzahl_Druck = 0;
157
                messanzahl_Druck = 0; // reset air pressure measurement counter
132
                                HoeheD = (int)(StartLuftdruck - tmpLuftdruck - HoehenWert);  // D-Anteil = neuerWert - AlterWert
158
                                HoeheD = (int16_t)(StartLuftdruck - tmpLuftdruck - HoehenWert);  // D-Anteil = neuerWert - AlterWert
133
                Luftdruck = (tmpLuftdruck + 3 * Luftdruck) / 4;
159
                Luftdruck = (tmpLuftdruck + 3 * Luftdruck) / 4; // averaging using history
134
                HoehenWert = StartLuftdruck - Luftdruck;
160
                HoehenWert = StartLuftdruck - Luftdruck;
135
                tmpLuftdruck = 0;
161
                tmpLuftdruck = 0;
136
                }
162
                }
137
            kanal = 0;
163
            adc_channel = 0; // set next channel to ADC0 = GIER GYRO
138
            state = 0;
164
            state = 0; // reset state
139
            break;
165
            break;
140
        default:
166
        default:
141
            kanal = 0;
167
            adc_channel = 0;
142
            state = 0;
168
            state = 0;
-
 
169
            break;
143
            break;
170
        }
-
 
171
    // set adc muxer to next adc_channel
144
        }
172
    ADMUX = (ADMUX & 0xE0) | adc_channel;
145
    ADMUX = kanal;
173
    // ??