Commit 86611d1a 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 () {
/*
** 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._updatePosition(context, delta_time);
......@@ -112,7 +112,7 @@ var FixedWingDroneAPI = /** @class */ (function () {
/*
** 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;
/*if (_this._start_altitude > 0) { //TODO move start_altitude here
_this.reachAltitude(drone);
......
......@@ -240,7 +240,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1015.19909.43325.57463</string> </value>
<value> <string>1015.21415.54357.41250</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -260,7 +260,7 @@
</tuple>
<state>
<tuple>
<float>1710776370.92</float>
<float>1710947081.61</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -27,6 +27,7 @@ var DroneManager = /** @class */ (function () {
this._maxClimbRate = 0;
this._maxCommandFrequency = 0;
this._last_command_timestamp = 0;
this._onupdate_count = 0;
this._speed = 0;
this._acceleration = 0;
this._direction = new BABYLON.Vector3(0, 0, 1); // North
......@@ -124,7 +125,7 @@ var DroneManager = /** @class */ (function () {
this._canCommunicate = true;
this._targetCoordinates = initial_position;
try {
return this.onStart(this._API._gameManager._game_duration);
return this.onStart(this._API._gameManager._start_time);
} catch (error) {
console.warn('Drone crashed on start due to error:', error);
this._internal_crash(error);
......@@ -132,17 +133,18 @@ var DroneManager = /** @class */ (function () {
};
DroneManager.prototype._callSetTargetCommand =
function (latitude, longitude, altitude, speed, radius) {
var current_time = this._API._gameManager.getCurrentTime();
if (!this.isReadyToFly()) {
return;
}
if (this._API._gameManager._game_duration - this._last_command_timestamp
if (current_time - this._last_command_timestamp
< 1000 / this._API.getMaxCommandFrequency()) {
this._internal_crash(new Error('Minimum interval between commands is ' +
1000 / this._API.getMaxCommandFrequency() + ' milliseconds'));
}
this._internal_setTargetCoordinates(latitude, longitude, altitude, speed,
radius);
this._last_command_timestamp = this._API._gameManager._game_duration;
this._last_command_timestamp = current_time;
};
/**
* Set a target point to move
......@@ -167,14 +169,16 @@ var DroneManager = /** @class */ (function () {
);
};
DroneManager.prototype.internal_update = function (delta_time) {
var context = this;
var context = this, gameManager = this._API._gameManager;
if (this._controlMesh) {
context._API.internal_update(context, delta_time);
if (context._canUpdate) {
context._API.internal_position_update(context, delta_time);
if (context._canUpdate &&
Math.floor(gameManager._game_duration / GAMEPARAMETERS.onupdate_interval_time) > this._onupdate_count) {
context._canUpdate = false;
this._onupdate_count += 1;
return new RSVP.Queue()
.push(function () {
return context.onUpdate(context._API._gameManager._game_duration);
return context.onUpdate(context._API._gameManager.getCurrentTime());
})
.push(function () {
context._canUpdate = true;
......@@ -183,7 +187,7 @@ var DroneManager = /** @class */ (function () {
context._internal_crash(error);
})
.push(function () {
context._API.internal_post_update(context);
context._API.internal_info_update(context);
})
.push(undefined, function (error) {
console.warn('Drone crashed on update due to error:', error);
......@@ -282,7 +286,7 @@ var DroneManager = /** @class */ (function () {
this._controlMesh.position.z,
this._controlMesh.position.y
);
position.timestamp = this._API._gameManager._game_duration;
position.timestamp = this._API._gameManager.getCurrentTime();
return position;
}
return null;
......@@ -705,7 +709,8 @@ var GameManager = /** @class */ (function () {
this._game_duration += delta_time;
var color, drone_position, game_manager = this, geo_coordinates,
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) {
this._droneList.forEach(function (drone, index) {
......@@ -724,7 +729,7 @@ var GameManager = /** @class */ (function () {
drone_position.z
);
game_manager._flight_log[index].push([
game_manager._game_duration, geo_coordinates.latitude,
current_time, geo_coordinates.latitude,
geo_coordinates.longitude,
map_info.start_AMSL + drone_position.z,
drone_position.z, drone.getYaw(), drone.getSpeed(),
......@@ -909,8 +914,9 @@ var GameManager = /** @class */ (function () {
_this.ongoing_update_promise = null;
_this.finish_deferred = RSVP.defer();
console.log("Simulation started.");
this._game_duration = Date.now();
this._totalTime = GAMEPARAMETERS.gameTime * 1000 + this._game_duration;
this._start_time = Date.now();
this._game_duration = 0;
this._totalTime = GAMEPARAMETERS.gameTime * 1000;
return new RSVP.Queue()
.push(function () {
......@@ -998,9 +1004,8 @@ var GameManager = /** @class */ (function () {
"let droneMe = function(NativeDate, me, Math, window, DroneManager," +
" GameManager, DroneLogAPI, FixedWingDroneAPI, BABYLON, " +
"GAMEPARAMETERS) {" +
"var start_time = (new Date(2070, 0, 0, 0, 0, 0, 0)).getTime();" +
"Date.now = function () {" +
"return start_time + drone._tick * 1000/60;}; " +
"return me._API._gameManager.getCurrentTime();}; " +
"function Date() {if (!(this instanceof Date)) " +
"{throw new Error('Missing new operator');} " +
"if (arguments.length === 0) {return new NativeDate(Date.now());} " +
......@@ -1056,6 +1061,10 @@ var GameManager = /** @class */ (function () {
}
};
GameManager.prototype.getCurrentTime = function () {
return this._start_time + this._game_duration;
};
return GameManager;
}());
......
......@@ -240,7 +240,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1015.21426.50931.23620</string> </value>
<value> <string>1015.24235.27009.16110</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -260,7 +260,7 @@
</tuple>
<state>
<tuple>
<float>1710926073.14</float>
<float>1711031751.77</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -128,6 +128,7 @@
DRAW = true,
LOG = true,
LOG_TIME = 1662.7915426540285,
ONUPDATE_INTERVAL = 100,
DRONE_LIST = [],
WIDTH = 680,
HEIGHT = 340,
......@@ -192,6 +193,17 @@
"hidden": 0,
"type": "IntegerField"
},
"my_onupdate_interval": {
"description": "Minimum interval (in milliseconds) between 2 executions of onUpdate function as well as periodicity to send telemetry to the swarm",
"title": "OnUpdate interval",
"default": ONUPDATE_INTERVAL,
"css_class": "",
"required": 1,
"editable": 1,
"key": "onupdate_interval",
"hidden": 0,
"type": "IntegerField"
},
"my_drone_min_speed": {
"description": "",
"title": "Drone min speed",
......@@ -445,8 +457,8 @@
form_definition: {
group_list: [[
"left",
[["my_simulation_speed"], ["my_simulation_time"], ["my_number_of_drones"],
["my_minimum_latitud"], ["my_maximum_latitud"],
[["my_simulation_speed"], ["my_simulation_time"], ["my_onupdate_interval"],
["my_number_of_drones"], ["my_minimum_latitud"], ["my_maximum_latitud"],
["my_minimum_longitud"], ["my_maximum_longitud"],
["my_init_pos_lat"], ["my_init_pos_lon"], ["my_init_pos_alt"],
["my_map_height"]]
......@@ -520,6 +532,7 @@
"temp_flight_path": true,
"log_drone_flight": LOG,
"log_interval_time": LOG_TIME,
"onupdate_interval_time": parseInt(options.onupdate_interval, 10),
"droneList": DRONE_LIST
};
return gadget.declareGadget("babylonjs.gadget.html",
......
......@@ -246,7 +246,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1015.15836.20157.60791</string> </value>
<value> <string>1015.24231.27679.34901</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -266,7 +266,7 @@
</tuple>
<state>
<tuple>
<float>1710527785.95</float>
<float>1711031688.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