Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2506 - 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + www.MikroKopter.com
3
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// + Software Nutzungsbedingungen (english version: see below)
5
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
6
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
7
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
8
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
9
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
12
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
13
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
14
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
15
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
16
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
17
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
18
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
19
// + des Mitverschuldens offen.
20
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
21
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
22
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
23
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
24
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
25
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
26
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
27
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Software LICENSING TERMS
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
32
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
33
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
34
// + The Software may only be used with the Licensor's products.
35
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
36
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
37
// + agreement shall be the property of the Licensor.
38
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
39
// + features that can be used to identify the program may not be altered or defaced by the customer.
40
// + The customer shall be responsible for taking reasonable precautions
41
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
42
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
43
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
44
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
45
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
46
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
47
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
48
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
49
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
50
// + #### END OF LICENSING TERMS ####
51
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
52
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53
 
54
#include "main.h"
55
#include "eeprom.h"
56
volatile int  Aktuell_Nick,Aktuell_Roll,Aktuell_Gier,Aktuell_ax, Aktuell_ay,Aktuell_az, UBat = 150;
57
volatile int  AdWertNickFilter = 0, AdWertRollFilter = 0, AdWertGierFilter = 0;
58
volatile int  HiResNick = 2500, HiResRoll = 2500;
59
volatile int  AdWertNick = 0, AdWertRoll = 0, AdWertGier = 0;
60
volatile int  AdWertAccRoll = 0,AdWertAccNick = 0,AdWertAccHoch = 0;
61
volatile long Luftdruck = 32000;
62
volatile long SummenHoehe = 0;
63
volatile long StartLuftdruck;
64
volatile unsigned int  MessLuftdruck = 1023;
65
unsigned char DruckOffsetSetting;
66
signed char ExpandBaro = 0;
67
volatile int VarioMeter = 0;
68
volatile unsigned int ZaehlMessungen = 0;
69
unsigned char AnalogOffsetNick = 115,AnalogOffsetRoll = 115,AnalogOffsetGier = 115;
70
volatile unsigned char AdReady = 1;
71
unsigned int BaroStep = 500;
72
long ExpandBaroStep = 0;
73
long HoehenWertF = 0;
74
long HoehenWert_Mess = 0;
75
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
76
long HoehenWertF_Mess = 0;
77
unsigned char CalAthmospheare = 16;
78
unsigned char AD_ACC_Y = 6;
79
unsigned char AD_ACC_X = 7;
80
#endif
81
 
82
//#######################################################################################
83
void ADC_Init(void)
84
//#######################################################################################
85
{
86
    ADMUX = 0;//Referenz ist extern
87
    ANALOG_ON;
88
}
89
 
90
#define DESIRED_H_ADC 800
91
 
92
void CalcExpandBaroStep(void)
93
{
94
  if(ACC_AltitudeControl) ExpandBaroStep = BaroStep * (long)ExpandBaro;
95
  else ExpandBaroStep = (16 * BaroStep) * (long)ExpandBaro - 4;
96
}
97
 
98
void SucheLuftruckOffset(void)
99
{
100
 unsigned int off;
101
 ExpandBaro = 0;
102
 CalcExpandBaroStep();
103
  off = GetParamByte(PID_PRESSURE_OFFSET);
104
  if(off < 240) off += 10;
105
  OCR0A = off;
106
  OCR0B = 255-off;
107
  Delay_ms_Mess(150);
108
  if(MessLuftdruck > DESIRED_H_ADC) off = 240;
109
  for(; off > 5; off--)
110
   {
111
    OCR0A = off;
112
    OCR0B = 255-off;
113
    Delay_ms_Mess(100);
114
    printf(".");
115
    if(MessLuftdruck > DESIRED_H_ADC) break;
116
   }
117
   DruckOffsetSetting = off;
118
   SetParamByte(PID_PRESSURE_OFFSET, off);
119
 if((EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG) && (DruckOffsetSetting < 10 || DruckOffsetSetting >= 230)) VersionInfo.HardwareError[0] |= FC_ERROR0_PRESSURE;
120
 
121
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
122
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123
// + correction of the altitude error in higher altitudes
124
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125
 CalAthmospheare = 16;
126
 if(ACC_AltitudeControl)
127
  {
128
   if(PlatinenVersion < 23) { if(off < 140) CalAthmospheare += (160 - off) / 26; }
129
   else { if(off < 170) CalAthmospheare += (188 - off) / 19; }
130
  }
131
 Luftdruck = MessLuftdruck * CalAthmospheare;
132
#endif
133
 Delay_ms_Mess(300);
134
}
135
 
136
/*
137
void SucheGyroOffset(void)
138
{
139
 unsigned char i, ready = 0;
140
 int timeout;
141
 timeout = SetDelay(2000);
142
 for(i=140; i != 0; i--)
143
  {
144
   if(ready == 3 && i > 10) i = 9;
145
   ready = 0;
146
   if(AdWertNick < 1020) AnalogOffsetNick--; else if(AdWertNick > 1030) AnalogOffsetNick++; else ready++;
147
   if(AdWertRoll < 1020) AnalogOffsetRoll--; else if(AdWertRoll > 1030) AnalogOffsetRoll++; else ready++;
148
   if(AdWertGier < 1020) AnalogOffsetGier--; else if(AdWertGier > 1030) AnalogOffsetGier++; else ready++;
149
   I2C_Start(TWI_STATE_GYRO_OFFSET_TX);
150
   if(AnalogOffsetNick < 10)  { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; AnalogOffsetNick = 10;}; if(AnalogOffsetNick > 245) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_NICK; AnalogOffsetNick = 245;};
151
   if(AnalogOffsetRoll < 10)  { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; AnalogOffsetRoll = 10;}; if(AnalogOffsetRoll > 245) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_ROLL; AnalogOffsetRoll = 245;};
152
   if(AnalogOffsetGier < 10)  { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW;  AnalogOffsetGier = 10;}; if(AnalogOffsetGier > 245) { VersionInfo.HardwareError[0] |= FC_ERROR0_GYRO_YAW;  AnalogOffsetGier = 245;};
153
   while(twi_state) if(CheckDelay(timeout)) {printf("\n\r DAC or I2C ERROR! Check I2C, 3Vref, DAC and BL-Ctrl"); break;}
154
   AdReady = 0;
155
   ANALOG_ON;
156
   while(!AdReady);
157
   if(i<10) Delay_ms_Mess(10);
158
  }
159
   Delay_ms_Mess(70);
160
}
161
*/
162
/*
163
 
164
1  r
165
2     g
166
3     y
167
4     x
168
5  n
169
6  r
170
7     u
171
8     z
172
9     L
173
10 n
174
11 r
175
12    g
176
13    y
177
14    x
178
15 n
179
16 r
180
17    L
181
*/
182
 
183
 
184
//#######################################################################################
185
//
186
ISR(ADC_vect)
187
//#######################################################################################
188
{
189
    static unsigned char kanal=0,state = 0;
190
        static signed int subcount = 0;
191
    static signed int gier1, roll1, nick1, nick_filter, roll_filter;
192
        static signed int accy, accx;
193
        static long tmpLuftdruck = 0;
194
        static char messanzahl_Druck = 0;
195
    switch(state++)
196
        {
197
        case 0:
198
            nick1 = ADC;
199
            kanal = AD_ROLL;
200
            break;
201
        case 1:
202
            roll1 = ADC;
203
                    kanal = AD_GIER;
204
            break;
205
        case 2:
206
            gier1 = ADC;
207
            kanal = AD_ACC_Y;
208
            break;
209
        case 3:
210
            Aktuell_ay = NeutralAccY - ADC;
211
            accy = Aktuell_ay;
212
                    kanal = AD_ACC_X;
213
            break;
214
        case 4:
215
            Aktuell_ax = ADC - NeutralAccX;
216
            accx =  Aktuell_ax;
217
            kanal = AD_NICK;
218
            break;
219
        case 5:
220
            nick1 += ADC;
221
            kanal = AD_ROLL;
222
            break;
223
        case 6:
224
            roll1 += ADC;
225
            kanal = AD_UBAT;
226
            break;
227
        case 7:
228
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
229
            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
230
                        else  
231
#endif
232
                         {
233
                          static unsigned int tmpVoltage = 0;
234
                          if(!tmpVoltage) tmpVoltage = (10 * ADC);
235
                          if(tmpVoltage <= (10 * ADC)) tmpVoltage += 2; else tmpVoltage -= 2;
236
              UBat = tmpVoltage / 31;
237
                         }
238
                    kanal = AD_ACC_Z;
239
            break;
240
       case 8:
241
                         Aktuell_az = ADC;
242
                         AdWertAccHoch = Aktuell_az - NeutralAccZ - (int) NeutralAccZfine;
243
                     if(!ACC_AltitudeControl) // The Offset must be corrected, because of the ACC-Drift from vibrations
244
                     {
245
                      if(AdWertAccHoch > 1)
246
               {
247
                if(NeutralAccZ < 750)
248
                 {
249
                  subcount += 5;
250
                  if(modell_fliegt < 500) subcount += 10;
251
                  if(subcount > 100) { NeutralAccZ++; subcount -= 100;}
252
                 }
253
               }
254
               else if(AdWertAccHoch < -1)
255
               {
256
                if(NeutralAccZ > 550)
257
                 {
258
                  subcount -= 5;
259
                  if(modell_fliegt < 500) subcount -= 10;
260
                  if(subcount < -100) { NeutralAccZ--; subcount += 100;}
261
                 }
262
               }
263
             }
264
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
265
                         else
266
                         if(CosAttitude > 8192 - 50) // horizontal leveled within 6°
267
                     {
268
                      if(AdWertAccHoch > 1)
269
               {
270
                  if(++subcount > 5000)
271
                                   {
272
                    if(NeutralAccZfine < 6) NeutralAccZfine++;
273
                                        subcount -= 5000;
274
                                   }
275
               }
276
               else
277
                           if(AdWertAccHoch < -1)
278
               {
279
                  if(--subcount < -5000)
280
                                   {
281
                    if(NeutralAccZfine > -6) NeutralAccZfine--;
282
                                        subcount += 5000;
283
                                   }
284
               }
285
             }
286
#endif
287
            Mess_Integral_Hoch += AdWertAccHoch;      // Integrieren
288
            Mess_Integral_Hoch -= Mess_Integral_Hoch / 1024; // dämfen
289
                kanal = AD_DRUCK;
290
            break;
291
   // "case 9:" fehlt hier absichtlich
292
        case 10:
293
            nick1 += ADC;
294
            kanal = AD_ROLL;
295
            break;
296
        case 11:
297
            roll1 += ADC;
298
                    kanal = AD_GIER;
299
            break;
300
        case 12:
301
/*            if(PlatinenVersion == 10)  AdWertGier = (ADC + gier1 + 1) / 2;
302
            else
303
            if(PlatinenVersion >= 20)  AdWertGier = 2047 - (ADC + gier1);
304
                        else                                       AdWertGier = (ADC + gier1);
305
*/
306
                        AdWertGier = 2047 - (ADC + gier1);
307
            kanal = AD_ACC_Y;
308
            break;
309
        case 13:
310
            Aktuell_ay = NeutralAccY - ADC;
311
            AdWertAccRoll = (Aktuell_ay + accy);
312
            kanal = AD_ACC_X;
313
            break;
314
        case 14:
315
            Aktuell_ax = ADC - NeutralAccX;
316
            AdWertAccNick =  (Aktuell_ax + accx);
317
            kanal = AD_NICK;
318
            break;
319
        case 15:
320
            nick1 += ADC;
321
            //if(PlatinenVersion == 10) nick1 *= 2; else 
322
                        nick1 *= 4;
323
            AdWertNick = nick1 / 8;
324
            nick_filter = (nick_filter + nick1) / 2;
325
            HiResNick = nick_filter - AdNeutralNick;
326
            AdWertNickFilter = (AdWertNickFilter + HiResNick) / 2;
327
            kanal = AD_ROLL;
328
            break;
329
        case 16:
330
            roll1 += ADC;
331
            //if(PlatinenVersion == 10) roll1 *= 2; else 
332
                        roll1 *= 4;
333
            AdWertRoll = roll1 / 8;
334
            roll_filter = (roll_filter + roll1) / 2;
335
            HiResRoll = roll_filter - AdNeutralRoll;
336
            AdWertRollFilter = (AdWertRollFilter + HiResRoll) / 2;
337
                kanal = AD_DRUCK;
338
            break;
339
        case 17:
340
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
341
                        if(ACC_AltitudeControl)                
342
                        {
343
                         HoehenWertF_Mess = (ACC_AltitudeFusion(0) + SA_FILTER/2)/SA_FILTER;    // cm
344
            }
345
                        else HoehenWertF_Mess = HoehenWert;
346
#endif
347
            state = 0;
348
                        AdReady = 1;
349
            ZaehlMessungen++;
350
            // "break" fehlt hier absichtlich
351
        case 9:
352
                MessLuftdruck = ADC;
353
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
354
                        if(ACC_AltitudeControl)                
355
                        {
356
//                              ExpandBaroStep = BaroStep * (long)ExpandBaro; // wird in fc.c aufgerufen
357
//                              tmpLuftdruck = MessLuftdruck - BaroStep * (long)ExpandBaro;  // -523 counts per offset step
358
                                tmpLuftdruck = MessLuftdruck - ExpandBaroStep;  // -523 counts per offset step
359
                                Luftdruck -= Luftdruck / CalAthmospheare; // 16
360
                                Luftdruck += tmpLuftdruck;
361
                                HoehenWert_Mess = StartLuftdruck - Luftdruck;   // cm
362
                        }
363
                        else
364
#endif
365
            {   // old version (until FC V2.1)
366
                                tmpLuftdruck += MessLuftdruck;
367
                                if(++messanzahl_Druck >= 16) // war bis 0.86 "18"
368
                                {
369
                            signed int tmp;
370
//                              Luftdruck = (7 * Luftdruck + tmpLuftdruck - (16 * BaroStep) * (long)ExpandBaro + 4) / 8;  // -523.19 counts per 10 counts offset step
371
//                              ExpandBaroStep = (16 * BaroStep) * (long)ExpandBaro - 4; // wird in fc.c aufgerufen
372
                                Luftdruck = (7 * Luftdruck + tmpLuftdruck - ExpandBaroStep) / 8;  // -523.19 counts per 10 counts offset step
373
                                HoehenWert_Mess = StartLuftdruck - Luftdruck;
374
                                SummenHoehe -= SummenHoehe/SM_FILTER;
375
                                SummenHoehe += HoehenWert_Mess;
376
                                tmp = (HoehenWert_Mess - SummenHoehe/SM_FILTER);
377
                                if(tmp > 1024) tmp = 1024;      else if(tmp < -1024) tmp = -1024;
378
                if(abs(VarioMeter) > 700) VarioMeter = (15 * VarioMeter + 8 * tmp)/16;
379
                                else VarioMeter = (31 * VarioMeter + 8 * tmp)/32;
380
                tmpLuftdruck /= 2;
381
                messanzahl_Druck = 16/2;
382
                                }
383
                        }
384
            kanal = AD_NICK;
385
            break;
386
        default:
387
            kanal = 0; state = 0; kanal = AD_NICK;
388
            break;
389
        }
390
    ADMUX = kanal;
391
    if(state != 0) ANALOG_ON;
392
}
393