Repository navigation
Expand file tree
/
Copy pathscene.py
More file actions
136 lines (110 loc) · 5.61 KB
/
Copy pathscene.py
File metadata and controls
136 lines (110 loc) · 5.61 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# pyright: reportMissingImports=false, reportAttributeAccessIssue=false
# pylint: disable=import-error
# EmioLabs libraries
from parts.controllers.assemblycontroller import AssemblyController
from utils.header import addHeader, addSolvers
# Sofa Python Libraries
from splib3.animation import AnimationManager, animate
# Scene Libraries
from optimization.parameters import low_leg, high_leg, weight, mode, center_part, movements
from sparts.sam import Sam
from animation import animation
import numpy as np
def createScene(rootnode):
_, modelling, simulation = addHeader(rootnode,
inverse = True,
friction = 0,
withCollision = True
)
rootnode.ConstraintSolver.epsilon = 1
rootnode.LocalMinDistance.setDataValues(
# alarmDistance = center_part.hThickness,
# contactDistance = center_part.hThickness/10
alarmDistance = 1,
contactDistance = 0.5
)
addSolvers(simulation)
rootnode.dt = movements.dt
rootnode.gravity = [0., -9810., 0.]
rootnode.VisualStyle.displayFlags.value = ["showVisual",
"showBehaviorModels",
"showForceFields",
"showWireframe",
'showCollisionModels',
'hideOptions']
rootnode.addObject(AnimationManager(rootnode))
#Leg generation
if (not low_leg.isValid() or not high_leg.isValid()):
rootnode.removeObject(AnimationManager(rootnode))
return
if (not low_leg.exportSTLCadQuery() or not high_leg.exportSTLCadQuery()):
rootnode.removeObject(AnimationManager(rootnode))
return
# Add SAM to the scene
sam = Sam(name = 'Sam',
legs = [low_leg, high_leg]*3,
centerpart = center_part,
mode = mode)
if not sam.isValid():
rootnode.removeObject(AnimationManager(rootnode))
return
simulation.addChild(sam)
sam.attachCenterPartToLegs()
assembly = AssemblyController(sam)
assembly.duration = 0.001
sam.addObject(assembly)
# Add effector
sam.effector.addObject("MechanicalObject",
template = "Rigid3",
position = [[0, center_part.height/2, 0, 0, 0, 0, 1],
[0, -center_part.height/2, 0, 0, 0, 0, 1]],
showObject = False)
sam.effector.addObject("RigidMapping",
index = 0)
# Target
effectorTarget = modelling.addChild('Target')
effectorTarget.addObject('EulerImplicitSolver',
firstOrder = True)
effectorTarget.addObject('CGLinearSolver',
iterations = 50,
tolerance = 1e-10,
threshold = 1e-10)
effectorTarget.addObject('MechanicalObject',
template = 'Rigid3',
position = [[0, high_leg.centerLine.endCoordinates[1]+center_part.hThickness, 0, 0, 0, 0, 1],
[0, low_leg.centerLine.endCoordinates[1]-center_part.lDepth, 0, 0, 0, 0, 1]],
showObject = True,
showObjectScale = 10)
#Sensor
sensor = sam.CenterPart.addChild('Sensor')
sensor.addObject('MechanicalObject',
position = [[0.0, center_part.height/2+20.0, 0.0]],
showObject = True,
showColor=[1,0,0,1],
showObjectScale = 5,
drawMode= 1)
sensor.addObject("UniformMass",
totalMass = weight)
sensor.addObject("RigidMapping",
index = 0)
if mode == 'trial':
sam.addInverseComponentAndGUI(targetMechaLink = effectorTarget.getMechanicalState().position.linkpath,
withGUI = True)
animate(onUpdate = animation,
params = {'target':effectorTarget.getMechanicalState().position,
'initialPosition':np.copy(effectorTarget.getMechanicalState().position.value),
'movements':movements,
'height':center_part.height/2
},
duration = movements.duration)
else:
sam.addInverseComponentAndGUI(targetMechaLink = effectorTarget.getMechanicalState().position.linkpath,
orientationWeight = 0)
sam.addConnectionComponents()
effectorTarget.MechanicalObject.position = [effectorTarget.MechanicalObject.position.value[0]]
sam.effector.MechanicalObject.position = [sam.effector.MechanicalObject.position.value[0]]
if 'EffectorCoord' in [object.name.value for object in sam.effector.objects]:
sam.effector.EffectorCoord.indices = [0]
if 'EffectorOrientation' in [object.name.value for object in sam.effector.objects]:
sam.effector.EffectorOrientation.indices = [0]
return rootnode