coppelia wrote: ↑06 Jul 2021, 14:28
From a non-threaded script, you could do something like:
Code: Select all
function sysCall_actuation()
if eventJustOccured then
eventJustOccured=nil
timeEventOccured=sim.getSimulationTime()
end
if timeEventOccured and sim.getSimulationTime()-timeEventOccured>=timeDelay then
timeEventOccured=nil
-- respond to event here
end
end
From a threaded script on the other hand, you can do:
Code: Select all
if eventJustOccured then
eventJustOccured=nil
sim.wait(timeDelay) -- wait timeDelay seconds (in sim. time)
-- respond to event here
end
Cheers
Thanks for your reply. I've tried to do like you said, but it's failed TOT. I want to control a car by joystick, and achieve time delay, which means when I push the joystick, the car will go forward after a time delay. The code controlling the car is following:
Code: Select all
axes, buttons,rotAxes,slider,pov = simJoy.getData(0)
if eventJustOccured then
eventJustOccured=nil
if(axes[2]>114 and -250<rotAxes[3] and rotAxes[3]<250) then
sim.wait(X[b][1])
axes[2]=-axes[2]/10000
sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*axes[2]*v)/(s*wheelRadius))
sim.setJointTargetVelocity(rightJointDynamic,(linearVelocityRight*axes[2]*v)/(s*wheelRadius))
m=2
elseif(axes[2]<-118 and -250<rotAxes[3] and rotAxes[3]<250) then
sim.wait(X[b][1])
axes[2]=-axes[2]/10000
sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*axes[2]*v)/(s*wheelRadius))
sim.setJointTargetVelocity(rightJointDynamic,(linearVelocityRight*axes[2]*v)/(s*wheelRadius))
m=1
elseif(131<rotAxes[3]) then
sim.wait(X[b][1])
rotAxes[3]=rotAxes[3]/50000
sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*rotAxes[3]*v/3)/(s*wheelRadius))
sim.setJointTargetVelocity(rightJointDynamic,-(linearVelocityRight*rotAxes[3]*v/3)/(s*wheelRadius))
m=4
elseif(rotAxes[3]<-131) then
sim.wait(X[b][1])
rotAxes[3]=rotAxes[3]/50000
sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*rotAxes[3]*v/3)/(s*wheelRadius))
sim.setJointTargetVelocity(rightJointDynamic,-(linearVelocityRight*rotAxes[3]*v/3)/(s*wheelRadius))
m=3
else
sim.wait(X[b][1])
m=0
sim.setJointTargetVelocity(leftJointDynamic,0)
sim.setJointTargetVelocity(rightJointDynamic,0)
end
end
Then, when I simulated the scene, it had an error: bad argument #9 to 'write' (string expected, got nil)
stack traceback:
[C]: in function 'write'
[string "LineTracer@childScript"]:254: in function <[string "LineTracer@childScript"]:67>
and the 254 line is a sentence recording the date:
Code: Select all
fp:write(string.format('%.2f',simulationtime),',',string.format('%.2f',current_position[1]),',',string.format('%.2f',current_position[2]),',',string.format('%.2f',math.deg(current_orientation[3])),',',current_moving(m),',',collisionTimes,',',X[b][1],',',current_map(p),',',current_clearance(c),'\n')
The problem is, only when I used sim.wait, it reports this error. When I used sleep function like I said before, there is no error.
How to solve it, please help me TOT!