Skip to content

Commit

Permalink
Adjusted the generation of cpp templates to be valid cpp (StoglRoboti…
Browse files Browse the repository at this point in the history
…cs#41)

* change templates to valid cpp
* adjust cpplint to check templates
* add real license

Co-authored-by: Denis Štogl <[email protected]>
  • Loading branch information
mamueluth and destogl authored Apr 8, 2022
1 parent 2a84136 commit 06f8060
Show file tree
Hide file tree
Showing 19 changed files with 352 additions and 270 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ jobs:
strategy:
fail-fast: false
matrix:
linter: [cppcheck, lint_cmake]
linter: [cppcheck, cpplint, lint_cmake]
arguments: [""]
include:
- linter: copyright
arguments: "--exclude CONTRIBUTING.md conf.py test_robot_description.launch.py view_robot.launch.py controller.cpp controller.hpp test_controller.cpp test_controller.hpp test_load_controller.cpp robot_hardware_interface.cpp robot_hardware_interface.hpp test_robot_hardware_interface.cpp visibility_control.h robot_ros2_control.launch.py test_forward_position_controller.launch.py test_joint_trajectory_controller.launch.py"
- linter: cpplint
arguments: "--exclude ./templates/ros2_control/controller/controller.cpp ./templates/ros2_control/controller/controller.hpp ./templates/ros2_control/controller/test_controller.cpp ./templates/ros2_control/controller/test_controller.hpp ./templates/ros2_control/controller/test_load_controller.cpp ./templates/ros2_control/controller/test_load_controller.hpp ./templates/ros2_control/hardware/robot_hardware_interface.cpp ./templates/ros2_control/hardware/robot_hardware_interface.hpp ./templates/ros2_control/hardware/visibility_control.cpp ./templates/ros2_control/hardware/test_robot_hardware_interface.cpp ./templates/ros2_control/hardware/visibility_control.h"
arguments: "--exclude conf.py test_robot_description.launch.py view_robot.launch.py robot_ros2_control.launch.py test_forward_position_controller.launch.py test_joint_trajectory_controller.launch.py"
steps:
- uses: actions/checkout@v2
- uses: ros-tooling/setup-ros@master
Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ repos:

# Python hooks
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.0
rev: v2.31.1
hooks:
- id: pyupgrade
args: [--py36-plus]

- repo: https://github.com/psf/black
rev: 21.12b0
rev: 22.3.0
hooks:
- id: black
args: ["--line-length=99"]
Expand Down Expand Up @@ -127,11 +127,11 @@ repos:
stages: [commit]
entry: ament_copyright
language: system
exclude: ^(templates/)
exclude: ^(templates/)|docs/conf.py

# Docs - RestructuredText hooks
- repo: https://github.com/PyCQA/doc8
rev: 0.10.1
rev: 0.11.0
hooks:
- id: doc8
args: ['--max-line-length=100', '--ignore=D001']
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ As this project, by default, uses the default GitHub issue labels


## Licensing
Any contribution that you make to this repository will be under the Apache 2 License, as dictated by that [license]:
Any contribution that you make to this repository will
be under the Apache 2 License, as dictated by that
[license](http://www.apache.org/licenses/LICENSE-2.0.html):

~~~
5. Submission of Contributions. Unless You explicitly state otherwise,
Expand All @@ -61,8 +63,6 @@ Any contribution that you make to this repository will be under the Apache 2 Lic
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
~~~

[issues]: https://github.com/StoglRobotics/ros_team_workspace/issues
[closed-issues]: https://github.com/StoglRobotics/ros_team_workspace/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aclosed%20
[help-wanted]: https://github.com/StoglRobotics/ros_team_workspace/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22
[license]: http://www.apache.org/licenses/LICENSE-2.0.html
44 changes: 20 additions & 24 deletions scripts/ros2_control/setup-controller-package.bash
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fi

PKG_NAME=$3
if [ -z "$3" ]; then
current=`pwd`
current=$(pwd)
PKG_NAME=$(basename "$current")
echo "Package name guessed from the current path is '$PKG_NAME'. Is this correct? If not provide it as the third parameter."
fi
Expand All @@ -63,7 +63,7 @@ choice=${choice:="1"}
if [ "$choice" != 0 ]; then
read -p "Insert your company or personal name (copyright): " NAME_ON_LICENSE
NAME_ON_LICENSE=${NAME_ON_LICENSE=""}
YEAR_ON_LICENSE=`date +%Y`
YEAR_ON_LICENSE=$(date +%Y)
fi

LICENSE_HEADER=""
Expand Down Expand Up @@ -103,12 +103,12 @@ TEST_HPP="test/test_$FILE_NAME.hpp"
if [[ ! -f "$VC_H" ]]; then
cp -n $ROS2_CONTROL_HW_ITF_TEMPLATES/visibility_control.h $VC_H
fi
cat $ROS2_CONTROL_CONTROLLER_TEMPLATES/controller_pluginlib.xml >> $PLUGIN_XML
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/controller.hpp $CTRL_HPP
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/controller.cpp $CTRL_CPP
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/test_load_controller.cpp $LOAD_TEST_CPP
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/test_controller.cpp $TEST_CPP
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/test_controller.hpp $TEST_HPP
cat $ROS2_CONTROL_CONTROLLER_TEMPLATES/dummy_controller_pluginlib.xml >> $PLUGIN_XML
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/dummy_package_namespace/dummy_controller.hpp $CTRL_HPP
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/dummy_controller.cpp $CTRL_CPP
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/test_load_dummy_controller.cpp $LOAD_TEST_CPP
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/test_dummy_controller.cpp $TEST_CPP
cp -n $ROS2_CONTROL_CONTROLLER_TEMPLATES/test_dummy_controller.hpp $TEST_HPP

echo "Template files copied."

Expand All @@ -124,8 +124,7 @@ if [[ "$LICENSE_HEADER" != "" ]]; then
touch $TMP_FILE
for FILE_TO_LIC in "${FILES_TO_LICENSE[@]}"; do
cat $LICENSE_HEADER > $TMP_FILE
cat $FILE_TO_LIC >> $TMP_FILE
sed -i "/\\\$LICENSE\\\$/d" $TMP_FILE
sed "1,13d" $FILE_TO_LIC >> $TMP_FILE # delete first 13 lines which correspond to fake license
mv $TMP_FILE $FILE_TO_LIC
sed -i "s/\\\$YEAR\\\$/${YEAR_ON_LICENSE}/g" $FILE_TO_LIC
sed -i "s/\\\$NAME_ON_LICENSE\\\$/${NAME_ON_LICENSE}/g" $FILE_TO_LIC
Expand All @@ -139,13 +138,15 @@ FILES_TO_SED+=("$PLUGIN_XML")
# declare -p FILES_TO_SED

for SED_FILE in "${FILES_TO_SED[@]}"; do
sed -i "s/\\\$PACKAGE_NAME\\\$/${PKG_NAME^^}/g" $SED_FILE
sed -i "s/\\\$package_name\\\$/${PKG_NAME}/g" $SED_FILE
sed -i "s/\\\$file_name\\\$/${FILE_NAME}/g" $SED_FILE
sed -i "s/\\\$FILE_NAME\\\$/${FILE_NAME^^}/g" $SED_FILE
sed -i "s/\\\$ClassName\\\$/${CLASS_NAME}/g" $SED_FILE
sed -i "s/\\\$interface_type\\\$/${INTERFACE_TYPE}/g" $SED_FILE
sed -i "s/\\\$Interface_Type\\\$/${INTERFACE_TYPE^}/g" $SED_FILE
sed -i "s/TEMPLATES__ROS2_CONTROL__CONTROLLER__DUMMY_PACKAGE_NAMESPACE/${PKG_NAME^^}/g" $SED_FILE # package name for include guard
sed -i "s/TEMPLATES__ROS2_CONTROL__CONTROLLER/${PKG_NAME^^}/g" $SED_FILE # package name for include guard
sed -i "s/TEMPLATES__ROS2_CONTROL__HARDWARE/${PKG_NAME^^}/g" $SED_FILE # package name for include guard from hardware
sed -i "s/dummy_package_namespace/${PKG_NAME}/g" $SED_FILE # package name for includes
sed -i "s/dummy_controller/${FILE_NAME}/g" $SED_FILE # file name
sed -i "s/DUMMY_CONTROLLER/${FILE_NAME^^}/g" $SED_FILE # file name for include guard
sed -i "s/DummyClassName/${CLASS_NAME}/g" $SED_FILE # class name
sed -i "s/dummy_interface_type/${INTERFACE_TYPE}/g" $SED_FILE # interface type for includes
sed -i "s/Dummy_Interface_Type/${INTERFACE_TYPE^}/g" $SED_FILE # Interface type in namespace resolution
done


Expand Down Expand Up @@ -173,9 +174,8 @@ echo ")" >> $TMP_FILE

echo "target_include_directories(" >> $TMP_FILE
echo " $FILE_NAME" >> $TMP_FILE
echo " PUBLIC" >> $TMP_FILE
echo " $<BUILD_INTERFACE:\${CMAKE_CURRENT_SOURCE_DIR}/include>" >> $TMP_FILE
echo " $<INSTALL_INTERFACE:include>" >> $TMP_FILE
echo " PRIVATE" >> $TMP_FILE
echo " include" >> $TMP_FILE
echo ")" >> $TMP_FILE

# TODO(anyone): Add this dependencies in a loop
Expand All @@ -190,10 +190,6 @@ echo " rclcpp_lifecycle" >> $TMP_FILE
echo " realtime_tools" >> $TMP_FILE
echo ")" >> $TMP_FILE

# TODO(anyone): Delete after Foxy!!!
echo "# prevent pluginlib from using boost" >> $TMP_FILE
echo "target_compile_definitions($FILE_NAME PUBLIC \"PLUGINLIB__DISABLE_BOOST_FUNCTIONS\")" >> $TMP_FILE

if [[ "$package_configured" == "no" ]]; then

echo "" >> $TMP_FILE
Expand Down
19 changes: 9 additions & 10 deletions scripts/ros2_control/setup-hardware-interface-package.bash
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ TEST_CPP="test/test_$FILE_NAME.cpp"

# Copy files
cp -n $ROS2_CONTROL_HW_ITF_TEMPLATES/visibility_control.h $VC_H
cp -n $ROS2_CONTROL_HW_ITF_TEMPLATES/robot_hardware_interface.hpp $HW_ITF_HPP
cp -n $ROS2_CONTROL_HW_ITF_TEMPLATES/dummy_package_namespace/robot_hardware_interface.hpp $HW_ITF_HPP
cp -n $ROS2_CONTROL_HW_ITF_TEMPLATES/robot_hardware_interface.cpp $HW_ITF_CPP
cp -n $ROS2_CONTROL_HW_ITF_TEMPLATES/robot_pluginlib.xml $PLUGIN_XML
cp -n $ROS2_CONTROL_HW_ITF_TEMPLATES/test_robot_hardware_interface.cpp $TEST_CPP
Expand All @@ -130,8 +130,7 @@ if [[ "$LICENSE_HEADER" != "" ]]; then
touch $TMP_FILE
for FILE_TO_LIC in "${FILES_TO_LICENSE[@]}"; do
cat $LICENSE_HEADER > $TMP_FILE
cat $FILE_TO_LIC >> $TMP_FILE
sed -i "/\\\$LICENSE\\\$/d" $TMP_FILE
sed "1,13d" $FILE_TO_LIC >> $TMP_FILE # delete first 13 lines which correspond to fake license
mv $TMP_FILE $FILE_TO_LIC
sed -i "s/\\\$YEAR\\\$/${YEAR_ON_LICENSE}/g" $FILE_TO_LIC
sed -i "s/\\\$NAME_ON_LICENSE\\\$/${NAME_ON_LICENSE}/g" $FILE_TO_LIC
Expand All @@ -149,13 +148,13 @@ FILES_TO_SED+=("$PLUGIN_XML")
# declare -p FILES_TO_SED

for SED_FILE in "${FILES_TO_SED[@]}"; do
sed -i "s/\\\$PACKAGE_NAME\\\$/${PKG_NAME^^}/g" $SED_FILE
sed -i "s/\\\$package_name\\\$/${PKG_NAME}/g" $SED_FILE
sed -i "s/\\\$file_name\\\$/${FILE_NAME}/g" $SED_FILE
sed -i "s/\\\$FILE_NAME\\\$/${FILE_NAME^^}/g" $SED_FILE
sed -i "s/\\\$ClassName\\\$/${CLASS_NAME}/g" $SED_FILE
sed -i "s/\\\$interface_type\\\$/${INTERFACE_TYPE}/g" $SED_FILE
sed -i "s/\\\$Interface_Type\\\$/${INTERFACE_TYPE^}/g" $SED_FILE
sed -i "s/TEMPLATES__ROS2_CONTROL__HARDWARE__DUMMY_PACKAGE_NAMESPACE/${PKG_NAME^^}/g" $SED_FILE # package name for include guard
sed -i "s/dummy_package_namespace/${PKG_NAME}/g" $SED_FILE # package name for includes
sed -i "s/dummy_file_name/${FILE_NAME}/g" $SED_FILE # file name
sed -i "s/ROBOT_HARDWARE_INTERFACE/${FILE_NAME^^}/g" $SED_FILE # file name for include guard
sed -i "s/DummyClassName/${CLASS_NAME}/g" $SED_FILE # class name
sed -i "s/dummy_interface_type/${INTERFACE_TYPE}/g" $SED_FILE # interface type for includes
sed -i "s/Dummy_Interface_Type/${INTERFACE_TYPE^}/g" $SED_FILE # Interface type in namespace resolution
done

# If type is "sensor" remove write and command_interfaces methods
Expand Down
8 changes: 0 additions & 8 deletions templates/ros2_control/controller/controller_pluginlib.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
$LICENSE$

#include "$package_name$/$file_name$.hpp"
// Copyright (c) 2022, Stogl Robotics Consulting UG (haftungsbeschränkt) (template)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "dummy_package_namespace/dummy_controller.hpp"

#include <limits>
#include <memory>
Expand All @@ -9,11 +21,11 @@

#include "controller_interface/helpers.hpp"

namespace $package_name$
namespace dummy_package_namespace
{
$ClassName$::$ClassName$() : controller_interface::ControllerInterface() {}
DummyClassName::DummyClassName() : controller_interface::ControllerInterface() {}

CallbackReturn $ClassName$::on_init()
CallbackReturn DummyClassName::on_init()
{
try {
get_node()->declare_parameter<std::vector<std::string>>("joints", std::vector<std::string>({}));
Expand All @@ -26,7 +38,7 @@ CallbackReturn $ClassName$::on_init()
return CallbackReturn::SUCCESS;
}

CallbackReturn $ClassName$::on_configure(const rclcpp_lifecycle::State & /*previous_state*/)
CallbackReturn DummyClassName::on_configure(const rclcpp_lifecycle::State & /*previous_state*/)
{
auto error_if_empty = [&](const auto & parameter, const char * parameter_name) {
if (parameter.empty()) {
Expand Down Expand Up @@ -82,7 +94,7 @@ CallbackReturn $ClassName$::on_configure(const rclcpp_lifecycle::State & /*previ
return CallbackReturn::SUCCESS;
}

controller_interface::InterfaceConfiguration $ClassName$::command_interface_configuration() const
controller_interface::InterfaceConfiguration DummyClassName::command_interface_configuration() const
{
controller_interface::InterfaceConfiguration command_interfaces_config;
command_interfaces_config.type = controller_interface::interface_configuration_type::INDIVIDUAL;
Expand All @@ -95,7 +107,7 @@ controller_interface::InterfaceConfiguration $ClassName$::command_interface_conf
return command_interfaces_config;
}

controller_interface::InterfaceConfiguration $ClassName$::state_interface_configuration() const
controller_interface::InterfaceConfiguration DummyClassName::state_interface_configuration() const
{
controller_interface::InterfaceConfiguration state_interfaces_config;
state_interfaces_config.type = controller_interface::interface_configuration_type::INDIVIDUAL;
Expand All @@ -108,29 +120,7 @@ controller_interface::InterfaceConfiguration $ClassName$::state_interface_config
return state_interfaces_config;
}

// Fill ordered_interfaces with references to the matching interfaces
// in the same order as in joint_names
// TODO(anyone): use the method from controller_interface/helpers.hpp when ros2_contol#370
// is merged
template <typename T>
bool get_ordered_interfaces(
std::vector<T> & unordered_interfaces, const std::vector<std::string> & joint_names,
const std::string & interface_type, std::vector<std::reference_wrapper<T>> & ordered_interfaces)
{
for (const auto & joint_name : joint_names) {
for (auto & command_interface : unordered_interfaces) {
if (
(command_interface.get_name() == joint_name) &&
(command_interface.get_interface_name() == interface_type)) {
ordered_interfaces.push_back(std::ref(command_interface));
}
}
}

return joint_names.size() == ordered_interfaces.size();
}

CallbackReturn $ClassName$::on_activate(const rclcpp_lifecycle::State & /*previous_state*/)
CallbackReturn DummyClassName::on_activate(const rclcpp_lifecycle::State & /*previous_state*/)
{
// Set default value in command
std::shared_ptr<ControllerCommandMsg> msg = std::make_shared<ControllerCommandMsg>();
Expand All @@ -141,12 +131,12 @@ CallbackReturn $ClassName$::on_activate(const rclcpp_lifecycle::State & /*previo
return CallbackReturn::SUCCESS;
}

CallbackReturn $ClassName$::on_deactivate(const rclcpp_lifecycle::State & /*previous_state*/)
CallbackReturn DummyClassName::on_deactivate(const rclcpp_lifecycle::State & /*previous_state*/)
{
return CallbackReturn::SUCCESS;
}

controller_interface::return_type $ClassName$::update(
controller_interface::return_type DummyClassName::update(
const rclcpp::Time & time, const rclcpp::Duration & /*period*/)
{
auto current_command = input_command_.readFromRT();
Expand All @@ -167,8 +157,9 @@ controller_interface::return_type $ClassName$::update(
return controller_interface::return_type::OK;
}

} // namespace $package_name$
} // namespace dummy_package_namespace

#include "pluginlib/class_list_macros.hpp"

PLUGINLIB_EXPORT_CLASS($package_name$::$ClassName$, controller_interface::ControllerInterface)
PLUGINLIB_EXPORT_CLASS(
dummy_package_namespace::DummyClassName, controller_interface::ControllerInterface)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<library path="dummy_package_namespace">
<class name="dummy_package_namespace/DummyClassName"
type="dummy_package_namespace::DummyClassName" base_class_type="controller_interface::ControllerInterface">
<description>
DummyClassName ros2_control controller.
</description>
</class>
</library>
Loading

0 comments on commit 06f8060

Please sign in to comment.