OpenSim/Matlab interfacing

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
hamed kouchebaghi
Posts: 47
Joined: Thu Nov 14, 2019 12:11 pm

OpenSim/Matlab interfacing

Post by hamed kouchebaghi » Sat Mar 07, 2020 12:50 pm

Hello
How can I load "Arm26" model in MATLAB?


Can you help me with this?
Best Regards.
Hamed

Tags:

User avatar
Ayman Habib
Posts: 2248
Joined: Fri Apr 01, 2005 12:24 pm

Re: OpenSim/Matlab interfacing

Post by Ayman Habib » Sat Mar 07, 2020 8:55 pm

Hello,

Please consult the documentation page at
https://simtk-confluence.stanford.edu/d ... ith+Matlab

Best regards,
-Ayman

User avatar
hamed kouchebaghi
Posts: 47
Joined: Thu Nov 14, 2019 12:11 pm

Re: OpenSim/Matlab interfacing

Post by hamed kouchebaghi » Sun Mar 08, 2020 1:08 am

Hello,

Many thanks in advance for your attention.
Actually I have done the steps in the link you aforementioned.
But now I have difficulty in loading the Arm26 model. I was trying to find an instruction or an example that could explain.

Best,
Hamed

User avatar
Thomas Uchida
Posts: 1793
Joined: Wed May 16, 2012 11:40 am

Re: OpenSim/Matlab interfacing

Post by Thomas Uchida » Mon Mar 09, 2020 3:55 am

now I have difficulty in loading the Arm26 model.
Here is the code:

Code: Select all

import org.opensim.modeling.*
myModel = Model('arm26.osim');
You will need to have set up your Matlab scripting environment (see the "Setting up your Matlab Scripting Environment" here: https://simtk-confluence.stanford.edu/d ... ith+Matlab) and the "arm26.osim" file must be accessible (e.g., in Matlab's working directory). If you are encountering other issues/errors, it would be helpful to provide a description.

User avatar
hamed kouchebaghi
Posts: 47
Joined: Thu Nov 14, 2019 12:11 pm

Re: OpenSim/Matlab interfacing

Post by hamed kouchebaghi » Thu Mar 12, 2020 2:47 pm

tkuchida wrote:
Mon Mar 09, 2020 3:55 am
now I have difficulty in loading the Arm26 model.
Here is the code:

Code: Select all

import org.opensim.modeling.*
myModel = Model('arm26.osim');
You will need to have set up your Matlab scripting environment (see the "Setting up your Matlab Scripting Environment" here: https://simtk-confluence.stanford.edu/d ... ith+Matlab) and the "arm26.osim" file must be accessible (e.g., in Matlab's working directory). If you are encountering other issues/errors, it would be helpful to provide a description.
Hello,

many thanks for your help.
I created the OpenSim-Matlab environment using the instructions givven.
When running this code

Code: Select all

>>org.opensim.modeling.opensimCommon.GetVersion()
in MATLAB, I can see the version of the OpenSim configured with MATLAB. Also I can see the list of the methods when running

Code: Select all

>> methods(Model)
and

Code: Select all

methodsview(myModel)
Then I tried to run the following code

Code: Select all

>>import org.opensim.modeling.*
myModel = Model('arm26.osim');
but i would face this error:

Java exception occurred:
java.io.IOException: Object: Cannot open file arm26.osim. It may not exist or you do not have
permission to read it.
Thrown at object.cpp:105 in Object().

at org.opensim.modeling.opensimSimulationJNI.new_Model__SWIG_1(Native Method)

at org.opensim.modeling.Model.<init>(Model.java:783)


What should I do now?
Can you help me please.

Thanks,
Hamed

User avatar
Thomas Uchida
Posts: 1793
Joined: Wed May 16, 2012 11:40 am

Re: OpenSim/Matlab interfacing

Post by Thomas Uchida » Thu Mar 12, 2020 3:05 pm

Cannot open file arm26.osim. It may not exist or you do not have permission to read it.
This error means that the file cannot be opened. It may not exist (perhaps you don't have the model on your hard drive; see viewtopicPhpbb.php?f=91&t=11565). If it is on your hard drive, you will need to change Matlab's working directory to the directory containing the file, or copy the file into Matlab's working directory, or provide the full path to the file between the single quotes.

User avatar
hamed kouchebaghi
Posts: 47
Joined: Thu Nov 14, 2019 12:11 pm

Re: OpenSim/Matlab interfacing

Post by hamed kouchebaghi » Thu Mar 12, 2020 3:29 pm

tkuchida wrote:
Thu Mar 12, 2020 3:05 pm
Cannot open file arm26.osim. It may not exist or you do not have permission to read it.
This error means that the file cannot be opened. It may not exist (perhaps you don't have the model on your hard drive; see viewtopicPhpbb.php?f=91&t=11565). If it is on your hard drive, you will need to change Matlab's working directory to the directory containing the file, or copy the file into Matlab's working directory, or provide the full path to the file between the single quotes.

I just added Arm26 to my directory and, now the error is solved.
but nothing else happens. Should I see the Arm26 model loaded in my screen?

User avatar
Ayman Habib
Posts: 2248
Joined: Fri Apr 01, 2005 12:24 pm

Re: OpenSim/Matlab interfacing

Post by Ayman Habib » Thu Mar 12, 2020 3:42 pm

Hello,

Creating a model from a file, is not supposed to visualize the model, you just get access to the model object to do things with it. Please check the developers' guide to see what operations are available here:
https://simtk-confluence.stanford.edu/d ... %27s+Guide

Best regards,
-Ayman

User avatar
hamed kouchebaghi
Posts: 47
Joined: Thu Nov 14, 2019 12:11 pm

Re: OpenSim/Matlab interfacing

Post by hamed kouchebaghi » Sat Mar 14, 2020 12:49 pm

aymanh wrote:
Thu Mar 12, 2020 3:42 pm

Creating a model from a file, is not supposed to visualize the model
Best regards,
-Ayman
Hello,

thanks for your help, I checked the developers' guide, but I'm a beginner yet.
through this https://github.com/opensim-org/opensim-core I found the following code

Code: Select all

%% Import Java libraries
import org.opensim.modeling.*

arm = Model();
arm.setName('bicep_curl');
arm.setUseVisualizer(true);

% ---------------------------------------------------------------------------
% Create two links, each with a mass of 1 kg, centre of mass at the body's
% origin, and moments and products of inertia of zero.
% ---------------------------------------------------------------------------

humerus = Body('humerus',...
                    1.0,...
                    Vec3(0),...
                    Inertia(0, 0, 0));
radius = Body('radius',...
                   1.0,...
                   Vec3(0),...
                   Inertia(0, 0, 0));

% ---------------------------------------------------------------------------
% Connect the bodies with pin joints. Assume each body is 1m long.
% ---------------------------------------------------------------------------

shoulder = PinJoint('shoulder',...
                         arm.getGround(),... % PhysicalFrame
                         Vec3(0),...
                         Vec3(0),...
                         humerus,... % PhysicalFrame
                         Vec3(0, 1, 0),...
                         Vec3(0));

elbow = PinJoint('elbow',...
                      humerus,... % PhysicalFrame
                      Vec3(0),...
                      Vec3(0),...
                      radius,... % PhysicalFrame
                      Vec3(0, 1, 0),...
                      Vec3(0));

% ---------------------------------------------------------------------------
% Add a muscle that flexes the elbow (actuator for robotics people).
% ---------------------------------------------------------------------------

biceps = Millard2012EquilibriumMuscle('biceps',...  % Muscle name
                                           200.0,...  % Max isometric force
                                           0.6,...  % Optimal fibre length
                                           0.55,...  % Tendon slack length
                                           0.0);  % Pennation angle
biceps.addNewPathPoint('origin',...
                       humerus,...
                       Vec3(0, 0.8, 0));

biceps.addNewPathPoint('insertion',...
                       radius,...
                       Vec3(0, 0.7, 0));

% ---------------------------------------------------------------------------
% Add a controller that specifies the excitation of the muscle.
% ---------------------------------------------------------------------------

brain = PrescribedController();
brain.addActuator(biceps);
brain.prescribeControlForActuator('biceps',...
                                  StepFunction(0.5, 3.0, 0.3, 1.0));

% ---------------------------------------------------------------------------
% Build model with components created above.
% ---------------------------------------------------------------------------

arm.addBody(humerus);
arm.addBody(radius);
arm.addJoint(shoulder); % Now required in OpenSim4.0
arm.addJoint(elbow);
arm.addForce(biceps);
arm.addController(brain);

% ---------------------------------------------------------------------------
% Add a console reporter to print the muscle fibre force and elbow angle.
% ---------------------------------------------------------------------------

% We want to write our simulation results to the console.
reporter = ConsoleReporter();
reporter.set_report_time_interval(1.0);
reporter.addToReport(biceps.getOutput('fiber_force'));
elbow_coord = elbow.getCoordinate().getOutput('value');
reporter.addToReport(elbow_coord, 'elbow_angle');
arm.addComponent(reporter);

% ---------------------------------------------------------------------------
% Add display geometry. 
% ---------------------------------------------------------------------------

bodyGeometry = Ellipsoid(0.1, 0.5, 0.1);
bodyGeometry.setColor(Vec3(0.5)); % Gray
humerusCenter = PhysicalOffsetFrame();
humerusCenter.setName('humerusCenter');
humerusCenter.setParentFrame(humerus);
humerusCenter.setOffsetTransform(Transform(Vec3(0, 0.5, 0)));
humerus.addComponent(humerusCenter);
humerusCenter.attachGeometry(bodyGeometry.clone());

radiusCenter = PhysicalOffsetFrame();
radiusCenter.setName('radiusCenter');
radiusCenter.setParentFrame(radius);
radiusCenter.setOffsetTransform(Transform(Vec3(0, 0.5, 0)));
radius.addComponent(radiusCenter);
radiusCenter.attachGeometry(bodyGeometry.clone());

% ---------------------------------------------------------------------------
% Configure the model.
% ---------------------------------------------------------------------------

state = arm.initSystem();
% Fix the shoulder at its default angle and begin with the elbow flexed.
shoulder.getCoordinate().setLocked(state, true);
elbow.getCoordinate().setValue(state, 0.5 * pi);
arm.equilibrateMuscles(state);

% ---------------------------------------------------------------------------
% Configure the visualizer
% ---------------------------------------------------------------------------

viz = arm.updVisualizer().updSimbodyVisualizer();
viz.setBackgroundColor(Vec3(0)); % white
viz.setGroundHeight(-2)

% ---------------------------------------------------------------------------
% Simulate.
% ---------------------------------------------------------------------------

manager = Manager(arm);
state.setTime(0);
manager.initialize(state);
state = manager.integrate(10.0);
and when I run it in MATLAB command window, it produces an animation.
I want to know how I can animate Arm26 in MATLAB. Is that possible at all?

Thanks and the Best.
Hamed

POST REPLY