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;
|
2018-08-15 17:58:07 +00:00
|
|
|
// When connection was intentionally closed
|
|
|
|
this.terminate = false;
|
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-08-15 17:58:07 +00:00
|
|
|
// Sensor data
|
|
|
|
this.sensors = {
|
|
|
|
'opponent': 99,
|
|
|
|
'line_left': 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
|
2018-08-15 17:58:07 +00:00
|
|
|
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-05-17 15:00:19 +00:00
|
|
|
}
|
2018-07-08 18:51:45 +00:00
|
|
|
// Reset watchdog counter
|
|
|
|
self.watchdogCounter = 0;
|
2018-05-17 15:00:19 +00:00
|
|
|
}, 3000);
|
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) {
|
2018-08-15 17:58:07 +00:00
|
|
|
// Clear the timers
|
2017-10-02 19:41:10 +00:00
|
|
|
clearInterval(self.watchdogTimer);
|
2018-08-15 17:58:07 +00:00
|
|
|
clearInterval(self.connectionTimer);
|
2018-07-08 18:51:45 +00:00
|
|
|
// Try to recnnect to the sumorobot
|
2018-08-15 17:58:07 +00:00
|
|
|
// 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-07-08 18:51:45 +00:00
|
|
|
// Get SumoRobot battery voltage
|
2018-08-15 17:58:07 +00:00
|
|
|
self.sensors = JSON.parse(data);
|
2018-07-08 18:51:45 +00:00
|
|
|
// When sensor data received
|
2018-08-15 17:58:07 +00:00
|
|
|
if (self.sensors['battery_voltage']) {
|
2018-09-12 12:30:13 +00:00
|
|
|
console.log(self.sensors);
|
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;
|
|
|
|
} 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
|
2018-08-15 17:58:07 +00:00
|
|
|
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() {
|
2018-08-15 17:58:07 +00:00
|
|
|
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
|
|
|
};
|