# Interfacing OpenSim models with MATLAB®/Simulink® OpenSim Jamboree July 2010

Acknowledgements OpenSim Developer Jamboree July 2010 .

actuators (muscles). • GUI provides tools for viewing models. and simulation of complex dynamic systems Con • Limited resources for simulating and analyzing neuromusculoskeletal systems Pros • Popular open-source software maintained on SimTK. and analyses (muscle-induced accelerations). generating muscle-actuated simulations. control. government.org • Used for simulating and analyzing neuromusculoskeletal systems • Offers tracking algorithms (CMC). and education • Rich resources for numerical integration • Simulink extends MATLAB with a graphical environment for rapid design. and plotting results Con • Limited resources for rapid design and control of dynamic systems OpenSim Developer Jamboree July 2010 .Why do we care about MATLAB and OpenSim? MATLAB® Simulink® S-function OpenSim Pros • World’s leading mathematical computing software • Used by engineers and scientists in industry. editing muscles.

or Fortran and compiled as MEX-files • S-functions are dynamically linked subroutines that MATLAB can automatically load and execute • The S-function API enables you to interact with the Simulink engine very similar to built-in Simulink blocks • By following a set of simple rules. C++. interface with OpenSim) and use the S-Function block in a Simulink model • After you write your S-function. C.g.What is an S-function? • S-functions are system-functions that extend the capabilities of Simulink • An S-function is a computer language description of a Simulink block written in MATLAB®. you can customize the user interface using masking OpenSim Developer Jamboree July 2010 . you can implement an algorithm in an S-function (e..

u ) OpenSim Developer Jamboree July 2010 . x.Simulink How does an S-function work? Basic Mathematics of Simulink Blocks (Input) u (States) x (Output) y y = f o (t . x. u )  x = f d (t .

mot OpenSim Developer Jamboree July 2010 .osim externalLoads .Simulink How does an S-function work? OpenSim Forward Dynamics controls . Accelerations Angles Multibody Dynamics ∫∫ OpenSim Observed Movement model .sto OpenSim Files EMGs Forces Neural Command Musculotendon Dynamics x Musculoskeletal (States) Geometry Moments Velocities.xml initialStates .

xml initialStates .osim Musculoskeletal Geometry Multibody Dynamics ∫∫ OpenSim MATLAB Observed Movement Simulink input externalLoads . Accelerations Angles Neural Command Musculotendon Dynamics S-function model .mot OpenSim Developer Jamboree July 2010 .Simulink How does an S-function work? MATLAB/Simulink Forward Dynamics controls .sto OpenSim Files MATLAB S-function EMGs Forces Moments Velocities.

so the solvers can compute the states for the S-function Locate discontinuities and end current time step at point of zero crossing OpenSim Developer Jamboree July 2010 Update states Calculate derivatives Calculate outputs Calculate derivatives Locate zero crossings End Simulation .How does Simulink interact with S-functions? Initialize Model Calculate time of next sample hit Calculate outputs Simulation loop Prior to the first simulation loop. a simulation structure that contains information about the S-function • Setting the number and dimensions of input and output ports • Setting the block sample times • Allocating storage areas If a variable sample time block. this stage calculates the next step size Calculation of outputs in the major time step — all the block output ports are valid for the current time step Update of states in the major time step — Integration performs once-per-time-step activities (minor time step) Engine calls the output and derivative portions of your S-function at minor time steps. the engine initializes the S-function: • Initializing the SimStruct.

outputs. states) model = new Model(modelFileName) Process parameters model->setup() initialStatesStore = new Storage(initialStatesFileName) controlSet = new ControlSet(controlsFileName) tool = new ForwardTool() tool->InitializeExternalLoads(…) new Storage(1000.How does Simulink interact with OpenSim? Initialize Model Calculate time of next sample hit Calculate outputs Simulation loop model->getNumControls() model->getNumStates() Block's characteristics (number of inputs. “statesStore”) Start of simulation Update states Calculate derivatives Calculate outputs Calculate derivatives Locate zero crossings End Simulation model->computeEquilibriumForAuxiliaryStates(x0) initialStatesStore->getData(…) Initialize conditions model->setInitialStates(x0) Integration (minor time step) OpenSim Developer Jamboree July 2010 .

ssGetNumContStates(S). x) Update states Calculate derivatives Calculate outputs Calculate derivatives Locate zero crossings End Simulation OpenSim Developer Jamboree July 2010 Integration (minor time step) .How does Simulink interact with OpenSim? Initialize Model Calculate time of next sample hit Calculate outputs Simulation loop statesStore->append(ssGetT(S).

How does Simulink interact with OpenSim? Initialize Model Calculate time of next sample hit Calculate outputs Simulation loop Sync OpenSim model with Simulink block Update states Calculate derivatives Calculate outputs Calculate derivatives Locate zero crossings End Simulation model->setStates(x) Integration (minor time step) OpenSim Developer Jamboree July 2010 .

u. u. x) model->getActuatorSet()->computeActuation() Actuation model->getDerivCallbackSet()->computeActuation(t. u. controls. x) model->computeDerivatives(dx) Derivatives model->getDerivCallbackSet()->computeDerivatives(…) Update states Calculate derivatives Calculate outputs Calculate derivatives Locate zero crossings End Simulation Integration (minor time step) OpenSim Developer Jamboree July 2010 . u. x) Time. u. x) model->getContactSet()->computeContact() Contact model->getDerivCallbackSet()->computeContact(t. u. x) model->getContactSet()->apply() model->getDerivCallbackSet()->applyContact(t. x) model->getActuatorSet()->apply() model->getDerivCallbackSet()->applyActuation(t. states model->getDerivCallbackSet()->set(t.How does Simulink interact with OpenSim? Initialize Model controlSet->getControlValues(…) If controls from file Calculate time of next sample hit Calculate outputs Simulation loop model->set(t.

How does Simulink interact with OpenSim? Initialize Model Calculate time of next sample hit Calculate outputs Simulation loop Update states Calculate derivatives Calculate outputs Calculate derivatives Locate zero crossings End Simulation OpenSim Developer Jamboree July 2010 Integration (minor time step) .

How does Simulink interact with OpenSim? Initialize Model Calculate time of next sample hit Calculate outputs Simulation loop Update states Calculate derivatives Calculate outputs Calculate derivatives Locate zero crossings End Simulation OpenSim Developer Jamboree July 2010 Integration (minor time step) .

How does Simulink interact with OpenSim? Initialize Model Calculate time of next sample hit Calculate outputs Simulation loop Update states Calculate derivatives Calculate outputs Calculate derivatives Locate zero crossings End Simulation model->getDynamicsEngine().convertRadiansToDegrees(…) statesStore->print(…) Save results OpenSim Developer Jamboree July 2010 Integration (minor time step) .

Examples Falling Cube Tug-of-War Elbow Flexion Swing Phase of Gait Pole Balancing OpenSim Developer Jamboree July 2010 .