Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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