Subversion Repositories FlightCtrl

Rev

Rev 1695 | Rev 1702 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1338 ingob 2
// + Copyright (c) Holger Buss, Ingo Busker
1 ingob 3
// + only for non-profit use
4
// + www.MikroKopter.com
1356 hbuss 5
// + porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
1 ingob 6
// + see the File "License.txt" for further Informations
7
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8
 
9
#include "main.h"
1622 killagreg 10
#include "eeprom.h"
1 ingob 11
volatile int  Aktuell_Nick,Aktuell_Roll,Aktuell_Gier,Aktuell_ax, Aktuell_ay,Aktuell_az, UBat = 100;
1166 hbuss 12
volatile int  AdWertNickFilter = 0, AdWertRollFilter = 0, AdWertGierFilter = 0;
13
volatile int  HiResNick = 2500, HiResRoll = 2500;
380 hbuss 14
volatile int  AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0;
15
volatile int  AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0;
1166 hbuss 16
volatile char messanzahl_AccHoch = 0;
1 ingob 17
volatile long Luftdruck = 32000;
1322 hbuss 18
volatile long SummenHoehe = 0;
1 ingob 19
volatile int  StartLuftdruck;
20
volatile unsigned int  MessLuftdruck = 1023;
21
unsigned char DruckOffsetSetting;
1036 hbuss 22
signed char ExpandBaro = 0;
1253 killagreg 23
volatile int VarioMeter = 0;
1 ingob 24
volatile unsigned int ZaehlMessungen = 0;
918 hbuss 25
unsigned char AnalogOffsetNick = 115,AnalogOffsetRoll = 115,AnalogOffsetGier = 115;
26
unsigned char GyroDefektN = 0,GyroDefektR = 0,GyroDefektG = 0;
1168 hbuss 27
volatile unsigned char AdReady = 1;
1643 holgerb 28
float NeutralAccZ_float;
1 ingob 29
//#######################################################################################
30
//
31
void ADC_Init(void)
32
//#######################################################################################
1246 killagreg 33
{
1 ingob 34
    ADMUX = 0;//Referenz ist extern
1155 hbuss 35
    ANALOG_ON;
1 ingob 36
}
37
 
1352 hbuss 38
#define DESIRED_H_ADC 800
1322 hbuss 39
 
1 ingob 40
void SucheLuftruckOffset(void)
41
{
42
 unsigned int off;
1622 killagreg 43
 off = GetParamByte(PID_PRESSURE_OFFSET);
173 holgerb 44
 if(off > 20) off -= 10;
45
 OCR0A = off;
1036 hbuss 46
 ExpandBaro = 0;
380 hbuss 47
 Delay_ms_Mess(100);
1322 hbuss 48
 if(MessLuftdruck < DESIRED_H_ADC) off = 0;
173 holgerb 49
 for(; off < 250;off++)
1 ingob 50
  {
51
  OCR0A = off;
380 hbuss 52
  Delay_ms_Mess(50);
1246 killagreg 53
  printf(".");
1322 hbuss 54
  if(MessLuftdruck < DESIRED_H_ADC) break;
1 ingob 55
  }
1622 killagreg 56
 SetParamByte(PID_PRESSURE_OFFSET, off);
173 holgerb 57
 DruckOffsetSetting = off;
1638 holgerb 58
 OCR0A = off;
380 hbuss 59
 Delay_ms_Mess(300);
1638 holgerb 60
 
1 ingob 61
}
62
 
918 hbuss 63
void SucheGyroOffset(void)
64
{
65
 unsigned char i, ready = 0;
1219 hbuss 66
 int timeout;
918 hbuss 67
 GyroDefektN = 0; GyroDefektR = 0; GyroDefektG = 0;
1219 hbuss 68
 timeout = SetDelay(2000);
918 hbuss 69
 for(i=140; i != 0; i--)
70
  {
921 hbuss 71
   if(ready == 3 && i > 10) i = 9;
918 hbuss 72
   ready = 0;
73
   if(AdWertNick < 1020) AnalogOffsetNick--; else if(AdWertNick > 1030) AnalogOffsetNick++; else ready++;
74
   if(AdWertRoll < 1020) AnalogOffsetRoll--; else if(AdWertRoll > 1030) AnalogOffsetRoll++; else ready++;
75
   if(AdWertGier < 1020) AnalogOffsetGier--; else if(AdWertGier > 1030) AnalogOffsetGier++; else ready++;
1662 killagreg 76
   I2C_Start(TWI_STATE_GYRO_OFFSET_TX);
1246 killagreg 77
   if(AnalogOffsetNick < 10)  { GyroDefektN = 1; AnalogOffsetNick = 10;}; if(AnalogOffsetNick > 245) { GyroDefektN = 1; AnalogOffsetNick = 245;};
78
   if(AnalogOffsetRoll < 10)  { GyroDefektR = 1; AnalogOffsetRoll = 10;}; if(AnalogOffsetRoll > 245) { GyroDefektR = 1; AnalogOffsetRoll = 245;};
79
   if(AnalogOffsetGier < 10)  { GyroDefektG = 1; AnalogOffsetGier = 10;}; if(AnalogOffsetGier > 245) { GyroDefektG = 1; AnalogOffsetGier = 245;};
1219 hbuss 80
   while(twi_state) if(CheckDelay(timeout)) {printf("\n\r DAC or I2C ERROR! Check I2C, 3Vref, DAC and BL-Ctrl"); break;}
1253 killagreg 81
   AdReady = 0;
918 hbuss 82
   ANALOG_ON;
1253 killagreg 83
   while(!AdReady);
1246 killagreg 84
   if(i<10) Delay_ms_Mess(10);
918 hbuss 85
  }
1246 killagreg 86
   Delay_ms_Mess(70);
87
}
1 ingob 88
 
1171 hbuss 89
/*
90
 
91
1  r
1246 killagreg 92
2     g
1171 hbuss 93
3     y
94
4     x
95
5  n
96
6  r
97
7     u
98
8     z
99
9     L
1246 killagreg 100
10 n
1171 hbuss 101
11 r
102
12    g
103
13    y
104
14    x
105
15 n
106
16 r
107
17    L
108
*/
109
 
1 ingob 110
//#######################################################################################
111
//
1561 killagreg 112
ISR(ADC_vect)
1 ingob 113
//#######################################################################################
114
{
115
    static unsigned char kanal=0,state = 0;
1687 holgerb 116
        static signed char subcount = 0;
1246 killagreg 117
    static signed int gier1, roll1, nick1, nick_filter, roll_filter;
1687 holgerb 118
        static signed int accy, accx;
1253 killagreg 119
        static long tmpLuftdruck = 0;
120
        static char messanzahl_Druck = 0;
1171 hbuss 121
    switch(state++)
122
        {
123
        case 0:
124
            nick1 = ADC;
125
            kanal = AD_ROLL;
126
            break;
127
        case 1:
128
            roll1 = ADC;
129
                    kanal = AD_GIER;
130
            break;
131
        case 2:
132
            gier1 = ADC;
133
            kanal = AD_ACC_Y;
134
            break;
135
        case 3:
136
            Aktuell_ay = NeutralAccY - ADC;
137
            accy = Aktuell_ay;
138
                    kanal = AD_ACC_X;
139
            break;
140
        case 4:
141
            Aktuell_ax = ADC - NeutralAccX;
142
            accx =  Aktuell_ax;
143
            kanal = AD_NICK;
144
            break;
145
        case 5:
146
            nick1 += ADC;
147
            kanal = AD_ROLL;
148
            break;
149
        case 6:
150
            roll1 += ADC;
151
            kanal = AD_UBAT;
152
            break;
153
        case 7:
154
            UBat = (3 * UBat + ADC / 3) / 4;
155
                    kanal = AD_ACC_Z;
156
            break;
157
       case 8:
158
            AdWertAccHoch =  (signed int) ADC - NeutralAccZ;
1246 killagreg 159
            if(AdWertAccHoch > 1)
1171 hbuss 160
             {
1246 killagreg 161
              if(NeutralAccZ < 750)
1171 hbuss 162
               {
1695 holgerb 163
                subcount += 5;
1701 holgerb 164
                if(modell_fliegt < 500) subcount += 10;
1639 holgerb 165
               }
1687 holgerb 166
              if(subcount > 100) { NeutralAccZ++; subcount -= 100;}
1639 holgerb 167
             }
168
             else if(AdWertAccHoch < -1)
169
             {
170
              if(NeutralAccZ > 550)
171
                {
1695 holgerb 172
                 subcount -= 5;
1701 holgerb 173
                 if(modell_fliegt < 500) subcount -= 10;
1687 holgerb 174
                 if(subcount < -100) { NeutralAccZ--; subcount += 100;}
1639 holgerb 175
                }
176
             }
1171 hbuss 177
            messanzahl_AccHoch = 1;
178
            Aktuell_az = ADC;
179
            Mess_Integral_Hoch += AdWertAccHoch;      // Integrieren
180
            Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen
181
                kanal = AD_DRUCK;
182
            break;
1701 holgerb 183
   // "case 9:" fehlt hier absichtlich
1171 hbuss 184
        case 10:
185
            nick1 += ADC;
186
            kanal = AD_ROLL;
187
            break;
188
        case 11:
189
            roll1 += ADC;
190
                    kanal = AD_GIER;
191
            break;
192
        case 12:
193
            if(PlatinenVersion == 10)  AdWertGier = (ADC + gier1 + 1) / 2;
1246 killagreg 194
            else
1660 holgerb 195
            if(PlatinenVersion >= 20)  AdWertGier = 2047 - (ADC + gier1);
1171 hbuss 196
                        else                                       AdWertGier = (ADC + gier1);
197
            kanal = AD_ACC_Y;
198
            break;
199
        case 13:
200
            Aktuell_ay = NeutralAccY - ADC;
201
            AdWertAccRoll = (Aktuell_ay + accy);
202
            kanal = AD_ACC_X;
203
            break;
204
        case 14:
205
            Aktuell_ax = ADC - NeutralAccX;
206
            AdWertAccNick =  (Aktuell_ax + accx);
207
            kanal = AD_NICK;
208
            break;
209
        case 15:
210
            nick1 += ADC;
1173 hbuss 211
            if(PlatinenVersion == 10) nick1 *= 2; else nick1 *= 4;
212
            AdWertNick = nick1 / 8;
213
            nick_filter = (nick_filter + nick1) / 2;
214
            HiResNick = nick_filter - AdNeutralNick;
1171 hbuss 215
            AdWertNickFilter = (AdWertNickFilter + HiResNick) / 2;
216
            kanal = AD_ROLL;
217
            break;
218
        case 16:
219
            roll1 += ADC;
1173 hbuss 220
            if(PlatinenVersion == 10) roll1 *= 2; else roll1 *= 4;
221
            AdWertRoll = roll1 / 8;
222
            roll_filter = (roll_filter + roll1) / 2;
223
            HiResRoll = roll_filter - AdNeutralRoll;
1171 hbuss 224
            AdWertRollFilter = (AdWertRollFilter + HiResRoll) / 2;
225
                kanal = AD_DRUCK;
226
            break;
227
        case 17:
228
            state = 0;
229
                        AdReady = 1;
230
            ZaehlMessungen++;
231
            // "break" fehlt hier absichtlich
232
        case 9:
1253 killagreg 233
                MessLuftdruck = ADC;
234
            tmpLuftdruck += MessLuftdruck;
235
            if(++messanzahl_Druck >= 18)
236
            {
1266 killagreg 237
                                Luftdruck = (7 * Luftdruck + tmpLuftdruck - (18 * 523) * (long)ExpandBaro + 4) / 8;  // -523.19 counts per 10 counts offset step
1253 killagreg 238
                                HoehenWert = StartLuftdruck - Luftdruck;
1272 hbuss 239
                                SummenHoehe -= SummenHoehe/SM_FILTER;
1253 killagreg 240
                                SummenHoehe += HoehenWert;
1695 holgerb 241
                                VarioMeter = (31 * VarioMeter + 8 * (int)(HoehenWert - SummenHoehe/SM_FILTER))/32;
1272 hbuss 242
                tmpLuftdruck /= 2;
243
                messanzahl_Druck = 18/2;
1253 killagreg 244
            }
1171 hbuss 245
            kanal = AD_NICK;
246
            break;
1246 killagreg 247
        default:
1171 hbuss 248
            kanal = 0; state = 0; kanal = AD_NICK;
249
            break;
1246 killagreg 250
        }
1171 hbuss 251
    ADMUX = kanal;
252
    if(state != 0) ANALOG_ON;
253
}
254