Commit a3bb1a73 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

erp5_officejs_drone_simulator: split updates

* Seperate onUpdate and info_update execution as they are two parallel processes on hardware side.
* Fix onUpdate loop duration:
  Until now even if the onUpdate function execution took more time than the minimum interval
  between 2 executions, the simulated time was only moving by steps equal to the minimum interval.
parent 521e2593
......@@ -27,7 +27,10 @@ var DroneManager = /** @class */ (function () {
this._maxClimbRate = 0;
this._maxCommandFrequency = 0;
this._last_command_timestamp = 0;
this._onupdate_count = 0;
this._lastInfoUpdateTime = 0;
this._lastOnUpdateTime = 0;
this._onUpdateExecutionDuration = 0;
this._timeDifference = 0;
this._speed = 0;
this._acceleration = 0;
this._direction = new BABYLON.Vector3(0, 0, 1); // North
......@@ -194,26 +197,33 @@ var DroneManager = /** @class */ (function () {
return;
};
DroneManager.prototype.internal_update = function (delta_time) {
var context = this, gameManager = this._API._gameManager;
var context = this, time = context._API._gameManager.getCurrentTime(),
min_interval = context._API.getOnUpdateInterval(),
onUpdateInterval = Math.max(min_interval,
this._onUpdateExecutionDuration);
if (this._controlMesh) {
context._API.internal_position_update(context, delta_time);
if (context._canUpdate &&
gameManager._game_duration / (context._API.getOnUpdateInterval() * this._onupdate_count) >= 1) {
if (time - this._lastInfoUpdateTime >= min_interval) {
context._API.internal_info_update(context);
this._lastInfoUpdateTime = time;
}
if (context._canUpdate
&& time - this._lastOnUpdateTime >= onUpdateInterval) {
context._canUpdate = false;
this._onupdate_count += 1;
this._lastOnUpdateTime = time;
this._timeDifference = time - Date.now();
return new RSVP.Queue()
.push(function () {
return context.onUpdate(context._API._gameManager.getCurrentTime());
})
.push(function () {
context._onUpdateExecutionDuration =
Date.now() + context._timeDifference - time;
context._canUpdate = true;
}, function (error) {
console.warn('Drone crashed on update due to error:', error);
context._internal_crash(error);
})
.push(function () {
context._API.internal_info_update(context);
})
.push(undefined, function (error) {
console.warn('Drone crashed on update due to error:', error);
context._internal_crash(error);
......@@ -1337,7 +1347,7 @@ var GameManager = /** @class */ (function () {
" GameManager, FixedWingDroneAPI, EnemyDroneAPI, BABYLON, " +
"GAMEPARAMETERS) {" +
"Date.now = function () {" +
"return me._API._gameManager.getCurrentTime();}; " +
"return NativeDate.now() + me._timeDifference;}; " +
"function Date() {if (!(this instanceof Date)) " +
"{throw new Error('Missing new operator');} " +
"if (arguments.length === 0) {return new NativeDate(Date.now());} " +
......
......@@ -246,7 +246,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1015.63156.48418.4590</string> </value>
<value> <string>1015.64427.62695.34013</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -266,7 +266,7 @@
</tuple>
<state>
<tuple>
<float>1713426083.91</float>
<float>1713445716.26</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -29,12 +29,18 @@
NUMBER_OF_DRONES = 1,
// Non-inputs parameters
DEFAULT_SCRIPT_CONTENT =
'function assert(a, b, msg) {\n' +
' if (a === b)\n' +
'function assert(result, msg) {\n' +
' if (result)\n' +
' console.log(msg + ": OK");\n' +
' else\n' +
' console.log(msg + ": FAIL");\n' +
'}\n' +
'function assertEqual(a, b, msg) {\n' +
' assert(a === b, msg);\n' +
'}\n' +
'function assertGreaterOrEqual(a, b, msg) {\n' +
' assert(a >= b, msg);\n' +
'}\n' +
'\n' +
'function distance(lat1, lon1, lat2, lon2) {\n' +
' var R = 6371e3, // meters\n' +
......@@ -49,14 +55,14 @@
'}\n' +
'\n' +
'function compare(coord1, coord2) {\n' +
' assert(coord1.latitude, coord2.latitude, "Latitude")\n' +
' assert(coord1.longitude, coord2.longitude, "Longitude")\n' +
' assert(coord1.altitude, coord2.altitude, "Altitude")\n' +
' assertEqual(coord1.latitude, coord2.latitude, "Latitude")\n' +
' assertEqual(coord1.longitude, coord2.longitude, "Longitude")\n' +
' assertEqual(coord1.altitude, coord2.altitude, "Altitude")\n' +
'}\n' +
'\n' +
'me.onStart = function (timestamp) {\n' +
' assert(me.getSpeed(), ' + DEFAULT_SPEED + ', "Initial speed");\n' +
' assert(me.getYaw(), 0, "Yaw angle")\n' +
' assertEqual(me.getSpeed(), ' + DEFAULT_SPEED + ', "Initial speed");\n' +
' assertEqual(me.getYaw(), 0, "Yaw angle")\n' +
' me.initialPosition = me.getCurrentPosition();\n' +
' me.start_time = timestamp;\n' +
' me.setTargetCoordinates(\n' +
......@@ -78,9 +84,9 @@
' time_interval = timestamp - me.start_time,\n' +
' expected_interval = ' + LOOP_INTERVAL + ',\n' +
' expectedDistance = (me.getSpeed() * expected_interval / 1000).toFixed(8);\n' +
' assert(time_interval.toFixed(4), expected_interval.toFixed(4), "Timestamp");\n' +
' assert(Date.now(), timestamp, "Date");\n' +
' assert(realDistance, expectedDistance, "Distance");\n' +
' assertEqual(time_interval.toFixed(4), expected_interval.toFixed(4), "Timestamp");\n' +
' assertGreaterOrEqual(Date.now(), timestamp, "Date.now");\n' +
' assertEqual(realDistance, expectedDistance, "Distance");\n' +
' current_position.latitude = current_position.latitude.toFixed(7);\n' +
' compare(current_position, {\n' +
' latitude: (me.initialPosition.latitude + 2.3992831666911723e-06).toFixed(7),\n' +
......
......@@ -246,7 +246,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1015.64187.34381.50346</string> </value>
<value> <string>1015.64310.38354.42888</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -266,7 +266,7 @@
</tuple>
<state>
<tuple>
<float>1713428877.4</float>
<float>1713436266.44</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -27,7 +27,10 @@ var DroneManager = /** @class */ (function () {
this._maxClimbRate = 0;
this._maxCommandFrequency = 0;
this._last_command_timestamp = 0;
this._onupdate_count = 0;
this._lastInfoUpdateTime = 0;
this._lastOnUpdateTime = 0;
this._onUpdateExecutionDuration = 0;
this._timeDifference = 0;
this._speed = 0;
this._acceleration = 0;
this._direction = new BABYLON.Vector3(0, 0, 1); // North
......@@ -169,26 +172,33 @@ var DroneManager = /** @class */ (function () {
);
};
DroneManager.prototype.internal_update = function (delta_time) {
var context = this, gameManager = this._API._gameManager;
var context = this, time = context._API._gameManager.getCurrentTime(),
min_interval = context._API.getOnUpdateInterval(),
onUpdateInterval = Math.max(min_interval,
this._onUpdateExecutionDuration);
if (this._controlMesh) {
context._API.internal_position_update(context, delta_time);
if (context._canUpdate &&
gameManager._game_duration / (context._API.getOnUpdateInterval() * this._onupdate_count) >= 1) {
if (time - this._lastInfoUpdateTime >= min_interval) {
context._API.internal_info_update(context);
this._lastInfoUpdateTime = time;
}
if (context._canUpdate
&& time - this._lastOnUpdateTime >= onUpdateInterval) {
context._canUpdate = false;
this._onupdate_count += 1;
this._lastOnUpdateTime = time;
this._timeDifference = time - Date.now();
return new RSVP.Queue()
.push(function () {
return context.onUpdate(context._API._gameManager.getCurrentTime());
return context.onUpdate(time);
})
.push(function () {
context._onUpdateExecutionDuration =
Date.now() + context._timeDifference - time;
context._canUpdate = true;
}, function (error) {
console.warn('Drone crashed on update due to error:', error);
context._internal_crash(error);
})
.push(function () {
context._API.internal_info_update(context);
})
.push(undefined, function (error) {
console.warn('Drone crashed on update due to error:', error);
context._internal_crash(error);
......@@ -1005,7 +1015,7 @@ var GameManager = /** @class */ (function () {
" GameManager, DroneLogAPI, FixedWingDroneAPI, BABYLON, " +
"GAMEPARAMETERS) {" +
"Date.now = function () {" +
"return me._API._gameManager.getCurrentTime();}; " +
"return NativeDate.now() + me._timeDifference;}; " +
"function Date() {if (!(this instanceof Date)) " +
"{throw new Error('Missing new operator');} " +
"if (arguments.length === 0) {return new NativeDate(Date.now());} " +
......
......@@ -240,7 +240,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1015.64164.6836.50824</string> </value>
<value> <string>1015.64485.49461.15035</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -260,7 +260,7 @@
</tuple>
<state>
<tuple>
<float>1713427455.14</float>
<float>1713446819.35</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -29,12 +29,18 @@
INIT_ALT = 15,
// Non-inputs parameters
DEFAULT_SCRIPT_CONTENT =
'function assert(a, b, msg) {\n' +
' if (a === b)\n' +
'function assert(result, msg) {\n' +
' if (result)\n' +
' console.log(msg + ": OK");\n' +
' else\n' +
' console.log(msg + ": FAIL");\n' +
'}\n' +
'function assertEqual(a, b, msg) {\n' +
' assert(a === b, msg);\n' +
'}\n' +
'function assertGreaterOrEqual(a, b, msg) {\n' +
' assert(a >= b, msg);\n' +
'}\n' +
'\n' +
'function distance(lat1, lon1, lat2, lon2) {\n' +
' var R = 6371e3, // meters\n' +
......@@ -49,14 +55,14 @@
'}\n' +
'\n' +
'function compare(coord1, coord2) {\n' +
' assert(coord1.latitude, coord2.latitude, "Latitude")\n' +
' assert(coord1.longitude, coord2.longitude, "Longitude")\n' +
' assert(coord1.altitude, coord2.altitude, "Altitude")\n' +
' assertEqual(coord1.latitude, coord2.latitude, "Latitude")\n' +
' assertEqual(coord1.longitude, coord2.longitude, "Longitude")\n' +
' assertEqual(coord1.altitude, coord2.altitude, "Altitude")\n' +
'}\n' +
'\n' +
'me.onStart = function (timestamp) {\n' +
' assert(me.getSpeed(), ' + DEFAULT_SPEED + ', "Initial speed");\n' +
' assert(me.getYaw(), 0, "Yaw angle")\n' +
' assertEqual(me.getSpeed(), ' + DEFAULT_SPEED + ', "Initial speed");\n' +
' assertEqual(me.getYaw(), 0, "Yaw angle")\n' +
' me.initialPosition = me.getCurrentPosition();\n' +
' me.start_time = timestamp;\n' +
' me.setTargetCoordinates(\n' +
......@@ -78,9 +84,9 @@
' time_interval = timestamp - me.start_time,\n' +
' expected_interval = ' + LOOP_INTERVAL + ',\n' +
' expectedDistance = (me.getSpeed() * expected_interval / 1000).toFixed(8);\n' +
' assert(time_interval.toFixed(4), expected_interval.toFixed(4), "Timestamp");\n' +
' assert(Date.now(), timestamp, "Date");\n' +
' assert(realDistance, expectedDistance, "Distance");\n' +
' assertEqual(time_interval.toFixed(4), expected_interval.toFixed(4), "Timestamp");\n' +
' assertGreaterOrEqual(Date.now(), timestamp, "Date.now");\n' +
' assertEqual(realDistance, expectedDistance, "Distance");\n' +
' current_position.latitude = current_position.latitude.toFixed(7);\n' +
' compare(current_position, {\n' +
' latitude: (me.initialPosition.latitude + 2.3992831666911723e-06).toFixed(7),\n' +
......
......@@ -246,7 +246,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1015.64176.45813.63488</string> </value>
<value> <string>1015.64296.21761.36249</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -266,7 +266,7 @@
</tuple>
<state>
<tuple>
<float>1713428376.53</float>
<float>1713435475.32</float>
<string>UTC</string>
</tuple>
</state>
......
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