/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.core; import java.nio.ByteBuffer; import vnsim.map.object.*; /** * Class that holds GPS & state information about a vehicle, accessibile to * the applications. * */ public class CarInfo { protected long timestamp; // can be int protected double speed; protected short roadIdx = -1; protected short pointIdx = -1; protected byte direction; // 1 - car runs on a street, from the first point // to the last one, in they are stored in the // in the road's points vector // 0 - otherwise protected byte hpoz; //0 behind, 1 center, 2 in front protected byte vpoz; //0 left, 1 center, 2 right //(position related to the point on the road to achieve //car positioning on lanes) protected byte signal; // 0 - through // 1 - left // 2 - right protected byte state; //3 bits describing the state of the car // = 0 - ok // = 1 - damaged // = 2 - crashed // = 3 - promiscuous mode // added for simulator: protected double offset; protected byte lane; protected int vehicleId; public CarInfo() { } public CarInfo(double speed, short roadIdx, short pointIdx, byte direction, double offset, byte lane) { this.speed = speed; this.roadIdx = roadIdx; this.pointIdx = pointIdx; this.direction = direction; this.offset = offset; this.lane = lane; } public CarInfo(double speed, short roadIdx, short pointIdx, byte infoByte) { this.speed = speed; this.roadIdx = roadIdx; this.pointIdx = pointIdx; parseInfoByte(infoByte); } public CarInfo(CarInfo x) { this.vehicleId = x.vehicleId; this.speed = x.speed; this.roadIdx = x.roadIdx; this.pointIdx = x.pointIdx; this.direction = x.direction; this.vpoz = x.vpoz; this.hpoz = x.hpoz; this.state = x.state; this.offset = x.offset; this.lane = x.lane; } // public boolean equals(Object arg0) { // CarInfo ci = (CarInfo)arg0; // return (roadIdx == ci.getRoadIdx() && pointIdx == ci.getPointIdx()); // } public void setPoz(double sin, double cos){ //determine hpoz and vpoz related to the point on the road, //having the angle between the segment AB (A crt point on the road obtained with peano keys, //B real position of the car) and segment BC (C previuos point on the road) double cos60 = 0.5; double cos30 = java.lang.Math.sqrt(3)/2; if (sin > 0) hpoz = 2; //right else hpoz = 0; //left /* if (cos > cos30){ vpoz = 0; //behind hpoz = 1; //center } if (cos < cos30 && cos > cos60){ vpoz = 0; //behind if (sin > 0) hpoz = 2; //right else hpoz = 0; //left } if (cos > - cos60 && cos < cos60){ vpoz = 1; //center if (sin > 0) hpoz = 2; //right else hpoz = 0; //left } if (cos > - cos30 && cos < - cos60){ vpoz = 2; //in front if (sin > 0) hpoz = 2; //right else hpoz = 0; //left } if (cos < - cos30){ vpoz = 2; //in front hpoz = 1; //center }*/ } public void parseInfoByte(byte b) { this.direction = (byte)((b & 0x80) >> 7); // this.vpoz = (byte)((b & 0x60) >> 5); this.signal = (byte)((b & 0x60) >> 5); this.hpoz = (byte)((b & 0x18) >> 3); this.state = (byte)(b & 0x7); } public byte createInfoByte(){ byte b = 0; b = (byte)((this.direction << 7) + // (this.vpoz << 5) + (this.signal << 5) + (this.hpoz << 3) + (this.state)); return b; } public byte getDirection() { return direction; } public void setDirection(byte direction) { this.direction = direction; } public byte getHpoz() { return hpoz; } public void setHpoz(byte hpoz) { this.hpoz = hpoz; } public short getPointIdx() { return pointIdx; } public void setPointIdx(short pointIdx) { this.pointIdx = pointIdx; } public short getRoadIdx() { return roadIdx; } public void setRoadIdx(short roadIdx) { this.roadIdx = roadIdx; } public double getSpeed() { return speed; } public void setSpeed(double speed) { this.speed = speed; } public byte getVpoz() { return vpoz; } public void setVpoz(byte vpoz) { this.vpoz = vpoz; } public byte getState() { return state; } public void setState(byte state) { this.state = state; } public long getTimestamp() { return timestamp; } public void setTimestamp(long timestamp) { this.timestamp = timestamp; } // added: public double getOffset() { return offset; } public void setOffset(double offset) { this.offset = offset; } public byte getLane() { return lane; } public void setLane(byte lane) { this.lane = lane; } public String toString(){ return vehicleId+" - " + "ri:"+roadIdx+", pi:"+pointIdx+", of:"+offset+", ln:"+lane; } public byte[] toBytes(){ ByteBuffer bb = ByteBuffer.allocate(Globals.NONAGG_RECORD_SIZE); bb.putInt(this.vehicleId); bb.putLong(this.timestamp); bb.put((byte)this.speed); bb.putShort(this.roadIdx); bb.putShort(this.pointIdx); bb.put(createInfoByte()); bb.put(this.lane); bb.putDouble(this.offset); return bb.array(); } public void wrap(byte buffer[]){ ByteBuffer bb = ByteBuffer.wrap(buffer); this.vehicleId = bb.getInt(); this.timestamp = bb.getLong(); this.speed = bb.get(); this.roadIdx = bb.getShort(); this.pointIdx = bb.getShort(); byte infoByte = bb.get(); parseInfoByte(infoByte); this.lane = bb.get(); this.offset = bb.getDouble(); } public void wrap(byte buffer[], int index){ ByteBuffer bb = ByteBuffer.wrap(buffer, index, buffer.length - index); this.vehicleId = bb.getInt(); this.timestamp = bb.getLong(); this.speed = bb.get(); this.roadIdx = bb.getShort(); this.pointIdx = bb.getShort(); byte infoByte = bb.get(); parseInfoByte(infoByte); this.lane = bb.get(); this.offset = bb.getDouble(); } public int getVehicleId() { return vehicleId; } public void setVehicleId(int vehicleId) { this.vehicleId = vehicleId; } public boolean equals(Object arg0) { if (!(arg0 instanceof CarInfo)) return false; CarInfo sci = (CarInfo)arg0; return (vehicleId == sci.getVehicleId()); } public byte getSignal() { return signal; } public void setSignal(byte signal) { this.signal = signal; } }