I am learning to work with Moco and trying to run MocoTrack based on the tracking_walking.py file from the paper code. I started with the code in the folder and then changed the types of tracking scenarios I'd like to run, and tried to change little else about the code other than to not include EMG data.
Code: Select all
import os
import numpy as np
import copy
import re
import matplotlib
import matplotlib.transforms as mtransforms
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.cm as cm
import cv2
import opensim as osim
os.add_dll_directory('C:/OpenSim 4.5/bin/')
print('Added directory')
from moco_paper_result_noEMG import MocoPaperResult
import utilities
from utilities import plot_joint_moment_breakdown
from utilities import toarray
root_dir=os.getcwd()
class MocoTrackConfig:
def __init__(self, name, legend_entry, tracking_weight, effort_weight,
color, linestyle='-',
mesh_interval=0.014, guess=None, flags=[]):
self.name = name
self.legend_entry = legend_entry
self.tracking_weight = tracking_weight
self.effort_weight = effort_weight
self.color = color
self.linestyle = linestyle
self.mesh_interval = mesh_interval
# If guess is 'None', we use the solution from the inverse problem.
self.guess = guess
self.flags = flags
self.tracking_solution_relpath_prefix = 'moco_results/motion_tracking_walking_solution'
def get_solution_path(self, root_dir):
return os.path.join(root_dir,
f'{self.tracking_solution_relpath_prefix}_'
f'{self.name}.sto')
def get_solution_path_fullcycle(self, root_dir):
return os.path.join(root_dir,
f'{self.tracking_solution_relpath_prefix}_'
f'{self.name}_fullcycle.sto')
def get_solution_path_grfs(self, root_dir):
return os.path.join(root_dir,
f'{self.tracking_solution_relpath_prefix}_'
f'{self.name}_fullcycle_grfs.sto')
print('Configured MocoTrack')
class MotionTrackingWalking(MocoPaperResult):
def __init__(self):
super(MotionTrackingWalking, self).__init__()
print('initialized')
self.initial_time = 0.50
self.half_time = 1.10
self.final_time = 1.70
print('final time set')
duration = self.half_time - self.initial_time
num_mesh_intervals = 40
mesh_interval = duration / num_mesh_intervals
self.passive_forces = False
self.inverse_solution_relpath = \
'moco_results/motion_tracking_walking_inverse_solution.sto'
self.cmap = cm.get_cmap('nipy_spectral')
self.config_track = MocoTrackConfig(
name='track',
legend_entry='normal',
tracking_weight=10,
effort_weight=10,
mesh_interval=mesh_interval,
color='black')
self.config_weakhipabd = MocoTrackConfig(
name='weakhipabd',
legend_entry='weak hip abductors',
tracking_weight=10,
effort_weight=10,
mesh_interval=mesh_interval,
color=self.cmap(0.5),
guess='track',
flags=['weakhipabd'])
self.configs = [
self.config_track,
self.config_weakhipabd
]
self.config_map = {config.name: config for config in self.configs}