-
Notifications
You must be signed in to change notification settings - Fork 4
/
launch_sofa.py
96 lines (76 loc) · 3.79 KB
/
launch_sofa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
from datetime import datetime
import Sofa.Core
import Sofa.Simulation
import SofaRuntime
import numpy as np
"""
Provides direct interface with SOFA and is either opened from sofa gui or run in the following command style:
$SOFA_BLD/bin/runSofa -l $SP3_BLD/lib/libSofaPython3.so $REPO_ROOT/launch_sofa.py
Imports problem_specification to add specified robot (FEM model) and specific controller.
"""
def createScene(rootNode):
# Start building scene
rootNode.addObject("RequiredPlugin", name="SoftRobots")
rootNode.addObject("RequiredPlugin", name="SofaSparseSolver")
rootNode.addObject("RequiredPlugin", name="SofaPreconditioner")
rootNode.addObject('RequiredPlugin', pluginName='SofaOpenglVisual')
rootNode.addObject("VisualStyle", displayFlags="showBehavior")
rootNode.addObject('RequiredPlugin', pluginName='SofaMatrix')
rootNode.addObject("FreeMotionAnimationLoop")
# rootNode.addObject('DefaultAnimationLoop', name='loop')
rootNode.addObject("GenericConstraintSolver", maxIterations=100, tolerance=1e-5)
rootNode.addObject('BackgroundSetting', color='0 0.168627 0.211765')
rootNode.addObject('OglSceneFrame', style="Arrows", alignment="TopRight")
# Define the specific instance via the problem_specification script
import problem_specification
#input1 = np.array([1400, 1400, 0, 0])
#input2 = np.array([1400, 1400, 1400, 1400])
#prob = problem_specification.problem(input=input2)
prob = problem_specification.problem()
prob.checkDefinition()
# Set the gravity and simulation time step
rootNode.gravity = prob.Robot.gravity
rootNode.dt = prob.Robot.dt
# Define filename prefix for saving
if prob.opt.get('save_prefix') is None:
prob.opt['save_prefix'] = datetime.now().strftime("%Y%m%d_%H%M")
robot = rootNode.addChild(prob.Robot.robot)
if robot.min_force > [0] * len(robot.actuator_list):
print('[PROBLEM WARNING] Minimal force for 1 or more actuators set to {}, which is higher than 0. '
'Undesired behavior might occur'.format(max(robot.min_force)))
rootNode.addObject(prob.ControllerClass(rootNode=rootNode,
robot=robot,
snapshots=prob.snapshots,
controller=prob.controller,
measurement_model=prob.measurement_model,
output_model=prob.output_model,
simdata_dir=prob.simdata_dir,
snapshots_dir=prob.snapshots_dir,
opt=prob.opt)
)
rootNode.autopaused = False # Enables terminating simulation at the end when running from command line
return rootNode
def main():
# Allows executing from terminal directly
# Requires adjusting to own path
# sofa_lib_path = "/home/jjalora/sofa/build/lib"
sofa_lib_path = "/home/jalora/sofa/build/lib"
if not os.path.exists(sofa_lib_path):
raise RuntimeError('Path non-existent, sofa_lib_path should be modified to point to local SOFA installation'
'in main() in launch_sofa.py')
SofaRuntime.PluginRepository.addFirstPath(sofa_lib_path)
SofaRuntime.importPlugin("SofaComponentAll")
SofaRuntime.importPlugin("SofaOpenglVisual")
global _runAsPythonScript
_runAsPythonScript = True
root = Sofa.Core.Node()
rootNode = createScene(root)
Sofa.Simulation.init(root)
while True:
Sofa.Simulation.animate(root, root.dt.value)
if rootNode.autopaused == True:
print('Simulation finished, exiting...')
break
if __name__ == '__main__':
main()