GCOP
1.0
|
00001 #ifndef GCOP_DOEP_H 00002 #define GCOP_DOEP_H 00003 00004 #include <Eigen/Dense> 00005 #include <vector> 00006 #include <type_traits> 00007 #include <algorithm> 00008 #include <iterator> 00009 #include "system.h" 00010 #include "sensor.h" 00011 #include "sensorcost.h" 00012 #include <cmath> 00013 #include "rn.h" 00014 00015 namespace gcop { 00016 00017 using namespace std; 00018 using namespace Eigen; 00019 00020 template <typename T, 00021 int nx = Dynamic, 00022 int nu = Dynamic, 00023 int np = Dynamic, 00024 typename Tz=VectorXd, 00025 int nz = Dynamic, 00026 typename T1=T, 00027 int nx1=nx> class Doep { 00028 00029 typedef Matrix<double, nx, 1> Vectornd; 00030 typedef Matrix<double, nu, 1> Vectorcd; 00031 typedef Matrix<double, np, 1> Vectormd; 00032 typedef Matrix<double, nz, 1> Vectorrd; 00033 00034 typedef Matrix<double, nx, nx> Matrixnd; 00035 typedef Matrix<double, nx, nu> Matrixncd; 00036 typedef Matrix<double, nu, nx> Matrixcnd; 00037 typedef Matrix<double, nu, nu> Matrixcd; 00038 00039 typedef void(*Func_type)(const T &, T1 &); 00040 00041 public: 00061 Doep(System<T, nx, nu, np> &sys, Sensor<T1, nx1, nu, np, Tz, nz> &sensor, 00062 SensorCost<T, nx, nu, np, Tz, nz> &cost, 00063 vector<double> &ts, vector<T> &xs, vector<Vectorcd> &us, 00064 Vectormd &p, vector<double> &ts1, Func_type _project=NULL, bool update = true); 00065 00066 virtual ~Doep(); 00067 00071 virtual void Iterate(); 00072 00077 void Update(bool der = true); 00078 00079 System<T, nx, nu, np> &sys; 00080 00081 Sensor<T1, nx1, nu, np, Tz, nz> &sensor; 00082 00083 SensorCost<T, nx, nu, np, Tz, nz> &cost; 00084 00085 std::vector<double> &ts; 00086 00087 std::vector<double> &ts1; 00088 00089 std::vector<T> &xs; 00090 00091 std::vector<Vectorcd> &us; 00092 00093 Vectormd &p; 00094 00095 std::vector<Vectornd> ws; 00096 00097 std::vector<Vectorrd> zs; 00098 00099 bool debug; 00100 00101 double eps; 00102 00103 Func_type project; 00104 00105 }; 00106 00107 using namespace std; 00108 using namespace Eigen; 00109 00110 template <typename T, int nx, int nu, int np, typename Tz, int nz, typename T1, int nx1> 00111 Doep<T, nx, nu, np, Tz, nz, T1, nx1>::Doep(System<T, nx, nu, np> &sys, Sensor<T1, nx1, nu, np, Tz, nz> &sensor, 00112 SensorCost<T, nx, nu, np, Tz, nz> &cost, 00113 vector<double> &ts, 00114 vector<T> &xs, 00115 vector<Matrix<double, nu, 1> > &us, 00116 Matrix<double, np, 1> &p, 00117 vector<double> &ts1, Func_type _project, bool update) : 00118 sys(sys), sensor(sensor), cost(cost), ts(ts), xs(xs), us(us), p(p), ts1(ts1), project(_project), 00119 debug(true), eps(1e-3) 00120 { 00121 int N = us.size(); 00122 assert(N > 0); 00123 assert(ts.size() == N+1); 00124 assert(xs.size() == N+1); 00125 ws.resize(N);//Internal process noise 00126 if(nx == Dynamic) 00127 { 00128 for(int i =0; i<N; ++i) 00129 { 00130 ws[i].resize(sys.X.n); 00131 } 00132 } 00133 zs.resize(ts1.size()); //Internal sensor measurements 00134 if(nz == Dynamic) 00135 { 00136 for(int i = 0; i<ts1.size(); ++i) 00137 { 00138 zs[i].resize(sensor.Z.n); 00139 } 00140 } 00141 for(int i =0; i<N; ++i) 00142 { 00143 ws[i].setZero();//Set the noise to zero when constructing 00144 } 00145 00146 if (update) { 00147 Update(); 00148 } 00149 } 00150 00151 template <typename T, int nx, int nu, int np, typename Tz, int nz, typename T1, int nx1> 00152 Doep<T, nx, nu, np, Tz, nz, T1, nx1>::~Doep() 00153 { 00154 } 00155 00156 template <typename T, int nx, int nu, int np, typename Tz, int nz, typename T1, int nx1> 00157 void Doep<T, nx, nu, np, Tz, nz, T1, nx1>::Update(bool der) { 00158 00159 typedef Matrix<double, nx, 1> Vectornd; 00160 typedef Matrix<double, nu, 1> Vectorcd; 00161 typedef Matrix<double, nz, 1> Vectorrd; 00162 00163 00164 int N = us.size(); 00165 T1 x1; //Temporary projection for finding sensor measurements 00166 00167 sys.Reset(xs[0],ts[0]);//Reset 00168 int sensor_index = 0; 00169 assert(ts1[0]>= ts[0]);//Sensor measurements are after the control started 00170 assert(ts1.back() <= ts.back());//Sensor measurements are before control ended 00171 00172 for (int k = 0; k < N; ++k) { 00173 double h = ts[k+1] - ts[k]; 00174 sys.Step(xs[k+1], us[k], h, ws[k], &p); 00175 //cout<<"ts1["<<sensor_index<<"]: "<<ts1[sensor_index]<<"\t"<<ts[k]<<"\t"<<ts[k+1]<<endl;//#DEBUG 00176 while((ts1[sensor_index] - ts[k])>= 0 && (ts1[sensor_index] - ts[k+1]) < 0)//Nearest state to find the sensor measurement 00177 { 00178 //#TODO Add Linear Interpolation if necessary 00179 int near_index = (ts1[sensor_index] - ts[k]) > -(ts1[sensor_index] - ts[k+1])?(k+1):k; 00180 //Project the state 00181 if(std::is_same<T, T1>::value) 00182 { 00183 x1 = (T1)(*((T1 *)(&xs[near_index])));//Hacky way of copying pointer over 00184 sensor(zs[sensor_index], ts[near_index], x1, us[near_index], &p); 00185 } 00186 else 00187 { 00188 assert(project != NULL);// Can be removed 00189 project(xs[near_index],x1); 00190 sensor(zs[sensor_index], ts[near_index], x1, us[near_index], &p); 00191 //cout<<"UPDATE: "<<"Zs["<<sensor_index<<"]: "<<zs[sensor_index].transpose()<<endl;//#DEBUG 00192 } 00193 if(sensor_index == (ts1.size()-1)) 00194 break; 00195 sensor_index = sensor_index < (ts1.size()-1)?sensor_index+1:sensor_index; 00196 } 00197 //getchar();//#DEBUG 00198 } 00199 assert(sensor_index == (ts1.size()-1));//Assert that we collected all the sensor data 00200 } 00201 00202 template <typename T, int nx, int nu, int np, typename Tz, int nz, typename T1, int nx1> 00203 void Doep<T, nx, nu, np, Tz, nz, T1, nx1>::Iterate() { 00204 cout << "[W] Doep::Iterate: subclasses should implement this!" << endl; 00205 } 00206 } 00207 00208 #endif