Skip to content

Commit 54ad2f1

Browse files
roncapatmamueluth
authored andcommitted
Fix wrong integration of velocity feedback in odometry in diff_drive_controller (ros-controls#331)
1 parent 03bbe3a commit 54ad2f1

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

diff_drive_controller/src/diff_drive_controller.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ InterfaceConfiguration DiffDriveController::state_interface_configuration() cons
140140
}
141141

142142
controller_interface::return_type DiffDriveController::update(
143-
const rclcpp::Time & time, const rclcpp::Duration & /*period*/)
143+
const rclcpp::Time & time, const rclcpp::Duration & period)
144144
{
145145
auto logger = get_node()->get_logger();
146146
if (get_state().id() == State::PRIMARY_STATE_INACTIVE)
@@ -153,8 +153,6 @@ controller_interface::return_type DiffDriveController::update(
153153
return controller_interface::return_type::OK;
154154
}
155155

156-
const auto current_time = time;
157-
158156
std::shared_ptr<Twist> last_command_msg;
159157
received_velocity_msg_ptr_.get(last_command_msg);
160158

@@ -164,7 +162,7 @@ controller_interface::return_type DiffDriveController::update(
164162
return controller_interface::return_type::ERROR;
165163
}
166164

167-
const auto age_of_last_command = current_time - last_command_msg->header.stamp;
165+
const auto age_of_last_command = time - last_command_msg->header.stamp;
168166
// Brake if cmd_vel has timeout, override the stored command
169167
if (age_of_last_command > cmd_vel_timeout_)
170168
{
@@ -178,6 +176,8 @@ controller_interface::return_type DiffDriveController::update(
178176
double & linear_command = command.twist.linear.x;
179177
double & angular_command = command.twist.angular.z;
180178

179+
previous_update_timestamp_ = time;
180+
181181
// Apply (possibly new) multipliers:
182182
const auto wheels = wheel_params_;
183183
const double wheel_separation = wheels.separation_multiplier * wheels.separation;
@@ -186,7 +186,7 @@ controller_interface::return_type DiffDriveController::update(
186186

187187
if (odom_params_.open_loop)
188188
{
189-
odometry_.updateOpenLoop(linear_command, angular_command, current_time);
189+
odometry_.updateOpenLoop(linear_command, angular_command, time);
190190
}
191191
else
192192
{
@@ -214,25 +214,27 @@ controller_interface::return_type DiffDriveController::update(
214214

215215
if (odom_params_.position_feedback)
216216
{
217-
odometry_.update(left_feedback_mean, right_feedback_mean, current_time);
217+
odometry_.update(left_feedback_mean, right_feedback_mean, time);
218218
}
219219
else
220220
{
221-
odometry_.updateFromVelocity(left_feedback_mean, right_feedback_mean, current_time);
221+
odometry_.updateFromVelocity(
222+
left_feedback_mean*period.seconds(), right_feedback_mean*period.seconds(),
223+
time);
222224
}
223225
}
224226

225227
tf2::Quaternion orientation;
226228
orientation.setRPY(0.0, 0.0, odometry_.getHeading());
227229

228-
if (previous_publish_timestamp_ + publish_period_ < current_time)
230+
if (previous_publish_timestamp_ + publish_period_ < time)
229231
{
230232
previous_publish_timestamp_ += publish_period_;
231233

232234
if (realtime_odometry_publisher_->trylock())
233235
{
234236
auto & odometry_message = realtime_odometry_publisher_->msg_;
235-
odometry_message.header.stamp = current_time;
237+
odometry_message.header.stamp = time;
236238
odometry_message.pose.pose.position.x = odometry_.getX();
237239
odometry_message.pose.pose.position.y = odometry_.getY();
238240
odometry_message.pose.pose.orientation.x = orientation.x();
@@ -247,7 +249,7 @@ controller_interface::return_type DiffDriveController::update(
247249
if (odom_params_.enable_odom_tf && realtime_odometry_transform_publisher_->trylock())
248250
{
249251
auto & transform = realtime_odometry_transform_publisher_->msg_.transforms.front();
250-
transform.header.stamp = current_time;
252+
transform.header.stamp = time;
251253
transform.transform.translation.x = odometry_.getX();
252254
transform.transform.translation.y = odometry_.getY();
253255
transform.transform.rotation.x = orientation.x();
@@ -258,15 +260,12 @@ controller_interface::return_type DiffDriveController::update(
258260
}
259261
}
260262

261-
const auto update_dt = current_time - previous_update_timestamp_;
262-
previous_update_timestamp_ = current_time;
263-
264263
auto & last_command = previous_commands_.back().twist;
265264
auto & second_to_last_command = previous_commands_.front().twist;
266265
limiter_linear_.limit(
267-
linear_command, last_command.linear.x, second_to_last_command.linear.x, update_dt.seconds());
266+
linear_command, last_command.linear.x, second_to_last_command.linear.x, period.seconds());
268267
limiter_angular_.limit(
269-
angular_command, last_command.angular.z, second_to_last_command.angular.z, update_dt.seconds());
268+
angular_command, last_command.angular.z, second_to_last_command.angular.z, period.seconds());
270269

271270
previous_commands_.pop();
272271
previous_commands_.emplace(command);
@@ -275,7 +274,7 @@ controller_interface::return_type DiffDriveController::update(
275274
if (publish_limited_velocity_ && realtime_limited_velocity_publisher_->trylock())
276275
{
277276
auto & limited_velocity_command = realtime_limited_velocity_publisher_->msg_;
278-
limited_velocity_command.header.stamp = current_time;
277+
limited_velocity_command.header.stamp = time;
279278
limited_velocity_command.twist = command.twist;
280279
realtime_limited_velocity_publisher_->unlockAndPublish();
281280
}

0 commit comments

Comments
 (0)