filter.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2014-2017, the neonavigation authors
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 are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the copyright holder nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #ifndef TRAJECTORY_TRACKER_FILTER_H
00031 #define TRAJECTORY_TRACKER_FILTER_H
00032 
00033 #include <cmath>
00034 
00035 namespace trajectory_tracker
00036 {
00037 class Filter
00038 {
00039 public:
00040   enum Type
00041   {
00042     FILTER_HPF,
00043     FILTER_LPF
00044   };
00045 
00046 protected:
00047   Type type_;
00048   float time_const_;
00049   float x_;
00050   float out_;
00051   float k_[4];
00052   bool angle_;
00053 
00054 public:
00055   Filter(const enum Type type, const float tc, const float out0, const bool angle = false)
00056   {
00057     angle_ = angle;
00058     time_const_ = tc;
00059     type_ = type;
00060     switch (type_)
00061     {
00062       case FILTER_LPF:
00063         k_[3] = -1 / (1.0 + 2 * time_const_);
00064         k_[2] = -k_[3];
00065         k_[1] = (1.0 - 2 * time_const_) * k_[3];
00066         k_[0] = -k_[1] - 1.0;
00067         x_ = (1 - k_[2]) * out0 / k_[3];
00068         break;
00069       case FILTER_HPF:
00070         k_[3] = -1 / (1.0 + 2 * time_const_);
00071         k_[2] = -k_[3] * 2 * time_const_;
00072         k_[1] = (1.0 - 2 * time_const_) * k_[3];
00073         k_[0] = 2 * time_const_ * (-k_[1] + 1.0);
00074         x_ = (1 - k_[2]) * out0 / k_[3];
00075         break;
00076     }
00077     out_ = out0;
00078   }
00079   void set(const float& out0)
00080   {
00081     x_ = (1 - k_[2]) * out0 / k_[3];
00082     out_ = out0;
00083   }
00084   float in(const float& i)
00085   {
00086     float in = i;
00087     assert(std::isfinite(in));
00088 
00089     if (angle_)
00090     {
00091       in = out_ + remainder(in - out_, M_PI * 2.0);
00092     }
00093     x_ = k_[0] * in + k_[1] * x_;
00094     out_ = k_[2] * in + k_[3] * x_;
00095 
00096     assert(std::isfinite(out_));
00097     return out_;
00098   }
00099   float get()
00100   {
00101     return out_;
00102   }
00103 };
00104 }  // namespace trajectory_tracker
00105 
00106 #endif  // TRAJECTORY_TRACKER_FILTER_H


trajectory_tracker
Author(s): Atsushi Watanabe
autogenerated on Sat Jun 22 2019 20:07:25