Skip to content

Commit

Permalink
fix(tutorial): minor improvements on tutorial applications (#409)
Browse files Browse the repository at this point in the history
* fix: Use own position when logging at which point the route was changed
* fix: separate event-tx and message-rx functionality
* fix: rather print connections than nodes because that's the more critical information
* fix: remove redundant information
* refactor: use precise wording, as the route switch may still fail
  • Loading branch information
hoelger authored Oct 1, 2024
1 parent b5d6372 commit 290fa8d
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@
package org.eclipse.mosaic.app.tutorial;

import org.eclipse.mosaic.app.tutorial.message.IntraVehicleMsg;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.AdHocModuleConfiguration;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.CamBuilder;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.ReceivedAcknowledgement;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.ReceivedV2xMessage;
import org.eclipse.mosaic.fed.application.app.AbstractApplication;
import org.eclipse.mosaic.fed.application.app.api.Application;
import org.eclipse.mosaic.fed.application.app.api.CommunicationApplication;
import org.eclipse.mosaic.fed.application.app.api.VehicleApplication;
import org.eclipse.mosaic.fed.application.app.api.os.VehicleOperatingSystem;
import org.eclipse.mosaic.interactions.communication.V2xMessageTransmission;
import org.eclipse.mosaic.lib.enums.AdHocChannel;
import org.eclipse.mosaic.lib.enums.SensorType;
import org.eclipse.mosaic.lib.objects.vehicle.VehicleData;
import org.eclipse.mosaic.lib.util.scheduling.Event;

import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class EventSendingApp extends AbstractApplication<VehicleOperatingSystem> implements VehicleApplication, CommunicationApplication {
public class EventSendingApp extends AbstractApplication<VehicleOperatingSystem> implements VehicleApplication {
/**
* Used for choosing a RAND id for the message that is sent intra-vehicle.
*/
Expand All @@ -44,55 +36,26 @@ public class EventSendingApp extends AbstractApplication<VehicleOperatingSystem>
@Override
public void onStartup() {
getLog().infoSimTime(this, "Initialize application");
getOs().getAdHocModule().enable(new AdHocModuleConfiguration()
.addRadio()
.channel(AdHocChannel.CCH)
.power(50)
.create());
getLog().infoSimTime(this, "Activated AdHoc Module");
}

@Override
public void onVehicleUpdated(@Nullable VehicleData previousVehicleData, @Nonnull VehicleData updatedVehicleData) {
final List<? extends Application> applications = getOs().getApplications();
final IntraVehicleMsg message = new IntraVehicleMsg(getOs().getId(), getRandom().nextInt(0, MAX_ID));

// Example usage for how to detect sensor readings
if (getOs().getStateOfEnvironmentSensor(SensorType.OBSTACLE) > 0) {
getLog().infoSimTime(this, "Reading sensor");
}

for (Application application : applications) {
final Event event = new Event(getOs().getSimulationTime() + 10, application, message);
this.getOs().getEventManager().addEvent(event);
}
}

@Override
public void onMessageReceived(ReceivedV2xMessage receivedV2xMessage) {
getLog().infoSimTime(this, "Received V2X Message from {}", receivedV2xMessage.getMessage().getRouting().getSource().getSourceName());
}

@Override
public void processEvent(Event event) throws Exception {
getLog().infoSimTime(this, "Received event: {}", getOs().getSimulationTimeMs(), event.getResourceClassSimpleName());
getLog().infoSimTime(this, "Received event: {}", event.getResourceClassSimpleName());
}

@Override
public void onShutdown() {
getLog().infoSimTime(this, "Shutdown application");
}

@Override
public void onAcknowledgementReceived(ReceivedAcknowledgement acknowledgedMessage) {
}

@Override
public void onCamBuilding(CamBuilder camBuilder) {
}

@Override
public void onMessageTransmitted(V2xMessageTransmission v2xMessageTransmission) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2024 Fraunhofer FOKUS and others. All rights reserved.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contact: mosaic@fokus.fraunhofer.de
*/

package org.eclipse.mosaic.app.tutorial;

import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.AdHocModuleConfiguration;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.CamBuilder;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.ReceivedAcknowledgement;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.ReceivedV2xMessage;
import org.eclipse.mosaic.fed.application.app.AbstractApplication;
import org.eclipse.mosaic.fed.application.app.api.CommunicationApplication;
import org.eclipse.mosaic.fed.application.app.api.os.VehicleOperatingSystem;
import org.eclipse.mosaic.interactions.communication.V2xMessageTransmission;
import org.eclipse.mosaic.lib.enums.AdHocChannel;
import org.eclipse.mosaic.lib.util.scheduling.Event;

import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class MessageReceivingApp extends AbstractApplication<VehicleOperatingSystem> implements CommunicationApplication {

@Override
public void onStartup() {
getLog().infoSimTime(this, "Initialize application");
getOs().getAdHocModule().enable(new AdHocModuleConfiguration()
.addRadio()
.channel(AdHocChannel.CCH)
.power(50)
.create());
getLog().infoSimTime(this, "Activated AdHoc Module");
}

@Override
public void onShutdown() {
getLog().infoSimTime(this, "Shutdown application");
}

@Override
public void processEvent(Event event) throws Exception {
getLog().infoSimTime(this, "Received event: {}", event.getResourceClassSimpleName());
}

@Override
public void onMessageReceived(ReceivedV2xMessage receivedV2xMessage) {
getLog().infoSimTime(this, "Received V2X Message from {}", receivedV2xMessage.getMessage().getRouting().getSource().getSourceName());
}

@Override
public void onAcknowledgementReceived(ReceivedAcknowledgement acknowledgedMessage) {
}

@Override
public void onCamBuilding(CamBuilder camBuilder) {
}

@Override
public void onMessageTransmitted(V2xMessageTransmission v2xMessageTransmission) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class WeatherWarningApp extends AbstractApplication<VehicleOperatingSyste
/**
* Flag that is set if the route has already been changed.
*/
private boolean routeChanged = false;
private boolean triedToChangeRoute = false;

/**
* This is the speed for the DEN message sent for rerouting.
Expand Down Expand Up @@ -120,8 +120,8 @@ public void onMessageReceived(ReceivedV2xMessage receivedV2xMessage) {

getLog().debug("Handle Environment Warning Message. Processing...");

if (routeChanged) {
getLog().infoSimTime(this, "Route already changed");
if (triedToChangeRoute) {
getLog().infoSimTime(this, "Route change already tried once.");
} else {
reactUponDENMessageChangeRoute(denm);
}
Expand Down Expand Up @@ -162,7 +162,7 @@ private void detectSensors() {
type = currentType;
// Method which is called to react on new or changed environment events
reactOnEnvironmentData(type, strength);
return;
return; // the early exit discards other possible environmental warnings, ok for this tutorial purpose
}
}

Expand Down Expand Up @@ -259,10 +259,10 @@ private void reactUponDENMessageChangeRoute(Denm denm) {
// Retrieve only the connection id and throw away the edge id
// NOTE: a route info id has the format connectionId_edgeId
if (connection.equals(affectedConnectionId)) {
getLog().infoSimTime(this, "The Event is on the vehicle's route {} = {}", connection, affectedConnectionId);
getLog().infoSimTime(this, "The event occurred on connection with id={}, which is part of vehicle's route with id={}", connection, routeInfo.getId());

circumnavigateAffectedRoad(denm, affectedConnectionId);
routeChanged = true;
triedToChangeRoute = true;
return;
}
}
Expand Down Expand Up @@ -300,7 +300,7 @@ private void circumnavigateAffectedRoad(Denm denm, final String affectedRoadId)
*/
CandidateRoute newRoute = response.getBestRoute();
if (newRoute != null) {
getLog().infoSimTime(this, "Sending Change Route Command at position: {}", denm.getSenderPosition());
getLog().infoSimTime(this, "Sending Change Route Command at position: {}", getOs().getPosition());
navigationModule.switchRoute(newRoute);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"name": "Car",
"applications": [
"org.eclipse.mosaic.app.tutorial.MessageReceivingApp",
"org.eclipse.mosaic.app.tutorial.EventSendingApp",
"org.eclipse.mosaic.app.tutorial.EventProcessingApp"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public boolean switchRoute(CandidateRoute newRoute) {
belongingUnit.getOsLog().info(
"NavigationModule#switchRoute: Switched to route {} [{}]",
route.getId(),
StringUtils.join(route.getNodeIds(), ",")
StringUtils.join(route.getConnectionIds(), ",")
);
} else if (route != null) {
belongingUnit.getOsLog().info("NavigationModule#switchRoute: Stay on route {}", route.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public String getLastNodeId() {

@Override
public String toString() {
return "Route{" + "id=" + routeId + ", connetions=" + connectionIds + ", nodes=" + nodeIds + ", length=" + length + '}';
return "Route{" + "id=" + routeId + ", connections=" + connectionIds + ", nodes=" + nodeIds + ", length=" + length + '}';
}

@Override
Expand Down

0 comments on commit 290fa8d

Please sign in to comment.