GCOP  1.0
controltparam.h
Go to the documentation of this file.
00001 #ifndef GCOP_CONTROLTPARAM_H
00002 #define GCOP_CONTROLTPARAM_H
00003 
00004 #include "tparam.h"
00005 
00006 namespace gcop {
00007   
00008   using namespace std;
00009   using namespace Eigen;
00010 
00020   template <typename T, 
00021     int nx = Dynamic, 
00022     int nu = Dynamic,
00023     int np = Dynamic,
00024     int _ntp = Dynamic> class ControlTparam : public Tparam<T, nx, nu, np, _ntp> {
00025     
00026     typedef Matrix<double, nx, 1> Vectornd;
00027     typedef Matrix<double, nu, 1> Vectorcd;
00028     typedef Matrix<double, nx, nx> Matrixnd;
00029     typedef Matrix<double, nx, nu> Matrixncd;
00030     typedef Matrix<double, nu, nx> Matrixcnd;
00031     typedef Matrix<double, nu, nu> Matrixcd;  
00032 
00033     typedef Matrix<double, np, 1> Vectormd;
00034 
00035     typedef Matrix<double, _ntp, 1> Vectorntpd;
00036     
00037   public:
00038     ControlTparam(System<T, nx, nu, np> &sys, const vector<double> &ts);
00039     
00040     bool To(Vectorntpd &s, 
00041             const vector<double> &ts, 
00042             const vector<T> &xs, 
00043             const vector<Vectorcd> &us,
00044             const Vectormd *p = 0);
00045     
00046     bool From(vector<double> &ts, 
00047               vector<T> &xs, 
00048               vector<Vectorcd> &us,
00049               const Vectorntpd &s,
00050               Vectormd *p = 0);
00051     
00052     const vector<double> &tks;  
00053   };
00054   
00055   template <typename T, int nx, int nu, int np, int _ntp> 
00056     ControlTparam<T, nx, nu, np, _ntp>::ControlTparam(System<T, nx, nu, np> &sys, const vector<double> &tks) :  Tparam<T, nx, nu, np, _ntp>(sys, tks.size()*sys.U.n), tks(tks) {
00057   }
00058 
00059   template <typename T, int nx, int nu, int np, int _ntp> 
00060     bool ControlTparam<T, nx, nu, np, _ntp>::To(Vectorntpd &s, 
00061                                              const vector<double> &ts, 
00062                                              const vector<T> &xs, 
00063                                              const vector<Vectorcd> &us,
00064                                              const Vectormd *p) {
00065     int j =0;//Iterator for tks
00066     cout<<"s.size: "<<s.size()<<endl;
00067     for(int i = 0;i < us.size(); ++i)
00068     {
00069       if ((ts[i] - tks[j]) >= 0)
00070       {
00071         if(j < tks.size())
00072         {
00073           s.segment(j*(this->sys.U.n), this->sys.U.n) = us[i];
00074           cout<<"s["<<j<<"]: "<<us[i].transpose()<<endl;
00075           j++;
00076         }
00077       }
00078     }
00079     s.tail(this->sys.U.n) = us[us.size()-1];
00080     cout<<"s: "<<s.transpose()<<endl;
00081     return true;
00082     //s.setConstant(.001);
00083   }
00084   
00085   template <typename T, int nx, int nu, int np, int _ntp> 
00086     bool ControlTparam<T, nx, nu, np, _ntp>::From(vector<double> &ts, 
00087                                                   vector<T> &xs, 
00088                                                   vector<Vectorcd> &us,
00089                                                   const Vectorntpd &s,
00090                                                   Vectormd *p) {
00091     if(this->ntp != tks.size()*this->sys.U.n)
00092       return false;
00093     
00094     int ki = 0; // knot index
00095     for (int i = 0; i < us.size(); ++i) {
00096       int si = ki*this->sys.U.n;
00097       assert(si + this->sys.U.n < s.size());
00098       const Vectorcd &ua = s.segment(si, this->sys.U.n);   // left control
00099       const Vectorcd &ub = s.segment(si + this->sys.U.n, this->sys.U.n);  // right control
00100 
00101       double &t = ts[i];
00102       us[i] = ua + (ub - ua)*(t - tks[ki])/(tks[ki+1] - tks[ki]);
00103       if (t > tks[ki + 1] + 1e-16) {
00104         ki++;
00105       }      
00106     }
00107 
00108     this->sys.Reset(xs[0],ts[0]);
00109     for (int i = 0; i < us.size(); ++i) {
00110       this->sys.Step(xs[i+1], us[i], ts[i+1] - ts[i], p);
00111       //cout<<"Xs["<<(i+1)<<"]"<<xs[i+1].transpose()<<endl; #DEBUG
00112       //cout<<"us["<<i<<"]"<<us[i].transpose()<<endl;
00113     }
00114     return true;
00115   }
00116 }
00117 
00118 #endif