00001 /* 00002 * Copyright 2018 The urg_stamped Authors 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef TIMESTAMP_OUTLIER_REMOVER_H 00018 #define TIMESTAMP_OUTLIER_REMOVER_H 00019 00020 #include <ros/ros.h> 00021 00022 #include <cmath> 00023 00024 class TimestampOutlierRemover 00025 { 00026 protected: 00027 ros::Time stamp_; 00028 ros::Duration diff_max_; 00029 ros::Duration interval_; 00030 size_t outlier_cnt_; 00031 00032 public: 00033 TimestampOutlierRemover( 00034 const ros::Duration &diff_max, 00035 const ros::Duration &interval) 00036 : diff_max_(diff_max) 00037 , interval_(interval) 00038 , outlier_cnt_(0) 00039 { 00040 } 00041 void setInterval(const ros::Duration &interval) 00042 { 00043 interval_ = interval; 00044 } 00045 ros::Time update(const ros::Time &stamp) 00046 { 00047 if (stamp_ == ros::Time()) 00048 stamp_ = stamp - interval_; 00049 00050 const auto interval = stamp - stamp_; 00051 const int scan_num = lround(interval.toSec() / interval_.toSec()); 00052 const double interval_remainder = remainder(interval.toSec(), interval_.toSec()); 00053 00054 if (fabs(interval_remainder) > diff_max_.toSec()) 00055 { 00056 stamp_ += interval_ * scan_num; 00057 if (outlier_cnt_ >= 1) 00058 stamp_ = stamp; 00059 00060 outlier_cnt_++; 00061 } 00062 else 00063 { 00064 stamp_ = stamp; 00065 outlier_cnt_ = 0; 00066 } 00067 00068 return stamp_; 00069 } 00070 void reset() 00071 { 00072 stamp_ = ros::Time(); 00073 } 00074 }; 00075 00076 #endif // TIMESTAMP_OUTLIER_REMOVER_H