UAVTopologyComponent.java 10.9 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
import de.tud.kom.p2psim.api.topology.Topology;
31
import de.tud.kom.p2psim.api.topology.TopologyComponent;
32
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
33
import de.tud.kom.p2psim.api.topology.movement.SimUAVLocationActuator;
34
35
import de.tud.kom.p2psim.api.topology.movement.UAVMovementModel;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
36
import de.tud.kom.p2psim.impl.energy.RechargeableBattery;
37
import de.tud.kom.p2psim.impl.energy.components.ActuatorComponent;
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.component.ComponentNotAvailableException;
Julian Zobel's avatar
Julian Zobel committed
43
import de.tudarmstadt.maki.simonstrator.api.component.overlay.OverlayComponent;
44
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
45
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
Julian Zobel's avatar
Julian Zobel committed
46
47
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BaseConnectedCallback;
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BaseDisconnectedCallback;
48
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BatteryReplacementCallback;
49
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.ReachedLocationCallback;
Julian Zobel's avatar
Julian Zobel committed
50
import de.tudarmstadt.maki.simonstrator.api.uavsupport.communication.UAVToBaseInterface;
51

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

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

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

	@Override
	public double getMaxMovementSpeed() {
136
		return movement.getHorizontalMaxVelocity();
137
138
139
140
	}

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

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

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

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

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

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

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

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

Julian Zobel's avatar
Julian Zobel committed
218
219
	@Override
	public void setUAVMovement(UAVMovementModel uavMovement) {
220
		this.movement = uavMovement;		
Julian Zobel's avatar
Julian Zobel committed
221
	}
222

223
	@Override
224
	public Set<IAttractionPoint> getAllAttractionPoints() {
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
		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
256
	public void setTargetAttractionPoint(IAttractionPoint targetAttractionPoint) {
257
258
259
260
		throw new UnsupportedOperationException();		
	}

	@Override
261
	public IAttractionPoint getCurrentTargetAttractionPoint() {
262
263
264
265
266
267
268
269
		throw new UnsupportedOperationException();
	}

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

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

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

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

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

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

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

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

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

	@Override
355
356
357
358
359
360
361
362
363
364
365
	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;
366
	}
367
	
368
369
370
371
	public double estimateOptimalSpeed() {
		return movement.estimateOptimalSpeed();
	}
	
372
373
374
375
376
	@Override
	public PositionVector getBaseLocation() {
		return baseLocation.clone();
	}

377
	
378
379
380
381
382
383
384
385
386
387
388
389
	public static class Factory implements TopologyComponentFactory {

		@Override
		public TopologyComponent createTopologyComponent(SimHost host,
				Topology topology, MovementModel movementModel,
				PlacementModel placementModel,
				boolean registerAsInformationProviderInSiS) {
			return new UAVTopologyComponent(host, topology, movementModel, placementModel, registerAsInformationProviderInSiS);
		}
		
	}
	
390
}