1 |
kusanagi |
1.1 |
/** @file |
2 |
kusanagi |
1.2 |
* $Source: /home/cvsmanager/yoda/techmodel/TmtcReader.cpp,v $ |
3 |
kusanagi |
2.0 |
* $Id: TmtcReader.cpp,v 1.4 2004/09/21 20:24:33 kusanagi Exp $ |
4 |
kusanagi |
1.2 |
* $Author: kusanagi $ |
5 |
kusanagi |
1.1 |
* |
6 |
|
|
* Implementation of the TmtcReader class. |
7 |
|
|
* ToBeDone: |
8 |
|
|
* Control the CRC for the entire data Packet not just for single records |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
#include <string> |
15 |
kusanagi |
1.4 |
#include <log4cxx/logger.h> |
16 |
kusanagi |
1.1 |
extern "C" { |
17 |
|
|
#include <sys/time.h> |
18 |
|
|
#include "CRC.h" |
19 |
|
|
} |
20 |
|
|
|
21 |
|
|
#include <fstream> |
22 |
|
|
#include "stdio.h" |
23 |
|
|
#include "ReaderAlgorithms.h" |
24 |
|
|
|
25 |
|
|
#include "event/tmtc/TmtcRecord.h" |
26 |
|
|
|
27 |
|
|
using namespace pamela; |
28 |
|
|
using namespace pamela::techmodel; |
29 |
|
|
|
30 |
kusanagi |
1.4 |
static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.TmtcReader")); |
31 |
kusanagi |
1.1 |
|
32 |
|
|
/** |
33 |
|
|
* Constructor. |
34 |
|
|
*/ |
35 |
|
|
TmtcReader::TmtcReader(void): |
36 |
|
|
TechmodelAlgorithm(PacketType::Tmtc, "TechmodelTmtcReader") { |
37 |
kusanagi |
1.4 |
logger->debug(_T("Constructor")); |
38 |
kusanagi |
1.1 |
Tmtc = new TmtcEvent(); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
/** |
42 |
|
|
* Get a string with the version info of the algorithm. |
43 |
|
|
*/ |
44 |
|
|
std::string TmtcReader::GetVersionInfo(void) const { |
45 |
|
|
return |
46 |
kusanagi |
2.0 |
"$Header: /home/cvsmanager/yoda/techmodel/TmtcReader.cpp,v 1.4 2004/09/21 20:24:33 kusanagi Exp $\n"; |
47 |
kusanagi |
1.1 |
} |
48 |
|
|
|
49 |
|
|
/** |
50 |
|
|
* Initialize the algorithm with a special run. This will initialize the |
51 |
|
|
* event reader routines for all packet types. |
52 |
|
|
*/ |
53 |
|
|
void TmtcReader::Init(PamelaRun *run) { |
54 |
|
|
SetInputStream(run); |
55 |
|
|
run->WriteSubPacket(this, &Tmtc, Tmtc->Class()); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
/** |
59 |
|
|
* Unpack the Tmtc event from an input file. |
60 |
|
|
*/ |
61 |
|
|
void TmtcReader::RunEvent(int EventNumber, long int length) { |
62 |
|
|
|
63 |
|
|
/* |
64 |
|
|
//Just to test crc |
65 |
|
|
char tempbuff[length]; |
66 |
|
|
InputFile->read(tempbuff, sizeof(tempbuff)); |
67 |
|
|
UINT16 subCRC = 0; |
68 |
|
|
for (int jj = 0; jj < length ; jj++){ |
69 |
|
|
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)(tempbuff+jj), 1); |
70 |
|
|
} |
71 |
|
|
//Just to test crc |
72 |
|
|
*/ |
73 |
|
|
|
74 |
|
|
int i, j; |
75 |
kusanagi |
1.2 |
char subData[TMTC_SUB_LENGTH]; |
76 |
kusanagi |
1.1 |
char CRCbuff[TMTC_SUBCRC_LENGTH]; |
77 |
|
|
char eventCRC[TMTC_CRC_LENGTH]; |
78 |
|
|
int numRecords = (length-TMTC_CRC_LENGTH)/(TMTC_SUB_LENGTH + TMTC_SUBCRC_LENGTH); //subtract the last two bytes because are the final CRC |
79 |
|
|
long int start; |
80 |
kusanagi |
1.2 |
|
81 |
|
|
TmtcRecord* rec; |
82 |
kusanagi |
1.1 |
Tmtc->Records->Clear(); |
83 |
|
|
TClonesArray &recs = *(Tmtc->Records); |
84 |
kusanagi |
1.2 |
|
85 |
kusanagi |
1.1 |
UINT16 partialCRC = 0; //partial CRC updated as mcmd packet is read (to compare with the last two bytes of this event) |
86 |
|
|
UINT16 readCRC = 0; //partial CRC updated as mcmd packet is read (to compare with the last two bytes of this event) |
87 |
|
|
|
88 |
kusanagi |
1.2 |
/* char tempData[length-TMTC_CRC_LENGTH]; |
89 |
|
|
InputFile->read(tempData, sizeof(tempData)); |
90 |
|
|
partialCRC = CM_Compute_CRC16(0, (UINT8*)tempData, (length-TMTC_CRC_LENGTH)); |
91 |
|
|
*/ |
92 |
kusanagi |
1.1 |
|
93 |
|
|
for(i = 0; i < numRecords; i++) { |
94 |
|
|
start = InputFile->tellg(); |
95 |
kusanagi |
1.2 |
InputFile->read(subData, sizeof(subData)); |
96 |
kusanagi |
1.1 |
InputFile->read(CRCbuff, sizeof(CRCbuff)); |
97 |
|
|
|
98 |
kusanagi |
1.2 |
/* for (int jj = 0; jj < TMTC_SUB_LENGTH ; jj++){ |
99 |
|
|
partialCRC = CM_Compute_CRC16(0, (UINT8*)tempData, (length-TMTC_CRC_LENGTH)); |
100 |
|
|
} |
101 |
|
|
*/ |
102 |
|
|
|
103 |
|
|
partialCRC = CM_Compute_CRC16(partialCRC, (UINT8*)&subData, TMTC_SUB_LENGTH); |
104 |
kusanagi |
1.1 |
partialCRC = CM_Compute_CRC16(partialCRC, (UINT8*)&CRCbuff, TMTC_SUBCRC_LENGTH); |
105 |
|
|
|
106 |
|
|
//This == CRCBuff is not really parametric take care if have to change the static lengths |
107 |
kusanagi |
1.2 |
if((UINT8)(CM_Compute_CRC16(0, (UINT8*)&subData, TMTC_SUB_LENGTH)) == (UINT8)CRCbuff[0]){ |
108 |
|
|
rec = new(recs[i]) TmtcRecord(); //add a new TmtcRecord |
109 |
|
|
rec->TM_RECORD_OBT = (((UINT32)subData[0]<<24)&0xFF000000) + (((UINT32)subData[1]<<16)&0x00FF0000) + (((UINT32)subData[2]<<8)&0x0000FF00) + ((UINT32)subData[3])&0x000000FF; |
110 |
kusanagi |
1.1 |
|
111 |
|
|
for(j = 0; j < 16; j++) { |
112 |
kusanagi |
1.2 |
rec->TM_DEA_ANA[j] = (((UINT16)subData[4+j]<<8)&0xFF00) + (((UINT16)subData[5+j])&0x00FF); |
113 |
kusanagi |
1.1 |
rec->TM_DEA_ANA_P[j] = (float)(rec->TM_DEA_ANA[j]*0.02); |
114 |
|
|
|
115 |
kusanagi |
1.2 |
rec->TM_TH_ANA[j] = subData[36+j]; //36 is the size of TM_DEA_ANA + OBT |
116 |
kusanagi |
1.1 |
rec->TM_TH_ANA_P[j] = convert_th(rec->TM_TH_ANA[j]); |
117 |
|
|
} |
118 |
|
|
|
119 |
kusanagi |
1.2 |
rec->TM_BIL_DIAG_ACQ = subData[53]; |
120 |
|
|
rec->TM_CC_DIAG_ACQ = (((UINT32)subData[54]<<24)&0xFF000000) + (((UINT32)subData[55]<<16)&0x00FF0000) + (((UINT32)subData[56]<<8)&0x0000FF00) + ((UINT32)subData[57])&0x000000FF; |
121 |
kusanagi |
1.1 |
} else { |
122 |
kusanagi |
1.4 |
stringstream oss; |
123 |
|
|
oss.flush(); |
124 |
|
|
oss << "Wrong CRC on Subpacket int TMTC Packet starting at position" |
125 |
|
|
<< start; |
126 |
|
|
logger->warn(oss.str().c_str()); |
127 |
kusanagi |
1.1 |
} |
128 |
|
|
} |
129 |
|
|
InputFile->read(eventCRC, sizeof(eventCRC)); |
130 |
|
|
readCRC = (((UINT16)(eventCRC[0]<<8))&0xFF00) + (((UINT16)eventCRC[1])&0x00FF); |
131 |
|
|
if(!(partialCRC == readCRC)) { |
132 |
kusanagi |
1.4 |
logger->warn(_T("The test of calculated CRC with one wrote on file FAILED!!")); |
133 |
kusanagi |
1.1 |
} |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
|
137 |
|
|
float TmtcReader::convert_th(int TH) { |
138 |
|
|
float a,q,deltax,deltay; |
139 |
|
|
static int chiama_fun=0; |
140 |
|
|
float gradi[22],grado=-1; |
141 |
|
|
int adc[22]={4095, 4036,2976,2213,1662,1259,983,742,576,450, |
142 |
|
|
354, 281,224,179,145, 117 ,95, 77, 64, 52, |
143 |
|
|
43, 35}; |
144 |
|
|
int maxpos=-1,minpos=-1,i; |
145 |
|
|
|
146 |
|
|
chiama_fun++; |
147 |
|
|
for (i=0;i<22;i++) |
148 |
|
|
{ |
149 |
|
|
gradi[i]=-35+(i*5); |
150 |
|
|
} |
151 |
|
|
if (TH==4095) |
152 |
|
|
{ |
153 |
|
|
grado=-35.; |
154 |
|
|
} |
155 |
|
|
else |
156 |
|
|
{ |
157 |
|
|
|
158 |
|
|
for (i=0;i<22;i++) |
159 |
|
|
{ |
160 |
|
|
if (TH>=adc[i]) |
161 |
|
|
{ |
162 |
|
|
minpos=i; |
163 |
|
|
maxpos=i-1; |
164 |
|
|
break; |
165 |
|
|
} |
166 |
|
|
} |
167 |
|
|
// grado=gradi[maxpos]-1./(adc[maxpos]-adc[minpos])*((float)TH-(float)adc[maxpos]) ; |
168 |
|
|
|
169 |
|
|
deltax=adc[maxpos]-adc[minpos]; |
170 |
|
|
deltay=gradi[maxpos]-gradi[minpos]; |
171 |
|
|
a=deltay/deltax; |
172 |
|
|
q=gradi[maxpos]-a*adc[maxpos]; |
173 |
|
|
grado=a*TH+q; |
174 |
|
|
} |
175 |
|
|
return (grado); |
176 |
|
|
} |