Skip to content

Commit 025f2cd

Browse files
refactor(ekf_localizer): remove current_ekf_pose and so on (#5422)
* refactor(ekf_localizer): remote current_ekf_pose and so on Signed-off-by: kminoda <koji.minoda@tier4.jp> * still build failes due to one current_ekf_twist_ Signed-off-by: kminoda <koji.minoda@tier4.jp> * now works Signed-off-by: kminoda <koji.minoda@tier4.jp> * style(pre-commit): autofix --------- Signed-off-by: kminoda <koji.minoda@tier4.jp> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent bfabbca commit 025f2cd

File tree

4 files changed

+71
-48
lines changed

4 files changed

+71
-48
lines changed

common/kalman_filter/include/kalman_filter/kalman_filter.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,20 @@ class KalmanFilter
109109
* @brief get current kalman filter state
110110
* @param x kalman filter state
111111
*/
112-
void getX(Eigen::MatrixXd & x);
112+
void getX(Eigen::MatrixXd & x) const;
113113

114114
/**
115115
* @brief get current kalman filter covariance
116116
* @param P kalman filter covariance
117117
*/
118-
void getP(Eigen::MatrixXd & P);
118+
void getP(Eigen::MatrixXd & P) const;
119119

120120
/**
121121
* @brief get component of current kalman filter state
122122
* @param i index of kalman filter state
123123
* @return value of i's component of the kalman filter state x[i]
124124
*/
125-
double getXelement(unsigned int i);
125+
double getXelement(unsigned int i) const;
126126

127127
/**
128128
* @brief calculate kalman filter state and covariance by prediction model with A, B, Q matrix.

common/kalman_filter/src/kalman_filter.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ void KalmanFilter::setR(const Eigen::MatrixXd & R)
7777
{
7878
R_ = R;
7979
}
80-
void KalmanFilter::getX(Eigen::MatrixXd & x)
80+
void KalmanFilter::getX(Eigen::MatrixXd & x) const
8181
{
8282
x = x_;
8383
}
84-
void KalmanFilter::getP(Eigen::MatrixXd & P)
84+
void KalmanFilter::getP(Eigen::MatrixXd & P) const
8585
{
8686
P = P_;
8787
}
88-
double KalmanFilter::getXelement(unsigned int i)
88+
double KalmanFilter::getXelement(unsigned int i) const
8989
{
9090
return x_(i);
9191
}

localization/ekf_localizer/include/ekf_localizer/ekf_localizer.hpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Simple1DFilter
8989
return;
9090
};
9191
void set_proc_dev(const double proc_dev) { proc_dev_x_c_ = proc_dev; }
92-
double get_x() { return x_; }
92+
double get_x() const { return x_; }
9393

9494
private:
9595
bool initialized_;
@@ -186,13 +186,6 @@ class EKFLocalizer : public rclcpp::Node
186186
AgedObjectQueue<geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr> pose_queue_;
187187
AgedObjectQueue<geometry_msgs::msg::TwistWithCovarianceStamped::SharedPtr> twist_queue_;
188188

189-
geometry_msgs::msg::PoseStamped current_ekf_pose_; //!< @brief current estimated pose
190-
geometry_msgs::msg::PoseStamped
191-
current_biased_ekf_pose_; //!< @brief current estimated pose without yaw bias correction
192-
geometry_msgs::msg::TwistStamped current_ekf_twist_; //!< @brief current estimated twist
193-
std::array<double, 36ul> current_pose_covariance_;
194-
std::array<double, 36ul> current_twist_covariance_;
195-
196189
/**
197190
* @brief computes update & prediction of EKF for each ekf_dt_[s] time
198191
*/
@@ -257,10 +250,23 @@ class EKFLocalizer : public rclcpp::Node
257250
*/
258251
void setCurrentResult();
259252

253+
/**
254+
* @brief get current ekf pose
255+
*/
256+
geometry_msgs::msg::PoseStamped getCurrentEKFPose(bool get_biased_yaw) const;
257+
258+
/**
259+
* @brief get current ekf twist
260+
*/
261+
geometry_msgs::msg::TwistStamped getCurrentEKFTwist() const;
262+
260263
/**
261264
* @brief publish current EKF estimation result
262265
*/
263-
void publishEstimateResult();
266+
void publishEstimateResult(
267+
const geometry_msgs::msg::PoseStamped & current_ekf_pose,
268+
const geometry_msgs::msg::PoseStamped & current_biased_ekf_pose,
269+
const geometry_msgs::msg::TwistStamped & current_ekf_twist);
264270

265271
/**
266272
* @brief publish diagnostics message

localization/ekf_localizer/src/ekf_localizer.cpp

+50-33
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ void EKFLocalizer::timerCallback()
232232
}
233233
twist_no_update_count_ = twist_is_updated ? 0 : (twist_no_update_count_ + 1);
234234

235+
const geometry_msgs::msg::PoseStamped current_ekf_pose = getCurrentEKFPose(false);
236+
const geometry_msgs::msg::PoseStamped current_biased_ekf_pose = getCurrentEKFPose(true);
237+
const geometry_msgs::msg::TwistStamped current_ekf_twist = getCurrentEKFTwist();
238+
239+
/* publish ekf result */
240+
publishEstimateResult(current_ekf_pose, current_biased_ekf_pose, current_ekf_twist);
241+
publishDiagnostics();
242+
}
243+
244+
geometry_msgs::msg::PoseStamped EKFLocalizer::getCurrentEKFPose(bool get_biased_yaw) const
245+
{
235246
const double x = ekf_.getXelement(IDX::X);
236247
const double y = ekf_.getXelement(IDX::Y);
237248
const double z = z_filter_.get_x();
@@ -242,27 +253,32 @@ void EKFLocalizer::timerCallback()
242253
const double roll = roll_filter_.get_x();
243254
const double pitch = pitch_filter_.get_x();
244255
const double yaw = biased_yaw + yaw_bias;
245-
const double vx = ekf_.getXelement(IDX::VX);
246-
const double wz = ekf_.getXelement(IDX::WZ);
247-
248-
current_ekf_pose_.header.frame_id = params_.pose_frame_id;
249-
current_ekf_pose_.header.stamp = this->now();
250-
current_ekf_pose_.pose.position = tier4_autoware_utils::createPoint(x, y, z);
251-
current_ekf_pose_.pose.orientation =
252-
tier4_autoware_utils::createQuaternionFromRPY(roll, pitch, yaw);
253256

254-
current_biased_ekf_pose_ = current_ekf_pose_;
255-
current_biased_ekf_pose_.pose.orientation =
256-
tier4_autoware_utils::createQuaternionFromRPY(roll, pitch, biased_yaw);
257+
geometry_msgs::msg::PoseStamped current_ekf_pose;
258+
current_ekf_pose.header.frame_id = params_.pose_frame_id;
259+
current_ekf_pose.header.stamp = this->now();
260+
current_ekf_pose.pose.position = tier4_autoware_utils::createPoint(x, y, z);
261+
if (get_biased_yaw) {
262+
current_ekf_pose.pose.orientation =
263+
tier4_autoware_utils::createQuaternionFromRPY(roll, pitch, biased_yaw);
264+
} else {
265+
current_ekf_pose.pose.orientation =
266+
tier4_autoware_utils::createQuaternionFromRPY(roll, pitch, yaw);
267+
}
268+
return current_ekf_pose;
269+
}
257270

258-
current_ekf_twist_.header.frame_id = "base_link";
259-
current_ekf_twist_.header.stamp = this->now();
260-
current_ekf_twist_.twist.linear.x = vx;
261-
current_ekf_twist_.twist.angular.z = wz;
271+
geometry_msgs::msg::TwistStamped EKFLocalizer::getCurrentEKFTwist() const
272+
{
273+
const double vx = ekf_.getXelement(IDX::VX);
274+
const double wz = ekf_.getXelement(IDX::WZ);
262275

263-
/* publish ekf result */
264-
publishEstimateResult();
265-
publishDiagnostics();
276+
geometry_msgs::msg::TwistStamped current_ekf_twist;
277+
current_ekf_twist.header.frame_id = "base_link";
278+
current_ekf_twist.header.stamp = this->now();
279+
current_ekf_twist.twist.linear.x = vx;
280+
current_ekf_twist.twist.angular.z = wz;
281+
return current_ekf_twist;
266282
}
267283

268284
void EKFLocalizer::showCurrentX()
@@ -282,12 +298,12 @@ void EKFLocalizer::timerTFCallback()
282298
return;
283299
}
284300

285-
if (current_ekf_pose_.header.frame_id == "") {
301+
if (params_.pose_frame_id == "") {
286302
return;
287303
}
288304

289305
geometry_msgs::msg::TransformStamped transform_stamped;
290-
transform_stamped = tier4_autoware_utils::pose2transform(current_ekf_pose_, "base_link");
306+
transform_stamped = tier4_autoware_utils::pose2transform(getCurrentEKFPose(false), "base_link");
291307
transform_stamped.header.stamp = this->now();
292308
tf_br_->sendTransform(transform_stamped);
293309
}
@@ -338,8 +354,6 @@ void EKFLocalizer::callbackInitialPose(
338354

339355
X(IDX::X) = initialpose->pose.pose.position.x + transform.transform.translation.x;
340356
X(IDX::Y) = initialpose->pose.pose.position.y + transform.transform.translation.y;
341-
current_ekf_pose_.pose.position.z =
342-
initialpose->pose.pose.position.z + transform.transform.translation.z;
343357
X(IDX::YAW) =
344358
tf2::getYaw(initialpose->pose.pose.orientation) + tf2::getYaw(transform.transform.rotation);
345359
X(IDX::YAWB) = 0.0;
@@ -488,7 +502,7 @@ bool EKFLocalizer::measurementUpdatePose(const geometry_msgs::msg::PoseWithCovar
488502

489503
// Considering change of z value due to measurement pose delay
490504
const auto rpy = tier4_autoware_utils::getRPY(pose.pose.pose.orientation);
491-
const double dz_delay = current_ekf_twist_.twist.linear.x * delay_time * std::sin(-rpy.y);
505+
const double dz_delay = ekf_.getXelement(IDX::VX) * delay_time * std::sin(-rpy.y);
492506
geometry_msgs::msg::PoseWithCovarianceStamped pose_with_z_delay;
493507
pose_with_z_delay = pose;
494508
pose_with_z_delay.pose.pose.position.z += dz_delay;
@@ -586,36 +600,39 @@ bool EKFLocalizer::measurementUpdateTwist(
586600
/*
587601
* publishEstimateResult
588602
*/
589-
void EKFLocalizer::publishEstimateResult()
603+
void EKFLocalizer::publishEstimateResult(
604+
const geometry_msgs::msg::PoseStamped & current_ekf_pose,
605+
const geometry_msgs::msg::PoseStamped & current_biased_ekf_pose,
606+
const geometry_msgs::msg::TwistStamped & current_ekf_twist)
590607
{
591608
rclcpp::Time current_time = this->now();
592609
const Eigen::MatrixXd X = ekf_.getLatestX();
593610
const Eigen::MatrixXd P = ekf_.getLatestP();
594611

595612
/* publish latest pose */
596-
pub_pose_->publish(current_ekf_pose_);
597-
pub_biased_pose_->publish(current_biased_ekf_pose_);
613+
pub_pose_->publish(current_ekf_pose);
614+
pub_biased_pose_->publish(current_biased_ekf_pose);
598615

599616
/* publish latest pose with covariance */
600617
geometry_msgs::msg::PoseWithCovarianceStamped pose_cov;
601618
pose_cov.header.stamp = current_time;
602-
pose_cov.header.frame_id = current_ekf_pose_.header.frame_id;
603-
pose_cov.pose.pose = current_ekf_pose_.pose;
619+
pose_cov.header.frame_id = current_ekf_pose.header.frame_id;
620+
pose_cov.pose.pose = current_ekf_pose.pose;
604621
pose_cov.pose.covariance = ekfCovarianceToPoseMessageCovariance(P);
605622
pub_pose_cov_->publish(pose_cov);
606623

607624
geometry_msgs::msg::PoseWithCovarianceStamped biased_pose_cov = pose_cov;
608-
biased_pose_cov.pose.pose = current_biased_ekf_pose_.pose;
625+
biased_pose_cov.pose.pose = current_biased_ekf_pose.pose;
609626
pub_biased_pose_cov_->publish(biased_pose_cov);
610627

611628
/* publish latest twist */
612-
pub_twist_->publish(current_ekf_twist_);
629+
pub_twist_->publish(current_ekf_twist);
613630

614631
/* publish latest twist with covariance */
615632
geometry_msgs::msg::TwistWithCovarianceStamped twist_cov;
616633
twist_cov.header.stamp = current_time;
617-
twist_cov.header.frame_id = current_ekf_twist_.header.frame_id;
618-
twist_cov.twist.twist = current_ekf_twist_.twist;
634+
twist_cov.header.frame_id = current_ekf_twist.header.frame_id;
635+
twist_cov.twist.twist = current_ekf_twist.twist;
619636
twist_cov.twist.covariance = ekfCovarianceToTwistMessageCovariance(P);
620637
pub_twist_cov_->publish(twist_cov);
621638

@@ -628,7 +645,7 @@ void EKFLocalizer::publishEstimateResult()
628645
/* publish latest odometry */
629646
nav_msgs::msg::Odometry odometry;
630647
odometry.header.stamp = current_time;
631-
odometry.header.frame_id = current_ekf_pose_.header.frame_id;
648+
odometry.header.frame_id = current_ekf_pose.header.frame_id;
632649
odometry.child_frame_id = "base_link";
633650
odometry.pose = pose_cov.pose;
634651
odometry.twist = twist_cov.twist;

0 commit comments

Comments
 (0)