Space / Mbed 2 deprecated DORAv5

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Librerie
00002 #include "mbed.h"
00003 #include <math.h>
00004 #include <string.h>
00005 #include <stdlib.h>
00006 #include "tlc_tlm_list.h"
00007 
00008 // Macros
00009 #define cmd_num 32
00010 #define buf_max_size 512
00011 #define tlc_to_tx 16
00012 #define tlm_size 16
00013 #define tlm_hsk_size 912
00014 #define tlm_hsk_sd_size 90608
00015 #define identifier_field 5
00016 #define max31865_buf_size 13
00017 #define channel_size 5
00018 #define size 1
00019 
00020 // Serial e periferiche 
00021 Serial spacecraft(USBTX, USBRX);
00022 Serial mima(PC_4, PC_5);
00023 Serial max31865(PA_9, PA_10);
00024 
00025 // Buffer
00026 volatile int sizeBuffer;
00027 int CMD[cmd_num];
00028 char TLM[tlm_size];
00029 char TLM_HSK[tlm_hsk_size];
00030 char TLM_HSK_SD[tlm_hsk_sd_size];
00031 char tlc_rx[buf_max_size];
00032 char caRxPacket[max31865_buf_size];
00033 char tempCH1[channel_size];
00034 char tempCH2[channel_size];
00035 char CH1_to_string[2*channel_size];
00036 char CH2_to_string[2*channel_size];
00037 
00038 // L16 Actuonix - Relay (spare commands)
00039 PwmOut M1 (PA_6);
00040 PwmOut M2 (PA_8);
00041 DigitalOut Relay5V (PA_7);
00042 DigitalOut Relay12V (PB_4);
00043 DigitalOut Relay24V (PB_5);
00044 DigitalOut Relay15_15_5V (PB_10);
00045 float offset = 0.0f;
00046 
00047 // Timer
00048 Timer timer;
00049 float TIMEOUT = 10, tTimeStart, tTimeStop, tTimeRead;
00050 
00051 // Vars definitions
00052 int temperature_flag;
00053 volatile int payload_to_dec;
00054 volatile int tlmode_option_1;
00055 volatile int tlmode_option_2;
00056 volatile int commandId;
00057 volatile int nRxCharCount;
00058 volatile int tlc_rx_length;
00059 volatile int checkZerosIndex, checkOnesIndex;
00060 volatile float CH1, CH2;
00061 volatile int increment_read;
00062 volatile int indexJ, indexT;
00063 
00064 // Functions
00065 void RxTelecommand();
00066 void TxTelecommand();
00067 void RxTelemetry();
00068 void TxTelemetry();
00069 void TxTemperature();
00070 void RxTemperature();
00071 void SpareCommand(int command);
00072 void printMyArray(Telecommand *myArray);
00073 void clearBuffer(char *arr, int arrLen);
00074 void checksum_tlc(Telecommand *toCheck_tlc);
00075 void checksum_tlm(Telemetry *toCheck_tlm);
00076 int BinToDec(int t, int dim, char *a);
00077 
00078 // Main
00079 int main()
00080 {
00081     // Baudrate
00082     spacecraft.baud(115200);
00083     mima.baud(9600);
00084     max31865.baud(115200);
00085 
00086     // Interrupts
00087     spacecraft.attach(&RxTelecommand, Serial::RxIrq);
00088     //max31865.attach(&RxTemperature, Serial::RxIrq);
00089     
00090     while (true)
00091     {
00092         /**********************************************************************
00093                         RICEZIONE DEI DATI DI TEMPERATURA
00094         **********************************************************************/
00095         
00096         if ((commandId == 0) && (payload_to_dec == 32) && (temperature_flag == 1))
00097         {
00098             TxTemperature();
00099             increment_read++;
00100 
00101             if (increment_read == 40) 
00102             {
00103                 temperature_flag = 0;
00104             }
00105         }
00106         
00107         /**********************************************************************
00108                          RICEZIONE DI TELEMETRIE DA MIMA
00109         **********************************************************************/
00110 
00111         // Scrittura del telecomando
00112         cmd00[(size-1)].PAYLOAD = "00000000"; 
00113         if (cmd00[(size-1)].PAYLOAD != '\0')
00114         {
00115             checksum_tlc(&cmd00[(size-1)]);   
00116             printMyArray(&cmd00[(size-1)]);
00117         }                   
00118         
00119         // In arrivo da periferica mima
00120         sizeBuffer = 0; // reset 
00121         clearBuffer(TLM, sizeof(TLM)/sizeof(TLM[0]));
00122         clearBuffer(TLM_HSK, sizeof(TLM_HSK)/sizeof(TLM_HSK[0]));
00123         clearBuffer(TLM_HSK_SD, sizeof(TLM_HSK_SD)/sizeof(TLM_HSK_SD[0]));
00124 
00125         timer.start();
00126         tTimeStart = timer.read(); 
00127         tTimeRead = tTimeStart;
00128         
00129         // Interrupt: entra nella routine quando riceve dalla seriale 
00130         mima.attach(&RxTelemetry, Serial::RxIrq);
00131 
00132         // Diagnostica del pacchetto ricevuto
00133         if (commandId == 5)
00134         {
00135             /* Ricezione di tutte le telemetrie. 57 parole da 16 bit contenenti
00136             tutte le telemetrie di tipo HOUSEKEEPING. */
00137             if (tlmode_option_1 == 0)
00138             {
00139                 while ((sizeBuffer < tlm_hsk_size) && ((tTimeRead - tTimeStart) < TIMEOUT))
00140                 {
00141                     tTimeRead = timer.read();
00142                 }
00143 
00144                 timer.stop();
00145 
00146                 if ((tTimeRead - tTimeStart) >= TIMEOUT)
00147                 {
00148                     sizeBuffer = 0;
00149                     clearBuffer(TLM_HSK, tlm_hsk_size);
00150                 }
00151                 else
00152                 {
00153                     TxTelemetry();
00154                 }
00155             }
00156             /* Ricezione di tutte le telemetrie e dati scientifici
00157             5663 parole da 16 bit così strutturate: 57 HSKP data packets,
00158             5606 di scientific data dell'interferometro su MIMA, */
00159             else if (tlmode_option_2 == 0)
00160             {
00161                 while ((sizeBuffer < tlm_hsk_sd_size) && ((tTimeRead - tTimeStart) < TIMEOUT))
00162                 {
00163                     tTimeRead = timer.read();
00164                 }
00165 
00166                 timer.stop();
00167 
00168                 if ((tTimeRead - tTimeStart) >= TIMEOUT)
00169                 {
00170                     sizeBuffer = 0;
00171                     clearBuffer(TLM_HSK_SD, tlm_hsk_sd_size);
00172                 }
00173                 else
00174                 {
00175                     TxTelemetry();
00176                 }
00177             }
00178         }
00179         /* Ricezione di un dato di telemetria da 16 bit. */
00180         else    
00181         {
00182             while ((sizeBuffer < tlm_size) && ((tTimeRead - tTimeStart) < TIMEOUT))
00183             {
00184                 tTimeRead = timer.read();
00185             }
00186 
00187             timer.stop();
00188 
00189             if ((tTimeRead - tTimeStart) >= TIMEOUT)
00190             {
00191                 sizeBuffer = 0;
00192                 clearBuffer(TLM, tlm_size);
00193             }
00194             else
00195             {
00196                 TxTelemetry();
00197             }
00198         }
00199     }
00200 }
00201 
00202 
00203 /* Gestione delle routine di ricezione e trasmissione del driver */
00204 
00205 // DORA riceve telecomando da Space/craft
00206 void RxTelecommand()
00207 {
00208     while (spacecraft.readable())
00209     {
00210         clearBuffer(tlc_rx, sizeof(tlc_rx) / sizeof(tlc_rx[0]));
00211         spacecraft.gets(tlc_rx, sizeof(tlc_rx));
00212         tlc_rx_length = strlen(tlc_rx) - 2; // rimozione tag di NR
00213 
00214         if (tlc_rx_length == tlc_to_tx)
00215         {
00216             int bit_B0 = 0, bit_B7 = 7, k = 0; 
00217             commandId = 0;  // init
00218             payload_to_dec = 0; // init
00219 
00220             commandId = BinToDec(bit_B0, identifier_field, tlc_rx);
00221             payload_to_dec = BinToDec(bit_B7, (tlc_to_tx - 1), tlc_rx);
00222 
00223             k = commandId;
00224             
00225             if (k == 2 || k == 15 || k == 30)
00226             {
00227                 // Spare
00228                 break;
00229             }
00230 
00231             /* Gestiione della lettura della temperatura */
00232             else if ((k == 0) && (tlc_rx[5] == '0') && (tlc_rx[6] == '0') && (payload_to_dec == 32))
00233             {
00234                 temperature_flag = 1;
00235                 break;
00236             }
00237 
00238             /* Gestione delle perifieriche HW: relay, attuatori, alimentazione */
00239             else if ((k == 0) && (tlc_rx[5] == '0') && (tlc_rx[6] == '0'))
00240             {
00241                 //SpareCommand(payload_to_dec);
00242                 break;
00243             }
00244             else if (k == 5)
00245             {
00246             /* Istruzione che serve a scorrere il Payload e a settare un determinato flag
00247             per consentire a DORA di aprire o meno determinati canali di comunicazione
00248             (buffer da riempire). */
00249             checkZerosIndex = bit_B7;
00250 
00251             while (checkZerosIndex < tlc_to_tx)
00252             {
00253                 if (tlc_rx[checkZerosIndex] == '0')
00254                 {
00255                     tlmode_option_1 = 0;
00256                     checkZerosIndex++;
00257                 }
00258                 else
00259                 {
00260                     tlmode_option_1 = 1;
00261                     break;
00262                 }
00263             }
00264 
00265             checkOnesIndex = bit_B7;
00266             while (checkOnesIndex < tlc_to_tx)
00267             {
00268                 if (tlc_rx[checkOnesIndex] == '1')
00269                 {
00270                     tlmode_option_2 = 0;
00271                     checkOnesIndex++;
00272                 }
00273                 else
00274                 {
00275                     tlmode_option_2 = 1;
00276                     break;
00277                 }
00278             }
00279             }
00280         }
00281         TxTelecommand();
00282     }
00283 }
00284 
00285 // Trasmetti il pacchetto
00286 void TxTelecommand()
00287 {
00288     for (int i = 0; i < tlc_to_tx + 1; i++)
00289     {
00290         mima.putc(tlc_rx[i]);
00291     }
00292 }
00293 
00294 // Riceve dalla seriale mima
00295 void RxTelemetry()
00296 {
00297     char data;
00298     
00299     while((mima.readable()))
00300     {
00301         data = mima.getc(); 
00302         
00303         if (tlmode_option_1 == 0)
00304         {
00305             TLM_HSK[sizeBuffer++] = data;
00306         } 
00307         else if (tlmode_option_2 == 0)
00308         {
00309             TLM_HSK_SD[sizeBuffer++] = data;
00310         }
00311         else 
00312         {
00313             TLM[sizeBuffer++] = data;
00314         }
00315             
00316     }
00317 }
00318 
00319 // Dora trasmette il pacchetto allo space/craft
00320 void TxTelemetry()
00321 {
00322     if (tlmode_option_1 == 0)
00323     {        
00324         for (int i = 0; i < tlm_hsk_size; i++)
00325         {
00326              spacecraft.putc(TLM_HSK[i]);
00327         } 
00328     }
00329     else if (tlmode_option_2 == 0)
00330     {
00331         for (int i = 0; i < tlm_hsk_sd_size; i++)
00332         {
00333             spacecraft.putc(TLM_HSK_SD[i]);
00334         }
00335     }
00336     else 
00337     {
00338         for (int i = 0; i < tlm_size; i++)
00339         {
00340             spacecraft.putc(TLM[i]);
00341         }
00342     }
00343     
00344 }
00345 
00346 /* Routines dedicate alla ricezione di dati della temperatura (Pt100) */
00347 
00348 void RxTemperature()
00349 {
00350     char cReadChar; 
00351     
00352     while((max31865.readable()))
00353     {
00354         cReadChar = max31865.getc();
00355         nRxCharCount++; 
00356         caRxPacket[nRxCharCount] = cReadChar;
00357     }
00358 } 
00359 
00360 void TxTemperature(void)
00361 {
00362     nRxCharCount = 0; // reset dell'indice del buffer
00363 
00364     int y = 0, u = 0; // indici dei buffer temporanei per i due canali
00365 
00366     for (int i = 0; i < max31865_buf_size; i++)
00367     {
00368         if (caRxPacket[i] == 'S')
00369         {
00370             indexJ = i + 1;
00371 
00372             while (indexJ < max31865_buf_size)
00373             {
00374                 if (caRxPacket[indexJ] == ';')
00375                 {
00376                     break;
00377                 }
00378                 else
00379                 {
00380                     tempCH1[y++] = caRxPacket[indexJ];
00381                 }
00382 
00383                 indexJ++;
00384             } // se trova ; esce dall'istruzione e salva il valore di uscita dell'indice
00385 
00386             indexT = indexJ + 1;
00387 
00388             while (indexT < max31865_buf_size)
00389             {
00390                 if (caRxPacket[indexT] == 'P')
00391                 {
00392                     break;
00393                 }
00394                 else
00395                 {
00396                     tempCH2[u++] = caRxPacket[indexT];
00397                 }
00398 
00399                 indexT++;
00400             }
00401             
00402             // trasforma il dato da sequenza di caratteri a numero float utile per analisi
00403 
00404             strcpy(CH1_to_string, tempCH1);
00405             CH1 = atof(CH1_to_string);
00406 
00407             strcpy(CH2_to_string, tempCH2);
00408             CH2 = atof(CH2_to_string);
00409 
00410             spacecraft.printf("> TEMPERATURE - CH1: %4.8f[Celsius], CH2: %4.8f[Celsius]\n\r", CH1, CH2);
00411 
00412             break;
00413         }
00414     }
00415 
00416     wait_ms(150); // pausa per garantire sincronizzazione tra riempimento buffer e svuotamento
00417 }
00418 
00419 /* Comandi spare/not used */
00420 
00421 void SpareCommand(int command)                                                      // Spare command (Relay, L16)
00422 {        
00423     if((command == 128) && (offset < 1.0f)) 
00424     { 
00425         offset += 0.2f; 
00426         M1.write(offset);
00427         M2.write(offset); 
00428         spacecraft.printf("> Duty Cycle %.2f / estensione \n\r", offset);    
00429     } 
00430     else if((command == 64) && (offset > 0.0f)) 
00431     {
00432         offset -= 0.2f; 
00433         M1.write(offset);
00434         M2.write(offset); 
00435         spacecraft.printf("> Duty Cycle %.2f / estensione \n\r", offset);  
00436     }
00437     else if (command == 1) 
00438     {
00439         Relay5V = 0;
00440         spacecraft.printf("\r\nRelay 5V ON\r\n");       
00441     } 
00442     else if (command == 0) 
00443     {
00444         Relay5V = 1;
00445         spacecraft.printf("\r\nRelay 5V OFF\r\n");
00446     } 
00447     else if (command == 3) 
00448     {
00449         Relay12V = 0;
00450         spacecraft.printf("\r\nRelay 12V ON\r\n");           
00451     }  
00452     else if (command == 2) 
00453     {
00454         Relay12V = 1;
00455         spacecraft.printf("\r\nRelay 12V OFF\r\n");
00456     } 
00457     else if (command == 7) 
00458     {
00459         Relay15_15_5V = 0;
00460         spacecraft.printf("\r\nRelay +15V, -15V, -5V ON\r\n");     
00461     } 
00462     else if (command == 6) 
00463     {
00464         Relay15_15_5V = 1;
00465         spacecraft.printf("\r\nRelay +15V, -15V, -5V OFF\r\n");
00466     } 
00467     else if (command == 15) 
00468     {
00469         Relay24V = 0;
00470         spacecraft.printf("\r\nRelay 24V ON\r\n");        
00471     } 
00472     else if (command == 8) 
00473     {
00474         Relay24V = 1;
00475         spacecraft.printf("\r\nRelay 24V OFF\r\n");   
00476     } 
00477     else if (command == 255) 
00478     {
00479         Relay5V = 1;
00480         Relay12V = 1;
00481         Relay15_15_5V = 1; // +15, -15, -5
00482         Relay24V = 1;
00483         offset = 0.0f;
00484             
00485         spacecraft.printf("**** SHUTTING DOWN ALL RELAYS... ****\n\r");
00486         M1.write(offset);
00487         M2.write(offset); 
00488         spacecraft.printf("> Duty Cycle %.2f / extension \n\r", offset);
00489     }
00490     else if ((command == 128) || (command == 64) && (Relay12V == 1))
00491     {
00492         offset = 0.0f;
00493         M1.write(offset);
00494         M2.write(offset); 
00495     }                   
00496 }
00497 
00498 /* Funzionalità di sistema */
00499 
00500 // Funzione che serve a ripulire un buffer di caratteri  
00501 void clearBuffer(char *arr, int arrLen)
00502 {
00503     int nIndex;
00504     
00505     for (nIndex = 0; nIndex < arrLen; nIndex++)
00506     {
00507         arr[nIndex] = '\0';
00508     }
00509 }
00510 
00511 // Inizializzazione del set di identificativi dello Spacecraft
00512 void CMD_REF()
00513 {
00514     int id = 0;
00515     
00516     for (int i = 0; i < cmd_num; i++)
00517     {
00518         CMD[id++] = i;
00519     }
00520 }
00521 
00522 // Trasformazione da binario a decimale per una sequenza di bit in un array
00523 int BinToDec(int t, int dim, char *a)                                               
00524 {
00525     volatile int result = 0;
00526 
00527     while (t < dim)
00528     {
00529         if (a[t] == '1')
00530         {
00531             int raise = dim - 1 - t;
00532             result += pow((float) 2, (float) raise);
00533         }
00534         else
00535         {
00536             result += 0;
00537         }
00538         t++;
00539     }
00540     return result;
00541 }
00542 
00543 // Print
00544 void printMyArray(Telecommand *myArray)
00545 {
00546     for (int i = 0; i < sizeof(myArray)/sizeof(myArray[0]); i++)
00547     {
00548         mima.puts(myArray[i].ID);
00549         mima.puts(myArray[i].TAG);
00550         mima.puts(myArray[i].PAYLOAD);
00551         mima.putc(myArray[i].LSB);
00552   
00553         spacecraft.printf("\n\r");
00554     }
00555 }
00556 
00557 
00558 // Checksum per telecomandi
00559 void checksum_tlc(Telecommand *toCheck_tlc)
00560 {
00561     int ones_tlc = 0;
00562     
00563     for (int i = 0; i < sizeof(toCheck_tlc)/sizeof(toCheck_tlc[0]); i++)
00564     {
00565         // contatore degli "1" nel campo "identifier"
00566         for (int j = 0; i < sizeof(toCheck_tlc[i].ID)/sizeof(toCheck_tlc[i].ID[0]); j++)
00567         {
00568             if (toCheck_tlc[i].ID[j] == '1')
00569             {
00570                 ones_tlc++;
00571             }
00572         }
00573         
00574         // contatore degli "1" nel campo "tag"
00575         for (int k = 0; i < sizeof(toCheck_tlc[i].TAG)/sizeof(toCheck_tlc[i].TAG[0]); k++)
00576         {
00577             if (toCheck_tlc[i].TAG[k] == '1')
00578             {
00579                 ones_tlc++;
00580             }
00581         }
00582 
00583         // contatore degli "1" nel campo "payload" (da assegnare nel file main)
00584         for (int l = 0; i < sizeof(toCheck_tlc[i].TAG)/sizeof(toCheck_tlc[i].TAG[0]); l++)
00585         {
00586             if (toCheck_tlc[i].PAYLOAD[l] == '1')
00587             {
00588                 ones_tlc++;
00589             }
00590         }
00591         
00592         // Scrivi LSB
00593         if (ones_tlc % 2 == 0)
00594         {
00595             toCheck_tlc[i].LSB = '0';  // If the number of ones in[B0÷B14] bits is even
00596         }
00597         else
00598         {
00599             toCheck_tlc[i].LSB = '1';  // If the number of ones in[B0÷B14] bits is odd
00600         }
00601         
00602         
00603     }
00604 }
00605 
00606 // Checksum per telemetrie housekeeping
00607 void checksum_tlm(Telemetry *toCheck_tlm)
00608 {
00609     int ones_tlm = 0;
00610     
00611     for (int i = 0; i < sizeof(toCheck_tlm)/sizeof(toCheck_tlm[0]); i++)
00612     {
00613         // contatore degli "1" nel campo "identifier"
00614         for (int j = 0; i < sizeof(toCheck_tlm[i].ID)/sizeof(toCheck_tlm[i].ID[0]); j++)
00615         {
00616             if (toCheck_tlm[i].ID[j] == '1')
00617             {
00618                 ones_tlm++;
00619             }
00620         }
00621         
00622         // contatore degli "1" nel campo "tag"
00623         for (int k = 0; i < sizeof(toCheck_tlm[i].TAG)/sizeof(toCheck_tlm[i].TAG[0]); k++)
00624         {
00625             if (toCheck_tlm[i].TAG[k] == '1')
00626             {
00627                 ones_tlm++;
00628             }
00629         }
00630 
00631         // contatore degli "1" nel campo "payload" (da assegnare nel file main)
00632         for (int l = 0; i < sizeof(toCheck_tlm[i].TAG)/sizeof(toCheck_tlm[i].TAG[0]); l++)
00633         {
00634             if (toCheck_tlm[i].PAYLOAD[l] == '1')
00635             {
00636                 ones_tlm++;
00637             }
00638         }
00639         
00640         // Scrivi LSB
00641         if (ones_tlm % 2 == 0)
00642         {
00643             toCheck_tlm[i].LSB = '0';  // If the number of ones in[B0÷B14] bits is even
00644         }
00645         else
00646         {
00647             toCheck_tlm[i].LSB = '1';  // If the number of ones in[B0÷B14] bits is odd
00648         }
00649         
00650         
00651     }
00652 }