EC机器人提供了丰富的sdk接口,可以在https://www.elibot.cn/#/download 搜索 EC_SDK_SOCKET手册,
也可以参考https://bbs.elibot.cn/forum/detail/topic/60.html
EC机器人的sdk,实质就是用户发送指定字符串(json格式)到机器人,并获取对应返回数据。
使用sdk时,机器人需要处于remote模式。sdk提供的接口比lua接口丰富,包括运动,力控等指令。
对于有些应用场合,没有上位机,不方便使用sdk开发,则可以直接在lua中使用sdk。
具体参考如下代码。
local json = require("json") local ret local recv IPAdd = "192.168.1.200" local cmd = "" local para1 = {} local ret1 local recv1 local id1 function NumToStr(data, num) local s = "%." .. num .. "f" return string.format(s, data) end function WaitUntil(var, value) --local vtmp = while get_global_variable(var) ~= value do sleep(0.1) end end function Connect1(IPAdd) ret = connect_tcp_server(IPAdd, 8055) end function SendCMD(IPAdd, cmd, params) local out_tmp if params == nil then out_tmp = json.encode({jsonrpc = "2.0", method = cmd, id = 1}) .. "\n" else out_tmp = json.encode({jsonrpc = "2.0", method = cmd, params = params, id = 1}) .. "\n" end ret = client_send_data(IPAdd, out_tmp, 0, 8055) ret, recv = client_recv_data(IPAdd, 1, 0, 8055) if ret < 0 then elite_print("接受8055数据失败") return false, nil, nil else local recv_json = json.decode(recv) return true, recv_json.result, recv_json.id end end function String2Table(str_t) local tmp = string.sub(str_t, 2, -2) local tmp1 = string.split(tmp, ",") local out = {} for i = 1, #tmp1, 1 do out[i] = tonumber(tmp1[i]) --out[i] = tonumber(tmp1[i]) end return out end sleep(0.3) Connect1(IPAdd) while true do local ec_state = get_robot_register(66) --register66: m528-m543 if ec_state == 1 then cmd = "setVirtualOutput" --控制命令 para1 = {addr = 552, status = 1} --参数,如果没有,下方SendCMD指令可以不加入para1 ret1, recv1, id1 = SendCMD(IPAdd, cmd, para1) --set_global_variable("B002", 0) end if ec_state == 2 then cmd = "setVirtualOutput" para1 = {addr = 552, status = 0} ret1, recv1, id1 = SendCMD(IPAdd, cmd, para1) --set_global_variable("B002", 0) end if ec_state == 10 then cmd = "get_joint_pos" ret1, recv1, id1 = SendCMD(IPAdd, cmd) local joints = String2Table(recv1) elite_print("机器人当前角度:", NumToStr(joints[1],2),NumToStr(joints[2],2),NumToStr(joints[3],2),NumToStr(joints[4],2),NumToStr(joints[5],2),NumToStr(joints[6],2)) --set_global_variable("B002", 0) end if ec_state == 20 then cmd = "get_tcp_pose" ret1, recv1, id1 = SendCMD(IPAdd, cmd) local pose1 = String2Table(recv1) elite_print("机器人当前位姿:", NumToStr(pose1[1],2),NumToStr(pose1[2],2),NumToStr(pose1[3],2),NumToStr(pose1[4],2),NumToStr(pose1[5],2),NumToStr(pose1[6],2)) --set_global_variable("B002", 0) end sleep(0.2) end