-
Notifications
You must be signed in to change notification settings - Fork 305
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
Issue 759: This will add the cleanup service #1236
Open
bailaC
wants to merge
12
commits into
ros-controls:master
Choose a base branch
from
StoglRobotics-forks:issue-759
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ae8b359
Issue-759: Adding cleanup service
bailaC b204120
Revert "Issue-759: Adding cleanup service"
bailaC 85a8fe9
Issue-759: Adding cleanup service
bailaC bde0339
Optimize cleanup controllers method.
destogl 2607b38
Fixing format
bailaC fd1449b
fixing pre-commit changes
bailaC bcb013d
Adding tests for cleanup service.
bailaC 4d4ed45
Merge branch 'master' into issue-759
destogl 6a6fe91
Adding controller_manager tests for cleanup controller
bailaC 8dfefc9
Removed unwanted file.
bailaC 872c385
Merge branch 'master' into issue-759
bailaC 418e47a
Correcting clang format and test case
bailaC 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -41,6 +41,18 @@ rclcpp::QoS qos_services = | |
.reliable() | ||
.durability_volatile(); | ||
|
||
inline bool is_controller_unconfigured( | ||
const controller_interface::ControllerInterfaceBase & controller) | ||
{ | ||
return controller.get_state().id() == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED; | ||
} | ||
|
||
inline bool is_controller_unconfigured( | ||
const controller_interface::ControllerInterfaceBaseSharedPtr & controller) | ||
{ | ||
return is_controller_unconfigured(*controller); | ||
} | ||
|
||
inline bool is_controller_inactive(const controller_interface::ControllerInterfaceBase & controller) | ||
{ | ||
return controller.get_state().id() == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE; | ||
|
@@ -504,6 +516,10 @@ void ControllerManager::init_services() | |
"~/unload_controller", | ||
std::bind(&ControllerManager::unload_controller_service_cb, this, _1, _2), qos_services, | ||
best_effort_callback_group_); | ||
cleanup_controller_service_ = create_service<controller_manager_msgs::srv::CleanupController>( | ||
"~/cleanup_controller", | ||
std::bind(&ControllerManager::cleanup_controller_service_cb, this, _1, _2), qos_services, | ||
best_effort_callback_group_); | ||
list_hardware_components_service_ = | ||
create_service<controller_manager_msgs::srv::ListHardwareComponents>( | ||
"~/list_hardware_components", | ||
|
@@ -602,52 +618,109 @@ controller_interface::ControllerInterfaceBaseSharedPtr ControllerManager::load_c | |
return load_controller(controller_name, controller_type); | ||
} | ||
|
||
controller_interface::return_type ControllerManager::unload_controller( | ||
controller_interface::return_type ControllerManager::cleanup_controller( | ||
const std::string & controller_name) | ||
{ | ||
std::lock_guard<std::recursive_mutex> guard(rt_controllers_wrapper_.controllers_lock_); | ||
std::vector<ControllerSpec> & to = rt_controllers_wrapper_.get_unused_list(guard); | ||
const std::vector<ControllerSpec> & from = rt_controllers_wrapper_.get_updated_list(guard); | ||
RCLCPP_INFO(get_logger(), "Cleanup controller '%s'", controller_name.c_str()); | ||
|
||
// Transfers the active controllers over, skipping the one to be removed and the active ones. | ||
to = from; | ||
const auto & controllers = get_loaded_controllers(); | ||
|
||
auto found_it = std::find_if( | ||
to.begin(), to.end(), | ||
controllers.begin(), controllers.end(), | ||
std::bind(controller_name_compare, std::placeholders::_1, controller_name)); | ||
if (found_it == to.end()) | ||
|
||
if (found_it == controllers.end()) | ||
{ | ||
// Fails if we could not remove the controllers | ||
to.clear(); | ||
RCLCPP_ERROR( | ||
get_logger(), | ||
"Could not unload controller with name '%s' because no controller with this name exists", | ||
"Could not cleanup controller with name '%s' because no controller with this name exists", | ||
controller_name.c_str()); | ||
return controller_interface::return_type::ERROR; | ||
} | ||
auto controller = found_it->c; | ||
|
||
auto & controller = *found_it; | ||
if (is_controller_unconfigured(controller)) | ||
{ | ||
// all good nothing to do! | ||
return controller_interface::return_type::OK; | ||
} | ||
|
||
if (is_controller_active(*controller.c)) | ||
auto state = controller->get_state(); | ||
if ( | ||
state.id() == lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE || | ||
state.id() == lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED) | ||
{ | ||
to.clear(); | ||
RCLCPP_ERROR( | ||
get_logger(), "Could not unload controller with name '%s' because it is still active", | ||
controller_name.c_str()); | ||
get_logger(), "Controller '%s' can not be cleaned-up from '%s' state.", | ||
controller_name.c_str(), state.label().c_str()); | ||
return controller_interface::return_type::ERROR; | ||
} | ||
if (controller.c->is_async()) | ||
|
||
// ASYNCHRONOUS CONTROLLERS: Stop background thread for update | ||
if (controller->is_async()) | ||
{ | ||
RCLCPP_DEBUG( | ||
get_logger(), "Removing controller '%s' from the list of async controllers", | ||
controller_name.c_str()); | ||
async_controller_threads_.erase(controller_name); | ||
} | ||
|
||
// CHAINABLE CONTROLLERS: remove reference interfaces of chainable controllers | ||
if (controller->is_chainable()) | ||
{ | ||
RCLCPP_DEBUG( | ||
get_logger(), | ||
"Controller '%s' is chainable. Interfaces are being removed from resource manager.", | ||
controller_name.c_str()); | ||
resource_manager_->remove_controller_reference_interfaces(controller_name); | ||
} | ||
|
||
RCLCPP_DEBUG(get_logger(), "Cleanup controller"); | ||
// TODO(destogl): remove reference interface if chainable; i.e., add a separate method for | ||
// cleaning-up controllers? | ||
controller.c->get_node()->cleanup(); | ||
auto new_state = controller->get_node()->cleanup(); | ||
if (new_state.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED) | ||
{ | ||
RCLCPP_ERROR( | ||
get_logger(), "After cleanup-up, controller '%s' is in state '%s', expected 'unconfigured'.", | ||
controller_name.c_str(), new_state.label().c_str()); | ||
return controller_interface::return_type::ERROR; | ||
} | ||
|
||
RCLCPP_DEBUG(get_logger(), "Successfully cleaned-up controller '%s'", controller_name.c_str()); | ||
|
||
return controller_interface::return_type::OK; | ||
} | ||
|
||
controller_interface::return_type ControllerManager::unload_controller( | ||
const std::string & controller_name) | ||
{ | ||
// first find and clean controller if it is inactive | ||
if (cleanup_controller(controller_name) != controller_interface::return_type::OK) | ||
{ | ||
return controller_interface::return_type::ERROR; | ||
} | ||
|
||
std::lock_guard<std::recursive_mutex> guard(rt_controllers_wrapper_.controllers_lock_); | ||
std::vector<ControllerSpec> & to = rt_controllers_wrapper_.get_unused_list(guard); | ||
const std::vector<ControllerSpec> & from = rt_controllers_wrapper_.get_updated_list(guard); | ||
|
||
to = from; | ||
auto found_it = std::find_if( | ||
to.begin(), to.end(), | ||
std::bind(controller_name_compare, std::placeholders::_1, controller_name)); | ||
if (found_it == to.end()) | ||
{ | ||
// Fails if we could not remove the controllers | ||
to.clear(); | ||
RCLCPP_ERROR( | ||
get_logger(), | ||
"Could not unload controller with name '%s' because no controller with this name exists", | ||
controller_name.c_str()); | ||
return controller_interface::return_type::ERROR; | ||
} | ||
|
||
auto & controller = *found_it; | ||
|
||
RCLCPP_DEBUG(get_logger(), "Unload controller"); | ||
executor_->remove_node(controller.c->get_node()->get_node_base_interface()); | ||
to.erase(found_it); | ||
|
||
|
@@ -723,7 +796,7 @@ controller_interface::return_type ControllerManager::configure_controller( | |
if (new_state.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE) | ||
{ | ||
RCLCPP_ERROR( | ||
get_logger(), "After configuring, controller '%s' is in state '%s' , expected inactive.", | ||
get_logger(), "After configuring, controller '%s' is in state '%s', expected 'inactive'.", | ||
controller_name.c_str(), new_state.label().c_str()); | ||
return controller_interface::return_type::ERROR; | ||
} | ||
|
@@ -1846,6 +1919,21 @@ void ControllerManager::unload_controller_service_cb( | |
get_logger(), "unloading service finished for controller '%s' ", request->name.c_str()); | ||
} | ||
|
||
void ControllerManager::cleanup_controller_service_cb( | ||
const std::shared_ptr<controller_manager_msgs::srv::CleanupController::Request> request, | ||
std::shared_ptr<controller_manager_msgs::srv::CleanupController::Response> response) | ||
{ | ||
// lock services | ||
RCLCPP_DEBUG(get_logger(), "cleanup service called for controller '%s' ", request->name.c_str()); | ||
std::lock_guard<std::mutex> guard(services_lock_); | ||
RCLCPP_DEBUG(get_logger(), "cleanup service locked"); | ||
|
||
response->ok = cleanup_controller(request->name) == controller_interface::return_type::OK; | ||
|
||
RCLCPP_DEBUG( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Print the cleanup finished message if the response of the |
||
get_logger(), "cleanup service finished for controller '%s' ", request->name.c_str()); | ||
} | ||
|
||
void ControllerManager::list_hardware_components_srv_cb( | ||
const std::shared_ptr<controller_manager_msgs::srv::ListHardwareComponents::Request>, | ||
std::shared_ptr<controller_manager_msgs::srv::ListHardwareComponents::Response> response) | ||
|
This file contains 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
This file contains 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,10 @@ | ||
# The CleanupController service allows you to cleanup a single controller | ||
# from controller_manager | ||
|
||
# To cleanup a controller, specify the "name" of the controller. | ||
# The return value "ok" indicates if the controller was successfully | ||
# cleaned up or not | ||
|
||
string name | ||
--- | ||
bool ok |
This file contains 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,40 @@ | ||
# Copyright 2020 PAL Robotics S.L. | ||
# | ||
# 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. | ||
|
||
from controller_manager import cleanup_controller | ||
|
||
from ros2cli.node.direct import add_arguments | ||
from ros2cli.node.strategy import NodeStrategy | ||
from ros2cli.verb import VerbExtension | ||
|
||
from ros2controlcli.api import add_controller_mgr_parsers, LoadedControllerNameCompleter | ||
|
||
|
||
class CleanupControllerVerb(VerbExtension): | ||
"""Cleanup a controller in a controller manager.""" | ||
|
||
def add_arguments(self, parser, cli_name): | ||
add_arguments(parser) | ||
arg = parser.add_argument("controller_name", help="Name of the controller") | ||
arg.completer = LoadedControllerNameCompleter() | ||
add_controller_mgr_parsers(parser) | ||
|
||
def main(self, *, args): | ||
with NodeStrategy(args) as node: | ||
response = cleanup_controller(node, args.controller_manager, args.controller_name) | ||
if not response.ok: | ||
return "Error cleanup controllers, check controller_manager logs" | ||
|
||
print(f"Successfully cleaned up controller {args.controller_name}") | ||
return 0 |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the above cleanup controller call should come after this, if not you get the error message of controller not found from cleanup rather than the unload controller