How can i convert speed to position with the api python

Typically: "How do I... ", "How can I... " questions
Post Reply
qwerty
Posts: 12
Joined: 01 Jun 2023, 13:29

How can i convert speed to position with the api python

Post by qwerty »

Hello,

How can i convert speed to position. I have a file with speed data on my computer and i want to make my object move. How can i do it ?

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

Re: How can i convert speed to position with the api python

Post by coppelia »

Hello,

what does the file look like? I mean what does speed data mean? Is it a list of speed value, associated with time values? Where your object would have infinite acceleration geoing from one speed value to the next?

Cheers

qwerty
Posts: 12
Joined: 01 Jun 2023, 13:29

Re: How can i convert speed to position with the api python

Post by qwerty »

First of all, thank you for your quick response.
The file is a . CSV generate after the measurement. In this file I have acceleration and speed according to x,y and z. and I also have time. So these data are associated.

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

Re: How can i convert speed to position with the api python

Post by coppelia »

You can try with something like that:

Code: Select all

speedData={{time=10,speed={0.1,0,0}},
{time=10.5,speed={0.2,0,0}},
{time=11,speed={0.01,0,0}},
{time=15,speed={0,0,0}},
{time=20,speed={-0.1,0,0}},
{time=40,speed={0,0,0}}
}

function sysCall_init()
    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)
    local dt=sim.getSimulationTimeStep()
    local i=1
    local h=sim.getObject('.')
    while i<=#speedData do
        local speed=speedData[i].speed
        print(speed)
        while sim.getSimulationTime()<speedData[i].time do
            local p=sim.getObjectPosition(h,sim.handle_world)
            p[1]=p[1]+speed[1]*dt
            p[2]=p[2]+speed[2]*dt
            p[3]=p[3]+speed[3]*dt
            sim.switchThread()
            sim.setObjectPosition(h,sim.handle_world,p)
        end
        i=i+1
    end
end
Make sure the object you want to move is not dynamically enabled, so that you can instantaneously set its position.

Cheers

Post Reply