示例程序
场景任务
被调用的执行场景
current_tcp_pose = get_target_tcp_pose()
print('current_tcp_pose',current_tcp_pose)
offset_position = {0, 0, 0.1, 0, 0, 0}
calculate_location = pose_times(current_tcp_pose, offset_position)
print('calculate_location',calculate_location)
movej(calculate_location, 1, 0.5, 0, 0)
场景传参解析方式
C_POSE = {}
J_POSE = {}
local params =...
print("params: ", params)
print('打印参数个数',#params)
print('打印数组第一个参数类型',type(params[1]))
local json = require("json")
if params and next(params) then
for i=1,#params do
jpose = json.decode(params[i])
lua_jpose = {j1=jpose[1],j2=jpose[2],j3=jpose[3],j4=jpose[4],j5=jpose[5],j6=jpose[6]}
J_POSE[i]= lua_jpose
end
end
local str = "{x = 1}"
local func, err = load("return " .. str)
if not func then
error("Failed to load string: " .. err)
end
local tbl = func()
print(tbl.x)
local str = "{1,2}"
local table_result = load("return " .. str)()
for key, value in pairs(table_result) do
print(key, value)
end
str = "{'a':2}"
local startIndex = string.find(str, "{")
local endIndex = string.find(str, "}")
if startIndex then
local substring = string.sub(str, startIndex + 1,endIndex-1)
print(substring)
end
场景接口调用
print(scene(10216,{'{1, 2}', 3, 4}))
print(var_test)
local params = ...
print("params: ", params)
print('打印参数类型',type(params[1]))
local json = require("json")
if params and next(params) then
for i=1,#params do
param = params[i]
if i ==1 then
print('打印参数1类型',type(params[1]))
table_data = load("return " .. param)()
print('打印参数1类型',type(table_data))
print(table_data)
else
print(param)
end
end
end
var_test = {
name = "小吴",
age = 16
}
return 4, 5, 6
定时调用某个任务场景
J_POSES = {}
C_POSES = {}
C_POSES[10007] = {-0.003969, 0.425042, 0.126468, 2.738853, 0.046511, 1.507116}
C_POSES["奉客位置"] = {-0.003969, 0.425042, 0.126468, 2.738853, 0.046511, 1.507116}
J_POSES[10007] = {j1 = 4.624948, j2 = -1.094781, j3 = 2.504036, j4 = -1.476085, j5 = 1.882478, j6 = -0.067120}
J_POSES["奉客位置"] = {j1 = 4.624948, j2 = -1.094781, j3 = 2.504036, j4 = -1.476085, j5 = 1.882478, j6 = -0.067120}
C_POSES[10006] = {-0.003969, 0.425042, 0.126468, 2.738853, 0.046511, 1.507116}
C_POSES["2023-02-08 10:55:14 AM"] = {-0.003969, 0.425042, 0.126468, 2.738853, 0.046511, 1.507116}
J_POSES[10006] = {j1 = 4.624948, j2 = -1.094781, j3 = 2.504036, j4 = -1.476085, j5 = 1.882478, j6 = -0.067120}
J_POSES["2023-02-08 10:55:14 AM"] = {j1 = 4.624948, j2 = -1.094781, j3 = 2.504036, j4 = -1.476085, j5 = 1.882478, j6 = -0.067120}
local date = os.date("*t", os.time());
C_POSE = {}
J_POSE = {}
execution_scene_flag=false
execution_exscene_flag=false
exscene_executed_tags=0
while true do
wait(100)
local date = os.date("*t", os.time())
if date['hour']>=8 and date['hour']<=21 then
execution_scene_flag=true
exscene_executed_tags=0
elseif (string.find(os.date(), " 22:")) then
execution_scene_flag=false
if exscene_executed_tags == 0 then
execution_exscene_flag=true
else
execution_exscene_flag=false
end
end
if execution_scene_flag==true then
print('运行场景')
scene(10103)
end
if execution_exscene_flag==true then
print('运行场景')
scene(10103)
exscene_executed_tags=1
end
end
开机自启任务
start_task('10026', nil, nil, true, 1)
function on_robot_stop(is_estop)
print("estop 当前TASK id:", task_id)
cancel_task(task_id)
end
local com = serial.open("/dev/ttyS1")
com:set_timeout(200)
com:set_baud_rate(9600)
function Com_read_data()
local success, rst = pcall(function() return com:read(1) end)
if success then
if rst[1] == 0xAA then
local success, rst = pcall(function() return com:read(11) end)
if success then
table.insert(rst, 1,0xAA )
return success, rst
end
end
end
return false,nil
end
local function calc_crc(data)
local crc = 0xFFFF
for i = 1, #data do
crc = crc ~ data[i]
for _ = 1, 8 do
local j = crc & 1
crc = crc >> 1
if j == 1 then
crc = crc ~ 0xA001
end
end
end
return crc
end
function parse_request(request)
if #request < 12 then return nil end
if request[1] ~= 0xAA or request[2] ~= 0x55 then return nil end
if request[3] ~= 0x01 then return nil end
if request[5] ~= 0x37 and request[5] ~= 0x38 then return nil end
local crc_low = request[11]
local crc_high = request[12]
local crc_data = {}
for i=1,10 do
crc_data[i] = request[i]
end
local calc_crc_val = calc_crc(crc_data)
if crc_low ~= (calc_crc_val & 0xFF) or crc_high ~= ((calc_crc_val >> 8) & 0xFF) then
return nil
end
return request[5], request[6]
end
while true do
local success, request = Com_read_data()
if success then
if request and #request == 12 then
local cmd_id, basket_id = parse_request(request)
print("com接收 :",request)
if basket_id ~= nil then
response = {0x55, 0xAA}
print("com发送<:", response)
local response_success, response_rst = pcall(function() return com:write(response) end)
if response_success then
print("com发送成功")
else
print("com发送失败")
end
end
end
end
if get_di(0) == 1 then
while true do
wait(10)
if get_di(0) == 0 then
print("收到来自控制箱上的di信号")
break
end
end
if robot_state == 'ESTOP' or robot_state == 'IDLE' or robot_state == 'TEACHING' then
if robot_state == 'ESTOP' then
start_sys()
sleep(2000)
end
state = get_task_state(task_id)
if state ~= 'WAIT' and state ~= 'RUNNING' and state ~= 'PAUSE'
then
if robot_state == 'TEACHING' then
end_teach_mode()
end
task_id = start_task('10024', nil, nil, false, 1)
print(string.format('Robot start task, task_id: %s', task_id))
end
end
end
local button_type = "SHOULDER"
if lebai:get_di(button_type, 0)==1 then
while lebai:get_di(button_type, 0)==1 do
wait(20)
end
task_id = get_main_task_id()
cancel_task(task_id)
end
sleep(10)
end
任务接口调用
task_id = start_task(10216,{'{1, 2}', 3, 4}, "", true, 1)
print(var_test)
local params = ...
print("params: ", params)
print('打印参数类型',type(params[1]))
local json = require("json")
if params and next(params) then
for i=1,#params do
param = params[i]
if i ==1 then
print('打印参数类型',type(params[1]))
table_data = load("return " .. param)()
print('打印参数类型',type(table_data))
print(table_data)
else
print(param)
end
end
end
var_test = {
name = "小吴",
age = 16
}
return 4, 5, 6
串口通讯
串口通讯交互控制
com1 = serial.open("/dev/ttyS1")
com1:set_timeout(200)
com1:set_baud_rate(9600)
function table2Hex(s)
rst = ''
for i = 1, #s do
rst = rst .. string.format('0x%02X ', s[i])
end
return rst
end
function String2Hex(s)
local rst = ""
for i = 1, #s do
local byte = string.byte(s, i)
rst = rst .. string.format('0x%02X', byte)
if i < #s then
rst = rst .. ' '
end
end
return rst
end
function u2s_8(num)
if num > 127 then
num = -(256 - num)
end
return num
end
function s2u_8(num)
if num < 0 then
num = 256 + num
end
return num
end
function usign2sign(num)
if num > 32767 then
num = -(65536 - num)
end
return num
end
function sign2usign(num)
if num < 0 then
num = 65536 + num
end
return num
end
function crc16_modbus(data)
local crc = 0xFFFF
local poly = 0xA001
local byte
for i = 1, #data do
if type(data) == "string" then
byte = string.byte(data, i)
else
byte = data[i]
end
crc=((crc ~ byte) & 0xFFFF)
for _ = 1, 8 do
if (crc & 1) ~= 0 then
crc = ((crc >> 1) ~ poly)& 0xFFFF
else
crc = crc >> 1
end
end
end
local crc_low = crc & 0xFF
local crc_high = (crc >> 8) & 0xFF
return crc_low, crc_high
end
function calculateChecksum(data)
local sum = 0
for i = 1, #data do
sum = sum + data[i]
end
return sum % 256
end
function xor_checksum(data)
local checksum = 0
for i = 1, #data do
local byte
if type(data) == "string" then
byte = string.byte(data, i)
else
byte = data[i]
end
checksum = checksum ~ byte
end
return checksum & 0xFF
end
function recv_data()
success, result = pcall(function() return com1:read() end)
if success and #result>2 then
local subTable = {}
for i = 1, #result-2 do
subTable[i] = result[i]
end
local crc_low, crc_high = crc16_modbus(subTable)
if result[#result-1] == crc_low and result[#result] ==crc_high then
print(table2Hex(subTable))
local data = string.char(table.unpack(subTable))
return data
else
return nil
end
else
print("Error: ", result)
end
end
while true
do
data = recv_data()
if data == "123456"
then
movej({j1 = 0, j2 = 0, j3 = 0, j4 = 0, j5 = 0, j6 = 0}, 0.1, 0.1, 0, 0)
sync()
set_claw(100,100)
local str = "finish1"
local crc_low, crc_high = crc16_modbus(str)
com1:write({string.byte(str, 1, #str),crc_low,crc_high})
elseif data == "234567"
then
scene(10001)
set_claw(100,0)
local str = "finish2"
local crc_low, crc_high = crc16_modbus(str)
com1:write({string.byte(str, 1, #str),crc_low,crc_high})
else
print("unknown data:", data)
end
end
串口控制甄小非咖啡机
function Crc16(buf)
local init = 0xFFFF
local poly = 0xA001
local ret = init
local byte=0
for j=1,#buf,1 do
byte = string.byte(buf,j)
ret=((ret ~ byte) & 0xFFFF)
for i=1,8,1 do
if((ret & 0x0001)>0) then
ret = (ret >> 1)
ret = ((ret ~ poly) & 0xFFFF)
else
ret= (ret >> 1)
end
end
end
local hi = ((ret >> 8) & 0xFF)
local lo = (ret & 0xFF)
ret = ((lo << 8) | hi)
return ret
end
local com1 = serial.open("/dev/ttyS0")
com1:set_timeout(300)
com1:set_baud_rate(9600)
com1:set_parity("None")
function Send_Cmd_to_com(cmd)
com1:write(cmd)
for i = 1,3 do
wait(100)
success, result = pcall(function() return com1:read() end)
if success==true and result~=nil then
crc_response = Crc16(string.char(table.unpack(result,1,#result-2)))
if result[#result-1]==crc_response>>8 and result[#result]==crc_response&0xFF then
return result
end
end
end
return false
end
function Make_ice(out_ice_time)
if out_ice_time==nil then
out_ice_time=10
end
makeice_cmd = {0xA5,0x5A,0x01,0x02}
out_time= math.ceil(out_ice_time)
table.insert(makeice_cmd, out_time&0xff00)
table.insert(makeice_cmd, out_time&0xff)
crc_result = Crc16(string.char(table.unpack(makeice_cmd)))
table.insert(makeice_cmd, crc_result>>8)
table.insert(makeice_cmd, crc_result&0xFF)
make_ice_result= Send_Cmd_to_com(makeice_cmd)
if make_ice_result ~=false then
if #make_ice_result==8 then
status_code=table.unpack(make_ice_result,5,5)
if status_code==0 then
return true
else
return false
end
elseif #make_ice_result==7 then
abnormal_code= table.unpack(make_ice_result,5,5)
if abnormal_code==0 then
return '未定义错误'
elseif abnormal_code==2 then
return '无法识别命令'
elseif abnormal_code==3 then
return 'CRC校验出错'
elseif abnormal_code==4 then
return '系统忙'
end
end
else
print('发送出冰命令时,与制冰机间的通讯异常')
return '通讯异常'
end
end
function Read_machine_status()
read_status_cmd = {0xA5,0x5A,0x01,0x01,0x00,0x00,0x10,0xDF}
read_status_result= Send_Cmd_to_com(read_status_cmd)
if read_status_result ~=false then
if #read_status_result==8 then
machine_status_code= table.unpack(read_status_result,5,5)
error_code = table.unpack(read_status_result,6,6)
print('制冰机器状态码:', machine_status_code , ' 错误码:',error_code )
if machine_status_code==2 then
return '空闲待机'
elseif machine_status_code==3 then
return '出冰或出水中'
elseif machine_status_code==4 then
return '排冰'
elseif machine_status_code==5 then
if error_code==1 then
return '供水不足'
elseif error_code==2 then
return '制冰机内部温度过低'
elseif error_code==3 then
return '制冰机内部温度过高'
elseif error_code==4 then
return '压缩机温度过高'
elseif error_code==5 then
return '排气孔温度过低'
elseif error_code==6 then
return '电机电流过大'
elseif error_code==7 then
return '通讯异常'
elseif error_code==8 then
return '数据存储出错'
elseif error_code==9 then
return '电机堵转'
elseif error_code==10 then
return '电机警告'
end
elseif machine_status_code==6 then
return '清洗'
elseif machine_status_code==1 or machine_status_code==0 then
return '上电初始化中'
elseif machine_status_code==7 then
return '等待取冰'
end
elseif #read_status_result==7 then
abnormal_code= table.unpack(read_status_result,5,5)
if abnormal_code==0 then
return '未定义错误'
elseif abnormal_code==2 then
return '无法识别命令'
elseif abnormal_code==3 then
return 'CRC校验出错'
elseif abnormal_code==4 then
return '系统忙'
end
end
else
print('读取制冰机状态时,与制冰机的通讯异常' )
return '通讯异常'
end
return false
end
function Read_icebucket_status()
read_icebucket_cmd = {0xA5,0x5A,0x01,0x03,0x00,0x00,0xB1,0x1F}
read_icebucket_result= Send_Cmd_to_com(read_icebucket_cmd)
if read_icebucket_result~=false then
if #read_icebucket_result==8 then
icebucket_status_code= table.unpack(read_icebucket_result,5,5)
if icebucket_status_code==1 then
return '冰桶满'
else
return '冰未满'
end
elseif #read_icebucket_result==7 then
icebucket_abnormal_code= table.unpack(read_icebucket_result,5,5)
if icebucket_abnormal_code==0 then
return '未定义错误'
elseif icebucket_abnormal_code==2 then
return '无法识别命令'
elseif icebucket_abnormal_code==3 then
return 'CRC校验出错'
elseif icebucket_abnormal_code==4 then
return '系统忙'
end
end
else
print('读取冰桶状态时,与制冰机的通讯异常' )
return '通讯异常'
end
return false
end
print(Make_ice(10))
print(Read_machine_status())
print(Read_icebucket_status())
机箱modbus
local com1 = serial.open("/dev/ttyS1")
com1:set_timeout(200)
com1:set_baud_rate(9600)
local mb = modbus.new_rtu(com1)
mb:set_timeout(500)
mb:set_slave(0x01)
success, result = pcall(function() mb:write_single_coil(0x0000, true) end)
if not success then
print("Error: "..tostring(result))
else
print("Write successful")
end
success, result = pcall(function()
mb:write_multiple_coils(0x0000, {true,true,true,true,true})
end)
if not success then
print("Error: ", result)
else
print("Write successful")
end
success, result = pcall(function() return mb:read_coils(0x0000, 5) end)
if not success then
print("Error: ", result)
else
print(result)
end
mb:write_single_register(0x0090, 0x0088)
mb:write_multiple_registers(0x0090, {0x0088, 0x0088})
mb:read_holding_registers(0x0090, 2)
mb:read_input_registers(0x0080, 2)
modbus控制三碁伺服电机
C_POSE = {}
J_POSE = {}
disable_auto_sync()
function String2Hex(s)
rst = ''
for i = 1, #s do
rst = rst .. string.format('0x%02X ', s[i])
end
return rst
end
function get_sign32(vx)
if (not vx) or (vx < 0x80000000) then
return vx
end
return vx - 0x100000000
end
function get_sign16(vx)
if (not vx) or (vx < 0x8000) then
return vx
end
return vx - 0x10000
end
function signed32_to_unsigned32(vx)
if not (vx<0) then
return vx
end
return vx + 0x100000000
end
com1 = serial.open("/dev/ttyS1")
com1:set_baud_rate(38400)
com1:set_parity("Even")
com1:set_timeout(200)
mb = modbus.new_rtu(com1)
mb:set_slave(0x01)
EI_status = 0
EI_address = 0x0000
function Servo_ON()
mb:write_multiple_registers(0x4230, {0, 1})
wait(20)
end
function Servo_OFF()
mb:write_multiple_registers(0x4230, {0, 0})
wait(20)
end
function Clear_EI()
mb:write_multiple_registers(EI_address, {0, 0})
wait(20)
end
function Set_zero_EI()
Clear_EI()
mb:write_multiple_registers(EI_address, {0x0100>>16, 0x0100&0x0000FFFF})
wait(20)
end
function Find_zero_EI()
Clear_EI()
mb:write_multiple_registers(EI_address, {0x0400>>16, 0x0400&0x0000FFFF})
wait(20)
end
function Go_position_EI()
Clear_EI()
mb:write_multiple_registers(EI_address, {0x1>>16, 0x1&0x0000FFFF})
wait(20)
end
function Read_EI()
EI_status = mb:read_coils(0x0400, 5)
print(EI_status)
if EI_status ~= false then
local rst = {}
rst['home'] = not EI_status[5]
rst['OT-'] = not EI_status[2]
rst['OT+'] = not EI_status[4]
return rst
else
return false
end
end
function Set_position( position_value )
position_value = signed32_to_unsigned32(position_value)
print(position_value)
print({position_value>>16, position_value&0x0000FFFF})
mb:write_multiple_registers(0x5102 , {position_value>>16, position_value&0x0000FFFF})
wait(20)
end
function Read_position()
local rst_position = mb:read_holding_registers(0x100c, 2)
wait(20)
if rst_position == false then
return false
end
local current_position = get_sign32((rst_position[1]<<16) + rst_position[2]) * 32 / 4000
print('Current position:' ,current_position)
return current_position
end
function Set_position_speed(speed_value)
mb:write_multiple_registers(0x5104 , {speed_value>>16, speed_value&0x0000FFFF})
wait(20)
end
function Read_position_speed()
speed_rst = mb:read_holding_registers(0x5104, 2)
wait(20)
current_speed = get_sign32((speed_rst[1]<<16) + speed_rst[2] )
print('Current speed:' ,current_speed)
return current_speed
end
function Read_vel()
print(444)
local rst = mb:read_holding_registers(0x1000, 2)
wait(20)
print(777)
if rst == false then
return false
end
local current_vel = get_sign32((rst[1]<<16) + rst[2])
print('Current velocity:' , math.modf(current_vel))
return current_vel
end
function Set_abs_mode()
mb:write_multiple_registers(0x5100, {0x00FF0000>> 16,0x00FF0000&0x0000FFFF})
wait(20)
end
function Init_servo()
if Read_vel() ~= 0 then
print('第七轴运动中, 不能复位.')
return false
end
Set_zero_EI()
Find_zero_EI()
Set_abs_mode()
while true do
wait(500)
if Read_vel() == 0 and math.abs(Read_position()) < 5 then
break
end
end
return true
end
function Move(pos,move_speed)
if move_speed==nil then
move_speed=80000
end
if Read_vel() ~= 0 then
print('第七轴运动中, 不能接收移动指令.')
return false
end
pos_driver = math.modf(pos / 32 * 4000)
print(pos_driver)
Set_position(pos_driver)
Set_position_speed(20000)
Go_position_EI()
while true do
if math.abs(Read_position() - pos) < 4 then
return true
else
wait(500)
end
end
end
末端法兰控制modbus夹爪
local mb = modbus.new_flange()
mb:set_timeout(600)
mb:set_slave(0x01)
mb:write_single_register(0x9c40, 100)
wait(100)
mb:write_single_register(0x9c41, 10)
amplitude = mb:read_holding_registers(0x9c45, 1)
print(amplitude)
force = mb:read_holding_registers(0x9c46, 1)
print(force)
success, result = pcall(function() return mb:read_holding_registers(0x9c46, 1) end)
if not success then
print("Error: ", result)
else:
print(result)
end
modbus TCP示例
mb = modbus.new_tcp('192.168.4.85', 22)
mb:set_timeout(500)
mb:set_slave(0x01)
function recv_data()
success, result = pcall(function() return mb:read_holding_registers(0x0000, 2) end)
if success then
print('recieve data ',result)
local data = string.char(table.unpack(result))
return data
else
print("Error: ", result)
end
end
function send_data(address,data)
success, result = pcall(function() mb:write_multiple_registers(address, data) end)
if success then
return true
else
print("Error: ", result)
return false
end
end
while true
do
wait(50)
data = recv_data()
if(data == "1")
then
scene(10001)
sync()
local str = "finish 1"
local address = 0x0000
send_data(address, {string.byte(str, 1, #str)})
elseif(data == "2")
then
scene(10001)
sync()
local str = "finish 2"
local address = 0x0000
send_data(address, {string.byte(str, 1, #str)})
end
end
空间位置计算
相对点位计算
current_tcp_pose = get_target_tcp_pose()
print('current_tcp_pose',current_tcp_pose)
offset_position = {0, 0, 0.1, 0, 0, 0}
calculate_location = pose_times(current_tcp_pose, offset_position)
print('calculate_location',calculate_location)
movej(calculate_location, 1, 0.5, 0, 0)
base = get_target_tcp_pose()
print('current_tcp_pose',base)
delta = {0, 0, 0.1,0,0, 0}
frame ={0, 0, 0, 0, 0, 0}
pose = pose_add(base, delta, frame)
print('pose',pose)
movej(pose, 1, 0.5, 0, 0)
base = get_target_tcp_pose()
print('current_tcp_pose',base)
delta = {0, 0, 0.1, 0, 0, 0}
frame = {0, 0, 0, 0 , 0.78, 0}
pose = pose_add(base, delta, frame)
print('pose',pose)
movej(pose, 1, 0.5, 0, 0)
运动控制
轨迹复现
name = "test"
function on_robot_state(state)
if state == 11
then
start_record_trajectory(0.01)
end
if last_state == 11
then
end_record_trajectory(name)
move_trajectory(name)
end
last_state = state
end
while true
do
sleep(1000)
end
画五角星
function clone(x)
return {x[1],x[2],x[3],x[4],x[5],x[6]}
end
local p = math.rad(36)/2
local vel = 0.06
local acc = 0.1
function 画☆(a, r)
local x = r * math.sin(p)
local y = r * math.cos(p)
print('x', x, 'y', y)
print('a', a)
movej(a, 0.4, 1, 0, 1)
wait(1000)
b = clone(a)
b[1] = a[1] - x
b[2] = a[2] - y
print('b', b)
movel(b, vel, acc, 0, 0)
c = clone(a)
c[1] = a[1] + r / 2
c[2] = a[2] - r / 2 * math.cos(math.rad(36))
print('c', c)
movel(c, vel, acc, 0, 0)
d = clone(a)
d[1] = a[1] - r / 2
d[2] = c[2]
print('d', d)
movel(d, vel, acc, 0, 0)
e = clone(a)
e[1] = a[1] + x
e[2] = a[2] - y
print('e', e)
movel(e, vel, acc, 0, 0)
movel(a, vel, acc, 0, 0)
end
a = {-0.34, -0.12, 0.4, -1.57, 0, 0.25}
for i=0,2 do
a[3] = a[3] - 0.08
画☆(a, 0.1+i*0.02)
end
a[2] = a[2] + 0.2
a[3] = a[3] - 0.04
for i=0,2 do
a[3] = a[3] + 0.08
画☆(a, 0.1+i*0.02)
end
双机同步
disable_auto_sync()
lebai = lebai_sdk.connect("127.0.0.1", false)
target = lebai_sdk.connect("192.168.4.100", false)
target:start_sys()
wait(3000)
pose = (lebai:get_kin_data())["actual_joint_pose"]
target:movej(pose, 0.1, 0.1, 0, 0)
target:wait_move()
print("start")
last_time = timestamp()
start_flag =0
while true
do
wait(10)
now = timestamp()
used_time = now-last_time
last_time = now
status = lebai:get_kin_data()
target:move_pvat(status["actual_joint_pose"], status["actual_joint_speed"], status["actual_joint_acc"], used_time/1000)
if start_flag == 0 then
start_flag = 1
scene_id = 10289
start_task(scene_id, nil, '', true, 1)
end
end
C_POSE = {}
J_POSE = {}
disable_auto_sync()
local robot_ips = {
"127.0.0.1",
"192.168.6.109" ,
"192.168.4.57",
"192.168.4.109"
}
local robots = {}
for i, ip in ipairs(robot_ips) do
success, result = pcall(function() return lebai_sdk.connect(ip, false) end)
if success then
robots[i] = result
print("成功连接: " .. ip)
robots[i]:start_sys()
else
print("连接失败: " .. ip)
end
end
wait(3000)
local pose = (robots[1]:get_kin_data())["actual_joint_pose"]
for i =1,#robot_ips do
if robots[i] then
robots[i]:movej(pose, 0.5, 0.5, 0, 0)
end
end
for i =1,#robot_ips do
if robots[i] then
robots[i]:wait_move()
end
end
print("开始动作")
for i =1,#robot_ips do
if robots[i] then
robots[i]:start_task(10010, nil, '', true, 2)
end
end
print("已经全部启动")
IO控制
DI交互控制
tasks = {}
function cancel_all_tasks()
for k,v in pairs(tasks) do
print(k,v)
cancel_task(v)
tasks[k] = nil
end
end
button_type = "SHOULDER"
while true do
wait(100)
if get_di(0) == 1 then
wait_di(0, 0, "=")
cancel_all_tasks()
tasks["0"] = start_task("10160", {}, "", true, 1)
end
if get_di(1) == 1 then
wait_di(1, 0, "=")
cancel_all_tasks()
tasks["1"] = start_task("10160", {}, "", true, 1)
end
if lebai:get_di(button_type, 0)==1 then
while lebai:get_di(button_type, 0)==1 do
wait(20)
end
cancel_all_tasks()
tasks["0"] = start_task("10160", {}, "", true, 1)
end
end
DI交互控制夹爪
set_auto('ARM_POWER' , true)
set_auto('ENABLE_JOINT' , true)
claw_status ='close'
button_type = "SHOULDER"
button_times = 0
interval_flag = timestamp()
while true do
if timestamp()-interval_flag >2000 then
button_times = 0
end
if lebai:get_di(button_type, 0)==1 then
while lebai:get_di(button_type, 0)==1 do
wait(20)
end
button_times = button_times + 1
if button_times ==1 then
interval_flag = timestamp()
elseif button_times == 3 then
button_times = 0
if claw_status =='close' then
claw_status='open'
print(claw_status)
set_claw(100,100)
sync()
elseif claw_status=='open' then
claw_status='close'
set_claw(100,0)
print(claw_status)
sync()
end
end
end
wait(20)
end
DO控制泵
object_pose = {j1=-1.9267757433836,j2=-1.9655087582777,j3=2.2828510337716,j4=-0.34313232749017,j5=-3.9459738292373,j6=0}
object_pose_top = pose_add(object_pose, {0,0,0.15,0,0,0})
movej(object_pose_top, 0.5, 0.5, 0, 0)
movel_until(object_pose, 0.2, 0.08, 0, function() return get_flange_di(0) == 1 end)
set_do(0, 1)
set_do(1, 0)
movej(object_pose_top, 0.5, 0.5, 0, 0)
put_pose = {j1=0,j2=-1.9655087582777,j3=2.2828510337716,j4=-0.34313232749017,j5=-3.9459738292373,j6= 0}
movej(put_pose, 0.5, 0.5, 0, 0)
set_do(0, 0)
set_do(1, 1)
wait(2000)
set_do(1, 0)