-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatchdog.lua
More file actions
154 lines (135 loc) · 5.05 KB
/
watchdog.lua
File metadata and controls
154 lines (135 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
-- simv: 仿真速度
-- ActionObjs: 所有需要更新的对象
function WatchDog(simv, ActionObjs, config)
-- 验证参数
if type(simv) ~= 'number' then
print(debug.traceback('[WatchDog] simv: ' .. simv .. ' not a number'))
elseif type(ActionObjs) ~= 'table' then
print(debug.traceback('[WatchDog] ActionObjs: ' .. ActionObjs .. ' not a table'))
end
-- 默认参数
local watchdog = {
-- 时间
lasttime = nil, -- 上一次更新的系统时间
-- 程序控制
runcommand = true,
isImmediateStop = true, -- 没有任务的时候立刻停止
maxdt = 60 / 1000, -- 最大时间步长限制
maxdt_step = 0, -- 实际使用的替代时间步长,如果CPU过慢建议使用maxdt代替
recycleType = {"agv"} -- 需要回收的对象类型
}
-- 更新参数
if config == nil then
config = {}
end
-- 立即停止参数
if config.isImmediateStop ~= nil then
watchdog.isImmediateStop = config.isImmediateStop
end
watchdog.recycleType = config.recycleType or watchdog.recycleType -- 需要回收的对象类型
-- delta时间上限(考虑暂停按钮)
watchdog.maxdt = config.maxdt or watchdog.maxdt
watchdog.maxdt_step = config.maxdt or watchdog.maxdt_step
function watchdog.refresh(f)
if type(f) == 'function' then
f()
end
-- 参数检查
if watchdog.lasttime == nil then -- 避免自动开始计时
watchdog.lasttime = os.clock()
end
-- 刷新所有agent的状态
local actionObjNum = 0 -- 有效更新agent数量
for i = 1, #ActionObjs do
local agent = ActionObjs[i]
if #agent.tasksequence > 0 then
agent:execute() -- 刷新对象状态,可能导致任务删除并预定新时间
actionObjNum = actionObjNum + 1
end
end
-- 检查是否需要回收
watchdog:scanRecycle()
-- 检查运行许可
watchdog.runcommand = scene.render(0)
if not watchdog.runcommand then
print('刷新已停止')
return
end
-- 检查是否需要立刻停止
if watchdog.isImmediateStop and actionObjNum == 0 then
print('无任务,刷新已停止')
return
end
-- 更新时钟
local now = os.clock()
local dt = now - watchdog.lasttime -- 本次调度与上次调度的时间间隔
dt = dt > watchdog.maxdt and watchdog.maxdt_step or dt -- 限制dt的上限(设为0可能导致过慢的CPU上无法运行)
watchdog.lasttime = now -- 刷新调度时间记录
-- 预定下一次更新
coroutine.queue(dt * simv, watchdog.refresh, f)
end
-- 打印所有组件任务列表
function watchdog:printTasks(objs)
print('watchdog debug printTasks at ', coroutine.qtime())
for k, obj in ipairs(objs) do
print(obj.type .. obj.id, 'executing', obj.tasksequence[1][1])
-- 打标签
if obj.label == nil then
obj.label = scene.addobj('label', {
text = obj.type .. obj.id
})
end
local x, y, z = obj:getpos()
obj.label:setpos(x, y + 5, z)
end
print('===================================')
end
function inRecycleList(objTypeStr)
assert(type(objTypeStr) == "string", "输入的类型不是字符串")
for _, v in ipairs(watchdog.recycleType) do
if v == objTypeStr then
return true
end
end
end
-- 检测是否需要回收
function watchdog:scanRecycle()
for i = 1, #ActionObjs do
local obj = ActionObjs[i]
if inRecycleList(obj.type) and #obj.tasksequence == 0 then
watchdog:recycle(obj)
table.remove(ActionObjs, i)
break -- 假设每次同时只能到达一个,因此可以中止
end
end
end
-- 回收某个对象
function watchdog:recycle(obj)
if inRecycleList(obj.type) then
if obj.container ~= nil then
obj.container:delete()
end
-- print('agv', obj.id, 'leave at ', coroutine.qtime())
obj:delete()
end
end
function watchdog:beforeStop()
for i = 1, #ActionObjs do
local obj = ActionObjs[i]
if obj.type ~= "container" then
local x, y, z = obj:getpos()
local label = scene.addobj('label', {
text = obj.type .. obj.id
})
label:setpos(x, y + 5, z)
elseif obj.type == "node" and obj.occupied then
local x, y, z = obj:getpos()
local label = scene.addobj('label', {
text = 'occupied by ' .. obj.occupied.type .. obj.occupied.id
})
label:setpos(x, y - 1, z)
end
end
end
return watchdog
end