source: proiecte/ptvs/src/vnsim/core/CarInfo.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 6.7 KB
Line 
1/************************************************************************************
2 * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University
3 * All rights reserved.
4 * Refer to LICENSE for terms and conditions of use.
5 ***********************************************************************************/
6package vnsim.core;
7
8
9import java.nio.ByteBuffer;
10
11import vnsim.map.object.*;
12
13
14/**
15 * Class that holds GPS & state information about a vehicle, accessibile to
16 * the applications.
17 *
18 */
19
20public class CarInfo 
21{
22        protected long timestamp;       // can be int
23        protected double speed;
24        protected short roadIdx = -1;
25        protected short pointIdx = -1;
26        protected byte direction;       // 1 -  car runs on a street, from the first point
27                                                                //              to the last one, in they are stored in the
28                                                                //              in the road's points vector
29                                                                // 0 -  otherwise
30       
31        protected byte hpoz;    //0 behind, 1 center, 2 in front
32        protected byte vpoz;    //0 left, 1 center, 2 right
33                                                        //(position related to the point on the road to achieve
34                                                        //car positioning on lanes)
35
36       
37        protected byte signal;  // 0 - through
38                                                        // 1 - left
39                                                        // 2 - right
40       
41        protected byte state;   //3 bits describing the state of the car
42                                                        // = 0 - ok
43                                                        // = 1 - damaged
44                                                        // = 2 - crashed
45                                                        // = 3 - promiscuous mode
46       
47        // added for simulator:
48        protected double offset;
49        protected byte lane;
50       
51        protected int vehicleId;
52
53        public CarInfo() {
54        }
55        public CarInfo(double speed, short roadIdx, short pointIdx, byte direction, double offset, byte lane) {
56                this.speed = speed; 
57                this.roadIdx = roadIdx;
58                this.pointIdx = pointIdx;
59                this.direction = direction;
60                this.offset = offset;
61                this.lane = lane;
62        }
63        public CarInfo(double speed, short roadIdx, short pointIdx, byte infoByte) {
64                this.speed = speed; 
65                this.roadIdx = roadIdx;
66                this.pointIdx = pointIdx;
67                parseInfoByte(infoByte);
68        }
69       
70        public CarInfo(CarInfo x) 
71        {
72                this.vehicleId = x.vehicleId;
73                this.speed = x.speed;
74                this.roadIdx = x.roadIdx;
75                this.pointIdx = x.pointIdx;
76                this.direction = x.direction;
77                this.vpoz = x.vpoz;
78                this.hpoz = x.hpoz;
79                this.state = x.state;
80                this.offset = x.offset;
81                this.lane = x.lane;
82
83
84        }
85
86//      public boolean equals(Object arg0) {
87//              CarInfo ci = (CarInfo)arg0;
88//              return (roadIdx == ci.getRoadIdx() && pointIdx == ci.getPointIdx());
89//      }
90       
91        public void setPoz(double sin, double cos){
92        //determine hpoz and vpoz related to the point on the road,
93        //having the angle between the segment AB (A crt point on the road obtained with peano keys,
94        //B real position of the car) and segment BC (C previuos point on the road)
95                double cos60 = 0.5;
96                double cos30 = java.lang.Math.sqrt(3)/2;
97
98                if (sin > 0)
99                        hpoz = 2;       //right
100                else
101                        hpoz = 0;       //left
102               
103/*              if (cos > cos30){
104                        vpoz = 0;       //behind
105                        hpoz = 1;       //center
106                }
107                if (cos < cos30 && cos > cos60){
108                        vpoz = 0;       //behind
109                        if (sin > 0)
110                                hpoz = 2;       //right
111                        else
112                                hpoz = 0;       //left
113                }
114                if (cos > - cos60 && cos < cos60){
115                        vpoz = 1;       //center
116                        if (sin > 0)
117                                hpoz = 2;       //right
118                        else
119                                hpoz = 0;       //left
120                }
121                if (cos > - cos30 && cos < - cos60){
122                        vpoz = 2;       //in front
123                        if (sin > 0)
124                                hpoz = 2;       //right
125                        else
126                                hpoz = 0;       //left
127                }
128                if (cos < - cos30){
129                        vpoz = 2;       //in front
130                        hpoz = 1;       //center
131                }*/
132               
133        }
134       
135        public void parseInfoByte(byte b)
136        {
137                this.direction = (byte)((b & 0x80) >> 7);
138//              this.vpoz = (byte)((b & 0x60) >> 5);
139                this.signal = (byte)((b & 0x60) >> 5);
140                this.hpoz = (byte)((b & 0x18) >> 3);
141                this.state = (byte)(b & 0x7);
142        }
143       
144        public byte createInfoByte(){
145                byte b = 0;
146                b = (byte)((this.direction << 7) + 
147//                      (this.vpoz << 5) +
148                        (this.signal << 5) +
149                        (this.hpoz << 3) +
150                        (this.state));
151
152                return b;
153        }
154       
155        public byte getDirection() {
156                return direction;
157        }
158
159        public void setDirection(byte direction) {
160                this.direction = direction;
161        }
162
163        public byte getHpoz() {
164                return hpoz;
165        }
166
167        public void setHpoz(byte hpoz) {
168                this.hpoz = hpoz;
169        }
170
171        public short getPointIdx() {
172                return pointIdx;
173        }
174
175        public void setPointIdx(short pointIdx) {
176                this.pointIdx = pointIdx;
177        }
178
179        public short getRoadIdx() {
180                return roadIdx;
181        }
182        public void setRoadIdx(short roadIdx) {
183                this.roadIdx = roadIdx;
184        }
185        public double getSpeed() {
186                return speed;
187        }
188        public void setSpeed(double speed) {
189                this.speed = speed;
190        }
191        public byte getVpoz() {
192                return vpoz;
193        }
194        public void setVpoz(byte vpoz) {
195                this.vpoz = vpoz;
196        }
197        public byte getState() {
198                return state;
199        }
200        public void setState(byte state) {
201                this.state = state;
202        }
203       
204        public long getTimestamp() {
205                return timestamp;
206        }
207        public void setTimestamp(long timestamp) {
208                this.timestamp = timestamp;
209        }
210
211
212        // added:
213       
214        public double getOffset() {
215                return offset;
216        }
217
218        public void setOffset(double offset) {
219                this.offset = offset;
220        }
221        public byte getLane() {
222                return lane;
223        }
224        public void setLane(byte lane) {
225                this.lane = lane;
226        }
227       
228        public String toString(){
229                return vehicleId+" - " + "ri:"+roadIdx+", pi:"+pointIdx+", of:"+offset+", ln:"+lane;
230        } 
231
232        public byte[] toBytes(){
233                ByteBuffer bb = ByteBuffer.allocate(Globals.NONAGG_RECORD_SIZE);
234               
235                bb.putInt(this.vehicleId);
236                bb.putLong(this.timestamp);
237                bb.put((byte)this.speed);
238                bb.putShort(this.roadIdx);
239                bb.putShort(this.pointIdx);
240                bb.put(createInfoByte());
241                bb.put(this.lane);
242                bb.putDouble(this.offset);
243                return bb.array();
244        }
245
246        public void wrap(byte buffer[]){
247               
248                ByteBuffer bb = ByteBuffer.wrap(buffer);
249               
250                this.vehicleId = bb.getInt();
251                this.timestamp = bb.getLong();
252                this.speed = bb.get();
253                this.roadIdx = bb.getShort();
254                this.pointIdx = bb.getShort();
255                byte infoByte = bb.get();
256                parseInfoByte(infoByte);
257                this.lane = bb.get();
258                this.offset = bb.getDouble();
259        }
260        public void wrap(byte buffer[], int index){
261               
262                ByteBuffer bb = ByteBuffer.wrap(buffer, index, buffer.length - index);
263               
264                this.vehicleId = bb.getInt();
265                this.timestamp = bb.getLong();
266                this.speed = bb.get();
267                this.roadIdx = bb.getShort();
268                this.pointIdx = bb.getShort();
269                byte infoByte = bb.get();
270                parseInfoByte(infoByte);
271                this.lane = bb.get();
272                this.offset = bb.getDouble();
273        }
274       
275       
276        public int getVehicleId() {
277                return vehicleId;
278        }
279        public void setVehicleId(int vehicleId) {
280                this.vehicleId = vehicleId;
281        }
282        public boolean equals(Object arg0) {
283                if (!(arg0 instanceof CarInfo)) return false;
284                CarInfo sci = (CarInfo)arg0;
285                return (vehicleId == sci.getVehicleId());
286        }
287        public byte getSignal() {
288                return signal;
289        }
290        public void setSignal(byte signal) {
291                this.signal = signal;
292        }
293
294}
Note: See TracBrowser for help on using the repository browser.