Last active
June 29, 2022 03:14
-
-
Save chrisdembia/ce3fd518988bf49f699639c884a17360 to your computer and use it in GitHub Desktop.
Intro to OpenSim API in Matlab. To configure Matlab to access OpenSim, visit https://simtk-confluence.stanford.edu/display/OpenSim/Scripting+with+Matlab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.opensim.modeling.*; | |
% Build a model. | |
model = Model(); | |
body = Body('body', 1.0, Vec3(0), Inertia(0)); | |
body.attachGeometry(Sphere(0.1)); | |
model.addBody(body); | |
joint = SliderJoint('joint', model.getGround(), body); | |
coord = joint.updCoordinate(); | |
coord.setName('translation'); | |
model.addJoint(joint); | |
actuator = CoordinateActuator('translation'); | |
actuator.setName('actuator'); | |
model.addForce(actuator); | |
controller = PrescribedController(); | |
% methodsview(controller); | |
controller.addActuator(actuator); | |
controller.prescribeControlForActuator('actuator', Sine()); | |
model.addController(controller); | |
model.finalizeConnections(); | |
model.print('pointmass.osim'); | |
% Simulate a model. | |
model.setUseVisualizer(true); | |
initState = model.initSystem(); | |
disp(initState.getY()); | |
finalState = opensimSimulation.simulate(model, initState, 1.5); | |
% Analyze a simulation. | |
disp(coord.getValue(finalState)); | |
model.realizePosition(finalState); | |
disp(model.calcMassCenterPosition(finalState)); | |
model.realizeAcceleration(finalState); | |
disp(joint.calcReactionOnParentExpressedInGround(finalState)); | |
% Inspect and modify a model. | |
clear all; | |
model = Model('pointmass.osim'); | |
body = model.updBodySet().get('body'); | |
body.setMass(2 * body.getMass()); | |
force = model.updForceSet().get('actuator'); | |
disp(class(force)); | |
actuator = CoordinateActuator.safeDownCast(force); | |
disp(class(actuator)); | |
actuator.setOptimalForce(4 * actuator.getOptimalForce()); | |
% Simulate the modified model. | |
initState = model.initSystem(); | |
disp(initState.getY()); | |
finalState = opensimSimulation.simulate(model, initState, 1.5); | |
disp(model.getCoordinateSet().get('translation').getValue(finalState)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment