Skip to content

Node

首先下载PROTO文件

安装依赖包

$ yarn add grpc @grpc/proto-loader

对于Node使用gRPC不需要提前生成相关包,可以在执行过程中动态调用PROTO文件

创建RPC Client

var grpc = require("grpc")
var proto_path = path.join(__static, "/grpc/proto/robot.proto")
var proto_loader = require("@grpc/proto-loader")
var package_definition = proto_loader.loadSync(
    proto_path, {
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true
    }
)
var proto_descriptor = grpc.loadPackageDefinition(package_definition)
var ip = store.state.connection.current_connection.ip
var port = store.state.connection.current_connection.port
var client = new proto_descriptor.robot.RobotService(ip + ":" + port, grpc.credentials.createInsecure())

测试RPC Serve状态

client.ConnectRPC({}, function(err, resp) {
 if (err) {
    console.log('error:' + err)
  } else {
    console.log('RPC连接成功')
  }
})

机械臂上电

client.MoveStart({}, function(err, resp) {
    if (!err) {
        console.log("上电")
    }
})

然后执行机械臂运动(MoveA)

var rob_pose = {xyz: [1, 1, 1],kps: [1, 1, 1]}
client.MoveA({
        rjoint: {
            dof: 6,
            angle: [x, x, 0, 0, 0, 0, 0, 0, 0, 0]
        },
        rspeed: {
            dof: 6,
            per_flag: 2,
            tcp_flag: 0,
            orl_flag: 0,
            per: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
            tcp: 50,
            orl: 50
        },
        rzone: {
            zone_flag: 1,
            zone_size: 1
        },
        rtool: {
            robhold: 1,
            tframe: rob_pose,
            payload: {
                m: 1,
                cm: [1, 2, 3],
                ii: [1, 2, 1, 2, 1, 2, 1, 2, 1],
                ii2: [1, 2, 1, 2, 1, 2, 1, 2, 1]
            }
        },
        wobj: {
            robhold: 1,
            uframe: rob_pose,
            oframe: rob_pose,
            ufprog: 1,
            ufmec: 1
        }
    },
    function(err, resp) {
    })

机械臂下电

client.MoveStop({}, function(err, resp) {
    if (!err) {
        console.log("下电")
    }
})