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.
- Make sure that Robotran is installed
- Make sure that ROS 2 is installed
- Create or go to the
src
folder of your ROS workspace
- Create or go to the
- 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>
- Create a new Robotran project with the same name as your ros project and save it
- Move all the folders from the Robotran project inside
<project-name>/src
. The architecture of your project should look like this:
- Move all the folders from the Robotran project inside
<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
- Edit the main file
CMakeLists.txt
and :
- Edit the main file
- Add the rclcpp package with the other packages:
find_package(rclcpp REQUIRED)
- Copy and paste after the
find_package(...)
lines all the lines from thesrc/workR/CMakeLists.txt
between the lines:
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) ... target_link_libraries(exe_${PROJECT_NAME} ${MBSYSC_LIBRARIES})
- Copy and paste after the
- Change the paths in the
file(...)
to be relative from thisCMakeLists.txt
:
- Change from
../
tosrc/
- Change from
src/
tosrc/workR/src/
- Change the paths in the
- Change the extension from
file(GLOB MAIN_SRC ...)
fromc
tocpp
. Be advised, the c function must be defined in a extern c {} block.
- Change the extension from
- 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 )
- Change the
- 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})
- Remove the
- 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})
- Remove the file
src/workR/CMakeLists.txt
- Remove the file
- Change the extension of the file
src/workR/src/main.c
tocpp
- Change the extension of the file
- Update the
main.cpp
file of the robotran project
- Update the
- Add the header:
#include <ament_index_cpp/get_package_share_directory.hpp>
- Allow the compatibility between
C
andCPP
of the header files with theextern "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" }
- Allow the compatibility between
- 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);
- 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"
- Generate all the files for Robotran
- 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.
- Build the project with command
- Test your code by running your node
ros2 run <project-name> <project-name>
- Test your code by running your node