Code: Select all
%% Import OpenSim Libraries
import org.opensim.modeling.*
%% Define the Model File Path.
model_path = 'DynamicWalkerModel.osim';
%% Instantiate the Model
newModel = Model(model_path);
newModel.setName('WalkerModel_cuff');
%% Define the Cuff mesh name
meshPath = 'Cuff1.obj';
%% Add Bodies, joints and forces for the cuff
% Make Cuff Bodies
mass = 0.0001;
massCenter = Vec3(0);
inertia = Inertia(1,1,.0001,0,0,0);
leftCuff = Body('LeftCuff', mass , massCenter, inertia);
% Add visual objects of the Cuff
leftCuff.attachGeometry( Mesh(meshPath) );
% Get a reference to the shank bodies
leftShank= newModel.getBodySet().get('LeftShank');
% Make Weld Joints for the Cuff
locationInParent = Vec3(0.075,-0.2,0);
orientationInParent = Vec3(0);
locationInChild = Vec3(0);
orientationInChild = Vec3(0);
cuff_l = WeldJoint('cuff_l',leftShank, locationInParent, orientationInParent, leftCuff, locationInChild, orientationInChild);
% Add the body and joint to the model
newModel.addBody(leftCuff);
newModel.addJoint(cuff_l);
% Define Coordinate Limit Force Parameters
upperStiffness = 0.5;
lowerStiffness = 0.5;
kneeUpperLimit = 0;
kneeLowerLimit = -140;
hipUpperLimit = 100;
hipLowerLimit = -100;
damping = 0.025;
transition = 5;
% Make a Left Cuff Coordinate Limit Force
LCuffForce = CoordinateLimitForce();
LCuffForce.setName('LCuffForce')
LCuffForce.set_coordinate('RHip_rz');
LCuffForce.setUpperStiffness(upperStiffness);
LCuffForce.setLowerStiffness(lowerStiffness);
LCuffForce.setUpperLimit(hipUpperLimit);
LCuffForce.setLowerLimit(hipLowerLimit);
LCuffForce.setDamping(damping);
LCuffForce.setTransition(transition)
% Add forces.
newModel.addForce(LCuffForce);
%% finalize connections
newModel.finalizeConnections()
%% Print the model to file.
newFilename = strrep(model_path, '.osim', '_CustomCuff.osim');
isSuccessful = newModel.print(newFilename);
if (~isSuccessful), error('Model printed to file failed'); end
fprintf('Model printed to file successfully\n');