-
Notifications
You must be signed in to change notification settings - Fork 429
Adding base class for chained controllers: ChainedControllersInterface
#663
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
Merged
destogl
merged 5 commits into
ros-controls:master
from
destogl:chained-controllers-interface-pr
May 17, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d19c724
Extending ControllerInterface with methods for chainable controllers.
destogl 03ae2b2
Add ControllerInterfaceBase class and use it in Controller Manager.
destogl c5d7383
Make nice user interface to export controllers.
destogl 862987e
Revert some changes to work on this stage.
destogl 6ca2227
Apply suggestions from code review
destogl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
controller_interface/include/controller_interface/chainable_controller_interface.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright (c) 2022, Stogl Robotics Consulting UG (haftungsbeschränkt) | ||
| // | ||
| // 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 CONTROLLER_INTERFACE__CHAINABLE_CONTROLLER_INTERFACE_HPP_ | ||
| #define CONTROLLER_INTERFACE__CHAINABLE_CONTROLLER_INTERFACE_HPP_ | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "controller_interface/controller_interface_base.hpp" | ||
| #include "controller_interface/visibility_control.h" | ||
| #include "hardware_interface/handle.hpp" | ||
|
|
||
| namespace controller_interface | ||
| { | ||
| /// Virtual class to implement when integrating a controller that can be preceded by other | ||
| /// controllers. | ||
| /** | ||
| * Specialization of ControllerInterface class to force implementation of methods specific for | ||
| * "chainable" controller, i.e., controller that can be preceded by an another controller, for | ||
| * example inner controller of an control cascade. | ||
| * | ||
| */ | ||
| class ChainableControllerInterface : public ControllerInterfaceBase | ||
| { | ||
| public: | ||
| CONTROLLER_INTERFACE_PUBLIC | ||
| ChainableControllerInterface(); | ||
|
|
||
| CONTROLLER_INTERFACE_PUBLIC | ||
| virtual ~ChainableControllerInterface() = default; | ||
|
|
||
| CONTROLLER_INTERFACE_PUBLIC | ||
| return_type update(const rclcpp::Time & time, const rclcpp::Duration & period) final; | ||
|
|
||
| CONTROLLER_INTERFACE_PUBLIC | ||
| bool is_chainable() const final; | ||
|
|
||
| CONTROLLER_INTERFACE_PUBLIC | ||
| std::vector<hardware_interface::CommandInterface> export_reference_interfaces() final; | ||
|
|
||
| CONTROLLER_INTERFACE_PUBLIC | ||
| bool set_chained_mode(bool chained_mode) final; | ||
destogl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| CONTROLLER_INTERFACE_PUBLIC | ||
| bool is_in_chained_mode() const final; | ||
|
|
||
| protected: | ||
| /// Virtual method that each chainable controller should implement to export its chainable | ||
| /// interfaces. | ||
| /** | ||
| * Each chainable controller implements this methods where all input (command) interfaces are | ||
| * exported. The method has the same meaning as `export_command_interface` method from | ||
| * hardware_interface::SystemInterface or hardware_interface::ActuatorInterface. | ||
| * | ||
| * \returns list of CommandInterfaces that other controller can use as their outputs. | ||
| */ | ||
| virtual std::vector<hardware_interface::CommandInterface> on_export_reference_interfaces() = 0; | ||
|
|
||
| /// Virtual method that each chainable controller should implement to switch chained mode. | ||
| /** | ||
| * Each chainable controller implements this methods to switch between "chained" and "external" | ||
| * mode. In "chained" mode all external interfaces like subscriber and service servers are | ||
| * disabled to avoid potential concurrency in input commands. | ||
| * | ||
| * \param[in] flag marking a switch to or from chained mode. | ||
| * | ||
| * \returns true if controller successfully switched between "chained" and "external" mode. \default returns true so the method don't have to be overridden if controller can always switch chained mode. | ||
| */ | ||
| virtual bool on_set_chained_mode(bool chained_mode); | ||
|
|
||
| /// Update reference from input topics when not in chained mode. | ||
| /** | ||
| * Each chainable controller implements this method to update reference from subscribers when not | ||
| * in chained mode. | ||
| * | ||
| * \returns return_type::OK if update is successfully, otherwise return_type::ERROR. | ||
| */ | ||
| virtual return_type update_reference_from_subscribers() = 0; | ||
|
|
||
| /// Execute calculations of the controller and update command interfaces. | ||
| /** | ||
| * Update method for chainable controllers. | ||
| * In this method is valid to assume that \reference_interfaces_ hold the values for calculation | ||
| * of the commands in the current control step. | ||
| * This means that this method is called after \update_reference_from_subscribers if controller is | ||
| * not in chained mode. | ||
| * | ||
| * \returns return_type::OK if calculation and writing of interface is successfully, otherwise | ||
| * return_type::ERROR. | ||
| */ | ||
| virtual return_type update_and_write_commands( | ||
| const rclcpp::Time & time, const rclcpp::Duration & period) = 0; | ||
|
|
||
| /// Storage of values for reference interfaces | ||
| std::vector<double> reference_interfaces_; | ||
|
|
||
| private: | ||
| /// A flag marking if a chainable controller is currently preceded by another controller. | ||
| bool in_chained_mode_ = false; | ||
| }; | ||
|
|
||
| } // namespace controller_interface | ||
|
|
||
| #endif // CONTROLLER_INTERFACE__CHAINABLE_CONTROLLER_INTERFACE_HPP_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
controller_interface/src/chainable_controller_interface.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Copyright (c) 2022, Stogl Robotics Consulting UG (haftungsbeschränkt) | ||
| // | ||
| // 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 "controller_interface/chainable_controller_interface.hpp" | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "hardware_interface/types/lifecycle_state_names.hpp" | ||
| #include "lifecycle_msgs/msg/state.hpp" | ||
|
|
||
| namespace controller_interface | ||
| { | ||
| ChainableControllerInterface::ChainableControllerInterface() : ControllerInterfaceBase() {} | ||
|
|
||
| bool ChainableControllerInterface::is_chainable() const { return true; } | ||
|
|
||
| return_type ChainableControllerInterface::update( | ||
| const rclcpp::Time & time, const rclcpp::Duration & period) | ||
| { | ||
| return_type ret = return_type::ERROR; | ||
|
|
||
| if (!is_in_chained_mode()) | ||
| { | ||
| ret = update_reference_from_subscribers(); | ||
| if (ret != return_type::OK) | ||
| { | ||
| return ret; | ||
| } | ||
| } | ||
|
|
||
| ret = update_and_write_commands(time, period); | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| std::vector<hardware_interface::CommandInterface> | ||
| ChainableControllerInterface::export_reference_interfaces() | ||
| { | ||
| auto reference_interfaces = on_export_reference_interfaces(); | ||
|
|
||
| // check if the "reference_interfaces_" variable is resized to number of interfaces | ||
| if (reference_interfaces_.size() != reference_interfaces.size()) | ||
| { | ||
| // TODO(destogl): Should here be "FATAL"? It is fatal in terms of controller but not for the | ||
| // framework | ||
| RCLCPP_FATAL( | ||
| get_node()->get_logger(), | ||
| "The internal storage for reference values 'reference_interfaces_' variable has size '%zu', " | ||
| "but it is expected to have the size '%zu' equal to the number of exported reference " | ||
| "interfaces. No reference interface will be exported. Please correct and recompile " | ||
| "the controller with name '%s' and try again.", | ||
| reference_interfaces_.size(), reference_interfaces.size(), get_node()->get_name()); | ||
| reference_interfaces.clear(); | ||
| } | ||
|
|
||
| // check if the names of the reference interfaces begin with the controller's name | ||
| for (const auto & interface : reference_interfaces) | ||
| { | ||
| if (interface.get_name() != get_node()->get_name()) | ||
| { | ||
| RCLCPP_FATAL( | ||
| get_node()->get_logger(), | ||
| "The name of the interface '%s' does not begin with the controller's name. This is " | ||
| "mandatory " | ||
| " for reference interfaces. No reference interface will be exported. Please correct and " | ||
| "recompile the controller with name '%s' and try again.", | ||
| interface.get_full_name().c_str(), get_node()->get_name()); | ||
| reference_interfaces.clear(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return reference_interfaces; | ||
| } | ||
|
|
||
| bool ChainableControllerInterface::set_chained_mode(bool chained_mode) | ||
| { | ||
| bool result = false; | ||
|
|
||
| if (get_state().id() != lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE) | ||
| { | ||
| result = on_set_chained_mode(chained_mode); | ||
|
|
||
| if (result) | ||
| { | ||
| in_chained_mode_ = chained_mode; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| RCLCPP_ERROR( | ||
| get_node()->get_logger(), | ||
| "Can not change controller's chained mode because it is no in '%s' state. " | ||
| "Current state is '%s'.", | ||
| hardware_interface::lifecycle_state_names::UNCONFIGURED, get_state().label().c_str()); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| bool ChainableControllerInterface::is_in_chained_mode() const { return in_chained_mode_; } | ||
|
|
||
| bool ChainableControllerInterface::on_set_chained_mode(bool /*chained_mode*/) { return true; } | ||
|
|
||
| } // namespace controller_interface |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.