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