Subversion Repositories FlightCtrl

Rev

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

Rev 687 Rev 701
Line 13... Line 13...
13
#include "timer0.h"
13
#include "timer0.h"
14
#include "fc.h"
14
#include "fc.h"
15
#include "printf_P.h"
15
#include "printf_P.h"
16
#include "eeprom.h"
16
#include "eeprom.h"
Line 17... Line 17...
17
 
17
 
18
volatile int16_t Current_Nick = 0, Current_Roll = 0, Current_Gier = 0;
18
volatile int16_t Current_Pitch = 0, Current_Roll = 0, Current_Yaw = 0;
19
volatile int16_t Current_AccX = 0, Current_AccY = 0, Current_AccZ = 0;
19
volatile int16_t Current_AccX = 0, Current_AccY = 0, Current_AccZ = 0;
20
volatile int16_t UBat = 100;
20
volatile int16_t UBat = 100;
21
volatile int16_t AdValueGyrNick = 0, AdValueGyrRoll = 0, AdValueGyrGier = 0;
21
volatile int16_t AdValueGyrPitch = 0, AdValueGyrRoll = 0, AdValueGyrYaw = 0;
22
volatile int16_t AdValueAccRoll = 0, AdValueAccNick = 0, AdValueAccTop = 0;
22
volatile int16_t AdValueAccRoll = 0, AdValueAccPitch = 0, AdValueAccTop = 0;
23
volatile uint8_t messanzahl_AccHoch = 0;
23
volatile uint8_t messanzahl_AccHoch = 0;
24
volatile int32_t Luftdruck = 32000;
24
volatile int32_t AirPressure = 32000;
25
volatile int16_t StartLuftdruck;
25
volatile int16_t StartAirPressure;
26
volatile uint16_t MessLuftdruck = 1023;
26
volatile uint16_t ReadingAirPressure = 1023;
27
uint8_t DruckOffsetSetting;
27
uint8_t DruckOffsetSetting;
28
volatile int16_t HoeheD = 0;
28
volatile int16_t HoeheD = 0;
29
volatile int16_t tmpLuftdruck;
29
volatile int16_t tmpAirPressure;
Line 30... Line 30...
30
volatile uint16_t ZaehlMessungen = 0;
30
volatile uint16_t ZaehlMessungen = 0;
31
 
31
 
32
/*****************************************************/
32
/*****************************************************/
Line 57... Line 57...
57
        ADC_Enable();
57
        ADC_Enable();
58
    // restore global interrupt flags
58
    // restore global interrupt flags
59
    SREG = sreg;
59
    SREG = sreg;
60
}
60
}
Line 61... Line 61...
61
 
61
 
62
void SucheLuftruckOffset(void)
62
void SearchAirPressureOffset(void)
63
{
63
{
64
 uint8_t off;
64
 uint8_t off;
65
 off = GetParamByte(PID_LAST_OFFSET);
65
 off = GetParamByte(PID_LAST_OFFSET);
66
 if(off > 20) off -= 10;
66
 if(off > 20) off -= 10;
67
 OCR0A = off;
67
 OCR0A = off;
68
 Delay_ms_Mess(100);
68
 Delay_ms_Mess(100);
69
 if(MessLuftdruck < 850) off = 0;
69
 if(ReadingAirPressure < 850) off = 0;
70
 for(; off < 250;off++)
70
 for(; off < 250;off++)
71
  {
71
  {
72
  OCR0A = off;
72
  OCR0A = off;
73
  Delay_ms_Mess(50);
73
  Delay_ms_Mess(50);
74
  printf(".");
74
  printf(".");
75
  if(MessLuftdruck < 900) break;
75
  if(ReadingAirPressure < 900) break;
76
  }
76
  }
77
 SetParamByte(PID_LAST_OFFSET, off);
77
 SetParamByte(PID_LAST_OFFSET, off);
78
 DruckOffsetSetting = off;
78
 DruckOffsetSetting = off;
79
 Delay_ms_Mess(300);
79
 Delay_ms_Mess(300);
Line 84... Line 84...
84
/*     Interrupt Service Routine for ADC             */
84
/*     Interrupt Service Routine for ADC             */
85
/*****************************************************/
85
/*****************************************************/
86
ISR(ADC_vect)
86
ISR(ADC_vect)
87
{
87
{
88
    static uint8_t adc_channel = 0, state = 0;
88
    static uint8_t adc_channel = 0, state = 0;
89
    static uint16_t gier1, roll1, nick1;
89
    static uint16_t yaw1, roll1, pitch1;
90
    static uint8_t messanzahl_Druck = 0;
90
    static uint8_t messanzahl_Druck = 0;
91
    // disable further AD conversion
91
    // disable further AD conversion
92
    ADC_Disable();
92
    ADC_Disable();
93
    // state machine
93
    // state machine
94
    switch(state++)
94
    switch(state++)
95
        {
95
        {
96
        case 0:
96
        case 0:
97
            gier1 = ADC; // get Gyro Gier Voltage 1st sample
97
            yaw1 = ADC; // get Gyro Yaw Voltage 1st sample
98
            adc_channel = 1; // set next channel to ADC1 = ROLL GYRO
98
            adc_channel = 1; // set next channel to ADC1 = ROLL GYRO
99
            ZaehlMessungen++; // increment total measurement counter
99
            ZaehlMessungen++; // increment total measurement counter
100
            break;
100
            break;
101
        case 1:
101
        case 1:
102
            roll1 = ADC; // get Gyro Roll Voltage 1st sample
102
            roll1 = ADC; // get Gyro Roll Voltage 1st sample
103
            adc_channel = 2; // set next channel to ADC2 = NICK GYRO
103
            adc_channel = 2; // set next channel to ADC2 = PITCH GYRO
104
            break;
104
            break;
105
        case 2:
105
        case 2:
106
            nick1 = ADC; // get Gyro Nick Voltage 1st sample
106
            pitch1 = ADC; // get Gyro Pitch Voltage 1st sample
107
            adc_channel = 4; // set next channel to ADC4 = UBAT
107
            adc_channel = 4; // set next channel to ADC4 = UBAT
108
            break;
108
            break;
109
        case 3:
109
        case 3:
110
                // get actual UBat (Volts*10) is ADC*30V/1024*10 = ADC/3
110
                // get actual UBat (Volts*10) is ADC*30V/1024*10 = ADC/3
111
            UBat = (3 * UBat + ADC / 3) / 4; // low pass filter updates UBat only to 1 quater with actual ADC value
111
            UBat = (3 * UBat + ADC / 3) / 4; // low pass filter updates UBat only to 1 quater with actual ADC value
Line 116... Line 116...
116
            AdValueAccRoll = Current_AccY;
116
            AdValueAccRoll = Current_AccY;
117
            adc_channel = 7; // set next channel to ADC7 = ACC_X
117
            adc_channel = 7; // set next channel to ADC7 = ACC_X
118
            break;
118
            break;
119
        case 5:
119
        case 5:
120
            Current_AccX = ADC - NeutralAccX; // get acceleration in X direction
120
            Current_AccX = ADC - NeutralAccX; // get acceleration in X direction
121
            AdValueAccNick =  Current_AccX;
121
            AdValueAccPitch =  Current_AccX;
122
                    adc_channel = 0; // set next channel to ADC7 = GIER GYRO
122
                    adc_channel = 0; // set next channel to ADC7 = YAW GYRO
123
            break;
123
            break;
124
        case 6:
124
        case 6:
125
                // average over two samples to create current ADValueGier
125
                // average over two samples to create current AdValueGyrYaw
126
            if(BoardRelease == 10)  AdValueGyrGier = (ADC + gier1) / 2;
126
            if(BoardRelease == 10)  AdValueGyrYaw = (ADC + yaw1) / 2;
127
                        else                                       AdValueGyrGier = ADC + gier1; // gain is 2 times lower on FC 1.1
127
                        else                                       AdValueGyrYaw = ADC + yaw1; // gain is 2 times lower on FC 1.1
128
            adc_channel = 1; // set next channel to ADC7 = ROLL GYRO
128
            adc_channel = 1; // set next channel to ADC7 = ROLL GYRO
129
            break;
129
            break;
130
        case 7:
130
        case 7:
131
                // average over two samples to create current ADValueRoll
131
                // average over two samples to create current ADValueGyrRoll
132
            if(BoardRelease == 10)  AdValueGyrRoll = (ADC + roll1) / 2;
132
            if(BoardRelease == 10)  AdValueGyrRoll = (ADC + roll1) / 2;
133
                        else                                       AdValueGyrRoll = ADC + roll1; // gain is 2 times lower on FC 1.1
133
                        else                                       AdValueGyrRoll = ADC + roll1; // gain is 2 times lower on FC 1.1
134
            adc_channel = 2; // set next channel to ADC2 = NICK GYRO
134
            adc_channel = 2; // set next channel to ADC2 = PITCH GYRO
135
            break;
135
            break;
136
        case 8:
136
        case 8:
137
                // average over two samples to create current ADValueNick
137
                // average over two samples to create current ADValuePitch
138
            if(BoardRelease == 10)  AdValueGyrNick = (ADC + nick1) / 2;
138
            if(BoardRelease == 10)  AdValueGyrPitch = (ADC + pitch1) / 2;
139
                        else                                       AdValueGyrNick = ADC + nick1; // gain is 2 times lower on FC 1.1
139
                        else                                       AdValueGyrPitch = ADC + pitch1; // gain is 2 times lower on FC 1.1
140
            adc_channel = 5; // set next channel to ADC5 = ACC_Z
140
            adc_channel = 5; // set next channel to ADC5 = ACC_Z
141
            break;
141
            break;
142
       case 9:
142
       case 9:
143
                // get z acceleration
143
                // get z acceleration
144
            AdValueAccTop =  (int16_t) ADC - NeutralAccZ; // get plain acceleration in Z direction
144
            AdValueAccTop =  (int16_t) ADC - NeutralAccZ; // get plain acceleration in Z direction
Line 151... Line 151...
151
             {
151
             {
152
              if(NeutralAccZ > 600) NeutralAccZ-= 0.02;
152
              if(NeutralAccZ > 600) NeutralAccZ-= 0.02;
153
             }
153
             }
154
            messanzahl_AccHoch = 1;
154
            messanzahl_AccHoch = 1;
155
            Current_AccZ = ADC;
155
            Current_AccZ = ADC;
156
            Mess_Integral_Hoch += AdValueAccTop;      // Integrieren
156
            Reading_Integral_Top += AdValueAccTop;      // Integrieren
157
            Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen
157
            Reading_Integral_Top -= Reading_Integral_Top / 1024; // dämfen
158
                adc_channel = 3; // set next channel to ADC3 = air pressure
158
                adc_channel = 3; // set next channel to ADC3 = air pressure
159
            break;
159
            break;
160
        case 10:
160
        case 10:
161
            tmpLuftdruck += ADC; // sum vadc values
161
            tmpAirPressure += ADC; // sum vadc values
162
            if(++messanzahl_Druck >= 5) // if 5 values are summerized for averaging
162
            if(++messanzahl_Druck >= 5) // if 5 values are summerized for averaging
163
                {
163
                {
164
                MessLuftdruck = ADC; // update measured air pressure
164
                ReadingAirPressure = ADC; // update measured air pressure
165
                messanzahl_Druck = 0; // reset air pressure measurement counter
165
                messanzahl_Druck = 0; // reset air pressure measurement counter
166
                                HoeheD = (int16_t)(StartLuftdruck - tmpLuftdruck - HoehenWert);  // D-Anteil = neuerWert - AlterWert
166
                                HoeheD = (int16_t)(StartAirPressure - tmpAirPressure - ReadingHight);  // D-Anteil = neuerWert - AlterWert
167
                Luftdruck = (tmpLuftdruck + 3 * Luftdruck) / 4; // averaging using history
167
                AirPressure = (tmpAirPressure + 3 * AirPressure) / 4; // averaging using history
168
                HoehenWert = StartLuftdruck - Luftdruck;
168
                ReadingHight = StartAirPressure - AirPressure;
169
                tmpLuftdruck = 0;
169
                tmpAirPressure = 0;
170
                }
170
                }
171
            adc_channel = 0; // set next channel to ADC0 = GIER GYRO
171
            adc_channel = 0; // set next channel to ADC0 = GIER GYRO
172
            state = 0; // reset state
172
            state = 0; // reset state
173
            break;
173
            break;
174
        default:
174
        default: