Subversion Repositories FlightCtrl

Rev

Rev 1210 | Rev 1212 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1051 killagreg 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.
1 ingob 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1051 killagreg 13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
1 ingob 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1051 killagreg 24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1 ingob 25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1051 killagreg 29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
1 ingob 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.
1051 killagreg 33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
1 ingob 34
// +     for non-commercial use (directly or indirectly)
1051 killagreg 35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1 ingob 36
// +     with our written permission
1051 killagreg 37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
1 ingob 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
1051 killagreg 50
// +  POSSIBILITY OF SUCH DAMAGE.
1 ingob 51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
#include "main.h"
53
 
54
unsigned char EEPromArray[E2END+1] EEMEM;
173 holgerb 55
unsigned char PlatinenVersion = 10;
723 hbuss 56
unsigned char SendVersionToNavi = 1;
1 ingob 57
// -- Parametersatz aus EEPROM lesen ---
1051 killagreg 58
// number [1..5]
1 ingob 59
void ReadParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
60
{
993 hbuss 61
   if((number > 5)||(number < 1)) number = 3;
1063 killagreg 62
   eeprom_read_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * (number - 1)], length);
921 hbuss 63
   LED_Init();
1 ingob 64
}
65
 
66
// -- Parametersatz ins EEPROM schreiben ---
1051 killagreg 67
// number [1..5]
1 ingob 68
void WriteParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
69
{
1051 killagreg 70
   if(number > 5) number = 5;
993 hbuss 71
   if(number < 1) return;
1063 killagreg 72
   eeprom_write_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * (number - 1)], length);
1171 hbuss 73
   eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_LENGTH], length); // Länge der Datensätze merken
74
   eeprom_write_block(buffer, &EEPromArray[EEPROM_ADR_CHANNELS], 8); // 8 Kanäle merken
1063 killagreg 75
   SetActiveParamSetNumber(number);
921 hbuss 76
   LED_Init();
1 ingob 77
}
78
 
79
unsigned char GetActiveParamSetNumber(void)
80
{
1063 killagreg 81
        unsigned char set;
82
        set = eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET]);
83
        if((set > 5) || (set < 1))
84
        {
85
                set = 3;
86
                SetActiveParamSetNumber(set);                           // diesen Parametersatz als aktuell merken
87
        }
88
        return(set);
1 ingob 89
}
90
 
1063 killagreg 91
 
92
void SetActiveParamSetNumber(unsigned char number)
93
{
94
        if(number > 5) number = 5;
95
        if(number < 1) return;
96
        eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], number);                                 // diesen Parametersatz als aktuell merken
97
}
98
 
99
 
819 hbuss 100
void CalMk3Mag(void)
101
{
102
 static unsigned char stick = 1;
103
 
104
 if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0;
105
 if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick)
106
  {
107
   stick = 1;
108
   WinkelOut.CalcState++;
1051 killagreg 109
   if(WinkelOut.CalcState > 4)
110
    {
819 hbuss 111
//     WinkelOut.CalcState = 0; // in Uart.c
112
     beeptime = 1000;
1051 killagreg 113
    }
819 hbuss 114
   else Piep(WinkelOut.CalcState);
115
  }
116
  DebugOut.Analog[19] = WinkelOut.CalcState;
117
}
118
 
1 ingob 119
//############################################################################
120
//Hauptprogramm
121
int main (void)
122
//############################################################################
123
{
1211 hbuss 124
        unsigned int timer,i;
1051 killagreg 125
    DDRB  = 0x00;
126
    PORTB = 0x00;
188 holgerb 127
    for(timer = 0; timer < 1000; timer++); // verzögern
1051 killagreg 128
    if(PINB & 0x01)
918 hbuss 129
     {
1051 killagreg 130
      if(PINB & 0x02) PlatinenVersion = 13;
131
       else           PlatinenVersion = 11;
918 hbuss 132
     }
1051 killagreg 133
    else
1021 hbuss 134
     {
1051 killagreg 135
      if(PINB & 0x02) PlatinenVersion = 20;
136
       else           PlatinenVersion = 10;
1021 hbuss 137
     }
138
 
173 holgerb 139
    DDRC  = 0x81; // SCL
1036 hbuss 140
    DDRC  |=0x40; // HEF4017 Reset
1 ingob 141
    PORTC = 0xff; // Pullup SDA
142
    DDRB  = 0x1B; // LEDs und Druckoffset
143
    PORTB = 0x01; // LED_Rot
144
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
1036 hbuss 145
    DDRD  |=0x80; // J7 -> Servo signal
1153 hbuss 146
        PORTD = 0x47; // LED
1171 hbuss 147
    HEF4017R_ON;
1 ingob 148
    MCUSR &=~(1<<WDRF);
149
    WDTCSR |= (1<<WDCE)|(1<<WDE);
150
    WDTCSR = 0;
151
 
152
    beeptime = 2000;
153
 
154
        StickGier = 0; PPM_in[K_GAS] = 0;StickRoll = 0; StickNick = 0;
1111 hbuss 155
    if(PlatinenVersion >= 20)  GIER_GRAD_FAKTOR = 1160; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC
1 ingob 156
    ROT_OFF;
1051 killagreg 157
 
1 ingob 158
    Timer_Init();
1156 hbuss 159
        TIMER2_Init();
1 ingob 160
        UART_Init();
161
    rc_sum_init();
162
        ADC_Init();
163
        i2c_init();
597 ingob 164
        SPI_MasterInit();
1051 killagreg 165
 
1 ingob 166
        sei();
167
 
1211 hbuss 168
        printf("\n\r===================================");
1051 killagreg 169
        printf("\n\rFlightControl\n\rHardware:%d.%d\n\rSoftware:V%d.%d%c ",PlatinenVersion/10,PlatinenVersion%10, VERSION_MAJOR, VERSION_MINOR,VERSION_PATCH + 'a');
1172 hbuss 170
    if(UCSR1A == 0x20 && UCSR1C == 0x06)  // initial Values for 644P
171
     {
172
      Uart1Init();
173
     }
1 ingob 174
        GRN_ON;
1063 killagreg 175
        ReadParameterSet(3, (unsigned char *) &EE_Parameter.Kanalbelegung[0], 9); // read only the first bytes
1051 killagreg 176
 
1211 hbuss 177
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE]) == MIXER_REVISION) // Check Revision in the first Byte
1209 hbuss 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;    
1211 hbuss 187
         printf("\n\rGenerating default Mixer Table");
1209 hbuss 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;
1211 hbuss 194
         Mixer.Revision = MIXER_REVISION;
1209 hbuss 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);
1211 hbuss 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
        for(i=0; i < MAX_MOTORS; i++)
205
         {
206
          UpdateMotor = 0;
207
      SendMotorData();
208
          while(!UpdateMotor);
209
          if(MotorPresent[i]) printf("%d ",i+1);
210
     }
211
        for(i=0; i < MAX_MOTORS; i++)
212
         {
213
          if(!MotorPresent[i] && Mixer.Motor[i][0] > 0) printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1);
214
          MotorError[i] = 0;
215
     }
216
        printf("\n\r===================================");
217
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
218
// + Check Settings
219
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1051 killagreg 220
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) != EE_DATENREVISION)
221
        {
1 ingob 222
          DefaultKonstanten1();
1171 hbuss 223
          printf("\n\rInit. EEPROM");
1051 killagreg 224
          for (unsigned char i=1;i<6;i++)
1 ingob 225
      {
395 hbuss 226
       if(i==2) DefaultKonstanten2(); // Kamera
227
       if(i==3) DefaultKonstanten3(); // Beginner
228
       if(i>3)  DefaultKonstanten2(); // Kamera
1111 hbuss 229
           if(PlatinenVersion >= 20)
230
            {
1167 hbuss 231
                 EE_Parameter.Gyro_D = 5;
1111 hbuss 232
                 EE_Parameter.Driftkomp = 0;
233
                 EE_Parameter.GyroAccFaktor = 27;
234
         EE_Parameter.WinkelUmschlagNick = 78;
235
         EE_Parameter.WinkelUmschlagRoll = 78;
236
                }
1171 hbuss 237
    // valid Stick-Settings?
238
           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)
239
            {
240
         EE_Parameter.Kanalbelegung[0] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+0]);
241
         EE_Parameter.Kanalbelegung[1] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+1]);
242
         EE_Parameter.Kanalbelegung[2] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+2]);
243
         EE_Parameter.Kanalbelegung[3] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+3]);
244
         EE_Parameter.Kanalbelegung[4] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+4]);
245
         EE_Parameter.Kanalbelegung[5] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+5]);
246
         EE_Parameter.Kanalbelegung[6] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+6]);
247
         EE_Parameter.Kanalbelegung[7] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS+7]);
248
         if(i==1) printf(": Generating Default-Parameter using old Stick Settings");
249
                } else DefaultStickMapping();
1 ingob 250
       WriteParameterSet(i, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
1051 killagreg 251
      }
1063 killagreg 252
          SetActiveParamSetNumber(3); // default-Setting
173 holgerb 253
          eeprom_write_byte(&EEPromArray[EEPROM_ADR_VALID], EE_DATENREVISION);
1 ingob 254
        }
513 hbuss 255
 
1051 killagreg 256
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4)
513 hbuss 257
     {
1211 hbuss 258
       printf("\n\rACC not calibrated !");
513 hbuss 259
     }
1051 killagreg 260
 
1 ingob 261
        ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
1211 hbuss 262
    printf("\n\rUsing parameterset %d", GetActiveParamSetNumber());
1 ingob 263
 
1051 killagreg 264
 
1 ingob 265
        if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
1051 killagreg 266
         {
1211 hbuss 267
           printf("\n\rCalibrating pressure sensor..");
1051 killagreg 268
           timer = SetDelay(1000);
1 ingob 269
       SucheLuftruckOffset();
270
           while (!CheckDelay(timer));
271
       printf("OK\n\r");
272
        }
1051 killagreg 273
 
1 ingob 274
        SetNeutral();
275
 
276
        ROT_OFF;
1051 killagreg 277
 
1 ingob 278
    beeptime = 2000;
1051 killagreg 279
    ExternControl.Digital[0] = 0x55;
1 ingob 280
 
1051 killagreg 281
 
1211 hbuss 282
        printf("\n\rControl: ");
1 ingob 283
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
1211 hbuss 284
        else printf("Normal (ACC-Mode)");
1051 killagreg 285
 
1211 hbuss 286
        printf("\n\r===================================\n\r");
1051 killagreg 287
 
1 ingob 288
    LcdClear();
173 holgerb 289
    I2CTimeout = 5000;
819 hbuss 290
    WinkelOut.Orientation = 1;
1 ingob 291
        while (1)
292
        {
1155 hbuss 293
            if(UpdateMotor && AdReady)      // ReglerIntervall
1051 killagreg 294
            {
1171 hbuss 295
                    UpdateMotor=0;
819 hbuss 296
            if(WinkelOut.CalcState) CalMk3Mag();
1051 killagreg 297
            else MotorRegler();
1 ingob 298
            SendMotorData();
299
            ROT_OFF;
1051 killagreg 300
            if(PcZugriff) PcZugriff--;
301
             else
492 hbuss 302
              {
595 hbuss 303
                           ExternControl.Config = 0;
492 hbuss 304
               ExternStickNick = 0;
305
               ExternStickRoll = 0;
306
               ExternStickGier = 0;
307
              }
1 ingob 308
            if(SenderOkay)  SenderOkay--;
1210 hbuss 309
            if(!--I2CTimeout || MissingMotor)
173 holgerb 310
                {
1210 hbuss 311
                  if(!I2CTimeout)
312
                                   {
313
                                    i2c_reset();
314
                    I2CTimeout = 5;
315
                                        }
1051 killagreg 316
                  if((BeepMuster == 0xffff) && MotorenEin)
317
                   {
173 holgerb 318
                    beeptime = 10000;
319
                    BeepMuster = 0x0080;
1051 killagreg 320
                   }
1 ingob 321
                }
1051 killagreg 322
            else
1 ingob 323
                {
1051 killagreg 324
                 ROT_OFF;
1 ingob 325
                }
805 hbuss 326
            if(SIO_DEBUG && (!UpdateMotor || !MotorenEin))
1 ingob 327
              {
328
               DatenUebertragung();
329
               BearbeiteRxDaten();
330
              }
331
              else BearbeiteRxDaten();
173 holgerb 332
         if(CheckDelay(timer))
1051 killagreg 333
            {
173 holgerb 334
            if(UBat < EE_Parameter.UnterspannungsWarnung)
1051 killagreg 335
                {
336
                  if(BeepMuster == 0xffff)
337
                   {
173 holgerb 338
                    beeptime = 6000;
339
                    BeepMuster = 0x0300;
1051 killagreg 340
                   }
173 holgerb 341
                }
1153 hbuss 342
             SPI_StartTransmitPacket();
723 hbuss 343
 
344
             SendSPI = 4;
1051 killagreg 345
                         timer = SetDelay(20);
346
            }
921 hbuss 347
           LED_Update();
604 hbuss 348
          }
723 hbuss 349
     if(!SendSPI) { SPI_TransmitByte(); }
1 ingob 350
    }
351
 return (1);
352
}
353