Subversion Repositories FlightCtrl

Rev

Rev 1452 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1400 acid 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) Holger Buss, Ingo Busker
3
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
#include "main.h"
53
 
54
unsigned char EEPromArray[E2END+1] EEMEM;
55
unsigned char PlatinenVersion = 10;
56
unsigned char SendVersionToNavi = 1;
57
unsigned char BattLowVoltageWarning = 94;
58
unsigned int FlugMinuten = 0,FlugMinutenGesamt = 0;
59
// -- Parametersatz aus EEPROM lesen ---
60
// number [1..5]
61
void ReadParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
62
{
63
   if((number > 5)||(number < 1)) number = 3;
64
   eeprom_read_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * (number - 1)], length);
65
   LED_Init();
66
}
67
 
68
// -- Parametersatz ins EEPROM schreiben ---
69
// number [1..5]
70
void WriteParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
71
{
72
   if(number > 5) number = 5;
73
   if(number < 1) return;
74
   eeprom_write_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * (number - 1)], length);
75
   eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_LENGTH], length); // Länge der Datensätze merken
76
   eeprom_write_block(buffer, &EEPromArray[EEPROM_ADR_CHANNELS], 8); // 8 Kanäle merken
77
   SetActiveParamSetNumber(number);
78
   LED_Init();
79
}
80
 
81
unsigned char GetActiveParamSetNumber(void)
82
{
83
        unsigned char set;
84
        set = eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET]);
85
        if((set > 5) || (set < 1))
86
        {
87
                set = 3;
88
                SetActiveParamSetNumber(set);                           // diesen Parametersatz als aktuell merken
89
        }
90
        return(set);
91
}
92
 
93
 
94
void SetActiveParamSetNumber(unsigned char number)
95
{
96
        if(number > 5) number = 5;
97
        if(number < 1) return;
98
        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], number);                                 // diesen Parametersatz als aktuell merken
99
}
100
 
101
 
102
void CalMk3Mag(void)
103
{
104
 static unsigned char stick = 1;
105
 
106
 if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0;
107
 if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick)
108
  {
109
   stick = 1;
110
   WinkelOut.CalcState++;
111
   if(WinkelOut.CalcState > 4)
112
    {
113
//     WinkelOut.CalcState = 0; // in Uart.c
114
     beeptime = 1000;
115
    }
116
   else Piep(WinkelOut.CalcState,150);
117
  }
118
  DebugOut.Analog[19] = WinkelOut.CalcState;
119
}
120
 
121
void LipoDetection(unsigned char print)
122
{
123
   unsigned int timer;
124
   if(print) printf("\n\rBatt:");
125
   if(EE_Parameter.UnterspannungsWarnung < 50) // automatische Zellenerkennung
126
    {
127
         timer = SetDelay(500);
128
         if(print) while (!CheckDelay(timer));
129
         if(UBat < 130)
130
          {
131
           BattLowVoltageWarning = 3 * EE_Parameter.UnterspannungsWarnung;
1452 acid 132
           if(print)
1400 acid 133
            {
134
                 Piep(3,200);
135
             printf(" 3 Cells  ");
1452 acid 136
                }
1400 acid 137
          }
138
         else
139
          {
140
           BattLowVoltageWarning = 4 * EE_Parameter.UnterspannungsWarnung;
1452 acid 141
           if(print)
1400 acid 142
            {
143
                 Piep(4,200);
144
             printf(" 4 Cells  ");
1452 acid 145
                }
1400 acid 146
          }
147
    }
148
    else BattLowVoltageWarning = EE_Parameter.UnterspannungsWarnung;
149
//      if(BattLowVoltageWarning < 93) BattLowVoltageWarning = 93;
150
   if(print) printf(" Low warning level: %d.%d",BattLowVoltageWarning/10,BattLowVoltageWarning%10);
151
}
152
 
153
//############################################################################
154
//Hauptprogramm
155
int main (void)
156
//############################################################################
157
{
158
        unsigned int timer,i,timer2 = 0;
159
    DDRB  = 0x00;
160
    PORTB = 0x00;
161
    for(timer = 0; timer < 1000; timer++); // verzögern
162
    if(PINB & 0x01)
163
     {
164
      if(PINB & 0x02) PlatinenVersion = 13;
165
       else           PlatinenVersion = 11;
166
     }
167
    else
168
     {
169
      if(PINB & 0x02) PlatinenVersion = 20;
170
       else           PlatinenVersion = 10;
171
     }
172
 
173
    DDRC  = 0x81; // SCL
174
    DDRC  |=0x40; // HEF4017 Reset
175
    PORTC = 0xff; // Pullup SDA
176
    DDRB  = 0x1B; // LEDs und Druckoffset
177
    PORTB = 0x01; // LED_Rot
178
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
179
        PORTD = 0x47; // LED
180
    HEF4017R_ON;
181
    MCUSR &=~(1<<WDRF);
182
    WDTCSR |= (1<<WDCE)|(1<<WDE);
183
    WDTCSR = 0;
184
 
185
    beeptime = 2000;
186
        StickGier = 0; PPM_in[K_GAS] = 0;StickRoll = 0; StickNick = 0;
187
    if(PlatinenVersion >= 20)  GIER_GRAD_FAKTOR = 1160; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC
188
    ROT_OFF;
189
 
190
    Timer_Init();
191
        TIMER2_Init();
192
        UART_Init();
193
    rc_sum_init();
194
        ADC_Init();
195
        i2c_init();
196
        SPI_MasterInit();
197
 
198
        sei();
199
 
200
        printf("\n\r===================================");
201
        printf("\n\rFlightControl\n\rHardware:%d.%d\n\rSoftware:V%d.%d%c ",PlatinenVersion/10,PlatinenVersion%10, VERSION_MAJOR, VERSION_MINOR,VERSION_PATCH + 'a');
202
        printf("\n\rthe use of this software is only permitted \n\ron original MikroKopter-Hardware");
203
        printf("\n\rwww.MikroKopter.de (c) HiSystems GmbH");
204
        printf("\n\r===================================");
205
 
206
    if(UCSR1A == 0x20 && UCSR1C == 0x06)  // initial Values for 644P
207
     {
208
      Uart1Init();
209
     }
210
        GRN_ON;
211
        ReadParameterSet(3, (unsigned char *) &EE_Parameter.Kanalbelegung[0], 9); // read only the first bytes
212
 
213
    if((eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE]) == MIXER_REVISION) && // Check Revision in the first Byte
214
       (eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) != 0xff))                   // Settings reset via Koptertool
215
        {
216
     unsigned char i;
217
         RequiredMotors = 0;
218
     eeprom_read_block(&Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
219
     for(i=0; i<16;i++) { if(Mixer.Motor[i][0] > 0) RequiredMotors++;}
220
        }
221
        else // default
222
        {
223
     unsigned char i;
224
         printf("\n\rGenerating default Mixer Table");
225
         for(i=0; i<16;i++) { Mixer.Motor[i][0] = 0;Mixer.Motor[i][1] = 0;Mixer.Motor[i][2] = 0;Mixer.Motor[i][3] = 0;};
226
     // default = Quadro
227
     Mixer.Motor[0][0] = 64; Mixer.Motor[0][1] = +64; Mixer.Motor[0][2] =   0; Mixer.Motor[0][3] = +64;
228
     Mixer.Motor[1][0] = 64; Mixer.Motor[1][1] = -64; Mixer.Motor[1][2] =   0; Mixer.Motor[1][3] = +64;
229
     Mixer.Motor[2][0] = 64; Mixer.Motor[2][1] =   0; Mixer.Motor[2][2] = -64; Mixer.Motor[2][3] = -64;
230
     Mixer.Motor[3][0] = 64; Mixer.Motor[3][1] =   0; Mixer.Motor[3][2] = +64; Mixer.Motor[3][3] = -64;
231
         Mixer.Revision = MIXER_REVISION;
232
     memcpy(Mixer.Name, "Quadro\0", 11);
233
     eeprom_write_block(&Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
234
    }
235
 
1465 acid 236
    for(int ii=0;ii<12;ii++) {
237
        printf("%d %d\n",ii,Mixer.Motor[1][ii]);
238
}
239
 
1400 acid 240
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
241
// + Check Settings
242
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
243
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) != EE_DATENREVISION)
244
        {
245
          DefaultKonstanten1();
246
          printf("\n\rInit. EEPROM");
247
          for (unsigned char i=1;i<6;i++)
248
      {
249
       if(i==2) DefaultKonstanten2(); // Kamera
250
       if(i==3) DefaultKonstanten3(); // Beginner
251
       if(i>3)  DefaultKonstanten2(); // Kamera
252
           if(PlatinenVersion >= 20)
253
            {
254
                 EE_Parameter.Gyro_D = 5;
255
                 EE_Parameter.Driftkomp = 0;
256
                 EE_Parameter.GyroAccFaktor = 27;
257
         EE_Parameter.WinkelUmschlagNick = 78;
258
         EE_Parameter.WinkelUmschlagRoll = 78;
259
                }
260
    // valid Stick-Settings?
261
           if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS]) < 12 && eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]) < 12 && eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]) < 12 && eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]) < 12)
262
            {
263
         EE_Parameter.Kanalbelegung[0] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+0]);
264
         EE_Parameter.Kanalbelegung[1] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]);
265
         EE_Parameter.Kanalbelegung[2] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]);
266
         EE_Parameter.Kanalbelegung[3] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]);
267
         EE_Parameter.Kanalbelegung[4] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+4]);
268
         EE_Parameter.Kanalbelegung[5] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+5]);
269
         EE_Parameter.Kanalbelegung[6] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+6]);
270
         EE_Parameter.Kanalbelegung[7] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+7]);
271
         if(i==1) printf(": Generating Default-Parameter using old Stick Settings");
272
                } else DefaultStickMapping();
273
       WriteParameterSet(i, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
274
      }
275
          SetActiveParamSetNumber(3); // default-Setting
276
          eeprom_write_byte(&EEPromArray[EEPROM_ADR_VALID], EE_DATENREVISION);
277
        }
278
 
279
    FlugMinuten = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_MINUTES2]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_MINUTES2+1]);
280
    FlugMinutenGesamt = (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_MINUTES]) * 256 + (int)eeprom_read_byte(&EEPromArray[EEPROM_ADR_MINUTES+1]);
281
    if(FlugMinutenGesamt == 0xffff || FlugMinuten == 0xffff)
282
         {
283
          FlugMinuten = 0;
284
          FlugMinutenGesamt = 0;
285
         }
286
    printf("\n\rFlight-time %u min  Total:%u min" ,FlugMinuten,FlugMinutenGesamt);
287
 
288
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4)
289
     {
290
       printf("\n\rACC not calibrated !");
291
     }
292
 
293
        ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
294
    printf("\n\rUsing parameterset %d", GetActiveParamSetNumber());
295
 
296
 
297
        if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
298
         {
299
           printf("\n\rCalibrating pressure sensor..");
300
           timer = SetDelay(1000);
301
       SucheLuftruckOffset();
302
           while (!CheckDelay(timer));
303
       printf("OK\n\r");
304
        }
305
 
306
        SetNeutral();
307
 
308
        ROT_OFF;
309
 
310
    beeptime = 2000;
311
    ExternControl.Digital[0] = 0x55;
312
 
313
 
314
        printf("\n\rControl: ");
315
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
316
        else printf("Normal (ACC-Mode)");
317
 
318
    LcdClear();
319
    I2CTimeout = 5000;
320
    WinkelOut.Orientation = 1;
321
    LipoDetection(1);
322
        printf("\n\r===================================\n\r");
323
//SpektrumBinding();
324
    timer = SetDelay(2000);
325
        while (1)
326
        {
327
            if(UpdateMotor && AdReady)      // ReglerIntervall
328
            {
329
                    UpdateMotor=0;
330
            if(WinkelOut.CalcState) CalMk3Mag();
331
            else MotorRegler();
332
            SendMotorData();
333
            ROT_OFF;
334
            if(SenderOkay)  SenderOkay--;
335
                        else TIMSK1 |= _BV(ICIE1); // enable PPM-Input
1452 acid 336
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1400 acid 337
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160;
338
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101;
339
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
340
            if(NaviDataOkay)
341
                         {
342
                          if(--NaviDataOkay == 0)
343
                           {
344
                    GPS_Nick = 0;
345
                GPS_Roll = 0;
346
               }
347
              }
1465 acid 348
            if(!--I2CTimeout || MissingMotor)
1400 acid 349
                {
350
                  if(!I2CTimeout)
351
                                   {
352
                                    i2c_reset();
353
                    I2CTimeout = 5;
354
                                        DebugOut.Analog[28]++; // I2C-Error
355
                                   }
356
                  if((BeepMuster == 0xffff) && MotorenEin)
357
                   {
358
                    beeptime = 10000;
359
                    BeepMuster = 0x0080;
360
                   }
361
                }
362
            else
363
                {
364
                 ROT_OFF;
365
                }
366
            if(SIO_DEBUG && (!UpdateMotor || !MotorenEin))
367
              {
368
               DatenUebertragung();
369
               BearbeiteRxDaten();
370
              }
371
                        else BearbeiteRxDaten();
372
                        if(CheckDelay(timer))
373
                        {
374
                          timer += 20;
375
              if(PcZugriff) PcZugriff--;
376
              else
377
               {
378
                            ExternControl.Config = 0;
379
                ExternStickNick = 0;
380
                ExternStickRoll = 0;
381
                ExternStickGier = 0;
382
                if(BeepMuster == 0xffff && SenderOkay == 0)
383
                {
384
                 beeptime = 15000;
385
                 BeepMuster = 0x0c00;
386
                }
387
               }
388
                           if(UBat < BattLowVoltageWarning)
389
                                {
390
                                        MikroKopterFlags |= FLAG_LOWBAT;
391
                                        if(BeepMuster == 0xffff)
392
                                        {
393
                                                beeptime = 6000;
394
                                                BeepMuster = 0x0300;
395
                                        }
396
                                }
397
                                else MikroKopterFlags &= ~FLAG_LOWBAT;
398
                                SPI_StartTransmitPacket();
399
                                SendSPI = 4;
400
                                if(!MotorenEin) timer2 = 1450; // 0,5 Minuten aufrunden
401
                                if(++timer2 == 2930)  // eine Minute
402
                                 {
403
                                   timer2 = 0;
404
                   FlugMinuten++;
405
                       FlugMinutenGesamt++;
1452 acid 406
                   eeprom_write_byte(&EEPromArray[EEPROM_ADR_MINUTES2],FlugMinuten / 256);
407
                   eeprom_write_byte(&EEPromArray[EEPROM_ADR_MINUTES2+1],FlugMinuten % 256);
408
                   eeprom_write_byte(&EEPromArray[EEPROM_ADR_MINUTES],FlugMinutenGesamt / 256);
409
                   eeprom_write_byte(&EEPromArray[EEPROM_ADR_MINUTES+1],FlugMinutenGesamt % 256);
1400 acid 410
                                   timer = SetDelay(20); // falls "timer += 20;" mal nicht geht
411
                             }
412
                        }
413
           LED_Update();
414
          }
415
     if(!SendSPI) { SPI_TransmitByte(); }
416
    }
417
 return (1);
418
}
419
 
420