Space / Mbed 2 deprecated DORAv4

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <math.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 
00006 /*
00007 TO-DO: i dati arrivano in DigitalIn e DigitalOut, anziché essere della classe Serial.
00008 Al fine di poter immagazzinare i dati, associo a determinati livelli logici (1/0) dei caratteri,
00009 riempio il buffer e ritrasmetto con i caratteri riconvertiti
00010 
00011 comando per azionamento periferiche: SPARE
00012 
00013 modifica sulla ricezione: devo ricevere solo se un certo comando è stato selezionato
00014 */
00015 
00016 // ************* INFO *******************
00017 
00018 //  ------                  ------------
00019 // | MIMA | ---- DORA ---- | SPACE/CRAFT |
00020 //  ------                  ------------
00021 
00022 // testato con 26LS31 e 26LS32 + Arduino 
00023 
00024 // ************ DEFINE ******************
00025 // definisce dimensione pacchetto inviato da pc
00026 // telecommand: 15 bit + 1 di controllo
00027 #define PACKETSIZE 20           // lunghezza massima della stringa trasmissibile prima di interrupt
00028 #define PACKET 15               // 15 bit, 1 di parità di controllo
00029 #define PACKET_CHECKED 16       // COMMAND IDENTIFIER + TAG + PAYLOAD + PARITY CHECK
00030 #define TELEMETRY_SIZE 16
00031 #define PACKETDIM 13
00032 #define IDENTIFIER 5
00033 #define TAG 2
00034 #define PAYLOAD 8
00035 #define CMD_NUM 32
00036 #define TLM_OPTION_1 912        // 57 * 16
00037 #define TLM_OPTION_2 90608      // 5663 * 16
00038 #define tb 7                    // bit sampling period
00039 #define word 16
00040 #define CHANNEL 2800
00041 #define TLM_NUM 57
00042 
00043 
00044 // ************ CN7+USB ******************
00045 Serial SC(USBTX, USBRX);                // A4 TX, A6 RX - DATA OUT, DATA IN
00046 
00047 // ************** CN10 *******************
00048 Serial MIMA(PC_4, PC_5);
00049 Serial MAX31865(PA_9, PC_0); //(?)
00050 Serial ARDUINO(PB_6, PC_10);
00051 
00052 // ************** TIMER *****************
00053 Timer t;
00054 float TIMEOUT = 10;
00055 float tTimeStart;
00056 float tTimeStop;
00057 float tTimeRead;
00058 
00059 // ******* L16 Actuatonix - Relay ********
00060 PwmOut M1 (PA_6);                       // D12
00061 PwmOut M2 (PA_8);                       // D7
00062 DigitalOut Relay5V (PA_7);              // D11
00063 DigitalOut Relay12V (PB_4);             // D5
00064 DigitalOut Relay24V (PB_5);             // D4
00065 DigitalOut Relay15_15_5V (PB_10);       // D6 | +15, -15, -5
00066 float offset = 0.0f;
00067 
00068 // **************** VARS *****************
00069 volatile int nIndex;                        // indice per clear del buffer
00070 char getPacket[PACKETSIZE];                 // buffer - pacchetto inviato di dimensione max PACKETSIZE
00071 char PacketChecked[PACKET_CHECKED];         // lato Space/Craft
00072 char TLM[TELEMETRY_SIZE];                   // telemetry format
00073 char TLM_HSK[TLM_OPTION_1];                 // lato MIMA: CMD05 -> option 1 (ALL HSKP)
00074 char TLM_HSK_SD[TLM_OPTION_2];              // lato MIMA: CMD05 -> option 2 (HSKP+SD)
00075 
00076 volatile int LEN;                           // numero di bit da ricevere per ricezione corretta 
00077 volatile int commandId;                     // CMDXX per telecomando
00078 volatile int payloadDec;                    // trasformo il payload in decimale per comandi spare
00079 volatile int sizeBuffer;                    // indice del buffer di ricezione
00080 volatile unsigned char data;                // in arrivo da MIMA per telemetria
00081 volatile int indexTemp;                     // indice del buffer di caratteri in arrivo da arduino/max31865
00082 volatile char ch;
00083 
00084 int base = 2;                               // base per l'esponenziale (conversione binary to decimal)
00085 int CMD[CMD_NUM];                           // CMD in decimale
00086 int TLM_ID[TLM_NUM];                        // TLM in decimale
00087 
00088 volatile int tlmode_option_1;               // TLMODE: 57 words
00089 volatile int tlmode_option_2;               // TLMODE: 5663 words (57 + 5606 scientific data word)
00090 volatile int interferogram_option_1;        // TLMODE: 57 words
00091 volatile int interferogram_option_2;        // TLMODE: 5663 words (57 + 5606 scientific data word)
00092 
00093 volatile int ch1_errorDetected;
00094 int ch1_intChecksum[word];                  
00095 char ch1_checksumWord[word];                // checksum sul pacchetto elaborato
00096 char ch1_checksumReceived[word];            // checksum ricevuto
00097 
00098 char CH1_to_string[20];                     // *temperatura canale 1
00099 char CH2_to_string[20];                     // *temperatura canale 2
00100 char tempCH1[10];                           // temperatura canale 1
00101 char tempCH2[10];                           // temperatura canale 2
00102 volatile int indexJ = 0;                    // indice riempimento canale 1
00103 volatile int indexT = 0;                    // indice riempimento canale 2
00104 volatile float CH1, CH2;                    // valore in float (da stringa)
00105 volatile int nRxCharCount;                  // indice riempimento buffer from:MAX31865
00106 char caRxPacket[PACKETDIM];                 // buffer from:MAX31865
00107 volatile int temperature_flag;              // flag per print su richiesta
00108 
00109 volatile int ch2_errorDetected;             // flag: errore di trasmissione
00110 int ch2_intChecksum[word];                  //     
00111 char ch2_checksumWord[word];                // checksum sul pacchetto elaborato
00112 char ch2_checksumReceived[word];            // checksum ricevuto
00113 
00114 // ************** SETUP *******************
00115 void CMD_REF();                             // crea la lista dei comandi
00116 void TLM_ID_REF();                          // crea la lista delle telemetrie
00117 void clearBuffer(char *arr, int arrLen);    // ripulisce il buffer di ricezione
00118 void clearIntBuffer(int *y, int s);
00119 void Checksum(char *a);                     // effettua checksum sui dati del CH1
00120 void RxTelecommand();                       // riceve da Spacecraft
00121 void TxTelecommand();                       // trasmette a MIMA
00122 void RxTelemetry();                         // riceve da MIMA
00123 void TxTelemetry();                         // trasmette a Spacecraft
00124 void SpareCommand(int command);             // controllo L16, Relay, periferiche
00125 void GetTemperature();
00126 void ReadTemperature();
00127 const char* MIMA_OPERATING_MODE(char *s);
00128 int  BinToDec(int t, int dim, char *a);
00129 
00130 // *********************************************************************
00131 
00132 
00133 int main()
00134 {
00135     // Setup per trasmissione seriale
00136     SC.baud(115200);
00137     MAX31865.baud(115200);
00138     MIMA.baud(9600);
00139                 
00140     SC.printf("******* Test DORA Communication *******\r\n");
00141     
00142     // Flag per ricezione
00143     tlmode_option_1 = 1;
00144     tlmode_option_2 = 1;
00145     
00146     // Temperatura
00147     nRxCharCount = 0;
00148     clearBuffer(caRxPacket,PACKETDIM);  
00149     
00150     // Gestione interrupt seriale
00151     SC.attach(&RxTelecommand, Serial::RxIrq); // arriva telecomando da PC
00152     MAX31865.attach(&GetTemperature, Serial::RxIrq); // arriva dato da MAX31865 via Arduino
00153     
00154     // Ciclo principale
00155     while (true)
00156     {
00157         /* *********************************************************************
00158                          RICEZIONE TEMPERATURA DA MAX31865
00159         ********************************************************************* */   
00160         
00161         if ((commandId == 0) && (payloadDec == 32) && (temperature_flag == 1))
00162         {
00163             ReadTemperature();
00164             temperature_flag = 0;
00165         }
00166         
00167                 
00168         /* *********************************************************************
00169                          RICEZIONE DI TELEMETRIE DA MIMA
00170         ********************************************************************* */
00171 
00172         sizeBuffer = 0; // reset 
00173         clearBuffer(TLM, sizeof(TLM)/sizeof(TLM[0]));
00174         clearBuffer(TLM_HSK, sizeof(TLM_HSK)/sizeof(TLM_HSK[0]));
00175         clearBuffer(TLM_HSK_SD, sizeof(TLM_HSK_SD)/sizeof(TLM_HSK_SD[0]));
00176         
00177         clearIntBuffer(ch1_intChecksum, sizeof(ch1_intChecksum)/sizeof(ch1_intChecksum[0]));
00178         clearIntBuffer(ch2_intChecksum, sizeof(ch2_intChecksum)/sizeof(ch2_intChecksum[0]));
00179         clearBuffer(ch1_checksumWord, sizeof(ch1_checksumWord)/sizeof(ch1_checksumWord[0]));
00180         clearBuffer(ch2_checksumWord, sizeof(ch2_checksumWord)/sizeof(ch2_checksumWord[0]));
00181 
00182         t.start();
00183         tTimeStart = t.read();  // in secondi 
00184         tTimeRead = tTimeStart;
00185         
00186         MIMA.attach(&RxTelemetry, Serial::RxIrq);   // entra in questa routine quando riceve un carattere dalla seriale del SERIAL
00187         
00188         if (commandId == 5) // TLMODE
00189         {
00190             if (tlmode_option_1 == 0)
00191             {
00192                 while (sizeBuffer < TLM_OPTION_1 && ((tTimeRead - tTimeStart) < TIMEOUT))
00193                 {
00194                     tTimeRead = t.read();
00195                 }
00196 
00197                 t.stop();
00198 
00199                 if ((tTimeRead - tTimeStart) >= TIMEOUT)
00200                 {
00201                     sizeBuffer = 0;
00202                     clearBuffer(TLM_HSK, TLM_OPTION_1);
00203                     //SC.printf("Timestamp: %f\n\r", tTimeRead);
00204                 }
00205                 else
00206                 {
00207                     TxTelemetry();
00208                 }
00209             }
00210             else if (tlmode_option_2 == 0)
00211             {
00212                 while (sizeBuffer < TLM_OPTION_2 && ((tTimeRead - tTimeStart) < TIMEOUT))
00213                 {
00214                     tTimeRead = t.read();
00215                 }
00216 
00217                 t.stop();
00218 
00219                 if ((tTimeRead - tTimeStart) >= TIMEOUT)
00220                 {
00221                     sizeBuffer = 0;
00222                     clearBuffer(TLM_HSK_SD, TLM_OPTION_1);
00223                     //SC.printf("Timestamp: %f\n\r", tTimeRead);
00224                 }
00225                 else
00226                 {
00227                     Checksum(TLM_HSK_SD);
00228                     if (ch1_errorDetected == 0 && ch2_errorDetected == 0) // no checksum error flags
00229                     {
00230                         TxTelemetry();
00231                     }
00232                 }
00233             }
00234         }
00235     }
00236 }
00237 
00238 
00239 // *********************************************************************
00240 
00241 
00242 // ************** FUNCTIONS *******************
00243 // init CMD
00244 void CMD_REF()
00245 {
00246     for (int i = 0; i < CMD_NUM; i++)
00247     {
00248         CMD[i] = i;
00249     }
00250 }
00251 
00252 // init TLM
00253 void TLM_ID_REF()
00254 {
00255     for (int i = 0; i < TLM_NUM; i++)
00256     {
00257         TLM_ID[i] = i;
00258     }
00259 }
00260 
00261 // clear
00262 void clearBuffer(char *arr, int arrLen)
00263 {
00264     for (nIndex = 0; nIndex < arrLen; nIndex++)
00265     {
00266         arr[nIndex] = '\0';
00267     }
00268 }
00269 
00270 // clear 
00271 void clearIntBuffer(int *y, int s)
00272 {
00273     for (nIndex = 0; nIndex < s; nIndex++)
00274     {
00275         y[nIndex] = '\0';
00276     }
00277 }
00278 
00279 // DORA riceve telecomando da SC
00280 void RxTelecommand()
00281 {
00282     CMD_REF();
00283 
00284     while (SC.readable()) 
00285     {
00286         clearBuffer(getPacket, sizeof(getPacket)/sizeof(getPacket[0])); 
00287         SC.gets(getPacket, sizeof(getPacket));  
00288         LEN = strlen(getPacket) - 2;    // calcola la sua lunghezza ignorando i tag \r\n
00289         ch = getPacket[0];
00290 
00291         if (LEN != PACKET)
00292         {
00293             SC.printf("> Bit number: %i is not equal to 15  // no tele-command\n\r", LEN);  // diagnostica  
00294         }
00295         else
00296         {
00297             for (int n = 0; n < LEN; n++)
00298             {
00299                 PacketChecked[n] = getPacket[n];
00300             }
00301 
00302             // *** CONTROLLO DEI BIT - LSB: EVEN/ODD ***
00303             int ones = 0;
00304             int check = 0;
00305             while (check < PACKET)
00306             {
00307                 if (getPacket[check] == '1')
00308                 {
00309                     ones++;
00310                 }
00311 
00312                 check++;
00313             }
00314 
00315             char newItem;
00316 
00317             if (ones % 2 == 0)
00318             {
00319                 newItem = '0';  // If the number of ones in[B0÷B14] bits is even (making the total number of ones even)
00320             }
00321             else
00322             {
00323                 newItem = '1';  // If the number of ones in[B0÷B14] bits is odd (making the total number of ones even)
00324             }
00325 
00326             int nPacket = PACKET + 1;
00327             // shift elements 
00328             for (int i = nPacket - 1; i >= PACKET_CHECKED; i--)
00329             {
00330                 PacketChecked[i] = PacketChecked[i - 1];
00331             }
00332 
00333             // insert LSB
00334             PacketChecked[PACKET_CHECKED - 1] = newItem;
00335 
00336             SC.printf("> Send: ");
00337             for (int z = 0; z < PACKET_CHECKED; z++)
00338             {
00339                 SC.printf("%c", PacketChecked[z]);
00340             }
00341 
00342             SC.printf("\n\r");
00343 
00344             // ************DIAGNOSTICA ************
00345             // un telecommand è formato da 16 bit. i primi 5 sono identificativi,
00346             // poi ho due bit che mi specificano se i dati in arrivo sono una o più parole,
00347             // dopodiché ho un payload da 8 bit e per concludere ho LSB (parity bit)
00348             // MSB IIII TT PPPPPPPP PC
00349  
00350             /* ** COMMAND IDENTIFIER ** */
00351             int CMDIndex = 0;   // puntatore dell'identificativo
00352             commandId = 0;
00353 
00354             int B7 = PacketChecked[7];
00355             int B9 = PacketChecked[9];
00356             int B11 = PacketChecked[11];
00357 
00358             // trasformo l'identificatore in numero decimale intero
00359             commandId = BinToDec(CMDIndex, IDENTIFIER, PacketChecked);
00360 
00361             // scorro la lista di comandi disponibli e verifico se il comando trasmesso rientra
00362             // nella lista, altrimenti finisco in uno dei 3 SPARE
00363             int k = 0;
00364             int isElementPresent = 0;
00365             while (k < CMD_NUM)
00366             {
00367                 if (commandId == CMD[k])
00368                 {
00369                     isElementPresent = 1;
00370                     SC.printf("> Telcommand sent belgons to MIMA command list: CMD %i\n\r", k);
00371 
00372                     if (k == 2 || k == 15 || k == 30) // CMD02, CMD15, CMD30
00373                     {
00374                         SC.printf("> CMD %i : SPARE\n\r", k);
00375                     }
00376                     else if (k == 0 && PacketChecked[5] == '0' && PacketChecked[6] == '0') // CMD0 - spare
00377                     {
00378                         int counter = 7; // parto dal settimo bit
00379                         payloadDec = 0;  // inizializzazione del payload in formato digitale
00380 
00381                         payloadDec = BinToDec(counter, PACKET, PacketChecked);        
00382 
00383                         SpareCommand(payloadDec);
00384                         break;
00385                     }
00386                     else if (k == 1) // CMD01
00387                     {
00388                         SC.printf("> CMD %i, CODE: MIMA MODE\n\r", k);
00389                         
00390                         SC.printf("%s\n\r", MIMA_OPERATING_MODE(PacketChecked));
00391                         
00392                     }
00393                     else if (k == 3)
00394                     {
00395                         SC.printf("> CMD %i, CODE: LDth_On_Off - On/Off of Laser Diode thermal control loop\n\r", k);
00396                     }
00397                     else if (k == 4)
00398                     {
00399                         SC.printf("> CMD %i, CODE: IRth_On_Off - On/Off of IR sensor thermal control loop\n\r", k);
00400                     }
00401                     else if (k == 5)
00402                     {
00403                         int checkZerosIndex = 7;
00404                         while (checkZerosIndex < PACKET_CHECKED)
00405                         {
00406                             if (PacketChecked[checkZerosIndex] == '0')
00407                             {
00408                                 tlmode_option_1 = 0;
00409                                 checkZerosIndex++;
00410                             }
00411                             else
00412                             {
00413                                 tlmode_option_1 = 1;
00414                                 break;
00415                             }
00416                         }
00417                         
00418                         int checkOnesIndex = 7;
00419                         while (checkOnesIndex < PACKET_CHECKED)
00420                         {
00421                             if (PacketChecked[checkOnesIndex] == '1')
00422                             {
00423                                 tlmode_option_2 = 0;
00424                                 checkOnesIndex++;
00425                             }
00426                             else
00427                             {
00428                                 tlmode_option_2 = 1;
00429                                 break;
00430                             }
00431                         }
00432                     }
00433                     else if (k == 6)
00434                     {
00435                         int checkZerosIndex = 7;
00436                         while (checkZerosIndex < PACKET_CHECKED)
00437                         {
00438                             if (PacketChecked[checkZerosIndex] == '0')
00439                             {
00440                                 interferogram_option_1 = 0;
00441                                 checkZerosIndex++;
00442                             }
00443                             else
00444                             {
00445                                 interferogram_option_1 = 1;
00446                                 break;
00447                             }
00448                         }
00449                         
00450                         int checkOnesIndex = 7;
00451                         while (checkOnesIndex < PACKET_CHECKED)
00452                         {
00453                             if (PacketChecked[checkOnesIndex] == '1')
00454                             {
00455                                 interferogram_option_2 = 0;
00456                                 checkOnesIndex++;
00457                             }
00458                             else
00459                             {
00460                                 interferogram_option_2 = 1;
00461                                 break;
00462                             }
00463                         } 
00464                         
00465                         if (interferogram_option_1 == 0)
00466                         {
00467                             SC.printf("> CMD %i, START interferogram acquisition\n\r", k);
00468                         }
00469                         else if (interferogram_option_2 == 0)
00470                         {
00471                             SC.printf("> CMD %i, STOP interferogram acquisition\n\r", k);
00472                         }
00473                     }
00474                     else if (k == 7) // CMD07
00475                     {
00476                         // IR sensor thermal loop
00477                         if (B7 == '1')
00478                         {
00479                             SC.printf("> IR sensor thermal loop...\n\r");
00480                             SC.printf("IRT1 telemetry: feedback signal in the thermal control loop (default). IRT2 only for monitoring\n\r");
00481                         }
00482                         else
00483                         {
00484                             SC.printf("> IR sensor thermal loop...\n\r");
00485                             SC.printf("IRT2 telemetry: feedback signal in the thermal control. IRT1 only for monitoring\n\r");
00486                         }
00487 
00488                         // Laser Diode thermal loop
00489                         if (B9 == '1')
00490                         {
00491                             SC.printf("> Laser Diode thermal loop...\n\r");
00492                             SC.printf("LDT1 telemetry: feedback signal in the thermal control loop (default). LDT2 only for monitoring\n\r");
00493                         }
00494                         else
00495                         {
00496                             SC.printf("> Laser Diode thermal loop...\n\r");
00497                             SC.printf("LDT2 telemetry: feedback signal in the thermal control. LDT1 only for monitoring\n\r");
00498 
00499                         }
00500 
00501                         // BlackBody thermal loop
00502                         if (B11 == '1')
00503                         {
00504                             SC.printf("> BlackBody thermal loop...\n\r");
00505                             SC.printf("BBT1 telemetry: feedback signal in the thermal control loop (default). BBT2 only for monitoring\n\r");
00506                         }
00507                         else
00508                         {
00509                             SC.printf("> BlackBody thermal loop...\n\r");
00510                             SC.printf("BBT2 telemetry: feedback signal in the thermal control. BBT1 only for monitoring\n\r");
00511                         }
00512                     }
00513                     
00514                     else if ((k == 0) && (payloadDec == 32))
00515                     {
00516                         temperature_flag = 1;
00517                     }
00518 
00519                 }
00520                 
00521                 k++;
00522             }
00523 
00524             if (isElementPresent == '0')
00525             {
00526                 SC.printf("Telecommand sent doesn't exist\n\r");
00527             }
00528 
00529             // ***TAG ***
00530             int B5 = PacketChecked[5];
00531             int B6 = PacketChecked[6];
00532 
00533             if (B5 == '0' && B6 == '0')
00534             {
00535                 // Single word telemetry
00536                 SC.printf("Single word telemetry\n\r");
00537             }
00538             else if (B5 == '0' && B6 == '1')
00539             {
00540                 // Multiple words telemetry (1st word)
00541                 SC.printf("Multiple words telemetry (1st word)\n\r");
00542             }
00543             else if (B5 == '1' && B6 == '0')
00544             {
00545                 // Multiple words telemetry (2nd word)
00546                 SC.printf("Multiple words telemetry (2nd word)\n\r");
00547             }
00548             else
00549             {
00550                 // Not used
00551                 SC.printf("NOT USED\n\r");
00552             }
00553 
00554             // diagnostica: payload
00555             SC.printf("> Payload: ");
00556             for (int IndexPayload = 7; IndexPayload < PACKET; IndexPayload++)
00557             {
00558                 SC.printf("%c", PacketChecked[IndexPayload]);
00559             }
00560 
00561             SC.printf("\n\r");  // per leggibilità
00562 
00563             //***********FINE DIAGNOSTICA PACCHETTO***********
00564 
00565         }
00566         
00567         TxTelecommand();
00568     }
00569 }
00570 
00571 
00572 /* ***************************************************************** */
00573 
00574 
00575 // DORA trasmette telecomando a MIMA
00576 void TxTelecommand()                                                                // Tx Telecommand
00577 {
00578     for (int i = 0; i < PACKET_CHECKED + 1; i++)
00579     {
00580         MIMA.putc(PacketChecked[i]);
00581     }
00582 }
00583 
00584 
00585 /* ***************************************************************** */
00586 
00587 
00588 // DORA riceve telemetria da MIMA
00589 void RxTelemetry(void)                                                              // Rx Telemetry
00590 {
00591     while((MIMA.readable()))
00592     {
00593         data = MIMA.getc(); // prendi carattere in arrivo
00594         
00595         if (tlmode_option_1 == 0)
00596         {
00597             TLM_HSK[sizeBuffer++] = data;
00598         } 
00599         else if (tlmode_option_2 == 0)
00600         {
00601             TLM_HSK_SD[sizeBuffer++] = data;
00602         }
00603         else 
00604         {
00605             TLM[sizeBuffer++] = data;
00606         }
00607             
00608     }
00609 }
00610 
00611 
00612 /* ***************************************************************** */
00613 
00614 
00615 // DORA trasmette dati verso Space/craft
00616 void TxTelemetry()                                                                  // Tx Telemetry
00617 {
00618     if (tlmode_option_1 == 0)
00619     {
00620         SC.printf("\n\r> Receiving 57 words (all HSK) from MIMA... \n\r");
00621         
00622         for (int i = 0; i < TLM_OPTION_1; i++)
00623         {
00624              SC.putc(TLM_HSK[i]);
00625              ARDUINO.putc(TLM_HSK[i]);
00626         } 
00627         
00628         // parsing del pacchetto da 16 bit * 57 parole
00629         SC.printf("\n\r");
00630     }
00631     else if (tlmode_option_2 == 0)
00632     {
00633         SC.printf("\n\r> Receiving 5663 words (HSK + Scientific Data) from MIMA... \n\r");
00634         
00635         for (int i = 0; i < TLM_OPTION_2; i++)
00636         {
00637            
00638             SC.putc(TLM_HSK_SD[i]);
00639             ARDUINO.putc(TLM_HSK_SD[i]);
00640         }
00641         
00642         // parsing del pacchetto da 16 bit * 57 parole
00643         SC.printf("\n\r");
00644     }
00645     else 
00646     {
00647         SC.printf("\n\r> Receiving Telemetry from MIMA... \n\r");
00648         
00649         for (int i = 0; i < TLM_OPTION_2; i++)
00650         {
00651             SC.putc(TLM[i]);
00652             ARDUINO.putc(TLM[i]);
00653         }
00654         
00655         // parsing del pacchetto da 16 bit
00656         // TLM_parsing(TLM);
00657         SC.printf("\n\r");
00658     }
00659     
00660 }
00661 
00662 
00663 /* ***************************************************************** */
00664 
00665 
00666 void TLM_parsing(char *tlm_buf) 
00667 {
00668     // identificatore
00669     int tlm_identifier = BinToDec(0, IDENTIFIER, tlm_buf); 
00670     volatile int single_word = 0, first_word = 0, second_word = 0;
00671     
00672     if (tlm_buf[5] == '0' && tlm_buf[6] == '0')
00673     {
00674         // single word
00675         single_word = 1;
00676     }
00677     else if (tlm_buf[5] == '0' && tlm_buf[6] == '1')
00678     {
00679         // 1st word
00680         first_word = 1;
00681     }
00682     else if (tlm_buf[5] == '1' && tlm_buf[6] == '0')
00683     {
00684         // 2nd word
00685         second_word = 1;
00686     }
00687     else if (tlm_buf[5] == '1' && tlm_buf[6] == '1')
00688     {
00689         // not used
00690     }
00691     
00692     if (commandId == 1)
00693     {
00694         if ((tlm_identifier == 25) && (first_word == 1))
00695         {
00696             SC.printf("%s\n\r", MIMA_OPERATING_MODE(tlm_buf));
00697         }
00698     }
00699 }
00700 
00701 
00702 /* ***************************************************************** */
00703 
00704 // integrità dei dati ricevuti da MIMA: Channel 1 - Channel 2
00705 void Checksum(char *a)                                                              // Checksum
00706 {
00707     volatile int temp, var, ai_int, aj_int, p = 0;
00708   
00709     for (int i = word; i < 2*word; i++) 
00710     {
00711         volatile int ch1_start = i+word;
00712         
00713         for (int j = ch1_start; j < CHANNEL*word; j+=word)
00714         {
00715             if (j == ch1_start) 
00716             {
00717                 ai_int = a[i] - '0';    // '1'/'0' char -> int 1/0
00718                 aj_int = a[j] - '0';    // '1'/'0' char -> int 1/0
00719                 temp = ai_int ^ aj_int; // XOR
00720             } else
00721             {
00722                 aj_int = a[j] - '0';
00723                 temp = temp ^ aj_int;   
00724             }
00725 
00726         }
00727         ch1_intChecksum[p++] = temp; // vettore del checksum in int
00728     }
00729     
00730     for (int i = 0; i < word; i++) // trasformo in char
00731     {
00732         if (ch1_intChecksum[i] == 0)
00733         {
00734             ch1_checksumWord[i++] = '0';
00735         } 
00736         else if (ch1_intChecksum[i] == 1)
00737         {
00738             ch1_checksumWord[i++] = '1';
00739         }
00740     }
00741     
00742     // confronto tra checksum ricevuto e checksum elaborato
00743     volatile int t = 0;
00744     for (int i = (CHANNEL+1)*word; i < (CHANNEL+2)*word; i++) 
00745     {
00746         ch1_checksumReceived[t++] = TLM_HSK_SD[i];
00747     }
00748     
00749     ch1_errorDetected = 0;
00750     
00751     for (int i = 0; i < sizeof(ch1_checksumWord)/sizeof(ch1_checksumWord[0]); i++) 
00752     {
00753         if (ch1_checksumWord[i] != ch1_checksumReceived[i])
00754         {
00755             ch1_errorDetected = 1;
00756             SC.printf("\n\r> CH1 Channel Checksum ERROR detected: BIT n. %i\n\r", (i+1));
00757             break;
00758         }         
00759     }  
00760     
00761     
00762     /***** CHANNEL 2 *****/
00763     
00764     p = 0;
00765     int ch2_limit = (2*CHANNEL+4);
00766     for (int i = (CHANNEL+4)*word; i < ch2_limit*word; i++) 
00767     {
00768         volatile int ch2_start = i+word;
00769         
00770         for (int j = ch2_start; j < (ch2_limit+CHANNEL)*word; j+=word)
00771         {
00772             if (j == ch2_start) 
00773             {
00774                 ai_int = a[i] - '0';    // '1'/'0' char -> int 1/0
00775                 aj_int = a[j] - '0';    // '1'/'0' char -> int 1/0
00776                 temp = ai_int ^ aj_int; // XOR
00777             } else
00778             {
00779                 aj_int = a[j] - '0';
00780                 temp = temp ^ aj_int;   
00781             }
00782 
00783         }
00784         ch2_intChecksum[p++] = temp; // vettore del checksum in int
00785     }
00786     
00787     for (int i = 0; i < word; i++) // trasformo in char
00788     {
00789         if (ch2_intChecksum[i] == 0)
00790         {
00791             ch2_checksumWord[i++] = '0';
00792         } 
00793         else if (ch1_intChecksum[i] == 1)
00794         {
00795             ch2_checksumWord[i++] = '1';
00796         }
00797     }
00798     
00799     // confronto tra checksum ricevuto e checksum elaborato
00800     t = 0;
00801     for (int i = (CHANNEL+4)*word; i < (2*CHANNEL+4)*word; i++) 
00802     {
00803         ch2_checksumReceived[t++] = TLM_HSK_SD[i];
00804     }
00805     
00806     ch2_errorDetected = 0; // flag
00807     
00808     for (int i = 0; i < sizeof(ch2_checksumWord)/sizeof(ch2_checksumWord[0]); i++) 
00809     {
00810         if (ch2_checksumWord[i] != ch2_checksumReceived[i])
00811         {
00812             ch2_errorDetected = 1;
00813             SC.printf("\n\r> CH2 Channel Checksum ERROR detected: BIT n. %i\n\r", (i+1));
00814             break;
00815         }         
00816     }     
00817 }
00818 
00819 
00820 /* ***************************************************************** */
00821 
00822 // controllo dell'attuatore e dei relay
00823 void SpareCommand(int command)                                                      // Spare command (Relay, L16)
00824 {        
00825     if((command == 128) && (offset < 1.0f)) 
00826     { 
00827         offset += 0.2f; 
00828         M1.write(offset);
00829         M2.write(offset); 
00830         SC.printf("> Duty Cycle %.2f / estensione \n\r", offset);    
00831     } 
00832     else if((command == 64) && (offset > 0.0f)) 
00833     {
00834         offset -= 0.2f; 
00835         M1.write(offset);
00836         M2.write(offset); 
00837         SC.printf("> Duty Cycle %.2f / estensione \n\r", offset);  
00838     }
00839     else if (command == 1) 
00840     {
00841         Relay5V = 0;
00842         SC.printf("\r\nRelay 5V ON\r\n");       
00843     } 
00844     else if (command == 0) 
00845     {
00846         Relay5V = 1;
00847         SC.printf("\r\nRelay 5V OFF\r\n");
00848     } 
00849     else if (command == 3) 
00850     {
00851         Relay12V = 0;
00852         SC.printf("\r\nRelay 12V ON\r\n");           
00853     }  
00854     else if (command == 2) 
00855     {
00856         Relay12V = 1;
00857         SC.printf("\r\nRelay 12V OFF\r\n");
00858     } 
00859     else if (command == 7) 
00860     {
00861         Relay15_15_5V = 0;
00862         SC.printf("\r\nRelay +15V, -15V, -5V ON\r\n");     
00863     } 
00864     else if (command == 6) 
00865     {
00866         Relay15_15_5V = 1;
00867         SC.printf("\r\nRelay +15V, -15V, -5V OFF\r\n");
00868     } 
00869     else if (command == 15) 
00870     {
00871         Relay24V = 0;
00872         SC.printf("\r\nRelay 24V ON\r\n");        
00873     } 
00874     else if (command == 8) 
00875     {
00876         Relay24V = 1;
00877         SC.printf("\r\nRelay 24V OFF\r\n");   
00878     } 
00879     else if (command == 255) 
00880     {
00881         Relay5V = 1;
00882         Relay12V = 1;
00883         Relay15_15_5V = 1; // +15, -15, -5
00884         Relay24V = 1;
00885         offset = 0.0f;
00886             
00887         SC.printf("**** SHUTTING DOWN ALL RELAYS... ****\n\r");
00888         M1.write(offset);
00889         M2.write(offset); 
00890         SC.printf("> Duty Cycle %.2f / extension \n\r", offset);
00891     }
00892     else if ((command == 128) || (command == 64) && (Relay12V == 1))
00893     {
00894         offset = 0.0f;
00895         M1.write(offset);
00896         M2.write(offset); 
00897     }                   
00898 }
00899 
00900 
00901 /* ***************************************************************** */
00902 
00903 // (t = indice da cui partire, dim = dimensione array, *a = array in input)
00904 int BinToDec(int t, int dim, char *a)                                               // Binary to decimal conversion
00905 {
00906     volatile int result = 0;
00907 
00908     while (t < dim)
00909     {
00910         if (a[t] == '1')
00911         {
00912             int raise = dim - 1 - t;
00913             result += pow((float) base, (float) raise);
00914         }
00915         else
00916         {
00917             result += 0;
00918         }
00919         t++;
00920     }
00921     return result;
00922 }
00923 
00924 
00925 /* ***************************************************************** */
00926 
00927 
00928 // TEMPERATURA
00929 void GetTemperature(void)                                                           // Get temperature from MAX31865-Arduino
00930 {
00931     char cReadChar; 
00932     
00933     while((MAX31865.readable()))
00934     {
00935         cReadChar = MAX31865.getc();
00936         nRxCharCount++; 
00937         caRxPacket[nRxCharCount] = cReadChar;
00938     }
00939 }
00940 
00941 
00942 /* ***************************************************************** */
00943 
00944 
00945 void ReadTemperature(void)                                                          // Read temperature float value 
00946 {
00947     nRxCharCount = 0; // indice
00948 
00949     int y = 0, u = 0; // indici
00950 
00951     for (int i = 0; i < PACKETDIM; i++)
00952     {
00953         if (caRxPacket[i] == 'S')
00954         {
00955             indexJ = i + 1;
00956 
00957             while (indexJ < PACKETDIM)
00958             {
00959                 if (caRxPacket[indexJ] == ';')
00960                 {
00961                     break;
00962                 }
00963                 else
00964                 {
00965                     tempCH1[y++] = caRxPacket[indexJ];
00966                 }
00967 
00968                 indexJ++;
00969             } // se trova ; esce dall'istruzione e salva il valore di uscita dell'indice
00970 
00971             indexT = indexJ + 1;
00972 
00973             while (indexT < PACKETDIM)
00974             {
00975                 if (caRxPacket[indexT] == 'P')
00976                 {
00977                     break;
00978                 }
00979                 else
00980                 {
00981                     tempCH2[u++] = caRxPacket[indexT];
00982                 }
00983 
00984                 indexT++;
00985             }
00986             
00987             // trasforma il dato da sequenza di caratteri a numero float utile per analisi
00988 
00989             strcpy(CH1_to_string, tempCH1);
00990             CH1 = atof(CH1_to_string);
00991 
00992             strcpy(CH2_to_string, tempCH2);
00993             CH2 = atof(CH2_to_string);
00994 
00995             SC.printf("> TEMPERATURE - CH1: %4.8f[Celsius], CH2: %4.8f[Celsius]\n\r", CH1, CH2);
00996 
00997             break;
00998         }
00999     }
01000 
01001     wait_ms(150); // pausa per garantire sincronizzazione tra riempimento buffer e svuotamento
01002 }
01003 
01004 
01005 /* ***************************************************************** */             // MIMA OPERATING MODE
01006 
01007 
01008 const char* MIMA_OPERATING_MODE(char *s)
01009 {
01010     if (s[7] == '0' && s[8] == '0' && s[9] == '0' && s[10] == '1')
01011     {
01012         return "> Setting MIMA... Sleeping mode";
01013     }
01014     else if (s[7] == '0' && s[8] == '0' && s[9] == '1' && s[10] == '0')
01015     {
01016         return "> Setting MIMA... Awake mode";
01017     }
01018     else if (s[7] == '0' && s[8] == '0' && s[9] == '1' && s[10] == '1')
01019     {
01020         return "> Setting MIMA... Calibration mode";
01021     }
01022     else if (s[7] == '0' && s[8] == '1' && s[9] == '0' && s[10] == '0')
01023     {
01024         return "> Setting MIMA... Observation mode";
01025     }
01026     else if (s[7] == '0' && s[8] == '1' && s[9] == '0' && s[10] == '1') 
01027     {
01028         return "> Setting MIMA... Auto-test mode";
01029     }
01030     else if (s[7] == '0' && s[8] == '1' && s[9] == '1' && s[10] == '0')
01031     {
01032         return "> Setting MIMA... Full testing mode";
01033     }
01034     
01035     return "\0";
01036 }
01037 
01038 
01039 /* ***************************************************************** */