robotology / idyntree

Multibody Dynamics Library designed for Free Floating Robots
BSD 3-Clause "New" or "Revised" License
172 stars 67 forks source link

Use fullfile to concatenate paths in Matlab bindings #1125

Closed LoreMoretti closed 10 months ago

LoreMoretti commented 10 months ago

Consider the following pseudo-code in Matlab:

    model_path = 'home/user/urdf_path';
    file_name = 'urdf_file.urdf'; 
    considered_joints_names = {'hip_roll', 'hip_pitch'}
    base_link_name = 'root';
    debug_mode = false;

    kin_dyn = iDynTreeWrappers.loadReducedModel( ...  
        considered_joints_names, ...
        base_link_name, ...
        model_path, ...
        file_name, ...
        debug_mode);

This will fail with an error like:

[ERROR] ModelLoader :: loadModelFromFile : Error in parsing model from URDF.
[ERROR] Model :: computeFullTreeTraversal : requested traversalBase is out of bounds
[ERROR] Model :: getLinkIndex : Impossible to find link root in the Model
[ERROR] Model :: computeFullTreeTraversal : requested traversalBase is out of bounds

This is because model_path does not end with a /. In fact, changing the pseudo-code as follows:

    model_path = 'home/user/urdf_path/';
    file_name = 'urdf_file.urdf'; 
    considered_joints_names = {'hip_roll', 'hip_pitch'}
    base_link_name = 'root';
    debug_mode = false;

    kin_dyn = iDynTreeWrappers.loadReducedModel( ...  
        considered_joints_names, ...
        base_link_name, ...
        model_path, ...
        file_name, ...
        debug_mode);

fixes the issue.

Though, we could make it more robust against this type of errors changing the way paths are concatenated in https://github.com/robotology/idyntree/blob/f4df648372a33b5471a78cbec5874931004dc0ef/bindings/matlab/%2BiDynTreeWrappers/loadReducedModel.m#L47C5-L47C5.

I suggest to use the built-in function fullfile of Matlab.

traversaro commented 10 months ago

Fixed in https://github.com/robotology/idyntree/pull/1126 .