StatefulActuatorComponent.java 4.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright (c) 2005-2010 KOM � Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.energy.components;

23

24
import de.tud.kom.p2psim.api.energy.Battery;
25
26
27
28
29
30
31
import de.tud.kom.p2psim.api.energy.ComponentType;
import de.tud.kom.p2psim.api.energy.EnergyEventListener;
import de.tud.kom.p2psim.api.energy.EnergyState;
import de.tud.kom.p2psim.impl.energy.DefaultEnergyState;
import de.tudarmstadt.maki.simonstrator.api.Time;


32
/**
Julian Zobel's avatar
Julian Zobel committed
33
 * Energy component for actuators, representing the electrical consumers of actuators.
34
 * This models uses several states, that define the used energy. 
35
36
37
38
 * 
 * @author Julian Zobel
 * @version 1.0, 11.09.2018
 */
39
public class StatefulActuatorComponent implements ActuatorComponent {
40
41
42
43
44
45
	
	/**
	 * States supported by this energy component
	 * 
	 * TODO More states reflecting a more accurate energy consumption?
	 */
46
	public final EnergyState OFF, FLY, MAX;
47
48
49
50
51
	
	private EnergyState currentState;
	
	private EnergyEventListener energyModel;
	
52
53
54
	private long lastEnergyConsumationEvent;
	
	private double actuatorLoad;
55
		
56
	public StatefulActuatorComponent(int numberOfActuators, double volt, double hoverAmp, double maxAmp) {
57
		OFF = new DefaultEnergyState("OFF", 0);		
58
59
		FLY = new DefaultEnergyState("FLY", numberOfActuators * (hoverAmp * volt) * Battery.uJconverison);		
		MAX = new DefaultEnergyState("MAX", numberOfActuators * (maxAmp * volt) * Battery.uJconverison);
60
61
		
		this.currentState = OFF;
62
63
		this.lastEnergyConsumationEvent = Time.getCurrentTime();
		this.actuatorLoad = 0;
64
65
	}
	
66
	public void doStateChange(EnergyState newState) {		
67
		long timeSpentInState = Time.getCurrentTime() - lastEnergyConsumationEvent;
68
		
69
70
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeSpentInState));			
		
71
		currentState = newState;		
72
		lastEnergyConsumationEvent = Time.getCurrentTime();				
73
74
	}
	
75
76
77
	@Override
	public double calculateEnergyConsumation(EnergyState state, long timeInState) {
		if(state.equals(FLY)) {
Julian Zobel's avatar
Julian Zobel committed
78
			double consumationDelta = MAX.getEnergyConsumption() - FLY.getEnergyConsumption();			
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
			double consumation = FLY.getEnergyConsumption() + consumationDelta * actuatorLoad;
			return consumation * ( (double) timeInState / (double) Time.SECOND);
		}
		else
			return state.getEnergyConsumption() * ( (double) timeInState / (double) Time.SECOND);
	}
	
	public void useActuator(double load) {
		if(load < 0 || load > 1.0) {
			throw new AssertionError("Actuator load must be between 0 and 1!");
		}
		else {			
			this.actuatorLoad = load;
			doStateChange(FLY);
		}
	}
	
96
97
98
99
100
101
	/**
	 * Estimates the energy consumption based on a given load.
	 * @param load: Actuator load between 0.0 and 1.0
	 * @return Energy consumption in J/s
	 */
	public double estimateEnergyConsumptionWatt(double load) {
102
103
104
		double consumationDelta = MAX.getEnergyConsumption() - FLY.getEnergyConsumption();			
		double estimation = FLY.getEnergyConsumption() + consumationDelta * load;
		
Julian Zobel's avatar
Julian Zobel committed
105
106
107
//		System.out.println("MAX " + ((MAX.getEnergyConsumption() / 14.8) /  Battery.uJconverison));
//		System.out.println("MIN" + ((FLY.getEnergyConsumption() / 14.8) /  Battery.uJconverison));
//		System.out.println(load);
108
109
		// this estimation is in uJ, but has to return J, thus, do conversion
		return estimation / Battery.uJconverison;
110
111
	}
	
112
113
	@Override
	public void eventOccurred(Object content, int type) {
114
		// TODO Auto-generated method stub		
115
116
117
118
119
120
121
122
	}

	@Override
	public ComponentType getType() {	
		return ComponentType.ACTUATOR;
	}

	@Override
123
124
125
	public boolean turnOff() {
		doStateChange(OFF);
		return true;
126
127
128
129
	}

	@Override
	public boolean turnOn() {
130
131
		if (isAvailable()) {
			if (currentState.equals(OFF)) {
132
				doStateChange(FLY);
133
134
135
			}			
			return true;
		}
136
137
138
139
		return false;
	}
	
	public boolean isAvailable() {
140
141
		if (energyModel.componentCanBeActivated(this))			
				return true;
142
143
144
145
146
		return false;
	}

	@Override
	public boolean isOn() {
147
		if(!currentState.equals(OFF) && isAvailable()) 
148
149
150
		{
			return true;			
		}
151
		return false;
152
	}
153
	
154
155
156
157
158
159
160
161
	@Override
	public void setEnergyEventListener(EnergyEventListener listener) {
		energyModel = listener;		
	}

	public EnergyState getCurrentState() {
		return currentState;
	}
162
163
	
	
164
}