Recreate Moco solution from osim, sto files of states and control

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Recreate Moco solution from osim, sto files of states and control

Post by Hide Kimpara » Thu Apr 25, 2024 7:01 am

Dear experts of OpenSim and Moco,

I'm learning OpenSim MOCO with the latest OpenSim. I'm interested in predicting gait motions using a osim model with contact bodies. I found an example of 2Dwalking and examing it in Python environment.
Then I have a question. This sample creates gaitTrackingSolution using MocoTracking, and solution variable is reused as a guess in gaitPrediction with MocoStudy. But is it possible to recreate the gaitTrackingSolution from osim, sto files of states and controls?
For example, I have a_model.osim with a_states.sto and a_control.sto files. How can I recreate a Moco's solution variable.
Here attached file is my python script converted from Matlab sample code.

Any suggestions are helpful.
Thank you and Best,
Hide
Attachments
example2DWalking_Predict.py.txt
(15.81 KiB) Downloaded 87 times

Tags:

User avatar
Nicholas Bianco
Posts: 1050
Joined: Thu Oct 04, 2012 8:09 pm

Re: Recreate Moco solution from osim, sto files of states and control

Post by Nicholas Bianco » Mon Apr 29, 2024 12:22 pm

Hi Hide,

There's a few options you can use through the MocoTrajectory class. You can use one of the constructors directly to create a new MocoTrajectory. Or, if you have an existing MocoTrajectory (e.g., from MocoCasADiSolver::createGuess()), then you can use insertStatesTrajectory() and insertControlsTrajectory() to insert states and controls from another source (my recommended approach). There is also createFromStatesControlsTables(), but this isn't as well used/tested as the other options.

-Nick

User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Re: Recreate Moco solution from osim, sto files of states and control

Post by Hide Kimpara » Wed May 01, 2024 12:07 am

Thank you, Nick!
createFromStatesControlsTables works for me. (I also tried insertStatesTrajectory with MocoTrajectory, but only state labels are registered without matrix part)

Code: Select all

single_state = osim.TimeSeriesTable('gaitTracking_solution_singleStride_states.sto')
single_contl = osim.TimeSeriesTable('gaitTracking_solution_singleStride_controls.sto')
problemRep = osim.MocoProblemRep()
mocoTraj = osim.MocoTrajectory().createFromStatesControlsTables(problemRep, single_state, single_contl)
Now I enjoy running example2DWalking. When I increase speed to 4 m/s, the model is running.

I will try to use other MocoGoal options.

Best,
Hide

User avatar
Nicholas Bianco
Posts: 1050
Joined: Thu Oct 04, 2012 8:09 pm

Re: Recreate Moco solution from osim, sto files of states and control

Post by Nicholas Bianco » Wed May 01, 2024 3:56 pm

Hi Hide,

Great! Glad you found a working solution.

(I realize now that insertStatesTrajectory() was a bad suggestion: it will only work if the MocoTrajectory already contains an existing time vector. It's not designed to build a trajectory for scratch.)

-Nick

POST REPLY