This example conducts a 2-D prediction of walking, employing a MocoPerodicityGoal.
#include <OpenSim/Common/STOFileAdapter.h>
#include <OpenSim/Moco/osimMoco.h>
MocoSolution gaitTracking(double controlEffortWeight = 10,
double stateTrackingWeight = 1,
double GRFTrackingWeight = 1) {
using SimTK::Pi;
MocoTrack track;
track.setName("gaitTracking");
ModelProcessor modelprocessor = ModelProcessor("2D_gait.osim");
track.setModel(modelprocessor);
track.setStatesReference(
TableProcessor("referenceCoordinates.sto") | TabOpLowPassFilter(6));
track.set_states_global_tracking_weight(stateTrackingWeight);
track.set_allow_unused_references(true);
track.set_track_reference_position_derivatives(true);
track.set_apply_tracked_states_to_guess(true);
track.set_initial_time(0.0);
track.set_final_time(0.47008941);
MocoStudy study = track.initialize();
MocoProblem& problem = study.updProblem();
auto* symmetryGoal = problem.addGoal<MocoPeriodicityGoal>("symmetryGoal");
Model model = modelprocessor.process();
model.initSystem();
for (const auto& coord : model.getComponentList<Coordinate>()) {
if (IO::EndsWith(coord.getName(), "_r")) {
symmetryGoal->addStatePair({coord.getStateVariableNames()[0],
std::regex_replace(coord.getStateVariableNames()[0],
std::regex("_r"), "_l")});
symmetryGoal->addStatePair({coord.getStateVariableNames()[1],
std::regex_replace(coord.getStateVariableNames()[1],
std::regex("_r"), "_l")});
}
if (IO::EndsWith(coord.getName(), "_l")) {
symmetryGoal->addStatePair({coord.getStateVariableNames()[0],
std::regex_replace(coord.getStateVariableNames()[0],
std::regex("_l"), "_r")});
symmetryGoal->addStatePair({coord.getStateVariableNames()[1],
std::regex_replace(coord.getStateVariableNames()[1],
std::regex("_l"), "_r")});
}
if (!IO::EndsWith(coord.getName(), "_l") &&
!IO::EndsWith(coord.getName(), "_r") &&
!IO::EndsWith(coord.getName(), "_tx")) {
symmetryGoal->addStatePair({coord.getStateVariableNames()[0],
coord.getStateVariableNames()[0]});
symmetryGoal->addStatePair({coord.getStateVariableNames()[1],
coord.getStateVariableNames()[1]});
}
}
symmetryGoal->addStatePair({"/jointset/groundPelvis/pelvis_tx/speed"});
symmetryGoal->addControlPair({"/lumbarAct"});
for (const auto& muscle : model.getComponentList<Muscle>()) {
if (IO::EndsWith(muscle.getName(), "_r")) {
symmetryGoal->addStatePair({muscle.getStateVariableNames()[0],
std::regex_replace(muscle.getStateVariableNames()[0],
std::regex("_r"), "_l")});
}
if (IO::EndsWith(muscle.getName(), "_l")) {
symmetryGoal->addStatePair({muscle.getStateVariableNames()[0],
std::regex_replace(muscle.getStateVariableNames()[0],
std::regex("_l"), "_r")});
}
}
MocoControlGoal& effort =
dynamic_cast<MocoControlGoal&>(problem.updGoal("control_effort"));
effort.setWeight(controlEffortWeight);
if (GRFTrackingWeight != 0) {
auto* contactTracking = problem.addGoal<MocoContactTrackingGoal>(
"contact", GRFTrackingWeight);
contactTracking->setExternalLoadsFile("referenceGRF.xml");
contactTracking->addContactGroup(
{"contactHeel_r", "contactFront_r"},"Right_GRF");
contactTracking->addContactGroup(
{"contactHeel_l", "contactFront_l"}, "Left_GRF");
contactTracking->setProjection("plane");
contactTracking->setProjectionVector(SimTK::Vec3(0, 0, 1));
}
problem.setStateInfo("/jointset/groundPelvis/pelvis_tilt/value",
{-20 * Pi / 180, -10 * Pi / 180});
problem.setStateInfo("/jointset/groundPelvis/pelvis_tx/value", {0, 1});
problem.setStateInfo(
"/jointset/groundPelvis/pelvis_ty/value", {0.75, 1.25});
problem.setStateInfo("/jointset/hip_l/hip_flexion_l/value",
{-10 * Pi / 180, 60 * Pi / 180});
problem.setStateInfo("/jointset/hip_r/hip_flexion_r/value",
{-10 * Pi / 180, 60 * Pi / 180});
problem.setStateInfo(
"/jointset/knee_l/knee_angle_l/value", {-50 * Pi / 180, 0});
problem.setStateInfo(
"/jointset/knee_r/knee_angle_r/value", {-50 * Pi / 180, 0});
problem.setStateInfo("/jointset/ankle_l/ankle_angle_l/value",
{-15 * Pi / 180, 25 * Pi / 180});
problem.setStateInfo("/jointset/ankle_r/ankle_angle_r/value",
{-15 * Pi / 180, 25 * Pi / 180});
problem.setStateInfo("/jointset/lumbar/lumbar/value", {0, 20 * Pi / 180});
MocoCasADiSolver& solver = study.updSolver<MocoCasADiSolver>();
solver.set_num_mesh_intervals(50);
solver.set_verbosity(2);
solver.set_optim_solver("ipopt");
solver.set_optim_convergence_tolerance(1e-4);
solver.set_optim_constraint_tolerance(1e-4);
solver.set_optim_max_iterations(1000);
MocoSolution solution = study.solve();
full.write("gaitTracking_solution_fullcycle.sto");
std::vector<std::string> contact_r;
std::vector<std::string> contact_l;
contact_r.push_back("contactHeel_r");
contact_r.push_back("contactFront_r");
contact_l.push_back("contactHeel_l");
contact_l.push_back("contactFront_l");
model, full, contact_r, contact_l);
STOFileAdapter::write(externalForcesTableFlat,
"gaitTracking_solutionGRF_fullcycle.sto");
return solution;
}
void gaitPrediction(const MocoSolution& gaitTrackingSolution) {
using SimTK::Pi;
MocoStudy study;
study.setName("gaitPrediction");
MocoProblem& problem = study.updProblem();
ModelProcessor modelprocessor = ModelProcessor("2D_gait.osim");
problem.setModelProcessor(modelprocessor);
auto* symmetryGoal = problem.addGoal<MocoPeriodicityGoal>("symmetryGoal");
Model model = modelprocessor.process();
model.initSystem();
for (const auto& coord : model.getComponentList<Coordinate>()) {
if (IO::EndsWith(coord.getName(), "_r")) {
symmetryGoal->addStatePair({coord.getStateVariableNames()[0],
std::regex_replace(coord.getStateVariableNames()[0],
std::regex("_r"), "_l")});
symmetryGoal->addStatePair({coord.getStateVariableNames()[1],
std::regex_replace(coord.getStateVariableNames()[1],
std::regex("_r"), "_l")});
}
if (IO::EndsWith(coord.getName(), "_l")) {
symmetryGoal->addStatePair({coord.getStateVariableNames()[0],
std::regex_replace(coord.getStateVariableNames()[0],
std::regex("_l"), "_r")});
symmetryGoal->addStatePair({coord.getStateVariableNames()[1],
std::regex_replace(coord.getStateVariableNames()[1],
std::regex("_l"), "_r")});
}
if (!IO::EndsWith(coord.getName(), "_l") &&
!IO::EndsWith(coord.getName(), "_r") &&
!IO::EndsWith(coord.getName(), "_tx")) {
symmetryGoal->addStatePair({coord.getStateVariableNames()[0],
coord.getStateVariableNames()[0]});
symmetryGoal->addStatePair({coord.getStateVariableNames()[1],
coord.getStateVariableNames()[1]});
}
}
symmetryGoal->addStatePair({"/jointset/groundPelvis/pelvis_tx/speed"});
symmetryGoal->addControlPair({"/lumbarAct"});
for (const auto& muscle : model.getComponentList<Muscle>()) {
if (IO::EndsWith(muscle.getName(), "_r")) {
symmetryGoal->addStatePair({muscle.getStateVariableNames()[0],
std::regex_replace(muscle.getStateVariableNames()[0],
std::regex("_r"), "_l")});
}
if (IO::EndsWith(muscle.getName(), "_l")) {
symmetryGoal->addStatePair({muscle.getStateVariableNames()[0],
std::regex_replace(muscle.getStateVariableNames()[0],
std::regex("_l"), "_r")});
}
}
auto* speedGoal = problem.addGoal<MocoAverageSpeedGoal>("speed");
speedGoal->set_desired_average_speed(1.2);
auto* effortGoal = problem.addGoal<MocoControlGoal>("effort", 10);
effortGoal->setExponent(3);
effortGoal->setDivideByDisplacement(true);
problem.setTimeBounds(0, {0.4, 0.6});
problem.setStateInfo("/jointset/groundPelvis/pelvis_tilt/value",
{-20 * Pi / 180, -10 * Pi / 180});
problem.setStateInfo("/jointset/groundPelvis/pelvis_tx/value", {0, 1});
problem.setStateInfo(
"/jointset/groundPelvis/pelvis_ty/value", {0.75, 1.25});
problem.setStateInfo("/jointset/hip_l/hip_flexion_l/value",
{-10 * Pi / 180, 60 * Pi / 180});
problem.setStateInfo("/jointset/hip_r/hip_flexion_r/value",
{-10 * Pi / 180, 60 * Pi / 180});
problem.setStateInfo(
"/jointset/knee_l/knee_angle_l/value", {-50 * Pi / 180, 0});
problem.setStateInfo(
"/jointset/knee_r/knee_angle_r/value", {-50 * Pi / 180, 0});
problem.setStateInfo("/jointset/ankle_l/ankle_angle_l/value",
{-15 * Pi / 180, 25 * Pi / 180});
problem.setStateInfo("/jointset/ankle_r/ankle_angle_r/value",
{-15 * Pi / 180, 25 * Pi / 180});
problem.setStateInfo("/jointset/lumbar/lumbar/value", {0, 20 * Pi / 180});
auto& solver = study.initCasADiSolver();
solver.set_num_mesh_intervals(50);
solver.set_verbosity(2);
solver.set_optim_solver("ipopt");
solver.set_optim_convergence_tolerance(1e-4);
solver.set_optim_constraint_tolerance(1e-4);
solver.set_optim_max_iterations(1000);
solver.setGuess(gaitTrackingSolution);
MocoSolution solution = study.solve();
full.write("gaitPrediction_solution_fullcycle.sto");
std::vector<std::string> contact_r;
std::vector<std::string> contact_l;
contact_r.push_back("contactHeel_r");
contact_r.push_back("contactFront_r");
contact_l.push_back("contactHeel_l");
contact_l.push_back("contactFront_l");
model, full, contact_r, contact_l);
STOFileAdapter::write(externalForcesTableFlat,
"gaitPrediction_solutionGRF_fullcycle.sto");
study.visualize(full);
}
int main() {
try {
const MocoSolution gaitTrackingSolution = gaitTracking();
gaitPrediction(gaitTrackingSolution);
} catch (const std::exception& e) { std::cout << e.what() << std::endl; }
return EXIT_SUCCESS;
}
OSIMMOCO_API MocoTrajectory createPeriodicTrajectory(const MocoTrajectory &halfPeriodTrajectory, std::vector< std::string > addPatterns={".*pelvis_tx/value"}, std::vector< std::string > negatePatterns={ ".*pelvis_list(?!/value).*", ".*pelvis_rotation.*", ".*pelvis_tz(?!/value).*", ".*lumbar_bending(?!/value).*", ".*lumbar_rotation.*"}, std::vector< std::string > negateAndShiftPatterns={ ".*pelvis_list/value", ".*pelvis_tz/value", ".*lumbar_bending/value"}, std::vector< std::pair< std::string, std::string > > symmetryPatterns={{R"(_r(\/|_|$))", "_l$1"}, {R"(_l(\/|_|$))", "_r$1"}})
Convert a trajectory covering half the period of a symmetric motion into a trajectory over the full p...
OSIMMOCO_API TimeSeriesTable createExternalLoadsTableForGait(Model model, const StatesTrajectory &trajectory, const std::vector< std::string > &forcePathsRightFoot, const std::vector< std::string > &forcePathsLeftFoot)
Obtain the ground reaction forces, centers of pressure, and torques resulting from Force elements (e....
The Moco interface is contained within the OpenSim namespace.
Definition: Moco.dox:5
TimeSeriesTable_< SimTK::Real > TimeSeriesTable
See TimeSeriesTable_ for details on the interface.
Definition: TimeSeriesTable.h:519