Subversion Repositories FlightCtrl

Rev

Rev 1038 | Rev 1053 | 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
// + only for non-profit use
4
// + www.MikroKopter.com
5
// + see the File "License.txt" for further Informations
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1051 killagreg 7
#include <stdarg.h>
8
#include <string.h>
1 ingob 9
#include "main.h"
10
#include "uart.h"
11
 
1051 killagreg 12
unsigned char GetExternalControl = 0,DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0, GetPPMChannelAnforderung = 0;
1 ingob 13
unsigned volatile char SioTmp = 0;
14
unsigned volatile char SendeBuffer[MAX_SENDE_BUFF];
15
unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF];
16
unsigned volatile char NMEABuffer[MAX_EMPFANGS_BUFF];
17
unsigned volatile char NeuerDatensatzEmpfangen = 0;
18
unsigned volatile char NeueKoordinateEmpfangen = 0;
19
unsigned volatile char UebertragungAbgeschlossen = 1;
20
unsigned volatile char CntCrcError = 0;
21
unsigned volatile char AnzahlEmpfangsBytes = 0;
1051 killagreg 22
unsigned char *pRxData = 0;
23
unsigned char RxDataLen = 0;
1 ingob 24
unsigned volatile char PC_DebugTimeout = 0;
1051 killagreg 25
 
499 hbuss 26
unsigned char DebugTextAnforderung = 255;
1 ingob 27
unsigned char PcZugriff = 100;
28
unsigned char MotorTest[4] = {0,0,0,0};
1036 hbuss 29
unsigned char MeineSlaveAdresse = 1; // Flight-Ctrl
595 hbuss 30
unsigned char ConfirmFrame;
1 ingob 31
struct str_DebugOut    DebugOut;
595 hbuss 32
struct str_ExternControl  ExternControl;
1 ingob 33
struct str_VersionInfo VersionInfo;
693 hbuss 34
struct str_WinkelOut WinkelOut;
1 ingob 35
 
693 hbuss 36
int Debug_Timer,Kompass_Timer;
1051 killagreg 37
unsigned int DebugDataIntervall = 200;
693 hbuss 38
 
499 hbuss 39
const unsigned char ANALOG_TEXT[32][16] =
40
{
1051 killagreg 41
   //1234567890123456
499 hbuss 42
    "IntegralNick    ", //0
43
    "IntegralRoll    ",
1051 killagreg 44
    "AccNick         ",
499 hbuss 45
    "AccRoll         ",
46
    "GyroGier        ",
47
    "HoehenWert      ", //5
48
    "AccZ            ",
49
    "Gas             ",
50
    "KompassValue    ",
513 hbuss 51
    "Spannung        ",
52
    "Empfang         ", //10
693 hbuss 53
    "Ersatzkompass   ",
499 hbuss 54
    "Motor_Vorne     ",
55
    "Motor_Hinten    ",
56
    "Motor_Links     ",
57
    "Motor_Rechts    ", //15
929 hbuss 58
    "                ",
744 hbuss 59
    "Distance        ",
60
    "OsdBar          ",
819 hbuss 61
    "MK3Mag CalState ",
854 hbuss 62
    "Servo           ", //20
63
    "Nick            ",
64
    "Roll            ",
720 ingob 65
    "                ",
66
    "                ",
67
    "                ", //25
68
    "                ",
992 hbuss 69
    "Kalman_MaxDrift ",
720 ingob 70
    "                ",
992 hbuss 71
    "Kalman K        ",
720 ingob 72
    "GPS_Nick        ", //30
73
    "GPS_Roll        "
499 hbuss 74
};
75
 
76
 
77
 
1 ingob 78
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
79
//++ Sende-Part der Datenübertragung
80
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
81
SIGNAL(INT_VEC_TX)
82
{
83
 static unsigned int ptr = 0;
84
 unsigned char tmp_tx;
1051 killagreg 85
 if(!UebertragungAbgeschlossen)
1 ingob 86
  {
87
   ptr++;                    // die [0] wurde schon gesendet
1051 killagreg 88
   tmp_tx = SendeBuffer[ptr];
1 ingob 89
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
90
    {
91
     ptr = 0;
92
     UebertragungAbgeschlossen = 1;
93
    }
1051 killagreg 94
   UDR = tmp_tx;
95
  }
1 ingob 96
  else ptr = 0;
97
}
98
 
99
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
100
//++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung
101
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
102
SIGNAL(INT_VEC_RX)
103
{
104
 static unsigned int crc;
105
 static unsigned char crc1,crc2,buf_ptr;
106
 static unsigned char UartState = 0;
107
 unsigned char CrcOkay = 0;
108
 
1051 killagreg 109
 SioTmp = UDR;
1 ingob 110
 if(buf_ptr >= MAX_EMPFANGS_BUFF)    UartState = 0;
1051 killagreg 111
 if(SioTmp == '\r' && UartState == 2)
1 ingob 112
  {
113
   UartState = 0;
114
   crc -= RxdBuffer[buf_ptr-2];
115
   crc -= RxdBuffer[buf_ptr-1];
116
   crc %= 4096;
117
   crc1 = '=' + crc / 64;
118
   crc2 = '=' + crc % 64;
119
   CrcOkay = 0;
120
   if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;};
121
   if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
122
    {
1051 killagreg 123
     NeuerDatensatzEmpfangen = 1;
124
         AnzahlEmpfangsBytes = buf_ptr + 1;
1 ingob 125
     RxdBuffer[buf_ptr] = '\r';
173 holgerb 126
         if(RxdBuffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando
1051 killagreg 127
        }
1 ingob 128
  }
129
  else
130
  switch(UartState)
131
  {
132
   case 0:
133
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
134
                  buf_ptr = 0;
135
                  RxdBuffer[buf_ptr++] = SioTmp;
136
                  crc = SioTmp;
137
          break;
138
   case 1: // Adresse auswerten
139
                  UartState++;
140
                  RxdBuffer[buf_ptr++] = SioTmp;
141
                  crc += SioTmp;
142
                  break;
143
   case 2: //  Eingangsdaten sammeln
144
                  RxdBuffer[buf_ptr] = SioTmp;
1051 killagreg 145
                  if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++;
1 ingob 146
                  else UartState = 0;
147
                  crc += SioTmp;
148
                  break;
1051 killagreg 149
   default:
150
          UartState = 0;
1 ingob 151
          break;
152
  }
153
}
154
 
155
 
156
// --------------------------------------------------------------------------
157
void AddCRC(unsigned int wieviele)
158
{
1051 killagreg 159
 unsigned int tmpCRC = 0,i;
1 ingob 160
 for(i = 0; i < wieviele;i++)
161
  {
162
   tmpCRC += SendeBuffer[i];
163
  }
164
   tmpCRC %= 4096;
165
   SendeBuffer[i++] = '=' + tmpCRC / 64;
166
   SendeBuffer[i++] = '=' + tmpCRC % 64;
167
   SendeBuffer[i++] = '\r';
168
  UebertragungAbgeschlossen = 0;
169
  UDR = SendeBuffer[0];
170
}
171
 
172
 
173
 
174
// --------------------------------------------------------------------------
1051 killagreg 175
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char BufferAnzahl, ...) //unsigned char *snd, unsigned char len)
1 ingob 176
{
1051 killagreg 177
 va_list ap;
1 ingob 178
 unsigned int pt = 0;
179
 unsigned char a,b,c;
180
 unsigned char ptr = 0;
181
 
1051 killagreg 182
 unsigned char *snd = 0;
183
 int len = 0;
184
 
1 ingob 185
 SendeBuffer[pt++] = '#';               // Startzeichen
186
 SendeBuffer[pt++] = modul;             // Adresse (a=0; b=1,...)
187
 SendeBuffer[pt++] = cmd;                       // Commando
188
 
1051 killagreg 189
 va_start(ap, BufferAnzahl);
190
 if(BufferAnzahl)
191
 {
192
                snd = va_arg(ap, unsigned char*);
193
                len = va_arg(ap, int);
194
                ptr = 0;
195
                BufferAnzahl--;
196
 }
1 ingob 197
 while(len)
198
  {
1051 killagreg 199
        if(len)
200
        {
201
           a = snd[ptr++];
202
           len--;
203
           if((!len) && BufferAnzahl)
204
                {
205
                        snd = va_arg(ap, unsigned char*);
206
                        len = va_arg(ap, int);
207
                        ptr = 0;
208
                        BufferAnzahl--;
209
                }
210
        }
211
        else a = 0;
212
        if(len)
213
        {
214
                b = snd[ptr++];
215
                len--;
216
                if((!len) && BufferAnzahl)
217
                {
218
                        snd = va_arg(ap, unsigned char*);
219
                        len = va_arg(ap, int);
220
                        ptr = 0;
221
                        BufferAnzahl--;
222
                }
223
        }
224
        else b = 0;
225
        if(len)
226
        {
227
                c = snd[ptr++];
228
                len--;
229
                if((!len) && BufferAnzahl)
230
                {
231
                        snd = va_arg(ap, unsigned char*);
232
                        len = va_arg(ap, int);
233
                        ptr = 0;
234
                        BufferAnzahl--;
235
                }
236
        }
237
        else c = 0;
1 ingob 238
   SendeBuffer[pt++] = '=' + (a >> 2);
239
   SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
240
   SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
241
   SendeBuffer[pt++] = '=' + ( c & 0x3f);
242
  }
1051 killagreg 243
 va_end(ap);
1 ingob 244
 AddCRC(pt);
245
}
246
 
247
 
248
// --------------------------------------------------------------------------
1051 killagreg 249
void Decode64(void)  // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden.
1 ingob 250
{
251
 unsigned char a,b,c,d;
252
 unsigned char x,y,z;
1051 killagreg 253
 unsigned char ptrIn = 3; // start at begin of data block
254
 unsigned char ptrOut = 3;
255
 unsigned char len = AnzahlEmpfangsBytes - 6; // von der Gesamtbytezahl eines Frames gehen 3 Bytes des Headers  ('#',Addr, Cmd) und 3 Bytes des Footers (CRC1, CRC2, '\r') ab.
256
 
1 ingob 257
 while(len)
258
  {
259
   a = RxdBuffer[ptrIn++] - '=';
260
   b = RxdBuffer[ptrIn++] - '=';
261
   c = RxdBuffer[ptrIn++] - '=';
262
   d = RxdBuffer[ptrIn++] - '=';
263
 
264
   x = (a << 2) | (b >> 4);
265
   y = ((b & 0x0f) << 4) | (c >> 2);
266
   z = ((c & 0x03) << 6) | d;
267
 
1051 killagreg 268
   if(len--) RxdBuffer[ptrOut++] = x; else break;
269
   if(len--) RxdBuffer[ptrOut++] = y; else break;
270
   if(len--) RxdBuffer[ptrOut++] = z;   else break;
1 ingob 271
  }
1051 killagreg 272
        pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte
273
        RxDataLen = ptrOut - 3;  // wie viele Bytes wurden dekodiert?
1 ingob 274
 
275
}
276
 
277
// --------------------------------------------------------------------------
278
void BearbeiteRxDaten(void)
279
{
280
 if(!NeuerDatensatzEmpfangen) return;
281
 
1051 killagreg 282
  Decode64(); // dekodiere datenblock im Empfangsbuffer
283
 
1 ingob 284
  switch(RxdBuffer[2])
285
  {
693 hbuss 286
   case 'K':// Kompasswert
1051 killagreg 287
            memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue));
693 hbuss 288
            KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
289
                        break;
499 hbuss 290
   case 'a':// Texte der Analogwerte
1051 killagreg 291
            DebugTextAnforderung = pRxData[0];
292
            if (DebugTextAnforderung > 31) DebugTextAnforderung = 31;
293
            PcZugriff = 255;
499 hbuss 294
                        break;
595 hbuss 295
   case 'b':
1051 killagreg 296
                        memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl));
595 hbuss 297
            ConfirmFrame = ExternControl.Frame;
1051 killagreg 298
            PcZugriff = 255;
595 hbuss 299
            break;
1051 killagreg 300
   case 'd': // Poll the debug data
301
                        DebugDataIntervall = pRxData[0] * 10;
302
            if (DebugDataIntervall) DebugDataAnforderung = 1;
1 ingob 303
            break;
1051 killagreg 304
   case 'l':// x-1 Displayzeilen
305
            MenuePunkt = pRxData[0];
1 ingob 306
                        DebugDisplayAnforderung = 1;
307
                        break;
1051 killagreg 308
   case 'm':// Motortest
309
            memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest));
310
            while(!UebertragungAbgeschlossen);
311
            SendOutData('M', MeineSlaveAdresse, 0);
312
            PcZugriff = 255;
1 ingob 313
                        break;
314
   case 'v': // Version-Anforderung     und Ausbaustufe
315
            GetVersionAnforderung = 1;
1051 killagreg 316
            break;
317
   case 'g'://
1038 hbuss 318
                        GetExternalControl = 1;
1 ingob 319
            break;
1051 killagreg 320
   case 'p': // get PPM Channels
321
                        GetPPMChannelAnforderung = 1;
322
                        break;
323
   case 't':// "Get"-Anforderung für Settings
1 ingob 324
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
1051 killagreg 325
            if(pRxData[0] == 0xFF)
326
                        {
327
                                pRxData[0] = GetActiveParamSetNumber();
328
                        }
329
                        else // request active parameter set
330
                        {
331
                                if(pRxData[0] < 1) pRxData[0] = 1; // limit to 5
332
                                else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5
333
                                // load requested parameter set
334
                                ReadParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
335
                        }
846 hbuss 336
            while(!UebertragungAbgeschlossen);
1051 killagreg 337
            SendOutData('T', MeineSlaveAdresse, 2, &pRxData[0], sizeof(unsigned char), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
1 ingob 338
            break;
1051 killagreg 339
   case 's': // Parametersatz speichern
340
                        if((1 <= pRxData[0]) && (pRxData[0] <= 5)) // check for setting to be in range
341
                        {
342
                                memcpy((unsigned char *) &EE_Parameter.Kanalbelegung[0], (unsigned char *)&pRxData[1], STRUCT_PARAM_LAENGE);
343
                                WriteParameterSet(pRxData[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
344
                                eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1);  // aktiven Datensatz merken
345
                                Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
346
                                Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
347
                                Piep(GetActiveParamSetNumber());
348
                                while(!UebertragungAbgeschlossen);
349
                SendOutData('S', MeineSlaveAdresse, 1, &pRxData[0], sizeof(unsigned char));
350
                        }
351
                        break;
352
 
1 ingob 353
  }
354
// DebugOut.AnzahlZyklen =  Debug_Timer_Intervall;
1051 killagreg 355
        NeuerDatensatzEmpfangen = 0;
356
        pRxData = 0;
357
        RxDataLen = 0;
1 ingob 358
}
359
 
360
//############################################################################
361
//Routine für die Serielle Ausgabe
362
int uart_putchar (char c)
363
//############################################################################
364
{
365
        if (c == '\n')
366
                uart_putchar('\r');
367
        //Warten solange bis Zeichen gesendet wurde
368
        loop_until_bit_is_set(USR, UDRE);
369
        //Ausgabe des Zeichens
370
        UDR = c;
1051 killagreg 371
 
1 ingob 372
        return (0);
373
}
374
 
375
// --------------------------------------------------------------------------
376
void WriteProgramData(unsigned int pos, unsigned char wert)
377
{
378
  //if (ProgramLocation == IN_RAM) Buffer[pos] = wert;
379
  // else eeprom_write_byte(&EE_Buffer[pos], wert);
380
  // Buffer[pos] = wert;
381
}
382
 
383
//############################################################################
384
//INstallation der Seriellen Schnittstelle
385
void UART_Init (void)
386
//############################################################################
387
{
388
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
389
 
390
        UCR=(1 << TXEN) | (1 << RXEN);
391
    // UART Double Speed (U2X)
1051 killagreg 392
        USR   |= (1<<U2X);
1 ingob 393
        // RX-Interrupt Freigabe
1051 killagreg 394
        UCSRB |= (1<<RXCIE);
1 ingob 395
        // TX-Interrupt Freigabe
1051 killagreg 396
        UCSRB |= (1<<TXCIE);
1 ingob 397
 
1051 killagreg 398
        //Teiler wird gesetzt
1 ingob 399
        UBRR=(SYSCLK / (BAUD_RATE * 8L) - 1);
400
        //UBRR = 33;
401
        //öffnet einen Kanal für printf (STDOUT)
402
        //fdevopen (uart_putchar, 0);
403
        //sbi(PORTD,4);
1051 killagreg 404
        Debug_Timer = SetDelay(DebugDataIntervall);
405
        Kompass_Timer = SetDelay(220);
406
        MeineSlaveAdresse = 1; // Flight-Ctrl
407
 
408
        VersionInfo.Major = VERSION_MAJOR;
409
        VersionInfo.Minor = VERSION_MINOR;
410
        VersionInfo.Patch = VERSION_PATCH;
411
        VersionInfo.Compatible  = VERSION_SERIAL_COMPATIBLE;
412
 
413
        pRxData = 0;
414
        RxDataLen = 0;
1 ingob 415
}
416
 
417
//---------------------------------------------------------------------------------------------
1051 killagreg 418
void DatenUebertragung(void)
1 ingob 419
{
420
 if(!UebertragungAbgeschlossen) return;
421
 
1038 hbuss 422
   if(GetExternalControl && UebertragungAbgeschlossen)        // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
1051 killagreg 423
   {
424
      SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl));
1038 hbuss 425
          GetExternalControl = 0;
1 ingob 426
   }
427
 
1051 killagreg 428
    if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen)
693 hbuss 429
         {
846 hbuss 430
                  WinkelOut.Winkel[0] = (int) (IntegralNick / 108);  // etwa in 0,1 Grad
431
                  WinkelOut.Winkel[1] = (int) (IntegralRoll / 108);  // etwa in 0,1 Grad
717 hbuss 432
                  WinkelOut.UserParameter[0] = Parameter_UserParam1;
433
                  WinkelOut.UserParameter[1] = Parameter_UserParam2;
1051 killagreg 434
          SendOutData('k',MeineSlaveAdresse, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut));
855 hbuss 435
          if(WinkelOut.CalcState > 4)  WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt
1051 killagreg 436
          Kompass_Timer = SetDelay(99);
437
         }
438
    if((( DebugDataIntervall && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen)
1 ingob 439
         {
1051 killagreg 440
          SendOutData('D',MeineSlaveAdresse, 1, (unsigned char *) &DebugOut,sizeof(DebugOut));
1 ingob 441
          DebugDataAnforderung = 0;
1051 killagreg 442
          if(DebugDataIntervall) Debug_Timer = SetDelay(DebugDataIntervall);
443
         }
499 hbuss 444
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
445
     {
1051 killagreg 446
      SendOutData('A', MeineSlaveAdresse, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),(unsigned char *) ANALOG_TEXT[DebugTextAnforderung], 16);
499 hbuss 447
      DebugTextAnforderung = 255;
448
         }
1051 killagreg 449
     if(ConfirmFrame && UebertragungAbgeschlossen)   // Datensatz bestätigen
595 hbuss 450
         {
1051 killagreg 451
                SendOutData('B', MeineSlaveAdresse, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame));
452
        ConfirmFrame = 0;
595 hbuss 453
     }
1 ingob 454
     if(DebugDisplayAnforderung && UebertragungAbgeschlossen)
455
         {
456
      Menu();
1051 killagreg 457
      SendOutData('L',MeineSlaveAdresse, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff));
1 ingob 458
          DebugDisplayAnforderung = 0;
1051 killagreg 459
         }
460
    if(GetVersionAnforderung && UebertragungAbgeschlossen)
461
     {
462
      SendOutData('V',MeineSlaveAdresse, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo));
1 ingob 463
          GetVersionAnforderung = 0;
464
     }
1051 killagreg 465
     if(GetPPMChannelAnforderung && UebertragungAbgeschlossen)
466
     {
467
                 SendOutData('P',MeineSlaveAdresse, 1, (unsigned char *) &PPM_in, sizeof(PPM_in));
468
                 GetPPMChannelAnforderung = 0;
469
         }
1 ingob 470
 
471
}
472