This is Moco's simplest example.
#include <OpenSim/Simulation/SimbodyEngine/SliderJoint.h>
#include <OpenSim/Actuators/CoordinateActuator.h>
#include <Moco/osimMoco.h>
std::unique_ptr<Model> createSlidingMassModel() {
auto model = make_unique<Model>();
model->setName("sliding_mass");
model->set_gravity(SimTK::Vec3(0, 0, 0));
auto* body = new Body("body", 2.0, SimTK::Vec3(0), SimTK::Inertia(0));
model->addComponent(body);
auto* joint = new SliderJoint("slider", model->getGround(), *body);
auto& coord = joint->updCoordinate(SliderJoint::Coord::TranslationX);
coord.setName("position");
model->addComponent(joint);
auto* actu = new CoordinateActuator();
actu->setCoordinate(&coord);
actu->setName("actuator");
actu->setOptimalForce(1);
model->addComponent(actu);
body->attachGeometry(new Sphere(0.05));
model->finalizeConnections();
return model;
}
int main() {
study.setName("sliding_mass");
problem.
setModel(createSlidingMassModel());
problem.
setStateInfo(
"/slider/position/speed", {-50, 50}, 0, 0);
solver.set_num_mesh_intervals(50);
study.print("sliding_mass.omoco");
return EXIT_SUCCESS;
}