SmoothDerivative_Test.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <gtest/gtest.h>
00003 #include "robodyn_utilities/SmoothDerivative.h"
00004 #include <ros/package.h>
00005 #include <stdexcept>
00006 #include <math.h>
00007 #include <fstream>
00008 
00009 class SmoothDerivativeTest : public ::testing::Test
00010 {
00011 protected:
00012     virtual void SetUp()
00013     {
00014         ASSERT_NO_THROW(sd = new SmoothDerivative());
00015     }
00016 
00017     virtual void TearDown()
00018     {
00019     }
00020 
00021     SmoothDerivative* sd;
00022 
00023 };
00024 
00025 
00026 TEST_F(SmoothDerivativeTest, initialize)
00027 {
00028     EXPECT_FALSE(sd->isInitialized);
00029 
00030     std::vector<double> a_0;
00031     std::vector<double> a_smoothing;
00032 
00033     EXPECT_TRUE(sd->Initialize(a_0, a_smoothing, 350));
00034 
00035     EXPECT_TRUE(sd->isInitialized);
00036 
00037     a_0.push_back(10);
00038 
00039     EXPECT_FALSE(sd->Initialize(a_0, a_smoothing, 350));
00040     EXPECT_FALSE(sd->isInitialized);
00041 
00042     a_smoothing.push_back(0.2);
00043 
00044     EXPECT_TRUE(sd->Initialize(a_0, a_smoothing, 350));
00045 
00046 }
00047 
00048 TEST_F(SmoothDerivativeTest, OnExecute)
00049 {
00050     std::vector<double> a_0;
00051     std::vector<double> a;
00052     std::vector<double> adot;
00053     std::vector<double> a_smoothing;
00054 
00055     a_0.push_back(0);
00056     a_smoothing.push_back(0.2);
00057 
00058     EXPECT_THROW(sd->OnExecute(a, adot), std::runtime_error);
00059 
00060     sd->Initialize(a_0, a_smoothing, 1);
00061 
00062     //size mismatch, a = size 0, but initilization vals = 1
00063     EXPECT_THROW(sd->OnExecute(a, adot), std::runtime_error);
00064 
00065     a.push_back(0);
00066 
00067     EXPECT_NO_THROW(sd->OnExecute(a, adot));
00068 
00069     for (int i = 1; i < 100; ++i)
00070     {
00071         a[0] = i * 3;
00072 
00073         EXPECT_NO_THROW(sd->OnExecute(a, adot));
00074 
00075         EXPECT_EQ(1, a.size());
00076         EXPECT_EQ(1, adot.size());
00077         //EXPECT_NEAR(3, adot[0], 3/i);
00078 
00079     }
00080 
00081     double oldvel = a[0];
00082     EXPECT_FLOAT_EQ(3, adot[0]);
00083 
00084     for (int i = 1; i < 10000; ++i)
00085     {
00086         a[0] = oldvel;
00087 
00088         EXPECT_NO_THROW(sd->OnExecute(a, adot));
00089 
00090         EXPECT_EQ(1, a.size());
00091         EXPECT_EQ(1, adot.size());
00092         //EXPECT_NEAR(0, adot[0], 3/i);
00093     }
00094 
00095     EXPECT_NEAR(0, adot[0], 0.001);
00096 
00097 }
00098 /*
00099 TEST_F(SmoothDerivativeTest, outputData)
00100 {
00101     std::vector<double> a(5, 0.);
00102     std::vector<double> adot(5, 0.8);
00103     std::vector<double> a_smoothing(5, 0.);
00104 
00105     a_smoothing[1] = 0.85;
00106     a_smoothing[2] = 0.9;
00107     a_smoothing[3] = 0.95;
00108     a_smoothing[4] = 0.98;
00109 
00110     sd->Initialize(a, a_smoothing, 350);
00111 
00112     std::ofstream file;
00113     file.open("test.csv");
00114     file << "t, p, dp, v0, v2, v5, v8, v10" << std::endl;
00115     file << "0, 0, 0, 0, 0, 0, 0, 0" << std::endl;
00116 
00117     unsigned int cycle = 0;
00118     unsigned int minCycles = 50;
00119     unsigned int maxCycles = 5000;
00120     double eps = 0.0001;
00121     double t = 0.;
00122     double prev = 0.;
00123 
00124     // constant
00125     cycle = 0;
00126     while ((fabs(adot[0]) > eps || fabs(adot[1]) > eps || fabs(adot[2]) > eps || fabs(adot[3]) > eps || fabs(adot[4]) > eps ||cycle < minCycles) && cycle < maxCycles)
00127     {
00128         t += 1./350.;
00129         EXPECT_NO_THROW(sd->OnExecute(a,adot));
00130         file << t << ", " << a[0] << ", " << (a[0] - prev) * 350. << ", " << adot[0] << ", " << adot[1] << ", " << adot[2] << ", " << adot[3] << ", " << adot[4] << std::endl;
00131         ++cycle;
00132         prev = a[0];
00133     }
00134 
00135     // step input
00136     a[0] = a[1] = a[2] = a[3] = a[4] = static_cast<double>(static_cast<long>(50.*57.33))/57.33;
00137     cycle = 0;
00138     while ((fabs(adot[0]) > eps || fabs(adot[1]) > eps || fabs(adot[2]) > eps || fabs(adot[3]) > eps || fabs(adot[4]) > eps ||cycle < minCycles) && cycle < maxCycles)
00139     {
00140         t += 1./350.;
00141         EXPECT_NO_THROW(sd->OnExecute(a,adot));
00142         file << t << ", " << a[0] << ", " << (a[0] - prev) * 350. << ", " << adot[0] << ", " << adot[1] << ", " << adot[2] << ", " << adot[3] << ", " << adot[4] << std::endl;
00143         ++cycle;
00144         prev = a[0];
00145     }
00146 
00147     // impulse input
00148     cycle = 0;
00149     while ((fabs(adot[0]) > eps || fabs(adot[1]) > eps || fabs(adot[2]) > eps || fabs(adot[3]) > eps || fabs(adot[4]) > eps ||cycle < minCycles) && cycle < maxCycles)
00150     {
00151         t += 1./350.;
00152         if (cycle == 0)
00153         {
00154             a[0] = a[1] = a[2] = a[3] = a[4] = 0.;
00155         }
00156         else
00157         {
00158             a[0] = a[1] = a[2] = a[3] = a[4] = static_cast<double>(static_cast<long>(50.*57.33))/57.33;
00159         }
00160         EXPECT_NO_THROW(sd->OnExecute(a,adot));
00161         file << t << ", " << a[0] << ", " << (a[0] - prev) * 350. << ", " << adot[0] << ", " << adot[1] << ", " << adot[2] << ", " << adot[3] << ", " << adot[4] << std::endl;
00162         ++cycle;
00163         prev = a[0];
00164     }
00165 
00166     // sin wave
00167     cycle = 0;
00168     while (cycle < maxCycles)
00169     {
00170         t += 1./350.;
00171         a[0] = a[1] = a[2] = a[3] = a[4] = static_cast<double>(static_cast<long>(57.33 * cos(cycle * 3.14159/350)*50.))/57.33;
00172         EXPECT_NO_THROW(sd->OnExecute(a,adot));
00173         file << t << ", " << a[0] << ", " << (a[0] - prev) * 350. << ", " << adot[0] << ", " << adot[1] << ", " << adot[2] << ", " << adot[3] << ", " << adot[4] << std::endl;
00174         ++cycle;
00175         prev = a[0];
00176     }
00177     file.close();
00178 }*/
00179 
00180 int main(int argc, char** argv)
00181 {
00182     testing::InitGoogleTest(&argc, argv);
00183     return RUN_ALL_TESTS();
00184 }


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