Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FreescaleIAP SimpleDMA mbed-rtos mbed
Fork of CDMS_CODE by
Compression.h
00001 /*-----------------------------------------to_do_compression.h-------------------------------------------------- 00002 00003 -> SD_Write error to be checked 00004 -> fsc_counter is 32 bit but in the code 24 bit. Talk to chaitu 00005 ---------------------------------------------------------------------------------------------------------------*/ 00006 00007 //updated compression algorithm 00008 /*#include <iostream> 00009 #include "stdio.h" 00010 #include "crc.h" 00011 #include "interleave.h" 00012 #include "Convolution.h" 00013 #include "bitset"*/ 00014 //using namespace std; 00015 00016 //reading functions 2byte, 4byte 00017 #define srpz 0 00018 #define scpz 0 00019 #define scpz_last 0 00020 #define srpz_last 0 00021 #define debug_fsc 0 00022 #define debug_time 0 00023 #define debug_dma 1 00024 00025 #if debug_dma 00026 uint16_t read_2byte(uint8_t* ptr) 00027 { 00028 uint16_t output = (((uint16_t )(*(ptr+1)))<<8); 00029 output += (((uint16_t)(*ptr))); 00030 return output; 00031 } 00032 00033 uint32_t read_4byte(uint8_t* ptr) 00034 { 00035 uint32_t output = (uint32_t)(*(ptr+3))<<24; 00036 output += (uint32_t)(*(ptr+2)<<16); 00037 output += (uint32_t)(*(ptr+1)<<8); 00038 output += (uint32_t)(*(ptr)); 00039 return output; 00040 } 00041 #endif 00042 #if !debug_dma 00043 uint16_t read_2byte(uint8_t* ptr) 00044 { 00045 uint16_t output = (uint8_t ) *(ptr+1); 00046 output += ( (uint8_t )(*ptr) ) << 8; 00047 return output; 00048 } 00049 00050 uint32_t read_4byte(uint8_t* ptr) 00051 { 00052 uint32_t output = (uint32_t) (*(ptr+3)); 00053 output += (uint32_t)(*(ptr+2)<<8); 00054 output += (uint32_t)(*(ptr+1)<<16); 00055 output += (uint32_t)(*(ptr)<<24); 00056 return output; 00057 } 00058 #endif 00059 uint8_t adjust(uint8_t size, uint8_t data, uint8_t* ptr , uint8_t space) 00060 { 00061 space = space&0x0f; 00062 if(space == 8) *ptr = 0; 00063 data = data&((1<<size)-1); 00064 if(space >= size){ 00065 *ptr += data<<(space-size); 00066 if(space - size == 0){ 00067 return 0x18; 00068 }else{ 00069 return space-size; 00070 } 00071 }else{ 00072 ptr[0] += data>>(size-space); 00073 ptr[1] = (data<<(8-(size-space)))&0xff ; 00074 return 0x10 + 8-(size - space); 00075 } 00076 } 00077 int compress (int data, int x, int y) //to be compressed with scheme (msb x)*4^y ; 00078 { 00079 for(int i = 0 ; i < (1<<y) ; i++) 00080 { 00081 if(data <= ( (1<<x)-1) * (1<<(2*i)) ) 00082 { 00083 return ( ((data>>i*2)<<y) + i); 00084 } 00085 } 00086 if ( data > (( (1<<x)-1) * (1<<(2*((1<<y)-1)))) ) 00087 { 00088 //cout <<"compression exception"<<endl; 00089 return (((1<<x)-1) * (1<<(2*((1<<y)-1)))); 00090 } 00091 } 00092 00093 //--------------------------------------------------------beacon 00094 uint8_t beacon_array[134]; 00095 uint8_t beacon_cntr = 1; 00096 uint8_t *beacon_ptr; 00097 //--------------------------------------------------------beacon 00098 00099 //variable declarations 00100 uint8_t srp_mode , at , pzf , ezf ,sfp[71] ,scp[56],beacon_at; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux. 00101 uint32_t sfp_bin[52] , scp_bin[52]; //storing the bin values. 00102 uint32_t sfp_threshold_m0[52] = {124,124,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,4032,252,252,252,252,252,4032,8128,8128,8128,8128,8128,8128,8128,8128,960,960,960,960,960,960,960,960,960,960,960,960,4032,4032,4032,4032,8128,8128,8128,8128}; 00103 uint32_t scp_threshold_m0[52] = {4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,114688,8128,4032,4032,4032,4032,114688,245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760}; 00104 uint32_t scp_threshold_m0_1[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for callibrated mode with data conservation 00105 uint32_t scp_sfp_threshold_m0_2[3] = {245760,65472,65472}; 00106 uint32_t sfp_threshold_m1[52] = {1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,8128,8128,8128}; 00107 uint32_t scp_threshold_m1[52] = {114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,245760,245760,245760,245760}; 00108 uint8_t frames[3][134] = {0}; 00109 uint8_t position_tm_frame[3] = {8,11,5} , position_tm_starting[3] = {8,11,5}; //{sc,sf-bt,sf-at} 00110 uint8_t id; //sf = 0,sc-at = 1,sc-bt = 2; 00111 uint8_t TM_interleave_data[512] , TM_convoluted_data[270] = {0}; 00112 uint64_t proton_scp_sum,electron_scp_sum,FSC_science; 00113 uint16_t temp_crc; 00114 uint32_t debug_cntr, size,space,length; 00115 uint8_t *ptr ,* pointer; 00116 uint64_t sci_time = 0, time_prev_scp = 0; 00117 uint32_t attitude = 0; 00118 uint32_t beacon_threshold[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472}; 00119 00120 /*----------------------------temporary counters used for testing purposes---------------------------*/ 00121 uint32_t test_fsc[4]={1,1,1,1}; 00122 /*---------------------------------------------------------------------------------------------------*/ 00123 00124 00125 Convolution ConvObj; 00126 void convolution (uint8_t * ptr){ 00127 ConvObj.convolutionEncode(ptr, TM_convoluted_data); 00128 ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135); 00129 } 00130 00131 //give the pointer of 6 second data to this function 00132 void srp(uint8_t * head) 00133 { 00134 uint8_t sd_stat = 0; 00135 SCI_LED1 =1; 00136 00137 // SCI_LED1 = !SCI_LED1; 00138 // gPC.printf("\n\rsrp"); 00139 debug_cntr = 0; 00140 sci_time = FCTN_CDMS_RD_RTC(); 00141 #if debug_time 00142 sci_time = 0x0000000021000000; 00143 #endif 00144 if(time_prev_scp==0){ 00145 time_prev_scp = sci_time; 00146 } 00147 FCTN_SD_MNGR(); ///changed recently 00148 for (int i = 0; i < 52 ; i++) 00149 { 00150 scp_bin[i] = 0; 00151 } 00152 ptr = head + 3; //ptr points to proton energy bin of srp 00153 srp_mode = head[2]&0x1; 00154 for(uint8_t counter = 0 ; counter < 60 ; counter++) 00155 { 00156 #if srpz 00157 // gPC.printf("\n\n\rSRP count:%d",counter+1); 00158 #endif 00159 /*-------------------- ------------populating sfp_bin and scp_bin[] starts here(also tag AT or BT)-----------------------*/ 00160 at = 0; pzf = 1; ezf = 1; 00161 if(srp_mode == 0) //calibrated mode 00162 { 00163 #if srpz 00164 // gPC.printf("\n\rSRP - Calibrated Mode"); 00165 #endif 00166 for(int i=0; i<48 ; i++) //first 48 bins or SRP are of 16 bits size 00167 { 00168 sfp_bin[i] = read_2byte(ptr + i*2); 00169 scp_bin[i] += sfp_bin[i]; 00170 if(sfp_bin[i]>sfp_threshold_m0[i]) 00171 { 00172 //gPC.printf("\n\rSFP above threshold index:%d",i); 00173 at = 1; 00174 } 00175 if(i<17) 00176 { 00177 if(sfp_bin[i] > 0) 00178 pzf = 0; 00179 } 00180 else if (i>17 && i < 23) 00181 { 00182 if(sfp_bin[i]>0) 00183 ezf = 0; 00184 } 00185 } 00186 for(int i=0; i<4; i++) //last 4 bins(excluding the spare bin) are 32 bit each 00187 { 00188 sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i ); 00189 scp_bin[i+48] += sfp_bin[i+48]; 00190 if(sfp_bin[i+48]>sfp_threshold_m0[i+48]){ 00191 //gPC.printf("\n\rSP above threshold index: %d",i+48); 00192 at = 1; 00193 } 00194 } 00195 } 00196 else if(srp_mode == 1) //scattered mode 00197 { 00198 #if srpz 00199 gPC.printf("\n\rSRP - Scatter Plot Mode"); 00200 #endif 00201 for(int i = 0; i <48; i++) //first 48 bins of SRP are of 16 bit size 00202 { 00203 sfp_bin[i] = read_2byte(ptr+2*i); 00204 scp_bin[i] += sfp_bin[i]; 00205 //gPC.printf("\n\rsfp_bin[%d]= %u\t\tscp_bin[%d]= %u",i,sfp_bin[i],i,scp_bin[i]); 00206 if(sfp_bin[i] > sfp_threshold_m1[i]){ 00207 //gPC.printf("\n\rSP above threshold index: %d",i); 00208 at = 1; 00209 } 00210 } 00211 for(int i = 0; i < 4 ; i++) //next 4 bins are of 32 bit size 00212 { 00213 sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i ); 00214 scp_bin[i+48] += sfp_bin[i+48]; 00215 if(sfp_bin[i+48] > sfp_threshold_m1[i+48]){ 00216 //gPC.printf("\n\rSP above threshold index: %d",i+48); 00217 at = 1; 00218 } 00219 } 00220 } 00221 /*---------------------------populating sfp_bin[] and scp_bin[] from srp ends here(also tagging AT or BT)------------------*/ 00222 00223 //ptr = ptr + 112; //moving the pointer to next srp packet in the DMA buffer 00224 00225 /*------------------------------------------forming a science fine packet starts here---------------------------------------- 00226 First we fill packet header, then fill packet data and finally assign its length 00227 ----------------------------------------------------------------------------------------------------------------------------*/ 00228 for(int i = 0; i<71; i++) 00229 sfp[i] = 0; 00230 if(srp_mode == 0) //calibrated mode 00231 { 00232 if(at == 0) 00233 { 00234 #if srpz 00235 gPC.printf("\n\rSFP Below Threshold"); 00236 #endif 00237 pointer = sfp; debug_cntr = 0; 00238 space = adjust(4, attitude,pointer,8); pointer += space>>4; debug_cntr += space>>4; 00239 space = adjust(6, counter,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00240 space = adjust(1, srp_mode,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00241 space = adjust(1, pzf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00242 space = adjust(1, ezf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00243 for(uint8_t i = 0 ; i < 8 ; i++){ 00244 space = adjust(1, ((compress(sfp_bin[24+i],7,2))&0x100)>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00245 space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00246 } 00247 for(uint8_t i = 0 ; i <12 ; i++){ 00248 space = adjust(6, (compress(sfp_bin[32+i],4,2)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00249 } 00250 for(uint8_t i = 0; i < 4; i++){ 00251 space = adjust(8, (compress(sfp_bin[44+i],6,2)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00252 } 00253 for(uint8_t i = 0 ; i < 4 ; i++){ 00254 space = adjust(1, (compress(sfp_bin[48+i],7,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00255 space = adjust(8, compress(sfp_bin[48+i],7,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00256 } 00257 space = adjust(8, ((compress(sfp_bin[17],6,2))&0xff) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00258 space = adjust(8, ((compress(sfp_bin[23],6,2))&0xff) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00259 if(pzf == 0){ 00260 for(uint8_t i = 0; i<2 ; i++){ 00261 space = adjust(6, ((compress(sfp_bin[i],5,1))&0xff) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00262 } 00263 for(uint8_t i = 0; i<15 ; i++){ 00264 space = adjust(5, ((compress(sfp_bin[i+2],4,1))&0xff) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00265 } 00266 } 00267 if(ezf == 0){ 00268 for(uint8_t i = 0; i <5 ;i++){ 00269 space = adjust(7, ((compress(sfp_bin[18+i],6,1))&0xff) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00270 } 00271 } 00272 } //below thershold ends here. 00273 if(at == 1){ 00274 #if srpz 00275 gPC.printf("\n\rSFP Above Threshold"); 00276 #endif 00277 pointer = sfp + 6; debug_cntr = 6;space = 8; 00278 sfp[0] = (sci_time>>27)&0xff; sfp[1] = (sci_time>>19)&0xff; sfp[2] = (sci_time>>11)&0xff; sfp[3] = (sci_time>>3)&0xff; 00279 sfp[4] = ((sci_time&0x07)<<5) + ((attitude&0x0f)<<1) + (counter>>5); 00280 sfp[5] = ((counter&0x1f)<<3) + (srp_mode<<2); 00281 sfp[5] += (pzf<<1) + ezf ; 00282 for(uint8_t i = 0 ; i < 8 ; i++){ 00283 space = adjust(1, (compress(sfp_bin[24+i],7,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00284 space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00285 } 00286 for(uint8_t i = 0 ; i <12 ; i++){ 00287 space = adjust(6, ((compress(sfp_bin[32+i],3,3))&0xff) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00288 } 00289 for(uint8_t i = 0 ; i < 4 ; i++){ 00290 space = adjust(2, (compress(sfp_bin[44+i],8,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00291 space = adjust(8, compress(sfp_bin[44+i],8,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00292 } 00293 for(uint8_t i = 0 ; i < 4 ; i++){ 00294 space = adjust(1, (compress(sfp_bin[48+i],6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00295 space = adjust(8, compress(sfp_bin[48+i],6,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00296 } 00297 space = adjust(2, (compress(sfp_bin[17],8,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00298 space = adjust(8, compress(sfp_bin[17],8,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00299 space = adjust(2, (compress(sfp_bin[23],8,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00300 space = adjust(8, compress(sfp_bin[23],8,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00301 00302 if(pzf == 0){ 00303 //cout<<"proton bins "; 00304 #if srpz 00305 gPC.printf("\n\rProton bins present in SRP"); 00306 #endif 00307 for(uint8_t i = 0; i<17 ; i++){ 00308 space = adjust(2, ((compress(sfp_bin[i],8,2))>>8) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00309 space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00310 //printf("%02X ",compress(sfp_bin[i],8,2)); 00311 } 00312 } 00313 if(ezf == 0){ 00314 //cout<<"electron bins "; 00315 #if srpz 00316 gPC.printf("\n\rElectron bins present in SRP"); 00317 #endif 00318 for(int i = 0; i<5 ; i++){ 00319 space = adjust(2,((compress(sfp_bin[18+i],8,2))>>8),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00320 space = adjust(8, compress(sfp_bin[18+i],8,2),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00321 //printf("%02X ",compress(sfp_bin[i],8,2)); 00322 } 00323 } 00324 } //above threshold ends here. 00325 if(at == 0) 00326 { 00327 id = 1; length = 241+5; //5 spare bits 00328 if(pzf == 0) 00329 length += 87; 00330 if(ezf == 0) 00331 length += 35; 00332 } 00333 else 00334 { 00335 id = 2; length = 288; //0 spare bits 00336 if(pzf == 0) 00337 length += 170; 00338 if(ezf == 0) 00339 length += 50; 00340 } 00341 } 00342 else if(srp_mode == 1) //scattered mode 00343 { 00344 if(at == 0) 00345 { 00346 #if srpz 00347 gPC.printf("\n\rSFP below threshold"); 00348 #endif 00349 pointer = sfp; debug_cntr = 0; space = 8; 00350 space = adjust(4, attitude,pointer,8); pointer += space>>4; debug_cntr += space>>4; 00351 space = adjust(6, counter,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00352 space = adjust(1, srp_mode,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00353 for(uint8_t i=0; i<48; i++) 00354 { 00355 space = adjust(7, compress(sfp_bin[i],5,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00356 } 00357 for(uint8_t i = 0 ; i < 4 ; i++) 00358 { 00359 space = adjust(1, (compress(sfp_bin[48+i],7,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00360 space = adjust(8, compress(sfp_bin[48+i],7,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00361 } 00362 } 00363 if(at == 1) 00364 { 00365 00366 #if srpz 00367 gPC.printf("\n\rSFP below threshold"); 00368 #endif 00369 pointer = sfp; debug_cntr = 0; space = 8; 00370 space = adjust(3, sci_time>>32 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00371 space = adjust(8, sci_time>>24 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00372 space = adjust(8, sci_time>>16 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00373 space = adjust(8, sci_time>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00374 space = adjust(8, sci_time ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00375 space = adjust(4, attitude,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00376 space = adjust(6, counter,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00377 space = adjust(1, srp_mode,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00378 for(uint8_t i=0; i<48; i++) 00379 { 00380 space = adjust(2, (compress(sfp_bin[i],8,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00381 space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00382 } 00383 for(uint8_t i = 0 ; i < 4 ; i++) 00384 { 00385 space = adjust(1, (compress(sfp_bin[48+i],6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00386 space = adjust(8, compress(sfp_bin[48+i],6,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00387 } 00388 } 00389 if(at == 0) 00390 { 00391 id = 1; length = 384; //0 spare bits 00392 } 00393 else 00394 { 00395 id = 2; length = 568; //0 spare bits 00396 } 00397 } 00398 length = (length%8==0)?(length/8):(length/8)+1; //converting length to mulitple of 8 00399 #if srpz 00400 gPC.printf("\n\rLength of SFP packet in bytes = %d",length); 00401 #endif 00402 /*----------------------------------------forming a science packet ends here-----------------------------------------------*/ 00403 00404 /*-----------------------------Inserting the above packet(sfp) into the TM frame(s) starts here----------------------------*/ 00405 if(id==1){ 00406 if(position_tm_frame[id]>130){ 00407 #if srpz 00408 gPC.printf("\n\rSkipping the current frame.Inserting sfp packet into the next frame."); 00409 #endif 00410 while(position_tm_frame[id]<132){ 00411 frames[id][position_tm_frame[id]] = 0; 00412 position_tm_frame[id]++; 00413 } 00414 } 00415 } 00416 else if(id==2){ 00417 if(position_tm_frame[id]>126){ 00418 #if srpz 00419 gPC.printf("\n\rSkipping the current frame.Inserting sfp packet into the next frame."); 00420 #endif 00421 while(position_tm_frame[id]<132){ 00422 frames[id][position_tm_frame[id]] = 0; 00423 position_tm_frame[id]++; 00424 } 00425 } 00426 } 00427 if(position_tm_frame[id]==position_tm_starting[id]){ 00428 frames[id][6-id] = position_tm_starting[id]; 00429 } 00430 for(uint16_t j=0 ; j<length ;) 00431 { 00432 if(position_tm_frame[id]==frames[id][6-id]){ 00433 if(id==1){ 00434 #if srpz 00435 gPC.printf("\n\rAdding Time to TM frame"); 00436 #endif 00437 frames[id][6] = (uint8_t)((sci_time>>32)&0x07); 00438 frames[id][7] = (uint8_t)((sci_time>>24)&0xff); 00439 frames[id][8] = (uint8_t)((sci_time>>16)&0xff); 00440 frames[id][9] = (uint8_t)((sci_time>>8)&0xff); 00441 frames[id][10] = (uint8_t)((sci_time)&0xff); 00442 } 00443 } 00444 if(position_tm_frame[id]<132){ 00445 frames[id][position_tm_frame[id]] = sfp[j]; 00446 j++; 00447 position_tm_frame[id]++; //incrementing position of write pointer in TM frame 00448 } 00449 if(position_tm_frame[id] == 132) 00450 { 00451 /*-----------the current frame is completely filled and we fill frame header to write into SD card---------*/ 00452 #if srpz 00453 gPC.printf("\n\rSFP_TM frame full. Length of current packet = %d",j); 00454 gPC.printf("\n\rFirst Header pointer: %d",frames[id][6-id]); 00455 gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][6-id]]); 00456 #endif 00457 pointer = frames[id]; 00458 if(id == 1){ //below threshold 00459 space = adjust(1,0,pointer,8); 00460 space = adjust(4,3,pointer,space); 00461 FSC_science = FSC_CURRENT[3]; ///to be used as this, but FSC_CURRENT[] is 32 bit 00462 00463 //gPC.printf("3 = 0x%X",FSC_science); 00464 #if debug_fsc 00465 frames[id][1] = (test_fsc[3]>>24)&0xff; 00466 frames[id][2] = (test_fsc[3]>>16)&0xff; 00467 frames[id][3] = (test_fsc[3]>>8)&0xff; 00468 frames[id][4] = test_fsc[3]&0xff; 00469 test_fsc[3]++; 00470 #endif 00471 #if !debug_fsc 00472 frames[id][1] = (FSC_science>>24)&0xff; 00473 frames[id][2] = (FSC_science>>16)&0xff; 00474 frames[id][3] = (FSC_science>>8)&0xff; 00475 frames[id][4] = FSC_science&0xff; 00476 #endif 00477 } 00478 else if(id == 2) //above threshold 00479 { 00480 space = adjust(1,0,pointer,8); 00481 space = adjust(4,2,pointer,space); 00482 FSC_science = FSC_CURRENT[2]; ///to be used as this 00483 //gPC.printf("2 = 0x%X",FSC_science); 00484 #if debug_fsc 00485 frames[id][1] = (test_fsc[2]>>16)&0xff; 00486 frames[id][2] = (test_fsc[2]>>8)&0xff; 00487 frames[id][3] = test_fsc[2]&0xff; 00488 test_fsc[2]++; 00489 #endif 00490 #if !debug_fsc 00491 frames[id][1] = (FSC_science>>16)&0xff; 00492 frames[id][2] = (FSC_science>>8)&0xff; 00493 frames[id][3] = FSC_science&0xff; 00494 #endif 00495 } 00496 temp_crc = crc16_gen(frames[id],132); 00497 frames[id][132] = temp_crc>>8; 00498 frames[id][133] = temp_crc & 0xff; 00499 00500 #if srpz 00501 gPC.printf("\n\rPrinting SFP_TM\n\r{"); 00502 //gPC.printf("\n\r{"); 00503 for(uint8_t z=0;z<134;z++){ 00504 gPC.printf("%02X",frames[id][z]); 00505 } 00506 gPC.printf("}\n\r"); 00507 #endif 00508 /*------------------current TM frame completely filled-----------------------*/ 00509 00510 exor(frames[id]); 00511 convolution(frames[id]); 00512 interleave(TM_convoluted_data,TM_interleave_data); 00513 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144); 00514 if(id == 1) 00515 { 00516 sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,3); //sd_write will return ack later, for now not included 00517 } 00518 else if (id == 2) 00519 { 00520 sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,2); //sd_write will return ack later, for now not included 00521 } 00522 if(sd_stat) 00523 { 00524 gPC.puts("sd write fail"); 00525 } 00526 position_tm_frame[id] = position_tm_starting[id]; 00527 frames[id][6-id] = (length - j)+position_tm_starting[id]; 00528 } 00529 } 00530 /*------------------------------finished inserting the sfp packet into the TM frame(s)-------------------------------------*/ 00531 } // for loop bracket which runs 60 times 00532 #if srpz_last 00533 //gPC.printf("\n\rPrinting the last SFP_TM frame"); 00534 //gPC.printf("\n\n\rPosition of tm_pointer: %d",position_tm_frame[id]); 00535 while(position_tm_frame[id]<132){ 00536 frames[id][position_tm_frame[id]] = 0; 00537 position_tm_frame[id]++; 00538 } 00539 if(position_tm_frame[id]==132){ 00540 //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][6-id]]); 00541 pointer = frames[id]; 00542 if(id == 1){ //below threshold 00543 space = adjust(1,0,pointer,8); 00544 space = adjust(4,3,pointer,space); 00545 FSC_science = FSC_CURRENT[3]; ///to be used as this, but FSC_CURRENT[] is 32 bit 00546 #if debug_fsc 00547 frames[id][1] = (FSC_science>>24)&0xff; 00548 frames[id][2] = (FSC_science>>16)&0xff; 00549 frames[id][3] = (FSC_science>>8)&0xff; 00550 frames[id][4] = FSC_science&0xff; 00551 test_fsc[3]++; 00552 #endif 00553 #if !debug_fsc 00554 frames[id][1] = (FSC_science>>24)&0xff; 00555 frames[id][2] = (FSC_science>>16)&0xff; 00556 frames[id][3] = (FSC_science>>8)&0xff; 00557 frames[id][4] = FSC_science&0xff; 00558 #endif 00559 } 00560 else if(id == 2) //above threshold 00561 { 00562 space = adjust(1,0,pointer,8); 00563 space = adjust(4,2,pointer,space); 00564 FSC_science = FSC_CURRENT[2]; ///to be used as this 00565 #if debug_fsc 00566 frames[id][1] = (test_fsc[2]>>16)&0xff; 00567 frames[id][2] = (test_fsc[2]>>8)&0xff; 00568 frames[id][3] = test_fsc[2]&0xff; 00569 test_fsc[2]++; 00570 #endif 00571 #if !debug_fsc 00572 frames[id][1] = (FSC_science>>16)&0xff; 00573 frames[id][2] = (FSC_science>>8)&0xff; 00574 frames[id][3] = FSC_science&0xff; 00575 #endif 00576 } 00577 temp_crc = crc16_gen(frames[id],132); 00578 frames[id][132] = temp_crc>>8; 00579 frames[id][133] = temp_crc & 0xff; 00580 00581 //gPC.printf("\n\rPrinting SFP_TM\n\r{"); 00582 gPC.printf("\n\r{"); 00583 for(uint8_t z=0;z<134;z++){ 00584 gPC.printf("%02X",frames[id][z]); 00585 } 00586 gPC.printf("}\n\r"); 00587 position_tm_frame[id] = position_tm_starting[id]; 00588 } 00589 #endif 00590 at = 0; pzf = 1; ezf = 1; 00591 srp_mode = head[2]&0x1; 00592 uint8_t compression_option = (head[2]>>1)&0x3; 00593 /*-------------------------------Tagging the scp packet formed from these 60 sfp's as AT or BT starts here------------------------- 00594 ------------------(in case compression option = 0 , then tagging beacon packet(not yet formed) as AT or BT is also done)----------*/ 00595 if(srp_mode ==0) 00596 { 00597 if(compression_option == 0) 00598 { 00599 for(int i=0; i<52 ;i++) 00600 { 00601 if(scp_bin[i] > scp_threshold_m0[i]) 00602 { 00603 //gPC.printf("\n\rSCP above threshold index: %d",i); 00604 at = 1; 00605 break; 00606 } 00607 } 00608 } 00609 else if(compression_option == 1) 00610 { 00611 if(scp_bin[44] > scp_threshold_m0_1[0]){//gPC.printf("\n\rSCP above threshold index: 44"); 00612 at=1;} 00613 for(int i=0; i<4 ;i++) 00614 { 00615 if(scp_bin[48+i] > scp_threshold_m0_1[i+1]) 00616 { 00617 //gPC.printf("\n\rSCP above threshold index: %d",i+48); 00618 at = 1; 00619 break; 00620 } 00621 } 00622 if(scp_bin[17] > scp_threshold_m0_1[5]){ 00623 //gPC.printf("\n\rSP above threshold index: 17"); 00624 at=1; 00625 } 00626 if(scp_bin[23] > scp_threshold_m0_1[6]){ 00627 //gPC.printf("\n\rSP above threshold index: 23"); 00628 at=1; 00629 } 00630 00631 proton_scp_sum = 0; electron_scp_sum = 0; 00632 for(int i=0;i<17;i++) 00633 { 00634 proton_scp_sum += scp_bin[i]; 00635 } 00636 for(int i=0; i<5; i++) 00637 { 00638 electron_scp_sum += scp_bin[18+i]; 00639 } 00640 if(proton_scp_sum > scp_threshold_m0_1[7]){ //gPC.printf("\n\rProton_scp_sum above threshold"); 00641 at=1;} 00642 if(electron_scp_sum > scp_threshold_m0_1[8]){//gPC.printf("\n\rElectron_scp_sum above threshold"); 00643 at=1;} 00644 } 00645 else if(compression_option == 2) 00646 { 00647 if(scp_bin[50] > scp_sfp_threshold_m0_2[0]){//gPC.printf("\n\rSCP above threshold index:50"); 00648 at=1;} 00649 proton_scp_sum = 0; electron_scp_sum = 0; 00650 for(int i=0;i<17;i++) 00651 { 00652 proton_scp_sum += scp_bin[i]; 00653 } 00654 for(int i=0; i<5; i++) 00655 { 00656 electron_scp_sum += scp_bin[18+i]; 00657 } 00658 if(proton_scp_sum > scp_sfp_threshold_m0_2[1]){//gPC.printf("\n\rProton_SCP_sum above threshold"); 00659 at=1;} 00660 if(electron_scp_sum > scp_sfp_threshold_m0_2[2]){//gPC.printf("\n\rElectron above threshold"); 00661 at=1;} 00662 } 00663 } 00664 else if(srp_mode ==1) 00665 { 00666 if(compression_option==0){ 00667 for(int i=0; i<48; i++) 00668 { 00669 if(scp_bin[i] > scp_threshold_m1[i]){ 00670 //gPC.printf("\nSCP above threshold index:%d, Value:%d",i,scp_bin[i]); 00671 at = 1; 00672 break; 00673 } 00674 } 00675 for(int i=48; i<52; i++) 00676 { 00677 if(scp_bin[i] > scp_threshold_m1[i]){ 00678 //gPC.printf("\nSCP above threshold index:%d, Value:%d",i,scp_bin[i]); 00679 at = 1; 00680 break; 00681 } 00682 } 00683 } 00684 else if(compression_option==2){ 00685 if(scp_bin[50]>scp_sfp_threshold_m0_2[0]){ 00686 //gPC.printf("\nSCP above threshold index:50, Value:%",scp_bin[50]); 00687 at=1; 00688 } 00689 } 00690 } 00691 /*----------------------------Tagging the scp packet formed from these 60 sfp's as AT or BT ends here--------------------------*/ 00692 00693 if(srp_mode == 0) 00694 { 00695 //determining if non zero values of proton and electron bins exist for calibrated mode (srp) 00696 for(int i=0; i<17 ;i++) 00697 { 00698 if(scp_bin[i]>0) 00699 pzf = 0; 00700 } 00701 for(int i=18; i<23 ;i++) 00702 { 00703 if(scp_bin[i]>0) 00704 ezf = 0; 00705 } 00706 } 00707 /*-----------------------------------------Tagging beacon packet as AT or BT starts here-------------------------------------*/ 00708 beacon_at = 0; 00709 if(srp_mode==0) //those bins which can be meaningfully compared only in srp_mode==0 00710 { 00711 if(scp_bin[44]>beacon_threshold[0]) beacon_at = 1; 00712 if(scp_bin[17]>beacon_threshold[5]) beacon_at = 1; 00713 if(scp_bin[23]>beacon_threshold[6]) beacon_at = 1; 00714 proton_scp_sum = 0; electron_scp_sum = 0; 00715 for(int i=0;i<17;i++) 00716 { 00717 proton_scp_sum += scp_bin[i]; 00718 } 00719 for(int i=0; i<5; i++) 00720 { 00721 electron_scp_sum += scp_bin[18+i]; 00722 } 00723 if(proton_scp_sum > beacon_threshold[7]) beacon_at =1; 00724 if(electron_scp_sum > beacon_threshold[8]) beacon_at =1; 00725 } 00726 if(scp_bin[48]>beacon_threshold[1]) beacon_at = 1; 00727 if(scp_bin[49]>beacon_threshold[2]) beacon_at = 1; 00728 if(scp_bin[50]>beacon_threshold[3]) beacon_at = 1; 00729 if(scp_bin[51]>beacon_threshold[4]) beacon_at = 1; 00730 /*----------------------------------------Tagging beacon packet as AT or BT ends here------------------------------------------*/ 00731 00732 /*-----------------------------------------------Forming the beacon packet starts here-----------------------------------------*/ 00733 //Last 512 bits of beacon_array excluding CRC bits is used for storing the 5 most recent beacon packets 00734 00735 if(beacon_cntr == 1) 00736 { 00737 beacon_ptr = &(beacon_array[47]); // starting block address, stores the oldest(among the 5 most recent) beacon packet 00738 } 00739 else if(beacon_cntr>1 && beacon_cntr<6) 00740 { 00741 beacon_ptr +=17; // increase the block number(totally 5 blocks for 5 beacon packets) 00742 } 00743 else if(beacon_cntr == 6) 00744 { 00745 for(uint16_t i=0;i<17;i++) // when all 5 blocks have been used shift the last 4 blocks up along the array and 00746 { // overwrite the new beacon packet in the 5th block 00747 beacon_array[47+i] = beacon_array[64+i]; 00748 beacon_array[64+i] = beacon_array[81+i]; 00749 beacon_array[81+i] = beacon_array[98+i]; 00750 beacon_array[98+i] = beacon_array[115+i]; 00751 } 00752 beacon_ptr = &(beacon_array[115]); //address of the 5th block 00753 beacon_cntr = 5; 00754 } 00755 pointer = beacon_ptr; debug_cntr = 0; space = 8; 00756 space = adjust(2, compression_option,pointer,space); pointer += space>>4; debug_cntr += space>>4; //first two bits of compression option 00757 space = adjust(1, srp_mode,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00758 space = adjust(3,(sci_time>>32)&0x07,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00759 space = adjust(8,(sci_time>>24)&0xff,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00760 space = adjust(8,(sci_time>>16)&0xff,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00761 space = adjust(8,(sci_time>>8)&0xff,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00762 space = adjust(8,(sci_time)&0xff,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00763 space = adjust(4, (attitude)&0x0f,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00764 00765 if(srp_mode==0){ 00766 if(beacon_at == 0){ 00767 space = adjust(1, 0,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00768 space = adjust(6, compress(scp_bin[44],3,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00769 space = adjust(7, compress(scp_bin[48],4,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00770 space = adjust(7, compress(scp_bin[49],4,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00771 space = adjust(7, compress(scp_bin[50],4,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00772 space = adjust(7, compress(scp_bin[51],4,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00773 space = adjust(6, compress(scp_bin[17],3,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00774 space = adjust(6, compress(scp_bin[23],3,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00775 space = adjust(4, compress(proton_scp_sum ,10,2)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00776 space = adjust(8, compress(proton_scp_sum ,10,2),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00777 space = adjust(4, compress(electron_scp_sum ,10,2)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00778 space = adjust(8, compress(electron_scp_sum ,10,2),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00779 pointer = pointer + 1; 00780 for(int temp_i = 12; temp_i<14; temp_i++) 00781 { 00782 *pointer = 0; 00783 pointer = pointer + 1; 00784 } 00785 } 00786 else if(beacon_at==1){ 00787 space = adjust(1, 1,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00788 space = adjust(1, compress(sfp_bin[44],6,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00789 space = adjust(8, compress(sfp_bin[44],6,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00790 space = adjust(4, compress(sfp_bin[48],9,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00791 space = adjust(8, compress(sfp_bin[48],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00792 space = adjust(4, compress(sfp_bin[49],9,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00793 space = adjust(8, compress(sfp_bin[49],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00794 space = adjust(4, compress(sfp_bin[50],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00795 space = adjust(8, compress(sfp_bin[50],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00796 space = adjust(4, compress(sfp_bin[51],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00797 space = adjust(8, compress(sfp_bin[51],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00798 space = adjust(1, compress(sfp_bin[17],6,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00799 space = adjust(8, compress(sfp_bin[17],6,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00800 space = adjust(1, compress(sfp_bin[23],6,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00801 space = adjust(8, compress(sfp_bin[23],6,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00802 space = adjust(1, compress(proton_scp_sum ,6,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00803 space = adjust(8, compress(proton_scp_sum ,6,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00804 space = adjust(1, compress(electron_scp_sum,6,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00805 space = adjust(8, compress(electron_scp_sum ,6,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00806 //cout<<"for beacon space = "<<(space&0x0f)<<" counter = "<<debug_cntr; 00807 } 00808 }else if(srp_mode==1){ 00809 if(beacon_at==0){ 00810 space = adjust(1, 0,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00811 space = adjust(6, compress(0,3,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00812 space = adjust(7, compress(sfp_bin[48],4,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00813 space = adjust(7, compress(sfp_bin[49],4,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00814 space = adjust(7, compress(sfp_bin[50],4,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00815 space = adjust(7, compress(sfp_bin[51],4,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00816 pointer = pointer + 1; 00817 for(uint8_t temp_i = 7; temp_i<14; temp_i++) 00818 { 00819 *pointer = 0; 00820 pointer = pointer + 1; 00821 } 00822 } 00823 else if(beacon_at==1){ 00824 space = adjust(1, 1,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00825 space = adjust(1, compress(0,6,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00826 space = adjust(8, compress(0,6,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00827 space = adjust(4, compress(sfp_bin[48],9,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00828 space = adjust(8, compress(sfp_bin[48],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00829 space = adjust(4, compress(sfp_bin[49],9,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00830 space = adjust(8, compress(sfp_bin[49],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00831 space = adjust(4, compress(sfp_bin[50],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00832 space = adjust(8, compress(sfp_bin[50],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00833 space = adjust(4, compress(sfp_bin[51],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00834 space = adjust(8, compress(sfp_bin[51],9,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00835 pointer = pointer + 1; 00836 for(uint8_t temp_i = 10; temp_i<14; temp_i++) 00837 { 00838 *pointer = 0; 00839 pointer = pointer + 1; 00840 } 00841 } 00842 } 00843 beacon_cntr++; 00844 /*--------------------------------------------------beacon packet ends here--------------------------------------------------------*/ 00845 00846 //gPC.printf("\n\n\rSCP"); 00847 uint8_t packet_pp,time_diff; 00848 /*------------------------------------------Forming a science packet(scp) starts here----------------------------------------------*/ 00849 00850 pointer = scp; debug_cntr = 0; space = 8; 00851 time_diff = 0; 00852 00853 packet_pp = 1; 00854 if((((uint8_t)(sci_time>>7))&0x3f)<(((uint8_t)(time_prev_scp>>7))&0x3f)) 00855 { 00856 time_diff = time_diff+60; 00857 } 00858 time_diff += (((uint8_t)(sci_time>>7))&0x3f); 00859 time_diff -= (((uint8_t)(time_prev_scp>>7))&0x3f); 00860 if(time_diff>10){ 00861 packet_pp = 0; 00862 } 00863 time_prev_scp = sci_time; 00864 uint32_t sfp_at_counter; 00865 sfp_at_counter = FSC_CURRENT[2]; 00866 space = adjust(1, packet_pp,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00867 space = adjust(5, (sci_time>>8)&0x1f,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00868 space = adjust(8, (sci_time)&0xff,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00869 space = adjust(4, (attitude)&0xf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00870 space = adjust(2, compression_option,pointer,space); pointer += space>>4; debug_cntr += space>>4; //first two bits of compression option 00871 space = adjust(1, at,pointer,space); pointer += space>>4; debug_cntr += space>>4; //last bit of compression option 00872 space = adjust(1, srp_mode,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00873 if(!(srp_mode==0 && compression_option==2)) 00874 { 00875 space = adjust(8, sfp_at_counter>>16,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00876 space = adjust(8, sfp_at_counter>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00877 space = adjust(8, sfp_at_counter,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00878 } 00879 if(srp_mode == 0 && compression_option == 0){ //normal callibrated mode 00880 #if scpz 00881 gPC.printf("\n\rSCP - Normal calibrated mode"); 00882 #endif 00883 space = adjust(1, pzf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00884 space = adjust(1, ezf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00885 if(at == 0 ){ 00886 #if scpz 00887 gPC.printf("\n\rBelow threshold"); 00888 #endif 00889 for(int i = 0; i<8 ;i++){ 00890 space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00891 } 00892 for(int i = 0; i<12 ;i++){ 00893 space = adjust(5,compress(scp_bin[32+i],2,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00894 } 00895 for(int i = 0; i<4 ;i++){ 00896 space = adjust(6,compress(scp_bin[44+i],3,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00897 } 00898 for(int i = 0; i<4 ;i++){ 00899 space = adjust(7,compress(scp_bin[48+i],4,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00900 } 00901 space = adjust(6,compress(scp_bin[17],3,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00902 space = adjust(6,compress(scp_bin[23],3,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00903 if(pzf == 0){ 00904 for(int i = 0; i<2 ;i++){ 00905 space = adjust(8,compress(scp_bin[i],6,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00906 } 00907 for(int i = 0; i<15 ;i++){ 00908 space = adjust(7,compress(scp_bin[i+2],5,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00909 } 00910 } 00911 if(ezf == 0){ 00912 space = adjust(1,compress(scp_bin[18],7,2)>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00913 space = adjust(8,compress(scp_bin[18],7,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00914 for(int i=0; i<4; i++){ 00915 space = adjust(8,compress(scp_bin[19+i],6,2) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00916 } 00917 } 00918 }// below threshold ends here 00919 if(at == 1){ 00920 #if scpz 00921 gPC.printf("\n\rAbove threshold"); 00922 #endif 00923 for(int i = 0; i<8 ;i++){ 00924 space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00925 } 00926 for(int i = 0; i<12 ;i++){ 00927 space = adjust(5,compress(scp_bin[32+i],2,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00928 } 00929 for(int i = 0; i<4 ;i++){ 00930 space = adjust(1,compress(scp_bin[44+i],6,3)>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00931 space = adjust(8,compress(scp_bin[44+i],6,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00932 } 00933 for(int i=0; i<4 ;i++){ 00934 space = adjust(4,compress(scp_bin[48+i],9,3)>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00935 space = adjust(8,compress(scp_bin[48+i],9,3) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00936 } 00937 space = adjust(1,compress(scp_bin[17],6,3)>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00938 space = adjust(8,compress(scp_bin[17],6,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00939 space = adjust(1,compress(scp_bin[23],6,3)>>8,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00940 space = adjust(8,compress(scp_bin[23],6,3),pointer,space); pointer += space>>4; debug_cntr += space>>4; 00941 00942 if(pzf == 0){ 00943 for(int i = 0; i<17 ; i++){ 00944 space = adjust(8, (compress(scp_bin[i],5,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00945 } 00946 } 00947 if(ezf == 0){ 00948 for(int i = 0; i<5 ; i++){ 00949 space = adjust(8, (compress(scp_bin[18+i],5,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00950 } 00951 } 00952 } //above thresholds ends 00953 } //srp_mode == 0 ends 00954 if(srp_mode == 1){ 00955 #if scpz 00956 gPC.printf("\n\rSCP - Scatter plot mode"); 00957 #endif 00958 if(at == 0){ 00959 #if scpz 00960 gPC.printf("\n\rBelow Threshold"); 00961 #endif 00962 for(int i=0; i<48; i++){ 00963 space = adjust(6, ((compress(scp_bin[i],3,3))&0xff) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00964 } 00965 for(int i = 0 ; i < 4 ; i++){ 00966 space = adjust(7, (compress(scp_bin[48+i],4,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00967 } 00968 } 00969 if(at == 1){ 00970 #if scpz 00971 gPC.printf("\n\rAbove threshold"); 00972 #endif 00973 for(int i=0; i<48; i++){ 00974 space = adjust(7, (compress((scp_bin[i]/2),4,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00975 } 00976 for(int i = 0 ; i < 4 ; i++){ 00977 space = adjust(4, (compress(scp_bin[48+i],9,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00978 space = adjust(8, (compress(scp_bin[48+i],9,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00979 } 00980 } 00981 }// scp mode 1 end 00982 if( srp_mode == 0 && compression_option == 1 ){ //scp data conservation mode 00983 #if scpz 00984 gPC.printf("\n\rSCP - Calibrated data conservation mode"); 00985 #endif 00986 space = adjust(1, pzf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00987 space = adjust(1, ezf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00988 if(at == 0){ 00989 #if scpz 00990 gPC.printf("\n\rBelow threshold"); 00991 #endif 00992 space = adjust(6, (compress(scp_bin[44],3,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00993 for(int i=0; i<4; i++){ 00994 space = adjust(7, (compress(scp_bin[48+i],4,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00995 } 00996 space = adjust(6, (compress(scp_bin[17],3,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00997 space = adjust(6, (compress(scp_bin[23],3,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 00998 if(pzf==0){ 00999 space = adjust(4, (compress(proton_scp_sum,10,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01000 space = adjust(8, ( compress(proton_scp_sum,10,2)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01001 } 01002 if(ezf==0){ 01003 space = adjust(4, (compress(electron_scp_sum,10,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01004 space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01005 } 01006 }else if(at == 1){ 01007 #if scpz 01008 gPC.printf("\n\rAbove threshold"); 01009 #endif 01010 space = adjust(1, (compress(scp_bin[44],6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01011 space = adjust(8, (compress(scp_bin[44],6,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01012 for(int i=0; i<4; i++){ 01013 space = adjust(4, (compress(scp_bin[48+i],9,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01014 space = adjust(8, (compress(scp_bin[48+i],9,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01015 } 01016 space = adjust(1, (compress(scp_bin[17],6,3)>>8) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01017 space = adjust(8, (compress(scp_bin[17],6,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01018 space = adjust(1, (compress(scp_bin[23],6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01019 space = adjust(8, (compress(scp_bin[23],6,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01020 if(pzf==0){ 01021 space = adjust(1, (compress(proton_scp_sum,6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01022 space = adjust(8, (compress(proton_scp_sum,6,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01023 } 01024 if(ezf==0){ 01025 space = adjust(1, (compress(electron_scp_sum,6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01026 space = adjust(8, (compress(electron_scp_sum,6,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01027 } 01028 } 01029 } 01030 if(compression_option == 2 ) //scp extreme data conservation mode 01031 { 01032 if(srp_mode==0) 01033 { 01034 #if scpz 01035 gPC.printf("\n\rSCP - calibrated extreme data conservation mode"); 01036 #endif 01037 space = adjust(1, pzf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01038 space = adjust(1, ezf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01039 if(at==0){ 01040 #if scpz 01041 gPC.printf("\n\rBelow threshold"); 01042 #endif 01043 space = adjust(7, (compress(scp_bin[50],4,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01044 if(pzf==0){ 01045 space = adjust(4, (compress(proton_scp_sum,10,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01046 space = adjust(8, ( compress(proton_scp_sum,10,2)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01047 } 01048 if(ezf==0){ 01049 space = adjust(4, (compress(electron_scp_sum,10,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01050 space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01051 } 01052 }else if(at==1){ 01053 #if scpz 01054 gPC.printf("\n\rAbove threshold"); 01055 #endif 01056 space = adjust(4, (compress(scp_bin[50],9,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01057 space = adjust(8, (compress(scp_bin[50],9,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01058 if(pzf==0){ 01059 space = adjust(1, (compress(proton_scp_sum,6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01060 space = adjust(8, (compress(proton_scp_sum,6,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01061 } 01062 if(ezf==0){ 01063 space = adjust(1, (compress(electron_scp_sum,6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01064 space = adjust(8, (compress(electron_scp_sum,6,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01065 } 01066 } 01067 } 01068 else if(srp_mode==1) 01069 { 01070 #if scpz 01071 gPC.printf("\n\rSCP - Scatter plot, extreme data conservation mode"); 01072 #endif 01073 pzf = 0; ezf = 0; //because proton energy bins are not available in scatter plot mode and hence in the packet we send dummy data(which is nothng but the maximum value which fits into that particular compression scheme) 01074 space = adjust(1, pzf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01075 space = adjust(1, ezf,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01076 if(at==0){ 01077 #if scpz 01078 gPC.printf("\n\rBelow threshold"); 01079 #endif 01080 space = adjust(7, (compress(scp_bin[50],4,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01081 // below 4 lines - filling the proton ane electron energy bins with zeros(junk value) since they're not available in srp 01082 space = adjust(4, (compress(0,10,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01083 space = adjust(8, (compress(0,10,2)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01084 space = adjust(4, (compress(0,10,2))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01085 space = adjust(8, (compress(0,10,2)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01086 } 01087 else if(at==1){ 01088 #if scpz 01089 gPC.printf("\n\rAbove threshold"); 01090 #endif 01091 space = adjust(4, (compress(scp_bin[50],9,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01092 space = adjust(8, (compress(scp_bin[50],9,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01093 space = adjust(1, (compress(0,6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01094 space = adjust(8, (compress(0,6,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01095 space = adjust(1, (compress(0,6,3))>>8 ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01096 space = adjust(8, (compress(0,6,3)) ,pointer,space); pointer += space>>4; debug_cntr += space>>4; 01097 } 01098 } 01099 } 01100 /*---------------------------------------------forming a science packet ends here----------------------------------------------*/ 01101 01102 id = 0; 01103 if(srp_mode == 0 && compression_option == 0){ 01104 if(at == 0){ 01105 length = 228+2; //2 spare bits 01106 if(pzf == 0) 01107 length += 121; 01108 if(ezf == 0) 01109 length +=41; 01110 }else if(at == 1){ 01111 length = 266+6; //6 spare bits 01112 if(pzf == 0) 01113 length += 136; 01114 if(ezf == 0) 01115 length += 40; 01116 } 01117 }else if(srp_mode == 0 && compression_option == 1){ //data conservation mode 01118 if(at == 0){ 01119 length = 94+2; //2 spare bits 01120 if(pzf == 0) 01121 length += 12; 01122 if(ezf == 0) 01123 length += 12; 01124 }else if(at == 1){ 01125 length = 123+3; //3 spare bits 01126 if(pzf == 0) 01127 length += 9; 01128 if(ezf == 0) 01129 length += 9; 01130 } 01131 } 01132 else if( srp_mode == 1){ 01133 if(at == 0) 01134 length = 368; 01135 else if(at == 1) 01136 length = 432; 01137 } 01138 else if(compression_option == 2) //in extreme data conservation mode, length is same for both srp_modes 01139 { 01140 if(at == 0){ 01141 length = 31+1; //1 spare bits 01142 if(pzf == 0) 01143 length += 12; 01144 if(ezf == 0) 01145 length += 12; 01146 }else if(at == 1){ 01147 length = 36+2; //2 spare bits 01148 if(pzf == 0) 01149 length += 9; 01150 if(ezf == 0) 01151 length += 9; 01152 } 01153 } 01154 length = (length%8==0)?(length/8):(length/8)+1; 01155 #if scpz 01156 gPC.printf("\n\rSCP packet length = %d",length); 01157 #endif 01158 if(position_tm_frame[id]>129){ 01159 #if scpz 01160 gPC.printf("\n\rSkipping the current TM frame. Inserting SCP into the next frame"); 01161 gPC.printf("\n\rposition_tm_frame = %d",position_tm_frame[id]); 01162 #endif 01163 while(position_tm_frame[id]<132){ 01164 frames[id][position_tm_frame[id]] = 0; 01165 position_tm_frame[id]++; 01166 } 01167 } 01168 if(position_tm_frame[id]==position_tm_starting[id]){ 01169 frames[id][4] = position_tm_starting[id]; 01170 } 01171 for(int j= 0; j < length ;) 01172 { 01173 if(position_tm_frame[id]==frames[id][4]){ 01174 #if scpz 01175 gPC.printf("\n\rInserting Time into SCP_TM"); 01176 #endif 01177 frames[id][5] = (uint8_t)((sci_time>>29)&0x3f); 01178 frames[id][6] = (uint8_t)((sci_time>>21)&0xff); 01179 frames[id][7] = (uint8_t)((sci_time>>13)&0x3f); 01180 } 01181 if(position_tm_frame[id]<132){ 01182 frames[id][position_tm_frame[id]] = scp[j]; 01183 j++; 01184 position_tm_frame[id]++; 01185 } 01186 if(position_tm_frame[id] == 132) //space full in the frame bro 01187 { 01188 #if scpz 01189 gPC.printf("\n\rSCP_TM frame full. Length of current SCP packet = %d",j); 01190 #endif 01191 pointer = frames[id]; 01192 space = adjust(1,0,pointer,8); 01193 space = adjust(4,1,pointer,space); 01194 FSC_science = FSC_CURRENT[1]; 01195 //gPC.printf("1 = 0x%X",FSC_science); 01196 01197 #if debug_fsc 01198 frames[id][1] = (FSC_science>>16)&0xff; 01199 frames[id][2] = (FSC_science>>8)&0xff; 01200 frames[id][3] = (FSC_science)&0xff; 01201 test_fsc[1]++; 01202 #endif 01203 #if !debug_fsc 01204 frames[id][1] = (FSC_science>>16)&0xff; 01205 frames[id][2] = (FSC_science>>8)&0xff; 01206 frames[id][3] = (FSC_science)&0xff; 01207 #endif 01208 01209 temp_crc = crc16_gen(frames[id],132); 01210 frames[id][132] = temp_crc>>8; 01211 frames[id][133] = temp_crc & 0xff; 01212 #if scpz 01213 //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][4]]); 01214 //gPC.printf("\n\rPrinting SCP_TM\n\r{"); 01215 gPC.printf("\n\r{"); 01216 for(uint8_t z=0;z<134;z++){ 01217 gPC.printf("%02X",frames[id][z]); 01218 } 01219 gPC.printf("}\n\r"); 01220 #endif 01221 exor(frames[id]); 01222 convolution(frames[id]); 01223 interleave(TM_convoluted_data,TM_interleave_data); 01224 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144); 01225 01226 sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,1); //sd_write returns ack, for now not included 01227 if(sd_stat) 01228 { 01229 gPC.puts("sd write fail"); 01230 } 01231 position_tm_frame[id] = position_tm_starting[id]; 01232 frames[id][4] = (length - j)+position_tm_starting[id]; 01233 } 01234 } 01235 #if scpz_last 01236 //gPC.printf("\n\rPrinting the last SCP_TM frame"); 01237 //gPC.printf("\n\n\rPosition of tm_pointer: %d",position_tm_frame[id]); 01238 while(position_tm_frame[id]<132){ 01239 frames[id][position_tm_frame[id]] = 0; 01240 position_tm_frame[id]++; 01241 } 01242 if(position_tm_frame[id] == 132) //space full in the frame bro 01243 { 01244 //gPC.printf("\n\rSCP_TM frame full. Length of current SCP packet = %d",j); 01245 pointer = frames[id]; 01246 space = adjust(1,0,pointer,8); 01247 space = adjust(4,1,pointer,space); 01248 FSC_science = FSC_CURRENT[1]; 01249 //gPC.printf("1 = 0x%X",FSC_science); 01250 01251 #if debug_fsc 01252 frames[id][1] = (FSC_science>>16)&0xff; 01253 frames[id][2] = (FSC_science>>8)&0xff; 01254 frames[id][3] = (FSC_science)&0xff; 01255 test_fsc[1]++; 01256 #endif 01257 #if !debug_fsc 01258 frames[id][1] = (FSC_science>>16)&0xff; 01259 frames[id][2] = (FSC_science>>8)&0xff; 01260 frames[id][3] = (FSC_science)&0xff; 01261 #endif 01262 01263 temp_crc = crc16_gen(frames[id],132); 01264 frames[id][132] = temp_crc>>8; 01265 frames[id][133] = temp_crc & 0xff; 01266 //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][4]]); 01267 //gPC.printf("\n\rPrinting SCP_TM\n\r{"); 01268 gPC.printf("\n\r{"); 01269 for(uint8_t z=0;z<134;z++){ 01270 gPC.printf("%02X",frames[id][z]); 01271 } 01272 gPC.printf("}\n\r"); 01273 position_tm_frame[id] = position_tm_starting[id]; 01274 } 01275 #endif 01276 gPC.printf("fsc = %u, %u,%u\n\r", FSC_CURRENT[1],FSC_CURRENT[2],FSC_CURRENT[3]); 01277 //gPC.printf("\n\rEndSRP"); 01278 // SCI_LED1 = !SCI_LED1; 01279 SCI_LED1 = 0; 01280 01281 01282 }
Generated on Fri Jul 15 2022 00:06:54 by
1.7.2
