Page 1 of 1

console reporter

Posted: Fri Sep 11, 2020 11:58 pm
by hamed1994
Hello,

this is the code for an example of an arm model that produces an animation:

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);
the internet link for the model is https://github.com/opensim-org/opensim-core
it is mentioned in the example that "fiber_force" and "elbow_angle" must be printed to the console, but when I run it in MATLAB the animation runs but no information about the force nor the angle shows on the Command Window.
Should I change or add something in the code ?

thanks,
Hamed

Re: console reporter

Posted: Sat Sep 12, 2020 5:55 pm
by ongcf
Can you please provide what OS and version of OpenSim you are using?

We have seen this behavior in the past with Windows and MATLAB. If you are using OpenSim 4.1 or before and want a workaround, you can look for the output in your current working directory in the file "out.log".

Re: console reporter

Posted: Sun Sep 13, 2020 12:19 am
by hamed1994
Hello,

my working environment is: Windows 10 64bit, OpenSim 4.1 64bit, Matlab 2016a 64bit.
and this is the out.log file
Screenshot (47).png
Screenshot (47).png (205.3 KiB) Viewed 254 times
how can I fix that ?

thank you and Best,
Hamed

Re: console reporter

Posted: Mon Sep 14, 2020 1:28 am
by hamed1994
ongcf wrote:
Sat Sep 12, 2020 5:55 pm
Can you please provide what OS and version of OpenSim you are using?

We have seen this behavior in the past with Windows and MATLAB. If you are using OpenSim 4.1 or before and want a workaround, you can look for the output in your current working directory in the file "out.log".
hello, hope you having a good time.

the problem of error showing in the "out.log" file was solved by running MATLAB as administrator.
but still can't see the results printed and only have access through out.log.
I want to design a controller for this model because of that I think I have to get access to the results.
can you help me with this ?

thanks you,
Hamed