Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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