diff --git a/agent.lua b/agent.lua index 329332a..1dfc4f4 100644 --- a/agent.lua +++ b/agent.lua @@ -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, @@ -171,22 +171,30 @@ 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, @@ -194,14 +202,13 @@ function Agent() -- 计算坐标 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 diff --git a/agv.lua b/agv.lua index bdc5d5b..905c8be 100644 --- a/agv.lua +++ b/agv.lua @@ -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任务