Rosbotran with ROS2

This tutorial explain how to create a robotran project compatible with ROS 2.

NOTE:

This tutorial has not been extensively tested. Contact us for more information info@robotran.be.

    1. Make sure that Robotran is installed
    1. Make sure that ROS 2 is installed
    1. Create or go to the src folder of your ROS workspace
    1. Create a new ROS 2 project in this folder with the following command:

ros2 pkg create --build-type ament_cmake --license Apache-2.0 <project-name>

    1. Create a new Robotran project with the same name as your ros project and save it
    1. Move all the folders from the Robotran project inside <project-name>/src. The architecture of your project should look like this:

<project-name>     ├── CMakeLists.txt     ├── include     │   └── <project-name>     ├── package.xml     ├── README.md     └── src         ├── animationR         │   └── vrml         ├── dataR         │   └── <project-name>.mbs         ├── resultsR         ├── symbolicR         ├── userfctR         └── workR             ├── CMakeLists.txt             └── src                 └── main.c

    1. Edit the main file CMakeLists.txt and :
    1. Add the rclcpp package with the other packages:
    find_package(rclcpp REQUIRED)
    
    1. Copy and paste after the find_package(...) lines all the lines from the src/workR/CMakeLists.txt between the lines:
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
    
    ...
    
    target_link_libraries(exe_${PROJECT_NAME} ${MBSYSC_LIBRARIES})
    
    1. Change the paths in the file(...) to be relative from this CMakeLists.txt:
    • Change from ../ to src/
    • Change from src/ to src/workR/src/
    1. Change the extension from file(GLOB MAIN_SRC ...) from c to cpp. Be advised, the c function must be defined in a extern c {} block.
    1. Change the include_directories commands to (add other include directories in the list if needed)
    include_directories(
       include
       src
       src/symbolicR
       src/userfctR
       src/workR/src
    )
    
    1. Remove the exe from the name of the executable:
    add_executable (exe_${PROJECT_NAME} ${MAIN_SRC})
    add_dependencies(exe_${PROJECT_NAME} Project_user Project_symbolic)
    target_link_libraries(exe_${PROJECT_NAME} ${MBSYSC_LIBRARIES})
    

    become

    add_executable (${PROJECT_NAME} ${MAIN_SRC})
    add_dependencies(${PROJECT_NAME} Project_user Project_symbolic)
    target_link_libraries(${PROJECT_NAME} ${MBSYSC_LIBRARIES})
    
    1. Add below (at the bottom of the file) those commands:
    ament_target_dependencies(
      <project-name>
      ament_index_cpp
    )
    
    install(TARGETS Project_user DESTINATION lib/${PROJECT_NAME})
    install(TARGETS Project_symbolic DESTINATION lib/${PROJECT_NAME})
    install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
    
    # To be able to find the non-compiled files
    install(DIRECTORY src DESTINATION share/${PROJECT_NAME})
    
    1. Remove the file src/workR/CMakeLists.txt
    1. Change the extension of the file src/workR/src/main.c to cpp
    1. Update the main.cpp file of the robotran project
    1. Add the header:
    #include <ament_index_cpp/get_package_share_directory.hpp>
    
    1. Allow the compatibility between C and CPP of the header files with the extern "C":
    extern "C" {
      #include "mbs_data.h"
      #include "mbs_part.h"
      #include "mbs_equil.h"
      #include "mbs_modal.h"
      #include "mbs_dirdyn.h"
      #include "integrator.h"
      #include "mbs_invdyn.h"
      #include "mbs_solvekin.h"
    
      #include "mbs_message.h"
      #include "mbs_loader.h"
      #include "mbs_set.h"
    }
    
    1. Change the loading code of the project:
    // Project name of ROS + robotran
    std::string project_name = "rosbotran";
    
    // Find package path
    std::string package_path = ament_index_cpp::get_package_share_directory(project_name);
    
    // Specify path for the libraries
    std::string project_path = package_path + "/src/dataR/" + project_name + ".mbs";
    // previous code was this but I (ST) modified it 25/2/25: std::string lib_path = "install/" + project_name + "/lib/" + project_name + "/";
    std::string lib_path = package_path + "/../../lib/" + project_name + "/";
    
    // Load the project with the libraries
    MbsData *mbs_data;  // if not already declared
    MbsLoader * mbs_loader =    mbs_new_loader();
    
    mbs_loader->opts->symbolicLib_path = (char*) lib_path.c_str();
    mbs_loader->opts->userfctLib_path= (char*) lib_path.c_str();
    
    mbs_data = mbs_load_with_loader(project_path.c_str(),  mbs_loader);
    
    1. Change the path of the output of robotran (respath and animpath) as they are relative to the location of the executable (this step is not clear, you perhaps need to test by trial and error):
    "../../src/<project-name>/resultsR/dirdyn_q.res"
    
    1. Generate all the files for Robotran
    1. Build the project with command colcon build in the root directory of your ros workspace. You can use “colcoon build –packages-select ” to only rebuild your package.
    1. Test your code by running your node ros2 run <project-name> <project-name>