ros_filter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 2015, 2016, Charles River Analytics, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution.
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef ROBOT_LOCALIZATION_ROS_FILTER_H
34 #define ROBOT_LOCALIZATION_ROS_FILTER_H
35 
39 
40 #include <robot_localization/SetPose.h>
41 #include <robot_localization/ToggleFilterProcessing.h>
42 
43 #include <ros/ros.h>
44 #include <std_msgs/String.h>
45 #include <std_srvs/Empty.h>
46 #include <nav_msgs/Odometry.h>
47 #include <sensor_msgs/Imu.h>
48 #include <geometry_msgs/Twist.h>
49 #include <geometry_msgs/TwistStamped.h>
50 #include <geometry_msgs/TwistWithCovarianceStamped.h>
51 #include <geometry_msgs/PoseWithCovarianceStamped.h>
52 #include <geometry_msgs/AccelWithCovarianceStamped.h>
55 #include <tf2_ros/message_filter.h>
60 #include <diagnostic_msgs/DiagnosticStatus.h>
61 
62 #include <XmlRpcException.h>
63 
64 #include <Eigen/Dense>
65 
66 #include <fstream>
67 #include <map>
68 #include <numeric>
69 #include <queue>
70 #include <string>
71 #include <vector>
72 #include <deque>
73 
74 namespace RobotLocalization
75 {
76 
78 {
79  CallbackData(const std::string &topicName,
80  const std::vector<int> &updateVector,
81  const int updateSum,
82  const bool differential,
83  const bool relative,
84  const double rejectionThreshold) :
85  topicName_(topicName),
86  updateVector_(updateVector),
87  updateSum_(updateSum),
88  differential_(differential),
89  relative_(relative),
90  rejectionThreshold_(rejectionThreshold)
91  {
92  }
93 
94  std::string topicName_;
95  std::vector<int> updateVector_;
98  bool relative_;
100 };
101 
102 typedef std::priority_queue<MeasurementPtr, std::vector<MeasurementPtr>, Measurement> MeasurementQueue;
103 typedef std::deque<MeasurementPtr> MeasurementHistoryDeque;
104 typedef std::deque<FilterStatePtr> FilterStateHistoryDeque;
105 
106 template<class T> class RosFilter
107 {
108  public:
114  explicit RosFilter(std::vector<double> args = std::vector<double>());
115 
120  ~RosFilter();
121 
124  void reset();
125 
130  bool toggleFilterProcessingCallback(robot_localization::ToggleFilterProcessing::Request&,
131  robot_localization::ToggleFilterProcessing::Response&);
132 
138  void accelerationCallback(const sensor_msgs::Imu::ConstPtr &msg,
139  const CallbackData &callbackData,
140  const std::string &targetFrame);
141 
145  void controlCallback(const geometry_msgs::Twist::ConstPtr &msg);
146 
150  void controlCallback(const geometry_msgs::TwistStamped::ConstPtr &msg);
151 
161  void enqueueMeasurement(const std::string &topicName,
162  const Eigen::VectorXd &measurement,
163  const Eigen::MatrixXd &measurementCovariance,
164  const std::vector<int> &updateVector,
165  const double mahalanobisThresh,
166  const ros::Time &time);
167 
177  void forceTwoD(Eigen::VectorXd &measurement,
178  Eigen::MatrixXd &measurementCovariance,
179  std::vector<int> &updateVector);
180 
185  bool getFilteredOdometryMessage(nav_msgs::Odometry &message);
186 
191  bool getFilteredAccelMessage(geometry_msgs::AccelWithCovarianceStamped &message);
192 
203  void imuCallback(const sensor_msgs::Imu::ConstPtr &msg, const std::string &topicName,
204  const CallbackData &poseCallbackData, const CallbackData &twistCallbackData,
205  const CallbackData &accelCallbackData);
206 
211  void integrateMeasurements(const ros::Time &currentTime);
212 
215  void loadParams();
216 
226  void odometryCallback(const nav_msgs::Odometry::ConstPtr &msg, const std::string &topicName,
227  const CallbackData &poseCallbackData, const CallbackData &twistCallbackData);
228 
235  void poseCallback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &msg,
236  const CallbackData &callbackData,
237  const std::string &targetFrame,
238  const bool imuData);
239 
242  void run();
243 
247  void setPoseCallback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &msg);
248 
253  bool setPoseSrvCallback(robot_localization::SetPose::Request& request,
254  robot_localization::SetPose::Response&);
255 
260  bool enableFilterSrvCallback(std_srvs::Empty::Request&,
261  std_srvs::Empty::Response&);
262 
268  void twistCallback(const geometry_msgs::TwistWithCovarianceStamped::ConstPtr &msg,
269  const CallbackData &callbackData,
270  const std::string &targetFrame);
271 
276  bool validateFilterOutput(const nav_msgs::Odometry &message);
277 
278  protected:
287  bool revertTo(const double time);
288 
294  void saveFilterState(FilterBase &filter);
295 
299  void clearExpiredHistory(const double cutoffTime);
300 
303  void clearMeasurementQueue();
304 
311  void addDiagnostic(const int errLevel,
312  const std::string &topicAndClass,
313  const std::string &message,
314  const bool staticDiag);
315 
319  void aggregateDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &wrapper);
320 
333  void copyCovariance(const double *arr,
334  Eigen::MatrixXd &covariance,
335  const std::string &topicName,
336  const std::vector<int> &updateVector,
337  const size_t offset,
338  const size_t dimension);
339 
347  void copyCovariance(const Eigen::MatrixXd &covariance,
348  double *arr,
349  const size_t dimension);
350 
355  std::vector<int> loadUpdateConfig(const std::string &topicName);
356 
365  bool prepareAcceleration(const sensor_msgs::Imu::ConstPtr &msg,
366  const std::string &topicName,
367  const std::string &targetFrame,
368  std::vector<int> &updateVector,
369  Eigen::VectorXd &measurement,
370  Eigen::MatrixXd &measurementCovariance);
371 
384  bool preparePose(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &msg,
385  const std::string &topicName,
386  const std::string &targetFrame,
387  const bool differential,
388  const bool relative,
389  const bool imuData,
390  std::vector<int> &updateVector,
391  Eigen::VectorXd &measurement,
392  Eigen::MatrixXd &measurementCovariance);
393 
403  bool prepareTwist(const geometry_msgs::TwistWithCovarianceStamped::ConstPtr &msg,
404  const std::string &topicName,
405  const std::string &targetFrame,
406  std::vector<int> &updateVector,
407  Eigen::VectorXd &measurement,
408  Eigen::MatrixXd &measurementCovariance);
409 
414  std::string baseLinkFrameId_;
415 
422 
426 
432  std::map<std::string, std::string> staticDiagnostics_;
433 
439  std::map<std::string, std::string> dynamicDiagnostics_;
440 
443  std::ofstream debugStream_;
444 
448 
452 
456 
459  double frequency_;
460 
466 
473 
476  Eigen::VectorXd latestControl_;
477 
481 
485 
488  std::vector<ros::Subscriber> topicSubs_;
489 
500  std::map<std::string, tf2::Transform> initialMeasurements_;
501 
510  std::map<std::string, ros::Time> lastMessageTimes_;
511 
520 
523  std::string mapFrameId_;
524 
529  MeasurementQueue measurementQueue_;
530 
534 
538 
541  std::string odomFrameId_;
542 
551  std::map<std::string, tf2::Transform> previousMeasurements_;
552 
555  std::map<std::string, Eigen::MatrixXd> previousMeasurementCovariances_;
556 
561 
565 
568  std::map<std::string, bool> removeGravitationalAcc_;
569 
573 
578 
583 
587 
591 
596 
599  std::vector<std::string> stateVariableNames_;
600 
604 
608 
612 
616 
623  bool twoDMode_;
624 
628 
635 
637  bool enabled_;
638 
641 
648  geometry_msgs::TransformStamped worldBaseLinkTransMsg_;
649 
652  std::string worldFrameId_;
653 
657 
661 
663  //
664  // front() refers to the filter state with the earliest timestamp.
665  // back() refers to the filter state with the latest timestamp.
666  FilterStateHistoryDeque filterStateHistory_;
667 
669  // when popped from the measurement priority queue.
670  // front() refers to the measurement with the earliest timestamp.
671  // back() refers to the measurement with the latest timestamp.
672  MeasurementHistoryDeque measurementHistory_;
673 };
674 
675 } // namespace RobotLocalization
676 
677 #endif // ROBOT_LOCALIZATION_ROS_FILTER_H
msg
std::priority_queue< MeasurementPtr, std::vector< MeasurementPtr >, Measurement > MeasurementQueue
Definition: ros_filter.h:102
CallbackData(const std::string &topicName, const std::vector< int > &updateVector, const int updateSum, const bool differential, const bool relative, const double rejectionThreshold)
Definition: ros_filter.h:79
bool useControl_
Whether or not we use a control term.
Definition: ros_filter.h:627
bool publishAcceleration_
Whether we publish the acceleration.
Definition: ros_filter.h:660
std::vector< int > updateVector_
Definition: ros_filter.h:95
ros::Subscriber controlSub_
Subscribes to the control input topic.
Definition: ros_filter.h:425
ros::NodeHandle nhLocal_
Local node handle (for params)
Definition: ros_filter.h:537
std::map< std::string, Eigen::MatrixXd > previousMeasurementCovariances_
We also need the previous covariance matrix for differential data.
Definition: ros_filter.h:555
bool resetOnTimeJump_
Whether to reset the filters when backwards jump in time is detected.
Definition: ros_filter.h:472
MeasurementHistoryDeque measurementHistory_
A deque of previous measurements which is implicitly ordered from earliest to latest measurement...
Definition: ros_filter.h:672
std::string baseLinkFrameId_
tf frame name for the robot&#39;s body frame
Definition: ros_filter.h:414
std::map< std::string, ros::Time > lastMessageTimes_
Store the last time a message from each topic was received.
Definition: ros_filter.h:510
ros::Subscriber setPoseSub_
Subscribes to the set_pose topic (usually published from rviz). Message type is geometry_msgs/PoseWit...
Definition: ros_filter.h:577
bool enabled_
Whether the filter is enabled or not. See disabledAtStartup_.
Definition: ros_filter.h:637
ros::ServiceServer toggleFilterProcessingSrv_
Service that allows another node to toggle on/off filter processing while still publishing. Uses a robot_localization ToggleFilterProcessing service.
Definition: ros_filter.h:595
Eigen::VectorXd latestControl_
The most recent control input.
Definition: ros_filter.h:476
ros::Duration tfTimeout_
Parameter that specifies the how long we wait for a transform to become available.
Definition: ros_filter.h:484
Structure used for storing and comparing measurements (for priority queues)
Definition: filter_base.h:61
ros::Time latestControlTime_
The time of the most recent control input.
Definition: ros_filter.h:480
std::vector< ros::Subscriber > topicSubs_
Vector to hold our subscribers until they go out of scope.
Definition: ros_filter.h:488
bool printDiagnostics_
Whether or not we print diagnostic messages to the /diagnostics topic.
Definition: ros_filter.h:564
std::string worldFrameId_
tf frame name that is the parent frame of the transform that this node will calculate and broadcast...
Definition: ros_filter.h:652
int staticDiagErrorLevel_
The max (worst) static diagnostic level.
Definition: ros_filter.h:603
ros::ServiceServer setPoseSrv_
Service that allows another node to change the current state and recieve a confirmation. Uses a custom SetPose service.
Definition: ros_filter.h:582
int dynamicDiagErrorLevel_
The max (worst) dynamic diagnostic level.
Definition: ros_filter.h:447
ros::Time lastSetPoseTime_
Store the last time set pose message was received.
Definition: ros_filter.h:519
bool publishTransform_
Whether we publish the transform from the world_frame to the base_link_frame.
Definition: ros_filter.h:656
std::deque< FilterStatePtr > FilterStateHistoryDeque
Definition: ros_filter.h:104
tf2_ros::TransformListener tfListener_
Transform listener for receiving transforms.
Definition: ros_filter.h:611
std::map< std::string, tf2::Transform > previousMeasurements_
Stores the last measurement from a given topic for differential integration.
Definition: ros_filter.h:551
std::string baseLinkOutputFrameId_
tf frame name for the robot&#39;s body frame
Definition: ros_filter.h:421
MeasurementQueue measurementQueue_
We process measurements by queueing them up in callbacks and processing them all at once within each ...
Definition: ros_filter.h:529
bool disabledAtStartup_
Start the Filter disabled at startup.
Definition: ros_filter.h:634
std::deque< MeasurementPtr > MeasurementHistoryDeque
Definition: ros_filter.h:103
ros::Duration tfTimeOffset_
For future (or past) dating the world_frame->base_link_frame transform.
Definition: ros_filter.h:615
ros::ServiceServer enableFilterSrv_
Service that allows another node to enable the filter. Uses a standard Empty service.
Definition: ros_filter.h:590
std::map< std::string, std::string > staticDiagnostics_
This object accumulates static diagnostics, e.g., diagnostics relating to the configuration parameter...
Definition: ros_filter.h:432
bool smoothLaggedData_
Whether or not we use smoothing.
Definition: ros_filter.h:586
bool toggledOn_
$brief Whether the filter should process new measurements or not.
Definition: ros_filter.h:640
geometry_msgs::TransformStamped worldBaseLinkTransMsg_
Message that contains our latest transform (i.e., state)
Definition: ros_filter.h:648
double gravitationalAcc_
What is the acceleration in Z due to gravity (m/s^2)? Default is +9.80665.
Definition: ros_filter.h:572
std::string mapFrameId_
tf frame name for the robot&#39;s map (world-fixed) frame
Definition: ros_filter.h:523
std::map< std::string, tf2::Transform > initialMeasurements_
Stores the first measurement from each topic for relative measurements.
Definition: ros_filter.h:500
double historyLength_
The depth of the history we track for smoothing/delayed measurement processing.
Definition: ros_filter.h:465
bool predictToCurrentTime_
By default, the filter predicts and corrects up to the time of the latest measurement. If this is set to true, the filter does the same, but then also predicts up to the current time step.
Definition: ros_filter.h:560
std::map< std::string, std::string > dynamicDiagnostics_
This object accumulates dynamic diagnostics, e.g., diagnostics relating to sensor data...
Definition: ros_filter.h:439
tf2_ros::Buffer tfBuffer_
Transform buffer for managing coordinate transforms.
Definition: ros_filter.h:607
diagnostic_updater::Updater diagnosticUpdater_
Used for updating the diagnostics.
Definition: ros_filter.h:451
bool twoDMode_
Whether or not we&#39;re in 2D mode.
Definition: ros_filter.h:623
std::string odomFrameId_
tf frame name for the robot&#39;s odometry (world-fixed) frame
Definition: ros_filter.h:541
double frequency_
The frequency of the run loop.
Definition: ros_filter.h:459
std::ofstream debugStream_
Used for outputting debug messages.
Definition: ros_filter.h:443
std::vector< std::string > stateVariableNames_
Contains the state vector variable names in string format.
Definition: ros_filter.h:599
ros::NodeHandle nh_
Node handle.
Definition: ros_filter.h:533
T filter_
Our filter (EKF, UKF, etc.)
Definition: ros_filter.h:455
FilterStateHistoryDeque filterStateHistory_
An implicitly time ordered queue of past filter states used for smoothing.
Definition: ros_filter.h:666
std::map< std::string, bool > removeGravitationalAcc_
If including acceleration for each IMU input, whether or not we remove acceleration due to gravity...
Definition: ros_filter.h:568


robot_localization
Author(s): Tom Moore
autogenerated on Wed Feb 3 2021 03:34:02