Skip to content

Commit b03a536

Browse files
committed
new addon
1 parent 2c5087a commit b03a536

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
sim = require 'sim'
2+
3+
function sysCall_info()
4+
return {autoStart = false, menu = 'Tools\nPause/Stop Simulation on condition...'}
5+
end
6+
7+
function sysCall_init()
8+
simUI = require 'simUI'
9+
ui = simUI.create[[<ui closeable="true" title="Pause/Stop Simulation on condition" placement="relative" position="-10,500" on-close="uiClosed">
10+
<label text="This add-on will pause or stop the simulation when the specified condition occurs" word-wrap="true" />
11+
<group id="${ui_all}" flat="true" content-margins="0,0,0,0" layout="form">
12+
<label text="Action:" />
13+
<group flat="true" content-margins="0,0,0,0" layout="hbox">
14+
<radiobutton text="Pause" on-click="setActionPause" checked="true" />
15+
<radiobutton text="Stop" on-click="setActionStop" />
16+
</group>
17+
<label text="Condition:" />
18+
<group flat="true" content-margins="0,0,0,0">
19+
<combobox id="${ui_condition}">
20+
<item>When all objects stop moving</item>
21+
</combobox>
22+
<group id="${ui_grp_condStatic}" visible="true" flat="true" content-margins="0,0,0,0">
23+
<label text="Time threshold: [s]" />
24+
<edit id="${ui_timeThreshold}" value="1.0" />
25+
</group>
26+
</group>
27+
</group>
28+
</ui>]]
29+
setActionPause()
30+
setConditionWhenObjectsStopMoving()
31+
end
32+
33+
function sysCall_nonSimulation()
34+
if leaveNow then return {cmd = 'cleanup'} end
35+
end
36+
37+
function sysCall_addOnScriptSuspend()
38+
return {cmd = 'cleanup'}
39+
end
40+
41+
function sysCall_beforeSimulation()
42+
threshold = tonumber(simUI.getEditValue(ui, ui_timeThreshold))
43+
reset()
44+
simUI.setEnabled(ui, ui_all, false)
45+
end
46+
47+
function sysCall_suspend()
48+
simUI.setEnabled(ui, ui_all, true)
49+
end
50+
51+
function sysCall_resume()
52+
threshold = tonumber(simUI.getEditValue(ui, ui_timeThreshold))
53+
reset()
54+
simUI.setEnabled(ui, ui_all, false)
55+
end
56+
57+
function sysCall_afterSimulation()
58+
simUI.setEnabled(ui, ui_all, true)
59+
end
60+
61+
function sysCall_actuation()
62+
if leaveNow then return {cmd = 'cleanup'} end
63+
64+
checkCondition()
65+
end
66+
67+
function reset()
68+
lastLocalPose = {}
69+
lastPose = {}
70+
lastMotionTime = sim.getSimulationTime()
71+
end
72+
73+
function onStaticConditionReached()
74+
sim.addLog(sim.verbosity_scriptinfos, 'Static condition reached: pausing simulation...')
75+
sim.pauseSimulation()
76+
end
77+
78+
function uiClosed()
79+
leaveNow = true
80+
end
81+
82+
function setActionPause()
83+
onStaticConditionReached = function()
84+
sim.addLog(sim.verbosity_scriptinfos, 'Condition reached: pausing simulation...')
85+
sim.pauseSimulation()
86+
end
87+
end
88+
89+
function setActionStop()
90+
onStaticConditionReached = function()
91+
sim.addLog(sim.verbosity_scriptinfos, 'Condition reached: stopping simulation...')
92+
sim.stopSimulation()
93+
end
94+
end
95+
96+
function setConditionWhenObjectsStopMoving()
97+
simUI.setWidgetVisibility(ui, ui_grp_condStatic, true)
98+
checkCondition = function()
99+
local d, n = 0, 0
100+
for _, h in ipairs(sim.getObjectsInTree(sim.handle_scene)) do
101+
local lp = sim.getObjectPosition(h, sim.handle_parent)
102+
local p = sim.getObjectPosition(h)
103+
if lastPose[h] then
104+
d = d + (Vector(lp) - Vector(lastLocalPose[h])):norm()
105+
d = d + (Vector(p) - Vector(lastPose[h])):norm()
106+
end
107+
lastPose[h] = p
108+
lastLocalPose[h] = lp
109+
n = n + 2
110+
end
111+
d = d / n
112+
if d > 0.0001 then
113+
lastMotionTime = sim.getSimulationTime()
114+
end
115+
if (sim.getSimulationTime() - lastMotionTime) > threshold then
116+
onStaticConditionReached()
117+
end
118+
end
119+
end

0 commit comments

Comments
 (0)