forked from marva/sumorobot-web
updates
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// Blockly workspace
|
||||
var workspace;
|
||||
|
||||
|
||||
window.addEventListener('load', function() {
|
||||
// To remember the control_if blockId
|
||||
var controlBlockId = '';
|
||||
@@ -10,29 +11,58 @@ window.addEventListener('load', function() {
|
||||
|
||||
// Remote previous and next statement from control_if block
|
||||
Blockly.defineBlocksWithJsonArray([
|
||||
{
|
||||
"type": "controls_if",
|
||||
"message0": "%{BKY_CONTROLS_IF_MSG_IF} %1",
|
||||
"args0": [
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "IF0",
|
||||
"check": "Boolean"
|
||||
}
|
||||
],
|
||||
"message1": "%{BKY_CONTROLS_IF_MSG_THEN} %1",
|
||||
"args1": [
|
||||
{
|
||||
"type": "input_statement",
|
||||
"name": "DO0"
|
||||
}
|
||||
],
|
||||
"colour": "%{BKY_LOGIC_HUE}",
|
||||
"helpUrl": "%{BKY_CONTROLS_IF_HELPURL}",
|
||||
"mutator": "controls_if_mutator",
|
||||
"extensions": ["controls_if_tooltip"]
|
||||
}
|
||||
]);
|
||||
{
|
||||
"type": "controls_if",
|
||||
"message0": "%{BKY_CONTROLS_IF_MSG_IF} %1",
|
||||
"args0": [
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "IF0",
|
||||
"check": "Boolean"
|
||||
}
|
||||
],
|
||||
"message1": "%{BKY_CONTROLS_IF_MSG_THEN} %1",
|
||||
"args1": [
|
||||
{
|
||||
"type": "input_statement",
|
||||
"name": "DO0"
|
||||
}
|
||||
],
|
||||
"colour": "%{BKY_LOGIC_HUE}",
|
||||
"helpUrl": "%{BKY_CONTROLS_IF_HELPURL}",
|
||||
"mutator": "controls_if_mutator",
|
||||
"extensions": ["controls_if_tooltip"]
|
||||
},
|
||||
{
|
||||
"type": "logic_operation",
|
||||
"message0": "%1 %2 %3",
|
||||
"args0": [
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "A",
|
||||
"check": "Boolean"
|
||||
},
|
||||
{
|
||||
"type": "field_dropdown",
|
||||
"name": "OP",
|
||||
"options": [
|
||||
["%{BKY_LOGIC_OPERATION_AND}", "AND"],
|
||||
["%{BKY_LOGIC_OPERATION_OR}", "OR"]
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "B",
|
||||
"check": "Boolean"
|
||||
}
|
||||
],
|
||||
"inputsInline": true,
|
||||
"output": "Boolean",
|
||||
"colour": "%{BKY_LOGIC_HUE}",
|
||||
"helpUrl": "%{BKY_LOGIC_OPERATION_HELPURL}",
|
||||
"extensions": ["logic_op_tooltip"]
|
||||
}
|
||||
]);
|
||||
|
||||
// Make control_if mutator icon bigger
|
||||
Blockly.Icon.prototype.renderIcon = function(cursorX) {
|
||||
@@ -82,9 +112,9 @@ window.addEventListener('load', function() {
|
||||
init: function() {
|
||||
this.setColour('#e98017');
|
||||
this.appendDummyInput()
|
||||
.appendField('sleep')
|
||||
.appendField(Blockly.Msg.SUMOROBOT_SLEEP)
|
||||
.appendField(new Blockly.FieldTextInput('1000',
|
||||
Blockly.FieldNumber.numberValidator), 'SLEEP');
|
||||
Blockly.FieldNumber.numberValidator), "SLEEP");
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
@@ -93,16 +123,16 @@ window.addEventListener('load', function() {
|
||||
Blockly.Blocks['sumorobot_move'] = {
|
||||
init: function() {
|
||||
var OPERATORS = [
|
||||
['move stop', 'STOP'],
|
||||
['move left', 'LEFT'],
|
||||
['move right', 'RIGHT'],
|
||||
['move search', 'SEARCH'],
|
||||
['move forward', 'FORWARD'],
|
||||
['move backward', 'BACKWARD']
|
||||
[Blockly.Msg.SUMOROBOT_STOP, 'STOP'],
|
||||
[Blockly.Msg.SUMOROBOT_LEFT, 'LEFT'],
|
||||
[Blockly.Msg.SUMOROBOT_RIGHT, 'RIGHT'],
|
||||
[Blockly.Msg.SUMOROBOT_SEARCH, 'SEARCH'],
|
||||
[Blockly.Msg.SUMOROBOT_FORWARD, 'FORWARD'],
|
||||
[Blockly.Msg.SUMOROBOT_BACKWARD, 'BACKWARD']
|
||||
];
|
||||
this.setColour('#d6382d');
|
||||
var dropdown = new Blockly.FieldDropdown(OPERATORS);
|
||||
this.appendDummyInput().appendField(dropdown, 'MOVE');
|
||||
this.appendDummyInput().appendField(dropdown, 'Move');
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
@@ -111,7 +141,7 @@ window.addEventListener('load', function() {
|
||||
Blockly.Blocks['sumorobot_opponent'] = {
|
||||
init: function() {
|
||||
this.setColour('#0099E6');
|
||||
this.appendDummyInput().appendField('opponent');
|
||||
this.appendDummyInput().appendField(Blockly.Msg.SUMOROBOT_OPPONENT);
|
||||
this.setOutput(true, 'Boolean');
|
||||
}
|
||||
};
|
||||
@@ -119,9 +149,9 @@ window.addEventListener('load', function() {
|
||||
Blockly.Blocks['sumorobot_line'] = {
|
||||
init: function() {
|
||||
var OPERATORS = [
|
||||
['line left', 'LEFT'],
|
||||
['line right', 'RIGHT'],
|
||||
['line middle', 'MIDDLE']
|
||||
[Blockly.Msg.SUMOROBOT_LINE_LEFT, 'LEFT'],
|
||||
[Blockly.Msg.SUMOROBOT_LINE_MIDDLE, 'MIDDLE'],
|
||||
[Blockly.Msg.SUMOROBOT_LINE_RIGHT, 'RIGHT']
|
||||
];
|
||||
this.setColour('#E6BF00');
|
||||
var dropdown = new Blockly.FieldDropdown(OPERATORS);
|
||||
@@ -131,12 +161,12 @@ window.addEventListener('load', function() {
|
||||
};
|
||||
|
||||
Blockly.Python['sumorobot_sleep'] = function(block) {
|
||||
var code = 'sumorobot.sleep(' + parseFloat(block.getFieldValue('SLEEP')) + ');;' + block.id + '\n';
|
||||
var code = 'sumorobot.sleep(' + parseFloat(block.getFieldValue("SLEEP")) + ');;' + block.id + '\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['sumorobot_move'] = function(block) {
|
||||
var code = 'sumorobot.move(' + block.getFieldValue('MOVE') + ');;' + block.id + '\n';
|
||||
var code = 'sumorobot.move(' + block.getFieldValue('Move') + ');;' + block.id + '\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
@@ -150,17 +180,17 @@ window.addEventListener('load', function() {
|
||||
return [code, Blockly.Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
// Inject Blockly
|
||||
// Inject Blocklyresiz
|
||||
var blocklyArea = document.getElementById('blocklyArea');
|
||||
var blocklyDiv = document.getElementById('blocklyDiv');
|
||||
workspace = Blockly.inject(blocklyDiv, {
|
||||
media: 'assets/blockly/media/',
|
||||
scrollbars: false,
|
||||
scrollbars: true,
|
||||
trashcan: true,
|
||||
sounds: true,
|
||||
zoom: {
|
||||
controls: true,
|
||||
startScale: 1.2
|
||||
startScale: 1.0
|
||||
},
|
||||
toolbox: document.getElementById('toolbox')
|
||||
});
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// The local/remote server URL
|
||||
//var ROBOT_SERVER = '192.168.2.1:80';
|
||||
var ROBOT_SERVER = '165.227.140.64:80';
|
||||
|
||||
//var ROBOT_SERVER = '165.227.140.64:80';
|
||||
var ROBOT_SERVER = 'sumo.koodur.com:80';
|
||||
// The sumorobot object
|
||||
var sumorobot;
|
||||
// Disable / enable coding mode
|
||||
@@ -112,6 +112,16 @@ window.addEventListener('load', function() {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function stopProcessCode() {
|
||||
// Stop highlighting blocks and lines
|
||||
lines = [];
|
||||
workspace.highlightBlock('');
|
||||
if (range_id) {
|
||||
readOnlyCodingEditor.session.removeMarker(range_id);
|
||||
}
|
||||
}
|
||||
|
||||
var range_id;
|
||||
var lines = [];
|
||||
var foundTrue = false;
|
||||
@@ -119,7 +129,8 @@ window.addEventListener('load', function() {
|
||||
function processCode(index) {
|
||||
|
||||
if(sumorobot.disconnected || !sumorobot.codeExecute) {
|
||||
$('.btn-stop').click();
|
||||
//$('.btn-stop').click();
|
||||
stopProcessCode();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -140,11 +151,11 @@ window.addEventListener('load', function() {
|
||||
} else if (/opponent/.test(code)) {
|
||||
foundTrue = (sumorobot.sensors['opponent'] < 40.0);
|
||||
} else if (/LEFT/.test(code)) {
|
||||
foundTrue = (sumorobot.sensors['line_left'] > 2000);
|
||||
foundTrue = (sumorobot.sensors['line_left'] < 2500);
|
||||
} else if (/RIGHT/.test(code)) {
|
||||
foundTrue = (sumorobot.sensors['line_right'] > 2000);
|
||||
foundTrue = (sumorobot.sensors['line_right'] < 2500);
|
||||
} else if (/MIDDLE/.test(code)) {
|
||||
foundTrue = (sumorobot.sensors['line_middle'] > 2000);
|
||||
foundTrue = (sumorobot.sensors['line_middle'] < 2500);
|
||||
} else {
|
||||
foundTrue = true;
|
||||
}
|
||||
@@ -170,6 +181,18 @@ window.addEventListener('load', function() {
|
||||
setTimeout(function() { processCode(index) }, timeout);
|
||||
}
|
||||
|
||||
|
||||
$('.btn-upload').click(function() {
|
||||
if(codingEnabled) {
|
||||
parsedCode = codingEditor.getValue();
|
||||
} else {
|
||||
parsedCode = Blockly.Python.workspaceToCode(workspace).replace(/;;.{20}/g, '');
|
||||
}
|
||||
|
||||
sumorobot.send('code', parsedCode.replace(/"/g, '\\"').replace(/\n/g, ';;'));
|
||||
lines = Blockly.Python.workspaceToCode(workspace).split('\n').filter(Boolean);
|
||||
});
|
||||
|
||||
// Start button listener
|
||||
$('.btn-start').click(function() {
|
||||
// When we are in Python coding mode
|
||||
@@ -182,22 +205,17 @@ window.addEventListener('load', function() {
|
||||
parsedCode = Blockly.Python.workspaceToCode(workspace).replace(/;;.{20}/g, '');
|
||||
}
|
||||
// Escape the qoutes, replace new lines and send the code
|
||||
sumorobot.send('code', parsedCode.replace(/"/g, '\\"').replace(/\n/g, ';;'));
|
||||
sumorobot.send('start', parsedCode.replace(/"/g, '\\"').replace(/\n/g, ';;'));
|
||||
// Split into lines of code and filter empty lines
|
||||
lines = Blockly.Python.workspaceToCode(workspace).split('\n').filter(Boolean);
|
||||
// Process the code starting from the first line
|
||||
processCode(0);
|
||||
//processCode(0);
|
||||
});
|
||||
|
||||
// Stop button listener
|
||||
$('.btn-stop').click(function() {
|
||||
sumorobot.send('stop');
|
||||
// Stop highlighting blocks and lines
|
||||
lines = [];
|
||||
workspace.highlightBlock('');
|
||||
if (range_id) {
|
||||
readOnlyCodingEditor.session.removeMarker(range_id);
|
||||
}
|
||||
stopProcessCode();
|
||||
});
|
||||
|
||||
// Enter (return) keypress listener on robot ID field
|
||||
@@ -227,7 +245,15 @@ window.addEventListener('load', function() {
|
||||
}
|
||||
// Connect to the selected robots WebSocket
|
||||
sumorobot = new Sumorobot(`ws://${ROBOT_SERVER}/p2p/browser/sumo-${robotId}/`, robotId);
|
||||
|
||||
$(sumorobot).on('start',function(){
|
||||
//console.log("START");
|
||||
lines = Blockly.Python.workspaceToCode(workspace).split('\n').filter(Boolean);
|
||||
processCode(0);
|
||||
});
|
||||
|
||||
// Hide the configuration panel
|
||||
$('#panel').hide();
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -16,6 +16,8 @@ var Sumorobot = function(wsUri, robotId) {
|
||||
this.moving = false;
|
||||
|
||||
this.codeExecute = false;
|
||||
|
||||
self.previousCodeExecuteValue = false;
|
||||
// Sensor data
|
||||
this.sensors = {
|
||||
'opponent': 99,
|
||||
@@ -66,21 +68,30 @@ Sumorobot.prototype.connect = function() {
|
||||
// When scope is received
|
||||
var data = evt.data.replace(/'/g, '"').toLowerCase();
|
||||
self.disconnected = false;
|
||||
var message = JSON.parse(data);
|
||||
|
||||
if(message.cmd == "sensors") {
|
||||
self.sensors = message.data;
|
||||
self.codeExecute = self.sensors.state;
|
||||
}else if(message.cmd == "code_upload") {
|
||||
if(message.status) {
|
||||
alert("Ülesslaadimine õnnestus");
|
||||
} else {
|
||||
alert("Ülesslaadimine ebaõnnestus");
|
||||
}
|
||||
}
|
||||
|
||||
// Get SumoRobot battery voltage
|
||||
self.sensors = JSON.parse(data);
|
||||
self.codeExecute = self.sensors.state;
|
||||
// When sensor data received
|
||||
if (self.sensors['battery_voltage']) {
|
||||
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");
|
||||
}
|
||||
if(self.codeExecute && !self.previousCodeExecuteValue){
|
||||
$(self).trigger("start");
|
||||
}
|
||||
|
||||
self.previousCodeExecuteValue = self.codeExecute;
|
||||
|
||||
// Count data received packets
|
||||
|
||||
$("#battery img").attr("src", "assets/img/checkmark-64.png");
|
||||
|
||||
self.watchdogCounter += 1;
|
||||
};
|
||||
// When there is an WebSocket error
|
||||
@@ -93,7 +104,7 @@ Sumorobot.prototype.connect = function() {
|
||||
Sumorobot.prototype.send = function(cmd, val) {
|
||||
if (cmd !== 'ping' && cmd !== 'stop') {
|
||||
this.moving = true;
|
||||
this.codeExecute = true;
|
||||
//this.codeExecute = true;
|
||||
} else {
|
||||
this.moving = false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user