-
Notifications
You must be signed in to change notification settings - Fork 408
Update odometry implementation in diff_drive #1854
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
base: master
Are you sure you want to change the base?
Changes from 7 commits
fe3a568
13edbec
629cf5e
d387750
bd1283b
3048023
93806d6
1d47fed
a52855b
77eade3
831c9c7
a95a450
8ecb548
ec25473
3e04f37
93b0c0c
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 |
---|---|---|
|
@@ -73,6 +73,31 @@ bool Odometry::update(double left_pos, double right_pos, const rclcpp::Time & ti | |
return true; | ||
} | ||
|
||
bool Odometry::updateFromPos( | ||
const double left_pos, const double right_pos, const rclcpp::Time & time) | ||
{ | ||
const double dt = time.seconds() - timestamp_.seconds(); | ||
// We cannot estimate angular velocity with very small time intervals | ||
if (std::fabs(dt) < 1e-6) | ||
{ | ||
return false; | ||
} | ||
|
||
// Estimate angular velocity of wheels using old and current position [rads/s]: | ||
double left_vel = (left_pos - left_wheel_old_pos_) / dt; | ||
double right_vel = (right_pos - right_wheel_old_pos_) / dt; | ||
|
||
// Update old position with current: | ||
left_wheel_old_pos_ = left_pos; | ||
right_wheel_old_pos_ = right_pos; | ||
|
||
if (updateFromVel(left_vel, right_vel, time)) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
Amronos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
bool Odometry::updateFromVelocity(double left_vel, double right_vel, const rclcpp::Time & time) | ||
{ | ||
const double dt = time.seconds() - timestamp_.seconds(); | ||
|
@@ -100,6 +125,31 @@ bool Odometry::updateFromVelocity(double left_vel, double right_vel, const rclcp | |
return true; | ||
} | ||
|
||
bool Odometry::updateFromVel( | ||
const double left_vel, const double right_vel, const rclcpp::Time & time) | ||
{ | ||
const double dt = time.seconds() - timestamp_.seconds(); | ||
|
||
|
||
// Compute linear and angular velocities of the robot: | ||
const double linear_vel = (left_vel * left_wheel_radius_ + right_vel * right_wheel_radius_) * 0.5; | ||
const double angular_vel = | ||
(right_vel * right_wheel_radius_ - left_vel * left_wheel_radius_) / wheel_separation_; | ||
|
||
// Integrate odometry: | ||
integrate(linear_vel * dt, angular_vel * dt); | ||
|
||
timestamp_ = time; | ||
|
||
// Estimate speeds using a rolling mean to filter them out: | ||
linear_accumulator_.accumulate(linear_vel); | ||
angular_accumulator_.accumulate(angular_vel); | ||
|
||
linear_ = linear_accumulator_.getRollingMean(); | ||
angular_ = angular_accumulator_.getRollingMean(); | ||
|
||
return true; | ||
} | ||
|
||
void Odometry::updateOpenLoop(double linear, double angular, const rclcpp::Time & time) | ||
{ | ||
/// Save last linear and angular velocity: | ||
|
@@ -112,6 +162,23 @@ void Odometry::updateOpenLoop(double linear, double angular, const rclcpp::Time | |
integrateExact(linear * dt, angular * dt); | ||
} | ||
|
||
bool Odometry::tryUpdateOpenLoop( | ||
const double linear_vel, const double angular_vel, const rclcpp::Time & time) | ||
{ | ||
const double dt = time.seconds() - timestamp_.seconds(); | ||
|
||
// Integrate odometry: | ||
integrate(linear_vel * dt, angular_vel * dt); | ||
|
||
timestamp_ = time; | ||
|
||
// Save last linear and angular velocity: | ||
linear_ = linear_vel; | ||
angular_ = angular_vel; | ||
|
||
return true; | ||
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. why a return value, which is always true? 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 is there to maintain consistency with other update methods, make it easier to add code to this function in the future that may return false, and make the |
||
} | ||
|
||
void Odometry::resetOdometry() | ||
{ | ||
x_ = 0.0; | ||
|
@@ -161,6 +228,24 @@ void Odometry::integrateExact(double linear, double angular) | |
} | ||
} | ||
|
||
void Odometry::integrate(const double dx, const double dheading) | ||
{ | ||
if (fabs(dheading) < 1e-6) | ||
{ | ||
// For very small dheading, approximate to linear motion | ||
x_ += (dx * std::cos(heading_)); | ||
y_ += (dx * std::sin(heading_)); | ||
heading_ += dheading; | ||
} | ||
else | ||
{ | ||
const double heading_old = heading_; | ||
heading_ += dheading; | ||
x_ += ((dx / dheading) * (std::sin(heading_) - std::sin(heading_old))); | ||
y_ += -(dx / dheading) * (std::cos(heading_) - std::cos(heading_old)); | ||
} | ||
} | ||
|
||
void Odometry::resetAccumulators() | ||
{ | ||
linear_accumulator_ = RollingMeanAccumulator(velocity_rolling_window_size_); | ||
|
Uh oh!
There was an error while loading. Please reload this page.