Repository navigation
How to integrate SOFA into a feedback control loop? #3052
|
Dear all, I'm trying to use SOFA in a feed back control loop that includes using the 3-D mesh reconstructed with depth images as an input and comparing the simulated deformation with the visual captured deformation then updating the parameters (mainly young's modulus and Poisson's ratio). The control algorithm is mainly written in Python so my question is
Victor |
Replies: 1 comment 1 reply
|
Hi @VictorKingbot, Since the plugin SofaPython3 it is possibly to integrate sofa simulation into any python3 program, there is no more the requirement to use runSofa at all. This is doable either you use a binary version or one you compiled yourself from the source code. # Required import for python
import Sofa.Core
import Sofa.Simulation
import SofaRuntime
# Function creating the scene and describing the graph (with nodes and components) before it is initialized
# does nothing here... you need to add your own scene.
def createScene(root):
pass
#Main function ONLY called if this script is called from a python environment
def main():
# Make sure to load all SOFA libraries and plugins
SofaRuntime.importPlugin("SofaBaseMechanics")
# Generate the root node
root = Sofa.Core.Node("root")
# Call the above function to create the scene graph
createScene(root)
# Once defined, initialization of the scene graph
Sofa.Simulation.init(root)
# Run the simulation for 10 steps
for iteration in range(10):
Sofa.Simulation.animate(root, root.dt.value)
# Function used only if this script is called from a python environment, triggers the main()
if __name__ == '__main__':
main()To make this exemple work with python interpreter you need to set the following environement variables: SOFA_ROOT=/path/to/SOFA_install
PYTHONPATH=/path/to/SofaPython3/lib/python3/site-packages:$PYTHONPATH
A simple example inspired from SofaPython3/examples/emptyController.py import Sofa
class MyController(Sofa.Core.Controller):
def __init__(self, *args, **kwargs):
# These are needed (and the normal way to override from a python class)
Sofa.Core.Controller.__init__(self, *args, **kwargs)
self.fem = kwargs.get("fem")
self.model = kwargs.get("model")
def onAnimateEndEvent(self, event): # called at each end of animation step
print("Positions", self.model.position.value)
print("youngModulus {0}, poissonRatio {1}".format(
self.fem.youngModulus.value,
self.fem.poissonRatio.value))
def createScene(root):
root.dt = 0.01
root.addObject('DefaultVisualManagerLoop')
root.addObject('DefaultAnimationLoop')
# Add our python controller in the scene
root.addObject("MeshGmshLoader", name="loader", filename="mesh/liver.msh")
root.addObject("TetrahedronSetTopologyContainer", src=root.loader.getLinkPath())
root.addObject("MechanicalObject", name="mo")
root.addObject("TetrahedronFEMForceField", name="fem",
poissonRatio=0.45,
youngModulus=100000)
root.addObject( MyController(name="Hello", fem=root.fem, model=root.mo) )Using a controller is very interest to access the data at specific simulation event but when working in a python3 interpreter it is way simpler to access the data out of the simulation step like int: # Generate the root node
root = Sofa.Core.Node("root")
# Call the above function to create the scene graph
createScene(root)
# Once defined, initialization of the scene graph
Sofa.Simulation.init(root)
# Run the simulation for 10 steps
for iteration in range(10):
Sofa.Simulation.animate(root, root.dt.value)
print("youngModulus {0}, poissonRatio {1}".format(
self.fem.youngModulus.value,
self.fem.poissonRatio.value)) |
Hi @VictorKingbot,
Since the plugin SofaPython3 it is possibly to integrate sofa simulation into any python3 program, there is no more the requirement to use runSofa at all. This is doable either you use a binary version or one you compiled yourself from the source code.
A quick example which can be either executed into runSofa or in python3 interpreter: