blockly_code_generator_server.js
#!/usr/bin/env node
var fs = require('fs');
var net = require('net');
var Blockly = require('./gauss_python_generators').Blockly;
Blockly.Python.STATEMENT_PREFIX = 'n.highlight_block(%1)\n';
Blockly.Python.addReservedWords('highlightBlock');
const generateCode = (dirPath) => {
var filenameRead = dirPath + '/blockly_xml';
var filenameWrite = dirPath + '/blockly_python';
try {
var xmlCode = fs.readFileSync(filenameRead, 'utf8');
}
catch (e) {
return {
status: 400, message: 'Could not read file : ' + filenameRead };
}
try {
var xml = Blockly.Xml.textToDom(xmlCode);
}
catch (e) {
return {
status: 400, message: 'Could not parse XML from file : ' + filenameRead };
}
var workspace = new Blockly.Workspace();
try {
Blockly.Xml.domToWorkspace(xml, workspace);
}
catch (e) {
return {
status: 400, message: 'Failed to parse given Xml' };
}
var code = '#!/usr/bin/env python\n\nfrom gauss_python_api.gauss_api import *\n'
+ 'import rospy\nrospy.init_node(\'gauss_generated_code_execution\')\nn = Gauss()\n\n';
try {
code += Blockly.Python.workspaceToCode(workspace);
}
catch (e) {
return {
status: 400, message: 'Could not generate code from given Xml' };
}
try {
fs.writeFileSync(filenameWrite, code, 'utf-8');
}
catch (e) {
return {
status: 400, message: 'Could not write generated code on file : ' + filenameWrite };
}
return {
status: 200, message: 'Successfully generated code' };
}
var HOST = '127.0.0.1';
var PORT = '1337';
var server = net.createServer(function (socket) {
console.log('CONNECTED: ' + socket.remoteAddress + ':' + socket.remotePort);
socket.on('data', function (dirPath) {
var response = generateCode(dirPath.toString('utf8'));
socket.write(JSON.stringify(response));
});
socket.on('close', function (data) {
console.log('Socket connection closed... ');
});
socket.on('error', function (error) {
console.log(error);
});
}).listen(PORT, HOST);
gauss_python_generators.js
// adds Custom Gauss blocks + Python generators
var Blockly = require('node-blockly/python');
/*
* Blocks definition
*/
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 运动部分 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Blockly.Blocks['gauss_move_joints'] = {
init: function () {
this.appendDummyInput()
.appendField("Move Joints");
this.appendDummyInput()
.appendField("joint1")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "JOINTS_1")
.appendField("joint2")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "JOINTS_2")
.appendField("joint3")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "JOINTS_3")
.appendField("joint4")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "JOINTS_4")
.appendField("joint5")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "JOINTS_5")
.appendField("joint6")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "JOINTS_6");
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour("#009FAB");
this.setTooltip("Give all 6 joints to move the robot");
this.setHelpUrl("");
}
};
Blockly.Blocks['gauss_move_pose'] = {
init: function () {
this.appendDummyInput()
.appendField("Move Pose");
this.appendDummyInput()
.appendField("x")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "POSE_X")
.appendField("y")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "POSE_Y")
.appendField("z")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "POSE_Z")
.appendField("roll")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "POSE_ROLL")
.appendField("pitch")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "POSE_PITCH")
.appendField("yaw")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "POSE_YAW");
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour("#009FAB");
this.setTooltip("");
this.setHelpUrl("");
}
};
Blockly.Blocks['gauss_shift_pose'] = {
init: function () {
this.appendDummyInput()
.appendField("Shift");
this.appendDummyInput()
.appendField(new Blockly.FieldDropdown([["pos. x", "0"], ["pos. y", "1"], ["pos. z", "2"], ["rot. x", "3"], ["rot. y", "4"], ["rot. z", "5"]]), "SHIFT_POSE_AXIS")
.appendField("by")
.appendField(new Blockly.FieldNumber(0, -Infinity, Infinity, 0.0001), "SHIFT_POSE_VALUE");
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour("#009FAB");
this.setTooltip("");
this.setHelpUrl("");
}
};
Blockly.Blocks['gauss_set_arm_max_speed'] = {
init: function () {
this.appendValueInput("SET_ARM_MAX_SPEED")