-
Notifications
You must be signed in to change notification settings - Fork 334
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
Improve diff_drive_controller odometry velocity and timestep handling #1394
Open
josephduchesne
wants to merge
3
commits into
ros-controls:master
Choose a base branch
from
josephduchesne:master
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.
+45
−33
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,10 +45,9 @@ void Odometry::init(const rclcpp::Time & time) | |
timestamp_ = time; | ||
} | ||
|
||
bool Odometry::update(double left_pos, double right_pos, const rclcpp::Time & time) | ||
bool Odometry::updateFromPosition(double left_pos, double right_pos, const rclcpp::Time & time, const double dt) | ||
{ | ||
// We cannot estimate the speed with very small time intervals: | ||
const double dt = time.seconds() - timestamp_.seconds(); | ||
if (dt < 0.0001) | ||
{ | ||
return false; // Interval too small to integrate with | ||
|
@@ -59,52 +58,49 @@ bool Odometry::update(double left_pos, double right_pos, const rclcpp::Time & ti | |
const double right_wheel_cur_pos = right_pos * right_wheel_radius_; | ||
|
||
// Estimate velocity of wheels using old and current position: | ||
const double left_wheel_est_vel = left_wheel_cur_pos - left_wheel_old_pos_; | ||
const double right_wheel_est_vel = right_wheel_cur_pos - right_wheel_old_pos_; | ||
const double left_wheel_est_vel = (left_wheel_cur_pos - left_wheel_old_pos_)/dt; | ||
const double right_wheel_est_vel = (right_wheel_cur_pos - right_wheel_old_pos_)/dt; | ||
|
||
// Update old position with current: | ||
left_wheel_old_pos_ = left_wheel_cur_pos; | ||
right_wheel_old_pos_ = right_wheel_cur_pos; | ||
|
||
updateFromVelocity(left_wheel_est_vel, right_wheel_est_vel, time); | ||
updateFromVelocity(left_wheel_est_vel, right_wheel_est_vel, time, dt); | ||
|
||
return true; | ||
} | ||
|
||
bool Odometry::updateFromVelocity(double left_vel, double right_vel, const rclcpp::Time & time) | ||
bool Odometry::updateFromVelocity(double left_vel, double right_vel, const rclcpp::Time & time, const double dt) | ||
{ | ||
const double dt = time.seconds() - timestamp_.seconds(); | ||
|
||
// Compute linear and angular diff: | ||
const double linear = (left_vel + right_vel) * 0.5; | ||
// Now there is a bug about scout angular velocity | ||
const double angular = (right_vel - left_vel) / wheel_separation_; | ||
|
||
// Integrate odometry: | ||
integrateExact(linear, angular); | ||
integrateExact(linear, angular, dt); | ||
|
||
timestamp_ = time; | ||
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. timestamp_ class variable should be removed now (marked as deprecated to be removed with the old methods) 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. good catch |
||
|
||
// Estimate speeds using a rolling mean to filter them out: | ||
linear_accumulator_.accumulate(linear / dt); | ||
angular_accumulator_.accumulate(angular / dt); | ||
linear_accumulator_.accumulate(linear); | ||
angular_accumulator_.accumulate(angular); | ||
|
||
linear_ = linear_accumulator_.getRollingMean(); | ||
angular_ = angular_accumulator_.getRollingMean(); | ||
|
||
return true; | ||
} | ||
|
||
void Odometry::updateOpenLoop(double linear, double angular, const rclcpp::Time & time) | ||
void Odometry::updateFromVelocityOpenLoop(double linear, double angular, const rclcpp::Time & time, const double dt) | ||
{ | ||
/// Save last linear and angular velocity: | ||
linear_ = linear; | ||
angular_ = angular; | ||
|
||
/// Integrate odometry: | ||
const double dt = time.seconds() - timestamp_.seconds(); | ||
timestamp_ = time; | ||
integrateExact(linear * dt, angular * dt); | ||
integrateExact(linear, angular, dt); | ||
} | ||
|
||
void Odometry::resetOdometry() | ||
|
@@ -129,28 +125,28 @@ void Odometry::setVelocityRollingWindowSize(size_t velocity_rolling_window_size) | |
resetAccumulators(); | ||
} | ||
|
||
void Odometry::integrateRungeKutta2(double linear, double angular) | ||
void Odometry::integrateRungeKutta2(double linear, double angular, const double dt) | ||
{ | ||
const double direction = heading_ + angular * 0.5; | ||
const double direction = heading_ + angular * 0.5 * dt; | ||
|
||
/// Runge-Kutta 2nd order integration: | ||
x_ += linear * cos(direction); | ||
y_ += linear * sin(direction); | ||
heading_ += angular; | ||
x_ += linear * cos(direction) * dt; | ||
y_ += linear * sin(direction) * dt; | ||
heading_ += angular * dt; | ||
} | ||
|
||
void Odometry::integrateExact(double linear, double angular) | ||
void Odometry::integrateExact(double linear, double angular, const double dt) | ||
{ | ||
if (fabs(angular) < 1e-6) | ||
if (fabs(angular*dt) < 1e-6) | ||
{ | ||
integrateRungeKutta2(linear, angular); | ||
integrateRungeKutta2(linear, angular, dt); | ||
} | ||
else | ||
{ | ||
/// Exact integration (should solve problems when angular is zero): | ||
const double heading_old = heading_; | ||
const double r = linear / angular; | ||
heading_ += angular; | ||
const double r = linear / angular * dt; | ||
heading_ += angular * dt; | ||
x_ += r * (sin(heading_) - sin(heading_old)); | ||
y_ += -r * (cos(heading_) - cos(heading_old)); | ||
} | ||
|
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.
Why should the measured period be different from the period argument of the update method? This is calculated from the controller_manager, we can assume that this is always valid?
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 was thinking on this some more myself and came to the same conclusion.
I'm hoping to rework this PR sometime next week focusing on the code quality/cleanup aspects suggested here and remove the inconsistent time handling (which as you suggest is probably a distinction without a difference).
Recently I've switched to using the RT_Linux kernel and set up permissions for real-time and found a fairly dramatic difference in perceived responsiveness with it enabled in the diff drive controller. Something isn't handling time/dates/update timesteps properly and I think I incorrectly identified this as the culprit. It still might be, but I should have started off with unit tests (or at least minimum steps to reproduce) rather than complicating an otherwise straightforward code cleanup.