Thank you for your help, Dimitar. I was able to base the structure off of your example code.
It requires the mexplus macros found here: https://github.com/kyamagu/mexplus
You create a class in Matlab to interface with mex, which I have included here:
Code: Select all
classdef gravity_engine < handle
properties (Access = private)
id_ % ID of the session
end
methods
function this = gravity_engine()
%GravityEngine Create a new
this.id_ = GravityEngine('new');
end
function delete(this)
%delete Destructor
GravityEngine('delete', this.id_);
end
function setup(this, fileName)
assert(isscalar(this));
assert(ischar(fileName));
GravityEngine('setup', this.id_, fileName)
end
function updateState(this, stateVec)
assert(isscalar(this));
GravityEngine('updateState', this.id_, stateVec);
end
function newQ = getStateCoords(this)
assert(isscalar(this));
newQ = GravityEngine('getStateCoords', this.id_);
end
function gravForces = calcGravForces(this)
assert(isscalar(this));
gravForces = GravityEngine('calcGravForces', this.id_);
end
end
end
Code: Select all
mex -I"C:/Simbody/include" -I"C:/OpenSim 4.0/sdk/include" -L"C:/Simbody/lib" -lSimTKcommon -lSimTKsimbody -lSimTKmath -L"C:/OpenSim 4.0/sdk/lib" -losimSimulation -losimTools -losimCommon -losimAnalyses -losimActuators GravityEngine.cpp
Code: Select all
% get handle to mex object
obj = gravity_engine();
%load and initialize your model
obj.setup(filePath);
%update the independent coordinates
obj.updateState(qVec);
%get the current coordinates (joint angles)
qVecNew = obj.getStateCoords();
%get the vector of generalized forces/torques due to gravity in ground frame
gravF = obj.calcGravForces();
% releases the object instance
obj.delete
clear obj;