SmoothDerivative.cpp
Go to the documentation of this file.
00001 
00018 #include "robodyn_utilities/SmoothDerivative.h"
00019 #include "nasa_common_logging/Logger.h"
00020 #include <string.h>
00021 #include <stdlib.h>
00022 
00023 
00024 
00030 SmoothDerivative::SmoothDerivative()
00031 {
00032     isInitialized = false;
00033 }
00034 
00035 
00036 
00047 SmoothDerivative::SmoothDerivative(std::vector<double> xA, std::vector<double> smoothingA, double frequencyA)
00048 {
00049     isInitialized = Initialize(xA, smoothingA, frequencyA);
00050 }
00051 
00052 
00053 
00059 SmoothDerivative::~SmoothDerivative()
00060 {
00061 }
00062 
00063 
00064 
00075 bool SmoothDerivative::Initialize(std::vector<double> xA, std::vector<double> smoothingA, double frequencyA)
00076 {
00077     isInitialized = false;
00078 
00079     //check that the sizes are equal
00080     if (xA.size() != smoothingA.size())
00081     {
00082         return false;
00083     }
00084 
00085     if (xDotCalc.size() != xA.size())
00086     {
00087         xDotCalc.clear();
00088         xDotCalc.resize(xA.size());
00089     }
00090 
00091     m_freq = frequencyA;
00092     m_smoothing = smoothingA;
00093 
00094     for (unsigned int iL = 0; iL < smoothingA.size(); iL++)
00095     {
00096         xDotCalc[iL].m_xPrevSample     = xA[iL];
00097         xDotCalc[iL].m_xPrevTransition = xA[iL];
00098         xDotCalc[iL].m_smooth          = (smoothingA[iL] < 0.0) ? 0.0 : (smoothingA[iL] > 1.0) ? 1.0 : smoothingA[iL];
00099         xDotCalc[iL].m_innovation      = (1.0 - xDotCalc[iL].m_smooth) * frequencyA;
00100         xDotCalc[iL].m_regression      = xDotCalc[iL].m_smooth;
00101         xDotCalc[iL].m_numTicks        = 0;
00102         xDotCalc[iL].m_delta           = 0;
00103         xDotCalc[iL].m_numTransitions  = 0;
00104         xDotCalc[iL].m_xDot            = 0;
00105     }
00106 
00107     isInitialized = true;
00108     return true;
00109 }
00110 
00111 
00121 void SmoothDerivative::OnExecute(std::vector<double> xA, std::vector<double>& xDotA)
00122 {
00123     if (!isInitialized)
00124     {
00125         // you muse initialize before you use
00126         std::stringstream err;
00127         err << "You must initilize SmoothDerivative before calling OnExecute";
00128         NasaCommonLogging::Logger::log("gov.nasa.robonet.SmoothDerivative", log4cpp::Priority::ERROR, err.str());
00129         throw std::runtime_error(err.str());
00130     }
00131 
00132     double       diffL;
00133     unsigned int iL;
00134 
00135     if (xA.size() != xDotCalc.size())
00136     {
00137         std::stringstream err;
00138         err << "xA should be of size " << xDotCalc.size() << " but it is " << xA.size() << ".";
00139         NasaCommonLogging::Logger::log("gov.nasa.robonet.SmoothDerivative", log4cpp::Priority::ERROR, err.str());
00140         //Initialize(xA, m_smoothing, m_freq );
00141         throw std::runtime_error(err.str());
00142     }
00143 
00144     if (xA.size() != xDotA.size())
00145     {
00146         xDotA.resize(xA.size());
00147     }
00148 
00149     for (iL = 0; iL < xA.size(); iL++)
00150     {
00151         ++xDotCalc[iL].m_numTicks;
00152 //        std::cout<<"num Ticks: "<<xDotCalc[iL].m_numTicks<<std::endl;
00153 
00154         diffL = xDotCalc[iL].m_xPrevSample - xA[iL];
00155 //        std::cout<<"diffL: "<<diffL<<std::endl;
00156 //        std::cout<<"numTransitions: "<<xDotCalc[iL].m_numTransitions;
00157 
00158         if (diffL != 0.)     //  SIGNAL VALUE CHANGED
00159         {
00160             xDotCalc[iL].m_numTicksPrev    = xDotCalc[iL].m_numTicks;
00161             xDotCalc[iL].m_xPrevTransition = xDotCalc[iL].m_xPrevSample;
00162             xDotCalc[iL].m_xPrevSample     = xA[iL];
00163             xDotCalc[iL].m_delta           = xDotCalc[iL].m_delta * 0.8 + xDotCalc[iL].m_numTicks * 0.2;     //  FILTER THE EFFECTIVE DELTA TIME
00164             ++xDotCalc[iL].m_numTransitions;
00165             xDotCalc[iL].m_numTicks = 0;
00166         }
00167         else
00168         {
00169             if (xDotCalc[iL].m_numTicks > xDotCalc[iL].m_numTicksPrev)
00170             {
00171                 xDotCalc[iL].m_delta = xDotCalc[iL].m_delta * 0.8 + xDotCalc[iL].m_numTicks * 0.2;
00172             }
00173         }
00174 
00175         if (xDotCalc[iL].m_numTransitions > 2 && xDotCalc[iL].m_delta != 0)
00176         {
00177             xDotCalc[iL].m_xDot = xDotCalc[iL].m_innovation * (xA[iL] - xDotCalc[iL].m_xPrevTransition) / xDotCalc[iL].m_delta + xDotCalc[iL].m_regression * xDotCalc[iL].m_xDot;
00178         }
00179         else
00180         {
00181             xDotCalc[iL].m_xDot = 0.0;
00182         }
00183 
00184         xDotA[iL] = xDotCalc[iL].m_xDot;
00185     }
00186 }


robodyn_utilities
Author(s):
autogenerated on Thu Jun 6 2019 18:56:08