opensim matlab july2010 pdf

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

Upload: mateus-dias

Post on 20-Apr-2015

233 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: OpenSim Matlab July2010 PDF

Interfacing OpenSim modelswith MATLAB®/Simulink®

OpenSim Jamboree July 2010

Page 2: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

Acknowledgements

Page 3: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

Con• Limited resources for simulating and

analyzing neuromusculoskeletalsystems

Why do we care about MATLAB and OpenSim?

Pros• World’s 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

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

OpenSimMATLAB®Simulink®

S-function

Page 4: OpenSim Matlab July2010 PDF

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

Page 5: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

How does an S-function work?

),,( uxtfy o=

),,( uxtfx d=

y(Output)

u(Input)

Basic Mathematics of Simulink Blocks

x(States)

Simulink

Page 6: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

x(States)

How does an S-function work?

OpenSim Forward Dynamics

ObservedMovement

Moments Accelerations

MusculoskeletalGeometry

MultibodyDynamics

MusculotendonDynamics

NeuralCommand

EMGs Forces

∫∫

initialStates.sto

externalLoads.mot

controls.xml

model.osim

OpenSim

Simulink

OpenSim

Files

Velocities,Angles

Page 7: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

How does an S-function work?

MATLAB/Simulink Forward Dynamics

ObservedMovement

Moments AccelerationsVelocities,Angles

MusculoskeletalGeometry

MultibodyDynamics

MusculotendonDynamics

NeuralCommand

EMGs Forces

initialStates.sto

externalLoads.mot

controls.xml

model.osim

OpenSim

Simulink

OpenSim

Files

MATLAB

∫∫

S-function

S-function

Simulinkinput

∫∫MATLAB

Page 8: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

How does Simulink interact with S-functions?Initialize Model

Calculate time of next sample hit

Calculate outputs

Update states

Calculate derivatives

Calculate outputs

Calculate derivatives

Locate zero crossings

Sim

ulat

ion

loop

Integration(minor time step)

End Simulation

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 —performs once-per-time-step activities

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

Page 9: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

How does Simulink interact with OpenSim?

Calculate time of next sample hit

Calculate outputs

Update states

Calculate derivatives

Calculate outputs

Calculate derivatives

Locate zero crossings

Sim

ulat

ion

loop

Integration(minor time step)

End Simulation

Initialize Model

model->getNumControls()model->getNumStates()

Block's characteristics (number of inputs,

outputs, states)

model = new Model(modelFileName)model->setup()initialStatesStore = new Storage(initialStatesFileName)controlSet = new ControlSet(controlsFileName)tool = new ForwardTool()tool->InitializeExternalLoads(…)

Process parameters

new Storage(1000, “statesStore”) Start of simulation

model->computeEquilibriumForAuxiliaryStates(x0)initialStatesStore->getData(…)model->setInitialStates(x0)

Initialize conditions

Page 10: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

How does Simulink interact with OpenSim?Initialize Model

Calculate time of next sample hit

Update states

Calculate derivatives

Calculate outputs

Calculate derivatives

Locate zero crossings

Sim

ulat

ion

loop

Integration(minor time step)

End Simulation

Calculate outputs statesStore->append(ssGetT(S), ssGetNumContStates(S), x)

Page 11: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

How does Simulink interact with OpenSim?Initialize Model

Calculate time of next sample hit

Calculate outputs

Calculate derivatives

Calculate outputs

Calculate derivatives

Locate zero crossings

Sim

ulat

ion

loop

Integration(minor time step)

End Simulation

Update states model->setStates(x) Sync OpenSim model with Simulink block

Page 12: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

How does Simulink interact with OpenSim?Initialize Model

Calculate time of next sample hit

Calculate outputs

Update states

Calculate outputs

Calculate derivatives

Locate zero crossings

Sim

ulat

ion

loop

Integration(minor time step)

End Simulation

Calculate derivatives

controlSet->getControlValues(…) If controls from file

model->set(t, u, x)model->getDerivCallbackSet()->set(t, u, x)

Time, controls, states

model->getActuatorSet()->computeActuation()model->getDerivCallbackSet()->computeActuation(t, u, x)model->getActuatorSet()->apply()model->getDerivCallbackSet()->applyActuation(t, u, x)

Actuation

model->getContactSet()->computeContact()model->getDerivCallbackSet()->computeContact(t, u, x)model->getContactSet()->apply()model->getDerivCallbackSet()->applyContact(t, u, x)

Contact

model->computeDerivatives(dx)model->getDerivCallbackSet()->computeDerivatives(…)

Derivatives

Page 13: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

How does Simulink interact with OpenSim?

Calculate time of next sample hit

Update states

Calculate derivatives

Calculate derivatives

Locate zero crossings

Sim

ulat

ion

loop

Integration(minor time step)

End Simulation

Initialize Model

Calculate outputs

Calculate outputs

Page 14: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

How does Simulink interact with OpenSim?

Calculate time of next sample hit

Update states

Locate zero crossings

Sim

ulat

ion

loop

Integration(minor time step)

End Simulation

Initialize Model

Calculate outputs

Calculate outputs

Calculate derivatives

Calculate derivatives

Page 15: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

Integration(minor time step)

How does Simulink interact with OpenSim?

Calculate time of next sample hit

Calculate outputs

Update states

Calculate derivatives

Calculate outputs

Calculate derivatives

Locate zero crossings

Sim

ulat

ion

loop

Initialize Model

End Simulation

model->getDynamicsEngine().convertRadiansToDegrees(…)statesStore->print(…) Save results

Page 16: OpenSim Matlab July2010 PDF

OpenSim Developer Jamboree July 2010

Examples

Falling Cube Tug-of-War Elbow Flexion

Swing Phase of Gait

Pole Balancing