From 7e864bd3c75f6e064ebc70ff47ac6b4258ac421c Mon Sep 17 00:00:00 2001 From: fros4943 Date: Tue, 28 Oct 2008 12:30:48 +0000 Subject: [PATCH] updated interfaces to new polling format. --- .../cooja/interfaces/ApplicationRadio.java | 9 +- .../se/sics/cooja/interfaces/Battery.java | 99 +++++++------------ .../se/sics/cooja/interfaces/IPAddress.java | 8 +- .../cooja/interfaces/PolledAfterAllTicks.java | 4 +- .../se/sics/cooja/interfaces/Position.java | 61 +++++------- 5 files changed, 67 insertions(+), 114 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/interfaces/ApplicationRadio.java b/tools/cooja/java/se/sics/cooja/interfaces/ApplicationRadio.java index 76bc850d7..a16210f76 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/ApplicationRadio.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/ApplicationRadio.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ApplicationRadio.java,v 1.4 2008/03/18 13:03:24 fros4943 Exp $ + * $Id: ApplicationRadio.java,v 1.5 2008/10/28 12:30:48 fros4943 Exp $ */ package se.sics.cooja.interfaces; @@ -45,9 +45,9 @@ import se.sics.cooja.*; * radio functionality. Supports radio channels and output power functionality. * The mote should observe the radio for incoming radio packet data. * - * @author Fredrik Osterlind + * @author Fredrik Österlind */ -public class ApplicationRadio extends Radio { +public class ApplicationRadio extends Radio implements PolledBeforeActiveTicks { private Mote myMote; private static Logger logger = Logger.getLogger(ApplicationRadio.class); @@ -244,9 +244,6 @@ public class ApplicationRadio extends Radio { } } - public void doActionsAfterTick() { - } - public JPanel getInterfaceVisualizer() { // Location JPanel panel = new JPanel(); diff --git a/tools/cooja/java/se/sics/cooja/interfaces/Battery.java b/tools/cooja/java/se/sics/cooja/interfaces/Battery.java index 1d631cd32..b13949d84 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/Battery.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/Battery.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, Swedish Institute of Computer Science. + * Copyright (c) 2008, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,13 +26,12 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Battery.java,v 1.4 2007/01/09 10:02:44 fros4943 Exp $ + * $Id: Battery.java,v 1.5 2008/10/28 12:30:48 fros4943 Exp $ */ package se.sics.cooja.interfaces; import java.util.*; - import javax.swing.*; import org.apache.log4j.Logger; import org.jdom.Element; @@ -41,27 +40,25 @@ import se.sics.cooja.*; /** * A Battery represents the energy source for a mote. This implementation has no - * connection with any underlying simulated software, hence a mote does not know - * the current energy levels. + * connection with underlying simulated software: a mote does not know about energy. *

- * This Battery decreases current energy left each tick depending on the current - * mote state. If the mote is sleeping all passive interfaces' energy - * consumptions will be summed up and detracted from the current energy. If the - * mote is awake both the active and passive interfaces' energy consumptions - * will be used. Also, the energy used by the CPU (depends on mote state) will - * be detracted each tick. + * This Battery updates energy after each mote tick: + * the energy consumption of each interface is summed up. + * In addtion, the energy used by the CPU (depends on mote state) is + * detracted each tick. *

- * This observable is changed and notifies observers every time the energy left - * is changed. When current energy left has decreased below 0 the mote state is - * set to dead. - * + * + * This observable notifies every tick (!). + * + * When energy left is below 0 the mote is dead. + * * @see MoteInterface * @see MoteInterface#energyConsumptionPerTick() - * - * @author Fredrik Osterlind + * + * @author Fredrik Österlind */ @ClassDescription("Battery") -public class Battery extends MoteInterface implements PassiveMoteInterface { +public class Battery extends MoteInterface implements PolledAfterAllTicks { /** * Approximate energy consumption of a mote's CPU in active mode (mA). ESB @@ -91,10 +88,10 @@ public class Battery extends MoteInterface implements PassiveMoteInterface { private double myEnergy; private boolean hasInfiniteEnergy; private double lastEnergyConsumption = 0; - + /** * Creates a new battery connected to given mote. - * + * * @see #INITIAL_ENERGY * @param mote * Mote holding battery @@ -111,62 +108,37 @@ public class Battery extends MoteInterface implements PassiveMoteInterface { Battery.class, "INFINITE_ENERGY_bool"); if (energyConsumptionAwakePerTick < 0) { - energyConsumptionAwakePerTick = ENERGY_CONSUMPTION_AWAKE_mA - * mote.getSimulation().getTickTimeInSeconds(); - energyConsumptionLPMPerTick = ENERGY_CONSUMPTION_LPM_mA - * mote.getSimulation().getTickTimeInSeconds(); + energyConsumptionAwakePerTick = ENERGY_CONSUMPTION_AWAKE_mA * 0.001; + energyConsumptionLPMPerTick = ENERGY_CONSUMPTION_LPM_mA * 0.001; } this.mote = mote; myEnergy = INITIAL_ENERGY; } - public void doActionsBeforeTick() { - // Nothing to do - } - public void doActionsAfterTick() { lastEnergyConsumption = 0; - + // If infinite energy, do nothing - if (hasInfiniteEnergy) + if (hasInfiniteEnergy) { return; + } // If mote is dead, do nothing - if (mote.getState() == Mote.State.DEAD) + if (mote.getState() == Mote.State.DEAD) { return; - - // Check mote state - if (mote.getState() == Mote.State.LPM) { - // Mote is sleeping. Sum up energy usage. - double totalEnergyConsumption = 0.0; - totalEnergyConsumption += energyConsumptionLPMPerTick; - - for (MoteInterface passiveInterface : mote.getInterfaces() - .getAllPassiveInterfaces()) { - totalEnergyConsumption += passiveInterface.energyConsumptionPerTick(); - } - - decreaseEnergy(totalEnergyConsumption); - lastEnergyConsumption += totalEnergyConsumption; - } else { - // Mote is awake. Sum up energy usage. - double totalEnergyConsumption = 0.0; - totalEnergyConsumption += energyConsumptionAwakePerTick; - - for (MoteInterface activeInterface : mote.getInterfaces() - .getAllActiveInterfaces()) { - totalEnergyConsumption += activeInterface.energyConsumptionPerTick(); - } - for (MoteInterface passiveInterface : mote.getInterfaces() - .getAllPassiveInterfaces()) { - totalEnergyConsumption += passiveInterface.energyConsumptionPerTick(); - } - - decreaseEnergy(totalEnergyConsumption); - lastEnergyConsumption += totalEnergyConsumption; } + double totalEnergyConsumption = 0.0; + + totalEnergyConsumption += energyConsumptionLPMPerTick; + for (MoteInterface intf : mote.getInterfaces().getInterfaces()) { + totalEnergyConsumption += intf.energyConsumptionPerTick(); + } + + decreaseEnergy(totalEnergyConsumption); + lastEnergyConsumption += totalEnergyConsumption; + // Check if we are out of energy if (getCurrentEnergy() <= 0.0) { setChanged(); @@ -176,8 +148,7 @@ public class Battery extends MoteInterface implements PassiveMoteInterface { } /** - * @param inf - * Set infinite energy state + * @param inf Infinite energy */ public void setInfiniteEnergy(boolean inf) { hasInfiniteEnergy = inf; @@ -187,7 +158,7 @@ public class Battery extends MoteInterface implements PassiveMoteInterface { } /** - * @return True if this battery has inifinite energy + * @return True if battery has infinite energy */ public boolean hasInfiniteEnergy() { return hasInfiniteEnergy; diff --git a/tools/cooja/java/se/sics/cooja/interfaces/IPAddress.java b/tools/cooja/java/se/sics/cooja/interfaces/IPAddress.java index 59b4aceb5..4e5964801 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/IPAddress.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/IPAddress.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: IPAddress.java,v 1.1 2006/08/21 12:12:59 fros4943 Exp $ + * $Id: IPAddress.java,v 1.2 2008/10/28 12:30:48 fros4943 Exp $ */ package se.sics.cooja.interfaces; @@ -34,9 +34,9 @@ package se.sics.cooja.interfaces; import se.sics.cooja.*; /** - * A IPAdress represents a mote Internet address. An implementation should notify all + * IP Address represents a mote Internet address. An implementation should notify all * observers if the address is set or changed. - * + * * @author Fredrik Osterlind */ @ClassDescription("IPv4 Address") @@ -47,7 +47,7 @@ public abstract class IPAddress extends MoteInterface { * @return IP address string */ public abstract String getIPString(); - + /** * Change/Set IP address. * @param ipAddress IP string on the form a.b.c.d diff --git a/tools/cooja/java/se/sics/cooja/interfaces/PolledAfterAllTicks.java b/tools/cooja/java/se/sics/cooja/interfaces/PolledAfterAllTicks.java index b6e049519..8a2641aae 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/PolledAfterAllTicks.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/PolledAfterAllTicks.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: PolledAfterAllTicks.java,v 1.1 2008/10/28 12:09:14 fros4943 Exp $ + * $Id: PolledAfterAllTicks.java,v 1.2 2008/10/28 12:30:48 fros4943 Exp $ */ package se.sics.cooja.interfaces; @@ -34,6 +34,8 @@ package se.sics.cooja.interfaces; /** * A mote interface polled after ALL mote ticks. * + * @see Battery + * * @author Fredrik Österlind */ public interface PolledAfterAllTicks { diff --git a/tools/cooja/java/se/sics/cooja/interfaces/Position.java b/tools/cooja/java/se/sics/cooja/interfaces/Position.java index bc4496c96..18f64ac99 100644 --- a/tools/cooja/java/se/sics/cooja/interfaces/Position.java +++ b/tools/cooja/java/se/sics/cooja/interfaces/Position.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Position.java,v 1.2 2007/01/09 10:02:44 fros4943 Exp $ + * $Id: Position.java,v 1.3 2008/10/28 12:30:48 fros4943 Exp $ */ package se.sics.cooja.interfaces; @@ -40,14 +40,12 @@ import org.jdom.Element; import se.sics.cooja.*; /** - * A Position represents the simulated 3D position of a mote. This - * implementation has no connection with any underlying simulated software, - * hence a mote does not know its current position. + * Mote 3D position. + * *

- * This observable is changed and notifies observers whenever new coordinates - * are set. - * - * @author Fredrik Osterlind + * This observable notifies when the position is changed. + * + * @author Fredrik Österlind */ @ClassDescription("Position") public class Position extends MoteInterface { @@ -57,7 +55,7 @@ public class Position extends MoteInterface { /** * Creates a position for given mote with coordinates (x=0, y=0, z=0). - * + * * @param mote * Led's mote. * @see Mote @@ -72,14 +70,11 @@ public class Position extends MoteInterface { } /** - * Updates coordiantes of associated mote to (x,y,z). - * - * @param x - * New X coordinate - * @param y - * New Y coordinate - * @param z - * New Z coordinate + * Set position to (x,y,z). + * + * @param x New X coordinate + * @param y New Y coordinate + * @param z New Z coordinate */ public void setCoordinates(double x, double y, double z) { coords[0] = x; @@ -113,9 +108,8 @@ public class Position extends MoteInterface { /** * Calculates distance from this position to given position. - * - * @param pos - * Compared position + * + * @param pos Compared position * @return Distance */ public double getDistanceTo(Position pos) { @@ -129,43 +123,32 @@ public class Position extends MoteInterface { /** * Calculates distance from associated mote to another mote. - * - * @param m - * Another mote + * + * @param m Another mote * @return Distance */ public double getDistanceTo(Mote m) { return getDistanceTo(m.getInterfaces().getPosition()); } - public void doActionsBeforeTick() { - // Nothing to do - } - - public void doActionsAfterTick() { - // Nothing to do - } - public JPanel getInterfaceVisualizer() { - - // Location JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); final NumberFormat form = NumberFormat.getNumberInstance(); final JLabel positionLabel = new JLabel(); - positionLabel.setText("(" + form.format(getXCoordinate()) + "," - + form.format(getYCoordinate()) + "," + form.format(getZCoordinate()) - + ")"); + positionLabel.setText("x=" + form.format(getXCoordinate()) + " " + + "y=" + form.format(getYCoordinate()) + " " + + "z=" + form.format(getZCoordinate())); panel.add(positionLabel); Observer observer; this.addObserver(observer = new Observer() { public void update(Observable obs, Object obj) { - positionLabel.setText("(" + form.format(getXCoordinate()) + "," - + form.format(getYCoordinate()) + "," - + form.format(getZCoordinate()) + ")"); + positionLabel.setText("x=" + form.format(getXCoordinate()) + " " + + "y=" + form.format(getYCoordinate()) + " " + + "z=" + form.format(getZCoordinate())); } });