GCOP
1.0
|
00001 #ifndef GCOP_BODY3DCONTROLLER_H 00002 #define GCOP_BODY3DCONTROLLER_H 00003 00004 #include "controller.h" 00005 #include "body3d.h" 00006 #include "so3.h" 00007 00008 namespace gcop { 00009 00010 using namespace std; 00011 using namespace Eigen; 00012 00018 template <int c = 6> 00019 class Body3dController : public Controller<Body3dState, Matrix<double, c, 1> > { 00020 public: 00021 typedef Matrix<double, c, 1> Vectorcd; 00022 00030 Body3dController(const Body3d<c> &sys, 00031 Body3dState *xd = 0, 00032 Vector6d *ad = 0); 00033 00034 virtual bool Set(Vectorcd &u, double t, const Body3dState &x); 00035 00036 virtual ~Body3dController(); 00037 00038 const Body3d<c> &sys; 00039 00040 Body3dState *xd; 00041 Vector6d *ad; 00042 00043 Vector6d Kp; 00044 Vector6d Kd; 00045 00046 }; 00047 00048 template <int c> 00049 Body3dController<c>::Body3dController(const Body3d<c> &sys, 00050 Body3dState *xd, 00051 Vector6d *ad) : 00052 sys(sys), xd(xd), ad(ad) 00053 { 00054 Kp << 1, 1, 1, 1, 1, 1; 00055 Kd << 2, 2, 2, 2, 2, 2; 00056 } 00057 00058 template <int c> 00059 Body3dController<c>::~Body3dController() 00060 { 00061 } 00062 00063 template <int c> 00064 bool Body3dController<c>::Body3dController::Set(Vectorcd &u, double t, const Body3dState &x) 00065 { 00066 const Matrix3d& R = x.first; // rotation 00067 00068 Vector3d eR; // error in rotation 00069 Vector3d ew; // error in ang vel (body frame) 00070 Vector3d ex; // error in position 00071 Vector3d ev; // error in lin vel (spatial frame) 00072 00073 if (xd) { 00074 SO3::Instance().log(eR, xd->first.transpose()*R); 00075 ew = x.second.segment<3>(3) - x.first.transpose()*xd->first*xd->second.segment<3>(3); 00076 ex = x.second.head<3>() - xd->second.head<3>(); 00077 ev = x.second.tail<3>() - xd->second.tail<3>(); 00078 } else { 00079 SO3::Instance().log(eR, R); 00080 ew = x.second.segment<3>(3); 00081 ex = x.second.head<3>(); 00082 ev = x.second.tail<3>(); 00083 } 00084 00085 u.head(3) = -Kp.head<3>().cwiseProduct(eR) - Kd.head<3>().cwiseProduct(ew); 00086 u.tail(3) = R.transpose()*(-Kp.tail<3>().cwiseProduct(ex) - Kd.tail<3>().cwiseProduct(ev) - sys.fp); 00087 return true; 00088 } 00089 }; 00090 00091 #endif