Skip to content

Commit

Permalink
Update templates to generate hardware interfaces on galactic+ (StoglR…
Browse files Browse the repository at this point in the history
  • Loading branch information
destogl authored Jan 18, 2022
1 parent f4a4040 commit 4a2ff16
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ The file name should use standard ROS format <my_cool_robot_hardware>.
A ``.cpp`` and ``.hpp`` files will added using this name.
If the class name is not set, it is guessed by camel-casing the file name.
If the package name is not set, it is guessed from the current path using the folder's name.
The script **has to be executed** from the folder where the package should be generated.
The script **has to be executed** from the package folder where the package should be generated.

**Note**: it is recomended to setup your package using :ref:`setup-new-package <uc-new-package>` scritpt.
**Note**: it is recommended to setup your package using :ref:`setup-new-package <uc-new-package>` script.

The scripts copies template files from the ``templates/ros2_control/hardware`` folder, rename the files, and replaces the placeholders.
The scripts copies template files from the ``templates/ros2_control/hardware`` folder, renames the files, and replaces the placeholders.
The scripts adds also a plugin description and simple test checking if the plugin can be loaded.

.. code-block:: bash
Expand Down
28 changes: 27 additions & 1 deletion scripts/ros2_control/setup-hardware-interface-package.bash
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,30 @@ for SED_FILE in "${FILES_TO_SED[@]}"; do
sed -i "s/\\\$Interface_Type\\\$/${INTERFACE_TYPE^}/g" $SED_FILE
done

# If type is "sensor" remove write and command_interfaces methods
if [[ "$INTERFACE_TYPE" == "sensor" ]]; then
METHODS_TO_DELETE=("write()" "export_command_interfaces()")
# Clean HPP
for DEL_METHOD in "${METHODS_TO_DELETE[@]}"; do
line_nr=`grep -n "${DEL_METHOD}" $HW_ITF_HPP | awk -F ":" '{print $1;}'`
let start_line=${line_nr}-1
let end_line=${line_nr}+1
sed -i "${start_line},${end_line}d" $HW_ITF_HPP
done
# Clean CPP
line_nr=`grep -n "write()" $HW_ITF_CPP | awk -F ":" '{print $1;}'`
let start_line=${line_nr}-1
let end_line=${line_nr}+5
sed -i "${start_line},${end_line}d" $HW_ITF_CPP

line_nr=`grep -n "export_command_interfaces()" $HW_ITF_CPP | awk -F ":" '{print $1;}'`
let start_line=${line_nr}-1
let end_line=${line_nr}+10
sed -i "${start_line},${end_line}d" $HW_ITF_CPP

# Remove command interfaces from test URDF
sed -i '/command_interface/d' $TEST_CPP
fi

# CMakeLists.txt: Remove comments if there any and add library
DEL_STRINGS=("# uncomment the following" "# further" "# find_package(<dependency>")
Expand Down Expand Up @@ -191,6 +215,7 @@ echo "ament_target_dependencies(" >> $TMP_FILE
echo " $PKG_NAME" >> $TMP_FILE
echo " hardware_interface" >> $TMP_FILE
echo " rclcpp" >> $TMP_FILE
echo " rclcpp_lifecycle" >> $TMP_FILE
echo ")" >> $TMP_FILE

# TODO(anyone): Delete after Foxy!!!
Expand Down Expand Up @@ -252,6 +277,7 @@ echo "ament_export_dependencies(" >> $TMP_FILE
echo " hardware_interface" >> $TMP_FILE
echo " pluginlib" >> $TMP_FILE
echo " rclcpp" >> $TMP_FILE
echo " rclcpp_lifecycle" >> $TMP_FILE
echo ")" >> $TMP_FILE

# Add last part
Expand All @@ -261,7 +287,7 @@ tail -n +$TEST_LINE CMakeLists.txt | tail -n +$CUT_LINE >> $TMP_FILE
mv $TMP_FILE CMakeLists.txt

# CMakeLists.txt & package.xml: Add dependencies if they not exist
DEP_PKGS=("rclcpp" "pluginlib" "hardware_interface")
DEP_PKGS=("rclcpp_lifecycle" "rclcpp" "pluginlib" "hardware_interface")

for DEP_PKG in "${DEP_PKGS[@]}"; do

Expand Down
29 changes: 12 additions & 17 deletions templates/ros2_control/hardware/robot_hardware_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,26 @@

namespace $package_name$
{
hardware_interface::return_type $ClassName$::configure(const hardware_interface::HardwareInfo & info)
CallbackReturn $ClassName$::on_init(const hardware_interface::HardwareInfo & info)
{
if (configure_default(info) != hardware_interface::return_type::OK) {
return hardware_interface::return_type::ERROR;
if (hardware_interface::$Interface_Type$Interface::on_init(info) != CallbackReturn::SUCCESS) {
return CallbackReturn::ERROR;
}

// TODO(anyone): read parameters and initialize the hardware
hw_states_.resize(info_.joints.size(), std::numeric_limits<double>::quiet_NaN());
hw_commands_.resize(info_.joints.size(), std::numeric_limits<double>::quiet_NaN());

status_ = hardware_interface::status::CONFIGURED;
return hardware_interface::return_type::OK;
return CallbackReturn::SUCCESS;
}

std::vector<hardware_interface::StateInterface> $ClassName$::export_state_interfaces()
{
std::vector<hardware_interface::StateInterface> state_interfaces;
for (uint i = 0; i < info_.joints.size(); i++) {
state_interfaces.emplace_back(hardware_interface::StateInterface(
// TODO(anyone): insert correct interfaces
info_.joints[i].name, hardware_interface::HW_IF_POSITION, &hw_states_[i]));
// TODO(anyone): insert correct interfaces
info_.joints[i].name, hardware_interface::HW_IF_POSITION, &hw_states_[i]));
}

return state_interfaces;
Expand All @@ -40,29 +39,25 @@ std::vector<hardware_interface::CommandInterface> $ClassName$::export_command_in
std::vector<hardware_interface::CommandInterface> command_interfaces;
for (uint i = 0; i < info_.joints.size(); i++) {
command_interfaces.emplace_back(hardware_interface::CommandInterface(
// TODO(anyone): insert correct interfaces
info_.joints[i].name, hardware_interface::HW_IF_POSITION, &hw_commands_[i]));
// TODO(anyone): insert correct interfaces
info_.joints[i].name, hardware_interface::HW_IF_POSITION, &hw_commands_[i]));
}

return command_interfaces;
}

hardware_interface::return_type $ClassName$::start()
CallbackReturn $ClassName$::on_activate(const rclcpp_lifecycle::State & /*previous_state*/)
{
// TODO(anyone): prepare the robot to receive commands

status_ = hardware_interface::status::STARTED;

return hardware_interface::return_type::OK;
return CallbackReturn::SUCCESS;
}

hardware_interface::return_type $ClassName$::stop()
CallbackReturn $ClassName$::on_deactivate(const rclcpp_lifecycle::State & /*previous_state*/)
{
// TODO(anyone): prepare the robot to stop receiving commands

status_ = hardware_interface::status::STOPPED;

return hardware_interface::return_type::OK;
return CallbackReturn::SUCCESS;
}

hardware_interface::return_type $ClassName$::read()
Expand Down
12 changes: 5 additions & 7 deletions templates/ros2_control/hardware/robot_hardware_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@

#include "$package_name$/visibility_control.h"
#include "hardware_interface/$interface_type$_interface.hpp"
#include "hardware_interface/base_interface.hpp"
#include "hardware_interface/handle.hpp"
#include "hardware_interface/hardware_info.hpp"
#include "hardware_interface/types/hardware_interface_return_values.hpp"
#include "hardware_interface/types/hardware_interface_status_values.hpp"
#include "rclcpp/macros.hpp"
#include "rclcpp_lifecycle/state.hpp"

namespace $package_name$
{
class $ClassName$
: public hardware_interface::BaseInterface<hardware_interface::$Interface_Type$Interface>
class $ClassName$ : public hardware_interface::$Interface_Type$Interface
{
public:
RCLCPP_SHARED_PTR_DEFINITIONS($ClassName$);

$PACKAGE_NAME$_PUBLIC
hardware_interface::return_type configure(const hardware_interface::HardwareInfo & info) override;
CallbackReturn on_init(const hardware_interface::HardwareInfo & info) override;

$PACKAGE_NAME$_PUBLIC
std::vector<hardware_interface::StateInterface> export_state_interfaces() override;
Expand All @@ -33,10 +31,10 @@ class $ClassName$
std::vector<hardware_interface::CommandInterface> export_command_interfaces() override;

$PACKAGE_NAME$_PUBLIC
hardware_interface::return_type start() override;
CallbackReturn on_activate(const rclcpp_lifecycle::State & previous_state) override;

$PACKAGE_NAME$_PUBLIC
hardware_interface::return_type stop() override;
CallbackReturn on_deactivate(const rclcpp_lifecycle::State & previous_state) override;

$PACKAGE_NAME$_PUBLIC
hardware_interface::return_type read() override;
Expand Down

0 comments on commit 4a2ff16

Please sign in to comment.