|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Fraunhofer FOKUS and others. All rights reserved. |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + * |
| 13 | + |
| 14 | + */ |
| 15 | + |
| 16 | +package org.eclipse.mosaic.fed.application.ambassador.simulation.perception; |
| 17 | + |
| 18 | +import org.eclipse.mosaic.fed.application.ambassador.SimulationKernel; |
| 19 | +import org.eclipse.mosaic.fed.application.app.api.perception.LidarSensorModule; |
| 20 | +import org.eclipse.mosaic.interactions.vehicle.VehicleSensorActivation; |
| 21 | +import org.eclipse.mosaic.lib.spatial.PointCloud; |
| 22 | +import org.eclipse.mosaic.rti.api.IllegalValueException; |
| 23 | +import org.eclipse.mosaic.rti.api.InternalFederateException; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.function.Consumer; |
| 28 | + |
| 29 | +public class DefaultLidarSensorModule implements LidarSensorModule { |
| 30 | + |
| 31 | + private boolean enabled; |
| 32 | + private final String unitId; |
| 33 | + |
| 34 | + private PointCloud currentPointcloud; |
| 35 | + private final List<Consumer<PointCloud>> callbacks = new ArrayList<>(); |
| 36 | + |
| 37 | + public DefaultLidarSensorModule(String unitId) { |
| 38 | + this.unitId = unitId; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void enable(double range) { |
| 43 | + this.enabled = true; |
| 44 | + |
| 45 | + // Create a VehicleSensorActivation interaction to be sent to the RTI |
| 46 | + VehicleSensorActivation interaction = new VehicleSensorActivation( |
| 47 | + SimulationKernel.SimulationKernel.getCurrentSimulationTime(), |
| 48 | + unitId, |
| 49 | + range, |
| 50 | + VehicleSensorActivation.SensorType.LIDAR |
| 51 | + ); |
| 52 | + |
| 53 | + // Send the interaction to the RTI, thereby enabling the LiDAR sensor |
| 54 | + try { |
| 55 | + SimulationKernel.SimulationKernel.getInteractable().triggerInteraction(interaction); |
| 56 | + } catch (IllegalValueException | InternalFederateException ex) { |
| 57 | + throw new RuntimeException(ex); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean isEnabled() { |
| 63 | + return enabled; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void disable() { |
| 68 | + this.enabled = false; |
| 69 | + |
| 70 | + // Create a VehicleSensorActivation interaction to be sent to the RTI that disables the LiDAR sensor |
| 71 | + VehicleSensorActivation interaction = new VehicleSensorActivation( |
| 72 | + SimulationKernel.SimulationKernel.getCurrentSimulationTime(), |
| 73 | + unitId, |
| 74 | + 0, |
| 75 | + VehicleSensorActivation.SensorType.LIDAR |
| 76 | + ); |
| 77 | + |
| 78 | + // Send the interaction to the RTI, thereby disabling the LiDAR sensor |
| 79 | + try { |
| 80 | + SimulationKernel.SimulationKernel.getInteractable().triggerInteraction(interaction); |
| 81 | + } catch (IllegalValueException | InternalFederateException ex) { |
| 82 | + throw new RuntimeException(ex); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void reactOnSensorUpdate(Consumer<PointCloud> callback) { |
| 88 | + this.callbacks.add(callback); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public PointCloud getPointCloud() { |
| 93 | + if (!enabled) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + return this.currentPointcloud; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * This method is called by the ApplicationAmbassador when it received new LidarUpdates from the federate. |
| 101 | + * It sets the most current pointcloud to the one received and triggers the callback for every application that set one. |
| 102 | + * @param pointCloud The most current pointcloud. |
| 103 | + */ |
| 104 | + public void updatePointCloud(PointCloud pointCloud) { |
| 105 | + this.currentPointcloud = pointCloud; |
| 106 | + for (Consumer<PointCloud> callback : callbacks) { |
| 107 | + callback.accept(pointCloud); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments