Page 1 of 1

Help Needed: Running CMC with Torque Actuators on Hip Motors

Posted: Thu Jun 13, 2024 3:17 am
by nobodix
Hi everyone,

I am currently working on a project using OpenSim 4.5 and I'm trying to run a Computed Muscle Control (CMC) simulation with torque actuators applied to the hip motors. I have been able to set up and run a basic CMC simulation, but I need assistance with correctly adding and configuring torque actuators for the hip motors.

What I've Done So Far:
Loaded the Model:
I loaded the model subject01_metabolics_hip_motors.osim.

Added Torque Actuators:
I added torque actuators to the model for both the right and left hip motors.

Created a Torque File:
I generated a .sto file containing the torque values over time for both hip motors.

Configured the CMC Tool:
I configured the CMC tool to use the modified model and specified the torque file.

Python Code
Here is the code I used to set up the torque actuators and run the CMC:

Code: Select all

import opensim as osim
import numpy as np
import pandas as pd

# Load the model
model_path = 'path/to/subject01_metabolics_hip_motors.osim'
model = osim.Model(model_path)

# Add torque actuators to the model
right_hip_motor_torque = osim.TorqueActuator()
right_hip_motor_torque.setName('right_hip_motor_torque')
right_hip_motor_torque.setCoordinateName('hip_flexion_r')
right_hip_motor_torque.setOptimalForce(1)
right_hip_motor_torque.setTorqueIsGlobal(False)
model.addForce(right_hip_motor_torque)

left_hip_motor_torque = osim.TorqueActuator()
left_hip_motor_torque.setName('left_hip_motor_torque')
left_hip_motor_torque.setCoordinateName('hip_flexion_l')
left_hip_motor_torque.setOptimalForce(1)
left_hip_motor_torque.setTorqueIsGlobal(False)
model.addForce(left_hip_motor_torque)

# Save the modified model to a new file
model.printToXML('path/to/modified_model_with_torque_actuators.osim')

# Create the torque .sto file
def create_sto_file(filename, time, right_hip_torque, left_hip_torque):
    data = {
        'time': time,
        'right_hip_motor_torque': right_hip_torque,
        'left_hip_motor_torque': left_hip_torque
    }
    df = pd.DataFrame(data)
    with open(filename, 'w') as f:
        f.write("name  torque\n")
        f.write("datacolumns  3\n")
        f.write("datarows  {}\n".format(len(time)))
        f.write("range  {}  {}\n".format(time[0], time[-1]))
        f.write("endheader\n")
        df.to_csv(f, sep='\t', index=False)

# Example torque data
time = np.linspace(0, 1, 100)
right_hip_torque = np.sin(2 * np.pi * time)
left_hip_torque = np.cos(2 * np.pi * time)

create_sto_file('path/to/hip_motor_torque.sto', time, right_hip_torque, left_hip_torque)

# Configure and run the CMC tool
cmc_tool = osim.CMCTool()
cmc_tool.setModel(osim.Model('path/to/modified_model_with_torque_actuators.osim'))
cmc_tool.setName('CMC_HipMotors_IncludingTorque')

cmc_tool.setDesiredKinematicsFileName('path/to/subject_adjusted_Kinematics_q.sto')
cmc_tool.setTaskSetFileName('path/to/gait10dof_Kinematics_Tracking_Tasks.xml')
cmc_tool.setExternalLoadsFileName('path/to/subject01_walk_grf.xml')

excluded_actuators = osim.ArrayStr()
excluded_actuators.append('right_hip_motor')
excluded_actuators.append('left_hip_motor')
cmc_tool.setExcludedActuators(excluded_actuators)

cmc_tool.setActuatorForceFileName('path/to/hip_motor_torque.sto')
cmc_tool.setUseVerbosePrinting(False)

try:
    cmc_tool.run()
    print("CMC simulation with hip motor torques completed and results are saved in ResultsCMC_HipMotors_IncludingTorque")
except Exception as e:
    print(f"An error occurred while running CMC: {e}")
The Issue:
When I try to run this code, I encounter the following error:
AttributeError: module 'opensim' has no attribute 'ActuatorForceSet'

My Questions:
Is there a correct way to specify the torque actuator forces without using ActuatorForceSet?
Are there any steps or configurations I am missing to properly add and use torque actuators in the CMC simulation?
How should the .sto file be formatted and referenced correctly in the CMC tool?


I would greatly appreciate any help or guidance on this issue. Thank you in advance!