UAVTopologyComponent.java 8.17 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
26
import java.util.Set;

import de.tud.kom.p2psim.api.common.SimHost;
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.ActuatorEnergyComponent;
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.component.ComponentNotAvailableException;
Julian Zobel's avatar
Julian Zobel committed
41
import de.tudarmstadt.maki.simonstrator.api.component.overlay.OverlayComponent;
42
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
43
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
Julian Zobel's avatar
Julian Zobel committed
44
45
46
import de.tudarmstadt.maki.simonstrator.api.uavsupport.UAVToBaseInterface;
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.ReachedLocationCallback;
48

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

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

117
118
	@Override
	public double getMinMovementSpeed() {
Julian Zobel's avatar
Julian Zobel committed
119
		return movement.getMinCruiseSpeed();
120
121
122
123
	}

	@Override
	public double getMaxMovementSpeed() {
Julian Zobel's avatar
Julian Zobel committed
124
		return movement.getMaxCruiseSpeed();
125
126
127
128
	}

	@Override
	public double getMovementSpeed() {
Julian Zobel's avatar
Julian Zobel committed
129
		return movement.getCurrentSpeed();
130
131
132
133
	}

	@Override
	public void setMovementSpeed(double speed) {
Julian Zobel's avatar
Julian Zobel committed
134
		movement.setPreferredCruiseSpeed(speed);
135
136
137
	}

	@Override
138
139
140
141
142
	public boolean isActive() {
		return actuator.isOn();
	}

	@Override
143
144
145
146
147
148
149
150
	public boolean activate() {		
		if(actuator.turnOn()) {
			state = UAVstate.ACTION;
			return true;
		}
		else {
			return false;
		}
151
152
153
154
	}

	@Override
	public boolean deactivate() {
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		actuator.turnOff();
	
		if(this.position.getAltitude() != 0) {			
			state = UAVstate.CRASHED;
			System.err.println("UAV was destroyed due to actuator deactivation during flight");
			
			uavOverlayComponent.shutdown();
				
			for (SimNetInterface net : getHost().getNetworkComponent()
					.getSimNetworkInterfaces()) {
				net.goOffline();
			}
		}
		else {
			state = UAVstate.OFFLINE;
		}
		
		return true;
173
174
175
	}

	@Override
176
177
	public PositionVector getCurrentLocation() {
		return position.clone();
178
179
180
	}

	@Override
181
	public double getCurrentBatteryLevel() {
182
		return battery.getCurrentPercentage();
183
	}
184

185
186
187
	public RechargeableBattery getBattery() {
		return battery;
	}
188
	
189
190
191
192
193
	@Override
	public UAVMovementModel getUAVMovement() {
		return movement;
	}

Julian Zobel's avatar
Julian Zobel committed
194
195
	@Override
	public void setUAVMovement(UAVMovementModel uavMovement) {
196
		this.movement = uavMovement;		
Julian Zobel's avatar
Julian Zobel committed
197
	}
198
199
200
201

	@Override
	public ActuatorEnergyComponent getActuatorEnergyComponent() {
		return actuator;
Julian Zobel's avatar
Julian Zobel committed
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
	@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();
	}

251
252
253
	public UAVstate getUAVState() {
		return state;
	}
254
	
255
256
257
258
259
260
261
262
	@Override
	public void returnToBase(ReachedLocationCallback cb) {
		this.state = UAVstate.RETURN;
		
		ReachedLocationCallback returnCallback = new ReachedLocationCallback() {
			
			@Override
			public void reachedLocation() {
Julian Zobel's avatar
Julian Zobel committed
263
				deactivate();				
264
				cb.reachedLocation();				
265
266
267
268
269
270
			}
		};
		
		movement.setTargetLocation(baseLocation, returnCallback);	
	}
		
Julian Zobel's avatar
Julian Zobel committed
271
	protected void batteryReplacement() {
272
		BaseTopologyComponent base = UAVBasePlacement.base;
Julian Zobel's avatar
Julian Zobel committed
273
		base.getCharger().charge(this, null);
274
	}
275

Julian Zobel's avatar
Julian Zobel committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
	public void setControllerInterface(UAVToBaseInterface controllerInterface) {
		this.controllerInterface = controllerInterface;
		UAVBasePlacement.base.connectUAVToBase(this.controllerInterface);		
		state =  UAVstate.OFFLINE;
	}

	@Override
	public void connectToBase(BaseConnectedCallback cb) {
		BaseTopologyComponent base = UAVBasePlacement.base;
		base.connectUAVToBase(controllerInterface);	
		
		if(cb != null)
			cb.successfulConnection();
	}

	@Override
	public void disconnectFromBase(BaseDisconnectedCallback cb) {
		BaseTopologyComponent base = UAVBasePlacement.base;
		base.disconnectUAVFromBase(controllerInterface);
		
		if(cb != null)
			cb.successfulDisconnection();
	}

	

302
303

}