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

erp5_officejs_drone_simulator: add loop interval parameter

Loop interval is the time in milliseconds between 2 executions of the
onUpdate function of the user script as well as the periodicity to send
messages to the swarm (which means updating telemetry values).
As this is something we can set in SlapOS mostly to handle bandwith
usage, this must not be linked to the time delta used to update drone
position in the simulation.
parent 25f8b070
...@@ -102,7 +102,7 @@ var FixedWingDroneAPI = /** @class */ (function () { ...@@ -102,7 +102,7 @@ var FixedWingDroneAPI = /** @class */ (function () {
/* /*
** Function called on every drone update, right before onUpdate AI script ** Function called on every drone update, right before onUpdate AI script
*/ */
FixedWingDroneAPI.prototype.internal_update = function (context, delta_time) { FixedWingDroneAPI.prototype.internal_position_update = function (context, delta_time) {
this._updateSpeed(context, delta_time); this._updateSpeed(context, delta_time);
this._updatePosition(context, delta_time); this._updatePosition(context, delta_time);
...@@ -112,7 +112,7 @@ var FixedWingDroneAPI = /** @class */ (function () { ...@@ -112,7 +112,7 @@ var FixedWingDroneAPI = /** @class */ (function () {
/* /*
** Function called on every drone update, right after onUpdate AI script ** Function called on every drone update, right after onUpdate AI script
*/ */
FixedWingDroneAPI.prototype.internal_post_update = function (drone) { FixedWingDroneAPI.prototype.internal_info_update = function (drone) {
var _this = this, drone_position = drone.getCurrentPosition(), drone_info; var _this = this, drone_position = drone.getCurrentPosition(), drone_info;
/*if (_this._start_altitude > 0) { //TODO move start_altitude here /*if (_this._start_altitude > 0) { //TODO move start_altitude here
_this.reachAltitude(drone); _this.reachAltitude(drone);
......
...@@ -240,7 +240,7 @@ ...@@ -240,7 +240,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1015.19909.43325.57463</string> </value> <value> <string>1015.21415.54357.41250</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1710776370.92</float> <float>1710947081.61</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -27,6 +27,7 @@ var DroneManager = /** @class */ (function () { ...@@ -27,6 +27,7 @@ var DroneManager = /** @class */ (function () {
this._maxClimbRate = 0; this._maxClimbRate = 0;
this._maxCommandFrequency = 0; this._maxCommandFrequency = 0;
this._last_command_timestamp = 0; this._last_command_timestamp = 0;
this._loop_count = 0;
this._speed = 0; this._speed = 0;
this._acceleration = 0; this._acceleration = 0;
this._direction = new BABYLON.Vector3(0, 0, 1); // North this._direction = new BABYLON.Vector3(0, 0, 1); // North
...@@ -124,7 +125,7 @@ var DroneManager = /** @class */ (function () { ...@@ -124,7 +125,7 @@ var DroneManager = /** @class */ (function () {
this._canCommunicate = true; this._canCommunicate = true;
this._targetCoordinates = initial_position; this._targetCoordinates = initial_position;
try { try {
return this.onStart(this._API._gameManager._game_duration); return this.onStart(this._API._gameManager._start_time);
} catch (error) { } catch (error) {
console.warn('Drone crashed on start due to error:', error); console.warn('Drone crashed on start due to error:', error);
this._internal_crash(error); this._internal_crash(error);
...@@ -132,17 +133,18 @@ var DroneManager = /** @class */ (function () { ...@@ -132,17 +133,18 @@ var DroneManager = /** @class */ (function () {
}; };
DroneManager.prototype._callSetTargetCommand = DroneManager.prototype._callSetTargetCommand =
function (latitude, longitude, altitude, speed, radius) { function (latitude, longitude, altitude, speed, radius) {
var current_time = this._API._gameManager.getCurrentTime();
if (!this.isReadyToFly()) { if (!this.isReadyToFly()) {
return; return;
} }
if (this._API._gameManager._game_duration - this._last_command_timestamp if (current_time - this._last_command_timestamp
< 1000 / this._API.getMaxCommandFrequency()) { < 1000 / this._API.getMaxCommandFrequency()) {
this._internal_crash(new Error('Minimum interval between commands is ' + this._internal_crash(new Error('Minimum interval between commands is ' +
1000 / this._API.getMaxCommandFrequency() + ' milliseconds')); 1000 / this._API.getMaxCommandFrequency() + ' milliseconds'));
} }
this._internal_setTargetCoordinates(latitude, longitude, altitude, speed, this._internal_setTargetCoordinates(latitude, longitude, altitude, speed,
radius); radius);
this._last_command_timestamp = this._API._gameManager._game_duration; this._last_command_timestamp = current_time;
}; };
/** /**
* Set a target point to move * Set a target point to move
...@@ -167,14 +169,16 @@ var DroneManager = /** @class */ (function () { ...@@ -167,14 +169,16 @@ var DroneManager = /** @class */ (function () {
); );
}; };
DroneManager.prototype.internal_update = function (delta_time) { DroneManager.prototype.internal_update = function (delta_time) {
var context = this; var context = this, gameManager = this._API._gameManager;
if (this._controlMesh) { if (this._controlMesh) {
context._API.internal_update(context, delta_time); context._API.internal_position_update(context, delta_time);
if (context._canUpdate) { if (context._canUpdate &&
Math.floor(gameManager._game_duration / GAMEPARAMETERS.loop_interval_time) > this._loop_count) {
context._canUpdate = false; context._canUpdate = false;
this._loop_count += 1;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return context.onUpdate(context._API._gameManager._game_duration); return context.onUpdate(context._API._gameManager.getCurrentTime());
}) })
.push(function () { .push(function () {
context._canUpdate = true; context._canUpdate = true;
...@@ -183,7 +187,7 @@ var DroneManager = /** @class */ (function () { ...@@ -183,7 +187,7 @@ var DroneManager = /** @class */ (function () {
context._internal_crash(error); context._internal_crash(error);
}) })
.push(function () { .push(function () {
context._API.internal_post_update(context); context._API.internal_info_update(context);
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
console.warn('Drone crashed on update due to error:', error); console.warn('Drone crashed on update due to error:', error);
...@@ -282,7 +286,7 @@ var DroneManager = /** @class */ (function () { ...@@ -282,7 +286,7 @@ var DroneManager = /** @class */ (function () {
this._controlMesh.position.z, this._controlMesh.position.z,
this._controlMesh.position.y this._controlMesh.position.y
); );
position.timestamp = this._API._gameManager._game_duration; position.timestamp = this._API._gameManager.getCurrentTime();
return position; return position;
} }
return null; return null;
...@@ -705,7 +709,8 @@ var GameManager = /** @class */ (function () { ...@@ -705,7 +709,8 @@ var GameManager = /** @class */ (function () {
this._game_duration += delta_time; this._game_duration += delta_time;
var color, drone_position, game_manager = this, geo_coordinates, var color, drone_position, game_manager = this, geo_coordinates,
log_count, map_info, map_manager, material, position_obj, log_count, map_info, map_manager, material, position_obj,
seconds = Math.floor(this._game_duration / 1000), trace_objects; current_time = this.getCurrentTime(),
seconds = Math.floor(current_time / 1000), trace_objects;
if (GAMEPARAMETERS.log_drone_flight || GAMEPARAMETERS.draw_flight_path) { if (GAMEPARAMETERS.log_drone_flight || GAMEPARAMETERS.draw_flight_path) {
this._droneList.forEach(function (drone, index) { this._droneList.forEach(function (drone, index) {
...@@ -724,7 +729,7 @@ var GameManager = /** @class */ (function () { ...@@ -724,7 +729,7 @@ var GameManager = /** @class */ (function () {
drone_position.z drone_position.z
); );
game_manager._flight_log[index].push([ game_manager._flight_log[index].push([
game_manager._game_duration, geo_coordinates.latitude, current_time, geo_coordinates.latitude,
geo_coordinates.longitude, geo_coordinates.longitude,
map_info.start_AMSL + drone_position.z, map_info.start_AMSL + drone_position.z,
drone_position.z, drone.getYaw(), drone.getSpeed(), drone_position.z, drone.getYaw(), drone.getSpeed(),
...@@ -909,8 +914,9 @@ var GameManager = /** @class */ (function () { ...@@ -909,8 +914,9 @@ var GameManager = /** @class */ (function () {
_this.ongoing_update_promise = null; _this.ongoing_update_promise = null;
_this.finish_deferred = RSVP.defer(); _this.finish_deferred = RSVP.defer();
console.log("Simulation started."); console.log("Simulation started.");
this._game_duration = Date.now(); this._start_time = Date.now();
this._totalTime = GAMEPARAMETERS.gameTime * 1000 + this._game_duration; this._game_duration = 0;
this._totalTime = GAMEPARAMETERS.gameTime * 1000;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -998,9 +1004,8 @@ var GameManager = /** @class */ (function () { ...@@ -998,9 +1004,8 @@ var GameManager = /** @class */ (function () {
"let droneMe = function(NativeDate, me, Math, window, DroneManager," + "let droneMe = function(NativeDate, me, Math, window, DroneManager," +
" GameManager, DroneLogAPI, FixedWingDroneAPI, BABYLON, " + " GameManager, DroneLogAPI, FixedWingDroneAPI, BABYLON, " +
"GAMEPARAMETERS) {" + "GAMEPARAMETERS) {" +
"var start_time = (new Date(2070, 0, 0, 0, 0, 0, 0)).getTime();" +
"Date.now = function () {" + "Date.now = function () {" +
"return start_time + drone._tick * 1000/60;}; " + "return me._API._gameManager.getCurrentTime();}; " +
"function Date() {if (!(this instanceof Date)) " + "function Date() {if (!(this instanceof Date)) " +
"{throw new Error('Missing new operator');} " + "{throw new Error('Missing new operator');} " +
"if (arguments.length === 0) {return new NativeDate(Date.now());} " + "if (arguments.length === 0) {return new NativeDate(Date.now());} " +
...@@ -1056,6 +1061,10 @@ var GameManager = /** @class */ (function () { ...@@ -1056,6 +1061,10 @@ var GameManager = /** @class */ (function () {
} }
}; };
GameManager.prototype.getCurrentTime = function () {
return this._start_time + this._game_duration;
};
return GameManager; return GameManager;
}()); }());
......
...@@ -240,7 +240,7 @@ ...@@ -240,7 +240,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1015.21426.50931.23620</string> </value> <value> <string>1015.24202.8036.31232</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1710926073.14</float> <float>1711029773.8</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -128,6 +128,7 @@ ...@@ -128,6 +128,7 @@
DRAW = true, DRAW = true,
LOG = true, LOG = true,
LOG_TIME = 1662.7915426540285, LOG_TIME = 1662.7915426540285,
LOOP_INTERVAL = 100,
DRONE_LIST = [], DRONE_LIST = [],
WIDTH = 680, WIDTH = 680,
HEIGHT = 340, HEIGHT = 340,
...@@ -192,6 +193,17 @@ ...@@ -192,6 +193,17 @@
"hidden": 0, "hidden": 0,
"type": "IntegerField" "type": "IntegerField"
}, },
"my_loop_interval": {
"description": "Minimum interval (in milliseconds) between 2 executions of flight loop",
"title": "Loop interval",
"default": LOOP_INTERVAL,
"css_class": "",
"required": 1,
"editable": 1,
"key": "loop_interval",
"hidden": 0,
"type": "IntegerField"
},
"my_drone_min_speed": { "my_drone_min_speed": {
"description": "", "description": "",
"title": "Drone min speed", "title": "Drone min speed",
...@@ -445,8 +457,8 @@ ...@@ -445,8 +457,8 @@
form_definition: { form_definition: {
group_list: [[ group_list: [[
"left", "left",
[["my_simulation_speed"], ["my_simulation_time"], ["my_number_of_drones"], [["my_simulation_speed"], ["my_simulation_time"], ["my_loop_interval"],
["my_minimum_latitud"], ["my_maximum_latitud"], ["my_number_of_drones"], ["my_minimum_latitud"], ["my_maximum_latitud"],
["my_minimum_longitud"], ["my_maximum_longitud"], ["my_minimum_longitud"], ["my_maximum_longitud"],
["my_init_pos_lat"], ["my_init_pos_lon"], ["my_init_pos_alt"], ["my_init_pos_lat"], ["my_init_pos_lon"], ["my_init_pos_alt"],
["my_map_height"]] ["my_map_height"]]
...@@ -520,6 +532,7 @@ ...@@ -520,6 +532,7 @@
"temp_flight_path": true, "temp_flight_path": true,
"log_drone_flight": LOG, "log_drone_flight": LOG,
"log_interval_time": LOG_TIME, "log_interval_time": LOG_TIME,
"loop_interval_time": parseInt(options.loop_interval, 10),
"droneList": DRONE_LIST "droneList": DRONE_LIST
}; };
return gadget.declareGadget("babylonjs.gadget.html", return gadget.declareGadget("babylonjs.gadget.html",
......
...@@ -246,7 +246,7 @@ ...@@ -246,7 +246,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1015.15836.20157.60791</string> </value> <value> <string>1015.22612.41991.56780</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -266,7 +266,7 @@ ...@@ -266,7 +266,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1710527785.95</float> <float>1710934372.06</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </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