Problem with sim.getJointVelocity function

Typically: "How do I... ", "How can I... " questions
Post Reply
pme1976
Posts: 19
Joined: 07 Feb 2018, 18:06

Problem with sim.getJointVelocity function

Post by pme1976 »

Hello.

I created a simple car model in CoppeliaSim: a body and 4 wheels connected with the body by revolute joints. For the rear wheels, I set some target speed. After I start the model, the car accelerates. At the same time, in the child script, I control the speed of the rear wheels by sim.getJointVelocity function. I observe strange behavior: the car is driving, but the value returned by the function drops to zero. Has anyone encountered similar behavior?
coppelia
Site Admin
Posts: 10807
Joined: 14 Dec 2012, 00:25

Re: Problem with sim.getJointVelocity function

Post by coppelia »

Hello,

not sure what is happening exactly. What engine are you using? Did you try with the MuJoCo engine?
With all other engines, the joint velocity is measured, and if the joint is turning too fast, sampling points are too few. In that case, try to reduce the dynamics dt, in the simulation dialog.
With the MuJoCo engine on the other hand, the joint velocity is tracked by the physics engine directly, and should display correct values also at higher speeds.

Cheers
pme1976
Posts: 19
Joined: 07 Feb 2018, 18:06

Re: Problem with sim.getJointVelocity function

Post by pme1976 »

I use Bullet 2.78. It's very strange, but there is no problem with MoJoCo engine. Joint turning is about 50 deg/s and dt = 0.05 sec. Scene link: https://disk.yandex.ru/d/Dh1hTGrJcLt8Dw
pme1976
Posts: 19
Joined: 07 Feb 2018, 18:06

Re: Problem with sim.getJointVelocity function

Post by pme1976 »

Correction: with the MuJoCo engine, the speed drop to zero also occurs, but not at the 17th second, but much later (130th).
coppelia
Site Admin
Posts: 10807
Joined: 14 Dec 2012, 00:25

Re: Problem with sim.getJointVelocity function

Post by coppelia »

You are right, we introduced a bug in last CoppeliaSim release... (V4.4). You can use following function as a workaround:

Code: Select all

function sim.getJointVelocity(handle)
    local t=sim.getSimulationTime()
    local p=sim.getJointPosition(handle)
    if _velData==nil then
        _velData={}
    end
    if _velData[handle]==nil then
        _velData[handle]={lastPos=p,lastTime=t,lastVel=0}
    end
    if _velData[handle].lastTime~=t then
        local dp=p-_velData[handle].lastPos
        local cyclic=sim.getJointInterval(handle)
        if cyclic then
            local sin_da=math.sin(p)*math.cos(_velData[handle].lastPos)-math.cos(p)*math.sin(_velData[handle].lastPos)
            local cos_da=math.cos(p)*math.cos(_velData[handle].lastPos)+math.sin(p)*math.sin(_velData[handle].lastPos)
            dp=math.atan2(sin_da,cos_da)
        end
        _velData[handle].lastVel=dp/(t-_velData[handle].lastTime)
        _velData[handle].lastPos=p
        _velData[handle].lastTime=t
    end
    return _velData[handle].lastVel
end
Cheers
pme1976
Posts: 19
Joined: 07 Feb 2018, 18:06

Re: Problem with sim.getJointVelocity function

Post by pme1976 »

Thank you for your answer. It works.

My question: why did you use trigonometry formulas for angle difference? Why did not you write:
sin_da=math.sin(p - _velData[handle].lastPos)
coppelia
Site Admin
Posts: 10807
Joined: 14 Dec 2012, 00:25

Re: Problem with sim.getJointVelocity function

Post by coppelia »

with cyclic angles (i.e. angles that vary between -pi and +pi), you need to pick the smallest distance in one or the other direction.

Cheers
Post Reply