Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gpio command controller #1251

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
5fecf27
added gpio_command_controller
mcbed Nov 2, 2022
6ffda9d
added doc to gpio controllers
mcbed Nov 2, 2022
f88fc7d
changed parameter interface_names to command_interfaces
mcbed Nov 2, 2022
56fc1e5
added test
mcbed Nov 2, 2022
9e73ab7
code formating
mcbed Nov 2, 2022
1cbd3a6
added gpio state publisher
Nov 2, 2022
7228e06
fixed broken node_state
Nov 2, 2022
880aeea
added DynamicJointState message for Gpio state
Nov 2, 2022
94befb0
fixed tests and added gpio state tests
Nov 2, 2022
882c936
fixed format
Nov 16, 2022
efce87a
Apply pre-commit fix
Wiktor-99 Jul 31, 2024
b53bff6
Fix compilation on ros rolling
Wiktor-99 Aug 1, 2024
67b6069
Add parameters file, use generated gpio_command_controller_parameters…
Wiktor-99 Aug 3, 2024
30d2604
Remove unnecessary members: interface_names_, gpios_names_ and use pa…
Wiktor-99 Aug 4, 2024
4c824e8
Refactor of storing interface types
Wiktor-99 Aug 4, 2024
dd86d9e
Remove unnecessary includes
Wiktor-99 Aug 4, 2024
4ac938d
Refacotr on_activate method. Add validate_configured_interfaces and i…
Wiktor-99 Aug 4, 2024
aa59297
Refactor update method, split update of state if and command if
Wiktor-99 Aug 4, 2024
6276617
Move test fixture form hpp to cpp
Wiktor-99 Aug 4, 2024
66bd971
Update docks and package.xml
Wiktor-99 Aug 4, 2024
7b70159
Add state validation
Wiktor-99 Aug 4, 2024
58c1afc
Store command and state ifs in the map
Wiktor-99 Aug 5, 2024
e5c65db
Use interface maps for command and state update. Use DynamicJointStat…
Wiktor-99 Aug 5, 2024
0dc5c87
Use ordered state if vector instead of map
Wiktor-99 Aug 6, 2024
765a3cb
Restore state if validation
Wiktor-99 Aug 6, 2024
a1c8fa3
Align logging
Wiktor-99 Aug 6, 2024
de8acba
Update code layout
Wiktor-99 Aug 7, 2024
aebbf0b
Simplifie test fixture
Wiktor-99 Aug 7, 2024
0deb417
Update tests. Stop using pointer, set members as public
Wiktor-99 Aug 7, 2024
3e7a12a
Add set of test for init
Wiktor-99 Aug 9, 2024
373053e
Add setup_command_and_state_interfaces as refactor UTs
Wiktor-99 Aug 13, 2024
1dc481f
Add two UTs to check wrong ifs assing
Wiktor-99 Aug 15, 2024
2bad07b
Add create node option function and remove redundant test
Wiktor-99 Aug 15, 2024
f78158e
Extract redundant code to methods
Wiktor-99 Aug 15, 2024
815f85e
Reafactor command UTs
Wiktor-99 Aug 16, 2024
0265503
Add two UTs for different commands
Wiktor-99 Aug 16, 2024
28db5aa
Add one more test with command with different ports order
Wiktor-99 Aug 16, 2024
4be8fac
Add UT for the wrong gpio/port name in the command
Wiktor-99 Aug 17, 2024
fc10ad5
Update package.xml
Wiktor-99 Aug 17, 2024
1ac37e4
Merge branch 'master' into gpio_controllers
Wiktor-99 Aug 17, 2024
e89d814
Update pkg version
Wiktor-99 Aug 22, 2024
9c90fb2
Fix includes
Wiktor-99 Aug 22, 2024
c165c1d
Fix linters
Wiktor-99 Aug 24, 2024
dd19879
Update toctree, add gpio controller info
Wiktor-99 Aug 24, 2024
b3d9ef4
Merge branch 'master' into gpio_controllers
Wiktor-99 Aug 26, 2024
4b60038
Merge branch 'master' into gpio_controllers
Wiktor-99 Sep 9, 2024
9fb380a
Merge branch 'master' into gpio_controllers
Wiktor-99 Sep 22, 2024
a2b1f4f
Merge branch 'master' into gpio_controllers
bmagyar Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/controllers_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ The controllers are using `common hardware interface definitions`_, and may use
PID Controller <../pid_controller/doc/userdoc.rst>
Position Controllers <../position_controllers/doc/userdoc.rst>
Velocity Controllers <../velocity_controllers/doc/userdoc.rst>
Gpio Command Controller <../gpio_controllers/doc/userdoc.rst>


Broadcasters
Expand Down
113 changes: 113 additions & 0 deletions gpio_controllers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
cmake_minimum_required(VERSION 3.8)
project(gpio_controllers)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(controller_interface REQUIRED)
find_package(hardware_interface REQUIRED)
find_package(pluginlib REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(realtime_tools REQUIRED)
find_package(std_msgs REQUIRED)
find_package(generate_parameter_library REQUIRED)


generate_parameter_library(gpio_command_controller_parameters
src/gpio_command_controller_parameters.yaml
)

add_library(gpio_controllers
SHARED
src/gpio_command_controller.cpp
)
target_include_directories(gpio_controllers PRIVATE include)
target_link_libraries(gpio_controllers PUBLIC gpio_command_controller_parameters)
ament_target_dependencies(gpio_controllers PUBLIC
builtin_interfaces
controller_interface
hardware_interface
pluginlib
rclcpp_lifecycle
rcutils
realtime_tools
std_msgs
)
# Causes the visibility macros to use dllexport rather than dllimport,
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(gpio_controllers PRIVATE "GPIO_COMMAND_CONTROLLER_BUILDING_DLL")
pluginlib_export_plugin_description_file(controller_interface gpio_controllers_plugin.xml)

install(
DIRECTORY include/
DESTINATION include
)

install(
TARGETS
gpio_controllers
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
)

if(BUILD_TESTING)
find_package(ament_cmake_gmock REQUIRED)
find_package(controller_manager REQUIRED)
find_package(hardware_interface REQUIRED)
find_package(ros2_control_test_assets REQUIRED)

ament_add_gmock(
test_load_gpio_command_controller
test/test_load_gpio_command_controller.cpp
)

target_include_directories(test_load_gpio_command_controller PRIVATE include)
ament_target_dependencies(test_load_gpio_command_controller
controller_manager
hardware_interface
ros2_control_test_assets
)

ament_add_gmock(
test_gpio_command_controller
test/test_gpio_command_controller.cpp
)
target_include_directories(test_gpio_command_controller PRIVATE include)
target_link_libraries(test_gpio_command_controller
gpio_controllers
)
ament_target_dependencies(test_gpio_command_controller
controller_interface
hardware_interface
rclcpp
rclcpp_lifecycle
realtime_tools
std_msgs
)
endif()

ament_export_dependencies(
controller_interface
hardware_interface
rclcpp
rclcpp_lifecycle
realtime_tools
std_msgs
)
ament_export_include_directories(
include
)
ament_export_libraries(
gpio_controllers
)
ament_package()
41 changes: 41 additions & 0 deletions gpio_controllers/doc/userdoc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.. _gpio_controllers_userdoc:

gpio_controllers
=====================

This is a collection of controllers for gpios that work with multiple interfaces.

Hardware interface type
-----------------------

These controllers work with gpios using user defined command interfaces.

Using GPIO Command Controller
-----------------------------
The controller expects at least one gpio interface abd the corresponding command interface names.
A yaml file for using it could be:
.. code-block:: yaml

controller_manager:
ros__parameters:
update_rate: 100 # Hz
joint_state_broadcaster:
type: joint_state_broadcaster/JointStateBroadcaster
gpio_command_controller:
type: gpio_controllers/GpioCommandController

gpio_command_controller:
ros__parameters:
gpios:
- Gpio1
- Gpio2
command_interfaces:
Gpio1:
- ports:
- dig.1
- dig.2
- dig.3
Gpio2:
-ports:
- ana.1
- ana.2
7 changes: 7 additions & 0 deletions gpio_controllers/gpio_controllers_plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<library path="gpio_controllers">
<class name="gpio_controllers/GpioCommandController" type="gpio_controllers::GpioCommandController" base_class_type="controller_interface::ControllerInterface">
<description>
The gpio command controller commands a group of gpios using multiple interfaces.
</description>
</class>
</library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2022 ICUBE Laboratory, University of Strasbourg
//
// 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.

#ifndef GPIO_CONTROLLERS__GPIO_COMMAND_CONTROLLER_HPP_
#define GPIO_CONTROLLERS__GPIO_COMMAND_CONTROLLER_HPP_

#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include "control_msgs/msg/dynamic_joint_state.hpp"
#include "controller_interface/controller_interface.hpp"
#include "gpio_command_controller_parameters.hpp"
#include "gpio_controllers/visibility_control.h"
#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
#include "rclcpp_lifecycle/state.hpp"
#include "realtime_tools/realtime_buffer.h"
#include "realtime_tools/realtime_publisher.h"

namespace gpio_controllers
{
using CmdType = control_msgs::msg::DynamicJointState;
using StateType = control_msgs::msg::DynamicJointState;
using CallbackReturn = controller_interface::CallbackReturn;
using InterfacesNames = std::vector<std::string>;
using MapOfReferencesToCommandInterfaces = std::unordered_map<
std::string, std::reference_wrapper<hardware_interface::LoanedCommandInterface>>;
using StateInterfaces =
std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface>>;

class GpioCommandController : public controller_interface::ControllerInterface
{
public:
GPIO_COMMAND_CONTROLLER_PUBLIC
GpioCommandController();

GPIO_COMMAND_CONTROLLER_PUBLIC
controller_interface::InterfaceConfiguration command_interface_configuration() const override;

GPIO_COMMAND_CONTROLLER_PUBLIC
controller_interface::InterfaceConfiguration state_interface_configuration() const override;

GPIO_COMMAND_CONTROLLER_PUBLIC
CallbackReturn on_init() override;

GPIO_COMMAND_CONTROLLER_PUBLIC
CallbackReturn on_configure(const rclcpp_lifecycle::State & previous_state) override;

GPIO_COMMAND_CONTROLLER_PUBLIC
CallbackReturn on_activate(const rclcpp_lifecycle::State & previous_state) override;

GPIO_COMMAND_CONTROLLER_PUBLIC
CallbackReturn on_deactivate(const rclcpp_lifecycle::State & previous_state) override;

GPIO_COMMAND_CONTROLLER_PUBLIC
controller_interface::return_type update(
const rclcpp::Time & time, const rclcpp::Duration & period) override;

private:
void store_interface_types();
void initialize_gpio_state_msg();
void update_gpios_states();
controller_interface::return_type update_gpios_commands();
MapOfReferencesToCommandInterfaces create_map_of_references_to_interfaces(
const InterfacesNames & interfaces_from_params);
template <typename T>
bool check_if_configured_interfaces_matches_received(
const InterfacesNames & interfaces_from_params, const T & interfaces_map);

protected:
InterfacesNames interface_types_;
MapOfReferencesToCommandInterfaces command_interfaces_map_;
StateInterfaces ordered_state_interfaces_;

realtime_tools::RealtimeBuffer<std::shared_ptr<CmdType>> rt_command_ptr_{};
rclcpp::Subscription<CmdType>::SharedPtr gpios_command_subscriber_{};

std::shared_ptr<rclcpp::Publisher<StateType>> gpio_state_publisher_{};
std::shared_ptr<realtime_tools::RealtimePublisher<StateType>> realtime_gpio_state_publisher_{};

std::shared_ptr<gpio_command_controller_parameters::ParamListener> param_listener_{};
gpio_command_controller_parameters::Params params_;
};

} // namespace gpio_controllers

#endif // GPIO_CONTROLLERS__GPIO_COMMAND_CONTROLLER_HPP_
49 changes: 49 additions & 0 deletions gpio_controllers/include/gpio_controllers/visibility_control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2022 ICUBE Laboratory, University of Strasbourg
//
// 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.

#ifndef GPIO_CONTROLLERS__VISIBILITY_CONTROL_H_
#define GPIO_CONTROLLERS__VISIBILITY_CONTROL_H_

// This logic was borrowed (then namespaced) from the examples on the gcc wiki:
// https://gcc.gnu.org/wiki/Visibility

#if defined _WIN32 || defined __CYGWIN__
#ifdef __GNUC__
#define GPIO_COMMAND_CONTROLLER_EXPORT __attribute__((dllexport))
#define GPIO_COMMAND_CONTROLLER_IMPORT __attribute__((dllimport))
#else
#define GPIO_COMMAND_CONTROLLER_EXPORT __declspec(dllexport)
#define GPIO_COMMAND_CONTROLLER_IMPORT __declspec(dllimport)
#endif
#ifdef GPIO_COMMAND_CONTROLLER_BUILDING_LIBRARY
#define GPIO_COMMAND_CONTROLLER_PUBLIC GPIO_COMMAND_CONTROLLER_EXPORT
#else
#define GPIO_COMMAND_CONTROLLER_PUBLIC GPIO_COMMAND_CONTROLLER_IMPORT
#endif
#define GPIO_COMMAND_CONTROLLER_PUBLIC_TYPE GPIO_COMMAND_CONTROLLER_PUBLIC
#define GPIO_COMMAND_CONTROLLER_LOCAL
#else
#define GPIO_COMMAND_CONTROLLER_EXPORT __attribute__((visibility("default")))
#define GPIO_COMMAND_CONTROLLER_IMPORT
#if __GNUC__ >= 4
#define GPIO_COMMAND_CONTROLLER_PUBLIC __attribute__((visibility("default")))
#define GPIO_COMMAND_CONTROLLER_LOCAL __attribute__((visibility("hidden")))
#else
#define GPIO_COMMAND_CONTROLLER_PUBLIC
#define GPIO_COMMAND_CONTROLLER_LOCAL
#endif
#define GPIO_COMMAND_CONTROLLER_PUBLIC_TYPE
#endif

#endif // GPIO_CONTROLLERS__VISIBILITY_CONTROL_H_
29 changes: 29 additions & 0 deletions gpio_controllers/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>gpio_controllers</name>
<version>4.12.1</version>
<description>Controllers to interact with gpios.</description>
<maintainer email="macbednarczyk@gmail.com">Maciej Bednarczyk</maintainer>
<maintainer email="wiktorbajor1@gmail.com">Wiktor Bajor</maintainer>
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>controller_interface</depend>
<depend>hardware_interface</depend>
<depend>rclcpp</depend>
<depend>rclcpp_lifecycle</depend>
<depend>realtime_tools</depend>
<depend>std_msgs</depend>

<build_depend>pluginlib</build_depend>

<test_depend>ament_cmake_gmock</test_depend>
<test_depend>controller_manager</test_depend>
<test_depend>ros2_control_test_assets</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Loading
Loading