OpenSim/Matlab interfacing
- hamed kouchebaghi
- Posts: 47
- Joined: Thu Nov 14, 2019 12:11 pm
OpenSim/Matlab interfacing
Hello
How can I load "Arm26" model in MATLAB?
Can you help me with this?
Best Regards.
Hamed
How can I load "Arm26" model in MATLAB?
Can you help me with this?
Best Regards.
Hamed
Tags:
- Ayman Habib
- Posts: 2248
- Joined: Fri Apr 01, 2005 12:24 pm
Re: OpenSim/Matlab interfacing
Hello,
Please consult the documentation page at
https://simtk-confluence.stanford.edu/d ... ith+Matlab
Best regards,
-Ayman
Please consult the documentation page at
https://simtk-confluence.stanford.edu/d ... ith+Matlab
Best regards,
-Ayman
- hamed kouchebaghi
- Posts: 47
- Joined: Thu Nov 14, 2019 12:11 pm
Re: OpenSim/Matlab interfacing
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
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
- Thomas Uchida
- Posts: 1793
- Joined: Wed May 16, 2012 11:40 am
Re: OpenSim/Matlab interfacing
Here is the code:now I have difficulty in loading the Arm26 model.
Code: Select all
import org.opensim.modeling.*
myModel = Model('arm26.osim');
- hamed kouchebaghi
- Posts: 47
- Joined: Thu Nov 14, 2019 12:11 pm
Re: OpenSim/Matlab interfacing
Hello,tkuchida wrote: ↑Mon Mar 09, 2020 3:55 amHere is the code:now I have difficulty in loading the Arm26 model.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.Code: Select all
import org.opensim.modeling.* myModel = Model('arm26.osim');
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()
Code: Select all
>> methods(Model)
Code: Select all
methodsview(myModel)
Code: Select all
>>import org.opensim.modeling.*
myModel = Model('arm26.osim');
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
- Thomas Uchida
- Posts: 1793
- Joined: Wed May 16, 2012 11:40 am
Re: OpenSim/Matlab interfacing
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.Cannot open file arm26.osim. It may not exist or you do not have permission to read it.
- hamed kouchebaghi
- Posts: 47
- Joined: Thu Nov 14, 2019 12:11 pm
Re: OpenSim/Matlab interfacing
tkuchida wrote: ↑Thu Mar 12, 2020 3:05 pmThis 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.Cannot open file arm26.osim. It may not exist or you do not have permission to read it.
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?
- Ayman Habib
- Posts: 2248
- Joined: Fri Apr 01, 2005 12:24 pm
Re: OpenSim/Matlab interfacing
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
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
- hamed kouchebaghi
- Posts: 47
- Joined: Thu Nov 14, 2019 12:11 pm
Re: OpenSim/Matlab interfacing
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);
I want to know how I can animate Arm26 in MATLAB. Is that possible at all?
Thanks and the Best.
Hamed