real-time simulation and high fps

Typically: "How do I... ", "How can I... " questions
Post Reply
lwang87
Posts: 41
Joined: 24 May 2021, 16:47

real-time simulation and high fps

Post by lwang87 »

Hello,
In my scrip, I use sleep function to achieve time delay, which means that when I control the joystick, the model will response after a second. And I used real-time simulation(Select below the Simulation at top toolbar), because I want to record the model's data(its velocity per second, and its moving state per second and so on). But the fps is too high(above 121) when I start the simulation, which results my uncomfortable controlling. How to solve this problem? Please help me.

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: real-time simulation and high fps

Post by coppelia »

Hello,

not sure I understand what you mean. When in real-time mode, the simulation will be artificially slowed-down in order to keep a speed quite to real-time.
When you say you use sleep... what function is that? In CoppeliaSim, you should use sim.wait from a threaded script. Trying to wait or sleep from a non-threaded script, will halt/pause/block everything and does not really make sense in that case.

Cheers

lwang87
Posts: 41
Joined: 24 May 2021, 16:47

Re: real-time simulation and high fps

Post by lwang87 »

coppelia wrote: 05 Jul 2021, 12:27 Hello,

not sure I understand what you mean. When in real-time mode, the simulation will be artificially slowed-down in order to keep a speed quite to real-time.
When you say you use sleep... what function is that? In CoppeliaSim, you should use sim.wait from a threaded script. Trying to wait or sleep from a non-threaded script, will halt/pause/block everything and does not really make sense in that case.

Cheers
Hello, appreciate for your answer. Actually, I want to achieve a time delay. For example, I press W on keyboard to forward my car model, and the car will forward after a delay time. I've learned there is a sleep function of lua, the code is following:

Code: Select all

function sleep(n)
    local t = os.clock()
    while os.clock() - t <=n do end
end
when I write sleep(1), then my controlling will delay 1 second, which means that the model will response after 1 second I press the W. But this way will Take up too much CPU and lead to the higher fps. Is there any other way to achieve time delay?

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: real-time simulation and high fps

Post by coppelia »

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

lwang87
Posts: 41
Joined: 24 May 2021, 16:47

Re: real-time simulation and high fps

Post by lwang87 »

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!

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: real-time simulation and high fps

Post by coppelia »

You say The problem is, only when I used sim.wait, it reports this error. But the error you mention is not at all related to sim.wait...

Cheers

lwang87
Posts: 41
Joined: 24 May 2021, 16:47

Re: real-time simulation and high fps

Post by lwang87 »

coppelia wrote: 19 Jul 2021, 15:05 You say The problem is, only when I used sim.wait, it reports this error. But the error you mention is not at all related to sim.wait...

Cheers
Thanks for your reply. So...could you please tell me the meaning of the error I mentioned and how to solve it?

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: real-time simulation and high fps

Post by coppelia »

Your error is
bad argument #9 to 'write' (string expected, got nil)
, that means in other words that instead of a string, you provided the nil argument to write. And it even tells you that the 9th argument is the culprit...

Check what your variables are (e.g. print them), and if they possibly are nil

Cheers

lwang87
Posts: 41
Joined: 24 May 2021, 16:47

Re: real-time simulation and high fps

Post by lwang87 »

coppelia wrote: 22 Jul 2021, 09:03 Your error is
bad argument #9 to 'write' (string expected, got nil)
, that means in other words that instead of a string, you provided the nil argument to write. And it even tells you that the 9th argument is the culprit...

Check what your variables are (e.g. print them), and if they possibly are nil

Cheers
Thank you coppelia. I deleted that line of code that reported "#9....." error, and then added sim.wait to the part that controls the movement of the car:

Code: Select all

            axes, buttons,rotAxes,slider,pov = simJoy.getData(0) ---transfer the joystick's data
            if eventJustOccured then
                eventJustOccured=nil                
                if(axes[2]>114 and -250<rotAxes[3] and rotAxes[3]<250) then            
                    sim.wait(2)
                    axes[2]=-axes[2]/10000
                    sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*axes[2]*v)/(s*wheelRadius))
                    sim.setJointTargetVelocity(rightJointDynamic,(linearVelocityRight*axes[2]*v)/(s*wheelRadius))
                elseif(axes[2]<-118 and -250<rotAxes[3] and rotAxes[3]<250) then
                    sim.wait(2)                 
                    axes[2]=-axes[2]/10000
                    sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*axes[2]*v)/(s*wheelRadius))
                    sim.setJointTargetVelocity(rightJointDynamic,(linearVelocityRight*axes[2]*v)/(s*wheelRadius))        
                elseif(131<rotAxes[3]) then
                    sim.wait(2)
                    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))
                elseif(rotAxes[3]<-131) then
                    sim.wait(2)
                    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))            
                else
                    sim.setJointTargetVelocity(leftJointDynamic,0)
                    sim.setJointTargetVelocity(rightJointDynamic,0)    
                end
            end
But after running, my car couldn't move, and it didn't report wrong. What's wrong with it??
All of the code above are written in threaded script.

Post Reply