StatelessActuatorComponent.java 8.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
23
24
25
/*
 * 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;


import java.util.Comparator;
import java.util.LinkedList;
26

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;


/**
 * Component for devices that provide thrust, such as electrical motors for UAV propulsion. 
 * Is configured by {@link MotorCharacteristic}s with a given thrust (N) and a given current (A). 
 * Values in between given characteristics are calculated by linear interpolation.
 * 
 * @author Julian Zobel
 * @version 1.0, 05.03.2019
 */
42
public class StatelessActuatorComponent implements ActuatorComponent {
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

	public enum componentState {OFF, ON}
	
	private componentState state;
	
	private EnergyEventListener energyModel;
	
	private long lastEnergyConsumationTime;	
	
	private double volts;
	private final double uJconversionFactor = 1000000;
	private final int numberOfActuators;
	
	private double thrust;
	private double amps;
	private EnergyState energyState;
	private LinkedList<MotorCharacteristic> characteristics = new LinkedList<>();
	
		
62
	public StatelessActuatorComponent(int numberOfActuators, double volt) {			
63
64
65
66
		this.volts = volt;		
		this.numberOfActuators = numberOfActuators;				
		this.state = componentState.OFF;
		this.lastEnergyConsumationTime = Time.getCurrentTime();
67
		
68
69
70
71
72
73
74
75
76
77
		thrust = 0;
		amps = 0;
		energyState = new DefaultEnergyState("OFF", 0);
	}
	
	/**
	 * Get the maximum thrust provided by this component.
	 * @return
	 */
	public double getMaxThrust() {
78
		return characteristics.getLast().getThrust() * numberOfActuators;
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
	}
	
	/**
	 * Set the new energy state and calculate the energy consumption from the last state
	 */
	private void setEnergyState() {	
		
		// set the new energy state
		EnergyState newState;
		
		if(state == componentState.ON) {
			newState = new DefaultEnergyState("Actuator", numberOfActuators * (amps * volts) * uJconversionFactor);
		}
		else {
			newState = new DefaultEnergyState("OFF", 0);
		}
				
		// calculate energy consumption for the previous state
		long timeSpentInState = Time.getCurrentTime() - lastEnergyConsumationTime;
		
		double cons =  calculateEnergyConsumation(energyState, timeSpentInState);
		
		energyModel.componentConsumedEnergy(this, cons);			
		
		// set new state
		energyState = newState;		
		lastEnergyConsumationTime = Time.getCurrentTime();
	}
	
	/**	
	 * Request a given amount of thrust to be provided from this component. If the amount is less than the minimum 
	 * or more than the maximum, the minimum or maximum thrust values, respectively, are enforced. 
	 *  
	 * @param targetThrust
	 * @return The amount of thrust this component now generates.
	 */
	public double requestThrust(double targetThrust) {
		
117
		if(targetThrust == 0 || targetThrust <= numberOfActuators * characteristics.getFirst().getThrust()) {
118
119
			setLoad(characteristics.getFirst());
		}
120
		else if(targetThrust >= numberOfActuators * characteristics.getLast().getThrust()) {
121
122
123
			setLoad(characteristics.getLast());
		}
		else {
124
			calculateAndSetThrustRelatedAmpereDraw(targetThrust);
125
		}
Julian Zobel's avatar
Julian Zobel committed
126
				
127
128
129
		return this.thrust;
	}
	
130
131
132
	/**
	 * 
	 * @param targetThrust
133
	 * @return The power consumption for the target thrust in Watt
134
135
	 */
	public double estimatePowerConsumptionWatt(double targetThrust) {
136
		if(targetThrust == 0 || targetThrust <= numberOfActuators * characteristics.getFirst().getThrust()) {
137
138
139
			// not allowed
			return Double.NaN;
		}
140
		else if(targetThrust > numberOfActuators * characteristics.getLast().getThrust()) {
141
142
143
144
145
146
			// not allowed
			return Double.NaN;
		}
		else {			
			double amps = approximateAmpereDraw(targetThrust);
			
Niklas-Stoehr's avatar
Niklas-Stoehr committed
147
			//System.out.println(amps);
148
			
149
150
151
152
153
			return numberOfActuators * amps * volts;
		}
	}
	
	
154
155
156
157
158
159
	/**
	 * Given an amount of thrust between the minimum and maximum values, the required current 
	 * to provide this amount of thrust is calculated by linear interpolation by the nearest lower
	 * and upper {@link MotorCharacteristic}s. 
	 * 
	 * @param targetThrust
160
	 * @return the approximated ampere draw
161
	 */
162
	private double approximateAmpereDraw(double targetThrust) {
163
164
165
166
167
168
		
		MotorCharacteristic lower = null, upper = null;
		
		// find the lower and upper bounding characteristics
		for (MotorCharacteristic ch : characteristics) {
			//
169
			if(ch.getThrust() * numberOfActuators == targetThrust) {
170
				return ch.getCurrent();
171
172
173
			}
			else {
				// list is sorted, lower bound is the biggest that is lower
174
				if(ch.getThrust() * numberOfActuators < targetThrust) {
175
176
177
					lower = ch;
				}
				// the first that is greater is used as upper bound
178
				else if(ch.getThrust() * numberOfActuators > targetThrust) {
179
180
181
182
183
184
185
186
187
188
					upper = ch;
					break;
				}
			}
		}
		
		if(upper == null || lower == null) {
			throw new UnsupportedOperationException("Lower or upper bounds cannot be null");
		}
		
189
		if(upper.getThrust() * numberOfActuators < targetThrust || lower.getThrust() * numberOfActuators > targetThrust) {
190
191
192
193
194
195
196
			throw new UnsupportedOperationException("Lower or upper bound do not match");
		}
		
		/*
		 * Calculate the approximated current with the upper and lower bounds:
		 * Amp_approx = Amp_lower + (T_target - T_lower)/(T_upper - T_lower) * (Amp_upper - Amp_lower)
		 */		
197
		double delta = (targetThrust - (lower.getThrust() * numberOfActuators))/(numberOfActuators * (upper.getThrust() - lower.getThrust()));		
198
199
200
201
202
203
204
205
206
207
208
		return lower.getCurrent() + delta * (upper.getCurrent() - lower.getCurrent());
	}
	
	/**
	 * Approximates the ampere draw required forthe requested thrust
	 * 
	 * Target thrust should be strictly within the possible thrust limits
	 * 
	 */
	private void calculateAndSetThrustRelatedAmpereDraw(double targetThrust) {				
		double calculatedAmps = approximateAmpereDraw(targetThrust);			
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
		setLoad(targetThrust, calculatedAmps);
	}
	
	private void setLoad(double thrust, double amps) {
		this.thrust = thrust;
		this.amps = amps;
		setEnergyState();
	}
	
	private void setLoad(MotorCharacteristic ch) {
		this.thrust = ch.getThrust();
		this.amps = ch.getCurrent();
		setEnergyState();
	}
	
	/**
	 * Add a {@link MotorCharacteristic} for this motor. 
	 * 
	 * @param c
	 */
	public void addChar(MotorCharacteristic c) {
		
		characteristics.add(c);
	
		// sort the characteristics starting from low to high thrust
		characteristics.sort(new Comparator<MotorCharacteristic>() {
			@Override
			public int compare(MotorCharacteristic o1,
					MotorCharacteristic o2) {
				return (int) (o1.getThrust() - o2.getThrust());
			}
		});
		
	}
	
		
	@Override
	public void eventOccurred(Object content, int type) {
		// TODO Auto-generated method stub
		
	}

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

	@Override
	public boolean turnOff() {
		this.thrust = 0;
		this.amps = 0;
		this.state = componentState.OFF;
		setEnergyState();
		
		return true;
	}

	@Override
	public boolean turnOn() {
		if (isAvailable()) {
			if(this.state != componentState.ON) {
				this.state = componentState.ON;
				requestThrust(0);
			}
			return true;
		}
		return false;
	}
	
	public boolean isAvailable() {
		if (energyModel.componentCanBeActivated(this)) {	
			return true;
		}
		return false;
	}

	@Override
	public boolean isOn() {
		if(this.state != componentState.OFF && isAvailable()) {			
			return true;			
		}
		return false;
	}
	
	@Override
	public void setEnergyEventListener(EnergyEventListener listener) {
		energyModel = listener;		
	}

	public EnergyState getCurrentState() {
		return energyState;
	}
	
	
	
}