Skip to content

Commit

Permalink
[feat] 修改agent的默认move任务为AGV模式的move
Browse files Browse the repository at this point in the history
删除AGV的move任务,直接继承自Agent
  • Loading branch information
huuhghhgyg committed Jun 16, 2024
1 parent ac6ea94 commit 82ee751
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 57 deletions.
37 changes: 22 additions & 15 deletions agent.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Agent()
local agent = {
speed = {1, 1, 1},
speed = 1,
model = nil,
pos = {0, 0, 0}, -- Agent位置,只有init更新,相当于每个任务的Origin
taskstart = nil,
Expand Down Expand Up @@ -171,37 +171,44 @@ function Agent()
end
}

-- params = {x, y, z, ...}
-- {'move2', {x, y, z, ...}}
agent.tasks.move2 = {
init = function(params)
print('init move2 at', coroutine.qtime())
local px, py, pz = agent.model:getpos()
agent.lastpos = {px, py, pz}
params.est, params.delta, params.vecE = {}, {}, {}
-- 参数检查
if type(params[1]) ~= 'number' or type(params[2]) ~= 'number' or type(params[3]) ~= 'number' then
print(agent.type .. agent.id, 'move2错误:输入的坐标参数有误:', params[1], params[2], params[3])
print(debug.traceback())
os.exit()
end

-- 计算坐标
-- 设置任务初始位置
agent.lastpos = {table.unpack(agent.pos)}

-- 设置参数
params.delta = {params[1] - agent.lastpos[1], params[2] - agent.lastpos[2], params[3] - agent.lastpos[3]} -- 位移
local distance = math.sqrt(params.delta[1] ^ 2 + params.delta[2] ^ 2 + params.delta[3] ^ 2)
params.est = distance / agent.speed -- 预计到达时间
params.speed = {} -- agent只有一个总速度,需要计算分方向的速度
-- 计算速度
for i = 1, 3 do
params.delta[i] = params[i] - agent.lastpos[i]
params.vecE[i] = params.delta[i] == 0 and 0 or params.delta[i] / math.abs(params.delta[i])
params.est[i] = params.delta[i] == 0 and 0 or math.abs(params.delta[i]) / agent.speed[i]
params.speed[i] = params.delta[i] == 0 and 0 or params.delta[i] / distance * agent.speed
end
params.dt = math.max(table.unpack(params.est)) -- 任务所需时间

params.dt = params.est -- 这个任务中est就是dt
params.init = true -- 标记完成初始化
coroutine.queue(params.dt, agent.execute, agent) -- 结束时间唤醒execute
end,
execute = function(dt, params)
-- 计算坐标
local position = {table.unpack(agent.lastpos)}
for i = 1, 3 do
position[i] = dt <= params.est[i] and agent.lastpos[i] + agent.speed[i] * dt * params.vecE[i] or
params[i]
position[i] = position[i] + params.speed[i] * dt
end

-- 设置位置
-- 设置agent位置
agent:setpos(table.unpack(position))

if dt == params.dt then
if math.abs(params.dt - dt) < agent.timeError then -- 如果时间误差小于agent.timeerror,任务结束
agent.pos = position -- 更新位置
agent:deltask() -- 删除任务
end
Expand Down
43 changes: 1 addition & 42 deletions agv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,48 +54,7 @@ function AGV(config)

-- 任务
-- {'move2', {x, y, z, ...}}
agv.tasks.move2 = {
init = function(params)
-- 参数检查
if type(params[1]) ~= 'number' or type(params[2]) ~= 'number' or type(params[3]) ~= 'number' then
print(agv.type .. agv.id, 'move2错误:输入的坐标参数有误:', params[1], params[2], params[3])
print(debug.traceback())
os.exit()
end

-- 设置任务初始位置
agv.lastpos = {table.unpack(agv.pos)}

-- 设置参数
params.delta = {params[1] - agv.lastpos[1], params[2] - agv.lastpos[2], params[3] - agv.lastpos[3]} -- 位移
local distance = math.sqrt(params.delta[1] ^ 2 + params.delta[2] ^ 2 + params.delta[3] ^ 2)
params.est = distance / agv.speed -- 预计到达时间
params.speed = {} -- agv只有一个总速度,需要计算分方向的速度
-- 计算速度
for i = 1, 3 do
params.speed[i] = params.delta[i] == 0 and 0 or params.delta[i] / distance * agv.speed
end

params.dt = params.est -- 这个任务中est就是dt
params.init = true -- 标记完成初始化
coroutine.queue(params.dt, agv.execute, agv) -- 结束时间唤醒execute
end,
execute = function(dt, params)
-- 计算坐标
local position = {table.unpack(agv.lastpos)}
for i = 1, 3 do
position[i] = position[i] + params.speed[i] * dt
end

-- 设置agv位置
agv:setpos(table.unpack(position))

if math.abs(params.dt - dt) < agv.timeError then -- 如果时间误差小于agv.timeerror,任务结束
agv.lastpos = position -- 更新位置
agv:deltask() -- 删除任务
end
end
}
-- 继承自Agent

-- {'waitoperator', {operator = operator}}
-- operator需要通知本agent结束waitoperator任务
Expand Down

0 comments on commit 82ee751

Please sign in to comment.