Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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