From 117d915aea6745f84eac00ed42163fa2a3b29d17 Mon Sep 17 00:00:00 2001 From: Diego Ferigo Date: Tue, 3 Apr 2018 14:11:41 +0200 Subject: [PATCH] Split toolbox components (library, simulink, coder) Fix https://github.com/robotology/wb-toolbox/issues/83 Fix https://github.com/robotology/wb-toolbox/issues/44 --- CMakeLists.txt | 10 +- toolbox/CMakeLists.txt | 306 +++++++++++++++++++++++++++++++---------- 2 files changed, 234 insertions(+), 82 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c69eb3af7..465cdc7f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,21 +2,19 @@ # Author: Jorhabib Eljaik, Francesco Romano, Diego Ferigo # CopyPolicy: Released under the terms of the GNU GPL v2.0. -project(WB-Toolbox) cmake_minimum_required(VERSION 3.5) +project(WB-Toolbox LANGUAGES CXX VERSION 3.1) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) +# Add custom functions / macros +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + find_package(YCM REQUIRED) include(YCMDefaultDirs) -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) -set(WB-TOOLBOX_SHARE_DIR "${CMAKE_INSTALL_PREFIX}/share/WB-Toolbox") - add_subdirectory(MxAnyType) add_subdirectory(toolbox) -install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/images DESTINATION ${WB-TOOLBOX_SHARE_DIR}) - include(AddUninstallTarget) diff --git a/toolbox/CMakeLists.txt b/toolbox/CMakeLists.txt index 1059ffe59..6f3fd5aa6 100755 --- a/toolbox/CMakeLists.txt +++ b/toolbox/CMakeLists.txt @@ -2,6 +2,9 @@ # Author: Jorhabib Eljaik, Francesco Romano, Diego Ferigo # CopyPolicy: Released under the terms of the GNU GPL v2.0. +# FIND DEPENDENCIES +# ================= + find_package(YARP REQUIRED) # Fail if YARP is not compiled as shared library @@ -10,21 +13,31 @@ if(NOT ${YARP_IS_SHARED_LIBRARY}) message(FATAL_ERROR "YARP was found, but it was compiled as static library. A shared library version of YARP is required.") endif() +find_package(Matlab REQUIRED + MX_LIBRARY + ENG_LIBRARY + MAIN_PROGRAM +) + +find_package(iDynTree 0.7.2 REQUIRED) + +# This find loads Eigen from YCM. +# It would be nice using the EigenConfig shipped with eigen package, but +# on Ubuntu 16.04 eigen is old and provides only FindEigen. +find_package(Eigen3 REQUIRED) + +# OPTIONS AND INCLUDES +# ==================== + set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${YARP_MODULE_PATH}) include(YarpInstallationHelpers) yarp_configure_external_installation(codyco) -find_package(Matlab REQUIRED MX_LIBRARY - ENG_LIBRARY - MAIN_PROGRAM) - -if(NOT Matlab_FOUND) - message(FATAL_ERROR "Matlab not found") -endif() +# Add configure_block macro +include(Utilities) -# Settings for rpath +# Settings for RPATH if(NOT MSVC) - # Add the option to enable RPATH option(WB-TOOLBOX_ENABLE_RPATH "Enable RPATH installation" TRUE) mark_as_advanced(WB-TOOLBOX_ENABLE_RPATH) endif(NOT MSVC) @@ -35,35 +48,38 @@ add_install_rpath_support(BIN_DIRS ${CMAKE_INSTALL_PREFIX}/bin DEPENDS WB-TOOLBOX_ENABLE_RPATH USE_LINK_PATH) -find_package(Eigen3 REQUIRED) -find_package(iDynTree 0.7.2 REQUIRED) - -include(Utilities) +# TOOLBOX SHARED LIBRARY +# ====================== +# +# This library will be linked to the S-Function for the usage in Simulink, and to +# another implementation for its usage within the code generated by the Simulink Coder. -configure_block(BLOCK_NAME "Base" +configure_block( + BLOCK_NAME "Base" LIST_PREFIX WBT - SOURCES src/base/toolbox.cpp - src/base/Block.cpp + SOURCES src/base/Block.cpp src/base/BlockInformation.cpp - src/base/WBBlock.cpp - src/base/Log.cpp - src/base/ToolboxSingleton.cpp - src/base/factory.cpp - src/base/SimulinkBlockInformation.cpp - src/base/Signal.cpp src/base/Configuration.cpp + src/base/Log.cpp + src/base/Parameter.cpp + src/base/Parameters.cpp + src/base/ConvertStdVector.cpp src/base/RobotInterface.cpp - HEADERS include/base/toolbox.h - include/base/Block.h + src/base/Signal.cpp + src/base/ToolboxSingleton.cpp + src/base/WBBlock.cpp + HEADERS include/base/Block.h include/base/BlockInformation.h - include/base/WBBlock.h - include/base/Log.h - include/base/ToolboxSingleton.h - include/base/SimulinkBlockInformation.h - include/base/Signal.h include/base/Configuration.h + include/base/Log.h + include/base/Parameter.h + include/base/Parameters.h + include/base/ConvertStdVector.h include/base/RobotInterface.h - ) + include/base/Signal.h + include/base/ToolboxSingleton.h + include/base/WBBlock.h +) # configure_block(BLOCK_NAME "Inverse Kinematics" # GROUP "Model" @@ -72,6 +88,7 @@ configure_block(BLOCK_NAME "Base" # HEADERS include/RemoteInverseKinematics.h) option(WBT_USES_ICUB "Build models which need iCub library (e.g. Minimum Jerk Traj. Generator)" ON) + if (WBT_USES_ICUB) find_package(ICUB REQUIRED) add_definitions(-DWBT_USES_ICUB) @@ -99,8 +116,6 @@ if (WBT_USES_ICUB) # # include_directories(SYSTEM ${iKin_INCLUDE_DIRS}) # endif() - - include_directories(SYSTEM ${ctrlLib_INCLUDE_DIRS}) endif() configure_block(BLOCK_NAME "Yarp Read" @@ -138,6 +153,7 @@ configure_block(BLOCK_NAME "Simulator Synchronizer" LIST_PREFIX WBT SOURCES src/SimulatorSynchronizer.cpp HEADERS include/SimulatorSynchronizer.h) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/autogenerated) configure_block(BLOCK_NAME "Mass Matrix" GROUP "Model" @@ -181,78 +197,157 @@ configure_block(BLOCK_NAME "Set References" SOURCES src/SetReferences.cpp HEADERS include/SetReferences.h) -configure_block(BLOCK_NAME "Set Low-Level PIDs" - GROUP "Actuators" - LIST_PREFIX WBT - SOURCES src/SetLowLevelPID.cpp - HEADERS include/SetLowLevelPID.h) -# + configure_block(BLOCK_NAME "Set Low-Level PIDs" + GROUP "Actuators" + LIST_PREFIX WBT + SOURCES src/SetLowLevelPID.cpp + HEADERS include/SetLowLevelPID.h) + configure_block(BLOCK_NAME "Get Measurement" GROUP "State" LIST_PREFIX WBT SOURCES src/GetMeasurement.cpp HEADERS include/GetMeasurement.h) -configure_block(BLOCK_NAME "Get Limits" - GROUP "State" - LIST_PREFIX WBT - SOURCES src/GetLimits.cpp - HEADERS include/GetLimits.h) + configure_block(BLOCK_NAME "Get Limits" + GROUP "State" + LIST_PREFIX WBT + SOURCES src/GetLimits.cpp + HEADERS include/GetLimits.h) get_property(ALL_HEADERS GLOBAL PROPERTY WBT_HEADERS) get_property(ALL_SOURCES GLOBAL PROPERTY WBT_SOURCES) -add_definitions(-DMATLAB_MEX_FILE) -include_directories(include) -include_directories(include/base) +add_library(WBToolboxLibrary SHARED + ${ALL_HEADERS} ${ALL_SOURCES} +) +set_target_properties(WBToolboxLibrary PROPERTIES + OUTPUT_NAME "WBToolbox" + # VERSION ${PROJECT_VERSION} + PUBLIC_HEADER "${ALL_HEADERS}" +) -include_directories(SYSTEM ${Matlab_INCLUDE_DIRS} "${Matlab_ROOT_DIR}/simulink/include") -include_directories(SYSTEM ${YARP_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIR}) +# Import math symbols from standard cmath +target_compile_definitions(WBToolboxLibrary PUBLIC "-D_USE_MATH_DEFINES") -list(APPEND LINKED_LIBRARIES ${YARP_LIBRARIES}) -list(APPEND LINKED_LIBRARIES ${iDynTree_LIBRARIES}) +# Set other properties of the target WBToolbox +# -------------------------------------------- + +target_include_directories(WBToolboxLibrary PUBLIC + $ + $ +) + +target_include_directories(WBToolboxLibrary SYSTEM PUBLIC + ${Matlab_INCLUDE_DIRS} + ${Matlab_ROOT_DIR}/simulink/include + ${YARP_INCLUDE_DIRS} + ${EIGEN3_INCLUDE_DIR} +) + +set(IDYNTREE_REQUIRED_LIBS + iDynTree::idyntree-core + iDynTree::idyntree-model + iDynTree::idyntree-modelio-urdf + iDynTree::idyntree-high-level +) +target_link_libraries(WBToolboxLibrary PUBLIC + ${YARP_LIBRARIES} + ${IDYNTREE_REQUIRED_LIBS} + ClockServer +) if (WBT_USES_ICUB) - list(APPEND LINKED_LIBRARIES ctrlLib) + target_compile_definitions(WBToolboxLibrary PUBLIC -DWBT_USES_ICUB) + target_link_libraries(WBToolboxLibrary PUBLIC ctrlLib) if (${ICUB_USE_IPOPT}) - list(APPEND LINKED_LIBRARIES iKin) + target_link_libraries(WBToolboxLibrary PUBLIC iKin) endif() endif() -# Adding files used for the generation of the dynamic library +# TOOLBOX S-FUNCTION MEX LIBRARY +# ============================== + matlab_add_mex( - NAME WBToolbox - SRC ${ALL_HEADERS} ${ALL_SOURCES} - LINK_TO ${LINKED_LIBRARIES} + NAME WBToolboxMex + OUTPUT_NAME WBToolbox + SRC include/base/SimulinkBlockInformation.h + src/base/SimulinkBlockInformation.cpp + include/base/toolbox.h + src/base/factory.cpp + src/base/WBToolbox.cpp + LINK_TO MxAnyType WBToolboxLibrary ) -# Link with MxAnyType library -# TODO: this will become an external project sooner or later -target_link_libraries(WBToolbox MxAnyType) +target_compile_options(WBToolboxMex PUBLIC -DMATLAB_MEX_FILE) -# Link with ClockServer library -add_subdirectory(autogenerated/) -target_link_libraries(WBToolbox ClockServer) +# Install S-Function +install(TARGETS WBToolboxMex DESTINATION ${CMAKE_INSTALL_PREFIX}/mex) -install(TARGETS WBToolbox DESTINATION ${CMAKE_INSTALL_PREFIX}/mex) +# Install TLC file +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/WBToolbox.tlc + DESTINATION ${CMAKE_INSTALL_PREFIX}/mex) -# The following line is to properly configure the installation script of the toolbox -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/matlab/startup_wbitoolbox.m.in - ${CMAKE_BINARY_DIR}/startup_wbitoolbox.m) +# MATLAB CLASSES AND SIMULINK CONFIGURATION +# ========================================= + +# Export the library +# ------------------ -# Custom script to generate the library to be committed on the repository +# The logic about library development is the following: +# +# * Developers should edit the library (e.g. changing block's masks) in the +# toolbox/matlab/library/WBToolboxLibrary_repository.mdl file. +# * Developers should export the changes using the export_library custom target. +# * Users will use the files installed from the exported/ folder. + +# Custom script to generate the library to be committed on the repository. # This target is excluded from the normal build and must be called explicitly by the -# developer who modifies the library -add_custom_target(export_libraries ${Matlab_MAIN_PROGRAM} -nosplash -nodesktop -r export_library WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/matlab) +# developer who modifies the library. +add_custom_target(export_libraries + ${Matlab_MAIN_PROGRAM} -nosplash -nodesktop -r export_library + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/matlab +) set_target_properties(export_libraries PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1) +# Install all the other files needed by the toolbox +# ------------------------------------------------- + +set(WB-TOOLBOX_SHARE_DIR "${CMAKE_INSTALL_PREFIX}/share/WB-Toolbox") + +# The following line is to properly configure the installation script of the toolbox +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/matlab/startup_wbitoolbox.m.in + ${CMAKE_BINARY_DIR}/startup_wbitoolbox.m) + # Install configuration files -install(FILES ${CMAKE_BINARY_DIR}/startup_wbitoolbox.m DESTINATION ${WB-TOOLBOX_SHARE_DIR}) -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/matlab/slblocks.m DESTINATION ${WB-TOOLBOX_SHARE_DIR}) +install( + FILES ${CMAKE_BINARY_DIR}/startup_wbitoolbox.m + DESTINATION ${WB-TOOLBOX_SHARE_DIR} +) +install( + FILES ${CMAKE_CURRENT_SOURCE_DIR}/matlab/slblocks.m + DESTINATION ${WB-TOOLBOX_SHARE_DIR} +) -# Install the package folder -install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/matlab/+WBToolbox - DESTINATION ${WB-TOOLBOX_SHARE_DIR}) +# Install the Matlab package folder +install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/matlab/+WBToolbox + DESTINATION ${WB-TOOLBOX_SHARE_DIR} +) + +# Install images for the Simulink Library +install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/images + DESTINATION ${WB-TOOLBOX_SHARE_DIR} +) + +# Install script for enabling debugging capabilities +# (update the mex and avoid restarting matlab) +configure_file( + matlab/setupForMatlabDebug.m.in + ${CMAKE_BINARY_DIR}/toolbox/setupForMatlabDebug.m + @ONLY +) #if MAJOR >= 2014 && MINOR >= b # Note: We had issues with Matlab 2014b and .mdl models. @@ -263,5 +358,64 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/matlab/+WBToolbox # else() # install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/WBToolboxLibrary.mdl DESTINATION ${WB-TOOLBOX_SHARE_DIR}) # endif() -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/matlab/library/exported/WBToolboxLibrary.slx - DESTINATION ${WB-TOOLBOX_SHARE_DIR}) +install( + FILES ${CMAKE_CURRENT_SOURCE_DIR}/matlab/library/exported/WBToolboxLibrary.slx + DESTINATION ${WB-TOOLBOX_SHARE_DIR} +) + +# AUTOMATIC CODE GENERATION SUPPORT +# ================================= + +set(CODER_HDR + include/base/CoderBlockInformation.h + include/base/GeneratedCodeWrapper.h +) + +set(CODER_SRC + src/base/CoderBlockInformation.cpp +) + +add_library(WBToolboxCoder SHARED ${CODER_HDR} ${CODER_SRC}) + +target_link_libraries(WBToolboxCoder PUBLIC WBToolboxLibrary) + +set_target_properties(WBToolboxCoder PROPERTIES + # VERSION ${PROJECT_VERSION} + PUBLIC_HEADER "${CODER_HDR}" +) + +target_include_directories(WBToolboxCoder INTERFACE + $) + +# INSTALL THE WBToolboxCoder TARGET +# ================================= +# +# This target will be imported and linked with the code automatically +# generated by Simulink Coder. + +install( + TARGETS WBToolboxCoder WBToolboxLibrary + EXPORT WBToolboxSimulinkCoder + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/WB-Toolbox +) + +set_property(GLOBAL APPEND + PROPERTY WBToolboxSimulinkCoderExport_TARGETS + WBToolboxCoder WBToolboxLibrary) + +set(WBToolboxSimulinkCoderExport_DEPENDENCIES YARP iDynTree) +if (WBT_USES_ICUB) + list(APPEND WBToolboxSimulinkCoderExport_DEPENDENCIES iCub) +endif() + +include(InstallBasicPackageFiles) +install_basic_package_files(WBToolboxSimulinkCoder + VERSION ${PROJECT_VERSION} + DEPENDENCIES ${WBToolboxSimulinkCoderExport_DEPENDENCIES} + COMPATIBILITY AnyNewerVersion + TARGETS_PROPERTY WBToolboxSimulinkCoderExport_TARGETS + NO_CHECK_REQUIRED_COMPONENTS_MACRO +)