Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
911 - 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
 
55
unsigned char PlatinenVersion = 10;
56
unsigned char SendVersionToNavi = 1;
57
unsigned char BattLowVoltageWarning = 94;
58
unsigned int FlugMinuten = 0,FlugMinutenGesamt = 0;
59
unsigned int FlugSekunden = 0;
60
pVoidFnct_pVoidFnctChar_const_fmt _printf_P;
61
unsigned char FoundMotors = 0;
62
 
63
void CalMk3Mag(void)
64
{
65
 static unsigned char stick = 1;
66
 if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0;
67
 if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick)
68
  {
69
   stick = 1;
70
   WinkelOut.CalcState++;
71
   if(WinkelOut.CalcState > 4)
72
    {
73
//     WinkelOut.CalcState = 0; // in Uart.c
74
     beeptime = 1000;
75
    }
76
   else Piep(WinkelOut.CalcState,150);
77
  }
78
  DebugOut.Analog[19] = WinkelOut.CalcState;
79
}
80
 
81
 
82
void LipoDetection(unsigned char print)
83
{
84
        #define MAX_CELL_VOLTAGE 43 // max cell volatage for LiPO
85
        unsigned int timer, cells;
86
        if(print) printf("\n\rBatt:");
87
        if(EE_Parameter.UnterspannungsWarnung < 50) // automatische Zellenerkennung
88
        {
89
                timer = SetDelay(500);
90
                if(print) while (!CheckDelay(timer));
91
                // up to 6s LiPo, less than 2s is technical impossible
92
                for(cells = 2; cells < 7; cells++)
93
                {
94
                        if(UBat < cells * MAX_CELL_VOLTAGE) break;
95
                }
96
 
97
                BattLowVoltageWarning = cells * EE_Parameter.UnterspannungsWarnung;
98
                if(print)
99
                {
100
                        Piep(cells, 200);
101
                        printf(" %d Cells ", cells);
102
                }
103
        }
104
        else BattLowVoltageWarning = EE_Parameter.UnterspannungsWarnung;
105
        if(print) printf(" Low warning level: %d.%d",BattLowVoltageWarning/10,BattLowVoltageWarning%10);
106
}
107
 
108
//############################################################################
109
//Hauptprogramm
110
int main (void)
111
//############################################################################
112
{
113
        unsigned int timer,i,timer2 = 0, timerPolling;
114
 
115
    DDRB  = 0x00;
116
    PORTB = 0x00;
117
    for(timer = 0; timer < 1000; timer++); // verzögern
118
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
119
        PlatinenVersion = 21;
120
#else
121
        if(PINB & 0x01)
122
     {
123
      if(PINB & 0x02) PlatinenVersion = 13;
124
       else           PlatinenVersion = 11;
125
     }
126
    else
127
     {
128
      if(PINB & 0x02) PlatinenVersion = 20;
129
       else           PlatinenVersion = 10;
130
     }
131
#endif
132
    DDRC  = 0x81; // SCL
133
    DDRC  |=0x40; // HEF4017 Reset
134
    PORTC = 0xff; // Pullup SDA
135
    DDRB  = 0x1B; // LEDs und Druckoffset
136
    PORTB = 0x01; // LED_Rot
137
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
138
        PORTD = 0x47; // LED
139
    HEF4017R_ON;
140
    MCUSR &=~(1<<WDRF);
141
    WDTCSR |= (1<<WDCE)|(1<<WDE);
142
    WDTCSR = 0;
143
 
144
    beeptime = 2500;
145
        StickGier = 0; PPM_in[K_GAS] = 0; StickRoll = 0; StickNick = 0;
146
    if(PlatinenVersion >= 20)  GIER_GRAD_FAKTOR = 1220; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC
147
    ROT_OFF;
148
 
149
//      printf("\n\rInitialising: ");
150
    Timer_Init();
151
        TIMER2_Init();
152
        UART_Init();
153
    rc_sum_init();
154
        ADC_Init();
155
        I2C_Init(1);
156
        SPI_MasterInit();
157
        Capacity_Init();
158
        LIBFC_Init();
159
        GRN_ON;
160
    sei();
161
        ParamSet_Init();
162
        printf("\n\rInitialised:\n\r");
163
 
164
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
165
// + Check connected BL-Ctrls
166
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
167
        // Check connected BL-Ctrls
168
        BLFlags |= BLFLAG_READ_VERSION;
169
        motor_read = 0;  // read the first I2C-Data
170
        SendMotorData();
171
        timer = SetDelay(500);
172
        while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer
173
 
174
    printf("\n\rFound BL-Ctrl: ");
175
    timer = SetDelay(4000);
176
        for(i=0; i < MAX_MOTORS; i++)
177
        {
178
                SendMotorData();
179
                while(!(BLFlags & BLFLAG_TX_COMPLETE)  && !CheckDelay(timer)); //wait for complete transfer
180
                if(Mixer.Motor[i][0] > 0) // wait max 4 sec for the BL-Ctrls to wake up
181
                {
182
                        while(!CheckDelay(timer) && !(Motor[i].State & MOTOR_STATE_PRESENT_MASK) )
183
                        {
184
                                SendMotorData();
185
                                while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer
186
                        }
187
                }
188
                if(Motor[i].State & MOTOR_STATE_PRESENT_MASK)
189
                {
190
                        printf("%d",i+1);
191
                        FoundMotors++;
192
//                      if(Motor[i].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) printf("(new) ");
193
                }
194
        }
195
        for(i=0; i < MAX_MOTORS; i++)
196
        {
197
                if(!(Motor[i].State & MOTOR_STATE_PRESENT_MASK) && Mixer.Motor[i][0] > 0)
198
                {
199
                        printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1);
200
                        ServoActive = 2; // just in case the FC would be used as camera-stabilizer
201
                }
202
                Motor[i].State &= ~MOTOR_STATE_ERROR_MASK; // clear error counter
203
        }
204
        printf("\n\r===================================");
205
 
206
    if(RequiredMotors < FoundMotors) VersionInfo.HardwareError[1] |= FC_ERROR1_MIXER;
207
 
208
        //if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
209
        {
210
                //printf("\n\rCalibrating pressure sensor..");
211
                //timer = SetDelay(1000);
212
                //SucheLuftruckOffset();
213
                //while (!CheckDelay(timer));
214
                //printf("OK\n\r");
215
                printf("\n\rCalibrating IR sensor..");
216
                timer = SetDelay(1000);
217
                //SucheLuftruckOffset();
218
                while (!CheckDelay(timer));
219
                printf("OK\n\r");
220
        }
221
 
222
        SetNeutral(0);
223
 
224
        ROT_OFF;
225
 
226
    beeptime = 2000;
227
    ExternControl.Digital[0] = 0x55;
228
 
229
 
230
        FlugMinuten = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES + 1);
231
        FlugMinutenGesamt = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL + 1);
232
 
233
        if((FlugMinutenGesamt == 0xFFFF) || (FlugMinuten == 0xFFFF))
234
        {
235
                FlugMinuten = 0;
236
                FlugMinutenGesamt = 0;
237
        }
238
    printf("\n\rFlight-time %u min  Total:%u min", FlugMinuten, FlugMinutenGesamt);
239
 
240
        printf("\n\rControl: ");
241
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
242
        else printf("Normal (ACC-Mode)");
243
 
244
    LcdClear();
245
    I2CTimeout = 5000;
246
    WinkelOut.Orientation = 1;
247
    LipoDetection(1);
248
 
249
        LIBFC_ReceiverInit(EE_Parameter.Receiver);
250
 
251
        printf("\n\r===================================\n\r");
252
        //SpektrumBinding();
253
    timer = SetDelay(2000);
254
        timerPolling = SetDelay(250);
255
 
256
        Debug(ANSI_CLEAR "FC-Start!\n\rFlugzeit: %d min", FlugMinutenGesamt);   // Note: this won't waste flash memory, if #DEBUG is not active
257
    DebugOut.Status[0] = 0x01 | 0x02;
258
 
259
        while (1)
260
        {
261
 
262
        if (JetiUpdateModeActive) while (1);
263
 
264
        if(CheckDelay(timerPolling))
265
        {
266
          timerPolling = SetDelay(100);
267
          LIBFC_Polling();
268
        }
269
        if(UpdateMotor && AdReady)      // ReglerIntervall
270
            {
271
                    UpdateMotor=0;
272
            if(WinkelOut.CalcState) CalMk3Mag();
273
            else  MotorRegler();
274
                        SendMotorData();
275
            ROT_OFF;
276
            if(SenderOkay)  { SenderOkay--; VersionInfo.HardwareError[1] &= ~FC_ERROR1_PPM; }
277
                        else
278
                        {
279
                                TIMSK1 |= _BV(ICIE1); // enable PPM-Input
280
                                PPM_in[0] = 0; // set RSSI to zero on data timeout
281
                                VersionInfo.HardwareError[1] |= FC_ERROR1_PPM;
282
                        }
283
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
284
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160;
285
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101;
286
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
287
            if(!--I2CTimeout || MissingMotor)
288
                {
289
                  if(!I2CTimeout)
290
                                   {
291
                                    I2C_Reset();
292
                    I2CTimeout = 5;
293
                                        DebugOut.Analog[28]++; // I2C-Error
294
                                        VersionInfo.HardwareError[1] |= FC_ERROR1_I2C;
295
                                        DebugOut.Status[1] |= 0x02; // BL-Error-Status
296
                                   }
297
                  if((BeepMuster == 0xffff) && MotorenEin)
298
                   {
299
                    beeptime = 10000;
300
                    BeepMuster = 0x0080;
301
                   }
302
                }
303
            else
304
                {
305
                 ROT_OFF;
306
                                 if(!beeptime)
307
                                  {
308
                                   VersionInfo.HardwareError[1] &= ~FC_ERROR1_I2C;
309
                                  }
310
                }
311
          if(!UpdateMotor)
312
                   {
313
                        if(CalculateServoSignals) CalculateServo();
314
                        DatenUebertragung();
315
                        BearbeiteRxDaten();
316
                        if(CheckDelay(timer))
317
                        {
318
                                static unsigned char second;
319
                                timer += 20; // 20 ms interval
320
                                if(MissingMotor)
321
                                 {
322
                                  VersionInfo.HardwareError[1] |= FC_ERROR1_BL_MISSING;
323
                                  DebugOut.Status[1] |= 0x02; // BL-Error-Status
324
                                 }
325
                                 else
326
                                 {
327
                                   VersionInfo.HardwareError[1] &= ~FC_ERROR1_BL_MISSING;
328
                                   if(I2CTimeout > 6) DebugOut.Status[1] &= ~0x02; // BL-Error-Status
329
                                 }
330
 
331
                            if(I2CTimeout > 6) VersionInfo.HardwareError[1] &= ~FC_ERROR1_I2C;
332
 
333
                                if(PcZugriff) PcZugriff--;
334
                                else
335
                                {
336
                                        ExternControl.Config = 0;
337
                                        ExternStickNick = 0;
338
                                        ExternStickRoll = 0;
339
                                        ExternStickGier = 0;
340
                                        if(BeepMuster == 0xffff && SenderOkay == 0)
341
                                        {
342
                                                beeptime = 15000;
343
                                                BeepMuster = 0x0c00;
344
                                        }
345
                                }
346
                                if(NaviDataOkay > 200)
347
                                {
348
                                        NaviDataOkay--;
349
                                        VersionInfo.HardwareError[1] &= ~FC_ERROR1_SPI_RX;
350
                                }
351
                                else
352
                                {
353
                                        if(NC_Version.Compatible)
354
                                         {
355
                                                VersionInfo.HardwareError[1] |= FC_ERROR1_SPI_RX;
356
                       if(BeepMuster == 0xffff && MotorenEin)
357
                                                {
358
                                                        beeptime = 15000;
359
                                                        BeepMuster = 0xA800;
360
                                                }
361
                                         }
362
                                        GPS_Nick = 0;
363
                                        GPS_Roll = 0;
364
                                        //if(!beeptime)
365
                    FromNaviCtrl.CompassValue = -1;
366
                    NaviDataOkay = 0;
367
                                }
368
                           if(UBat < BattLowVoltageWarning)
369
                                {
370
                                        FC_StatusFlags |= FC_STATUS_LOWBAT;
371
                                        if(BeepMuster == 0xffff)
372
                                        {
373
                                                beeptime = 6000;
374
                                                BeepMuster = 0x0300;
375
                                        }
376
                                }
377
                                else if(!beeptime) FC_StatusFlags &= ~FC_STATUS_LOWBAT;
378
 
379
                                SPI_StartTransmitPacket();
380
                                SendSPI = 4;
381
                                if(!MotorenEin) timer2 = 1450; // 0,5 Minuten aufrunden
382
                                else
383
                if(++second == 49)
384
                                 {
385
                                   second = 0;
386
                                   FlugSekunden++;
387
                                 }
388
                                if(++timer2 == 2930)  // eine Minute
389
                                 {
390
                                   timer2 = 0;
391
                   FlugMinuten++;
392
                       FlugMinutenGesamt++;
393
                   SetParamByte(PID_FLIGHT_MINUTES,FlugMinuten / 256);
394
                   SetParamByte(PID_FLIGHT_MINUTES+1,FlugMinuten % 256);
395
                   SetParamByte(PID_FLIGHT_MINUTES_TOTAL,FlugMinutenGesamt / 256);
396
                   SetParamByte(PID_FLIGHT_MINUTES_TOTAL+1,FlugMinutenGesamt % 256);
397
                                   timer = SetDelay(20); // falls "timer += 20;" mal nicht geht
398
                             }
399
                        }
400
           LED_Update();
401
           Capacity_Update();
402
           }
403
          }
404
     if(!SendSPI) { SPI_TransmitByte(); }
405
    }
406
 return (1);
407
}
408
 
409