Inquiry for follow-path code

Typically: "How do I... ", "How can I... " questions
Luis Nieto
Posts: 49
Joined: 26 Oct 2021, 01:35

Inquiry for follow-path code

Post by Luis Nieto »

I would like to ask what is wrong with the set of code below which I wrote. It is intended to make the ik-group, therefore the entire leg, follow the path I made NOT at press-play of simulation but at 4 SECS. If the code should be different or more complicated than this, I'd like to know.

Code:

Code: Select all

function sysCall_threadmain()
    rktarg = sim.getObjectHandle('rightkneetarget')
    rkpath = sim.getObjectHandle('rightkneepath')

    ...

    if sim.getSimulationTime() > 4 then 
        sim.followPath(rktarg, rkpath, 1, 0, 0.035, 0)
    
    end
end
Thanks in Advance.

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

Re: Inquiry for follow-path code

Post by coppelia »

Hello,

sim.followPath is deprecated and only works with the old-type path objects. That function is still there for backward compatibility reasons, but old-type paths are not available directly anymore since CoppeliaSim V4.2
Use the new paths object, and use your own path following function. As an example, open the demo scene scenes/movingAlongAPath.ttt, and replace the code of the blue cube with following:

Code: Select all

function sysCall_init()
    cube=sim.getObject('.')
    path=sim.getObject('/Path')
    pathData=sim.unpackDoubleTable(sim.readCustomDataBlock(path,'PATH'))
    local m=Matrix(#pathData//7,7,pathData)
    pathPositions=m:slice(1,1,m:rows(),3):data()
    pathQuaternions=m:slice(1,4,m:rows(),7):data()
    pathLengths,totalLength=sim.getPathLengths(pathPositions,3)
    velocity=0.04 -- m/s
    
    corout=coroutine.create(coroutineMain)
end

function sysCall_actuation()
    if coroutine.status(corout)~='dead' then
        local ok,errorMsg=coroutine.resume(corout)
        if errorMsg then
            error(debug.traceback(corout,errorMsg),2)
        end
    end
end

function coroutineMain()
    sim.setThreadAutomaticSwitch(false)
    followPath(0,totalLength*0.25,velocity,0.01,99)
    followPath(totalLength*0.25,-totalLength*0.25,velocity*2,0.01,99)
    followPath(-totalLength*0.25,2*totalLength,velocity*4,0.01,99)
end

function callback(config,vel,accel)
    local p=config[1]
    p=p%totalLength
    local pos=sim.getPathInterpolatedConfig(pathPositions,pathLengths,p)
    local quat=sim.getPathInterpolatedConfig(pathQuaternions,pathLengths,p,nil,{2,2,2,2})
    sim.setObjectPosition(cube,path,pos)
    sim.setObjectQuaternion(cube,path,quat)
end

function followPath(startPos,endPos,vel,accel,jerk)
    sim.moveToConfig(-1,{startPos},{0},{0},{vel},{accel},{jerk},{endPos},{0},callback)
end
also about your code: it would do nothing anyway, since the condition sim.getSimulationTime()>4 won't be met, and your thread will immediately end. Instead it should be something like:

Code: Select all

function coroutineMain()
    sim.setThreadAutomaticSwitch(false)
    while sim.getSimulationTime()<4 do
        sim.switchThread()
    end
    
    follow path here

end
Make sure to use CoppeliaSim V4.3, or at least V4.2! (but CoppeliaSim V4.3 just came out and introduced many new features and bug fixes)

Cheers

Luis Nieto
Posts: 49
Joined: 26 Oct 2021, 01:35

Re: Inquiry for follow-path code

Post by Luis Nieto »

May I kindly ask what does matrixHandle:slice() do exactly? Like in the blueCube code "m:slice(1,1,m:rows(),3)". Thank you.

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

Re: Inquiry for follow-path code

Post by coppelia »


Luis Nieto
Posts: 49
Joined: 26 Oct 2021, 01:35

Re: Inquiry for follow-path code

Post by Luis Nieto »

So recently, I've updated my Coppeliasim and it's now the V4.3 version and the movingAlongAPath.ttt sample scene that you brought me to looks like it should with everything new that the older version did not have.

For the IK-related dummy in my personal scene to follow a path I made for it, I decided to pattern my code after the one of the red cube in the movingAlongAPath.ttt scene. My version with personalized handles but same commands is as follows:

Code: Select all

function sysCall_init()
    -- do some initialization here
    leftlegtarg=sim.getObject('.')
    leftpath=sim.getObject('/Path')
    leftpathprops=sim.unpackDoubleTable(sim.readCustomDataBlock(leftpath,'PATH'))--,0,0,0)
    local mtx=Matrix(#leftpathprops//7,7,leftpathprops)
    leftlegtargpos=mtx:slice(1,1,mtx:rows(),3):data()
    llpathlength,llpathtotal=sim.getPathLengths(leftlegtargpos,3)
    vel=0.05 -- m/s
    leftpathpos=0
    t0=0
end

function sysCall_actuation()
    -- put your actuation code here
    local t=sim.getSimulationTime()
    leftpathpos=leftpathpos+vel*(t-t0)
    leftpathpos=leftpathpos % llpathtotal
    local leftlegpos=sim.getPathInterpolatedConfig(leftlegtargpos,llpathlength,leftpathpos)
    sim.setObjectPosition(leftlegtarg,leftpath,leftpathpos)
    t0=t
end
My problem is that I have an error that says that the sim.setObjectPosition() command,

Code: Select all

    sim.setObjectPosition(leftlegtarg,leftpath,leftpathpos)
has one of its argument types as not correct. I would like any advice soon on what I've done wrong in this code to get that error notice. Pretty please and much much thanks.

Warmest regards!

Luis Nieto
Posts: 49
Joined: 26 Oct 2021, 01:35

Re: Inquiry for follow-path code

Post by Luis Nieto »

Hello! I would appreciate a just a very quick answer on my last inquiry please. I would really respectfully appreciate.

I would just like to know when that one line of code with the sim.setObjectPosition(). It said that my argument type was wrong.

Again thanks in advance.

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

Re: Inquiry for follow-path code

Post by coppelia »

did you try to simply print the arguments before calling the command? What does it say?

Luis Nieto
Posts: 49
Joined: 26 Oct 2021, 01:35

Re: Inquiry for follow-path code

Post by Luis Nieto »

No I have not tried printing the arguments first before calling it. I would like to know how to do that if it will fix my issue.

So what happens when I run my code with this in the actuation function:

Code: Select all

function sysCall_actuation()
    -- put your actuation code here
    local t=sim.getSimulationTime()
    leftpathpos=leftpathpos+vel*(t-t0)
    leftpathpos=leftpathpos % llpathtotal
    local leftlegpos=sim.getPathInterpolatedConfig(leftlegtargpos,llpathlength,leftpathpos)
    sim.setObjectPosition(leftlegtarg,leftpath,leftpathpos)
    t0=t
end
similar to the code in the movingAlongAPath.ttt scene, I get an error message saying:

" 20: in sim.setObjectPosition: one of the function's argument type is not correct.
stack traceback:
[C]: in function 'simSetObjectPosition'
[string "/pelvis_respondable/leftfoottarget@childScrip..."]:20: in function 'sysCall_actuation'
"

What is wrong with my sim.setObjectPosition?

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

Re: Inquiry for follow-path code

Post by coppelia »

print the arguments!!! Then you might find out what is wrong:

Code: Select all

...
local leftlegpos=sim.getPathInterpolatedConfig(leftlegtargpos,llpathlength,leftpathpos)
print(leftlegtarg,leftpath,leftpathpos)
sim.setObjectPosition(leftlegtarg,leftpath,leftpathpos)
...
it will probably print nil for the first argument, since it appears not defined.

Luis Nieto
Posts: 49
Joined: 26 Oct 2021, 01:35

Re: Inquiry for follow-path code

Post by Luis Nieto »

Hello. First of all, I was able to fix my problem of the scene not responding to the code. It turns out I put the wrong handle in one of the arguments. Thank for helping with that.

Next I would like to clarify a little more about the followPath command, this one being the example you gave me:

followPath(0,totalLength*0.25,velocity,0.01,99)

I would just like to have a brief clarification to what each of the arguments means please. Thank you.

Post Reply