Skip to content

Commit

Permalink
Update autogeneration (#92)
Browse files Browse the repository at this point in the history
* Add driver.cpp.in template

* Update clang-format

* Update main CMakeLists.txt to use the driver.cpp.in template

* Minor edit in RegisteMdl.cmake

* Update CreateAutogeneratedCodeTargets.cmake.in template

* fix block names for autogen

* fixed also the model without Gazebo for autogen

* modified installation path of +wbc library

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* models exported to 2017b

Co-authored-by: Diego Ferigo <[email protected]>
  • Loading branch information
gabrielenava and diegoferigo authored Mar 23, 2020
1 parent 12ab05e commit 1fa9a76
Show file tree
Hide file tree
Showing 23 changed files with 1,569 additions and 1,392 deletions.
6 changes: 5 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ IncludeCategories:
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '^"rtwtypes'
Priority: -1
- Regex: '^"tmwtypes'
Priority: -1
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
Expand All @@ -89,7 +93,7 @@ PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: false
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: true
Expand Down
26 changes: 15 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ project(whole-body-controllers)
option(WBC_EXPORT_AUTOGENERATED "Enable the target to export code generated with Simulink Coder" OFF)
option(WBC_INSTALL_ALL_HOME_POS "Installation of all available home positions" ON)

# ======================
# Install home positions
# ======================
# ======================================
# Install home positions and WBC library
# ======================================

find_package(YARP REQUIRED)
yarp_configure_external_installation(wbc)

add_subdirectory(utilities)
add_subdirectory(library)
add_subdirectory(config)

# ======================
# Autogeneration routine
Expand Down Expand Up @@ -51,8 +53,7 @@ file(WRITE "${CMAKE_BINARY_DIR}/autogenerated/CMakeLists.txt" "# Exported contro
# Find clang-format
find_program(ClangFormat_EXECUTABLE
DOC "Path to the clang-format executable."
NAMES clang-format clang-format-7.0 clang-format-6.0 clang-format-5.0 clang-format-4.0
)
NAMES clang-format clang-format-9 clang-format-8 clang-format-7 clang-format-6.0 clang-format-5.0 clang-format-4.0)
configure_file(${CMAKE_SOURCE_DIR}/.clang-format ${CMAKE_BINARY_DIR}/.clang-format COPYONLY)

# Copy only h and cpp files
Expand All @@ -62,17 +63,21 @@ foreach(SOURCEDIR ${AUTOGENERATED_FOLDERS})
file(APPEND "${CMAKE_BINARY_DIR}/autogenerated/CMakeLists.txt" "add_subdirectory(${MDLDIRNAME})\n")
file(MAKE_DIRECTORY "${MDLDESTDIR}")
string(REGEX MATCH "[^_]*" MDL_NAME ${MDLDIRNAME}) # Remove _gtw_rtw
# Create the CMakeLists.txt of the model
configure_file(
"${CMAKE_SOURCE_DIR}/cmake/CreateAutogeneratedCodeTargets.cmake.in"
"${MDLDESTDIR}/CMakeLists.txt"
@ONLY
)
@ONLY)
# Copy the main file
configure_file(
"${PROJECT_SOURCE_DIR}/src/driver.cpp.in"
"${MDLDESTDIR}/driver.cpp"
@ONLY)
# Copy sources to the build directory
add_custom_command(TARGET copy-autogenerated-models
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${SOURCEDIR}/*.h" "${SOURCEDIR}/*.cpp" "${SOURCEDIR}/defines.txt" "${MDLDESTDIR}"
COMMENT "Copying ${MDLDIRNAME} to the build folder."
)
COMMENT "Copying ${MDLDIRNAME} to the build folder.")
# Apply clang-format style
if(NOT ${ClangFormat_EXECUTABLE} STREQUAL "ClangFormat_EXECUTABLE-NOTFOUND")
add_custom_command(TARGET copy-autogenerated-models
Expand All @@ -86,5 +91,4 @@ endforeach()
add_custom_command(TARGET copy-autogenerated-models
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_BINARY_DIR}/autogenerated" "${AUTOGENERATED_WBC_SOURCE_DIR}/autogenerated"
COMMENT "Copying generated files to ${AUTOGENERATED_WBC_SOURCE_DIR}/autogenerated"
)
COMMENT "Copying generated files to ${AUTOGENERATED_WBC_SOURCE_DIR}/autogenerated")
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,37 @@ The repository contains `Simulink-based whole-body controllers` developed to con

- The controllers stored in this repository are an **overview** of the possibile control frameworks that can be implemented using the `robotology` software infrastructure. Also, the repository contains a [library](library/README.md) of configuration and utility Matlab functions to design simulations with [Gazebo](http://gazebosim.org/) simulator and on the real robot iCub.

- The robot dynamics and kinematics is computed run-time by means of [WBToolbox](https://github.com/robotology/wb-toolbox), a Simulink libary that wraps [iDyntree](https://github.com/robotology/idyntree). For more information on iDyntree library, see also this [README](https://github.com/robotology/idyntree/blob/master/README.md).
- The robot dynamics and kinematics is computed run-time by means of [WBToolbox](https://github.com/robotology/wb-toolbox), a Simulink library that wraps [iDyntree](https://github.com/robotology/idyntree). For more information on iDyntree library, see also this [README](https://github.com/robotology/idyntree/blob/master/README.md).

- The Simulink models implement different control strategies both for fixed-base and for floating-base robots. They space from `momentum-based` torque control to `inverse-kinematics-based` position control. Have a look at the [controllers](controllers/README.md) folder for more details.

## Dependencies

This repository depends upon the following Software:

- [CMake](https://cmake.org/), at least version **3.5**.
- [Matlab/Simulink](https://it.mathworks.com/products/matlab.html), default version **R2017b**.
- [WB-Toolbox](https://github.com/robotology/WB-Toolbox) and [blockfactory](https://github.com/robotology/blockfactory).
- [Gazebo Simulator](http://gazebosim.org/), default version **9.0**.
- [gazebo-yarp-plugins](https://github.com/robotology/gazebo-yarp-plugins).
- [icub-gazebo](https://github.com/robotology/icub-gazebo), [icub-gazebo-wholebody](https://github.com/robotology-playground/icub-gazebo-wholebody) and [icub-models](https://github.com/robotology/icub-models) to access iCub models.
- [codyco-modules](https://github.com/robotology/codyco-modules) (**Optional**, for using [wholeBodyDynamics](https://github.com/robotology/codyco-modules/tree/master/src/devices/wholeBodyDynamics) device).
- [whole-body-estimators](https://github.com/robotology/whole-body-estimators) (**Optional**, for using [wholeBodyDynamics](https://github.com/robotology/whole-body-estimators/tree/master/devices/wholeBodyDynamics) device).
- [YARP](https://github.com/robotology/yarp) and [icub-main](https://github.com/robotology/icub-main).

## Installation and usage

The repository is usually tested and developed on **Ubuntu** and **macOS** operating systems. Some functionalities may not work properly on **Windows**.

**NOTE:** it is suggested to install `whole-body-controllers` and most of its dependencies (namely, `YARP`, `icub-main`, `codyco-modules`,`icub-gazebo`,`icub-gazebo-wholebody`, `icub-models`, `gazebo-yarp-plugins`, `blockfactory` and `WB-Toolbox` and their dependencies) using the [robotology-superbuild](https://github.com/robotology/robotology-superbuild) (enable `ROBOTOLOGY_USES_GAZEBO`, `ROBOTOLOGY_ENABLE_DYNAMICS`, `ROBOTOLOGY_USES_MATLAB` options).
- **NOTE:** it is suggested to install `whole-body-controllers` and most of its dependencies (namely, `YARP`, `icub-main`, `whole-body-estimators`,`icub-gazebo`,`icub-gazebo-wholebody`, `icub-models`, `gazebo-yarp-plugins`, `blockfactory` and `WB-Toolbox` and their dependencies) using the [robotology-superbuild](https://github.com/robotology/robotology-superbuild) (enable `ROBOTOLOGY_USES_GAZEBO`, `ROBOTOLOGY_ENABLE_DYNAMICS`, `ROBOTOLOGY_USES_MATLAB` options).

- Otherwise, after intalling all the dependencies, **clone the repository** on your pc by running on a terminal `git clone https://github.com/robotology/whole-body-controllers`, or download the repository.
- Otherwise, after installing all the dependencies, **clone the repository** on your pc by running on a terminal `git clone https://github.com/robotology/whole-body-controllers`, or download the repository. Then (on Ubuntu), open a terminal from the folder where you downloaded whole-body-controllers and run:

```
mkdir build
cd build
ccmake ..
```
in the GUI that it will open, set the `CMAKE_PREFIX_PATH` as your desired installation folder. Then, run `make install`.

- Set the environmental variable `YARP_ROBOT_NAME` in your `.bashrc` file (or equivalent) to be the name of the robot you want to control. List of supported robot names:

Expand All @@ -45,26 +53,21 @@ The repository is usually tested and developed on **Ubuntu** and **macOS** opera
| iCubGazeboV2_5|[model.urdf](https://github.com/robotology/icub-models/blob/master/iCub/robots/iCubGazeboV2_5/model.urdf)|
| icubGazeboSim |[model.urdf](https://github.com/robotology/yarp-wholebodyinterface/blob/master/app/robots/icubGazeboSim/model.urdf) |

- **IMPORTANT!** to use the WBC Simulink controllers, it is **required** to add the [matlab-wbc](library/matlab-wbc) folder to the Matlab path. There are two possible ways to add the folder to the path:
- **IMPORTANT!** to use the WBC Simulink controllers, it is **required** to add the **installed** [+wbc](library/matlab-wbc/+wbc) folder to the Matlab path. There are two possible ways to add the folder to the Matlab path:

- `manually` and `permanently` add the folder to the Matlab path;
- run **only once** the [startup_WBC.m](config/startup_WBC.m) script. In this case, path is **not** permanently added to Matlab, and it is required to **always** start Matlab from the folder where your `pathdef.m` file is (usually `~/Documents/MATLAB`). To facilitate the reaching of the WBC working folder from the folder containing the `pathdef.m`, a `goToWholeBodyController.m` script will be automatically created in that folder. Run it to jump to the WBC folder. For further information see also the [WBToolbox documentation](https://robotology.github.io/wb-toolbox/mkdocs/install/#matlab). **WARNING**: if the repository is installed through the `robotology-superbuild`, **DO NOT** run the `startup_WBC.m` file but instead run the [startup_robotology_superbuild.m](https://github.com/robotology/robotology-superbuild/blob/master/cmake/template/startup_robotology_superbuild.m.in) file that comes along with robotology-superbuild installation. The result will be the same.
- **Note**: to use any function inside the package [matlab-wbc/+wbc](library/matlab-wbc/+wbc), add the `wbc` prefix to the function name when the function is invoked, i.e. `[outputs] = wbc.myFunction(inputs)`. More information on packages can be found in the [Matlab documentation](https://it.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html).
**1a.** `manually` and `permanently` add the folder to the Matlab path;

- There are some functionalities of the repo such as the [automatic generation of c++ code from Simulink](https://github.com/robotology/whole-body-controllers#automatic-generation-of-c-code-from-simulink) that require to compile and install the repository using `cmake`. To do so (on Ubuntu), open a terminal from the folder where you installed whole-body-controllers and run:
**1b.** run **only once** the [startup_WBC.m](config/startup_WBC.m.in) script, which is installed in your `${BUILD}` folder. In this case, path is **not** permanently added to Matlab, and it is required to **always** start Matlab from the folder where your `pathdef.m` file is (usually `~/Documents/MATLAB`). To facilitate the reaching of the WBC working folder from the folder containing the `pathdef.m`, a `goToWholeBodyController.m` script can be [automatically created](config/createGoToWBC.m) in that folder. Run it to jump to the WBC folder. For further information on the installation procedure see also the [WBToolbox documentation](https://robotology.github.io/wb-toolbox/mkdocs/install/#matlab).
**WARNING**: if the repository is installed through the `robotology-superbuild`, **DO NOT** run the `startup_WBC.m` file but instead run the [startup_robotology_superbuild](https://github.com/robotology/robotology-superbuild/blob/master/cmake/template/startup_robotology_superbuild.m.in) file that comes along with robotology-superbuild installation.
- **Note**: to use any function inside the package [matlab-wbc/+wbc](library/matlab-wbc/+wbc), add the `wbc` prefix to the function name when the function is invoked, i.e. `[outputs] = wbc.myFunction(inputs)`. More information on packages can be found in the [Matlab documentation](https://it.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html).

```
mkdir build
cd build
ccmake ..
```
then in the GUI that it will open, set the `CMAKE_PREFIX_PATH` as your desired installation folder. Then, run `make install`.
- There are some functionalities of the repo such as the [automatic generation of c++ code from Simulink](https://github.com/robotology/whole-body-controllers#automatic-generation-of-c-code-from-simulink) that require to enable not-default cmake options. Check the available options by running `ccmake .` in your `build` directory.

## Troubleshooting

Please refer to the [WBToolbox troubleshooting documentation](https://robotology.github.io/wb-toolbox/mkdocs/troubleshooting/).

## Structure of the repo
## Relevant folders of the repo

- **config**: a collection of scripts to correctly configure this repo. [[README]](config/README.md)

Expand Down
29 changes: 18 additions & 11 deletions cmake/CreateAutogeneratedCodeTargets.cmake.in
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
# Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT). All rights reserved.
# This software may be modified and distributed under the terms of the
# GNU Lesser General Public License v2.1 or any later version.

find_package(BlockFactory COMPONENTS SimulinkCoder REQUIRED)

# ===============================================
# Generate a library from the autogenerated class
# ===============================================

set(MDL_NAME @MDL_NAME@)
add_generated_code_lib(MODELNAME ${MDL_NAME})
include(TargetFromGeneratedCode)
set(SIMULINK_MODEL_NAME "@MDL_NAME@")

# This macro creates a target named as the mdl file for the autogenerated class
target_from_generated_code(
MODELNAME ${SIMULINK_MODEL_NAME}
SOURCE_FOLDER ${CMAKE_CURRENT_SOURCE_DIR})

# ================================
# Test executable with custom main
# ================================

# Set the target name for the custom driver
set(CODER_MAIN "CoderMain_${MDL_NAME}")

configure_file(
"${PROJECT_SOURCE_DIR}/src/driver.cpp.in"
"${CMAKE_CURRENT_BINARY_DIR}/driver.cpp"
@ONLY)
set(CODER_MAIN "Run${SIMULINK_MODEL_NAME}")

add_executable(${CODER_MAIN}
"${CMAKE_CURRENT_BINARY_DIR}/driver.cpp"
)
"${CMAKE_CURRENT_SOURCE_DIR}/driver.cpp")

target_link_libraries(${CODER_MAIN} PUBLIC ${MDL_NAME}_LIB)
target_link_libraries(${CODER_MAIN} PUBLIC ${SIMULINK_MODEL_NAME})
4 changes: 4 additions & 0 deletions cmake/RegisterMdl.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ function(initialize_mdl_set)
# PARSE ARGUMENTS
# ===============

set(_options)
set(_oneValueArgs NAME)
set(_multiValueArgs)

cmake_parse_arguments(PREFIX
"${_options}"
Expand All @@ -31,7 +33,9 @@ function(register_mdl)
# PARSE ARGUMENTS
# ===============

set(_options)
set(_oneValueArgs MODELNAME)
set(_multiValueArgs)

cmake_parse_arguments(PREFIX
"${_options}"
Expand Down
1 change: 1 addition & 0 deletions config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/startup_WBC.m.in ${CMAKE_BINARY_DIR}/startup_WBC.m)
4 changes: 3 additions & 1 deletion config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ A collection of scripts used for configuring the repo.

- [export_WBC.m](export_WBC.m): run this script. Then, digit the Matlab version in which you want to export the Simulink models. All models in the repo will be exported to that version. By default, Simulnk models in this repo are written using Matlab 2017b. **Remember: you cannot export a model in a Matlab version newer than the one you are using**!

- [startup_WBC.m](startup_WBC.m): run this script. Then, the path to the `matlab-wbc` folder will be **permanently** added to your `pathdef.m` file, which will be saved inside the Matlab `userpath`. **WARNING**! In order to have the `matlab-wbc` folder inside the Matlab path, it is **required** to start Matlab from the folder where the `pathdef.m` file is (i.e., from the folder that the `userpath` is pointing, usually `~/Documents/MATLAB`).
- [startup_WBC.m.in](startup_WBC.m.in): **DO NOT** run this script, but run its installation inside the procjet `${BUILD}` folder. Then, the path to the `+wbc` folder will be **permanently** added to your `pathdef.m` file, which will be saved inside the Matlab `userpath`. **WARNING**! In order to have the `+wbc` folder inside the Matlab path, it is **required** to start Matlab from the folder where the `pathdef.m` file is (i.e., from the folder that the `userpath` is pointing, usually `~/Documents/MATLAB`).

- [createGoToWBC.m](createGoToWBC.m): run this script. A file named 'goToWholeBodyControllers.m' will be created in the folder pointed by your `userpath`, to facilitate the reaching of WBC source folder when Matlab is started from the `userpath` folder.
39 changes: 4 additions & 35 deletions config/startup_WBC.m → config/createGoToWBC.m
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
%% startup_WBC.m
%% createGoToWBC.m
%
% Run this script once to permanently add the matlab-wbc library to your MATLAB path.
% Run this script once create the goToWholeBodyControllers.m script in your userpath.

clc
fprintf('\n## whole-body-controllers ##\n');
fprintf('\nAdding "matlab-wbc" library to your pathdef.m...\n\n');

% path to whole-body-controllers
pathToWBC = pwd;
pathToWBC = pathToWBC(1:end-6);

% path to the matlab-wbc library
pathToLibrary = [pathToWBC, filesep, 'library/matlab-wbc'];

if exist(pathToLibrary, 'dir')

addpath(pathToLibrary);
else
error('Path to the "matlab-wbc" library not found or not correct.')
end
fprintf('\nCreating "goToWholeBodyControllers.m" in your userpath...\n\n');

% Path to the Matlab userpath
pathToUserpath = userpath;
Expand Down Expand Up @@ -63,22 +49,6 @@
error('There are still multiple userpath. Please set a single userpath before running this script');
end

if (~isempty(pathSeparatorLocation))

pathToUserpath(pathSeparatorLocation) = [];
end

fprintf('Saving paths to %s\n\n', [pathToUserpath, filesep, 'pathdef.m']);

if (~savepath([pathToUserpath, filesep, 'pathdef.m']))

fprintf(['A file called pathdef.m has been created in your %s folder.\n', ...
'This should be enough to permanently add matlab-wbc to ', ...
'your MATLAB installation.\n'], pathToUserpath);
else
disp('There was an error generating the pathdef.m. Please manually add the matlab-wbc folder to your matlabpath');
end

% save a script named "goToWholeBodyControllers" inside the pathdef folder,
% to facilitate the user to reach the WBC working folder

Expand All @@ -100,8 +70,7 @@
fclose(fid);

fprintf('\n')
fprintf(['A file called goToWholeBodyControllers.m has also been created in your %s folder.\n', ...
fprintf(['A file called goToWholeBodyControllers.m has been created in your %s folder.\n', ...
'This will help to quickly reach the WBC-project folder after ', ...
'Matlab is launched.\n'], pathToUserpath);
end

Loading

0 comments on commit 1fa9a76

Please sign in to comment.