Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1234 ligi 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
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 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 to systems other than 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
// -- Parametersatz aus EEPROM lesen ---
59
// number [1..5]
60
void ReadParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
61
{
62
   if((number > 5)||(number < 1)) number = 3;
63
   eeprom_read_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * (number - 1)], length);
64
   LED_Init();
65
}
66
 
67
// -- Parametersatz ins EEPROM schreiben ---
68
// number [1..5]
69
void WriteParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
70
{
71
   if(number > 5) number = 5;
72
   if(number < 1) return;
73
   eeprom_write_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * (number - 1)], length);
74
   eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_LENGTH], length); // Länge der Datensätze merken
75
   eeprom_write_block(buffer, &EEPromArray[EEPROM_ADR_CHANNELS], 8); // 8 Kanäle merken
76
   SetActiveParamSetNumber(number);
77
   LED_Init();
78
}
79
 
80
unsigned char GetActiveParamSetNumber(void)
81
{
82
        unsigned char set;
83
        set = eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET]);
84
        if((set > 5) || (set < 1))
85
        {
86
                set = 3;
87
                SetActiveParamSetNumber(set);                           // diesen Parametersatz als aktuell merken
88
        }
89
        return(set);
90
}
91
 
92
 
93
void SetActiveParamSetNumber(unsigned char number)
94
{
95
        if(number > 5) number = 5;
96
        if(number < 1) return;
97
        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], number);                                 // diesen Parametersatz als aktuell merken
98
}
99
 
100
 
101
void CalMk3Mag(void)
102
{
103
 static unsigned char stick = 1;
104
 
105
 if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0;
106
 if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick)
107
  {
108
   stick = 1;
109
   WinkelOut.CalcState++;
110
   if(WinkelOut.CalcState > 4)
111
    {
112
//     WinkelOut.CalcState = 0; // in Uart.c
113
     beeptime = 1000;
114
    }
115
   else Piep(WinkelOut.CalcState,150);
116
  }
117
  DebugOut.Analog[19] = WinkelOut.CalcState;
118
}
119
 
120
//############################################################################
121
//Hauptprogramm
122
int main (void)
123
//############################################################################
124
{
125
        unsigned int timer,i;
126
    DDRB  = 0x00;
127
    PORTB = 0x00;
128
    for(timer = 0; timer < 1000; timer++); // verzögern
129
    if(PINB & 0x01)
130
     {
131
      if(PINB & 0x02) PlatinenVersion = 13;
132
       else           PlatinenVersion = 11;
133
     }
134
    else
135
     {
136
      if(PINB & 0x02) PlatinenVersion = 20;
137
       else           PlatinenVersion = 10;
138
     }
139
 
140
    DDRC  = 0x81; // SCL
141
    DDRC  |=0x40; // HEF4017 Reset
142
    PORTC = 0xff; // Pullup SDA
143
    DDRB  = 0x1B; // LEDs und Druckoffset
144
    PORTB = 0x01; // LED_Rot
145
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
146
        PORTD = 0x47; // LED
147
    HEF4017R_ON;
148
    MCUSR &=~(1<<WDRF);
149
    WDTCSR |= (1<<WDCE)|(1<<WDE);
150
    WDTCSR = 0;
151
 
152
    beeptime = 2000;
153
        StickGier = 0; PPM_in[K_GAS] = 0;StickRoll = 0; StickNick = 0;
154
    if(PlatinenVersion >= 20)  GIER_GRAD_FAKTOR = 1160; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC
155
    ROT_OFF;
156
 
157
    Timer_Init();
158
        TIMER2_Init();
159
        UART_Init();
160
    rc_sum_init();
161
        ADC_Init();
162
        i2c_init();
163
        SPI_MasterInit();
164
 
165
        sei();
166
 
167
        printf("\n\r===================================");
168
        printf("\n\rFlightControl\n\rHardware:%d.%d\n\rSoftware:V%d.%d%c ",PlatinenVersion/10,PlatinenVersion%10, VERSION_MAJOR, VERSION_MINOR,VERSION_PATCH + 'a');
169
    if(UCSR1A == 0x20 && UCSR1C == 0x06)  // initial Values for 644P
170
     {
171
      Uart1Init();
172
     }
173
        GRN_ON;
174
        ReadParameterSet(3, (unsigned char *) &EE_Parameter.Kanalbelegung[0], 9); // read only the first bytes
175
 
176
    if((eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE]) == MIXER_REVISION) && // Check Revision in the first Byte
177
       (eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) != 0xff))                   // Settings reset via Koptertool
178
        {
179
     unsigned char i;    
180
         RequiredMotors = 0;
181
     eeprom_read_block(&Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
182
     for(i=0; i<16;i++) { if(Mixer.Motor[i][0] > 0) RequiredMotors++;}
183
        }
184
        else // default
185
        {
186
     unsigned char i;    
187
         printf("\n\rGenerating default Mixer Table");
188
         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;};
189
     // default = Quadro
190
     Mixer.Motor[0][0] = 64; Mixer.Motor[0][1] = +64; Mixer.Motor[0][2] =   0; Mixer.Motor[0][3] = +64;
191
     Mixer.Motor[1][0] = 64; Mixer.Motor[1][1] = -64; Mixer.Motor[1][2] =   0; Mixer.Motor[1][3] = +64;
192
     Mixer.Motor[2][0] = 64; Mixer.Motor[2][1] =   0; Mixer.Motor[2][2] = -64; Mixer.Motor[2][3] = -64;
193
     Mixer.Motor[3][0] = 64; Mixer.Motor[3][1] =   0; Mixer.Motor[3][2] = +64; Mixer.Motor[3][3] = -64;
194
         Mixer.Revision = MIXER_REVISION;
195
     memcpy(Mixer.Name, "Quadro\0", 11);  
196
     eeprom_write_block(&Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
197
    }
198
    printf("\n\rMixer-Config: '%s' (%u Motors)",Mixer.Name,RequiredMotors);
199
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
200
// + Check connected BL-Ctrls
201
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
202
        printf("\n\rFound BL-Ctrl: ");
203
    motorread = 0;   UpdateMotor = 0;   SendMotorData(); while(!UpdateMotor); motorread = 0;  // read the first I2C-Data
204
    timer = SetDelay(2000);
205
        for(i=0; i < MAX_MOTORS; i++)
206
         {
207
          UpdateMotor = 0;
208
      SendMotorData();
209
          while(!UpdateMotor);
210
      if(Mixer.Motor[i][0] > 0) // wait max 2 sec for the BL-Ctrls to wake up
211
        {
212
             while(!CheckDelay(timer) && !MotorPresent[i]) {UpdateMotor = 0; SendMotorData(); while(!UpdateMotor);};
213
                }
214
          if(MotorPresent[i]) printf("%d ",i+1);
215
     }
216
        for(i=0; i < MAX_MOTORS; i++)
217
         {
218
          if(!MotorPresent[i] && Mixer.Motor[i][0] > 0) printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1);
219
          MotorError[i] = 0;
220
     }
221
        printf("\n\r===================================");
222
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
223
// + Check Settings
224
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
225
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) != EE_DATENREVISION)
226
        {
227
          DefaultKonstanten1();
228
          printf("\n\rInit. EEPROM");
229
          for (unsigned char i=1;i<6;i++)
230
      {
231
       if(i==2) DefaultKonstanten2(); // Kamera
232
       if(i==3) DefaultKonstanten3(); // Beginner
233
       if(i>3)  DefaultKonstanten2(); // Kamera
234
           if(PlatinenVersion >= 20)
235
            {
236
                 EE_Parameter.Gyro_D = 5;
237
                 EE_Parameter.Driftkomp = 0;
238
                 EE_Parameter.GyroAccFaktor = 27;
239
         EE_Parameter.WinkelUmschlagNick = 78;
240
         EE_Parameter.WinkelUmschlagRoll = 78;
241
                }
242
    // valid Stick-Settings?
243
           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)
244
            {
245
         EE_Parameter.Kanalbelegung[0] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+0]);
246
         EE_Parameter.Kanalbelegung[1] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]);
247
         EE_Parameter.Kanalbelegung[2] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]);
248
         EE_Parameter.Kanalbelegung[3] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]);
249
         EE_Parameter.Kanalbelegung[4] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+4]);
250
         EE_Parameter.Kanalbelegung[5] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+5]);
251
         EE_Parameter.Kanalbelegung[6] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+6]);
252
         EE_Parameter.Kanalbelegung[7] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+7]);
253
         if(i==1) printf(": Generating Default-Parameter using old Stick Settings");
254
                } else DefaultStickMapping();
255
       WriteParameterSet(i, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
256
      }
257
          SetActiveParamSetNumber(3); // default-Setting
258
          eeprom_write_byte(&EEPromArray[EEPROM_ADR_VALID], EE_DATENREVISION);
259
        }
260
 
261
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4)
262
     {
263
       printf("\n\rACC not calibrated !");
264
     }
265
 
266
        ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
267
    printf("\n\rUsing parameterset %d", GetActiveParamSetNumber());
268
 
269
 
270
        if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
271
         {
272
           printf("\n\rCalibrating pressure sensor..");
273
           timer = SetDelay(1000);
274
       SucheLuftruckOffset();
275
           while (!CheckDelay(timer));
276
       printf("OK\n\r");
277
        }
278
 
279
        SetNeutral();
280
 
281
        ROT_OFF;
282
 
283
    beeptime = 2000;
284
    ExternControl.Digital[0] = 0x55;
285
 
286
 
287
        printf("\n\rControl: ");
288
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
289
        else printf("Normal (ACC-Mode)");
290
 
291
    LcdClear();
292
    I2CTimeout = 5000;
293
    WinkelOut.Orientation = 1;
294
 
295
   printf("\n\rBatt:");
296
   if(EE_Parameter.UnterspannungsWarnung < 50) // automatische Zellenerkennung
297
    {
298
         timer = SetDelay(500);
299
         while (!CheckDelay(timer));
300
         if(UBat < 130)
301
          {
302
           BattLowVoltageWarning = 3 * EE_Parameter.UnterspannungsWarnung;
303
           Piep(3,200);
304
           printf(" 3 Cells  ");
305
          }
306
         else          
307
          {
308
           BattLowVoltageWarning = 4 * EE_Parameter.UnterspannungsWarnung;
309
           Piep(4,200);
310
           printf(" 4 Cells  ");
311
          }
312
    }
313
    else BattLowVoltageWarning = EE_Parameter.UnterspannungsWarnung;
314
    printf(" Low warning level: %d.%d",BattLowVoltageWarning/10,BattLowVoltageWarning%10);
315
        printf("\n\r===================================\n\r");
316
        while (1)
317
        {
318
            if(UpdateMotor && AdReady)      // ReglerIntervall
319
            {
320
                    UpdateMotor=0;
321
            if(WinkelOut.CalcState) CalMk3Mag();
322
            else MotorRegler();
323
            SendMotorData();
324
            ROT_OFF;
325
            if(PcZugriff) PcZugriff--;
326
             else
327
              {
328
                           ExternControl.Config = 0;
329
               ExternStickNick = 0;
330
               ExternStickRoll = 0;
331
               ExternStickGier = 0;
332
              }
333
            if(SenderOkay)  SenderOkay--;
334
                        else
335
                         {
336
                          TIMSK1 |= _BV(ICIE1); // enable PPM-Input
337
                         }
338
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160;
339
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101;
340
            if(NaviDataOkay)
341
                         {
342
                          if(--NaviDataOkay == 0)
343
                           {
344
                    GPS_Nick = 0;
345
                GPS_Roll = 0;
346
               }
347
              }
348
            if(!--I2CTimeout || MissingMotor)
349
                {
350
                  if(!I2CTimeout)
351
                                   {
352
                                    i2c_reset();
353
                    I2CTimeout = 5;
354
                                        }
355
                  if((BeepMuster == 0xffff) && MotorenEin)
356
                   {
357
                    beeptime = 10000;
358
                    BeepMuster = 0x0080;
359
                   }
360
                }
361
            else
362
                {
363
                 ROT_OFF;
364
                }
365
            if(SIO_DEBUG && (!UpdateMotor || !MotorenEin))
366
              {
367
               DatenUebertragung();
368
               BearbeiteRxDaten();
369
              }
370
              else BearbeiteRxDaten();
371
         if(CheckDelay(timer))
372
            {
373
            if(UBat < BattLowVoltageWarning)
374
                {
375
                  if(BeepMuster == 0xffff)
376
                   {
377
                    beeptime = 6000;
378
                    BeepMuster = 0x0300;
379
                   }
380
                }
381
             SPI_StartTransmitPacket();
382
 
383
             SendSPI = 4;
384
                         timer = SetDelay(20);
385
            }
386
           LED_Update();
387
          }
388
     if(!SendSPI) { SPI_TransmitByte(); }
389
    }
390
 return (1);
391
}
392
 
393