Skip to content

Updating MATLAB Toolbox Wrapping + Integration #54

@mattking-smith

Description

@mattking-smith

Description

I have been working on using the current GTSAM wrap repository for integrating this toolbox into MATLAB for research purposes. I was able to successfully do this some time ago on another development computer, but the wrap repository has changed since then, and I am now facing a segmentation fault errors in the MATLAB environment, when running a few of the examples, post toolbox wrapping.

Additionally, I hope this issue provides information for new documentation on an up-to-date MATLAB + GPMP2 toolbox wrapping procedure on the README.md.

Steps to reproduce current wrapping method

Wrapping GTSAM to MATLAB Toolbox

  1. Build GTSAM from source, clone or download the latest release from the GTSAM Github repo. Then follow the build & install instructions posted on GTSAM build page.
  2. To flag the MATLAB toolbox for to build in the \home\user\ directory in the folder \toolbox, before the build process, in the terminal, run the command
cmake -DGTSAM_TOOLBOX_INSTALL_PATH:PATH= $HOME/toolbox ..`
  1. Install GTSAM by running the command make install
  2. Follow the step given in the MATLAB Wrapper documentation.

Wrapping GPMP2 to MATLAB Toolbox

  1. In the command prompt run
git clone https://github.com/gtrll/gpmp2.git 
  1. Download and install the current matlab.h from the GTSAM wrap repository into a wrap folder within the gpmp2 folder.
sudo mkdir wrap
  1. Noting the MATLAB wrapper vdocumentation in the current GTSAM wrap repository , and after some discussion with the developers of the wrap repository I needed to make edits to the CMakeLists.txt in the main gpmp2 repository. Specifically, I changes the following lines:

    gpmp2/CMakeLists.txt

    Lines 65 to 74 in 48c41c1

    # Wrapping to MATLAB
    if(GPMP2_BUILD_MATLAB_TOOLBOX)
    # wrap
    include(GtsamMatlabWrap)
    wrap_and_install_library(gpmp2.h ${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}" "")
    # install matlab functions and scripts
    add_subdirectory(matlab)
    endif()

    to
# Find current wrap repository
find_package(gtwrap)

# Wrapping to MATLAB
if(GPMP2_BUILD_MATLAB_TOOLBOX)

  include(MatlabWrap)
  matlab_wrap(gpmp2.h ${PROJECT_NAME} "gtsam;${PROJECT_NAME}" "" "" "")
  
  # install matlab functions and scripts
  add_subdirectory(matlab)
endif()
  1. To flag for installing the MATLAB toolbox and a path for the toolbox to be installed (I have chosen to install in the /home/user/toolbox directory), in the command prompt run
cd gpmp2 && mkdir build && cd build
sudo cmake -DGPMP2_BUILD_MATLAB_TOOLBOX:=ON -DWRAP_TOOLBOX_INSTALL_PATH=$HOME/gpmp2_toolbox ..
  1. Now executing sudo make install in the command prompt resulted initially in the following errors:
In file included from /usr/local/gpmp2/gpmp2/kinematics/GaussianPriorWorkspacePositionArm.h:12,
                 from /usr/local/gpmp2/build/wrap/gpmp2/gpmp2_wrapper.cpp:20:
/usr/local/gpmp2/gpmp2/kinematics/GaussianPriorWorkspacePosition.h: In member function ‘gtsam::Vector gpmp2::GaussianPriorWorkspacePosition<ROBOT>::evaluateError(const Pose&, boost::optional<Eigen::Matrix<double, -1, -1>&>) const’:
/usr/local/gpmp2/gpmp2/kinematics/GaussianPriorWorkspacePosition.h:66:28: error: ‘gtsam::Point3’ {aka ‘class Eigen::Matrix<double, 3, 1>’} has no member named ‘vector’
   66 |       return curr_position.vector() - des_position_.vector();
      |                            ^~~~~~
/usr/local/gpmp2/gpmp2/kinematics/GaussianPriorWorkspacePosition.h:66:53: error: ‘const Point3’ {aka ‘const class Eigen::Matrix<double, 3, 1>’} has no member named ‘vector’
   66 |       return curr_position.vector() - des_position_.vector();
      |                                                     ^~~~~~
/usr/local/gpmp2/gpmp2/kinematics/GaussianPriorWorkspacePosition.h:69:46: error: ‘const Point3’ {aka ‘const class Eigen::Matrix<double, 3, 1>’} has no member named ‘vector’
   69 |       return joint_pos[joint_].translation().vector() - des_position_.vector();
      |                                              ^~~~~~
/usr/local/gpmp2/gpmp2/kinematics/GaussianPriorWorkspacePosition.h:69:71: error: ‘const Point3’ {aka ‘const class Eigen::Matrix<double, 3, 1>’} has no member named ‘vector’
   69 |       return joint_pos[joint_].translation().vector() - des_position_.vector();
      |                                                                       ^~~~~~
.
.
.

make[2]: *** [CMakeFiles/gpmp2_matlab_wrapper.dir/build.make:79: CMakeFiles/gpmp2_matlab_wrapper.dir/wrap/gpmp2/gpmp2_wrapper.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:401: CMakeFiles/gpmp2_matlab_wrapper.dir/all] Error 2
make: *** [Makefile:141: all] Error 2

which I believe is an error related to an open pull request.

Specifically, it appears that Eigen and Pose Tranform have changed in GTSAM and those changes need to be updated in a few key header files. Without specifically without naming all of the header files I changed here in this issues, as I will open a pull request after this issue submission, note that I had to remove the functions .vector() and .print() in order to successfully compile and wrap.

  1. After removing .vector() and .print() from key header files, again run sudo make install in the command prompt.
  2. Note that for my system I was originally having a mex linker problem as discussed on the GTSAM MATLAB section, so in order to satisfy MATLAB when using GTSAM + GPMP2 once wrapped, I run the following at the command line to open MATLAB
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6; matlab
  1. Once in MATLAB make sure to add the file paths to the toolboxes
addpath('/home/user/toolbox');
addpath('/home/user/gpmp2_toolbox');

which should be sufficient for running the toolbox with both GTSAM + GPMP2.

Outstanding Issues

Currently I am able to successfully run a handful of examples without issue, like PointRobot2DFactorGraphExample.m, but I am getting a segmentation fault in one crucial function, arm.forwardKinematicsPosition() (which is called in many plotting functions likes PlotPlanarArm.m), is possibly related to the altering of the header files necessary for building, as I was mentioning previously.

For instance, if I run >> Arm3FactorGraphExample in the MATLAB command prompt, I get the following segmentation error:

------------------------------------------------
MATLAB Log File
------------------------------------------------ 


--------------------------------------------------------------------------------
          Segmentation violation detected at 2021-12-12 11:30:43 -0500
--------------------------------------------------------------------------------

Configuration:
  Crash Decoding           : Disabled - No sandbox or build area path
  Crash Mode               : continue (default)
  Default Encoding         : UTF-8
  Deployed                 : false
  Desktop Environment      : ubuntu:GNOME
  GNU C Library            : 2.31 stable
  Graphics Driver          : Intel Mesa Intel(R) HD Graphics 5500 (BDW GT2) Version 4.6 (Compatibility Profile) Mesa 21.0.3
  Graphics card 1          : Not Started 0x10de ( 0x10de ) 0x1346 Version 0.0.0.0 (0-0-0)
  Graphics card 2          : 0x8086 ( 0x8086 ) 0x1616 Version 0.0.0.0 (0-0-0)
  Java Version             : Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
  MATLAB Architecture      : glnxa64
  MATLAB Entitlement ID    : 2075860
  MATLAB Root              : /usr/local/MATLAB/R2021a
  MATLAB Version           : 9.10.0.1684407 (R2021a) Update 3
  OpenGL                   : hardware
  Operating System         : Ubuntu 20.04.3 LTS
  Process ID               : 91269
  Processor ID             : x86 Family 6 Model 61 Stepping 4, GenuineIntel
  Session Key              : f4ed3dee-2797-450d-91e9-c762785e1bfc
  Static TLS mitigation    : Enabled: Full
  Window System            : The X.Org Foundation (12011000), display :0

Fault Count: 1


Abnormal termination:
Segmentation violation

Current Thread: 'MCR 0 interpret' id 140577137805056

Register State (from fault):
  RAX = 00007fda2bb51ac0  RBX = 00007fdaaa5c6440
  RCX = 0000000000000000  RDX = 0000000000000018
  RSP = 00007fdaaa5c6078  RBP = 00007fdaaa5c6590
  RSI = 00007fda29c821d0  RDI = 00007fda2bb51ac0

   R8 = 00007fdaaa5c64a0   R9 = 00007fdaaa5c6480
  R10 = 00007fdaaa5c6020  R11 = 0000000000000000
  R12 = 0000000000000000  R13 = 00007fdaaa5c6690
  R14 = 00007fd9d7d2da70  R15 = 00007fdaaa5c65f0

  RIP = 00007fd9cf34a15a  EFL = 0000000000010206

   CS = 0033   FS = 0000   GS = 0000

Stack Trace (from fault):
[  0] 0x00007fd9cf34a15a                       /usr/local/lib/libgpmp2.so.0+00528730 _ZN5Eigen8internal21compute_inverse_size4ILi1EdNS_6MatrixIdLi4ELi4ELi0ELi4ELi4EEES3_E3runERKS3_RS3_+00001354
[  1] 0x00007fd9cf346586                       /usr/local/lib/libgpmp2.so.0+00513414
.
.
.

The forwardKinematicsPosition() is defined in the ForwardKinematics-inl.h, where we can see .vector() is used as follows:

jpx[i].translation().vector()).finished();

jpx_mat.col(i) = jpx[i].translation().vector();

So in my code, I explicitly removed .vector() from each of these lines in order to successfully compile and wrap. However now whenever MATLAB tries to execute arm.forwardKinematicsPosition() I get a segmentation faults when I get the program gets to a custom wrapper definition generated in the toolbox:

    function varargout = forwardKinematicsPosition(this, varargin)
      % FORWARDKINEMATICSPOSITION usage: forwardKinematicsPosition(Pose2Vector jp) : returns Matrix
      % Doxygen can be found at https://gtsam.org/doxygen/
      if length(varargin) == 1 && isa(varargin{1},'gpmp2.Pose2Vector')
        varargout{1} = gpmp2_wrapper(56, this, varargin{:});
        return
      end
      error('Arguments do not match any overload of function gpmp2.Pose2MobileArm.forwardKinematicsPosition');
    end

I am sure this is result of editing ForwardKinematics-inl.h](https://github.com/gtrll/gpmp2/blob/main/gpmp2/kinematics/ForwardKinematics-inl.h), but I am not sure what edits need to made to allow me to compile + wrap and work in MATLAB.

Any suggestions or edits on this matter would be greatly appreciated.

Also if someone could verify this install method, that would be beneficial too.

Environment

Linux OS: Ubuntu 20.04
MATLAB version: 2021a
Standard GTSAM + GPMP2 + MATLAB Toolbox build procedure.

Additional information

I do current have a fully working (i.e. no segmentation faults on any GPMP2 example) development computer which has successfully wrapped and working GTSAM + GPMP2 for the MATLAB environment, but that was done some time ago and that code is out of sync with GPMP2. Hence I have not pulled the current repositories onto the development computer and have been testing this MATLAB toolbox integration on a different computer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions