-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstep1_insertion.py
117 lines (102 loc) · 5.49 KB
/
step1_insertion.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from splib3.numerics import Quat, vsub, vadd
import numpy as np
from scripts.Camera import createCamera
from scripts.NeedleProbe import createNeedleProbe, addProbe
from scripts.Robot import createRobot
from scripts.RobotGUI import RobotGUI
from scripts.Body import createBody, addCollisionToBody, addLesionToBody
from scripts.Header import addHeader
from importlib.machinery import SourceFileLoader
import os
params = SourceFileLoader('params', os.getcwd() + '/scripts/SceneParams.py').load_module()
# Simulation Options
ROBOT_CONTROL = True
INVERSE = True
# Precomputed position
if ROBOT_CONTROL:
robotBasePosition = params.robotBasePosition
needleMecaPosition = params.initSoftExtremityPosition
needleMecaOrientation = params.initSoftExtremityOrientation
needleMecaPosition = vadd(needleMecaPosition, robotBasePosition)
initAngles = params.initAngles
initEffectorPosition = params.initEffectorPosition + params.initEffectorOrientation
robotInitConfiguration = [initAngles, initEffectorPosition]
else:
needleMecaPosition = [0, -70, -220]
needleMecaOrientation = [-0.7, -0.1, -0.7, -0.1]
def createScene(rootNode):
addHeader(rootNode)
rootNode.addObject('RequiredPlugin', name='ExternalPlugins',
pluginName=['BeamAdapter', 'SoftRobots'])
if INVERSE:
rootNode.addObject('RequiredPlugin', name='SoftRobots.Inverse')
rootNode.addObject('FreeMotionAnimationLoop')
rootNode.addObject('QPInverseProblemSolver' if INVERSE else 'GenericConstraintSolver', printLog=0, tolerance=1e-8,
maxIterations=1000, epsilon=0.001)
rootNode.addObject('DefaultContactManager', response='FrictionContactConstraint', responseParams='mu=0.0')
rootNode.addObject('CollisionPipeline')
rootNode.addObject('BruteForceBroadPhase')
rootNode.addObject('BVHNarrowPhase')
rootNode.addObject('NewProximityIntersection', alarmDistance=3, contactDistance=0.1)
###############################
# Needle Probe
###############################
needleProbe = createNeedleProbe(rootNode, 'NeedleProbe',
withProbe=False if ROBOT_CONTROL else True,
withNeedle=True,
position=[0, 0, 0] + needleMecaOrientation)
###############################
# Needle & Probe Rest Shape
###############################
needleRestShape = rootNode.addChild('NeedleRestShape')
needleRestShape.addObject('MechanicalObject', name='dofs', template='Rigid3',
position=[list(np.copy(needleMecaPosition + needleMecaOrientation)) for i in range(3)],
showObject=False, showObjectScale=10, drawMode=1, showColor=[0, 255, 0, 255])
softPartExtremity = None
if ROBOT_CONTROL:
###################################################################
# Robot with User Interface
###################################################################
robot = createRobot(rootNode, translation=robotBasePosition, initConfiguration=robotInitConfiguration,
inverse=INVERSE, withEffector=False)
robot.Articulations.joint0.maxAngle = 0
robot.Articulations.joint0.minAngle = 0
softPartExtremity = robot.SoftExtremity
addProbe(softPartExtremity, 0)
if INVERSE:
robot.SoftExtremity.ProbeColli.addObject('PositionEffector', name='effector', template='Rigid3', indices=0,
effectorGoal=rootNode.EffectorTarget.dofs.findData(
'position').getLinkPath(),
useDirections=[1, 1, 1, 1, 1, 1])
q = Quat(-0.5, -0.5, -0.3, 0.3)
q.normalize()
rootNode.EffectorTarget.dofs.position = [[10.743, -29.625, 17] + list(q)]
for i in range(3):
needleProbe.addObject('RestShapeSpringsForceField', template='Rigid3', name='attach' + str(i), points=i,
external_points=0,
external_rest_shape=robot.SoftExtremity.dofs.getLinkPath(),
stiffness=1e6, angularStiffness=1e12)
needleProbe.EulerImplicitSolver.rayleighMass = 0.0
needleProbe.EulerImplicitSolver.rayleighStiffness = 0.0
if not INVERSE:
RobotGUI(robot)
else:
for i in range(3):
needleProbe.addObject('RestShapeSpringsForceField', template='Rigid3', name='attach' + str(i), points=i,
external_points=i, external_rest_shape=needleRestShape.dofs.getLinkPath(),
stiffness=1e6, angularStiffness=1e12,
drawSpring=False)
needleProbe.init() # BeamInterpolation acts weird when we translate the beam at init. TODO: find out why
needleProbe.dofs.translation = needleMecaPosition
###############################
# Camera
###############################
camera = createCamera(needleProbe, [210, 0, 0, 0, 0, 0, 1], 0)
###############################
# Body Mechanical
###############################
tissue = createBody(rootNode, params)
addCollisionToBody(tissue, params.prostateColliPath, 'ProstateCollision', params.rotation, params.translation)
for i in range(4):
addLesionToBody(tissue, params.lesionsPath[i], 'Lesion' + str(i), params.rotation, params.translation)
return rootNode