Subversion Repositories FlightCtrl

Rev

Rev 1984 | 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;
1168 hbuss 26
volatile unsigned char AdReady = 1;
2013 - 27
//float NeutralAccZ_float; // MartinR : wozu ?
1 ingob 28
//#######################################################################################
29
//
30
void ADC_Init(void)
31
//#######################################################################################
1246 killagreg 32
{
1 ingob 33
    ADMUX = 0;//Referenz ist extern
1155 hbuss 34
    ANALOG_ON;
1 ingob 35
}
36
 
1352 hbuss 37
#define DESIRED_H_ADC 800
1322 hbuss 38
 
1 ingob 39
void SucheLuftruckOffset(void)
40
{
41
 unsigned int off;
1036 hbuss 42
 ExpandBaro = 0;
1726 holgerb 43
 
44
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
45
 {
46
  unsigned char off2;
47
  OCR0A = 150;
48
  off2 = GetParamByte(PID_PRESSURE_OFFSET);
49
  if(off2 < 230) off2 += 10;
50
  OCR0B = off2;
51
  Delay_ms_Mess(100);
52
  if(MessLuftdruck > DESIRED_H_ADC) off2 = 240;
53
  for(; off2 >= 5; off2 -= 5)
54
   {
55
   OCR0B = off2;
56
   Delay_ms_Mess(50);
57
   printf("*");
58
   if(MessLuftdruck > DESIRED_H_ADC) break;
59
   }
60
   SetParamByte(PID_PRESSURE_OFFSET, off2);
61
  if(off2 >= 15) off = 140; else off = 0;
62
  for(; off < 250;off++)
63
   {
64
   OCR0A = off;
65
   Delay_ms_Mess(50);
66
   printf(".");
67
   if(MessLuftdruck < DESIRED_H_ADC) break;
68
   }
69
   DruckOffsetSetting = off;
70
 }
1765 killagreg 71
#else
1726 holgerb 72
  off = GetParamByte(PID_PRESSURE_OFFSET);
73
  if(off > 20) off -= 10;
1 ingob 74
  OCR0A = off;
1726 holgerb 75
  Delay_ms_Mess(100);
76
  if(MessLuftdruck < DESIRED_H_ADC) off = 0;
77
  for(; off < 250;off++)
78
   {
79
   OCR0A = off;
80
   Delay_ms_Mess(50);
81
   printf(".");
82
   if(MessLuftdruck < DESIRED_H_ADC) break;
83
   }
84
   DruckOffsetSetting = off;
85
   SetParamByte(PID_PRESSURE_OFFSET, off);
86
#endif
1765 killagreg 87
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG) && (DruckOffsetSetting < 10 || DruckOffsetSetting >= 245)) VersionInfo.HardwareError[0] |= FC_ERROR0_PRESSURE;
1638 holgerb 88
 OCR0A = off;
380 hbuss 89
 Delay_ms_Mess(300);
1 ingob 90
}
91
 
1726 holgerb 92
 
918 hbuss 93
void SucheGyroOffset(void)
94
{
95
 unsigned char i, ready = 0;
1219 hbuss 96
 int timeout;
97
 timeout = SetDelay(2000);
918 hbuss 98
 for(i=140; i != 0; i--)
99
  {
921 hbuss 100
   if(ready == 3 && i > 10) i = 9;
918 hbuss 101
   ready = 0;
102
   if(AdWertNick < 1020) AnalogOffsetNick--; else if(AdWertNick > 1030) AnalogOffsetNick++; else ready++;
103
   if(AdWertRoll < 1020) AnalogOffsetRoll--; else if(AdWertRoll > 1030) AnalogOffsetRoll++; else ready++;
104
   if(AdWertGier < 1020) AnalogOffsetGier--; else if(AdWertGier > 1030) AnalogOffsetGier++; else ready++;
1662 killagreg 105
   I2C_Start(TWI_STATE_GYRO_OFFSET_TX);
1765 killagreg 106
   if(AnalogOffsetNick < 10)  { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; AnalogOffsetNick = 10;}; if(AnalogOffsetNick > 245) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; AnalogOffsetNick = 245;};
107
   if(AnalogOffsetRoll < 10)  { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; AnalogOffsetRoll = 10;}; if(AnalogOffsetRoll > 245) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; AnalogOffsetRoll = 245;};
108
   if(AnalogOffsetGier < 10)  { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW;  AnalogOffsetGier = 10;}; if(AnalogOffsetGier > 245) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW;  AnalogOffsetGier = 245;};
1219 hbuss 109
   while(twi_state) if(CheckDelay(timeout)) {printf("\n\r DAC or I2C ERROR! Check I2C, 3Vref, DAC and BL-Ctrl"); break;}
1253 killagreg 110
   AdReady = 0;
918 hbuss 111
   ANALOG_ON;
1253 killagreg 112
   while(!AdReady);
1246 killagreg 113
   if(i<10) Delay_ms_Mess(10);
918 hbuss 114
  }
1246 killagreg 115
   Delay_ms_Mess(70);
116
}
1 ingob 117
 
1171 hbuss 118
/*
119
 
120
1  r
1246 killagreg 121
2     g
1171 hbuss 122
3     y
123
4     x
124
5  n
125
6  r
126
7     u
127
8     z
128
9     L
1246 killagreg 129
10 n
1171 hbuss 130
11 r
131
12    g
132
13    y
133
14    x
134
15 n
135
16 r
136
17    L
137
*/
138
 
1 ingob 139
//#######################################################################################
140
//
1561 killagreg 141
ISR(ADC_vect)
1 ingob 142
//#######################################################################################
143
{
144
    static unsigned char kanal=0,state = 0;
2013 - 145
        //static signed char subcount = 0; // MartinR: wird nicht mehr benötigt, da deaktiviert
1246 killagreg 146
    static signed int gier1, roll1, nick1, nick_filter, roll_filter;
1687 holgerb 147
        static signed int accy, accx;
1253 killagreg 148
        static long tmpLuftdruck = 0;
149
        static char messanzahl_Druck = 0;
1171 hbuss 150
    switch(state++)
151
        {
152
        case 0:
153
            nick1 = ADC;
154
            kanal = AD_ROLL;
155
            break;
156
        case 1:
157
            roll1 = ADC;
158
                    kanal = AD_GIER;
159
            break;
160
        case 2:
161
            gier1 = ADC;
162
            kanal = AD_ACC_Y;
163
            break;
164
        case 3:
165
            Aktuell_ay = NeutralAccY - ADC;
166
            accy = Aktuell_ay;
167
                    kanal = AD_ACC_X;
168
            break;
169
        case 4:
170
            Aktuell_ax = ADC - NeutralAccX;
171
            accx =  Aktuell_ax;
172
            kanal = AD_NICK;
173
            break;
174
        case 5:
175
            nick1 += ADC;
176
            kanal = AD_ROLL;
177
            break;
178
        case 6:
179
            roll1 += ADC;
180
            kanal = AD_UBAT;
181
            break;
182
        case 7:
1806 holgerb 183
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
184
            if(EE_Parameter.ExtraConfig & CFG_3_3V_REFERENCE) UBat = (3 * UBat + (11 * ADC) / 30) / 4; // there were some single FC2.1 with 3.3V reference
185
                        else  
186
#endif
187
                        UBat = (3 * UBat + ADC / 3) / 4;
1171 hbuss 188
                    kanal = AD_ACC_Z;
189
            break;
190
       case 8:
191
            AdWertAccHoch =  (signed int) ADC - NeutralAccZ;
2013 - 192
 
193
                        /*  // MartinR: deaktivieren Anfang
194
 
1246 killagreg 195
            if(AdWertAccHoch > 1)
1171 hbuss 196
             {
1246 killagreg 197
              if(NeutralAccZ < 750)
1171 hbuss 198
               {
1695 holgerb 199
                subcount += 5;
1701 holgerb 200
                if(modell_fliegt < 500) subcount += 10;
1639 holgerb 201
               }
1687 holgerb 202
              if(subcount > 100) { NeutralAccZ++; subcount -= 100;}
1639 holgerb 203
             }
204
             else if(AdWertAccHoch < -1)
205
             {
206
              if(NeutralAccZ > 550)
207
                {
1695 holgerb 208
                 subcount -= 5;
1701 holgerb 209
                 if(modell_fliegt < 500) subcount -= 10;
1687 holgerb 210
                 if(subcount < -100) { NeutralAccZ--; subcount += 100;}
1639 holgerb 211
                }
212
             }
2013 - 213
 
214
                         */ // MartinR: deaktivieren Ende
215
 
1171 hbuss 216
            messanzahl_AccHoch = 1;
217
            Aktuell_az = ADC;
218
            Mess_Integral_Hoch += AdWertAccHoch;      // Integrieren
219
            Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen
220
                kanal = AD_DRUCK;
221
            break;
1701 holgerb 222
   // "case 9:" fehlt hier absichtlich
1171 hbuss 223
        case 10:
224
            nick1 += ADC;
225
            kanal = AD_ROLL;
226
            break;
227
        case 11:
228
            roll1 += ADC;
229
                    kanal = AD_GIER;
230
            break;
231
        case 12:
232
            if(PlatinenVersion == 10)  AdWertGier = (ADC + gier1 + 1) / 2;
1246 killagreg 233
            else
1660 holgerb 234
            if(PlatinenVersion >= 20)  AdWertGier = 2047 - (ADC + gier1);
1171 hbuss 235
                        else                                       AdWertGier = (ADC + gier1);
236
            kanal = AD_ACC_Y;
237
            break;
238
        case 13:
239
            Aktuell_ay = NeutralAccY - ADC;
240
            AdWertAccRoll = (Aktuell_ay + accy);
241
            kanal = AD_ACC_X;
242
            break;
243
        case 14:
244
            Aktuell_ax = ADC - NeutralAccX;
245
            AdWertAccNick =  (Aktuell_ax + accx);
246
            kanal = AD_NICK;
247
            break;
248
        case 15:
249
            nick1 += ADC;
1173 hbuss 250
            if(PlatinenVersion == 10) nick1 *= 2; else nick1 *= 4;
251
            AdWertNick = nick1 / 8;
252
            nick_filter = (nick_filter + nick1) / 2;
2013 - 253
                        //HiResNick = nick_filter - AdNeutralNick; // MartinR: so war es
254
                        HiResNick = (nick_filter - AdNeutralNick + 4 ) / 8 ; // MartinR: + 4um Rundungsfehler zu verbessern
255
                        // MartinR: /8 von fc.c nach hier verschoben um Code zu sparen
1171 hbuss 256
            AdWertNickFilter = (AdWertNickFilter + HiResNick) / 2;
257
            kanal = AD_ROLL;
258
            break;
259
        case 16:
260
            roll1 += ADC;
1173 hbuss 261
            if(PlatinenVersion == 10) roll1 *= 2; else roll1 *= 4;
262
            AdWertRoll = roll1 / 8;
263
            roll_filter = (roll_filter + roll1) / 2;
2013 - 264
            //HiResRoll = roll_filter - AdNeutralRoll; // MartinR: so war es
265
                        HiResRoll = (roll_filter - AdNeutralRoll + 4 ) / 8; // MartinR: + 4um Rundungsfehler zu verbessern
266
                        // MartinR: /8 von fc.c nach hier verschoben um Code zu sparen
1171 hbuss 267
            AdWertRollFilter = (AdWertRollFilter + HiResRoll) / 2;
268
                kanal = AD_DRUCK;
269
            break;
270
        case 17:
271
            state = 0;
272
                        AdReady = 1;
273
            ZaehlMessungen++;
274
            // "break" fehlt hier absichtlich
275
        case 9:
2013 - 276
                //MessLuftdruck = ADC; // MartinR: so war es
277
                        MessLuftdruck = ADC + ((Parameter_UserParam4 * AdWertAccHoch) / 256) ; // MartinR: 
278
                        // mit Korrektur der Beschleunigungsempfindlichkeit des Luftdrucksensors
1253 killagreg 279
            tmpLuftdruck += MessLuftdruck;
280
            if(++messanzahl_Druck >= 18)
281
            {
1944 holgerb 282
                            signed int tmp;
1266 killagreg 283
                                Luftdruck = (7 * Luftdruck + tmpLuftdruck - (18 * 523) * (long)ExpandBaro + 4) / 8;  // -523.19 counts per 10 counts offset step
1253 killagreg 284
                                HoehenWert = StartLuftdruck - Luftdruck;
1272 hbuss 285
                                SummenHoehe -= SummenHoehe/SM_FILTER;
1253 killagreg 286
                                SummenHoehe += HoehenWert;
1944 holgerb 287
                                tmp = (HoehenWert - SummenHoehe/SM_FILTER);
1982 holgerb 288
                                if(tmp > 1024) tmp = 1024;      else if(tmp < -1024) tmp = -1024;
289
                if(abs(VarioMeter) > 700) VarioMeter = (15 * VarioMeter + 8 * tmp)/16;
1944 holgerb 290
                                else VarioMeter = (31 * VarioMeter + 8 * tmp)/32;
1272 hbuss 291
                tmpLuftdruck /= 2;
292
                messanzahl_Druck = 18/2;
1253 killagreg 293
            }
1171 hbuss 294
            kanal = AD_NICK;
295
            break;
1246 killagreg 296
        default:
1171 hbuss 297
            kanal = 0; state = 0; kanal = AD_NICK;
298
            break;
1246 killagreg 299
        }
1171 hbuss 300
    ADMUX = kanal;
301
    if(state != 0) ANALOG_ON;
302
}
303