add moving function
This commit is contained in:
parent
d6346b44eb
commit
9c4399859f
@ -5,11 +5,13 @@ var Sumorobot = function(wsUri, robotId) {
|
||||
// Assign the SumoRobot ID
|
||||
this.robotId = robotId;
|
||||
// To keep track of the WebSocket connection
|
||||
this.watchdogTimer = 0;
|
||||
this.watchdogCounter = 0;
|
||||
// Timer to ping the SumoRobot
|
||||
this.watchdogTimer = null;
|
||||
// To store Blockly code
|
||||
this.blocklyCode = "";
|
||||
this.blocklyCode = '';
|
||||
// If robot is moving
|
||||
this.moving = false;
|
||||
// Start connecting to the WebSocket
|
||||
this.connect();
|
||||
};
|
||||
@ -22,8 +24,8 @@ Sumorobot.prototype.connect = function() {
|
||||
// Setup connection watchdog interval
|
||||
setInterval(function() {
|
||||
if (self.watchdogCounter == 0) {
|
||||
$("#battery").removeClass("connected");
|
||||
$("#battery").html("Disconnected");
|
||||
$('#battery').removeClass('connected');
|
||||
$('#battery').html('Disconnected');
|
||||
}
|
||||
// Reset watchdog counter
|
||||
self.watchdogCounter = 0;
|
||||
@ -35,7 +37,7 @@ Sumorobot.prototype.connect = function() {
|
||||
// Setup a timer to ping the robot
|
||||
self.watchdogTimer = setInterval(function() {
|
||||
// Send a ping to the robot
|
||||
self.send(self.robotId, "ping");
|
||||
self.send(self.robotId, 'ping');
|
||||
}, 500);
|
||||
};
|
||||
// When the WebSocket closes
|
||||
@ -50,41 +52,43 @@ Sumorobot.prototype.connect = function() {
|
||||
// When scope is received
|
||||
var data = evt.data.replace(/'/g, '"').toLowerCase();
|
||||
// Get SumoRobot battery voltage
|
||||
var battery = JSON.parse(data)["battery_voltage"];
|
||||
var batVolt = JSON.parse(data)['battery_voltage'];
|
||||
// When sensor data received
|
||||
if (battery) {
|
||||
$("#battery").html(battery + "V");
|
||||
$("#battery").addClass("connected");
|
||||
if (batVolt) {
|
||||
$('#battery').html(batVolt + 'V');
|
||||
$('#battery').addClass('connected');
|
||||
}
|
||||
// Count data received packets
|
||||
self.watchdogCounter += 1;
|
||||
};
|
||||
// When there is an WebSocket error
|
||||
this.websocket.onerror = function(err) {
|
||||
console.log("ERROR websocket error: " + err);
|
||||
console.log('ERROR websocket error: ' + err);
|
||||
};
|
||||
};
|
||||
|
||||
// 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
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
|
||||
if (this.websocket.readyState == 1) {
|
||||
if (val === 'undefined') {
|
||||
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${msg}"}`);
|
||||
if (val) {
|
||||
this.websocket.send(`{"to": "sumo-${this.robotId}@00000514", "cmd": "${cmd}", "val": "${val}"}`);
|
||||
} 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
|
||||
Sumorobot.prototype.close = function() {
|
||||
// When a WebSocket connection exists
|
||||
if (this.websocket !== 'undefined') {
|
||||
// Close the WebSocket connection
|
||||
this.websocket.close();
|
||||
}
|
||||
// Close the WebSocket connection
|
||||
this.websocket.close();
|
||||
};
|
||||
|
||||
// Function to get SumoRobot Blockly code
|
||||
|
Loading…
Reference in New Issue
Block a user