new energy method: collecting total energy consumption from each interface

This commit is contained in:
fros4943 2008-10-28 13:28:35 +00:00
parent 237de606b1
commit 5c2d5940ed

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: Battery.java,v 1.5 2008/10/28 12:30:48 fros4943 Exp $ * $Id: Battery.java,v 1.6 2008/10/28 13:28:35 fros4943 Exp $
*/ */
package se.sics.cooja.interfaces; package se.sics.cooja.interfaces;
@ -44,33 +44,33 @@ import se.sics.cooja.*;
* <p> * <p>
* This Battery updates energy after each mote tick: * This Battery updates energy after each mote tick:
* the energy consumption of each interface is summed up. * the energy consumption of each interface is summed up.
* In addtion, the energy used by the CPU (depends on mote state) is * In addition, the Battery adds the CPU energy.
* detracted each tick.
* <p> * <p>
* *
* This observable notifies every tick (!). * This observable notifies every tick (relatively time-consuming).
* *
* When energy left is below 0 the mote is dead. * When the energy left is below 0 the mote is dead.
* *
* @see MoteInterface * @see MoteInterface
* @see MoteInterface#energyConsumptionPerTick() * @see MoteInterface#energyConsumption()
* *
* @author Fredrik Österlind * @author Fredrik Österlind
*/ */
@ClassDescription("Battery") @ClassDescription("Battery")
public class Battery extends MoteInterface implements PolledAfterAllTicks { public class Battery extends MoteInterface implements PolledAfterAllTicks {
private static Logger logger = Logger.getLogger(Battery.class);
/** /**
* Approximate energy consumption of a mote's CPU in active mode (mA). ESB * Approximate energy consumption of a mote's CPU in active mode (mA). ESB
* measured energy consumption is 1.49 mA. * measured energy consumption is 1.49 mA.
*/ */
public final double ENERGY_CONSUMPTION_AWAKE_mA; public final double CPU_ENERGY_CONSUMPTION_AWAKE_mA;
/** /**
* Approximate energy consumption of a mote's CPU in low power mode (mA). ESB * Approximate energy consumption of a mote's CPU in low power mode (mA). ESB
* measured energy consumption is 1.34 mA. * measured energy consumption is 1.34 mA.
*/ */
public final double ENERGY_CONSUMPTION_LPM_mA; public final double CPU_ENERGY_CONSUMPTION_LPM_mA;
/** /**
* Initial energy of battery in milli coulomb (mQ). ESB mote: 3 regular AA * Initial energy of battery in milli coulomb (mQ). ESB mote: 3 regular AA
@ -79,46 +79,39 @@ public class Battery extends MoteInterface implements PolledAfterAllTicks {
*/ */
public final double INITIAL_ENERGY; public final double INITIAL_ENERGY;
private double energyConsumptionLPMPerTick = -1.0;
private double energyConsumptionAwakePerTick = -1.0;
private Mote mote = null; private Mote mote = null;
private static Logger logger = Logger.getLogger(Battery.class);
private double myEnergy; private double cpuEnergyConsumptionLPMPerMs;
private double cpuEnergyConsumptionAwakePerMs;
private boolean hasInfiniteEnergy; private boolean hasInfiniteEnergy;
private double lastEnergyConsumption = 0;
private double cpuEnergyConsumption = 0;
private double totalEnergyConsumption = 0;
/** /**
* Creates a new battery connected to given mote. * Creates a new battery connected to given mote.
* *
* @param mote Mote
*
* @see #energyConsumption
* @see #INITIAL_ENERGY * @see #INITIAL_ENERGY
* @param mote
* Mote holding battery
*/ */
public Battery(Mote mote) { public Battery(Mote mote) {
// Read class configurations of this mote type /* Read configuration */
ENERGY_CONSUMPTION_AWAKE_mA = mote.getType().getConfig() CPU_ENERGY_CONSUMPTION_AWAKE_mA = mote.getType().getConfig().getDoubleValue(Battery.class, "CPU_AWAKE_mA");
.getDoubleValue(Battery.class, "CPU_AWAKE_mA"); CPU_ENERGY_CONSUMPTION_LPM_mA = mote.getType().getConfig().getDoubleValue(Battery.class, "CPU_LPM_mA");
ENERGY_CONSUMPTION_LPM_mA = mote.getType().getConfig().getDoubleValue( INITIAL_ENERGY = mote.getType().getConfig().getDoubleValue(Battery.class, "INITIAL_ENERGY_mQ");
Battery.class, "CPU_LPM_mA"); hasInfiniteEnergy = mote.getType().getConfig().getBooleanValue(Battery.class, "INFINITE_ENERGY_bool");
INITIAL_ENERGY = mote.getType().getConfig().getDoubleValue(
Battery.class, "INITIAL_ENERGY_mQ");
hasInfiniteEnergy = mote.getType().getConfig().getBooleanValue(
Battery.class, "INFINITE_ENERGY_bool");
if (energyConsumptionAwakePerTick < 0) { cpuEnergyConsumptionAwakePerMs = CPU_ENERGY_CONSUMPTION_AWAKE_mA * 0.001; /* TODO Voltage */
energyConsumptionAwakePerTick = ENERGY_CONSUMPTION_AWAKE_mA * 0.001; cpuEnergyConsumptionLPMPerMs = CPU_ENERGY_CONSUMPTION_LPM_mA * 0.001; /* TODO Voltage */
energyConsumptionLPMPerTick = ENERGY_CONSUMPTION_LPM_mA * 0.001;
}
this.mote = mote; this.mote = mote;
myEnergy = INITIAL_ENERGY;
} }
public void doActionsAfterTick() { public void doActionsAfterTick() {
lastEnergyConsumption = 0;
// If infinite energy, do nothing // If infinite energy, do nothing
if (hasInfiniteEnergy) { if (hasInfiniteEnergy) {
return; return;
@ -129,22 +122,24 @@ public class Battery extends MoteInterface implements PolledAfterAllTicks {
return; return;
} }
double totalEnergyConsumption = 0.0; if (mote.getState() == Mote.State.ACTIVE) {
cpuEnergyConsumption += cpuEnergyConsumptionLPMPerMs;
totalEnergyConsumption += energyConsumptionLPMPerTick; } else {
for (MoteInterface intf : mote.getInterfaces().getInterfaces()) { cpuEnergyConsumption += cpuEnergyConsumptionAwakePerMs;
totalEnergyConsumption += intf.energyConsumptionPerTick();
} }
decreaseEnergy(totalEnergyConsumption); totalEnergyConsumption = cpuEnergyConsumption;
lastEnergyConsumption += totalEnergyConsumption; for (MoteInterface intf : mote.getInterfaces().getInterfaces()) {
totalEnergyConsumption += intf.energyConsumption();
}
// Check if we are out of energy /* Check if we are out of energy */
if (getCurrentEnergy() <= 0.0) { if (getEnergyConsumption() > INITIAL_ENERGY) {
setChanged();
notifyObservers();
mote.setState(Mote.State.DEAD); mote.setState(Mote.State.DEAD);
} }
setChanged();
notifyObservers();
} }
/** /**
@ -172,49 +167,52 @@ public class Battery extends MoteInterface implements PolledAfterAllTicks {
} }
/** /**
* @return Current energy left * @return Current energy consumption
*/ */
public double getCurrentEnergy() { public double getEnergyConsumption() {
return myEnergy; return totalEnergyConsumption;
} }
private void decreaseEnergy(double consumption) { /**
if (!hasInfiniteEnergy) { * @return Energy left ratio
myEnergy -= consumption; */
setChanged(); public double getEnergyLeftRatio() {
notifyObservers(); return ((getInitialEnergy() - getEnergyConsumption()) / getInitialEnergy());
}
} }
public JPanel getInterfaceVisualizer() { public JPanel getInterfaceVisualizer() {
// Battery energy left // Battery energy left
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
final JLabel initialLabel = new JLabel("");
final JLabel energyLabel = new JLabel(""); final JLabel energyLabel = new JLabel("");
final JLabel energyPercentLabel = new JLabel(""); final JLabel leftLabel = new JLabel("");
if (hasInfiniteEnergy()) { if (hasInfiniteEnergy()) {
energyLabel.setText("INFINITE"); initialLabel.setText("[infinite energy]");
energyPercentLabel.setText(""); energyLabel.setText("");
leftLabel.setText("");
} else { } else {
energyLabel.setText("Energy left (mQ) = " + getCurrentEnergy()); initialLabel.setText("Total energy (mQ): " + getInitialEnergy());
energyPercentLabel.setText("Energy left (%) = " energyLabel.setText("Consumed energy (mQ): " + getEnergyConsumption());
+ (getCurrentEnergy() / getInitialEnergy() * 100) + "%"); leftLabel.setText("Energy left (%): " + (getEnergyLeftRatio() * 100));
} }
panel.add(initialLabel);
panel.add(energyLabel); panel.add(energyLabel);
panel.add(energyPercentLabel); panel.add(leftLabel);
Observer observer; Observer observer;
this.addObserver(observer = new Observer() { this.addObserver(observer = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
if (hasInfiniteEnergy()) { if (hasInfiniteEnergy()) {
energyLabel.setText("INFINITE"); initialLabel.setText("[infinite energy]");
energyPercentLabel.setText(""); energyLabel.setText("");
leftLabel.setText("");
} else { } else {
energyLabel.setText("Energy left (mQ) = " + getCurrentEnergy()); initialLabel.setText("Total energy (mQ): " + getInitialEnergy());
energyPercentLabel.setText("Energy left (%) = " energyLabel.setText("Consumed energy (mQ): " + getEnergyConsumption());
+ (getCurrentEnergy() / getInitialEnergy() * 100) + "%"); leftLabel.setText("Energy left (%): " + (getEnergyLeftRatio() * 100));
} }
} }
}); });
@ -235,15 +233,10 @@ public class Battery extends MoteInterface implements PolledAfterAllTicks {
this.deleteObserver(observer); this.deleteObserver(observer);
} }
public double energyConsumptionPerTick() { public double energyConsumption() {
// The battery itself does not require any power.
return 0.0; return 0.0;
} }
public double getLastTotalEnergyConsumption() {
return lastEnergyConsumption;
}
public Collection<Element> getConfigXML() { public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>(); Vector<Element> config = new Vector<Element>();
Element element; Element element;