How to build magnetic link between two magnets?

Typically: "How do I... ", "How can I... " questions
JamesHu
Posts: 5
Joined: 06 Mar 2018, 08:27

How to build magnetic link between two magnets?

Post by JamesHu » 06 Mar 2018, 10:06

Greetings,

There are two magnets in my scene. One is the cylinder which can be moved and rotated freely, and the other is the capsule which is actuated by the magnetic force and torque. In the script attached to the capsule, magnetic force and torque applied to capsule is calculated and simAddForceAndTorque(cap_handle,fm,taum) is used to apply magnetic force and torque to capsule.

But when simulation timestep is set greater than 1ms (like 50ms), the capsule moves and rotates rapidly and becomes unstable. When simulation timestep is set 1ms or less, capsule can move and ratate stably, but simulation is very slow.

So I wonder if there is any way to solve the problem.

Thanks a lot.

https://pan.baidu.com/s/1fwLEtRHmlO3Q49ga1E5Ltg

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

Re: How to build magnetic link between two magnets?

Post by coppelia » 07 Mar 2018, 10:57

Hello,

your problem can currently only be solved by trying to adjust the algorithm that adds the external force. Maybe.
In any case, it does not make sense to reduce the simulation time step to 1ms, since all other calculations (e.g. proximity sensors, vision sensors, etc.) will also be handled every 1ms, and drastically slows doen the simulation. Best is to keep the default simulation time step of 50ms, and set the dynamics time step to 1 ms. Then, ideally, you should run code in each dynamic simulation pass. This can currently only be done inside of a joint control callback script, and you would still run into the problem of constantly needing to adjust the external force (sim.addForceAndTorque will cumulate all forces, and clear them after the physics engine executed its steps), which is currently not feasable.

However, in V-REP3.5.0, rev3+, planned for sometime next week, we have added a dynamics callback function and slightly adjusted the way how sim.addForceAndTorque operates. Then, you will be able to do following:

Code: Select all

function sysCall_dynCallback(inData)
    -- This function gets called often, so it might slow down the simulation
    --     (this is called twice at each dynamic simulation step, by default 20x more often than a child script)
    -- We have:
    -- inData.passCnt : the current dynamics calculation pass. 1-10 by default. See next item for details.
    -- inData.totalPasses : the number of dynamics calculation passes for each "regular" simulation pass.
    --                      10 by default (i.e. 10*5ms=50ms which is the default simulation time step)
    -- inData.dynStepSize : the step size used for the dynamics calculations (by default 5ms)
    -- inData.afterStep : <strong>false</strong> when called before, and <strong>true</strong> after a dynamics step was computed.

    local txt=string.format(" the %ith dynamics calculation step (out of %i steps)",inData.passCnt,inData.totalPasses)
    if inData.afterStep then
        txt="After"..txt
    else
        txt="Before"..txt
        sim.addForceAndTorque(shapeHandle+sim.handleflag_resetforce,force) -- set the external force for this dyn. sim. step
    end
    print(txt)
end
Cheers

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

Re: How to build magnetic link between two magnets?

Post by coppelia » 07 Mar 2018, 16:19

V-REP V3.5.0.rev3 is now available for download.

Cheers

JamesHu
Posts: 5
Joined: 06 Mar 2018, 08:27

Re: How to build magnetic link between two magnets?

Post by JamesHu » 08 Mar 2018, 03:04

Greetings,

As you said, it makes much more sense to keep the default simulation time step of 50ms, and set the dynamics time step to 1 ms. I am looking forward to V-REP3.5.0, rev3+ and try dynamics callback function.

Thanks a lot.

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

Re: How to build magnetic link between two magnets?

Post by coppelia » 08 Mar 2018, 08:48

The above mentioned changes are included in V-REP V3.5.0.rev3, now available (released faster than expected... for once ;) ).

Cheers

JamesHu
Posts: 5
Joined: 06 Mar 2018, 08:27

Re: How to build magnetic link between two magnets?

Post by JamesHu » 08 Mar 2018, 10:13

Greetings,

Thanks for your reply. I have got general ideas how to do that.

I still wonders if there is any way to change step size for the dynamics.

sim.setEngineFloatParameter(sim.bullet_global_stepsize, -1, 0.001)--set step size for the dynamics 1ms
sim.resetDynamicObject(sim_handle_all)

Will above code do that?

Cheers

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

Re: How to build magnetic link between two magnets?

Post by coppelia » 08 Mar 2018, 12:51

You can manually change the dynamics step size here.

If you programmatically want to change that step size, make sure that:
  • You have selected Configuration: Customized
  • That simulation is not running (you will have to use a customization script for that).
Cheers

JamesHu
Posts: 5
Joined: 06 Mar 2018, 08:27

Re: How to build magnetic link between two magnets?

Post by JamesHu » 09 Mar 2018, 03:06

Greetings,

picture:
https://pan.baidu.com/s/1fwLEtRHmlO3Q49ga1E5Ltg

files:
https://pan.baidu.com/s/12ftStwDump-35ep_IIWQTw

I have tried dynamics callback function as follows and I made sure the function was correctly called. But the capsule still moves and rotates rapidly and becomes unstable when using default simulation timestep(50ms).

Code: Select all

function sysCall_dynCallback(inData)
    -- This function gets called often, so it might slow down the simulation
    --     (this is called twice at each dynamic simulation step, by default 20x more often than a child script)
    -- We have:
    -- inData.passCnt : the current dynamics calculation pass. 1-10 by default. See next item for details.
    -- inData.totalPasses : the number of dynamics calculation passes for each "regular" simulation pass.
    --                      10 by default (i.e. 10*5ms=50ms which is the default simulation time step)
    -- inData.dynStepSize : the step size used for the dynamics calculations (by default 5ms)
    -- inData.afterStep : <strong>false</strong> when called before, and <strong>true</strong> after a dynamics step was computed.
    local matrix = require "matrix"
    --local txt=string.format(" the %ith dynamics calculation step (out of %i steps)",inData.passCnt,inData.totalPasses)
    if inData.afterStep then
        --txt="After"..txt
    else
        --txt="Before"..txt
        local epm_matrix=sim.getObjectMatrix(epm_handle,-1)--retrieve cylinder magnet transformation matrix
        local cap_matrix=sim.getObjectMatrix(cap_handle,-1)--retrieve capsule magnet transformation matrix
        local p=matrix{cap_matrix[4]-epm_matrix[4],cap_matrix[8]-epm_matrix[8],cap_matrix[12]-epm_matrix[12]}
        local u0=4*math.pi/10000000
        local ma=matrix{epm_matrix[3],epm_matrix[7],epm_matrix[11]}
        ma=ma*math.pi*65^2*120/1000000000*1.45/u0
        local mc=matrix{cap_matrix[3],cap_matrix[7],cap_matrix[11]}
        mc=mc*math.pi*1.29^2*16/1000000000*1.45/u0
        eB_v=u0*(3*p*ma^'T'*p/(p^'T'*p)-ma)/(4*math.pi*((p^'T'*p):sqrt())^3)
        fm_v=3*u0*(ma*mc^'T'*p+mc*ma^'T'*p+p*(ma^'T'*mc-5*(mc^'T'*p)*(ma^'T'*p)/(p^'T'*p)))/(4*math.pi*((p^'T'*p):sqrt())^5)
        taum_v=mc:cross(eB_v)
        local fm = {}
        local taum = {}
        --print(tostring(taum_v))
        --cap_linearVelocity,cap_angularVelocity=sim.getObjectVelocity(cap_handle)
        for i=1,3 do
            table.insert( fm, fm_v[i][1] )
            table.insert( taum, taum_v[i][1] )
            --taum[i]=taum[i]-0.02*cap_angularVelocity[i]
        end
        fm[3]=fm[3]+(0.0027-0.00015)*9.81
        sim.addForceAndTorque(cap_handle+sim.handleflag_resetforce,fm,taum)-- set the external force for this dyn. sim. step
    end
    --print(txt)
end
Cheers

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

Re: How to build magnetic link between two magnets?

Post by coppelia » 12 Mar 2018, 07:27

Did you also set the dynamics time step to 1ms? (while keeping the simulation time step at 50ms)

Cheers

JamesHu
Posts: 5
Joined: 06 Mar 2018, 08:27

Re: How to build magnetic link between two magnets?

Post by JamesHu » 12 Mar 2018, 12:35

I tried different sets of dynamics and simulation timesteps. Dynamics timestep 1ms and simulation timestep 3ms, also dynamics timestep 5ms and simulation timestep 10ms worked. Other sets inculding dynamics timestep 1ms and simulation timestep 50ms didn't work.

I also tried all physical engines, bullet 2.78 and vortex worked with above settings. Others didn't work no matter what settings.

Cheers

Post Reply