source: tmcsimulator/branches/FEPSimulator/DataPacker.cpp @ 159

Revision 159, 8.7 KB checked in by jtorres, 9 years ago (diff)

network.h: commented data structs, DataPacker?.h moved include to header from source

Line 
1#include "DataPacker.h"
2
3unsigned char * DataPacker::msg;
4
5unsigned char * DataPacker::packData(STATION *station) {
6    int pos = 26;
7   
8    DataPacker packer;
9    // static meta data pack
10    msg = packer.staticDataPack(station);
11   
12    // dynamic data pack
13    pos = packer.dynamicDataPack(station, 5, pos);   
14    pos = packer.dynamicDataPack(station, 6, pos);
15    pos = packer.dynamicDataPack(station, 7, pos);
16    pos = packer.dynamicDataPack(station, 8, pos);
17    // Update data for BYTE 9, 16, 21, 22, 23, 24
18
19    // what is the current metering rate?
20    // check each LDS, if there is demand detector, the downstream node the
21    // ldsMap[i].dataPack[22-1] = ldsMap[i].dataPack[9-1] = ;
22
23    // will need to check if queue override is enabled (Currently, no queue
24    // ldsMap[i].dataPack[16-1] = ;
25
26    // byte 23: MAINLINE total volume
27    msg[23 - 1] = station->MlTotVol;
28
29    // byte 24: opposite total volume
30    msg[24 - 1] = station->OppTotVol;
31
32    // last BYTE: checksum
33    msg[station->length - 1] =
34            packer.chksum(msg, station->length - 1 - 1);
35   
36    /* hex dump of full datapack message - for debugging purposes
37    for (int j = 0; j < station->length; j++) {
38        printf("%02X", msg[j]);
39    }
40    printf("\n");
41    */
42   
43    return msg; 
44}
45
46int DataPacker::dynamicDataPack(STATION *station, int packNo, int pos) {
47   
48    int i, j, vol, occ, haveData;
49   
50    //used for data byte 23 and 24
51    // Need to check: should we do this here? or is this already set?
52    station->MlTotVol = 0;
53    station->OppTotVol = 0;
54
55    for (j = 1; j <= 8; j++) {
56        if (DataAvail(msg[packNo - 1], j)) {
57            // loop at bit j has data
58            for (int lane = 0; lane < station->loops.size(); lane++) {
59                // what's the corresponding loop of bit j
60                char * currLoopLoc = station->loops.at(lane)->loop_loc;
61               
62                // check loop location type and see if there is data for this
63                // loop in the station
64                if (packNo == 5)
65                    haveData = strcmp(currLoopLoc, dp5[j - 1]);
66                else if (packNo == 6)
67                    haveData = strcmp(currLoopLoc, dp6[j - 1]);
68                else if (packNo == 7)
69                    haveData = strcmp(currLoopLoc, dp7[j - 1]);
70                else if (packNo == 8)
71                    haveData = strcmp(currLoopLoc, dp8[j - 1]);
72                else
73                    return -1;
74               
75                // if there is NO data for this loop in the station
76                // Need to check: is this if clause doing the correct thing?
77                if (haveData == 0) {
78                    LOOP *currLoop = station->loops.at(lane);
79                    vol = currLoop->vol;
80                    occ = (int) (currLoop->occ * 900 + 0.5);
81                    VOLOCC packedVOLOCC = packVOLOCC(vol, occ);
82                    pos++;
83                    msg[pos - 1] = packedVOLOCC.high;
84                    pos++;
85                    msg[pos - 1] = packedVOLOCC.low;
86                   
87                    // Need to check: same. should this be in this if clause?
88                    if (packNo == 5) {
89                        station->MlTotVol += vol;
90                    } else if (packNo == 6) {
91                        station->OppTotVol += vol;
92                    }
93                }
94            }
95        }
96    }
97    return pos;
98}
99
100unsigned char * DataPacker::staticDataPack(STATION *station) {
101    int j;
102    // Allocate memory for dataPack
103    unsigned char *dataPack = (unsigned char *) calloc(sizeof(unsigned char), station->length);
104
105    // dataPack 5-8: lane config
106    char d5 = 0, d6 = 0, d7 = 0, d8 = 0;
107    for (j = 0; j < station->loops.size(); j++) {
108        for (int k = 0; k < 8; k++) {
109            char * currLoopLoc = station->loops.at(j)->loop_loc;
110            if (strcmp(currLoopLoc, dp5[k]) == 0)
111                d5 += pow(2, k);
112            if (strcmp(currLoopLoc, dp6[k]) == 0)
113                d6 += pow(2, k);
114            if (strcmp(currLoopLoc, dp7[k]) == 0)
115                d7 += pow(2, k);
116            if (strcmp(currLoopLoc, dp8[k]) == 0)
117                d8 += pow(2, k);
118        }
119    }
120    dataPack[5 - 1] = d5;
121    dataPack[6 - 1] = d6;
122    dataPack[7 - 1] = d7;
123    dataPack[8 - 1] = d8;
124
125    // dataPack 1: Drop number, i.e. station address
126    dataPack[1 - 1] = station->drop;
127    // dataPack2 (2 bytes per loop)
128    dataPack[2 - 1] = station->loops.size() * 2 + Fixed_Byte_To_Checksum;
129
130    // dataPacket 3 (lowbyte: # of mainline loops, highbyte: # of opposite loops)
131    int low = 0, high = 0;
132    for (j = 1; j <= 6; j++) {
133        low += DataAvail(dataPack[5 - 1], j);
134        high += DataAvail(dataPack[6 - 1], j);
135    }
136    high = high << 4;
137    dataPack[3 - 1] = high | low;
138
139    // dataPack4 (Miscl. flags: samples are: 80, A0, E0, 00)
140    dataPack[4 - 1] = 0xA0;
141
142    // dataPack 9: initialized as 00 (meaning no metering); need to be updated every 30 sec
143    dataPack[9 - 1] = 0;
144
145    // datadataPack 10-13: lane malfunction? Assuming all functional
146    dataPack[10 - 1] = 0;
147    dataPack[11 - 1] = 0;
148    dataPack[12 - 1] = 0;
149    dataPack[13 - 1] = 0;
150
151    // dataPack 14-22: ramp metering data
152    // BYTE 16 and 22 need to be updated every 30 sec
153    bool found = false;
154    for (j = 0; j < station->loops.size(); j++) {
155        if (strcmp(station->loops.at(j)->loop_loc, "DEMAND") == 0) {
156            found = true;
157            break;
158        }
159    }
160    // Need to check: if there is a demand loop, we set bit 14 to 0x0B (No metering)
161    // is this correct?
162    if (found) {
163        // Need to check: what does this flag represent??
164        // BYTE 14: mostly 07, some are 05, 03, 00
165        dataPack[14 - 1] = 0x07;
166        // mostly 06(TOD table 1); some are 0B (No metering) or 05(traffic responsive)
167        dataPack[15 - 1] = 0x06;
168        // most 00, some are 01 (queue override) or 80(Meter ON sign)
169        dataPack[16 - 1] = 0x00;
170        // Field Manual Rate
171        dataPack[17 - 1] = 0xFF;
172        // TOC Manual Rate
173        dataPack[18 - 1] = 0xFF;
174        // PSO Manual Rate
175        dataPack[19 - 1] = 0xFF;
176        // CORM Rate
177        dataPack[20 - 1] = 0xFF;
178        // Local Responsive Rate. DON'T UNDERSTAND YET
179        dataPack[21 - 1] = 0x00;
180        // TOD Rate: need to query RAMP plugin! Need to check, might be important?
181        dataPack[22 - 1] = 0x00;
182    }// LDS: NO Metering
183    else {
184        dataPack[14 - 1] = 0x00;
185        dataPack[15 - 1] = 0x0B;
186        dataPack[16 - 1] = 0x00;
187        dataPack[17 - 1] = 0xFF;
188        dataPack[18 - 1] = 0xFF;
189        dataPack[19 - 1] = 0xFF;
190        dataPack[20 - 1] = 0xFF;
191        dataPack[21 - 1] = 0x00;
192        dataPack[22 - 1] = 0x00;
193    }
194
195    // dataPack 23-24: sum of mainline/Opp traffic data; need to be updated every 30 sec
196    // Need to check, is this in sync with the notes in dynamicDataPack? why
197    // are we setting it to 0 here, might not need to...
198    station->MlTotVol = 0;
199    station->OppTotVol = 0;
200    dataPack[23 - 1] = station->MlTotVol;
201    dataPack[24 - 1] = station->OppTotVol;
202
203    // dataPack 25-26: BYTE 25 is fixed, i.e. 03; BYTE 26 is either 0xA2 or 0x84
204    // Need to check: what is byte 26? what are 0xA2 and 0x84
205    dataPack[25 - 1] = 0x03;
206    dataPack[26 - 1] = 0x84;
207
208    return dataPack;
209}
210
211bool DataPacker::DataAvail(char flag, int num) {
212    int mag, fel;
213
214    // find mask value
215    if (num == 1)
216        mag = 0x01;
217    else if (num == 2)
218        mag = 0x02;
219    else if (num == 3)
220        mag = 0x04;
221    else if (num == 4)
222        mag = 0x08;
223    else if (num == 5)
224        mag = 0x10;
225    else if (num == 6)
226        mag = 0x20;
227    else if (num == 7)
228        mag = 0x40;
229    else if (num == 8)
230        mag = 0x80;
231
232    fel = flag & mag;
233    fel = fel >> (num - 1);
234
235    if (fel == 1)
236        return true;
237    else
238        return false;
239}
240
241// checksum based on data from byte 1 (after 0DOA) to the second last byte (the
242char DataPacker::chksum(unsigned char *dataptr, int len) {
243    int i;
244    unsigned char checksum = 0;
245
246    for (i = 0; i < len; i++)
247        checksum += dataptr[i];
248
249    return checksum;
250}
251
252// convert vol and occ data to a two-byte data packet
253VOLOCC DataPacker::packVOLOCC(int vol, int occ)
254{
255   int bit, i;
256   int   high, low; 
257   int high_high = 0, high_low = 0;
258   VOLOCC com;
259   
260   // occ data: 10 bits
261   low = 0;
262   for (i=9; i>=0; i--) 
263   {
264      if (i >= 8)
265      {
266         bit = ((occ >> i) & 1);
267         high_low += pow(2, (i - 8)) * bit; 
268      }
269      // lower byte
270      else
271      {
272         bit = ((occ >> i) & 1);
273         low += pow(2, i) * bit;
274      }
275   }
276
277   // vol: 6 bits (1. vol, shift to the left for two times; 2. combine data)
278   high_high = vol << 2;
279   high = high_high | high_low;
280
281   com.high = high;
282   com.low = low;
283
284   return com;
285}
Note: See TracBrowser for help on using the repository browser.