Creating a spring

Typically: "How do I... ", "How can I... " questions
Post Reply
smilamc
Posts: 3
Joined: 12 Feb 2014, 22:57

Creating a spring

Post by smilamc »

I have read your posts about using a prismatic joint to create a spring damper system and I am still unclear about how to do that, could you walk me through how you would use a prismatic joint to replicate a 12N/m spring with a rest length of .02 meters.

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

Re: Creating a spring

Post by coppelia »

Hello,

For a spring, you have Hook's law:

F=k*x

where F is the force of a spring, k its constant, and x its displacement (or stretch/compression)

In V-REP, set your joint (let's suppose it's a prismatic joint) into torque/force mode, the motor enabled, the Control loop enabled, Position control (PID) selected, and Regulate torque/force selected. You can do all this in the joint dynamics properties.

In V-REP, with above settings, your joint will exert a force:

F=ctrl*maxForce

where maxforce is the max. torque/force item, and ctrl is a PID control:

ctrl=x*P+integratedErrors*I+(x-previous_x)*D

So, in case of a pure spring:

F=x*P*maxForce

so, k=P*maxForce. In your case, you could pick: maxForce=12 and P=1.

Cheers

s7405570
Posts: 4
Joined: 29 Apr 2018, 12:42

Re: Creating a spring

Post by s7405570 »

hi
I tried this methond , and it shows a very strange graph.
Image
The first strange thing is that the fig is supposed to plot a sin wave.
In the mean time, the amplitude descend fast along with time. The descend is bigger when simulation step time is bigger.
I set step time to 0.1ms, and get this fig. The descend is still unacceptable.
Last edited by fferri on 09 May 2018, 20:30, edited 1 time in total.
Reason: changed your image links

s7405570
Posts: 4
Joined: 29 Apr 2018, 12:42

Re: Creating a spring

Post by s7405570 »

I've find another way slove this problem.

First disable the prismatic joint motor , we don't need the motor contorl.

Then create a threaded script with the following code:

Code: Select all

function sysCall_threadmain()
sim.setThreadAutomaticSwitch(false)
cyhandle=sim.getObjectHandle('Cylinder')
cyhandle0=sim.getObjectHandle('Cylinder0')
initPosition=sim.getObjectPosition(cyhandle,cyhandle0)
k=100
 while sim.getSimulationState()~=sim.simulation_advancing_abouttostop do
  tempPosition=sim.getObjectPosition(cyhandle,cyhandle0)
  distance=tempPosition[3]-initPosition[3]
  print(distance)
  lastforce=distance*k
  sim.addForceAndTorque(cyhandle0,{0,0,distance*k},{0,0,0})
  sim.switchThread() -- resume in next simulation step
 end
end
As you can see in the pic, the fig is real sin wave and the descend is very small
cheers!

This script serves as a real spring between the two objects. Here is the result:
Image
Last edited by fferri on 09 May 2018, 20:31, edited 2 times in total.
Reason: changed your image links & format your code

Post Reply