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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 * 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;
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
 */
41
public class StatelessActuatorComponent implements ActuatorComponent {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

	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<>();
	
		
61
	public StatelessActuatorComponent(int numberOfActuators, double volt) {			
62
63
64
65
		this.volts = volt;		
		this.numberOfActuators = numberOfActuators;				
		this.state = componentState.OFF;
		this.lastEnergyConsumationTime = Time.getCurrentTime();
66
		
67
68
69
70
71
72
73
74
75
76
		thrust = 0;
		amps = 0;
		energyState = new DefaultEnergyState("OFF", 0);
	}
	
	/**
	 * Get the maximum thrust provided by this component.
	 * @return
	 */
	public double getMaxThrust() {
77
		return characteristics.getLast().getThrust() * numberOfActuators;
78
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
	}
	
	/**
	 * 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) {
		
116
		if(targetThrust == 0 || targetThrust <= numberOfActuators * characteristics.getFirst().getThrust()) {
117
118
			setLoad(characteristics.getFirst());
		}
119
		else if(targetThrust >= numberOfActuators * characteristics.getLast().getThrust()) {
120
121
122
			setLoad(characteristics.getLast());
		}
		else {
123
			calculateAndSetThrustRelatedAmpereDraw(targetThrust);
124
		}
Julian Zobel's avatar
Julian Zobel committed
125
				
126
127
128
		return this.thrust;
	}
	
129
130
131
	/**
	 * 
	 * @param targetThrust
132
	 * @return The power consumption for the target thrust in Watt
133
134
	 */
	public double estimatePowerConsumptionWatt(double targetThrust) {
135
		if(targetThrust == 0 || targetThrust <= numberOfActuators * characteristics.getFirst().getThrust()) {
136
137
138
			// not allowed
			return Double.NaN;
		}
139
		else if(targetThrust > numberOfActuators * characteristics.getLast().getThrust()) {
140
141
142
143
144
145
			// not allowed
			return Double.NaN;
		}
		else {			
			double amps = approximateAmpereDraw(targetThrust);
			
146
147
			System.out.println(amps);
			
148
149
150
151
152
			return numberOfActuators * amps * volts;
		}
	}
	
	
153
154
155
156
157
158
	/**
	 * 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
159
	 * @return the approximated ampere draw
160
	 */
161
	private double approximateAmpereDraw(double targetThrust) {
162
163
164
165
166
167
		
		MotorCharacteristic lower = null, upper = null;
		
		// find the lower and upper bounding characteristics
		for (MotorCharacteristic ch : characteristics) {
			//
168
			if(ch.getThrust() * numberOfActuators == targetThrust) {
169
				return ch.getCurrent();
170
171
172
			}
			else {
				// list is sorted, lower bound is the biggest that is lower
173
				if(ch.getThrust() * numberOfActuators < targetThrust) {
174
175
176
					lower = ch;
				}
				// the first that is greater is used as upper bound
177
				else if(ch.getThrust() * numberOfActuators > targetThrust) {
178
179
180
181
182
183
184
185
186
187
					upper = ch;
					break;
				}
			}
		}
		
		if(upper == null || lower == null) {
			throw new UnsupportedOperationException("Lower or upper bounds cannot be null");
		}
		
188
		if(upper.getThrust() * numberOfActuators < targetThrust || lower.getThrust() * numberOfActuators > targetThrust) {
189
190
191
192
193
194
195
			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)
		 */		
196
		double delta = (targetThrust - (lower.getThrust() * numberOfActuators))/(numberOfActuators * (upper.getThrust() - lower.getThrust()));		
197
198
199
200
201
202
203
204
205
206
207
		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);			
208
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
		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;
	}
	
	
	
}