UAVTopologyComponent.java 9.25 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;

26
27
import org.apache.batik.bridge.AbstractSVGGradientElementBridge.Stop;

28
import de.tud.kom.p2psim.api.common.SimHost;
29
30
import de.tud.kom.p2psim.api.energy.ComponentType;
import de.tud.kom.p2psim.api.energy.EnergyModel;
31
import de.tud.kom.p2psim.api.network.SimNetInterface;
32
33
import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
34
import de.tud.kom.p2psim.api.topology.movement.SimUAVLocationActuator;
35
36
import de.tud.kom.p2psim.api.topology.movement.UAVMovementModel;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
37
import de.tud.kom.p2psim.impl.energy.RechargeableBattery;
38
39
import de.tud.kom.p2psim.impl.energy.components.StateActuatorEnergyComponent;
import de.tud.kom.p2psim.impl.energy.components.StatelessMotorComponent;
40
41
import de.tud.kom.p2psim.impl.energy.models.AbstractEnergyModel;
import de.tud.kom.p2psim.impl.topology.placement.UAVBasePlacement;
42
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
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

Julian Zobel's avatar
Julian Zobel committed
48
49
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BaseConnectedCallback;
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BaseDisconnectedCallback;
50
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BatteryReplacementCallback;
51
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.ReachedLocationCallback;
Julian Zobel's avatar
Julian Zobel committed
52
import de.tudarmstadt.maki.simonstrator.api.uavsupport.communication.UAVToBaseInterface;
53

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

91
92
93
94
95
96
	@Override
	public void initialize() {		
		super.initialize();
		
		try {
			actuator = getHost().getComponent(EnergyModel.class)
97
98
					.getComponent(ComponentType.ACTUATOR, StatelessMotorComponent.class);	
			movement.setMotorControl(actuator);
99
100
101
102
103
		} catch (ComponentNotAvailableException e) {
			System.err.println("No Acutator Energy Component was found!");
		}
		
		try {
104
			battery = (RechargeableBattery) getHost().getComponent(AbstractEnergyModel.class).getBattery();
105
106
107
108
				
		} catch (ComponentNotAvailableException e) {
			System.err.println("No Battery Component was found!");
		}
109
		
Julian Zobel's avatar
Julian Zobel committed
110
111
		// retrieve base location
		baseLocation = UAVBasePlacement.base.position.clone();		
112
113
114
115
	}
	
	public void setState(UAVstate newState) {
		this.state = newState;
116
117
118
		

		// TODO analyzer
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() {
Julian Zobel's avatar
WIP    
Julian Zobel committed
131
		return movement.minimumVelocity();
132
133
134
135
	}

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

	@Override
	public double getMovementSpeed() {
Julian Zobel's avatar
Julian Zobel committed
141
		return movement.getCurrentSpeed();
142
143
144
145
	}

	@Override
	public void setMovementSpeed(double speed) {
Julian Zobel's avatar
Julian Zobel committed
146
		movement.setPreferredSpeed(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
201
202
	
	@Override
	public double getCurrentBatteryEnergy() {
		return battery.getCurrentEnergyLevel();
	}
203

204
205
206
	public RechargeableBattery getBattery() {
		return battery;
	}
207
	
208
209
210
211
212
	@Override
	public UAVMovementModel getUAVMovement() {
		return movement;
	}

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

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

Julian Zobel's avatar
Julian Zobel committed
295
	public void setControllerInterface(UAVToBaseInterface controllerInterface) {
296
		this.controllerInterface = controllerInterface;				
Julian Zobel's avatar
Julian Zobel committed
297
298
299
300
301
302
303
304
305
	}

	@Override
	public void connectToBase(BaseConnectedCallback cb) {
		BaseTopologyComponent base = UAVBasePlacement.base;
		base.connectUAVToBase(controllerInterface);	
		
		if(cb != null)
			cb.successfulConnection();
306
		
307
		this.setState(UAVstate.BASE_CONNECTION);		
308
		shutdownCommunication();
Julian Zobel's avatar
Julian Zobel committed
309
310
311
312
	}

	@Override
	public void disconnectFromBase(BaseDisconnectedCallback cb) {
313
314
		startCommunication();
		
Julian Zobel's avatar
Julian Zobel committed
315
316
317
318
319
		BaseTopologyComponent base = UAVBasePlacement.base;
		base.disconnectUAVFromBase(controllerInterface);
		
		if(cb != null)
			cb.successfulDisconnection();
320
		
Julian Zobel's avatar
Julian Zobel committed
321
322
	}

323
324
325
326
	private void shutdownCommunication() {
		for (SimNetInterface net : getHost().getNetworkComponent().getSimNetworkInterfaces()) 
			net.goOffline();
	}
Julian Zobel's avatar
Julian Zobel committed
327

328
329
330
331
	private void startCommunication() {
		for (SimNetInterface net : getHost().getNetworkComponent().getSimNetworkInterfaces()) 
			net.goOnline();
	}
332

Julian Zobel's avatar
Julian Zobel committed
333
334
335
336
337
338
339
340
341
342
	@Override
	public PositionVector getCurrentDirection() {
		return direction;
	}

	@Override
	public void updateCurrentDirection(PositionVector direction) {
		this.direction.set(direction);
	}

343
344
	

345
}