00001 #include <vector>
00002 #include <Eigen/Core>
00003 #include <gtest/gtest.h>
00004
00005
00006 #include <mpc/types.h>
00007 #include <mpc/mpc.h>
00008
00009 #include <mpc/examples/euclidean.h>
00010
00011 using namespace MPC;
00012
00013 void dummyDyn(const VectorT &x, const VectorT &u, VectorT &xN) {
00014 return;
00015 }
00016
00017 void dummyDynD(const VectorT &x, const VectorT &u, Deriv &d) {
00018 return;
00019 }
00020
00021 float dummyCost(const VectorT &x, const VectorT &u) {
00022 return 0.;
00023 }
00024
00025 void dummyCostD(const VectorT &x, const VectorT &u, Deriv &d) {
00026 return;
00027 }
00028
00029 TEST(MPC, DDPInitTest) {
00030 bool init_success = DDP_init(&dummyDyn, &dummyDynD, &dummyCost, &dummyCostD);
00031 ASSERT_TRUE(init_success);
00032 }
00033
00034 TEST(MPC, DDPeuclidean2DTest) {
00035 VectorT goal(VectorT::Zero(2));
00036 VectorT x0(VectorT::Zero(2));
00037 MatrixT u0(MatrixT::Random(2, 100));
00038
00039 x0(0) = 2.;
00040 goal(0) = 2.;
00041 goal(1) = 4.;
00042 euclidean::setGoal(goal);
00043
00044 bool init_success = DDP_init(&euclidean::dynamics, &euclidean::dynamicsD, &euclidean::cost, &euclidean::costD);
00045 ASSERT_TRUE(init_success);
00046
00047 DDPParams p;
00048 Trajectory res;
00049 MPC::DDP(x0, u0, p, 100, res);
00050
00051 ASSERT_NEAR((res.x.col(res.x.cols()-1) - goal).norm(), 0., 5e-4);
00052 }
00053
00054 TEST(MPC, DDPeuclideanWithObstaclesTest) {
00055 VectorT goal(VectorT::Zero(2));
00056 VectorT otmp(VectorT::Zero(2));
00057 VectorT x0(VectorT::Zero(2));
00058 MatrixT u0(MatrixT::Random(2, 100));
00059 u0.array() = u0.array().abs();
00060
00061
00062 goal(0) = 0.;
00063 goal(1) = 4.;
00064 euclidean::setGoal(goal);
00065 euclidean::EuclideanObstacles &obs = euclidean::getObstacles();
00066 obs.clear();
00067
00068 otmp(0) = 1.;
00069 otmp(1) = 2.;
00070 obs.push_back(otmp);
00071 otmp(0) = 2.;
00072 otmp(1) = 1.;
00073 obs.push_back(otmp);
00074 otmp(0) = 3.;
00075 otmp(1) = 2.;
00076 obs.push_back(otmp);
00077 otmp(0) = 1.5;
00078 otmp(1) = 2.;
00079 obs.push_back(otmp);
00080 otmp(0) = 2.;
00081 otmp(1) = 3.;
00082 obs.push_back(otmp);
00083 otmp(0) = 2.5;
00084 otmp(1) = 2.;
00085 obs.push_back(otmp);
00086
00087 bool init_success = DDP_init(&euclidean::dynamics, &euclidean::dynamicsD, &euclidean::cost, &euclidean::costD);
00088 ASSERT_TRUE(init_success);
00089
00090 DDPParams p;
00091 Trajectory res;
00092 MPC::DDP(x0, u0, p, 200, res);
00093
00094 ASSERT_NEAR((res.x.col(res.x.cols()-1) - goal).norm(), 0., 1e-3);
00095 }
00096
00097 TEST(MPC, DDPeuclidean3DWithObstaclesTest) {
00098 VectorT goal(VectorT::Zero(3));
00099 VectorT otmp(VectorT::Zero(3));
00100 VectorT x0(VectorT::Zero(3));
00101 MatrixT u0(MatrixT::Random(3, 100));
00102 u0.array() = u0.array().abs();
00103
00104
00105 goal(0) = 0.;
00106 goal(1) = 4.;
00107 goal(2) = 1.;
00108 euclidean::setGoal(goal);
00109 euclidean::EuclideanObstacles &obs = euclidean::getObstacles();
00110 obs.clear();
00111
00112 otmp(0) = 1.;
00113 otmp(1) = 2.;
00114 otmp(2) = 0.;
00115 obs.push_back(otmp);
00116 otmp(0) = 2.;
00117 otmp(1) = 1.;
00118 otmp(2) = 1.;
00119 obs.push_back(otmp);
00120 otmp(0) = 3.;
00121 otmp(1) = 2.;
00122 otmp(2) = 0.;
00123 obs.push_back(otmp);
00124 otmp(0) = 1.5;
00125 otmp(1) = 2.;
00126 otmp(2) = 1.;
00127 obs.push_back(otmp);
00128 otmp(0) = 2.;
00129 otmp(1) = 3.;
00130 otmp(2) = 0.;
00131 obs.push_back(otmp);
00132 otmp(0) = 2.5;
00133 otmp(1) = 2.;
00134 otmp(2) = 1.;
00135 obs.push_back(otmp);
00136
00137 bool init_success = DDP_init(&euclidean::dynamics, &euclidean::dynamicsD, &euclidean::cost, &euclidean::costD);
00138 ASSERT_TRUE(init_success);
00139
00140 DDPParams p;
00141 Trajectory res;
00142 MPC::DDP(x0, u0, p, 200, res);
00143
00144 ASSERT_NEAR((res.x.col(res.x.cols()-1) - goal).norm(), 0., 1e-3);
00145 }
00146
00147
00148 int main(int argc, char **argv) {
00149 testing::InitGoogleTest(&argc, argv);
00150 return RUN_ALL_TESTS();
00151 }