-
Notifications
You must be signed in to change notification settings - Fork 304
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
Working async controllers and components [not synchronized] #1041
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
43e36c3
add read + write, move async controllers
VX792 b4ca790
add header guards + license
VX792 871537f
precommit
VX792 c5f02a1
remove stuff added to a deprecated fn
VX792 4a8d89c
Merge branch 'master' into finishing-async-stuff
bmagyar f03a2d0
Merge branch 'master' into finishing-async-stuff
bmagyar 7f6eb29
reformat
bmagyar ed305de
Rework handles to support atomic doubles
VX792 ad1510a
remove comment
VX792 58f90b2
Revert "remove comment"
VX792 8befba3
Revert "Rework handles to support atomic doubles"
VX792 5110243
Merge branch 'master' into finishing-async-stuff
VX792 67da21c
don't write in the first tick
VX792 9469f8b
Update the year in license.
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
111 changes: 111 additions & 0 deletions
111
controller_interface/include/controller_interface/async_controller.hpp
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,111 @@ | ||
// Copyright 2024 ros2_control development team | ||
// | ||
// 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__ASYNC_CONTROLLER_HPP_ | ||
#define CONTROLLER_INTERFACE__ASYNC_CONTROLLER_HPP_ | ||
|
||
#include <atomic> | ||
#include <memory> | ||
#include <thread> | ||
|
||
#include "controller_interface_base.hpp" | ||
#include "lifecycle_msgs/msg/state.hpp" | ||
|
||
namespace controller_interface | ||
{ | ||
|
||
class AsyncControllerThread | ||
{ | ||
public: | ||
/// Constructor for the AsyncControllerThread object. | ||
/** | ||
* | ||
* \param[in] controller shared pointer to a controller. | ||
* \param[in] cm_update_rate the controller manager's update rate. | ||
*/ | ||
AsyncControllerThread( | ||
std::shared_ptr<controller_interface::ControllerInterfaceBase> & controller, int cm_update_rate) | ||
: terminated_(false), controller_(controller), thread_{}, cm_update_rate_(cm_update_rate) | ||
{ | ||
} | ||
|
||
AsyncControllerThread(const AsyncControllerThread & t) = delete; | ||
AsyncControllerThread(AsyncControllerThread && t) = delete; | ||
|
||
// Destructor, called when the component is erased from its map. | ||
~AsyncControllerThread() | ||
{ | ||
terminated_.store(true, std::memory_order_seq_cst); | ||
if (thread_.joinable()) | ||
{ | ||
thread_.join(); | ||
} | ||
} | ||
|
||
/// Creates the controller's thread. | ||
/** | ||
* Called when the controller is activated. | ||
* | ||
*/ | ||
void activate() | ||
{ | ||
thread_ = std::thread(&AsyncControllerThread::controller_update_callback, this); | ||
} | ||
|
||
/// Periodically execute the controller's update method. | ||
/** | ||
* Callback of the async controller's thread. | ||
* **Not synchronized with the controller manager's write and read currently** | ||
* | ||
*/ | ||
void controller_update_callback() | ||
{ | ||
using TimePoint = std::chrono::system_clock::time_point; | ||
unsigned int used_update_rate = | ||
controller_->get_update_rate() == 0 ? cm_update_rate_ : controller_->get_update_rate(); | ||
|
||
auto previous_time = controller_->get_node()->now(); | ||
while (!terminated_.load(std::memory_order_relaxed)) | ||
{ | ||
auto const period = std::chrono::nanoseconds(1'000'000'000 / used_update_rate); | ||
TimePoint next_iteration_time = | ||
TimePoint(std::chrono::nanoseconds(controller_->get_node()->now().nanoseconds())); | ||
|
||
if (controller_->get_state().id() == lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE) | ||
{ | ||
auto const current_time = controller_->get_node()->now(); | ||
auto const measured_period = current_time - previous_time; | ||
previous_time = current_time; | ||
controller_->update( | ||
controller_->get_node()->now(), | ||
(controller_->get_update_rate() != cm_update_rate_ && controller_->get_update_rate() != 0) | ||
? rclcpp::Duration::from_seconds(1.0 / controller_->get_update_rate()) | ||
: measured_period); | ||
} | ||
|
||
next_iteration_time += period; | ||
std::this_thread::sleep_until(next_iteration_time); | ||
} | ||
} | ||
|
||
private: | ||
std::atomic<bool> terminated_; | ||
std::shared_ptr<controller_interface::ControllerInterfaceBase> controller_; | ||
std::thread thread_; | ||
unsigned int cm_update_rate_; | ||
}; | ||
|
||
} // namespace controller_interface | ||
|
||
#endif // CONTROLLER_INTERFACE__ASYNC_CONTROLLER_HPP_ |
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
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'm afraid that
AsyncControllerThread::controller_update_callback
andAsyncComponentThread::write_and_read
can drift inexorably in long runs.Again I don't feel comfortable having data dependencies between threads that awake independently.