You are on page 1of 16

Interfacing OpenSim models with MATLAB/Simulink OpenSim Jamboree July 2010

Acknowledgements

OpenSim Developer Jamboree July 2010

Why do we care about MATLAB and OpenSim?

MATLAB Simulink

S-function

OpenSim

Pros Worlds leading mathematical computing software Used by engineers and scientists in industry, government, and education Rich resources for numerical integration Simulink extends MATLAB with a graphical environment for rapid design, control, and simulation of complex dynamic systems Con Limited resources for simulating and analyzing neuromusculoskeletal systems

Pros Popular open-source software maintained on SimTK.org Used for simulating and analyzing neuromusculoskeletal systems Offers tracking algorithms (CMC), actuators (muscles), and analyses (muscle-induced accelerations). GUI provides tools for viewing models, editing muscles, generating muscle-actuated simulations, and plotting results Con Limited resources for rapid design and control of dynamic systems

OpenSim Developer Jamboree July 2010

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, C, C++, 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, you can implement an algorithm in an S-function (e.g., interface with OpenSim) and use the S-Function block in a Simulink model After you write your S-function, you can customize the user interface using masking
OpenSim Developer Jamboree July 2010

Simulink

How does an S-function work?


Basic Mathematics of Simulink Blocks

(Input)

(States)

(Output)

y = f o (t , x, u ) x = f d (t , x, u )
OpenSim Developer Jamboree July 2010

Simulink

How does an S-function work?


OpenSim Forward Dynamics
controls .xml initialStates .sto

OpenSim Files

EMGs

Forces

Neural Command

Musculotendon Dynamics

x Musculoskeletal (States) Geometry

Moments

Velocities, Accelerations Angles

Multibody Dynamics

OpenSim

Observed Movement

model .osim

externalLoads .mot

OpenSim Developer Jamboree July 2010

Simulink

How does an S-function work?


MATLAB/Simulink Forward Dynamics
controls .xml initialStates .sto

OpenSim Files MATLAB S-function

EMGs

Forces

Moments

Velocities, Accelerations Angles

Neural Command

Musculotendon Dynamics

S-function
model .osim

Musculoskeletal Geometry

Multibody Dynamics

OpenSim MATLAB

Observed Movement

Simulink input

externalLoads .mot

OpenSim Developer Jamboree July 2010

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, the engine initializes the S-function: Initializing the SimStruct, 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, 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 OpenSim?


Initialize Model Calculate time of next sample hit Calculate outputs Simulation loop
model->getNumControls() model->getNumStates() Block's characteristics (number of inputs, 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, 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

How does Simulink interact with OpenSim?


Initialize Model Calculate time of next sample hit Calculate outputs Simulation loop

statesStore->append(ssGetT(S), 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
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

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, u, x) Time, controls, states model->getDerivCallbackSet()->set(t, u, x) model->getActuatorSet()->computeActuation() Actuation model->getDerivCallbackSet()->computeActuation(t, u, x) model->getActuatorSet()->apply() model->getDerivCallbackSet()->applyActuation(t, u, x) model->getContactSet()->computeContact() Contact model->getDerivCallbackSet()->computeContact(t, u, x) model->getContactSet()->apply() model->getDerivCallbackSet()->applyContact(t, u, 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

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

You might also like