Skip to content

Commit

Permalink
Add Sensor/Detector Registration to Infrastructure Ambassador (#152)
Browse files Browse the repository at this point in the history
<!-- Thanks for the contribution, this is awesome. -->

# PR Details
## Description

<!--- Describe your changes in detail -->
Added additional information to the Infrastructure Registration Message
including :
- Port for transmitting relevant interactions
- List of sensors/detectors to register for infrastructure instance

Added functionality to process new registration message, trigger
sensors/detectors registration interactions to prompt creation of
sensors/detectors.
Added functionality to process incoming Detected Object interactions
from sensors and forward them via the new interactions port to
registered infrastructure instances based on the sensor that reports the
detection.

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an
issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps
to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->
New functionality will be used to create sensor in CARLA and retrieve
detection information for the VRU use case
## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->
Unit tested and Integration testing with XIL Cloud setup
Only tested that CDASim properly processes Infrastructure registration
and still forwards time sync messages. Further testing for detection
interaction processing is advised once CARLA Ambassador work completes.
## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Defect fix (non-breaking change that fixes an issue)
- [x] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that cause existing functionality
to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [ ] I have added any new packages to the sonar-scanner.properties file
- [x] My change requires a change to the documentation.
- [] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
[CARMA Contributing Guide](Contributing.md) 
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
  • Loading branch information
kjrush authored Aug 8, 2023
2 parents 443a82a + 66769eb commit a1590e2
Show file tree
Hide file tree
Showing 31 changed files with 2,603 additions and 384 deletions.
7 changes: 7 additions & 0 deletions co-simulation/fed/mosaic-infrastructure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
<artifactId>mosaic-objects</artifactId>
<version>${mosaic.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.mosaic</groupId>
<artifactId>mosaic-objects</artifactId>
<version>${mosaic.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>gov.dot.fhwa.saxton</groupId>
<artifactId>mosaic-carma-utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@

package org.eclipse.mosaic.fed.infrastructure.ambassador;

import org.eclipse.mosaic.lib.geo.CartesianPoint;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.List;

import org.eclipse.mosaic.lib.geo.CartesianPoint;
import org.eclipse.mosaic.lib.objects.detector.DetectedObject;
import org.eclipse.mosaic.lib.objects.detector.Detector;
import org.eclipse.mosaic.rti.api.Interaction;

import com.google.gson.Gson;

/**
* InfrastructureInstance class represents a physical instance of an
* infrastructure node in the simulated environment.
* It contains information about the infrastructure node such as its ID,
* infrastructure instance in the simulated environment.
* It contains information about the infrastructure instance such as its ID,
* location, target address, and ports.
*/
public class InfrastructureInstance {
Expand All @@ -35,40 +41,48 @@ public class InfrastructureInstance {
private InetAddress targetAddress;
private int rxMessagePort;
private int timeSyncPort;
private int simulatedInteractionPort;
private CartesianPoint location = null;
private DatagramSocket rxMsgsSocket = null;
private DatagramSocket socket = null;
private List<Detector> sensors;

/**
* Constructor for InfrastructureInstance
*
* @param infrastructureId the ID of the infrastructure node
* @param targetAddress the target IP address of the infrastructure node
* @param rxMessagePort the receive message port of the infrastructure node
* @param timeSyncPort the time synchronization port of the infrastructure
* node
* @param location the location of the infrastructure node in the
* simulated environment
*/
public InfrastructureInstance(String infrastructureId, InetAddress targetAddress,
int rxMessagePort, int timeSyncPort, CartesianPoint location) {
* @param infrastructureId the ID of the infrastructure instance.
* @param targetAddress the target IP address of the infrastructure instance.
* @param rxMessagePort the receive V2X message port of the infrastructure instance.
* @param timeSyncPort the receive time synchronization message port of the infrastructure.
*
* @param simulatedInteractionPort the receive simulated interaction message port of the infrastructure
* instance.
* @param location the location of the infrastructure instance in the
* simulated environment
*/
public InfrastructureInstance(String infrastructureId, InetAddress targetAddress, int rxMessagePort,
int timeSyncPort, int simulatedInteractionPort, CartesianPoint location, List<Detector> sensors) {
this.infrastructureId = infrastructureId;
this.targetAddress = targetAddress;
this.rxMessagePort = rxMessagePort;
this.timeSyncPort = timeSyncPort;
this.simulatedInteractionPort = simulatedInteractionPort;
this.location = location;
this.sensors = sensors;
}



/**
* Returns the target IP address of the infrastructure node
* Returns the target IP address of the infrastructure instance
*
* @return InetAddress the target IP address of the infrastructure node
* @return InetAddress the target IP address of the infrastructure instance
*/
public InetAddress getTargetAddress() {
return targetAddress;
}

/**
* Sets the target IP address of the infrastructure node
* Sets the target IP address of the infrastructure instance
*
* @param targetAddress the target IP address to set
*/
Expand All @@ -77,16 +91,16 @@ public void setTargetAddress(InetAddress targetAddress) {
}

/**
* Returns the location of the infrastructure node in the simulated environment
* Returns the location of the infrastructure instance in the simulated environment
*
* @return CartesianPoint the location of the infrastructure node
* @return CartesianPoint the location of the infrastructure instance
*/
public CartesianPoint getLocation() {
return this.location;
}

/**
* Sets the location of the infrastructure node in the simulated environment
* Sets the location of the infrastructure instance in the simulated environment
*
* @param location the location to set
*/
Expand All @@ -95,16 +109,16 @@ public void setLocation(CartesianPoint location) {
}

/**
* Returns the ID of the infrastructure node
* Returns the ID of the infrastructure instance
*
* @return String the ID of the infrastructure node
* @return String the ID of the infrastructure instance
*/
public String getInfrastructureId() {
return infrastructureId;
}

/**
* Sets the ID of the infrastructure node
* Sets the ID of the infrastructure instance
*
* @param infrastructureId the ID to set
*/
Expand All @@ -113,16 +127,16 @@ public void setInfrastructureId(String infrastructureId) {
}

/**
* Returns the receive message port of the infrastructure node
* Returns the receive message port of the infrastructure instance
*
* @return int the receive message port of the infrastructure node
* @return int the receive message port of the infrastructure instance
*/
public int getRxMessagePort() {
return rxMessagePort;
}

/**
* Sets the receive message port of the infrastructure node
* Sets the receive message port of the infrastructure instance
*
* @param rxMessagePort the port to set
*/
Expand All @@ -131,32 +145,87 @@ public void setRxMessagePort(int rxMessagePort) {
}

/**
* Returns the time synchronization port of the infrastructure node
* Returns the time synchronization port of the infrastructure instance
*
* @return int the time synchronization port of the infrastructure node
* @return int the time synchronization port of the infrastructure instance
*/
public int getTimeSyncPort() {
return timeSyncPort;
}

/**
* Sets the time synchronization port of the infrastructure node
* Sets the time synchronization port of the infrastructure instance
*
* @param timeSyncPort the port to set
*/
public void setTimeSyncPort(int timeSyncPort) {
this.timeSyncPort = timeSyncPort;
}

/**
* Returns the simulated interaction port of the infrastructure instance.
*
* @return int simulated interaction port of the infrastructure instance.
*/
public int getSimulatedInteractionPort() {
return simulatedInteractionPort;
}


/**
* Creates a DatagramSocket object and binds it to this infrastructure
* instance's receive message port
* Sets the simulated interaction port of the infrastructure instance.
*
* @throws IOException if there is an issue with the underlying socket object or
* methods
* @param simulatedInteractionPort int simulated interaction port of the infrastructure
* instance.
*/
public void setSimulatedInteractionPort(int simulatedInteractionPort) {
this.simulatedInteractionPort = simulatedInteractionPort;
}


/**
* Returns list of Sensor/Detectors registered to this infrastructure instance.
*
* @return list of detectors registered to infrastructure instance.
*/
public List<Detector> getSensors() {
return sensors;
}


/**
* Sets list of Sensors/Detectors registered to infrastructure instance.
*
* @param sensors list of detectors registered to infrastructure instance.
*/
public void setSensors(List<Detector> sensors) {
this.sensors = sensors;
}


/**
* Method that returns boolean value based on whether any registered Sensor/Detector
* in the sensor list has a sensorId equivalent to the passed parameter.
*
* @param sensorId sensor ID to search for
* @return true if sensor with sensor ID exists in sensor list. False otherwise.
*/
public void bind() throws IOException {
rxMsgsSocket = new DatagramSocket();
public boolean containsSensor(String sensorId) {
for (Detector sensor : sensors) {
if (sensor.getSensorId().equals(sensorId) ) {
return true;
}
}
return false;
}

/**
* Creates a DatagramSocket object
*
* @throws IOException if there is an issue with the underlying socket object
*/
public void connect() throws IOException {
socket = new DatagramSocket();
}

/**
Expand All @@ -167,27 +236,52 @@ public void bind() throws IOException {
* @throws IOException If there is an issue with the underlying socket object or
* methods
*/
public void sendMsgs(byte[] data) throws IOException {
if (rxMsgsSocket == null) {
throw new IllegalStateException("Attempted to send data before opening socket");
}
DatagramPacket packet = new DatagramPacket(data, data.length, targetAddress, rxMessagePort);
rxMsgsSocket.send(packet);
public void sendV2xMsg(byte[] data) throws IOException {
sendPacket(data, rxMessagePort);
}

/**
* Sends time sync data to the Infrastrucutre Instance Time Sync port.
*
* @param data The binary data to transmit
* @throws IOException If there is an issue with the underlying socket object or methods
*/
public void sendTimeSyncMsg(InfrastructureTimeMessage message) throws IOException {
sendPacket(toJsonBytes(message), timeSyncPort);
}

/**
* Helper method to serialize message into JSON and encode as bytes.
*
* @param message java object containing message information
* @return bytes encoded from JSON string representation of object.
*/
private byte[] toJsonBytes(Object message) {
Gson gson = new Gson();
return gson.toJson(message).getBytes();
}

/**
* Sends time sync data to the Infrastructure Device communications interface configured at construction time.
* Sends time sync data to the Infrastrucutre Instance Simulated Interaction port.
*
* @param data The binary data to transmit
* @throws IOException If there is an issue with the underlying socket object or methods
*/
public void sendTimeSyncMsgs(byte[] data) throws IOException {
if (rxMsgsSocket == null) {
public void sendDetection(DetectedObject detectedObject) throws IOException {
sendPacket(toJsonBytes(detectedObject), simulatedInteractionPort);
}
/**
* Method to send byte data to specified port using the infrastructure instance Datagramsocket.
*
* @param data byte data to send using Datagramsocket.
* @param port in integer format to send Datagram to.
* @throws IOException
*/
private void sendPacket(byte[] data, int port) throws IOException {
if (socket == null) {
throw new IllegalStateException("Attempted to send data before opening socket");
}

DatagramPacket packet = new DatagramPacket(data, data.length, targetAddress, timeSyncPort);
rxMsgsSocket.send(packet);

DatagramPacket packet = new DatagramPacket(data, data.length, targetAddress, port);
socket.send(packet);
}
}
Loading

0 comments on commit a1590e2

Please sign in to comment.