UAVTopologyComponent.java 10.4 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
import de.tud.kom.p2psim.impl.energy.components.ActuatorComponent;
37
38
import de.tud.kom.p2psim.impl.energy.models.AbstractEnergyModel;
import de.tud.kom.p2psim.impl.topology.placement.UAVBasePlacement;
39
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
40
import de.tudarmstadt.maki.simonstrator.api.Monitor;
41
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
Julian Zobel's avatar
Julian Zobel committed
42
import de.tudarmstadt.maki.simonstrator.api.component.overlay.OverlayComponent;
43
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
44
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
Julian Zobel's avatar
Julian Zobel committed
45
46
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BaseConnectedCallback;
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BaseDisconnectedCallback;
47
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BatteryReplacementCallback;
48
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.ReachedLocationCallback;
Julian Zobel's avatar
Julian Zobel committed
49
import de.tudarmstadt.maki.simonstrator.api.uavsupport.communication.UAVToBaseInterface;
50

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

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

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

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

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

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

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

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

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

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

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

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

Julian Zobel's avatar
Julian Zobel committed
217
218
	@Override
	public void setUAVMovement(UAVMovementModel uavMovement) {
219
		this.movement = uavMovement;		
Julian Zobel's avatar
Julian Zobel committed
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
	@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();
	}

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

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

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

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

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

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

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

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

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

372
373
	

374
}