Matlab crashing when creating multiple custom joints (SOLVED)

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Samuele Gould
Posts: 16
Joined: Tue Nov 19, 2019 5:53 am

Matlab crashing when creating multiple custom joints (SOLVED)

Post by Samuele Gould » Tue Apr 19, 2022 4:55 am

Dear OpenSim users,

I am using the Matlab API to create a model. When I run the code it 'successfully' creates the model as I expect, I can open the model in OpenSim and the properties seem correct there, likewise if I look at the model code if I open it in a text editor. However, about 30-60s after running the whole code Matlab crashes without error messages (but it sometimes offers to send a report to Matlab).

I have managed to isolate the problem to a specific for loop (which I have put below) where I am creating custom joints (+ transformAxis and SpatialTransform) iteratively using the Matlab API.

Code: Select all

% Prepare OpenSim objects needed to create custom joint
linearFunction = LinearFunction(1,0);
% Create custom joints between the correct bodies - locations are incorrect
% are corrected later
numberOfJoints = max(size(jointNames));
for ii = 2:numberOfJoints
    %
    % Create transform axes
    % Rotation about X
    tformAxisXR = TransformAxis(ArrayStr(strcat(jointNames(ii),'RotationX')),Vec3(1,0,0));
    tformAxisXR.set_coordinates(0,strcat(jointNames(ii),'RotationX'));
    tformAxisXR.setFunction(linearFunction); % setting of function required to enable motion between bodies
    % Rotation about Y
    tformAxisYR = TransformAxis(ArrayStr(strcat(jointNames(ii),'RotationY')),Vec3(0,1,0));
    tformAxisYR.set_coordinates(0,strcat(jointNames(ii),'RotationY'));
    tformAxisYR.setFunction(linearFunction);
    % Rotation about Z
    tformAxisZR = TransformAxis(ArrayStr(strcat(jointNames(ii),'RotationZ')),Vec3(0,0,1));
    tformAxisZR.set_coordinates(0,strcat(jointNames(ii),'RotationZ'));
    tformAxisZR.setFunction(linearFunction);
    % Translation in X
    tformAxisXT = TransformAxis(ArrayStr(strcat(jointNames(ii),'TranslationX')),Vec3(1,0,0));
    tformAxisXT.set_coordinates(0,strcat(jointNames(ii),'TranslationX'));
    tformAxisXT.setFunction(linearFunction);
    % Translation in Y
    tformAxisYT = TransformAxis(ArrayStr(strcat(jointNames(ii),'TranslationY')),Vec3(0,1,0));
    tformAxisYT.set_coordinates(0,strcat(jointNames(ii),'TranslationY'));
    tformAxisYT.setFunction(linearFunction);
    % Translation in Z
    tformAxisZT = TransformAxis(ArrayStr(strcat(jointNames(ii),'TranslationZ')),Vec3(0,0,1));
    tformAxisZT.set_coordinates(0,strcat(jointNames(ii),'TranslationZ'));
    tformAxisZT.setFunction(linearFunction);
    %
    % Create a spatial transform
    spatialTrans = SpatialTransform();
    spatialTrans.set_rotation1(tformAxisXR);
    spatialTrans.set_rotation2(tformAxisYR);
    spatialTrans.set_rotation3(tformAxisZR);
    spatialTrans.set_translation1(tformAxisXT);
    spatialTrans.set_translation2(tformAxisYT);
    spatialTrans.set_translation3(tformAxisZT);
    %
    % Create the joint for the specific body
    osimJoint.(jointNames(ii)) = CustomJoint(char(jointNames(ii)), ...
        osimBody.(KCB(ii-1)),zeroVec3, zeroVec3, ...
        osimBody.(KCB(ii)),zeroVec3, zeroVec3,spatialTrans);
    %
    % Add the joint to the model
    osimModel.addJoint(osimJoint.(jointNames(ii)));
    %
    % Update coordinate names to set the coordinate range
    % Rotations
    for jj=0:2
        coordRot = osimJoint.(jointNames(ii)).updCoordinate(jj);
        coordRot.setRange([deg2rad(-100), deg2rad(100)]);
    end
    % Translations
    for jj=3:5
        coordTrans = osimJoint.(jointNames(ii)).updCoordinate(jj);
        coordTrans.setRange([1, 1]);
    end
end
osimModel.finalizeConnections()
Creating the first joint (+ transformAxis and SpatialTransform) works and Matlab does not crash however if I look in the OpenSim log it introduces "[error Mesh path2Mesh\Mesh.stl not connected to model...ignoring.]" If I run the code without trying to add any joints only "[warning] Body 'X' not connected by a Joint.A FreeJoint will be added to connect it to ground." appears, as expected.

As soon as I add more than one joint (all 6 joints or just one more), Matlab crashes 30-60s after running the full code. I have tried running the code in a for loop or as separate calls to create each joint but that makes no difference (as expected).

Occasionally the full code will run once without Matlab crashing, however, running the code later (with any changes reverted) causes Matlab to crash.
I suspect it is a memory problem, so I have tried to clear the spatial transform and the transformation axis from the workspace but that does not help.
I saw others mention java.lang.System.gc() to clear memory but that also did not stop the crash from occurring.

Any suggestions appreciated.
Thanks,
Samuele

Tags:

User avatar
Samuele Gould
Posts: 16
Joined: Tue Nov 19, 2019 5:53 am

Re: Matlab crashing when creating multiple custom joints (SOLVED)

Post by Samuele Gould » Thu Apr 21, 2022 8:01 am

similar to the post found here:

viewtopicPhpbb.php?f=91&t=14067&p=40440&start=0&view=

the problem was related to assigning the function to the transform axis.
setFunction was causing the crash, changing setFunction to set_function resolved the problem.

This is because to the best of my understanding there is a slight difference in the inputs they take:
https://simtk.org/api_docs/opensim/api_ ... dcbbbe5868
see viewtopicPhpbb.php?f=91&t=9234&p=25377&start=0&view= for an explanation

POST REPLY