Subversion Repositories FlightCtrl

Rev

Rev 1002 | 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
 
8
#include "main.h"
9
#include "uart.h"
10
 
11
unsigned char DebugGetAnforderung = 0,DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0;
12
unsigned volatile char SioTmp = 0;
13
unsigned volatile char SendeBuffer[MAX_SENDE_BUFF];
14
unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF];
15
unsigned volatile char NMEABuffer[MAX_EMPFANGS_BUFF];
16
unsigned volatile char NeuerDatensatzEmpfangen = 0;
17
unsigned volatile char NeueKoordinateEmpfangen = 0;
18
unsigned volatile char UebertragungAbgeschlossen = 1;
19
unsigned volatile char CntCrcError = 0;
20
unsigned volatile char AnzahlEmpfangsBytes = 0;
21
unsigned volatile char PC_DebugTimeout = 0;
499 hbuss 22
unsigned char RemotePollDisplayLine = 0;
395 hbuss 23
unsigned char NurKanalAnforderung = 0;
499 hbuss 24
unsigned char DebugTextAnforderung = 255;
1 ingob 25
unsigned char PcZugriff = 100;
26
unsigned char MotorTest[4] = {0,0,0,0};
595 hbuss 27
unsigned char DubWiseKeys[4] = {0,0,0,0};
1 ingob 28
unsigned char MeineSlaveAdresse;
595 hbuss 29
unsigned char ConfirmFrame;
1 ingob 30
struct str_DebugOut    DebugOut;
595 hbuss 31
struct str_ExternControl  ExternControl;
1 ingob 32
struct str_VersionInfo VersionInfo;
693 hbuss 33
struct str_WinkelOut WinkelOut;
1 ingob 34
 
693 hbuss 35
int Debug_Timer,Kompass_Timer;
36
 
499 hbuss 37
const unsigned char ANALOG_TEXT[32][16] =
38
{
39
   //1234567890123456 
40
    "IntegralNick    ", //0
41
    "IntegralRoll    ",
42
    "AccNick         ",
43
    "AccRoll         ",
44
    "GyroGier        ",
45
    "HoehenWert      ", //5
1003 salvo 46
    "Giermischanteil ",
499 hbuss 47
    "Gas             ",
48
    "KompassValue    ",
513 hbuss 49
    "Spannung        ",
1003 salvo 50
    "Mess_integ_gier ", //10
693 hbuss 51
    "Ersatzkompass   ",
499 hbuss 52
    "Motor_Vorne     ",
53
    "Motor_Hinten    ",
54
    "Motor_Links     ",
55
    "Motor_Rechts    ", //15
929 hbuss 56
    "                ",
720 ingob 57
    "                ",
58
    "                ",
59
    "                ",
1003 salvo 60
    "                ", //20
61
    "Nick            ",
62
    "Roll            ",
63
    "debug_0         ",
64
    "debug_1         ",
65
    "debug_2         ", //25
66
    "utm_east        ",
67
    "utm_north       ",
68
    "utm_alt         ",
69
    "GPS_State       ",
720 ingob 70
    "GPS_Nick        ", //30
71
    "GPS_Roll        "
499 hbuss 72
};
73
 
74
 
75
 
1 ingob 76
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
77
//++ Sende-Part der Datenübertragung
78
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
79
SIGNAL(INT_VEC_TX)
80
{
81
 static unsigned int ptr = 0;
82
 unsigned char tmp_tx;
83
 if(!UebertragungAbgeschlossen)  
84
  {
85
   ptr++;                    // die [0] wurde schon gesendet
86
   tmp_tx = SendeBuffer[ptr];  
87
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
88
    {
89
     ptr = 0;
90
     UebertragungAbgeschlossen = 1;
91
    }
92
   UDR = tmp_tx;
93
  }
94
  else ptr = 0;
95
}
96
 
97
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
98
//++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung
99
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
100
SIGNAL(INT_VEC_RX)
101
{
102
 static unsigned int crc;
103
 static unsigned char crc1,crc2,buf_ptr;
104
 static unsigned char UartState = 0;
105
 unsigned char CrcOkay = 0;
106
 
1003 salvo 107
 SioTmp = UDR;
108
 //Salvo 11.9.2007 GPS Daten holen
109
 Get_Ublox_Msg(SioTmp); // Daten vom GPS Modul holen
110
 // Salvo End
111
 
1 ingob 112
 if(buf_ptr >= MAX_EMPFANGS_BUFF)    UartState = 0;
113
 if(SioTmp == '\r' && UartState == 2)
114
  {
115
   UartState = 0;
116
   crc -= RxdBuffer[buf_ptr-2];
117
   crc -= RxdBuffer[buf_ptr-1];
118
   crc %= 4096;
119
   crc1 = '=' + crc / 64;
120
   crc2 = '=' + crc % 64;
121
   CrcOkay = 0;
122
   if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;};
123
   if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
124
    {
125
     NeuerDatensatzEmpfangen = 1;
126
         AnzahlEmpfangsBytes = buf_ptr;
127
     RxdBuffer[buf_ptr] = '\r';
173 holgerb 128
         if(RxdBuffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando
1 ingob 129
        }                                
130
  }
131
  else
132
  switch(UartState)
133
  {
134
   case 0:
135
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
136
                  buf_ptr = 0;
137
                  RxdBuffer[buf_ptr++] = SioTmp;
138
                  crc = SioTmp;
139
          break;
140
   case 1: // Adresse auswerten
141
                  UartState++;
142
                  RxdBuffer[buf_ptr++] = SioTmp;
143
                  crc += SioTmp;
144
                  break;
145
   case 2: //  Eingangsdaten sammeln
146
                  RxdBuffer[buf_ptr] = SioTmp;
147
                  if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++;
148
                  else UartState = 0;
149
                  crc += SioTmp;
150
                  break;
151
   default:
152
          UartState = 0;
153
          break;
154
  }
155
}
156
 
157
 
158
// --------------------------------------------------------------------------
159
void AddCRC(unsigned int wieviele)
160
{
161
 unsigned int tmpCRC = 0,i;
162
 for(i = 0; i < wieviele;i++)
163
  {
164
   tmpCRC += SendeBuffer[i];
165
  }
166
   tmpCRC %= 4096;
167
   SendeBuffer[i++] = '=' + tmpCRC / 64;
168
   SendeBuffer[i++] = '=' + tmpCRC % 64;
169
   SendeBuffer[i++] = '\r';
170
  UebertragungAbgeschlossen = 0;
171
  UDR = SendeBuffer[0];
172
}
173
 
174
 
175
 
176
// --------------------------------------------------------------------------
177
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len)
178
{
179
 unsigned int pt = 0;
180
 unsigned char a,b,c;
181
 unsigned char ptr = 0;
182
 
183
 SendeBuffer[pt++] = '#';               // Startzeichen
184
 SendeBuffer[pt++] = modul;             // Adresse (a=0; b=1,...)
185
 SendeBuffer[pt++] = cmd;                       // Commando
186
 
187
 while(len)
188
  {
189
   if(len) { a = snd[ptr++]; len--;} else a = 0;
190
   if(len) { b = snd[ptr++]; len--;} else b = 0;
191
   if(len) { c = snd[ptr++]; len--;} else c = 0;
192
   SendeBuffer[pt++] = '=' + (a >> 2);
193
   SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
194
   SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
195
   SendeBuffer[pt++] = '=' + ( c & 0x3f);
196
  }
197
 AddCRC(pt);
198
}
199
 
200
 
201
// --------------------------------------------------------------------------
202
void Decode64(unsigned char *ptrOut, unsigned char len, unsigned char ptrIn,unsigned char max)  // Wohin mit den Daten; Wie lang; Wo im RxdBuffer
203
{
204
 unsigned char a,b,c,d;
205
 unsigned char ptr = 0;
206
 unsigned char x,y,z;
207
 while(len)
208
  {
209
   a = RxdBuffer[ptrIn++] - '=';
210
   b = RxdBuffer[ptrIn++] - '=';
211
   c = RxdBuffer[ptrIn++] - '=';
212
   d = RxdBuffer[ptrIn++] - '=';
213
   if(ptrIn > max - 2) break;     // nicht mehr Daten verarbeiten, als empfangen wurden
214
 
215
   x = (a << 2) | (b >> 4);
216
   y = ((b & 0x0f) << 4) | (c >> 2);
217
   z = ((c & 0x03) << 6) | d;
218
 
219
   if(len--) ptrOut[ptr++] = x; else break;
220
   if(len--) ptrOut[ptr++] = y; else break;
221
   if(len--) ptrOut[ptr++] = z; else break;
222
  }
223
 
224
}
225
 
226
// --------------------------------------------------------------------------
227
void BearbeiteRxDaten(void)
228
{
229
 if(!NeuerDatensatzEmpfangen) return;
230
 
693 hbuss 231
 unsigned int tmp_int_arr1[1];
304 ingob 232
// unsigned int tmp_int_arr2[2];
233
// unsigned int tmp_int_arr3[3];
1 ingob 234
 unsigned char tmp_char_arr2[2];
304 ingob 235
// unsigned char tmp_char_arr3[3];
236
// unsigned char tmp_char_arr4[4];
1 ingob 237
 //if(!MotorenEin) 
238
  switch(RxdBuffer[2])
239
  {
693 hbuss 240
   case 'K':// Kompasswert
241
            Decode64((unsigned char *) &tmp_int_arr1[0],sizeof(tmp_int_arr1),3,AnzahlEmpfangsBytes);
242
            KompassValue = tmp_int_arr1[0];
243
            KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
244
                        break;
499 hbuss 245
   case 'a':// Texte der Analogwerte
246
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
247
            DebugTextAnforderung = tmp_char_arr2[0];
693 hbuss 248
            PcZugriff = 255;
499 hbuss 249
                        break;
595 hbuss 250
   case 'b':
251
                        Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes);
252
                        RemoteTasten |= ExternControl.RemoteTasten;
253
            ConfirmFrame = ExternControl.Frame;
918 hbuss 254
            PcZugriff = 255;
595 hbuss 255
            break;
256
   case 'c':
257
                        Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes);
258
                        RemoteTasten |= ExternControl.RemoteTasten;
259
            ConfirmFrame = ExternControl.Frame;
1 ingob 260
            DebugDataAnforderung = 1;
693 hbuss 261
            PcZugriff = 255;
1 ingob 262
            break;
263
   case 'h':// x-1 Displayzeilen
264
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
265
            RemoteTasten |= tmp_char_arr2[0];
395 hbuss 266
                        if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten
1 ingob 267
                        DebugDisplayAnforderung = 1;
268
                        break;
269
   case 't':// Motortest
270
            Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes);
693 hbuss 271
            PcZugriff = 255;
1 ingob 272
                        break;
492 hbuss 273
   case 'k':// Keys von DubWise
274
            Decode64((unsigned char *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,AnzahlEmpfangsBytes);
595 hbuss 275
                        ConfirmFrame = DubWiseKeys[3];
693 hbuss 276
            PcZugriff = 255;
492 hbuss 277
                        break;
1 ingob 278
   case 'v': // Version-Anforderung     und Ausbaustufe
279
            GetVersionAnforderung = 1;
280
            break;                                                               
281
   case 'g':// "Get"-Anforderung für Debug-Daten 
282
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
283
            DebugGetAnforderung = 1;
284
            break;
285
   case 'q':// "Get"-Anforderung für Settings
286
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
287
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
846 hbuss 288
            while(!UebertragungAbgeschlossen);
1 ingob 289
            if(tmp_char_arr2[0] != 0xff)
290
             {
291
                          if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5;
292
                  ReadParameterSet(tmp_char_arr2[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);                   
293
                  SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
294
             }
295
             else
296
                  SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
297
 
298
            break;
299
 
300
   case 'l':
301
   case 'm':
302
   case 'n':
303
   case 'o':
304
   case 'p': // Parametersatz speichern
305
            Decode64((unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes);
306
                        WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
307
            eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1);  // aktiven Datensatz merken
395 hbuss 308
            Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
309
            Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
1 ingob 310
            Piep(GetActiveParamSetNumber());
311
         break;
312
 
313
 
314
  }
315
// DebugOut.AnzahlZyklen =  Debug_Timer_Intervall;
316
 NeuerDatensatzEmpfangen = 0;
317
}
318
 
319
//############################################################################
320
//Routine für die Serielle Ausgabe
321
int uart_putchar (char c)
322
//############################################################################
323
{
324
        if (c == '\n')
325
                uart_putchar('\r');
326
        //Warten solange bis Zeichen gesendet wurde
327
        loop_until_bit_is_set(USR, UDRE);
328
        //Ausgabe des Zeichens
329
        UDR = c;
330
 
331
        return (0);
332
}
333
 
334
// --------------------------------------------------------------------------
335
void WriteProgramData(unsigned int pos, unsigned char wert)
336
{
337
  //if (ProgramLocation == IN_RAM) Buffer[pos] = wert;
338
  // else eeprom_write_byte(&EE_Buffer[pos], wert);
339
  // Buffer[pos] = wert;
340
}
341
 
342
//############################################################################
343
//INstallation der Seriellen Schnittstelle
344
void UART_Init (void)
345
//############################################################################
346
{
347
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
348
 
349
        UCR=(1 << TXEN) | (1 << RXEN);
350
    // UART Double Speed (U2X)
351
        USR   |= (1<<U2X);          
352
        // RX-Interrupt Freigabe
353
        UCSRB |= (1<<RXCIE);          
354
        // TX-Interrupt Freigabe
355
        UCSRB |= (1<<TXCIE);          
356
 
357
        //Teiler wird gesetzt 
358
        UBRR=(SYSCLK / (BAUD_RATE * 8L) - 1);
359
        //UBRR = 33;
360
        //öffnet einen Kanal für printf (STDOUT)
361
        //fdevopen (uart_putchar, 0);
362
        //sbi(PORTD,4);
363
  Debug_Timer = SetDelay(200);  
693 hbuss 364
  Kompass_Timer = SetDelay(220);  
1 ingob 365
}
366
 
367
//---------------------------------------------------------------------------------------------
368
void DatenUebertragung(void)  
369
{
370
 if(!UebertragungAbgeschlossen) return;
371
 
372
   if(DebugGetAnforderung && UebertragungAbgeschlossen)               // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
373
   {
595 hbuss 374
      SendOutData('G',MeineSlaveAdresse,(unsigned char *) &ExternControl,sizeof(ExternControl));
1 ingob 375
          DebugGetAnforderung = 0;
376
   }
377
 
693 hbuss 378
    if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen)        
379
         {
846 hbuss 380
                  WinkelOut.Winkel[0] = (int) (IntegralNick / 108);  // etwa in 0,1 Grad
381
                  WinkelOut.Winkel[1] = (int) (IntegralRoll / 108);  // etwa in 0,1 Grad
717 hbuss 382
                  WinkelOut.UserParameter[0] = Parameter_UserParam1;
383
                  WinkelOut.UserParameter[1] = Parameter_UserParam2;
693 hbuss 384
          SendOutData('w',MeineSlaveAdresse,(unsigned char *) &WinkelOut,sizeof(WinkelOut));
855 hbuss 385
          if(WinkelOut.CalcState > 4)  WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt
693 hbuss 386
          Kompass_Timer = SetDelay(99);  
387
         }
388
 
1 ingob 389
    if((CheckDelay(Debug_Timer) || DebugDataAnforderung) && UebertragungAbgeschlossen)  
390
         {
391
          SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut));
392
          DebugDataAnforderung = 0;
393
          Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL);  
394
         }
499 hbuss 395
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
396
     {
397
      SendOutData('A',DebugTextAnforderung + '0',(unsigned char *) ANALOG_TEXT[DebugTextAnforderung],16);
398
      DebugTextAnforderung = 255;
399
         }
595 hbuss 400
     if(ConfirmFrame && UebertragungAbgeschlossen)   // Datensatz ohne CRC bestätigen 
401
         {
402
      SendeBuffer[0] = '#';
403
      SendeBuffer[1] = ConfirmFrame;
404
      SendeBuffer[2] = '\r';
405
      UebertragungAbgeschlossen = 0;
406
      ConfirmFrame = 0;
407
      UDR = SendeBuffer[0];
408
     }
1 ingob 409
     if(DebugDisplayAnforderung && UebertragungAbgeschlossen)
410
         {
411
      Menu();
412
          DebugDisplayAnforderung = 0;
499 hbuss 413
      if(++RemotePollDisplayLine == 4 || NurKanalAnforderung)
173 holgerb 414
      {
304 ingob 415
       SendOutData('4',0,(unsigned char *)&PPM_in,sizeof(PPM_in));   // DisplayZeile übertragen
499 hbuss 416
       RemotePollDisplayLine = -1;
173 holgerb 417
      }
499 hbuss 418
      else  SendOutData('0' + RemotePollDisplayLine,0,(unsigned char *)&DisplayBuff[20 * RemotePollDisplayLine],20);   // DisplayZeile übertragen
1 ingob 419
         }
420
    if(GetVersionAnforderung && UebertragungAbgeschlossen)
421
     {
422
      SendOutData('V',MeineSlaveAdresse,(unsigned char *) &VersionInfo,sizeof(VersionInfo));
423
          GetVersionAnforderung = 0;
424
     }
425
 
426
}
427