Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions emio/parts/centerpart.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def createScene(root):
{'name': 'rotation', 'type': 'Vec3d', 'help': '', 'default': [0, 0, 0]}
]

_validState = True

def _getFilePath(self, filename) -> str:
"""
Expand Down Expand Up @@ -100,41 +101,52 @@ def _checkFile(self, filename) -> bool:
Sofa.msg_error(self.getName(), f'Missing file: {filename} in data/meshes/centerparts directory.')
return False


def isValid(self) -> bool:
"""
Check if the centerpart is in a valid state. Returns True if the centerpart is in a valid state, False otherwise.
"""
return self._validState

def __init__(self, *args, **kwargs):
Sofa.Prefab.__init__(self, *args, **kwargs)

self._addRequiredPlugins()

filename = self.partName.value + '.json'
self._checkFile(filename)
self._params = json.load(open(self._getFilePath(filename)))

self.flip = False
if self.rotation[0] == 180: # the legs are pointing upward, we flip the center part
self.flip = True

self._checkFile(self.partName.value + '.stl')
match self.type.value:
case "deformable":
self._addDeformableCenterPart()
case "rigid":
self._addRigidCenterPart()
case _:
Sofa.msg_error("centerpart.py", 'Unknown model, value should be "deformable", or "rigid".')
return

self._addVisualModel()

if self._checkFile(filename):
self._params = json.load(open(self._getFilePath(filename)))

self.flip = False
if self.rotation[0] == 180: # the legs are pointing upward, we flip the center part
self.flip = True

if self._checkFile(self.partName.value + '.stl'):
match self.type.value:
case "deformable":
if self._checkFile(self.partName.value + '.vtk'):
self._addDeformableCenterPart()
else:
self._validState = False
return
case "rigid":
self._addRigidCenterPart()
case _:
Sofa.msg_error("centerpart.py", 'Unknown model, value should be "deformable", or "rigid".')
return
self._addVisualModel()
else:
self._validState = False

else:
self._validState = False

def _addDeformableCenterPart(self):
"""
Add a deformable center part to the simulation.
The part is created using a tetrahedral mesh loaded from a VTK file. The mass density, Poisson's ratio, and Young's modulus
are set based on the parameters provided. The part is then attached to the legs using a rigid mapping.
"""
self._checkFile(self.partName.value + '.vtk')

# Load the mesh and create the topology, mechanical object, mass and force field
# This node contains the entire object
part = Sofa.Core.Node("EntireObject")
Expand Down
32 changes: 19 additions & 13 deletions emio/parts/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,27 @@ def __init__(self, centerPartClass=CenterPart, *args, **kwargs):
if self.centerPartName.value is not None and self.centerPartName.value != "None":
color = getColorFromFilename(self.centerPartName.value) if "blue" not in self.centerPartName.value else RGBAColor.lightblue
self.centerpart = centerPartClass(name="CenterPart",
positions=centerPartPositions,
partName=self.centerPartName.value,
model=self.centerPartModel.value,
massDensity=self.centerPartMassDensity.value,
poissonRatio=self.centerPartPoissonRatio.value,
youngModulus=self.centerPartYoungModulus.value,
type=self.centerPartType.value,
color=color,
rotation=[0, 0, 0] if "down" in legsPositionOnMotor[0] else [180, 180, 0]
)
if self.centerPartType.value == "rigid":
self.effector = self.centerpart.addChild("Effector")
positions=centerPartPositions,
partName=self.centerPartName.value,
model=self.centerPartModel.value,
massDensity=self.centerPartMassDensity.value,
poissonRatio=self.centerPartPoissonRatio.value,
youngModulus=self.centerPartYoungModulus.value,
type=self.centerPartType.value,
color=color,
rotation=[0, 0, 0] if "down" in legsPositionOnMotor[0] else [180, 180, 0]
)
if self.centerpart.isValid():
if self.centerPartType.value == "rigid":
self.effector = self.centerpart.addChild("Effector")
else:
self.effector = self.centerpart.attach.addChild("Effector")
else:
self.effector = self.centerpart.attach.addChild("Effector")
self._validState = False
self.addChild(self.centerpart)

# Structure and camera visual
if self._validState:
self._addBox()
self._addCamera()

Expand Down
Loading