/************************************************************************************ * 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.network; import java.io.Serializable; import java.util.List; import vnsim.applications.adaptiveTL.WirelessTrafficLight; import vnsim.core.Communicator; import vnsim.core.events.*; import vnsim.map.object.*; import vnsim.map.utils.*; public class RadioDev implements Serializable{ /** serialVersionUID */ private static final long serialVersionUID = -5883719321862303634L; public static final long LIGHT_SPEED = 299792458; // m/s public static final long FREQUENCY = 2400000000L; //Hz public static final double LAMBDA = (double)LIGHT_SPEED/FREQUENCY; private Communicator communicator; // height of the antenna in m private double antennaHeight; // radio device sensitivity in DBm (decibel referenced to 1 mW power) public double sensitivity; public double rxThreshold; public double rxThreshold_W; public double snrThreshold; // transmission power in DBm public double txPower; // antenna gain in DBm public double gain; public int lastTxTime = 0; public RadioDev(Communicator c){ communicator = c; antennaHeight = 1.5; // m sensitivity = -91.0; //DBm snrThreshold = 9.1; rxThreshold = -81.0; // DBm rxThreshold_W = convertDBmToW(rxThreshold); txPower = 15; //DBm //30 + 10 * Math.log10(0.1); gain = 0; // DBm } public ReceiveEvent receive(List receivedSignals){ if (Globals.engine.crtTime == communicator.getRadio().lastTxTime){ // ignore all signals, receiver radio in transmit mode return null; } int capturableSignals = 0; ReceiveEvent max = null; double maxrxpower = -1000; double maxrxpower_W = 0; double totalPower = 0; double totalPower_W = 0; for (ReceiveEvent re : receivedSignals){ RadioDev txradio = re.sender.getRadio(); Road r1 = (Road) Globals.map.roads.get(re.sender.getRoadIdx()); Point p1 = (Point) r1.points.get(re.sender.getPointIdx()); Road r2 = (Road) Globals.map.roads.get(communicator.getRoadIdx()); Point p2 = (Point) r2.points.get(communicator.getPointIdx()); double distance = GPSutil.distance(p1, p2); double pl = pathloss(re.sender, communicator, distance); double rxpower = txradio.getTxPower() - pl; double rxpower_W = convertDBmToW(rxpower); totalPower += rxpower; totalPower_W += rxpower_W; if (rxpower > maxrxpower){ maxrxpower = rxpower; maxrxpower_W = rxpower_W; max = re; } // if (distance > 0.7) // System.out.println("DIST="+distance+"; RXPOWER="+rxpower+"; THR="+rxThreshold); if (rxpower >= this.rxThreshold){ capturableSignals ++; if (distance > Globals.maxRange ) Globals.maxRange = distance; } } Globals.packets += capturableSignals; double noise_W = totalPower_W - convertDBmToW(maxrxpower); if (capturableSignals > 1){ int i = 0; i++; } if (maxrxpower_W >= this.rxThreshold_W && maxrxpower_W >= noise_W * this.snrThreshold){ Globals.lost += capturableSignals - 1; return max; } if (capturableSignals == 1){ Globals.interfered ++ ; } if (capturableSignals > 0){ // collision Globals.collided += capturableSignals; return null; } // only weak signals return null; } // Two ray pathloss public static double pathloss(Communicator s, Communicator d, double distance){ RadioDev tx = s.getRadio(); RadioDev rx = d.getRadio(); distance *= 1000; if (distance < 1) return 0; double pathloss = -(tx.getGain() + rx.getGain()); double freeSpace = (4.0 * Math.PI * distance) / LAMBDA; double planeEarth = (distance * distance) / (tx.antennaHeight * rx.antennaHeight); if (planeEarth > freeSpace){ pathloss += 20.0 * Math.log10(planeEarth); }else{ pathloss += 20.0 * Math.log10(freeSpace); } return pathloss; } /** * * @param dbm power value in DBm * @return power in mW */ public static double convertDBmToW(double dbm){ return Math.pow(10, (dbm - 30) / 10); } /** * * @param w power in watts * @return value in DBm */ public static double convertWToDBm(double w){ return 30 + Math.log10(w); } public double getGain() { return gain; } public void setGain(double gain) { this.gain = gain; } public double getSensitivity() { return sensitivity; } public void setSensitivity(double sensitivity) { this.sensitivity = sensitivity; } public double getTxPower() { return txPower; } public void setTxPower(double txPower) { this.txPower = txPower; } public void setRadioTxMode(){ lastTxTime = Globals.engine.crtTime; } }