UAVTopologyComponent.java 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * 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/>.
 *
 */

21
package de.tud.kom.p2psim.impl.topology.component;
22

23
import java.util.LinkedList;
24
25
import java.util.Set;
import de.tud.kom.p2psim.api.common.SimHost;
26
import de.tud.kom.p2psim.api.energy.Battery;
27
28
import de.tud.kom.p2psim.api.energy.ComponentType;
import de.tud.kom.p2psim.api.energy.EnergyModel;
29
import de.tud.kom.p2psim.api.network.SimNetInterface;
30
31
import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
32
import de.tud.kom.p2psim.api.topology.movement.SimUAVLocationActuator;
33
34
import de.tud.kom.p2psim.api.topology.movement.UAVMovementModel;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
35
import de.tud.kom.p2psim.impl.energy.RechargeableBattery;
36
37
import de.tud.kom.p2psim.impl.energy.components.ActuatorComponent;
import de.tud.kom.p2psim.impl.energy.components.StatelessActuatorComponent;
38
39
import de.tud.kom.p2psim.impl.energy.models.AbstractEnergyModel;
import de.tud.kom.p2psim.impl.topology.placement.UAVBasePlacement;
40
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
41
import de.tudarmstadt.maki.simonstrator.api.Monitor;
42
import de.tudarmstadt.maki.simonstrator.api.Time;
43
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
Julian Zobel's avatar
Julian Zobel committed
44
import de.tudarmstadt.maki.simonstrator.api.component.overlay.OverlayComponent;
45
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
46
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
Julian Zobel's avatar
Julian Zobel committed
47
48
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BaseConnectedCallback;
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BaseDisconnectedCallback;
49
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BatteryReplacementCallback;
50
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.ReachedLocationCallback;
Julian Zobel's avatar
Julian Zobel committed
51
import de.tudarmstadt.maki.simonstrator.api.uavsupport.communication.UAVToBaseInterface;
52

Julian Zobel's avatar
Julian Zobel committed
53
54
55
56
57
58
/**
 * Topology component used for UAVs.
 * 
 * @author Julian Zobel
 * @version 1.0, 06.09.2018
 */
59
public class UAVTopologyComponent extends AbstractTopologyComponent implements SimUAVLocationActuator {
60
	
61
	public enum UAVstate {OFFLINE, BASE_CONNECTION, ACTION, RETURN, CRASHED}
62
	
63
	private UAVMovementModel movement;
64
	
65
66
	private OverlayComponent uavOverlayComponent;
	
Julian Zobel's avatar
Julian Zobel committed
67
68
	protected PositionVector direction;
	
69
	private ActuatorComponent actuator;
70
71
	private RechargeableBattery battery;
	
72
	private UAVstate state = UAVstate.OFFLINE;
73
	private PositionVector baseLocation;
Julian Zobel's avatar
Julian Zobel committed
74
	
Julian Zobel's avatar
Julian Zobel committed
75
76
	private UAVToBaseInterface controllerInterface;
	
77
78
79
80
81
82
83
84
85
	/**
	 * Create a TopologyComponent for the current host.
	 *
	 * @param host
	 * @param topology
	 * @param movementModel
	 */
	public UAVTopologyComponent(SimHost host, Topology topology,
			MovementModel movementModel, PlacementModel placementModel, boolean registerAsInformationProviderInSiS) {
86
		super(host, topology, movementModel, placementModel, registerAsInformationProviderInSiS);		
Julian Zobel's avatar
Julian Zobel committed
87
		direction = new PositionVector(0,-1,0);
88
89
	}

90
91
92
93
94
95
	@Override
	public void initialize() {		
		super.initialize();
		
		try {
			actuator = getHost().getComponent(EnergyModel.class)
96
					.getComponent(ComponentType.ACTUATOR, ActuatorComponent.class);	
97
			movement.setMotorControl(actuator);
98
99
100
101
102
		} catch (ComponentNotAvailableException e) {
			System.err.println("No Acutator Energy Component was found!");
		}
		
		try {
103
			battery = (RechargeableBattery) getHost().getComponent(AbstractEnergyModel.class).getBattery();
104
105
106
107
				
		} catch (ComponentNotAvailableException e) {
			System.err.println("No Battery Component was found!");
		}
108
		
Julian Zobel's avatar
Julian Zobel committed
109
110
		// retrieve base location
		baseLocation = UAVBasePlacement.base.position.clone();		
111
112
113
114
	}
	
	public void setState(UAVstate newState) {
		this.state = newState;
115
116
		
		// TODO analyzer
117
118
119
		if(Monitor.hasAnalyzer(UAVStatisticAnalyzer.class)) {
			Monitor.getOrNull(UAVStatisticAnalyzer.class).uavSwitchedStates(this, newState);
		}
120
	}
121
			
122
123
124
	public void setUAVComponent(OverlayComponent uavOverlayComponent) {
		this.uavOverlayComponent = uavOverlayComponent;
	}
125
	
126
127
128
129
	public OverlayComponent getUAVComponent() {
		return uavOverlayComponent;
	}

130
131
	@Override
	public double getMinMovementSpeed() {
132
		return movement.horizontalMinVelocity();
133
134
135
136
	}

	@Override
	public double getMaxMovementSpeed() {
Julian Zobel's avatar
WIP    
Julian Zobel committed
137
		return movement.horizontalMaxVelocity();
138
139
140
141
	}

	@Override
	public double getMovementSpeed() {
142
		return movement.getCurrentVelocity();
143
144
145
146
	}

	@Override
	public void setMovementSpeed(double speed) {
147
		movement.setTargetVelocity(speed);
148
149
150
	}

	@Override
151
	public boolean isActive() {
152
		if(actuator.isOn()) {
153
			return true;
154
		} else {
155
156
157
158
159
			if(state == UAVstate.ACTION || state == UAVstate.RETURN) {
				this.deactivate();
			}
			return false;
		}
160
161
162
	}

	@Override
163
164
	public boolean activate() {		
		if(actuator.turnOn()) {
165
			this.setState(UAVstate.ACTION);			
166
167
168
169
170
			return true;
		}
		else {
			return false;
		}
171
172
173
174
	}

	@Override
	public boolean deactivate() {
175
		actuator.turnOff();
176
				
177
178
179
		if(this.position.getAltitude() != 0) {
			this.setState(UAVstate.CRASHED);		
						
180
181
			uavOverlayComponent.shutdown();	
			shutdownCommunication();
182
183
184
185
			
		} else {
			this.setState(UAVstate.OFFLINE);				
		}				
186
		
187
		return true;
188
189
190
	}

	@Override
191
192
	public PositionVector getCurrentLocation() {
		return position.clone();
193
194
195
	}

	@Override
196
	public double getCurrentBatteryLevel() {
197
		return battery.getCurrentPercentage();
198
	}
199
200
201
	
	@Override
	public double getCurrentBatteryEnergy() {
202
		return battery.getCurrentEnergy();
203
	}
204

205
206
207
	public RechargeableBattery getBattery() {
		return battery;
	}
208
	
209
210
211
212
213
	@Override
	public double getMaximumBatteryCapacity() {
		return battery.getMaximumEnergy();
	}
	
214
215
216
217
218
	@Override
	public UAVMovementModel getUAVMovement() {
		return movement;
	}

Julian Zobel's avatar
Julian Zobel committed
219
220
	@Override
	public void setUAVMovement(UAVMovementModel uavMovement) {
221
		this.movement = uavMovement;		
Julian Zobel's avatar
Julian Zobel committed
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
	@Override
	public Set<AttractionPoint> getAllAttractionPoints() {
		throw new UnsupportedOperationException();
	}

	@Override
	public void setTargetLocation(PositionVector targetLocation,
			ReachedLocationCallback cb) {
		movement.setTargetLocation(new PositionVector(targetLocation), cb);		
	}

	@Override
	public void addTargetLocation(PositionVector targetLocation,
			ReachedLocationCallback cb) {
		movement.addTargetLocation(new PositionVector(targetLocation), cb);
	}

	@Override
	public void setTargetLocationRoute(LinkedList<PositionVector> route,
			ReachedLocationCallback cb) {
		LinkedList<PositionVector> positionvectorlist = new LinkedList<>();
		for (Location loc : route) {
			positionvectorlist.add(new PositionVector(loc));
		}
		movement.setTargetLocationRoute(positionvectorlist, cb);
	}

	@Override
	public void removeAllTargetLocations() {
		movement.removeTargetLocations();
	}

	@Override
	public void setTargetAttractionPoint(AttractionPoint targetAttractionPoint) {
		throw new UnsupportedOperationException();		
	}

	@Override
	public AttractionPoint getCurrentTargetAttractionPoint() {
		throw new UnsupportedOperationException();
	}

	@Override
	public LinkedList<PositionVector> getTargetLocations() {
		return movement.getTargetLocations();
	}

271
272
273
	public UAVstate getUAVState() {
		return state;
	}
274
	
275
	@Override
276
277
	public void returnToBase(ReachedLocationCallback cb) {		
		this.setState(UAVstate.RETURN);		
278
279
280
281
282
		
		ReachedLocationCallback returnCallback = new ReachedLocationCallback() {
			
			@Override
			public void reachedLocation() {
Julian Zobel's avatar
Julian Zobel committed
283
				deactivate();				
284
				cb.reachedLocation();				
285
286
287
			}
		};
		
288
		movement.setTargetVelocity(movement.horizontalMaxVelocity());
289
290
291
		movement.setTargetLocation(baseLocation, returnCallback);	
	}
		
292
293
	public void batteryReplacement(BatteryReplacementCallback cb) {
		
294
		if(state != UAVstate.BASE_CONNECTION) {
295
			throw new UnsupportedOperationException("Cannot recharge if not connected to base!");
296
		}
297
		
298
		BaseTopologyComponent base = UAVBasePlacement.base;
299
		base.getCharger().charge(this, cb);
300
	}
301

Julian Zobel's avatar
Julian Zobel committed
302
	public void setControllerInterface(UAVToBaseInterface controllerInterface) {
303
		this.controllerInterface = controllerInterface;				
Julian Zobel's avatar
Julian Zobel committed
304
305
306
307
308
309
310
311
312
	}

	@Override
	public void connectToBase(BaseConnectedCallback cb) {
		BaseTopologyComponent base = UAVBasePlacement.base;
		base.connectUAVToBase(controllerInterface);	
		
		if(cb != null)
			cb.successfulConnection();
313
		
314
		this.setState(UAVstate.BASE_CONNECTION);		
315
		shutdownCommunication();
Julian Zobel's avatar
Julian Zobel committed
316
317
318
319
	}

	@Override
	public void disconnectFromBase(BaseDisconnectedCallback cb) {
320
321
		startCommunication();
		
Julian Zobel's avatar
Julian Zobel committed
322
323
324
325
326
		BaseTopologyComponent base = UAVBasePlacement.base;
		base.disconnectUAVFromBase(controllerInterface);
		
		if(cb != null)
			cb.successfulDisconnection();
327
		
Julian Zobel's avatar
Julian Zobel committed
328
329
	}

330
331
332
333
	private void shutdownCommunication() {
		for (SimNetInterface net : getHost().getNetworkComponent().getSimNetworkInterfaces()) 
			net.goOffline();
	}
Julian Zobel's avatar
Julian Zobel committed
334

335
336
337
338
	private void startCommunication() {
		for (SimNetInterface net : getHost().getNetworkComponent().getSimNetworkInterfaces()) 
			net.goOnline();
	}
339

Julian Zobel's avatar
Julian Zobel committed
340
341
342
343
344
345
346
347
348
	@Override
	public PositionVector getCurrentDirection() {
		return direction;
	}

	@Override
	public void updateCurrentDirection(PositionVector direction) {
		this.direction.set(direction);
	}
349
	
350
	@Override
351
352
	public double estimatePowerConsumptionWatt(double velocity) {
		return movement.estimatePowerConsumptionWatt(velocity);
353
354
355
	}

	@Override
356
357
358
359
360
361
362
363
364
365
366
	public double estimateFlightDistance(double velocity, double batterylevel, double batterythreshold) {
				
		assert batterylevel > batterythreshold;
		assert batterylevel <= 1.0 && batterylevel >= 0.0;
		assert batterythreshold <= 1.0 && batterythreshold >= 0.0;
		
		double availableEnergy = (battery.getMaximumEnergy() * (batterylevel - batterythreshold)) / Battery.uJconverison; // since battery energy is in uJ, conversion in J is required		
		double powerconsumption = estimatePowerConsumptionWatt(velocity); // J/s (or Watt)				
		double distance = (availableEnergy / powerconsumption) * velocity; // d = (E/P)* v [m]
	
		return distance;
367
	}
368
	
369
370
371
372
373
	@Override
	public PositionVector getBaseLocation() {
		return baseLocation.clone();
	}

374
375
	

376
}