simAddForce confused

Typically: "How do I... ", "How can I... " questions
Post Reply
vrepUser1
Posts: 10
Joined: 22 Oct 2016, 16:20

simAddForce confused

Post by vrepUser1 » 20 Feb 2019, 23:00

Hello,
I'm a bit confused about simAddForce. In the description added forces are cumulative (within a timestep??) and the force will be reset to zero after one dynamic timestep by default or I have to set the handleflag? How it works when I want to add a force within each dynamic timestep in my c++ plugin?

Thank you!
Best,
vrepUser1

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

Re: simAddForce confused

Post by coppelia » 21 Feb 2019, 15:08

Hello,

if you use sim.addForce (or sim.addForceAndTorque) within a non-specific function, then forces are cumulative within a given time step. E.g. following compensates for gravity, once per simulation step:

Code: Select all

function sysCall_init()
    h=sim.getObjectAssociatedWithScript(sim.handle_self)
    m=sim.getShapeMassAndInertia(h)
end

function sysCall_actuation()
    local accel=sim.getArrayParameter(sim.arrayparam_gravity)
    sim.addForceAndTorque(h,{-accel[1]*m,-accel[2]*m,-accel[3]*m})
end
Above will set the force/torque for the given simulation step. By default, a simulation step contains 10 dynamics simulation steps. If you want to adjust the force/torque for each of the individual dynamics simulation steps, use a dynamics callback function. In this case, it is important to explicitely reset the accumulated force:

Code: Select all

function sysCall_init()
    h=sim.getObjectAssociatedWithScript(sim.handle_self)
    m=sim.getShapeMassAndInertia(h)
end

function sysCall_dynCallback(inData)
    local accel=sim.getArrayParameter(sim.arrayparam_gravity)
    sim.addForceAndTorque(h+sim.handleflag_resetforce+sim.handleflag_resettorque,{-accel[1]*m,-accel[2]*m,-accel[3]*m})
end

function sysCall_actuation()
end
Cheers

vrepUser1
Posts: 10
Joined: 22 Oct 2016, 16:20

Re: simAddForce confused

Post by vrepUser1 » 21 Feb 2019, 21:11

Thank you very much. It is much clearer now. One question arises now. I will call sim.addForce on a body B in my c++ plugin. How I reset the accumulated force within the c++ plugin?

Note: I'm using version 3.5.0 rev.4.

Best,
vrepuser1

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

Re: simAddForce confused

Post by coppelia » 22 Feb 2019, 08:18

if you want to do this from within a plugin, I would do it like:

Have your plugin register a custom Lua function simMyPlugin.dynCallback.
Then have a child script with following:

Code: Select all

function sysCall_dynCallback(inData)
    ...
    local force,torque=simMyPlugin.dynCallback(inData,additionalData)
    sim.addForceAndTorque(h+sim.handleflag_resetforce+sim.handleflag_resettorque,force,torque)
end
Basically your child script will call a function in your plugin to get the desired force/torque, and apply it in the child script.
Also, make sure to upgrade to V3.6.0 rev1

Cheers

vrepUser1
Posts: 10
Joined: 22 Oct 2016, 16:20

Re: simAddForce confused

Post by vrepUser1 » 22 Feb 2019, 18:40

I tried the way like you suggested, but its a lot of code I would generate in dynCallback. Anyway its working for this:

Code: Select all

function sysCall_dynCallback(inData)
    ....
    sim.addForce(h[i], tb, fb)
end
But as far I try to reset the accumulated force with only the resetforce flag or together with both flags:

Code: Select all

function sysCall_dynCallback(inData)
    ....
    sim.addForce(h[i]+sim.handleflag_resetforce+sim.handleflag_resettorque, tb, fb)
end
I receive an error that the object doesnt exist.
EDIT: Ok updated to 3.6.0. Now this works.


Is not possible to do it directly in plugin? If I would do it like you suggest, I had to maintain a lot of entities, which are inside the plugin and not the child script itself. This is the way I really want to do it.

Best,
vrepuser1

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

Re: simAddForce confused

Post by coppelia » 01 Mar 2019, 14:19

Unfortunately you can't do that from the plugin directly. But by generating a callback from sysCall_dynCallback to the plugin, you can still do most of the things in the plugin. Even simAddForce can be done in the plugin.
All you are doing is redirecting the callback to the plugin.

Cheers

Post Reply