ros_filter.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2014, 2015, 2016, Charles River Analytics, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  * notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above
00012  * copyright notice, this list of conditions and the following
00013  * disclaimer in the documentation and/or other materials provided
00014  * with the distribution.
00015  * 3. Neither the name of the copyright holder nor the names of its
00016  * contributors may be used to endorse or promote products derived
00017  * from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00027  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00029  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef ROBOT_LOCALIZATION_ROS_FILTER_H
00034 #define ROBOT_LOCALIZATION_ROS_FILTER_H
00035 
00036 #include "robot_localization/ros_filter_utilities.h"
00037 #include "robot_localization/filter_common.h"
00038 #include "robot_localization/filter_base.h"
00039 
00040 #include <robot_localization/SetPose.h>
00041 
00042 #include <ros/ros.h>
00043 #include <std_msgs/String.h>
00044 #include <nav_msgs/Odometry.h>
00045 #include <sensor_msgs/Imu.h>
00046 #include <geometry_msgs/Twist.h>
00047 #include <geometry_msgs/TwistStamped.h>
00048 #include <geometry_msgs/TwistWithCovarianceStamped.h>
00049 #include <geometry_msgs/PoseWithCovarianceStamped.h>
00050 #include <geometry_msgs/AccelWithCovarianceStamped.h>
00051 #include <tf2_ros/transform_listener.h>
00052 #include <tf2_ros/transform_broadcaster.h>
00053 #include <tf2_ros/message_filter.h>
00054 #include <tf2/LinearMath/Transform.h>
00055 #include <message_filters/subscriber.h>
00056 #include <diagnostic_updater/diagnostic_updater.h>
00057 #include <diagnostic_updater/publisher.h>
00058 #include <diagnostic_msgs/DiagnosticStatus.h>
00059 
00060 #include <XmlRpcException.h>
00061 
00062 #include <Eigen/Dense>
00063 
00064 #include <fstream>
00065 #include <map>
00066 #include <numeric>
00067 #include <queue>
00068 #include <string>
00069 #include <vector>
00070 #include <deque>
00071 
00072 namespace RobotLocalization
00073 {
00074 
00075 struct CallbackData
00076 {
00077   CallbackData(const std::string &topicName,
00078                const std::vector<int> &updateVector,
00079                const int updateSum,
00080                const bool differential,
00081                const bool relative,
00082                const double rejectionThreshold) :
00083     topicName_(topicName),
00084     updateVector_(updateVector),
00085     updateSum_(updateSum),
00086     differential_(differential),
00087     relative_(relative),
00088     rejectionThreshold_(rejectionThreshold)
00089   {
00090   }
00091 
00092   std::string topicName_;
00093   std::vector<int> updateVector_;
00094   int updateSum_;
00095   bool differential_;
00096   bool relative_;
00097   double rejectionThreshold_;
00098 };
00099 
00100 typedef std::priority_queue<MeasurementPtr, std::vector<MeasurementPtr>, Measurement> MeasurementQueue;
00101 typedef std::deque<MeasurementPtr> MeasurementHistoryDeque;
00102 typedef std::deque<FilterStatePtr> FilterStateHistoryDeque;
00103 
00104 template<class T> class RosFilter
00105 {
00106   public:
00112     explicit RosFilter(std::vector<double> args = std::vector<double>());
00113 
00118     ~RosFilter();
00119 
00122     void reset();
00123 
00129     void accelerationCallback(const sensor_msgs::Imu::ConstPtr &msg,
00130                               const CallbackData &callbackData,
00131                               const std::string &targetFrame);
00132 
00136     void controlCallback(const geometry_msgs::Twist::ConstPtr &msg);
00137 
00141     void controlCallback(const geometry_msgs::TwistStamped::ConstPtr &msg);
00142 
00152     void enqueueMeasurement(const std::string &topicName,
00153                             const Eigen::VectorXd &measurement,
00154                             const Eigen::MatrixXd &measurementCovariance,
00155                             const std::vector<int> &updateVector,
00156                             const double mahalanobisThresh,
00157                             const ros::Time &time);
00158 
00168     void forceTwoD(Eigen::VectorXd &measurement,
00169                    Eigen::MatrixXd &measurementCovariance,
00170                    std::vector<int> &updateVector);
00171 
00176     bool getFilteredOdometryMessage(nav_msgs::Odometry &message);
00177 
00182     bool getFilteredAccelMessage(geometry_msgs::AccelWithCovarianceStamped &message);
00183 
00194     void imuCallback(const sensor_msgs::Imu::ConstPtr &msg, const std::string &topicName,
00195       const CallbackData &poseCallbackData, const CallbackData &twistCallbackData,
00196       const CallbackData &accelCallbackData);
00197 
00202     void integrateMeasurements(const ros::Time &currentTime);
00203 
00206     void loadParams();
00207 
00217     void odometryCallback(const nav_msgs::Odometry::ConstPtr &msg, const std::string &topicName,
00218       const CallbackData &poseCallbackData, const CallbackData &twistCallbackData);
00219 
00226     void poseCallback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &msg,
00227                       const CallbackData &callbackData,
00228                       const std::string &targetFrame,
00229                       const bool imuData);
00230 
00233     void run();
00234 
00238     void setPoseCallback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &msg);
00239 
00244     bool setPoseSrvCallback(robot_localization::SetPose::Request& request,
00245                             robot_localization::SetPose::Response&);
00246 
00250     std::string tfFailureReasonString(const tf2_ros::FilterFailureReason reason);
00251 
00257     void twistCallback(const geometry_msgs::TwistWithCovarianceStamped::ConstPtr &msg,
00258                        const CallbackData &callbackData,
00259                        const std::string &targetFrame);
00260 
00261   protected:
00270     bool revertTo(const double time);
00271 
00277     void saveFilterState(FilterBase &filter);
00278 
00282     void clearExpiredHistory(const double cutoffTime);
00283 
00290     void addDiagnostic(const int errLevel,
00291                        const std::string &topicAndClass,
00292                        const std::string &message,
00293                        const bool staticDiag);
00294 
00298     void aggregateDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &wrapper);
00299 
00312     void copyCovariance(const double *arr,
00313                         Eigen::MatrixXd &covariance,
00314                         const std::string &topicName,
00315                         const std::vector<int> &updateVector,
00316                         const size_t offset,
00317                         const size_t dimension);
00318 
00326     void copyCovariance(const Eigen::MatrixXd &covariance,
00327                         double *arr,
00328                         const size_t dimension);
00329 
00334     std::vector<int> loadUpdateConfig(const std::string &topicName);
00335 
00344     bool prepareAcceleration(const sensor_msgs::Imu::ConstPtr &msg,
00345                              const std::string &topicName,
00346                              const std::string &targetFrame,
00347                              std::vector<int> &updateVector,
00348                              Eigen::VectorXd &measurement,
00349                              Eigen::MatrixXd &measurementCovariance);
00350 
00363     bool preparePose(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &msg,
00364                      const std::string &topicName,
00365                      const std::string &targetFrame,
00366                      const bool differential,
00367                      const bool relative,
00368                      const bool imuData,
00369                      std::vector<int> &updateVector,
00370                      Eigen::VectorXd &measurement,
00371                      Eigen::MatrixXd &measurementCovariance);
00372 
00382     bool prepareTwist(const geometry_msgs::TwistWithCovarianceStamped::ConstPtr &msg,
00383                       const std::string &topicName,
00384                       const std::string &targetFrame,
00385                       std::vector<int> &updateVector,
00386                       Eigen::VectorXd &measurement,
00387                       Eigen::MatrixXd &measurementCovariance);
00388 
00391     std::string baseLinkFrameId_;
00392 
00395     ros::Subscriber controlSub_;
00396 
00402     std::map<std::string, std::string> staticDiagnostics_;
00403 
00409     std::map<std::string, std::string> dynamicDiagnostics_;
00410 
00413     std::ofstream debugStream_;
00414 
00417     int dynamicDiagErrorLevel_;
00418 
00421     diagnostic_updater::Updater diagnosticUpdater_;
00422 
00425     T filter_;
00426 
00429     double frequency_;
00430 
00435     double historyLength_;
00436 
00442     bool resetOnTimeJump_;
00443 
00446     Eigen::VectorXd latestControl_;
00447 
00450     ros::Time latestControlTime_;
00451 
00454     ros::Duration tfTimeout_;
00455 
00458     std::vector<ros::Subscriber> topicSubs_;
00459 
00470     std::map<std::string, tf2::Transform> initialMeasurements_;
00471 
00480     std::map<std::string, ros::Time> lastMessageTimes_;
00481 
00489     ros::Time lastSetPoseTime_;
00490 
00493     std::string mapFrameId_;
00494 
00499     MeasurementQueue measurementQueue_;
00500 
00503     ros::NodeHandle nh_;
00504 
00507     ros::NodeHandle nhLocal_;
00508 
00511     std::string odomFrameId_;
00512 
00521     std::map<std::string, tf2::Transform> previousMeasurements_;
00522 
00525     std::map<std::string, Eigen::MatrixXd> previousMeasurementCovariances_;
00526 
00529     bool printDiagnostics_;
00530 
00533     std::map<std::string, bool> removeGravitationalAcc_;
00534 
00537     double gravitationalAcc_;
00538 
00542     ros::Subscriber setPoseSub_;
00543 
00547     ros::ServiceServer setPoseSrv_;
00548 
00551     bool smoothLaggedData_;
00552 
00555     std::vector<std::string> stateVariableNames_;
00556 
00559     int staticDiagErrorLevel_;
00560 
00563     tf2_ros::Buffer tfBuffer_;
00564 
00567     tf2_ros::TransformListener tfListener_;
00568 
00571     ros::Duration tfTimeOffset_;
00572 
00579     bool twoDMode_;
00580 
00583     bool useControl_;
00584 
00591     geometry_msgs::TransformStamped worldBaseLinkTransMsg_;
00592 
00595     std::string worldFrameId_;
00596 
00599     bool publishTransform_;
00600 
00603     bool publishAcceleration_;
00604 
00606     //
00607     // front() refers to the filter state with the earliest timestamp.
00608     // back() refers to the filter state with the latest timestamp.
00609     FilterStateHistoryDeque filterStateHistory_;
00610 
00612     // when popped from the measurement priority queue.
00613     // front() refers to the measurement with the earliest timestamp.
00614     // back() refers to the measurement with the latest timestamp.
00615     MeasurementHistoryDeque measurementHistory_;
00616 };
00617 
00618 }  // namespace RobotLocalization
00619 
00620 #endif  // ROBOT_LOCALIZATION_ROS_FILTER_H


robot_localization
Author(s): Tom Moore
autogenerated on Thu Jun 6 2019 21:01:48