## Creating a spring

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

### Creating a spring

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
Posts: 7517
Joined: 14 Dec 2012, 00:25

### Re: Creating a spring

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

hi
I tried this methond , and it shows a very strange graph. 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.

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

### Re: Creating a spring

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()
cyhandle=sim.getObjectHandle('Cylinder')
cyhandle0=sim.getObjectHandle('Cylinder0')
initPosition=sim.getObjectPosition(cyhandle,cyhandle0)
k=100
tempPosition=sim.getObjectPosition(cyhandle,cyhandle0)
distance=tempPosition-initPosition
print(distance)
lastforce=distance*k 