Lua场景编程示例
乐白机器人Lua场景编程示例
运动控制
相对位置点计算
-- 求末端TCP坐标系的位置,在基座坐标系下的位置,进行相对于末端的运动
current_tcp_pose = get_target_tcp_pose() --当前末端坐标
print('current_tcp_pose',current_tcp_pose)
offset_position = {0, 0, 0.1, 0, 0, 0} --相对与末端tcp的位置偏移量,沿着末端tcp坐标系,z轴往前移动0.1m
calculate_location = pose_times(current_tcp_pose, offset_position) --计算的新位置
print('calculate_location',calculate_location)
movej(calculate_location, 1, 0.5, 0, 0)
--如果想让当前位置姿态,以某个坐标系,进行姿态变换。例如相对于基座坐标系,沿着基座z轴增加0.1
base = get_target_tcp_pose() --当前末端坐标
print('current_tcp_pose',base)
delta = {0, 0, 0.1,0,0, 0} --沿着frame坐标系z轴方向移动0.1m
frame ={0, 0, 0, 0, 0, 0} --基座坐标系 位姿偏移量方向,仅姿态部分有效。
pose = pose_add(base, delta, frame)
print('pose',pose)
movej(pose, 1, 0.5, 0, 0)
--相对于绕y轴旋转45°,使z轴斜向上 的新坐标系,沿着基座z轴增加0.1
base = get_target_tcp_pose() --当前末端坐标
print('current_tcp_pose',base)
delta = {0, 0, 0.1, 0, 0, 0} -- 沿着frame坐标系z轴方向移动0.1m
frame = {0, 0, 0, 0 , 0.78, 0} -- 绕y轴旋转45°,使z轴斜向上 的新坐标系
pose = pose_add(base, delta, frame)
print('pose',pose)
movej(pose, 1, 0.5, 0, 0)
画五角星
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 = get_actual_tcp_pose()
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.35", 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() --上一次同步的毫秒时间戳
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) --同步运动
end
通讯控制
串口通讯串口数据交互控制案例
com1 = serial.open("/dev/ttyS1")
com1:set_timeout(0)
com1:set_baud_rate(9600)
-- 将字节流转换为16进制表示
function table2Hex(s)
rst = ''
for i = 1, #s do
rst = rst .. string.format('0x%02X ', s[i])
-- print(s[i])
end
return rst
end
function recv_data()
-- 读取串口,使用pcall捕获错误
success, result = pcall(function() return com1:read() end)
if success then
print(table2Hex(result)) -- 打印HEX
local data = string.char(table.unpack(result))
return data
else
print("Error: ", result)
end
end
while true
do
data = recv_data()
if(data == "1")
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 = "finish 1"
com1:write({string.byte(str, 1, #str)}) -- 发送字符串
elseif(data == "2")
then
scene(10001) -- 执行场景
set_claw(100,0)
local str = "finish 2"
com1:write({string.byte(str, 1, #str)}) -- 发送字符串
else
print("unknown data:", data)
end
end
任务场景调用
场景调用
--在场景A中调用B
print(scene(10216,{'{1, 2}', 3, 4}))-- 打印场景返回值
print(var_test) -- 打印全局变量
--在场景B中解析传参
local params = ...
print("params: ", params)
print('打印参数类型',type(params[1]))
local json = require("json")
-- 如果通过python调用start_task传参,参数形式为 {'{"j1":"aa"}', 3, 4} 则第1个参数解析 table_data = json.decode(param)
-- 解析字符串成lua可用位置形式
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
任务调用
--在场景A中调用B
task_id = start_task(10216,{'{1, 2}', 3, 4}, "", true, 1)
--在lua场景中执行,注意这里的第四个参数,必须true 并行执行,因为正在执行的场景本身就是个串行任务,会阻塞程序
--另外这里不像scene返回场景的返回值