This is a simple example of how to use a MocoStateTrackingGoal.
#include <OpenSim/Simulation/SimbodyEngine/PinJoint.h>
#include <OpenSim/Actuators/CoordinateActuator.h>
#include <OpenSim/Moco/osimMoco.h>
std::unique_ptr<Model> createDoublePendulumModel() {
auto model = make_unique<Model>();
model->setName("double_pendulum");
using SimTK::Vec3;
using SimTK::Inertia;
model->addBody(b0);
model->addBody(b1);
auto* m0 = new Marker("m0", *b0, Vec3(0));
auto* m1 = new Marker("m1", *b1, Vec3(0));
model->addMarker(m0);
model->addMarker(m1);
auto* j0 = new PinJoint("j0", model->getGround(), Vec3(0), Vec3(0),
*b0, Vec3(-1, 0, 0), Vec3(0));
auto& q0 = j0->updCoordinate();
q0.setRangeMin(-10);
q0.setRangeMax(10);
q0.setName("q0");
auto* j1 = new PinJoint("j1",
*b0, Vec3(0), Vec3(0), *b1, Vec3(-1, 0, 0), Vec3(0));
auto& q1 = j1->updCoordinate();
q1.setRangeMin(-10);
q1.setRangeMax(10);
q1.setName("q1");
model->addJoint(j0);
model->addJoint(j1);
auto* tau0 = new CoordinateActuator();
tau0->setCoordinate(&j0->updCoordinate());
tau0->setName("tau0");
tau0->setOptimalForce(1);
tau0->setMinControl(-40);
tau0->setMaxControl(40);
model->addForce(tau0);
auto* tau1 = new CoordinateActuator();
tau1->setCoordinate(&j1->updCoordinate());
tau1->setName("tau1");
tau1->setOptimalForce(1);
tau1->setMinControl(-40);
tau1->setMaxControl(40);
model->addForce(tau1);
SimTK::Transform transform(SimTK::Vec3(-0.5, 0, 0));
auto* b0Center = new PhysicalOffsetFrame("b0_center", *b0, transform);
b0->addComponent(b0Center);
b0Center->attachGeometry(new Ellipsoid(0.5, 0.1, 0.1));
auto* b1Center = new PhysicalOffsetFrame("b1_center", *b1, transform);
b1->addComponent(b1Center);
b1Center->attachGeometry(new Ellipsoid(0.5, 0.1, 0.1));
model->finalizeConnections();
return model;
}
int main() {
MocoStudy study;
study.setName("double_pendulum_tracking");
MocoProblem& problem = study.updProblem();
problem.setModel(createDoublePendulumModel());
double finalTime = 1.0;
problem.setTimeBounds(0, finalTime);
problem.setStateInfo("/jointset/j0/q0/value", {-10, 10});
problem.setStateInfo("/jointset/j0/q0/speed", {-50, 50});
problem.setStateInfo("/jointset/j1/q1/value", {-10, 10});
problem.setStateInfo("/jointset/j1/q1/speed", {-50, 50});
problem.setControlInfo("/forceset/tau0", {-40, 40});
problem.setControlInfo("/forceset/tau1", {-40, 40});
auto* tracking = problem.addGoal<MocoStateTrackingGoal>();
ref.setColumnLabels({"/jointset/j0/q0/value", "/jointset/j1/q1/value"});
for (double time = -0.05; time < finalTime + 0.05; time += 0.01) {
ref.appendRow(time, {
0.5 * SimTK::Pi * time,
0.25 * SimTK::Pi * time
});
}
tracking->setReference(ref);
auto& solver = study.initCasADiSolver();
solver.set_num_mesh_intervals(50);
solver.set_verbosity(2);
solver.set_optim_solver("ipopt");
study.print("double_pendulum_tracking.omoco");
MocoSolution solution = study.solve();
solution.write("exampleTracking_solution.sto");
study.visualize(solution);
return EXIT_SUCCESS;
}
void addTableMetaData(const std::string &key, const Value &value)
Add key-value pair to the table metadata.
Definition: AbstractDataTable.h:220
An OpenSim::Body is a PhysicalFrame (reference frame) with associated inertia specified by its mass,...
Definition: Body.h:42
The Moco interface is contained within the OpenSim namespace.
Definition: ActivationCoordinateActuator.h:30
TimeSeriesTable_< SimTK::Real > TimeSeriesTable
See TimeSeriesTable_ for details on the interface.
Definition: TimeSeriesTable.h:519