Try to get the position, displacement and force from the SOFA #4855
Replies: 3 comments 5 replies
-
For such need, Python is the answer. # Required import for python
import Sofa
import SofaRuntime
from scipy import sparse
from scipy import linalg
from matplotlib import pyplot as plt
import numpy as np
timeVector=[]
positionVector=[]
class PlotPositionController(Sofa.Core.Controller):
def __init__(self, *args, **kwargs):
Sofa.Core.Controller.__init__(self, *args, **kwargs)
self.mechanical_object = kwargs.get("mechanical_object")
def onAnimateEndEvent(self, event):
time = self.mechanical_object.getContext().time.value
timeVector.append(time)
pos = self.mechanical_object.position.value
positionVector.append(pos[318][2]) ## Replace this index 318 by the index of the tip point
plt.plot(timeVector,positionVector)
plt.show(block=False)
# Function called when the scene graph is being created
def createScene(root):
root.addObject('VisualStyle', displayFlags="showBehaviorModels showForceFields")
root.addObject("RequiredPlugin", pluginName=['Sofa.Component.Constraint.Projective',
'Sofa.Component.Engine.Select',
'Sofa.Component.LinearSolver.Direct',
'Sofa.Component.Mass',
'Sofa.Component.ODESolver.Backward',
'Sofa.Component.SolidMechanics.FEM.Elastic',
'Sofa.Component.StateContainer',
'Sofa.Component.Topology.Container.Grid',
'Sofa.Component.Visual'
])
root.addObject('DefaultAnimationLoop')
root.addObject('DefaultVisualManagerLoop')
root.addObject('EulerImplicitSolver', rayleighStiffness="0.1", rayleighMass="0.1")
root.addObject('SparseLDLSolver', template="CompressedRowSparseMatrixd")
MO = root.addObject('MechanicalObject', name="DoFs")
root.addObject('MeshMatrixMass', name="mass", totalMass="320")
root.addObject('RegularGridTopology', name="grid", nx="4", ny="4", nz="20", xmin="-9", xmax="-6", ymin="0", ymax="3", zmin="0", zmax="19")
root.addObject('BoxROI', name="box", box="-10 -1 -0.0001 -5 4 0.0001")
root.addObject('FixedProjectiveConstraint', indices="@box.indices")
root.addObject('HexahedronFEMForceField', name="FEM", youngModulus="4000", poissonRatio="0.3", method="large")
root.addObject(PlotPositionController('PlotPositionController', name='plotPosition', mechanical_object=MO))
return root |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answers! @hugtalbot
Controller:
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hi,
I am now trying to analysis the displacement of the gripper tip(the pneumatic gripper example in softrobot).
I can run the simulation, but how can I get the data saved in the MechanicalObject? I can see them in the GUI?
What I want to do is to plot the position of the gripper tip. Could you please help me?
I see the writestate function, but I think it export all the points, I just need one of the tip point.
Thanks a lot!
Beta Was this translation helpful? Give feedback.
All reactions