|
| 1 | +/* |
| 2 | + * Copyright 2018, |
| 3 | + * Julian Viereck |
| 4 | + * |
| 5 | + * CNRS/AIST |
| 6 | + * |
| 7 | + * This file is part of sot-core. |
| 8 | + * sot-core is free software: you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public License |
| 10 | + * as published by the Free Software Foundation, either version 3 of |
| 11 | + * the License, or (at your option) any later version. |
| 12 | + * sot-core is distributed in the hope that it will be |
| 13 | + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 14 | + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Lesser General Public License for more details. You should |
| 16 | + * have received a copy of the GNU Lesser General Public License along |
| 17 | + * with sot-core. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <boost/function.hpp> |
| 21 | + |
| 22 | +#include <dynamic-graph/all-commands.h> |
| 23 | +#include <dynamic-graph/factory.h> |
| 24 | + |
| 25 | +#include <sot/core/factory.hh> |
| 26 | +#include <sot/core/exp-moving-avg.hh> |
| 27 | + |
| 28 | +namespace dg = ::dynamicgraph; |
| 29 | + |
| 30 | +/* ---------------------------------------------------------------------------*/ |
| 31 | +/* ------- GENERIC HELPERS -------------------------------------------------- */ |
| 32 | +/* ---------------------------------------------------------------------------*/ |
| 33 | + |
| 34 | +namespace dynamicgraph { |
| 35 | + namespace sot { |
| 36 | + |
| 37 | + |
| 38 | +DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(ExpMovingAvg,"ExpMovingAvg"); |
| 39 | + |
| 40 | + |
| 41 | +/* --------------------------------------------------------------------- */ |
| 42 | +/* --- CLASS ----------------------------------------------------------- */ |
| 43 | +/* --------------------------------------------------------------------- */ |
| 44 | + |
| 45 | + |
| 46 | +ExpMovingAvg:: |
| 47 | +ExpMovingAvg( const std::string& n ) |
| 48 | + :Entity(n) |
| 49 | + ,updateSIN(NULL, "ExpMovingAvg(" + n + ")::input(vector)::update") |
| 50 | + ,alpha(0.) |
| 51 | + ,init(false) |
| 52 | + ,refresherSINTERN( "ExpMovingAvg("+n+")::intern(dummy)::refresher" ) |
| 53 | + ,averageSOUT( |
| 54 | + boost::bind(&ExpMovingAvg::update,this,_1,_2), |
| 55 | + updateSIN << refresherSINTERN, "ExpMovingAvg(" + n + ")::output(vector)::average") |
| 56 | +{ |
| 57 | + // Register signals into the entity. |
| 58 | + signalRegistration(updateSIN << averageSOUT); |
| 59 | + refresherSINTERN.setDependencyType( TimeDependency<int>::ALWAYS_READY ); |
| 60 | + |
| 61 | + std::string docstring; |
| 62 | + // setAlpha |
| 63 | + docstring = |
| 64 | + "\n" |
| 65 | + " Set the alpha used to update the current value." |
| 66 | + "\n"; |
| 67 | + addCommand(std::string("setAlpha"), |
| 68 | + new ::dynamicgraph::command::Setter<ExpMovingAvg, double> |
| 69 | + (*this, &ExpMovingAvg::setAlpha, docstring)); |
| 70 | +} |
| 71 | + |
| 72 | +ExpMovingAvg::~ExpMovingAvg() |
| 73 | +{ |
| 74 | +} |
| 75 | + |
| 76 | +/* --- COMPUTE ----------------------------------------------------------- */ |
| 77 | +/* --- COMPUTE ----------------------------------------------------------- */ |
| 78 | +/* --- COMPUTE ----------------------------------------------------------- */ |
| 79 | + |
| 80 | +void ExpMovingAvg::setAlpha(const double& alpha_) { |
| 81 | + assert(alpha <= 1. && alpha >= 0.); |
| 82 | + alpha = alpha_; |
| 83 | +} |
| 84 | + |
| 85 | +dynamicgraph::Vector& ExpMovingAvg::update(dynamicgraph::Vector& res, |
| 86 | + const int& inTime) |
| 87 | +{ |
| 88 | + const dynamicgraph::Vector& update = updateSIN(inTime); |
| 89 | + |
| 90 | + if (init == false) { |
| 91 | + init = true; |
| 92 | + average = update; |
| 93 | + average.setZero(); |
| 94 | + res.resize(average.size()); |
| 95 | + } |
| 96 | + |
| 97 | + res = average = alpha * average + (1. - alpha) * update; |
| 98 | + return res; |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + } /* namespace sot */ |
| 104 | +} /* namespace dynamicgraph */ |
0 commit comments