blockly server 产生python代码

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")
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值