Moving an Object with Lua Code

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

Re: Moving an Object with Lua Code

Post by Luis Nieto »

Furthermore, I would like to just simply implement sim.movetoPose on its own - I wonder if it is possible. I would just like to initialize two poses like so

Code: Select all

pose1 = {x,y,x,qx,qy,qz,qw}
pose2 = {x,y,x,qx,qy,qz,qw}

 and implement smooth controlled motion simply just using sim.movetoPose without the need of the loop in the sample algorithm you sent me before as seen above...

 for n=1,100 do
        for i=1,#waypoints do
            local from,to=waypoints[i],waypoints[i%4+1]
            sim.moveToPose(-1,from,{0.8},{0.02},{0.01},to,function(currentPose,currentVel,currentAccel,auxData)
                sim.setObjectPose(self,-1,currentPose)
            end,nil,{1,1,1,0.1})
        end
    end
end
From examining how this works with the help of the API reference on the sim.movetoPose command, it seems like a lot of this needs to be changed...

Code: Select all

 sim.moveToPose(-1,from,{0.8},{0.02},{0.01},to,function(currentPose,currentVel,currentAccel,auxData)
                sim.setObjectPose(self,-1,currentPose)
            end,nil,{1,1,1,0.1})
How can I change this code in order to implement the command without the for-loop if it is possible?

Thanks In Advance.

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

Re: Moving an Object with Lua Code

Post by coppelia »

If you want to move from an intial pose to a target pose (e.g. from pose1 to pose2) via a smooth movement profile, then have a look at the demo scene scenes/trajectoryAndMotion/ruckigOnlineTrajectoryGeneratorExamples.ttt and inspect the code attached to the red cylinder.

Cheers

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

Re: Moving an Object with Lua Code

Post by Luis Nieto »

What is wrong with my sim.moveToPose code? The result is the object snapping right to the final pose instead of moving at the velocity I established.

Code: Select all

function coroutineMain()
    start = thingpose
    finish = thingpose
    finish[2] = thingpose[2] + 0.5
    sim.moveToPose(-1, start, {0.1,5,0.1,0.1}, {0.1,2,0.1,0.1}, {0.1,1,0.1,0.1}, finish, callback, thing, nil, 0)
end

function callback(poseofthing, v, a, Thing)
    sim.setObjectPose(Thing, -1, poseofthing)
end
For reference, 'thingpose' is the handle I gave the object that I want to move.

Thanks in advance.

fferri
Posts: 1193
Joined: 09 Sep 2013, 19:28

Re: Moving an Object with Lua Code

Post by fferri »

Luis Nieto wrote: 06 Jun 2022, 09:13

Code: Select all

    start = thingpose
    finish = thingpose
    finish[2] = thingpose[2] + 0.5
You can't do that in Lua. It doesn't do what you want in this case.

To understand why, try to print the values of start and finish.

Additional reading: https://stackoverflow.com/questions/53990332

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

Re: Moving an Object with Lua Code

Post by Luis Nieto »

Hello!

Alright, I'll just get straight to the point.

The code that you sent me with the moving the object in a square-shaped trajectory has helped me a lot thus far, however it has something extra that is giving me problems which are preventing me from achieving major progress for my project.

When the all the indicated endpoints for the sim.moveToPose loop are covered by motion, the object goes back to the initial pose. I wish that there was a way the object could just stay and stop at the pose indicated by the last row of the trajectories table.

This problem is demonstrated in the video link attached as a
cuboid shown just for example of how the code works.

https://drive.google.com/file/d/1NTi3MX ... sp=sharing

Here is also the modified version of the code I used:

Code: Select all

function sysCall_init()
    corout=coroutine.create(coroutineMain)
    cube = sim.getObject('.')
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 sysCall_cleanup()
    -- do some clean-up here
end

function coroutineMain()
    -- Put some initialization code here
    vel = 40
    accel = 40
    jerk = 10
    cubepse = sim.getObjectPose(cube, -1)
    
    trajrow1 = cubepse
    trajrow2 = sim.copyTable(trajrow1)
        trajrow2[2] = trajrow2[2]+0.05
        trajrow2[3] = trajrow2[3]+0.02
    trajrow3 = sim.copyTable(trajrow1)
        trajrow3[2] = trajrow3[2]+0.1
    trajrow4 = sim.copyTable(trajrow1)

local trajectories={
        -- define a square. waypoints are {x,y,z,qx,qy,qz,qw}
        trajrow1,
        trajrow2,
        trajrow3}
    
        for i=1,#trajectories do
            local from,to=trajectories[i],trajectories[i%3+1]
            sim.moveToPose(-1,from,{vel},{accel},{jerk},to,function(currentPose,currentVel,currentAccel,auxData)
                sim.setObjectPose(cube,-1,currentPose)
            end,nil,{1,1,1,0.1})
        end


end
Thanks in advance. I need it now more that before.

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

Re: Moving an Object with Lua Code

Post by coppelia »

Then simply reduce the loop count by 1?
i.e., instead of:

Code: Select all

for i=1,#trajectories do
do

Code: Select all

for i=1,#trajectories-1 do
Cheers

Post Reply