Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This PR adds an example of geting the inertial parameters of a cuboid on CoppeliaSim #10

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

ffasilva
Copy link
Member

@ffasilva ffasilva commented Feb 16, 2024

Hi, @dqrobotics/developers,

This PR adds an example of geting the inertial parameters of a cuboid on CoppeliaSim. The motivation is to test the new methods added to DQ_VrepInterface in 109.

The LUA child script on the scene cuboid_inertial_parameters.ttt diverges from the one used in the C++ examples. As they were, functions get_center_of_mass() and get_inertia() only returned the parameters with respect to either the shape frame or the inertial reference frame through an if-else check "is(reference frame == 'absolute_frame')". To allow arbitrary reference frames, the LUA function sim.getObjectMatrix needs to receive a handle (i.e., an int), which required the modifications. Namely, the reference frame is now passed as an inInts rather than an inStrings. I also removed unused arguments, renamed some variables, and added comments to make the functions clearer.

Kind regards,
Frederico

P.S.: As described in the .m file, the necessary scene is available in OSF.

@ffasilva
Copy link
Member Author

Hi @juanjqo,

I modified the Lua API to behave the same as the one you implemented for your C++ examples, while still allowing for arbitrary reference frames. Since the Lua script is somewhat of an add-on to the library, I believe having a slightly different version in some examples is not a problem.

Kind regards,
Frederico

@juanjqo
Copy link
Member

juanjqo commented Sep 13, 2024

@ffasilva thank you for your example. I recommend using try-catch blocks to finish the connection automatically if an execution error happens, especially when using the legacy remote API. Otherwise, the connection port remains open, and the user must close it manually or restart Matlab.

Layout example:

clear all;
close all;
clc;

include_namespace_dq;

vi = DQ_VrepInterface();

try
    vi.connect('127.0.0.1', 19997);
    vi.start_simulation();
    %-----------Your code here-------------------

    %----------------------------------------------
    vi.stop_simulation();
    vi.disconnect();

catch ME
    vi.stop_simulation();
    vi.disconnect();
    rethrow(ME)
end

On the other hand, it looks like there is some bug when using BODY_FRAME arguments.

Minimal example:

clear all;
close all;
clc;

include_namespace_dq;

vi = DQ_VrepInterface();

try
   vi.connect('127.0.0.1', 19997);
   vi.start_simulation();

   % Get the Cuboid's handle
   handle = vi.get_handle('Cuboid');
   % Get the Cuboid's inertia matrix
   inertia_matrix_in_sf = vi.get_inertia_matrix(handle, vi.BODY_FRAME);
   disp('inertia matrix =')
   disp(inertia_matrix_in_sf)

   vi.stop_simulation();
   vi.disconnect();

catch ME
   vi.stop_simulation();
   vi.disconnect();
   rethrow(ME)
end

Output:

Arrays have incompatible sizes for this operation.

Error in DQ_VrepInterface/get_inertia_matrix (line 903)
               if(reference_frame == obj.ABSOLUTE_FRAME)

Error in untitled2 (line 18)
   inertia_matrix_in_sf = vi.get_inertia_matrix(handle,
   vi.BODY_FRAME);

Related documentation

Please let me know if you can reproduce the issue.

Best regards,

Juancho

@ffasilva
Copy link
Member Author

Hi @juanjqo

@ffasilva thank you for your example. I recommend using try-catch blocks to finish the connection automatically if an execution error happens, especially when using the legacy remote API. Otherwise, the connection port remains open, and the user must close it manually or restart Matlab.

I didn't add a try-catch block because I'm already checking for any previous connection that wasn't properly closed (e.g., due to an execution error).

%% V-REP setup
% Create a DQ_VrepInterface object and start communication with V-REP
vi = DQ_VrepInterface();

% Finish any previous V-REP communication
vi.stop_simulation();
vi.disconnect_all();

% Start a new connection
vi.connect('127.0.0.1',19997);
disp('Communication established!')

On the other hand, it looks like there is some bug when using BODY_FRAME arguments.

Thanks for pointing that out. I fixed it in d29125d.

Feel free to let me know if you have any more suggestions.

Kind regards,
Frederico

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants