Commit 7f97381b authored by Jean Meurice's avatar Jean Meurice
Browse files

Serialization Fixes

parent 15e8b9e6
Pipeline #307411 passed with stage
in 1 minute and 5 seconds
......@@ -126,7 +126,7 @@ public class EESimulator extends DiscreteEventSimulator<EEEventType, EEDiscreteE
throw new ParsingException("Unknown component: " + name);
Json.fromJson(t, o.get(), context);
}
} else if (e.key.equals(K_COMPONENTS)) {
} else if (e.key.equals(K_EVENTS)) {
for (ValueType vt : t.streamArray()) {
eventList.offer(Json.instantiateFromJson(t, EventData.class, context).getEvent(componentManager));
}
......
......@@ -27,6 +27,8 @@ public class ActuatorProperties extends EEComponentProperties {
this.change_rate = changeRate;
}
protected ActuatorProperties() {}
// TODO
// public ActuatorProperties setName(String name) {
// this.name = name;
......
/* (c) https://github.com/MontiCore/monticore */
package de.rwth.montisim.simulation.eesimulator.sensor;
import java.lang.reflect.InvocationTargetException;
import java.time.Instant;
import de.rwth.montisim.commons.simulation.PhysicalValue;
import de.rwth.montisim.commons.simulation.TimeUpdate;
import de.rwth.montisim.commons.simulation.Updatable;
import de.rwth.montisim.commons.utils.json.CustomJson;
import de.rwth.montisim.commons.utils.json.Json;
import de.rwth.montisim.commons.utils.json.JsonTraverser;
import de.rwth.montisim.commons.utils.json.JsonTraverser.Entry;
import de.rwth.montisim.commons.utils.json.JsonTraverser.ObjectIterable;
import de.rwth.montisim.commons.utils.json.JsonWriter;
import de.rwth.montisim.commons.utils.json.SerializationContext;
import de.rwth.montisim.simulation.eesimulator.components.EEComponent;
import de.rwth.montisim.simulation.eesimulator.exceptions.EEMessageTypeException;
import de.rwth.montisim.simulation.eesimulator.message.MessageInformation;
public class SensorLogic implements Updatable {
public class SensorLogic implements Updatable, CustomJson {
final transient PhysicalValue watchedValue;
final transient SensorProperties properties;
Instant nextUpdate = Instant.EPOCH; // Time after which the sensor can send its next value
Object lastValue = null; // TODO serialize using type
......@@ -32,10 +40,10 @@ public class SensorLogic implements Updatable {
@Override
public void update(TimeUpdate newTime) {
if (newTime.oldTime.isAfter(nextUpdate)){
if (newTime.oldTime.isAfter(nextUpdate)) {
this.nextUpdate = newTime.oldTime.plus(properties.update_interval);
Object readValue = readValue();
if (!properties.send_only_changed || hasChanged(readValue)){
if (!properties.send_only_changed || hasChanged(readValue)) {
lastValue = readValue;
component.sendMessage(newTime.oldTime.plus(properties.read_time), msgInfo, readValue);
}
......@@ -49,7 +57,32 @@ public class SensorLogic implements Updatable {
}
// Can be overwritten to add noise, bias, ...
public Object readValue(){
public Object readValue() {
return watchedValue.get();
}
private static final String K_NEXT_UPDATE = "nextUpdate";
private static final String K_LAST_VALUE = "lastValue";
@Override
public void write(JsonWriter w, SerializationContext context) throws IllegalAccessException {
w.startObject();
w.writeKey(K_NEXT_UPDATE);
Json.toJson(w, nextUpdate, context);
if (properties.send_only_changed){
w.writeKey(K_LAST_VALUE);
watchedValue.type.toJson(w, lastValue, context);
}
w.endObject();
}
@Override
public void read(JsonTraverser t, ObjectIterable it, SerializationContext context)
throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
for (Entry e : t.streamObject()){
if (e.key.equals(K_NEXT_UPDATE)) nextUpdate = Json.instantiateFromJson(t, Instant.class, context);
else if (e.key.equals(K_LAST_VALUE)) lastValue = watchedValue.type.fromJson(t, context);
else t.unexpected(e);
}
}
}
\ No newline at end of file
......@@ -24,6 +24,8 @@ public class SensorProperties extends EEComponentProperties {
this.send_only_changed = sendOnlyChanged;
}
protected SensorProperties() {}
public SensorProperties setName(String name) {
this.name = name;
return this;
......
......@@ -10,6 +10,10 @@ public class Path {
trajectoryX = new double[length];
trajectoryY = new double[length];
}
protected Path() {
trajectoryX = null;
trajectoryY = null;
}
public int getLength(){
return trajectoryX.length;
}
......
{
"config": {
"name": "UnnamedCar",
"body": {
"mass": 1642.0,
"length": 4.971,
"width": 1.87,
"height": 1.383,
"center_of_gravity_height": 0.34575
},
"wheels": {
"diameter": 0.6612,
"width": 0.255,
"front_wheel_axis_offset": 1.0,
"max_turning_angle": 30.0,
"front_track_width": 1.57988,
"back_track_dist": 1.60274,
"wheelbase": 2.812,
"front_wheels_depth": 1.406,
"back_wheels_depth": 1.406
},
"powertrain": {
"type": "electrical",
"traction": "rear",
"braking": "rear",
"max_braking_force": 5000.0,
"motor": {
"motor_efficiency": 0.7,
"regen_efficiency": 0.5,
"motor_peek_torque": 450.0
},
"battery": {
"type": "infinite",
"capacity": 1.8E8,
"critical_charge": 10.0
},
"transmission_ratio": 9.0
},
"physics": {
"type": "rigidbody"
},
"components": [
{
"type": "actuator",
"name": "SteeringActuator",
"priority": {},
"buses": [
"DefaultBus"
],
"physical_value_name": "steering",
"min": -30.0,
"max": 30.0,
"change_rate": 60.0,
"sensor": {
"type": "sensor",
"name": "UnnamedComponent",
"priority": {},
"buses": [
"DefaultBus"
],
"update_interval": [
0,
100000000
],
"read_time": [
0,
1000000
],
"send_only_changed": false
}
},
{
"type": "actuator",
"name": "BrakingActuator",
"priority": {},
"buses": [
"DefaultBus"
],
"physical_value_name": "braking",
"min": 0.0,
"max": 1.0,
"change_rate": 10.0,
"sensor": {
"type": "sensor",
"name": "UnnamedComponent",
"priority": {},
"buses": [
"DefaultBus"
],
"update_interval": [
0,
100000000
],
"read_time": [
0,
1000000
],
"send_only_changed": false
}
},
{
"type": "actuator",
"name": "GasActuator",
"priority": {},
"buses": [
"DefaultBus"
],
"physical_value_name": "gas",
"min": -0.5,
"max": 1.0,
"change_rate": "Infinity",
"sensor": {
"type": "sensor",
"name": "UnnamedComponent",
"priority": {},
"buses": [
"DefaultBus"
],
"update_interval": [
0,
100000000
],
"read_time": [
0,
1000000
],
"send_only_changed": false
}
},
{
"type": "sensor",
"name": "TrueVelocitySensor",
"priority": {},
"buses": [
"DefaultBus"
],
"physical_value_name": "true_velocity",
"update_interval": [
0,
100000000
],
"read_time": [
0,
10000000
],
"send_only_changed": false
},
{
"type": "sensor",
"name": "TruePositionSensor",
"priority": {},
"buses": [
"DefaultBus"
],
"physical_value_name": "true_position",
"update_interval": [
0,
100000000
],
"read_time": [
0,
10000000
],
"send_only_changed": false
},
{
"type": "sensor",
"name": "TrueCompassSensor",
"priority": {},
"buses": [
"DefaultBus"
],
"physical_value_name": "true_compass",
"update_interval": [
0,
100000000
],
"read_time": [
0,
10000000
],
"send_only_changed": false
},
{
"type": "navigation",
"name": "Navigation",
"priority": {},
"buses": [
"DefaultBus"
]
},
{
"type": "constant_bus",
"name": "DefaultBus",
"priority": {},
"buses": [
"DefaultBus"
],
"mode": "instant",
"rate": 0.0
},
{
"type": "java_autopilot",
"name": "TestAutopilot",
"priority": {},
"buses": [
"DefaultBus"
],
"compute_time": [
0,
0
]
}
]
},
"state": {
"eesimulator": {
"components": {
"SteeringActuator": {
"sensor": {
"nextUpdate": [
6,
50000000
]
},
"targetValue": 48.69053678498651
},
"BrakingActuator": {
"sensor": {
"nextUpdate": [
6,
50000000
]
},
"targetValue": 0.0
},
"GasActuator": {
"sensor": {
"nextUpdate": [
6,
50000000
]
},
"targetValue": "-Infinity"
},
"TrueVelocitySensor": {
"logic": {
"nextUpdate": [
6,
50000000
]
}
},
"TruePositionSensor": {
"logic": {
"nextUpdate": [
6,
50000000
]
}
},
"TrueCompassSensor": {
"logic": {
"nextUpdate": [
6,
50000000
]
}
},
"Navigation": {
"targets": [
[
-63.83,
-171.96
]
],
"currentPath": {
"trajectoryX": [
18,
0.0,
-2.3721876315154202,
-2.1798136749816006,
4.211962552465172,
4.289310779669538,
3.1923722851743537,
-9.68962336796794,
-18.338561497475773,
-23.091961640246605,
-42.70325305754577,
-45.76905551645384,
-49.706783445404504,
-76.86304284074316,
-92.37487821783468,
-107.28902274852298,
-76.17394045318929,
-63.41763696847143,
-63.83
],
"trajectoryY": [
18,
0.0,
-0.12404716846665131,
-3.8028664914396786,
-3.4692817115581356,
-7.083116827073201,
-8.372977976211862,
-11.419718966323348,
-13.64361749913033,
-15.689604149597187,
-30.64532178345498,
-33.9255721190885,
-38.32889121431496,
-75.87941794250301,
-97.71810153550507,
-114.73092531183404,
-155.4727464348806,
-171.6345266987817,
-171.96
]
},
"currentPos": [
-19.889621889067694,
-10.921222990568737
]
},
"DefaultBus": {},
"TestAutopilot": {
"currentVelocity": 30.7304439181972,
"currentPosition": [
-19.889621889067694,
-10.921222990568737
],
"currentCompass": -144.49731009228105,
"newTrajX": [
10,
-18.338561497475773,
-23.091961640246605,
-42.70325305754577,
-45.76905551645384,
-49.706783445404504,
-76.86304284074316,
-92.37487821783468,
-107.28902274852298,
-76.17394045318929,
-63.41763696847143
],
"trajX": [
10,
-18.338561497475773,
-23.091961640246605,
-42.70325305754577,
-45.76905551645384,
-49.706783445404504,
-76.86304284074316,
-92.37487821783468,
-107.28902274852298,
-76.17394045318929,
-63.41763696847143
],
"trajY": [
10,
-13.64361749913033,
-15.689604149597187,
-30.64532178345498,
-33.9255721190885,
-38.32889121431496,
-75.87941794250301,
-97.71810153550507,
-114.73092531183404,
-155.4727464348806,
-171.6345266987817
],
"lastTime": [
0,
130000000
],
"currSeg": {
"posStart": [
-18.338561497475773,
-13.64361749913033
],
"posEnd": [
-23.091961640246605,
-15.689604149597187
],
"dir": [
-0.9185271534456512,
-0.39535789910286273
],
"normal": [
0.39535789910286273,
-0.9185271534456512
],
"relPos": [
-1.5510603915919212,
2.722394508561594
],
"length": 5.1750240860485235,
"projPos": 0.34837091287714195,
"orthoPos": -3.113817256306599,
"dist": 3.113817256306599,
"projDistToEnd": 4.8266531731713815
},
"nextSeg": {
"posStart": [
-23.091961640246605,
-15.689604149597187
],
"posEnd": [
-42.70325305754577,
-30.64532178345498
],
"dir": [
-0.7951622729911606,
-0.6063967015176872
],
"normal": [
0.6063967015176872,
-0.7951622729911606
],
"relPos": [
3.2023397511789113,
4.768381159028451
],
"length": 24.663256901672916,
"projPos": -5.437910361851309,
"orthoPos": -1.8497485386474244,
"dist": 1.8497485386474244,
"projDistToEnd": 30.101167263524225
},
"relPos": [
0.0,
0.0
],
"targetDir": [
0.7722172602623475,
0.6353585625085365
],
"carDir": [
-0.8853785084027052,
-0.46487083889893643
],
"mode": 0,
"target": 1
}
},
"events": []
},
"powerTrain": {
"steering": 30.0,
"braking": 0.0,
"gas": -0.5,
"battery": {
"charge": "Infinity"
}
},
"physicsModel": {},
"physicalObject": {
"type": "rigidbody",
"pos": [
-20.342159292421346,
-11.557891975265491,
0.6815323409375669
],
"rotation": [
[
-0.7467040639151694,
-0.6651544697253815,
-0.0016038506530543209
],
[
0.6651452847965487,
-0.7467033532626417,
0.003981499622540266
],
[
-0.0038459127488009977,
0.001906208279879593,
0.999990787620127
]
],
"bbox": {
"type": "obb",
"offset": [
0.0,