add moving function

This commit is contained in:
Silver Kuusik 2018-08-12 12:39:18 +02:00
parent d6346b44eb
commit 9c4399859f
1 changed files with 23 additions and 19 deletions

View File

@ -5,11 +5,13 @@ var Sumorobot = function(wsUri, robotId) {
// Assign the SumoRobot ID // Assign the SumoRobot ID
this.robotId = robotId; this.robotId = robotId;
// To keep track of the WebSocket connection // To keep track of the WebSocket connection
this.watchdogTimer = 0; this.watchdogCounter = 0;
// Timer to ping the SumoRobot // Timer to ping the SumoRobot
this.watchdogTimer = null; this.watchdogTimer = null;
// To store Blockly code // To store Blockly code
this.blocklyCode = ""; this.blocklyCode = '';
// If robot is moving
this.moving = false;
// Start connecting to the WebSocket // Start connecting to the WebSocket
this.connect(); this.connect();
}; };
@ -22,8 +24,8 @@ Sumorobot.prototype.connect = function() {
// Setup connection watchdog interval // Setup connection watchdog interval
setInterval(function() { setInterval(function() {
if (self.watchdogCounter == 0) { if (self.watchdogCounter == 0) {
$("#battery").removeClass("connected"); $('#battery').removeClass('connected');
$("#battery").html("Disconnected"); $('#battery').html('Disconnected');
} }
// Reset watchdog counter // Reset watchdog counter
self.watchdogCounter = 0; self.watchdogCounter = 0;
@ -35,7 +37,7 @@ Sumorobot.prototype.connect = function() {
// Setup a timer to ping the robot // Setup a timer to ping the robot
self.watchdogTimer = setInterval(function() { self.watchdogTimer = setInterval(function() {
// Send a ping to the robot // Send a ping to the robot
self.send(self.robotId, "ping"); self.send(self.robotId, 'ping');
}, 500); }, 500);
}; };
// When the WebSocket closes // When the WebSocket closes
@ -50,41 +52,43 @@ Sumorobot.prototype.connect = function() {
// When scope is received // When scope is received
var data = evt.data.replace(/'/g, '"').toLowerCase(); var data = evt.data.replace(/'/g, '"').toLowerCase();
// Get SumoRobot battery voltage // Get SumoRobot battery voltage
var battery = JSON.parse(data)["battery_voltage"]; var batVolt = JSON.parse(data)['battery_voltage'];
// When sensor data received // When sensor data received
if (battery) { if (batVolt) {
$("#battery").html(battery + "V"); $('#battery').html(batVolt + 'V');
$("#battery").addClass("connected"); $('#battery').addClass('connected');
} }
// Count data received packets // Count data received packets
self.watchdogCounter += 1; self.watchdogCounter += 1;
}; };
// When there is an WebSocket error // When there is an WebSocket error
this.websocket.onerror = function(err) { this.websocket.onerror = function(err) {
console.log("ERROR websocket error: " + err); console.log('ERROR websocket error: ' + err);
}; };
}; };
// Function to send WebSocket data // Function to send WebSocket data
Sumorobot.prototype.send = function(msg, val) { Sumorobot.prototype.send = function(cmd, val) {
if (cmd !== 'ping' && cmd !== 'stop') {
this.moving = true;
} else {
this.moving = false;
}
// Ready state constants: CONNECTING 0, OPEN 1, CLOSING 2, CLOSED 3 // Ready state constants: CONNECTING 0, OPEN 1, CLOSING 2, CLOSED 3
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
if (this.websocket.readyState == 1) { if (this.websocket.readyState == 1) {
if (val === 'undefined') { if (val) {
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${msg}"}`); this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${cmd}", "val": "${val}"}`);
} else { } else {
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${msg}", "val": "${val}"}`); this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${cmd}"}`);
} }
} }
}; };
// Function to close the WebSocket connection // Function to close the WebSocket connection
Sumorobot.prototype.close = function() { Sumorobot.prototype.close = function() {
// When a WebSocket connection exists // Close the WebSocket connection
if (this.websocket !== 'undefined') { this.websocket.close();
// Close the WebSocket connection
this.websocket.close();
}
}; };
// Function to get SumoRobot Blockly code // Function to get SumoRobot Blockly code