StatelessMotorComponent.java 7.46 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
/*
 * 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.EnergyComponent;
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
 */
public class StatelessMotorComponent implements EnergyComponent {

	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<>();
	
		
	public StatelessMotorComponent(int numberOfActuators, double volt) {			
		this.volts = volt;		
		this.numberOfActuators = numberOfActuators;				
		this.state = componentState.OFF;
		this.lastEnergyConsumationTime = Time.getCurrentTime();
			
		thrust = 0;
		amps = 0;
		energyState = new DefaultEnergyState("OFF", 0);
	}
	
	/**
	 * Get the maximum thrust provided by this component.
	 * @return
	 */
	public double getMaxThrust() {
		return characteristics.getLast().getThrust();
	}
	
	/**
	 * 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);
		
		System.out.println("consumed energy : " + (cons/uJconversionFactor) + " in " + Time.getFormattedTime(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) {
		
		if(targetThrust == 0 || targetThrust <= characteristics.getFirst().getThrust()) {
			setLoad(characteristics.getFirst());
		}
		else if(targetThrust >= characteristics.getLast().getThrust()) {
			setLoad(characteristics.getLast());
		}
		else {
			approximateThrustCurrentRelation(targetThrust);
		}
		
		System.out.println("Now providing Thrust: " + this.thrust + " N");
		
		return this.thrust;
	}
	
	/**
	 * 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
	 */
	private void approximateThrustCurrentRelation(double targetThrust) {
		
		MotorCharacteristic lower = null, upper = null;
		
		// find the lower and upper bounding characteristics
		for (MotorCharacteristic ch : characteristics) {
			//
			if(ch.getThrust() == targetThrust) {
				setLoad(ch);
				return;
			}
			else {
				// list is sorted, lower bound is the biggest that is lower
				if(ch.getThrust() < targetThrust) {
					lower = ch;
				}
				// the first that is greater is used as upper bound
				else if(ch.getThrust() > targetThrust) {
					upper = ch;
					break;
				}
			}
		}
		
		if(upper == null || lower == null) {
			throw new UnsupportedOperationException("Lower or upper bounds cannot be null");
		}
		
		if(upper.getThrust() < targetThrust || lower.getThrust() > targetThrust) {
			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)
		 */		
		double delta = (targetThrust - lower.getThrust())/(upper.getThrust() - lower.getThrust());
		
		double calculatedAmps = lower.getCurrent() + delta * (upper.getCurrent() - lower.getCurrent());
		
		System.out.println(targetThrust + " N targeted => " + calculatedAmps + " A calculated | " + delta + " <> " + lower + " " + upper);
		
		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;
	}
	
	
	
}