StatefulActuatorComponent.java 4.2 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
25
26
27
28
29
30
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;


31
/**
Julian Zobel's avatar
Julian Zobel committed
32
 * Energy component for actuators, representing the electrical consumers of actuators.
33
 * This models uses several states, that define the used energy. 
34
35
36
37
 * 
 * @author Julian Zobel
 * @version 1.0, 11.09.2018
 */
38
public class StatefulActuatorComponent implements ActuatorComponent {
39
40
41
42
43
44
	
	/**
	 * States supported by this energy component
	 * 
	 * TODO More states reflecting a more accurate energy consumption?
	 */
45
	public final EnergyState OFF, FLY, MAX;
46
47
48
49
50
	
	private EnergyState currentState;
	
	private EnergyEventListener energyModel;
	
51
52
53
	private long lastEnergyConsumationEvent;
	
	private double actuatorLoad;
54
		
55
	public StatefulActuatorComponent(int numberOfActuators, double volt, double hoverAmp, double maxAmp) {
56
57
58
		OFF = new DefaultEnergyState("OFF", 0);		
		FLY = new DefaultEnergyState("FLY", numberOfActuators * (hoverAmp * volt) * 1000000);		
		MAX = new DefaultEnergyState("MAX", numberOfActuators * (maxAmp * volt) * 1000000);
59
60
		
		this.currentState = OFF;
61
62
		this.lastEnergyConsumationEvent = Time.getCurrentTime();
		this.actuatorLoad = 0;
63
64
	}
	
65
	public void doStateChange(EnergyState newState) {		
66
		long timeSpentInState = Time.getCurrentTime() - lastEnergyConsumationEvent;
67
		
68
69
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeSpentInState));			
		
70
		currentState = newState;		
71
		lastEnergyConsumationEvent = Time.getCurrentTime();				
72
73
	}
	
74
75
76
	@Override
	public double calculateEnergyConsumation(EnergyState state, long timeInState) {
		if(state.equals(FLY)) {
Julian Zobel's avatar
Julian Zobel committed
77
			double consumationDelta = MAX.getEnergyConsumption() - FLY.getEnergyConsumption();			
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
			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);
		}
	}
	
95
96
97
98
99
100
101
	public double estimatePowerConsumption(double load) {
		double consumationDelta = MAX.getEnergyConsumption() - FLY.getEnergyConsumption();			
		double estimation = FLY.getEnergyConsumption() + consumationDelta * load;
		
		return estimation;
	}
	
102
103
	@Override
	public void eventOccurred(Object content, int type) {
104
		// TODO Auto-generated method stub		
105
106
107
108
109
110
111
112
	}

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

	@Override
113
114
115
	public boolean turnOff() {
		doStateChange(OFF);
		return true;
116
117
118
119
	}

	@Override
	public boolean turnOn() {
120
121
		if (isAvailable()) {
			if (currentState.equals(OFF)) {
122
				doStateChange(FLY);
123
124
125
			}			
			return true;
		}
126
127
128
129
		return false;
	}
	
	public boolean isAvailable() {
130
131
		if (energyModel.componentCanBeActivated(this))			
				return true;
132
133
134
135
136
		return false;
	}

	@Override
	public boolean isOn() {
137
		if(!currentState.equals(OFF) && isAvailable()) 
138
139
140
		{
			return true;			
		}
141
		return false;
142
	}
143
	
144
145
146
147
148
149
150
151
	@Override
	public void setEnergyEventListener(EnergyEventListener listener) {
		energyModel = listener;		
	}

	public EnergyState getCurrentState() {
		return currentState;
	}
152
153
	
	
154
}