sumorobot-web/assets/js/sumorobot.js

117 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-07-08 18:51:45 +00:00
// Sumorobot constructor
var Sumorobot = function(wsUri, robotId) {
// Assign the WebSocket URI
2017-09-25 09:37:41 +00:00
this.wsUri = wsUri;
2018-07-08 18:51:45 +00:00
// Assign the SumoRobot ID
this.robotId = robotId;
// To keep track of the WebSocket connection
2018-08-12 10:39:18 +00:00
this.watchdogCounter = 0;
// When connection was intentionally closed
this.terminate = false;
2018-11-04 20:42:04 +00:00
// when there is no connection with robot
this.disconnected = true;
2018-07-08 18:51:45 +00:00
// To store Blockly code
2018-08-12 10:39:18 +00:00
this.blocklyCode = '';
// If robot is moving
this.moving = false;
2018-11-04 20:42:04 +00:00
this.codeExecute = false;
// Sensor data
this.sensors = {
'opponent': 99,
'line_left': 0,
2018-11-02 10:50:15 +00:00
'line_middle': 0,
'line_right': 0,
'battery_voltage': 0
};
2018-07-08 18:51:45 +00:00
// Start connecting to the WebSocket
this.connect();
2017-09-25 09:37:41 +00:00
};
2018-07-08 18:51:45 +00:00
// Function to initiate the WebSocket connection
2017-09-25 09:37:41 +00:00
Sumorobot.prototype.connect = function() {
2018-07-08 18:51:45 +00:00
// To have access to this object inside events
2017-09-25 09:37:41 +00:00
var self = this;
this.websocket = new WebSocket(this.wsUri);
2018-07-08 18:51:45 +00:00
// Setup connection watchdog interval
self.connectionTimer = setInterval(function() {
if (self.watchdogCounter == 0 && !self.terminate) {
2018-08-18 16:48:27 +00:00
$("#battery img").attr("src", "assets/img/battery_disconnected.png");
2018-11-04 20:42:04 +00:00
self.disconnected = true;
2018-05-17 15:00:19 +00:00
}
2018-07-08 18:51:45 +00:00
// Reset watchdog counter
self.watchdogCounter = 0;
2018-11-04 20:42:04 +00:00
}, 1500);
2018-07-08 18:51:45 +00:00
// When the WebSocket gets connected
2017-09-25 09:37:41 +00:00
this.websocket.onopen = function(evt) {
2018-07-08 18:51:45 +00:00
// Setup a timer to ping the robot
2017-10-02 19:41:10 +00:00
self.watchdogTimer = setInterval(function() {
2018-07-08 18:51:45 +00:00
// Send a ping to the robot
2018-09-12 13:12:50 +00:00
self.send('ping');
2018-04-08 17:46:36 +00:00
}, 500);
2017-09-25 09:37:41 +00:00
};
2018-07-08 18:51:45 +00:00
// When the WebSocket closes
2017-09-25 09:37:41 +00:00
this.websocket.onclose = function(evt) {
// Clear the timers
2017-10-02 19:41:10 +00:00
clearInterval(self.watchdogTimer);
clearInterval(self.connectionTimer);
2018-07-08 18:51:45 +00:00
// Try to recnnect to the sumorobot
// Only if the connection wasn't closed intentionally
if (!self.terminate) {
self.connect();
}
2017-09-25 09:37:41 +00:00
};
2018-07-08 18:51:45 +00:00
// When there is a message from the WebSocket
2017-09-25 09:37:41 +00:00
this.websocket.onmessage = function(evt) {
2018-07-08 18:51:45 +00:00
// When scope is received
2017-10-31 16:59:15 +00:00
var data = evt.data.replace(/'/g, '"').toLowerCase();
2018-11-04 20:42:04 +00:00
self.disconnected = false;
2018-07-08 18:51:45 +00:00
// Get SumoRobot battery voltage
self.sensors = JSON.parse(data);
2018-11-04 20:42:04 +00:00
self.codeExecute = self.sensors.state;
2018-07-08 18:51:45 +00:00
// When sensor data received
if (self.sensors['battery_voltage']) {
2018-08-18 16:48:27 +00:00
if (self.sensors['battery_voltage'] > 4.0) {
$("#battery img").attr("src", "assets/img/battery_full.png");
} else if (self.sensors['battery_voltage'] > 3.1) {
$("#battery img").attr("src", "assets/img/battery_half.png");
} else {
$("#battery img").attr("src", "assets/img/battery_empty.png");
}
2018-07-08 18:51:45 +00:00
}
// Count data received packets
self.watchdogCounter += 1;
2017-09-25 09:37:41 +00:00
};
2018-07-08 18:51:45 +00:00
// When there is an WebSocket error
2017-09-25 09:37:41 +00:00
this.websocket.onerror = function(err) {
2018-08-12 10:39:18 +00:00
console.log('ERROR websocket error: ' + err);
2017-09-25 09:37:41 +00:00
};
};
2018-07-08 18:51:45 +00:00
// Function to send WebSocket data
2018-08-12 10:39:18 +00:00
Sumorobot.prototype.send = function(cmd, val) {
if (cmd !== 'ping' && cmd !== 'stop') {
this.moving = true;
2018-11-04 20:42:04 +00:00
this.codeExecute = true;
2018-08-12 10:39:18 +00:00
} else {
this.moving = false;
}
2018-07-08 18:51:45 +00:00
// Ready state constants: CONNECTING 0, OPEN 1, CLOSING 2, CLOSED 3
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
if (!this.terminate && this.websocket.readyState == 1) {
2018-08-12 10:39:18 +00:00
if (val) {
2018-09-12 12:30:13 +00:00
this.websocket.send(`{"cmd": "${cmd}", "val": "${val}"}`);
2018-07-08 18:51:45 +00:00
} else {
2018-09-12 12:30:13 +00:00
this.websocket.send(`{"cmd": "${cmd}"}`);
2018-07-08 18:51:45 +00:00
}
2017-09-25 09:37:41 +00:00
}
};
2018-07-08 18:51:45 +00:00
// Function to close the WebSocket connection
2018-01-16 21:13:23 +00:00
Sumorobot.prototype.close = function() {
this.terminate = true;
2018-08-12 10:39:18 +00:00
// Close the WebSocket connection
this.websocket.close();
2018-07-08 18:51:45 +00:00
};