Commit ea56c886 authored by Jean Meurice's avatar Jean Meurice Committed by Evgeny Kusmenko
Browse files

Software simulator

parent 1144e736
......@@ -30,7 +30,7 @@ masterJobWindows:
- mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean deploy --settings settings.xml
- mvn package sonar:sonar -s settings.xml
tags:
- Windows10
- Windows10_OS
only:
- master
......@@ -39,7 +39,7 @@ branchJobWindows:
script:
- mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean install --settings settings.xml
tags:
- Windows10
- Windows10_OS
except:
- master
......
......@@ -7,7 +7,8 @@
package simulation.EESimulator;
import de.rwth.monticore.EmbeddedMontiArc.simulators.commons.controller.commons.BusEntry;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.HardwareEmulatorInterface;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.interfaces.*;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.config.ControllerConfig;
import org.apache.commons.math3.linear.RealVector;
import simulation.bus.Bus;
import simulation.bus.BusMessage;
......@@ -24,7 +25,7 @@ import static com.vividsolutions.jts.util.Memory.free;
* Controls the velocity and path of the vehicle.
* Calculates actuator values for brakes, engine, steering.
*/
public class DirectModelAsEEComponent extends ImmutableEEComponent {
public class ControllerAsEEComponent extends ImmutableEEComponent {
private static final int MAX_TRAJECTORY_LENGTH = 100;
/**
......@@ -73,7 +74,7 @@ public class DirectModelAsEEComponent extends ImmutableEEComponent {
/**
* Direct reference to the hardware emulator
*/
HardwareEmulatorInterface modelServer;
SoftwareSimulator softwareSimulator;
int modelId;
......@@ -90,20 +91,20 @@ public class DirectModelAsEEComponent extends ImmutableEEComponent {
/**
* Create a DirectModelAsEEComponent that is connected to bus.
* @param bus Bus that the DirectModelAsEEComponent is connected to
* @return DirectModelAsEEComponent with default configuration
* Create a ControllerAsEEComponent that is connected to bus.
* @param bus Bus that the ControllerAsEEComponent is connected to
* @return ControllerAsEEComponent with default configuration
*/
public static DirectModelAsEEComponent createDirectModelAsEEComponent(Bus bus) {
return createDirectModelAsEEComponent(Collections.singletonList(bus));
public static ControllerAsEEComponent createControllerAsEEComponent(Bus bus) {
return createControllerAsEEComponent(Collections.singletonList(bus));
}
/**
* Create a DirectModelAsEEComponent that is connected to bus.
* @param buses Buses that the DirectModelAsEEComponent is connected to
* @return DirectModelAsEEComponent with default configuration
* Create a ControllerAsEEComponent that is connected to bus.
* @param buses Buses that the ControllerAsEEComponent is connected to
* @return ControllerAsEEComponent with default configuration
*/
public static DirectModelAsEEComponent createDirectModelAsEEComponent(List<Bus> buses){
public static ControllerAsEEComponent createControllerAsEEComponent(List<Bus> buses){
HashMap<BusEntry, List<EEComponent>> targetsByMessageId = new HashMap<>();
targetsByMessageId.put(BusEntry.ACTUATOR_BRAKE, new LinkedList<>());
targetsByMessageId.put(BusEntry.ACTUATOR_STEERING, new LinkedList<>());
......@@ -119,11 +120,11 @@ public class DirectModelAsEEComponent extends ImmutableEEComponent {
targetsByMessageId.get(BusEntry.ACTUATOR_BRAKE).add(bus);
}
}
return new DirectModelAsEEComponent(buses.get(0).getSimulator(), targetsByMessageId);
return new ControllerAsEEComponent(buses.get(0).getSimulator(), targetsByMessageId);
}
public DirectModelAsEEComponent(EESimulator simulator, HashMap<BusEntry, List<EEComponent>> targetsByMessageId) {
public ControllerAsEEComponent(EESimulator simulator, HashMap<BusEntry, List<EEComponent>> targetsByMessageId) {
super(simulator, EEComponentType.AUTOPILOT, STANDARD_SUBSCRIBED_MESSAGES, targetsByMessageId);
}
......@@ -138,26 +139,22 @@ public class DirectModelAsEEComponent extends ImmutableEEComponent {
public void free(){
if (modelId >= 0){
modelServer.free_autopilot(modelId);
modelId = -1;
try {
softwareSimulator.free();
} catch(Exception e){
e.printStackTrace();
}
}
/**
* Set the HardwareEmulatorInterface, create the autopilot and set the cycle time.
* @param model_server
* @param autopilot_config
* Set the SoftwareSimulator interface, create the autopilot and set the cycle time.
* @param softwareSimManager
* @param controllerConfig
* @param cycleTime
* @throws Exception
*/
public void initializeController(HardwareEmulatorInterface model_server, String autopilot_config, Duration cycleTime) throws Exception {
this.modelServer = model_server;
this.modelId = model_server.alloc_autopilot(autopilot_config);
if (this.modelId < 0){
String error_msg = model_server.query("get_error_msg");
throw new Exception("Error allocating autopilot. Config:\n"+autopilot_config+"\n"+error_msg);
}
public void initializeController(SoftwareSimulatorManager softwareSimManager, ControllerConfig controllerConfig, Duration cycleTime) throws Exception {
this.softwareSimulator = softwareSimManager.newSimulator(controllerConfig);
this.cycleTime = cycleTime;
//add controller execute event
this.getSimulator().addEvent(new ControllerExecuteEvent(this.getSimulator().getSimulationTime().plus(cycleTime), this));
......@@ -218,17 +215,25 @@ public class DirectModelAsEEComponent extends ImmutableEEComponent {
}
if(wakeUpNeeded) {
executeController(event);
try {
executeController(event);
} catch (Exception e){
e.printStackTrace();
}
}
} else if (event.getEventType() == EEDiscreteEventTypeEnum.CONTROLLER_EXECUTE_EVENT) {
if(newInputs) {
executeController(event);
try {
executeController(event);
} catch (Exception e){
e.printStackTrace();
}
}
else {
wakeUpNeeded = true;
}
} else {
throw new IllegalArgumentException("DirectModelAsEEComponent expect BusMessage or ControllerExecuteEvent as event. Event was: " + event.getEventType());
throw new IllegalArgumentException("ControllerAsEEComponent expect BusMessage or ControllerExecuteEvent as event. Event was: " + event.getEventType());
}
}
......@@ -236,13 +241,12 @@ public class DirectModelAsEEComponent extends ImmutableEEComponent {
* Execute the controller once. Add a ControllerExecuteEvent for the next cycle.
* @param event
*/
private void executeController(EEDiscreteEvent event) {
private void executeController(EEDiscreteEvent event) throws Exception {
double timeIncrement = Duration.between(event.getEventTime(), lastFinishTime).toMillis();
this.inputs.put("timeIncrement", timeIncrement);
this.modelServer.set_inputs(modelId, inputs);
Duration delay = modelServer.time_execute(modelId, Duration.between(lastFinishTime, event.getEventTime()));
this.outputs = modelServer.get_outputs(modelId);
this.inputs.put("timeIncrement", timeIncrement);
this.softwareSimulator.setInputs(inputs);
Duration delay = this.softwareSimulator.runCycle();
this.outputs = this.softwareSimulator.getOutputs();
lastFinishTime = event.getEventTime().plus(delay);
Object engine = outputs.get("engine");
......
......@@ -33,14 +33,13 @@
<properties>
<!-- USE THIS VARIABLE TO SET THE VERSION OF THIS PROJECT AND ALL SUB PROJECTS -->
<revision>2.0.4</revision>
<revision>2.0.5</revision>
<!-- USE THESE TO SET THE VERSIONS OF THE DEPENDENCIES FOR ALL THE SUB-PROJECTS -->
<commons.version>1.0.11</commons.version>
<controller.version>1.0.4</controller.version>
<server.version>2.0.1</server.version>
<rmimodelserver.version>1.1.4</rmimodelserver.version>
<hardwareemulator.version>1.0.2</hardwareemulator.version>
<hardwareemulator.version>1.0.3</hardwareemulator.version>
<assembly.plugin>2.5.4</assembly.plugin>
<compiler.plugin>3.3</compiler.plugin>
......
......@@ -9,7 +9,8 @@ package simulation.vehicle;
import com.google.gson.Gson;
import de.rwth.monticore.EmbeddedMontiArc.simulators.commons.controller.commons.BusEntry;
import de.rwth.monticore.EmbeddedMontiArc.simulators.commons.controller.interfaces.FunctionBlockInterface;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.HardwareEmulatorInterface;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.interfaces.SoftwareSimulatorManager;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.config.ControllerConfig;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import sensors.abstractsensors.AbstractSensor;
......@@ -148,7 +149,7 @@ public class EEVehicleBuilder {
if (!busStructure.getController().isEmpty()) {
for (ParsableBusStructureProperties.Tupel controller : busStructure.getController()) {
DirectModelAsEEComponent newController = DirectModelAsEEComponent.createDirectModelAsEEComponent(busList.get(controller.busIds[0]));
ControllerAsEEComponent newController = ControllerAsEEComponent.createControllerAsEEComponent(busList.get(controller.busIds[0]));
newController.setCycleTime(Duration.parse(controller.parameter[0]));
components.add(newController);
}
......@@ -172,15 +173,15 @@ public class EEVehicleBuilder {
return createNavigation(Collections.singletonList(bus));
}
public DirectModelAsEEComponent createController(HardwareEmulatorInterface modelServer, String autopilotConfig, List<Bus> buses) {
public ControllerAsEEComponent createController(SoftwareSimulatorManager softwareSimManager, ControllerConfig controllerConfig, List<Bus> buses) {
List<EEComponent> targets = new ArrayList<EEComponent>(buses);
HashMap<BusEntry, List<EEComponent>> targetsByMessageId = new HashMap<BusEntry, List<EEComponent>>();
for(BusEntry busEntry : DirectModelAsEEComponent.MASSPOINT_OUTPUT_MESSAGES) {
for(BusEntry busEntry : ControllerAsEEComponent.MASSPOINT_OUTPUT_MESSAGES) {
targetsByMessageId.put(busEntry, targets);
}
DirectModelAsEEComponent comp = new DirectModelAsEEComponent(eeSimulator, targetsByMessageId);
ControllerAsEEComponent comp = new ControllerAsEEComponent(eeSimulator, targetsByMessageId);
try {
comp.initializeController(modelServer, autopilotConfig, Duration.ofMillis(30));
comp.initializeController(softwareSimManager, controllerConfig, Duration.ofMillis(30));
} catch (Exception e) {
throw new IllegalStateException("Controller could not be created. " + e.getMessage() + "\r\n" + e.getStackTrace());
}
......@@ -188,8 +189,8 @@ public class EEVehicleBuilder {
return comp;
}
public DirectModelAsEEComponent createController(HardwareEmulatorInterface modelServer, String autopilotConfig, Bus bus) {
return createController(modelServer, autopilotConfig, Collections.singletonList(bus));
public ControllerAsEEComponent createController(SoftwareSimulatorManager softwareSimManager, ControllerConfig controllerConfig, Bus bus) {
return createController(softwareSimManager, controllerConfig, Collections.singletonList(bus));
}
public void createSensor(BusEntry sensorType, List<Bus> buses) {
......@@ -245,6 +246,19 @@ public class EEVehicleBuilder {
return createMassPointActuators(Collections.singletonList(bus));
}
public List<VehicleActuator> createModelicaActuators(List<Bus> buses){
List<VehicleActuator> comps = new ArrayList<VehicleActuator>();
comps.add(createActuator(VehicleActuatorType.VEHICLE_ACTUATOR_TYPE_BRAKE, buses));
comps.add(createActuator(VehicleActuatorType.VEHICLE_ACTUATOR_TYPE_CLUTCH, buses));
comps.add(createActuator(VehicleActuatorType.VEHICLE_ACTUATOR_TYPE_GEAR, buses));
comps.add(createActuator(VehicleActuatorType.VEHICLE_ACTUATOR_TYPE_THROTTLE, buses));
return comps;
}
public List<VehicleActuator> createModelicaActuators(Bus bus){
return createModelicaActuators(Collections.singletonList(bus));
}
public void createControllerSensors(List<Bus> buses){
createSensor(BusEntry.SENSOR_VELOCITY, buses);
createSensor(BusEntry.SENSOR_GPS_COORDINATES, buses);
......@@ -388,7 +402,7 @@ class ParsableBusStructureProperties {
List<Bridge> processedBridges = new LinkedList<>();
List<AbstractSensor> processedSensors = new LinkedList<>();
List<VehicleActuator> processedActuators = new LinkedList<>();
List<DirectModelAsEEComponent> processedController = new LinkedList<>();
List<ControllerAsEEComponent> processedController = new LinkedList<>();
//id to assign component to the busAndParameter and index of the busAndParameter in busList of the eeVehicle
int busId = 0;
......@@ -420,7 +434,7 @@ class ParsableBusStructureProperties {
actuators.add(new Tupel(((VehicleActuator) component).getActuatorType().toString(), busIdArr, paramAct));
break;
case AUTOPILOT:
String[] paramController = {((DirectModelAsEEComponent) component).getCycleTime().toString()};
String[] paramController = {((ControllerAsEEComponent) component).getCycleTime().toString()};
controller.add(new Tupel("autopilot", busIdArr, paramController));
break;
case NAVIGATION:
......
......@@ -346,7 +346,9 @@ public abstract class PhysicalVehicle implements IPhysicalVehicle, SimulationLoo
Optional<VehicleActuator> actuator = vehicle.getEEVehicle().getActuator(type);
if(actuator.isPresent()) {
actuators.add(actuator.get());
}
} else {
throw new IllegalStateException("EEVehicle does not contain actuator: " + type);
}
}
if(actuatorTypes.length > actuators.size()) {
throw new IllegalStateException("EEVehicle does not contain all necessary actuators");
......
......@@ -21,8 +21,10 @@ import org.junit.Test;
import com.vividsolutions.jts.util.Assert;
import de.rwth.monticore.EmbeddedMontiArc.simulators.commons.controller.commons.BusEntry;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.HardwareEmulatorInterface;
import simulation.EESimulator.DirectModelAsEEComponent;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.interfaces.SoftwareSimulatorManager;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.config.*;
import de.rwth.monticore.EmbeddedMontiArc.simulators.hardware_emulator.DirectSoftwareSimulatorManager;
import simulation.EESimulator.ControllerAsEEComponent;
import simulation.EESimulator.EEComponent;
import simulation.EESimulator.EEDiscreteEvent;
import simulation.EESimulator.EEDiscreteEventTypeEnum;
......@@ -33,20 +35,19 @@ import simulation.bus.InstantBus;
public class ControllerTest {
//hardcoded config
private final String AUTOPILOT_CONFIG = "autopilot=AutopilotAdapter\n"+
"os=windows\n" +
"no_time=true";
private final ControllerConfig AUTOPILOT_CONFIG = new ControllerConfig(ControllerConfig.EmulatorType.HARDWARE_EMULATOR, "AutopilotAdapter")
.set_os(ControllerConfig.OS.WINDOWS).set_timemodel_constant(Duration.ofMillis(60));
@Test
public void testController() throws Exception {
HardwareEmulatorInterface modelServer = new HardwareEmulatorInterface("autopilots_folder=autopilots", "");
SoftwareSimulatorManager softwareSimulatorManager = new DirectSoftwareSimulatorManager(new SoftwareSimulatorConfig().set_softwares_folder("autopilots"));
PhysicalVehicleBuilder physicalVehicleBuilder = new MassPointPhysicalVehicleBuilder();
EESimulator eeSimulator = new EESimulator(Instant.EPOCH);
EEVehicleBuilder eeVehicleBuilder = new EEVehicleBuilder(eeSimulator);
InstantBus bus = new InstantBus(eeSimulator);
eeVehicleBuilder.createControllerSensors(bus);
eeVehicleBuilder.createMassPointActuators(bus);
DirectModelAsEEComponent ecu = eeVehicleBuilder.createController(modelServer, AUTOPILOT_CONFIG, bus);
ControllerAsEEComponent ecu = eeVehicleBuilder.createController(softwareSimulatorManager, AUTOPILOT_CONFIG, bus);
Vehicle vehicle = new Vehicle(physicalVehicleBuilder, eeVehicleBuilder);
EEVehicle eeVehicle = vehicle.getEEVehicle();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment