StatefulActuatorComponent.java 5.54 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, HOVER, MIN, 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 minAmp, double maxAmp) {
57
		OFF = new DefaultEnergyState("OFF", 0);		
58
59
		HOVER = new DefaultEnergyState("HOVER", numberOfActuators * (hoverAmp * volt) * Battery.uJconverison);	
		MIN = new DefaultEnergyState("MIN", numberOfActuators * (minAmp * volt) * Battery.uJconverison);		
60
		MAX = new DefaultEnergyState("MAX", numberOfActuators * (maxAmp * volt) * Battery.uJconverison);
61
62
		
		this.currentState = OFF;
63
64
		this.lastEnergyConsumationEvent = Time.getCurrentTime();
		this.actuatorLoad = 0;
65
66
	}
	
67
	public void doStateChange(EnergyState newState) {		
68
		long timeSpentInState = Time.getCurrentTime() - lastEnergyConsumationEvent;
69
		
70
71
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeSpentInState));			
		
72
		currentState = newState;		
73
		lastEnergyConsumationEvent = Time.getCurrentTime();				
74
75
	}
	
76
77
	@Override
	public double calculateEnergyConsumation(EnergyState state, long timeInState) {
78
79
80
81
82
83
84
85
86
87
		if(state.equals(HOVER)) {			
			return HOVER.getEnergyConsumption() * ( (double) timeInState / (double) Time.SECOND);
		}
		else if(state.equals(MIN)) {		
			double consumationDelta = MAX.getEnergyConsumption() - MIN.getEnergyConsumption();			
			double consumation = MIN.getEnergyConsumption() + consumationDelta * actuatorLoad;
			return consumation * ( (double) timeInState / (double) Time.SECOND);
		}
		else if(state.equals(MAX)) {
			double consumation = 0.5 * MAX.getEnergyConsumption() + MAX.getEnergyConsumption() * actuatorLoad;
88
89
90
91
92
93
94
			return consumation * ( (double) timeInState / (double) Time.SECOND);
		}
		else
			return state.getEnergyConsumption() * ( (double) timeInState / (double) Time.SECOND);
	}
	
	public void useActuator(double load) {
95
96
97
98
		if(load < 0) {
			throw new AssertionError("Actuator load cannot be < 0.0!");
		}		
		else {
99
			this.actuatorLoad = load;
100
101
102
103
104
105
106
107
108
			if(load == 0) {			
				doStateChange(HOVER);
			}
			else if(load <= 1.0) {		
				doStateChange(MIN);
			}
			else if(load > 1.0) {
				doStateChange(MAX);
			}
109
110
111
		}
	}
	
112
113
114
115
116
117
	/**
	 * 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) {
118
119
120
121
122
123
124
125
126
127
128
129
130
131
		
		double estimation = -1;
		
		if(load == 0) {			
			estimation = HOVER.getEnergyConsumption();
		}
		else if(load <= 1.0) {		
			double consumationDelta = MAX.getEnergyConsumption() - MIN.getEnergyConsumption();			
			estimation = MIN.getEnergyConsumption() + consumationDelta * load;
		}
		else if(load > 1.0) {
			estimation = 0.5 * MAX.getEnergyConsumption() + MAX.getEnergyConsumption() * load;
		}
	
132
		
Julian Zobel's avatar
Julian Zobel committed
133
134
135
//		System.out.println("MAX " + ((MAX.getEnergyConsumption() / 14.8) /  Battery.uJconverison));
//		System.out.println("MIN" + ((FLY.getEnergyConsumption() / 14.8) /  Battery.uJconverison));
//		System.out.println(load);
136
137
		// this estimation is in uJ, but has to return J, thus, do conversion
		return estimation / Battery.uJconverison;
138
139
	}
	
140
141
	@Override
	public void eventOccurred(Object content, int type) {
142
		// TODO Auto-generated method stub		
143
144
145
146
147
148
149
150
	}

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

	@Override
151
152
153
	public boolean turnOff() {
		doStateChange(OFF);
		return true;
154
155
156
157
	}

	@Override
	public boolean turnOn() {
158
159
		if (isAvailable()) {
			if (currentState.equals(OFF)) {
160
				doStateChange(MIN);
161
162
163
			}			
			return true;
		}
164
165
166
167
		return false;
	}
	
	public boolean isAvailable() {
168
169
		if (energyModel.componentCanBeActivated(this))			
				return true;
170
171
172
173
174
		return false;
	}

	@Override
	public boolean isOn() {
175
		if(!currentState.equals(OFF) && isAvailable()) 
176
177
178
		{
			return true;			
		}
179
		return false;
180
	}
181
	
182
183
184
185
186
187
188
189
	@Override
	public void setEnergyEventListener(EnergyEventListener listener) {
		energyModel = listener;		
	}

	public EnergyState getCurrentState() {
		return currentState;
	}
190
191
	
	
192
}