Page 1 of 1

Parallel/Symmetric mechanism in opensim?

Posted: Sun Jan 15, 2023 7:26 pm
by haolee
Hi community
I am trying to build a model with symmetric mechanisms. However, I wonder if opensim can solve the inverse kinematic, or even the inverse dynamics.

The model is created with the model "gait2392_simbody_scaled.osim" and the following script:

Code: Select all

import opensim as osim
import sys

# sys.path.append('C:/OpenSim 4.3/Geometry') #how to add geometry if it is not in the same folder? 

model = osim.Model('gait2392_simbody_scaled.osim')
# model.setUseVisualizer(True)

exo_thigh_len = 0.45
exo_calf_len = 0.45

#get all the bodies
pelvis = model.getBodySet().get('pelvis')
femur_r = model.getBodySet().get('femur_r')
tibia_r = model.getBodySet().get('tibia_r')
talus_r = model.getBodySet().get('talus_r')
calcn_r = model.getBodySet().get('calcn_r')
toes_r = model.getBodySet().get('toes_r')

femur_l = model.getBodySet().get('femur_l')
tibia_l = model.getBodySet().get('tibia_l')
talus_l = model.getBodySet().get('talus_l')
calcn_l = model.getBodySet().get('calcn_l')
toes_l = model.getBodySet().get('toes_l')

torso = model.getBodySet().get('torso')




#get all the joints
ground_pelvis = model.getJointSet().get("ground_pelvis")
hip_r = model.getJointSet().get("hip_r")
knee_r = model.getJointSet().get("knee_r")
ankle_r = model.getJointSet().get("ankle_r")
subtalar_r = model.getJointSet().get("subtalar_r")
mtp_r = model.getJointSet().get("mtp_r")
hip_l = model.getJointSet().get("hip_l")
knee_l = model.getJointSet().get("hip_l")
ankle_l = model.getJointSet().get("ankle_l")
subtalar_l = model.getJointSet().get("subtalar_l")
mtp_l = model.getJointSet().get("mtp_l")
back = model.getJointSet().get("back")


## add body to model
exo_l_thigh = osim.Body("exo_l_thigh",
                    1.0,
                    osim.Vec3(0, 0, 0),
                    osim.Inertia(0, 0, 0))
exo_l_calf = osim.Body("exo_l_calf",
                       1.0,
                       osim.Vec3(0,0,0),
                       osim.Inertia(0,0,0))
## add exo joints
exo_hip_l = osim.PinJoint("exo_hip_l",
                         pelvis, # PhysicalFrame
                         osim.Vec3(-0.0707, -0.0661, -0.0835),
                         osim.Vec3(0, 0, 0),
                         exo_l_thigh, # PhysicalFrame
                         osim.Vec3(0, exo_thigh_len/2, 0),
                         osim.Vec3(0, 0, 0))
exo_knee_l = osim.PinJoint("exo_knee_l",
                           exo_l_thigh,
                           osim.Vec3(0,-exo_thigh_len/2,0),
                           osim.Vec3(0,0,0),
                           exo_l_calf,
                           osim.Vec3(0,exo_calf_len/2,0),
                           osim.Vec3(0,0,0))

exo_ankle_l = osim.PinJoint("exo_ank_l",
                            exo_l_calf,
                            osim.Vec3(0,-exo_calf_len/2,0),
                            osim.Vec3(0,0,0),
                            tibia_l,
                            osim.Vec3(0,0,0),
                            osim.Vec3(0,0,0))

model.addBody(exo_l_thigh)
model.addBody(exo_l_calf)

model.addJoint(exo_hip_l)
model.addJoint(exo_knee_l)
model.addJoint(exo_ankle_l)


## add geometry
exo_geo_thigh = osim.Ellipsoid(0.025, exo_thigh_len/2, 0.025)
exo_geo_thigh.setColor(osim.Black)
exo_l_thigh_center = osim.PhysicalOffsetFrame()
exo_l_thigh_center.setName("exo_l_thigh_center")
exo_l_thigh_center.setParentFrame(exo_l_thigh)
exo_l_thigh_center.setOffsetTransform(osim.Transform(osim.Vec3(0,0,0)))
exo_l_thigh.addComponent(exo_l_thigh_center)
exo_l_thigh_center.attachGeometry(exo_geo_thigh.clone())

exo_geo_calf = osim.Ellipsoid(0.025,exo_calf_len/2,0.025)
exo_geo_calf.setColor(osim.Blue)
exo_l_calf_center = osim.PhysicalOffsetFrame()
exo_l_calf_center.setName("exo_l_calf_center")
exo_l_calf_center.setParentFrame(exo_l_calf)
exo_l_calf_center.setOffsetTransform(osim.Transform(osim.Vec3(0,0,0)))
exo_l_calf.addComponent(exo_l_calf_center)
exo_l_calf_center.attachGeometry(exo_geo_calf.clone())

# set coordinate name
exo_hip_l.getCoordinate().setName("exo_hip_rz")
exo_knee_l.getCoordinate().setName("exo_knee_rz")
exo_ankle_l.getCoordinate().setName("exo_ank_rz")

#get the coordinate
pelvis_ty = model.getCoordinateSet().get("pelvis_ty")

state = model.initSystem()
pelvis_ty.setValue(state,1)
pelvis_ty.setLocked(state, True)

model.printToXML("test.osim")
I wish I can have something like:
https://imgur.com/9pae8Rw

I wonder since the parallel coordinates (exo_hip_rz, exo_knee_rz, exo_ank_rz) will not auto-satisfy the parallel constraints, will OpenSim solve inverse kinematic/dynamic if I set the initial condition correctly?

Also, is there anyway I can load the geometry in python if it is not in the same folder as the *osim file?