Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/robot_localization_estimator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void RobotLocalizationEstimator::extrapolate(
rclcpp::Time time_stamp = rclcpp::Time(
boundary_state.time_stamp *
1000000000);
rclcpp::Duration delta_duration = rclcpp::Duration(delta * 1000000000);
rclcpp::Duration delta_duration = rclcpp::Duration::from_nanoseconds(delta * 1000000000);
filter_->predict(time_stamp, delta_duration);

state_at_req_time.time_stamp = requested_time;
Expand Down
44 changes: 22 additions & 22 deletions src/ros_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ RosFilter<T>::RosFilter(const rclcpp::NodeOptions & options)
static_diag_error_level_(diagnostic_msgs::msg::DiagnosticStatus::OK),
frequency_(30.0),
gravitational_acceleration_(9.80665),
history_length_(0),
history_length_(0ns),
latest_control_(),
last_set_pose_time_(0, 0, RCL_ROS_TIME),
latest_control_time_(0, 0, RCL_ROS_TIME),
tf_timeout_(0),
tf_time_offset_(0)
tf_timeout_(0ns),
tf_time_offset_(0ns)
{
tf_buffer_ = std::make_unique<tf2_ros::Buffer>(this->get_clock());
tf_listener_ = std::make_unique<tf2_ros::TransformListener>(*tf_buffer_);
Expand Down Expand Up @@ -604,7 +604,7 @@ void RosFilter<T>::integrateMeasurements(const rclcpp::Time & current_time)
const std::string first_measurement_topic =
first_measurement->topic_name_;
// revertTo may invalidate first_measurement
if (!revertTo(first_measurement_time - rclcpp::Duration(1))) {
if (!revertTo(first_measurement_time - rclcpp::Duration(1ns))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ayrton04 should this be 1ns, 1s, ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I meant to ask the same question.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the equivalent ROS 1 code, it is also subtracting 1 nanosecond:

if (!revertTo(firstMeasurementTime - 1e-9)) // revertTo may invalidate firstMeasurement

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the PRs involved here are

https://github.com/cra-ros-pkg/robot_localization/pull/266/files

and

https://github.com/cra-ros-pkg/robot_localization/pull/279/files

The only reason we subtract any time at all is to ensure that we go to a measurement (presumably the first) that is before first_measurement_time. But we could also just change this to use >=:

while (!filter_state_history_.empty() &&
filter_state_history_.back()->last_measurement_time_ > time)

If I had to guess (I didn't dig too far), the measurement times were likely stored as doubles originally, and the 1e-9 thing was just me being careful with floating point numbers. But ROS times have exact representations, so I think the 1e-9 could go, and we can change the revertTo reverse iteration to check for >= instead of just >.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, that change could be outside of scope for this. It was meant to be 1 nanosecond, yes.

RF_DEBUG(
"ERROR: history interval is too small to revert to time " <<
filter_utilities::toSec(first_measurement_time) << "\n");
Expand Down Expand Up @@ -815,7 +815,7 @@ void RosFilter<T>::loadParams()
// Try to resolve tf_prefix
std::string tf_prefix = "";
std::string tf_prefix_path = "";
this->declare_parameter("tf_prefix");
this->declare_parameter<std::string>("tf_prefix");
if (this->get_parameter("tf_prefix", tf_prefix_path)) {
// Append the tf prefix in a tf2-friendly manner
filter_utilities::appendPrefix(tf_prefix, map_frame_id_);
Expand All @@ -834,18 +834,18 @@ void RosFilter<T>::loadParams()
// Transform future dating
double offset_tmp = this->declare_parameter("transform_time_offset", 0.0);
tf_time_offset_ =
rclcpp::Duration(filter_utilities::secToNanosec(offset_tmp));
rclcpp::Duration::from_nanoseconds(filter_utilities::secToNanosec(offset_tmp));

// Transform timeout
double timeout_tmp = this->declare_parameter("transform_timeout", 0.0);
tf_timeout_ = rclcpp::Duration(filter_utilities::secToNanosec(timeout_tmp));
tf_timeout_ = rclcpp::Duration::from_nanoseconds(filter_utilities::secToNanosec(timeout_tmp));

// Update frequency and sensor timeout
frequency_ = this->declare_parameter("frequency", 30.0);

double sensor_timeout = this->declare_parameter("sensor_timeout", 1.0 / frequency_);
filter_.setSensorTimeout(
rclcpp::Duration(filter_utilities::secToNanosec(sensor_timeout)));
rclcpp::Duration::from_nanoseconds(filter_utilities::secToNanosec(sensor_timeout)));

// Determine if we're in 2D mode
two_d_mode_ = this->declare_parameter("two_d_mode", false);
Expand All @@ -865,7 +865,7 @@ void RosFilter<T>::loadParams()
" specified. Absolute value will be assumed.";
}

history_length_ = rclcpp::Duration(
history_length_ = rclcpp::Duration::from_nanoseconds(
filter_utilities::secToNanosec(std::abs(history_length_double)));

// Whether we reset filter on jump back in time
Expand All @@ -883,7 +883,7 @@ void RosFilter<T>::loadParams()
control_timeout = this->declare_parameter("control_timeout", 0.0);

if (use_control_) {
this->declare_parameter("control_config");
this->declare_parameter<std::vector<bool>>("control_config");
if (this->get_parameter("control_config", control_update_vector)) {
if (control_update_vector.size() != TWIST_SIZE) {
std::cerr << "Control configuration must be of size " << TWIST_SIZE <<
Expand All @@ -899,7 +899,7 @@ void RosFilter<T>::loadParams()
use_control_ = false;
}

this->declare_parameter("acceleration_limits");
this->declare_parameter<std::vector<double>>("acceleration_limits");
if (this->get_parameter("acceleration_limits", acceleration_limits)) {
if (acceleration_limits.size() != TWIST_SIZE) {
std::cerr << "Acceleration configuration must be of size " << TWIST_SIZE <<
Expand All @@ -915,7 +915,7 @@ void RosFilter<T>::loadParams()
acceleration_limits.resize(TWIST_SIZE, 1.0);
}

this->declare_parameter("acceleration_gains");
this->declare_parameter<std::vector<double>>("acceleration_gains");
if (this->get_parameter("acceleration_gains", acceleration_gains)) {
const int size = acceleration_gains.size();
if (size != TWIST_SIZE) {
Expand All @@ -929,7 +929,7 @@ void RosFilter<T>::loadParams()
}
}

this->declare_parameter("deceleration_limits");
this->declare_parameter<std::vector<double>>("deceleration_limits");
if (this->get_parameter("deceleration_limits", deceleration_limits)) {
if (deceleration_limits.size() != TWIST_SIZE) {
std::cerr << "Deceleration configuration must be of size " << TWIST_SIZE <<
Expand All @@ -945,7 +945,7 @@ void RosFilter<T>::loadParams()
deceleration_limits = acceleration_limits;
}

this->declare_parameter("deceleration_gains");
this->declare_parameter<std::vector<double>>("deceleration_gains");
if (this->get_parameter("deceleration_gains", deceleration_gains)) {
const int size = deceleration_gains.size();
if (size != TWIST_SIZE) {
Expand Down Expand Up @@ -977,7 +977,7 @@ void RosFilter<T>::loadParams()
dynamic_process_noise_covariance);

std::vector<double> initial_state;
this->declare_parameter("initial_state");
this->declare_parameter<std::vector<double>>("initial_state");
if (this->get_parameter("initial_state", initial_state)) {
if (initial_state.size() != STATE_SIZE) {
std::cerr << "Initial state must be of size " << STATE_SIZE <<
Expand Down Expand Up @@ -1066,7 +1066,7 @@ void RosFilter<T>::loadParams()
ss << "odom" << topic_ind++;
std::string odom_topic_name = ss.str();
std::string odom_topic;
this->declare_parameter(odom_topic_name);
this->declare_parameter<std::string>(odom_topic_name);

rclcpp::Parameter parameter;
if (rclcpp::PARAMETER_NOT_SET != this->get_parameter(odom_topic_name, parameter)) {
Expand Down Expand Up @@ -1217,7 +1217,7 @@ void RosFilter<T>::loadParams()
ss << "pose" << topic_ind++;
std::string pose_topic_name = ss.str();
std::string pose_topic;
this->declare_parameter(pose_topic_name);
this->declare_parameter<std::string>(pose_topic_name);

rclcpp::Parameter parameter;
if (rclcpp::PARAMETER_NOT_SET != this->get_parameter(pose_topic_name, parameter)) {
Expand Down Expand Up @@ -1335,7 +1335,7 @@ void RosFilter<T>::loadParams()
ss << "twist" << topic_ind++;
std::string twist_topic_name = ss.str();
std::string twist_topic;
this->declare_parameter(twist_topic_name);
this->declare_parameter<std::string>(twist_topic_name);

rclcpp::Parameter parameter;
if (rclcpp::PARAMETER_NOT_SET != this->get_parameter(twist_topic_name, parameter)) {
Expand Down Expand Up @@ -1417,7 +1417,7 @@ void RosFilter<T>::loadParams()
ss << "imu" << topic_ind++;
std::string imu_topic_name = ss.str();
std::string imu_topic;
this->declare_parameter(imu_topic_name);
this->declare_parameter<std::string>(imu_topic_name);

rclcpp::Parameter parameter;
if (rclcpp::PARAMETER_NOT_SET != this->get_parameter(imu_topic_name, parameter)) {
Expand Down Expand Up @@ -1663,7 +1663,7 @@ void RosFilter<T>::loadParams()

filter_.setControlParams(
control_update_vector,
rclcpp::Duration(filter_utilities::secToNanosec(control_timeout)),
rclcpp::Duration::from_nanoseconds(filter_utilities::secToNanosec(control_timeout)),
acceleration_limits, acceleration_gains, deceleration_limits,
deceleration_gains);

Expand Down Expand Up @@ -1727,7 +1727,7 @@ void RosFilter<T>::loadParams()
process_noise_covariance.setZero();
std::vector<double> process_noise_covar_flat;

this->declare_parameter("process_noise_covariance");
this->declare_parameter<std::vector<double>>("process_noise_covariance");
if (this->get_parameter(
"process_noise_covariance",
process_noise_covar_flat))
Expand All @@ -1754,7 +1754,7 @@ void RosFilter<T>::loadParams()
initial_estimate_error_covariance.setZero();
std::vector<double> estimate_error_covar_flat;

this->declare_parameter("initial_estimate_covariance");
this->declare_parameter<std::vector<double>>("initial_estimate_covariance");
if (this->get_parameter(
"initial_estimate_covariance",
estimate_error_covar_flat))
Expand Down