-
Notifications
You must be signed in to change notification settings - Fork 431
Spin timer improvement #646
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,17 +41,19 @@ int main(int argc, char ** argv) | |||||||||
| RCLCPP_INFO(cm->get_logger(), "update rate is %d Hz", cm->get_update_rate()); | ||||||||||
|
|
||||||||||
| rclcpp::Time begin = cm->now(); | ||||||||||
| rclcpp::Time begin_last = begin; | ||||||||||
| rclcpp::Time end = begin; | ||||||||||
|
|
||||||||||
| // Use nanoseconds to avoid chrono's rounding | ||||||||||
| std::this_thread::sleep_for(std::chrono::nanoseconds(1000000000 / cm->get_update_rate())); | ||||||||||
| while (rclcpp::ok()) | ||||||||||
| { | ||||||||||
| rclcpp::Time begin_last = begin; | ||||||||||
| begin = cm->now(); | ||||||||||
|
Contributor
Author
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. Start the timer right at the beginning of the new iteration
Contributor
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. This does seem like a better order. |
||||||||||
| cm->read(); | ||||||||||
|
Comment on lines
51
to
52
Member
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. I think we should rather use something like this:
Suggested change
In this case, only update method is not counted in the period. This is not 100% exact when having multiple controller but closer to the real value. |
||||||||||
| cm->update(begin, begin - begin_last); | ||||||||||
| begin_last = begin; | ||||||||||
| cm->write(); | ||||||||||
| rclcpp::Time end = cm->now(); | ||||||||||
| end = cm->now(); | ||||||||||
| std::this_thread::sleep_for(std::max( | ||||||||||
|
Contributor
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. This looks like it would cause the time to drift each loop since it's sleeping for a fixed amount of time instead of until the end of the period. Would ros::rate work here? Or setting a hard end time at the beginning of each loop based on the original start? I can test that out if it seems like a reasonable approach.
Contributor
Author
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. If you look at L35, there's a comment about why What's here seems fine to me but please feel free to test out your other idea. 👍 This still isn't working as well as I'd hope for. (It's not clear if this bit of code is the cause, though) |
||||||||||
| std::chrono::nanoseconds(0), | ||||||||||
| std::chrono::nanoseconds(1000000000 / cm->get_update_rate()) - | ||||||||||
|
|
||||||||||
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.
This issue was closed