GCOP  1.0
body3davoidcontroller.h
Go to the documentation of this file.
00001 #ifndef GCOP_BODY3DAVOIDCONTROLLER_H
00002 #define GCOP_BODY3DAVOIDCONTROLLER_H
00003 
00004 #include "body3dcontroller.h"
00005 #include "gavoidcontroller.h"
00006 
00007 namespace gcop {
00008   
00009   using namespace std;
00010   using namespace Eigen;
00011   
00019   template <int nu = 6> 
00020     class Body3dAvoidController : public Controller<Body3dState, Matrix<double, nu, 1>, Matrix<double, 5, 1>, Body3dState > {
00021   public:
00022   typedef Matrix<double, nu, 1> Vectorcd;
00023   typedef Matrix<double, 5, 1> Vector5d;
00024   
00033   Body3dAvoidController(const Body3d<nu> &sys,
00034                         Body3dState *xd = 0,
00035                         Vector6d *ad = 0,
00036                         Body3dConstraint *con = 0);
00037   
00038   virtual bool Set(Vectorcd &u, double t, const Body3dState &x);
00039 
00040   virtual bool SetParams(const Vector5d &s);
00041 
00042   virtual bool SetContext(const Body3dState &c);
00043 
00044   Body3dController<nu> stabCtrl;
00045   GavoidController *avoidCtrl;
00046   
00047   };
00048   
00049   template <int nu>   
00050   Body3dAvoidController<nu>::Body3dAvoidController(const Body3d<nu> &sys,
00051                                                   Body3dState *xd, 
00052                                                   Vector6d *ad,
00053                                                   Body3dConstraint *con) :
00054     stabCtrl(sys, xd, ad)
00055     {
00056       if (con) 
00057         avoidCtrl = new GavoidController(*con);
00058       else
00059         avoidCtrl = 0;
00060     }
00061   
00062   template <int nu>   
00063     bool Body3dAvoidController<nu>::Body3dAvoidController::Set(Vectorcd &u, 
00064                                                                double t, 
00065                                                                const Body3dState &x)
00066     {
00067       stabCtrl.Set(u, t, x);      
00068       if (avoidCtrl) {
00069         Vector6d uo;
00070         avoidCtrl->Set(uo, t, x);
00071         u += uo; // add translational forces for obstacle avoidance
00072       }
00073       return true;
00074     }
00075   
00076   template <int nu>   
00077     bool Body3dAvoidController<nu>::SetParams(const Vector5d &s) {
00078     stabCtrl.Kp[0] = s[0];
00079     stabCtrl.Kp[1] = s[0];
00080     stabCtrl.Kp[2] = s[0];
00081     stabCtrl.Kd[0] = s[1];
00082     stabCtrl.Kd[1] = s[1];
00083     stabCtrl.Kd[2] = s[1];
00084     stabCtrl.Kp[3] = s[2];
00085     stabCtrl.Kp[4] = s[2];
00086     stabCtrl.Kp[5] = s[2];
00087     stabCtrl.Kd[3] = s[3];
00088     stabCtrl.Kd[4] = s[3];
00089     stabCtrl.Kd[5] = s[3];
00090     
00091     if (avoidCtrl)
00092       avoidCtrl->k = s[4];
00093 
00094     return true;
00095   }
00096 
00097   template <int nu>   
00098     bool Body3dAvoidController<nu>::SetContext(const Body3dState &c) {
00099     
00100     if (avoidCtrl) { // check for feasibility
00101       Matrix<double, 1, 1> g;
00102       (avoidCtrl->con)(g, 0, c);
00103 
00104       //      cout << "CHecking" << c.second.transpose() << " g=" << g << endl;
00105       
00106       if (g[0] > 0)
00107         return false;
00108     }
00109         
00110     stabCtrl.xd = (Body3dState*)&c;
00111 
00112     return true;
00113   }
00114 
00115 };
00116 
00117 #endif