Moving an Object with Lua Code

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

Moving an Object with Lua Code

Post by Luis Nieto »

Hello! I would like to ask what is the proper syntax for the position value/s in the sim.setObjectPosition command. I would like to rectangularly translate my mesh part during a simulation which is why I am asking. Thanks in advance.

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

Re: Moving an Object with Lua Code

Post by fferri »

The function arguments are described here: sim.setObjectPosition
The position value is {x,y,z}

By the way, sim.setObjectPosition will jump to the set position. If you are looking for a smooth continuous motion, then use sim.moveToPose, e.g.:

Code: Select all

function coroutineMain()
    local waypoints={
        -- define a square. waypoints are {x,y,z,qx,qy,qz,qw}
        {0,0,0,0,0,0,1},
        {1,0,0,0,0,0,1},
        {1,1,0,0,0,0,1},
        {0,1,0,0,0,0,1},
    }
    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

function sysCall_init()
    corout=coroutine.create(coroutineMain)
    self=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

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

Re: Moving an Object with Lua Code

Post by Luis Nieto »

So this sample code would be able to move the object named 'self' through a square-shaped trajectory? Is that correct?

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

Re: Moving an Object with Lua Code

Post by fferri »

Yes.

See also the examples in the scenes/trajectoryAndMotion/ folder that comes with CoppeliaSim.

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

Re: Moving an Object with Lua Code

Post by Luis Nieto »

I would just like to clarify Euler angles:

Alpha is about x, beta is about y and gamma is about z? Is this correct?

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

Re: Moving an Object with Lua Code

Post by Luis Nieto »

Also, since you gave me a sample algorithm to do smooth movement of an object during a simulation,

how about doing smooth rotation during a simulation? Is it possible to execute that?

Thanks in advance.

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

Re: Moving an Object with Lua Code

Post by fferri »

Yes, in the same way. Simply use a different orientation (quaternion) than 0,0,0,1.

Euler angles are explained here: https://www.coppeliarobotics.com/helpFi ... Angles.htm

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

Re: Moving an Object with Lua Code

Post by Luis Nieto »

I wish you could also explain this waypoint arrays in the sample code please.

function coroutineMain()
local waypoints={
-- define a square. waypoints are {x,y,z,qx,qy,qz,qw}
{0,0,0,0,0,0,1},
{1,0,0,0,0,0,1},
{1,1,0,0,0,0,1},
{0,1,0,0,0,0,1},
}
for n=1,100 do
for i=1,#waypoints do
local from,to=waypoints,waypoints[i%4+1]
sim.moveToPose(-1,from,{2},{2},{0.01},to,function(currentPose,currentVel,currentAccel,auxData)
sim.setObjectPose(self,-1,currentPose)
end,nil,{1,1,1,0.1})
end
end
end

Like, how did this

{0,0,0,0,0,0,1}

make a trajectory for the moving object which was going exactly the +x direction?

Thanks

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

Re: Moving an Object with Lua Code

Post by coppelia »

The waypoints are defined as poses: position + orientation. The position are the (x, y, z) coordinates. The orientation is expressed as a quaternion (qx, qy, qz, qw).

See also sim.buildPose, sim.getObjectPose, sim.setObjectPose

Cheers

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

Re: Moving an Object with Lua Code

Post by Luis Nieto »

How do you manipulate the quaternion to get negative rotation?

For example, I know that for it to rotate about x the quarternion is {1,0,0,0}.

I thought maybe making it {-1,0,0,0} would make it rotate opposite when it was a one I cited above, but it did not.

Thanks

Post Reply