This is an example using the MocoTrack tool with a complex model to track walking.
#include <OpenSim/Actuators/CoordinateActuator.h>
#include <OpenSim/Actuators/ModelOperators.h>
#include <OpenSim/Moco/osimMoco.h>
#include <OpenSim/Common/STOFileAdapter.h>
void torqueDrivenMarkerTracking() {
track.
setName(
"torque_driven_marker_tracking");
modelProcessor.
append(ModOpAddExternalLoads(
"grf_walk.xml") );
modelProcessor.
append(ModOpRemoveMuscles());
modelProcessor.
append(ModOpAddReserves(250.0, 1.0));
}
void muscleDrivenStateTracking() {
track.
setName(
"muscle_driven_state_tracking");
modelProcessor.append(ModOpAddExternalLoads("grf_walk.xml"));
modelProcessor.append(ModOpIgnoreTendonCompliance());
modelProcessor.append(ModOpReplacePathsWithFunctionBasedPaths(
"subject_walk_scaled_FunctionBasedPathSet.xml"));
Model model = modelProcessor.process();
if (coordPath.find("pelvis") != std::string::npos) {
}
}
periodicityGoal->addStatePair(coord.getStateVariableNames()[0]);
}
periodicityGoal->addStatePair(coord.getStateVariableNames()[1]);
}
periodicityGoal->addStatePair(muscle.getStateVariableNames()[0]);
periodicityGoal->addControlPair(muscle.getAbsolutePathString());
}
periodicityGoal->addControlPair(actu.getAbsolutePathString());
}
solver.set_optim_constraint_tolerance(1e-4);
solution.
write(
"exampleMocoTrack_state_tracking_solution.sto");
}
void muscleDrivenJointMomentTracking() {
track.
setName(
"muscle_driven_joint_moment_tracking");
modelProcessor.append(ModOpAddExternalLoads("grf_walk.xml"));
modelProcessor.append(ModOpIgnoreTendonCompliance());
modelProcessor.append(ModOpReplacePathsWithFunctionBasedPaths(
"subject_walk_scaled_FunctionBasedPathSet.xml"));
Model model = modelProcessor.process();
if (coordPath.find("pelvis") != std::string::npos) {
}
}
periodicityGoal->addStatePair(coord.getStateVariableNames()[0]);
}
periodicityGoal->addStatePair(coord.getStateVariableNames()[1]);
}
periodicityGoal->addControlPair(muscle.getAbsolutePathString());
periodicityGoal->addStatePair(muscle.getStateVariableNames()[0]);
}
periodicityGoal->addControlPair(actu.getAbsolutePathString());
}
auto* jointMomentTracking =
"joint_moment_tracking", 1e-2);
jointMomentTracking->setReference(jointMomentRef);
jointMomentTracking->setForcePaths({".*externalloads.*"});
jointMomentTracking->setAllowUnusedReferences(true);
jointMomentTracking->setNormalizeTrackingError(true);
jointMomentTracking->setIgnoreConstrainedCoordinates(true);
const auto& coordName = coordinate.getName();
if (coordName.find("pelvis") != std::string::npos) {
jointMomentTracking->setWeightForGeneralizedForce(coordName, 0);
}
if (coordName.find("ankle") != std::string::npos) {
jointMomentTracking->setWeightForGeneralizedForce(coordName, 100);
}
}
solver.set_optim_constraint_tolerance(1e-4);
solver.resetProblem(problem);
solver.setGuessFile("exampleMocoTrack_state_tracking_solution.sto");
}
solution.
write(
"exampleMocoTrack_joint_moment_tracking_solution.sto");
model.
print(
"exampleMocoTrack_model.osim");
{".*externalloads.*"});
}
int main() {
torqueDrivenMarkerTracking();
muscleDrivenStateTracking();
muscleDrivenJointMomentTracking();
return EXIT_SUCCESS;
}