Skip to content

Instantly share code, notes, and snippets.

@klotzambein
Last active July 7, 2021 10:38
Show Gist options
  • Save klotzambein/8bc814f9019182492a354cf39346ded6 to your computer and use it in GitHub Desktop.
Save klotzambein/8bc814f9019182492a354cf39346ded6 to your computer and use it in GitHub Desktop.
Pose manipulation for OpenSim using Vishal's Model
Copyright 2021 Robin Kock
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# This is my reset function, in my environment
def reset(self, init_pose: 'Pose'):
self.state = self.model.initializeState()
qt = init_pose.getQ()
ut = init_pose.getU()
state = self.get_state()
q = state.getQ()
u = state.getQDot()
for i in range(17):
q[i] = qt[i]
u[i] = ut[i]
state.setQ(q)
state.setU(u)
self.set_state(state)
self.model.equilibrateMuscles(self.state)
self.state.setTime(0)
self.reset_manager()
#This is both the Q and Qdot vector, containing all the angles and positions
class Pose:
"""
This is the Layout of the Q vector:
00: pelvis tilt
01: pelvis list
02: pelvis rotation
03: pelvis x
04: pelvis y
05: pelvis z
06: hip flexion right
07: hip abduction right
08: (hip ... right)
09: hip flexion left
10: hip abduction left
11: (hip ... left)
12: (lumbar ext)
13: knee right
14: ankle right
15: knee left
16: ankle left
"""
def __init__(self):
self.pose = np.zeros((17, 2))
# The lumbar extension is always -5 degree
self.pose[16, 0] = -5.0 * np.pi / 180
def set_pelvis_euler_angles(self, tilt: float, list: float, rotation: float):
"""
This sets the rotation of the pelvis relative to the ground. All angles are in radians.
:param tilt: pitch (positive moves the head back and the feet forward)
:param list: roll (positive moves the head to the right and the feet to the left)
:param rotation: yaw (positive turns the model to the left)
"""
self.pose[0:3, 0] = [tilt, list, rotation]
def set_pelvis_position(self, x: float, y: float, z: float):
self.pose[3:6, 0] = [x, y, z]
def set_leg_left(self, hip_flexion: float, hip_abduction: float, knee: float, ankle: float):
self.pose[9, 0] = hip_flexion
self.pose[10, 0] = hip_abduction
self.pose[15, 0] = knee
self.pose[16, 0] = ankle
def set_leg_right(self, hip_flexion: float, hip_abduction: float, knee: float, ankle: float):
self.pose[6, 0] = hip_flexion
self.pose[7, 0] = hip_abduction
self.pose[13, 0] = knee
self.pose[14, 0] = ankle
def set_from_dict(self, pose, rotation_scale: float = 1.0):
rs = rotation_scale
self.set_pelvis_position(
pose['pelvis_tx'], pose['pelvis_ty'], pose['pelvis_tz'])
self.set_pelvis_euler_angles(
pose['pelvis_tilt'] * rs, pose['pelvis_list'] * rs, pose['pelvis_rotation'] * rs)
self.set_leg_left(pose['hip_flexion_l'] * rs, pose['hip_adduction_l']
* rs, pose['knee_angle_l'] * rs, pose['ankle_angle_l'] * rs)
self.set_leg_right(pose['hip_flexion_r'] * rs, pose['hip_adduction_r']
* rs, pose['knee_angle_r'] * rs, pose['ankle_angle_r'] * rs)
def set_from_dict_degrees(self, pose):
self.set_from_dict(pose, rotation_scale=np.pi / 180)
def set_from_q_vector(self, q):
for i in range(17):
self.pose[i, 0] = q[i]
def compute_velocities(self, next: 'Pose', delta_time: float):
"""
Given the next pose and the time between the current pose and the next
pose this function will compute the appropriate velocities to go from
the current pose to the next one.
"""
self.pose[:, 1] = (next.pose[:, 0] - self.pose[:, 0]) / delta_time
def is_in_bounds(self, bounds_center: 'Pose', max_angle: float, max_distance: float):
delta = abs(self.pose[:, 0] - bounds_center.pose[:, 0])
pelvis_rot = np.all(delta[0:3] < max_angle)
pelvis_pos = np.all(delta[3:6] < max_distance)
hip_right = np.all(delta[6:8] < max_angle)
hip_left = np.all(delta[9:11] < max_angle)
rest = np.all(delta[13:17] < max_angle)
# print(pelvis_rot, pelvis_pos, hip_right, hip_left, rest, delta[13:17] / max_angle)
return pelvis_rot and pelvis_pos and hip_right and hip_left and rest
def getQ(self):
return self.pose[:, 0]
def getU(self):
return self.pose[:, 1]
def toString(self):
return ",".join([str(x) for x in self.getQ().tolist()])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment