Lua 例程

多线程

function 运动(param)
    print(param, '开始运行')
    movej({j1=0,j2=0,j3=0,j4=0,j5=0,j6=0}, 0, 0, 1, 0)
    return "运动完成"
end

function 夹爪()
    wait(1000)
    set_claw(0, 100)
    return "夹爪完成"
end

wait_all = wait_all or join

ret1, ret2 = wait_all(async_task(运动, '线程一'), async_task(夹爪))
print(ret1, ret2)

绘制几何图形

local v = 0.06
local a = 0.1

function 画☆(base, r)
  local x = r * math.sin(math.rad(18))

  local a = {0,0,0,0,0,0,base=base}
  movej(a, 0.4, 1, 0, 1)

  local b = {-x,-y,0,0,0,0,base=base}
  movel(b, v, a, 0, 0)

  local c = {r / 2, -r / 2 * math.cos(math.rad(36)), 0,0,0,0,base=base}
  movel(c, v, a, 0, 0)

  local d = {-r / 2, c[2], 0,0,0,0,base=base}
  movel(d, v, a, 0, 0)

  local e = {x, -y, 0,0,0,0,base=base}
  movel(e, v, a, 0, 0)

  movel(a, v, a, 0, 0)
end

local a = get_actual_tcp_pose()

画☆(a, 0.1)

HTTP 请求和解析

local http = require("socket.http")
local json = require("json")

body, code, header = http.request("http://10.20.17.1/public/api/condition_tasks")

data = json.decode(body)
print(data, code, header)

输出结果:

{code=2005}  404  {connection=close,content-type=application/json; charset=utf-8,content-length=25,date=Wed, 07 Apr 2021 10:28:07 GMT,cache-control=no-cache}

MODBUS 主站

-- local mb = modbus.new_rtu("/dev/ttyS1", 9600)
local mb = modbus.new_tcp("192.168.1.2", 10001)
mb:set_slave(0x01)

-- 设置1路DO
mb:write_single_coil(0x0000, true)
-- 设置5路DO
mb:write_multiple_coils(0x0000, {true,true,true,true,true})
-- 读取5路DO
mb:read_coils(0x0000, 5)
-- 读取5路DI
mb:read_discrete_inputs(0x0000, 5)

-- 设置1路AO
mb:write_single_register(0x0090, 0x0088)
-- 设置2路AO
mb:write_multiple_registers(0x0090, {0x0088, 0x0088})
-- 读取2路AO
mb:read_holding_registers(0x0090, 2)
-- 读取2路AI
mb:read_input_registers(0x0080, 2)
上次更新: