jspace/jspace/controller_library.cpp

Go to the documentation of this file.
00001 /*
00002  * Stanford Whole-Body Control Framework http://stanford-wbc.sourceforge.net/
00003  *
00004  * Copyright (C) 2010 The Board of Trustees of The Leland Stanford Junior University. All rights reserved.
00005  *
00006  * This program is free software: you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation, either version 3 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this program.  If not, see
00018  * <http://www.gnu.org/licenses/>
00019  */
00020 
00026 #include "controller_library.hpp"
00027 #include "Model.hpp"
00028 #include "strutil.hpp"
00029 
00030 
00031 namespace jspace {
00032   
00033   
00034   Status FloatController::
00035   setGoal(Vector const & goal)
00036   {
00037     Status ok;
00038     return ok;
00039   }
00040   
00041   
00042   Status FloatController::
00043   getGoal(Vector & goal) const
00044   {
00045     goal.resize(0);
00046     Status ok;
00047     return ok;
00048   }
00049   
00050   
00051   Status FloatController::
00052   getActual(Vector & actual) const
00053   {
00054     actual.resize(0);
00055     Status ok;
00056     return ok;
00057   }
00058   
00059   
00060   Status FloatController::
00061   setGains(Vector const & kp, Vector const & kd)
00062   {
00063     Status ok;
00064     return ok;
00065   }
00066   
00067   
00068   Status FloatController::
00069   getGains(Vector & kp, Vector & kd) const
00070   {
00071     kp.resize(0);
00072     kd.resize(0);
00073     Status ok;
00074     return ok;
00075   }
00076   
00077   
00078   Status FloatController::
00079   latch(Model const & model)
00080   {
00081     Status ok;
00082     return ok;
00083   }
00084   
00085   
00086   Status FloatController::
00087   computeCommand(Model const & model, Vector & tau)
00088   {
00089     Vector gg;
00090     model.getGravity(gg);
00091     tau.resize(gg.rows());
00092     memcpy(&tau[0], gg.data(), gg.rows() * sizeof(double));
00093     Status ok;
00094     return ok;
00095   }
00096   
00097   
00098   GoalControllerBase::
00099   GoalControllerBase(int compensation_flags,
00100          Vector const & default_kp,
00101          Vector const & default_kd)
00102     : compensation_flags_(compensation_flags),
00103       default_kp_(default_kp),
00104       default_kd_(default_kd)
00105   {
00106   }
00107   
00108   
00109   Status GoalControllerBase::
00110   init(Model const & model)
00111   {
00112     Status status;
00113     ssize_t const ndof(model.getNDOF());
00114     
00115     if (model.getState().position_.size() != ndof) {
00116       status.ok = false;
00117       status.errstr =
00118   "inconsistent model: ndof = " + sfl::to_string(ndof)
00119   + " but state.size() = " + sfl::to_string(model.getState().position_.size());
00120       return status;
00121     }
00122 
00123     // If this is the first time we got called, then initialize gains.
00124     if (ndof != goal_.size()) {
00125       if ((ndof != default_kp_.size()) || (ndof != default_kd_.size())) {
00126   status.ok = false;
00127   status.errstr =
00128     "inconsistent default gains: ndof = " + sfl::to_string(ndof)
00129     + " but default_kp_.size() = " + sfl::to_string(default_kp_.size())
00130     + " and default_kd_.size() = " + sfl::to_string(default_kd_.size());
00131   return status;
00132       }
00133       kp_ = default_kp_;
00134       kd_ = default_kd_;
00135     }
00136     
00137     // Set goal to current position.
00138     goal_ = model.getState().position_;
00139     
00140     return status;
00141   }
00142   
00143   
00144   Status GoalControllerBase::
00145   setGoal(Vector const & goal)
00146   {
00147     Status status;
00148     if (goal.size() != goal_.size()) {
00149       status.ok = false;
00150       status.errstr =
00151   "goal size mismatch: expected " + sfl::to_string(goal_.size())
00152   + " but got " + sfl::to_string(goal.size());
00153       return status;
00154     }
00155     goal_ = goal;
00156     return status;
00157   }
00158   
00159   
00160   Status GoalControllerBase::
00161   getGoal(Vector & goal) const
00162   {
00163     goal = goal_;
00164     Status ok;
00165     return ok;
00166   }
00167   
00168   
00169   Status GoalControllerBase::
00170   setGains(Vector const & kp, Vector const & kd)
00171   {
00172     Status status;
00173     if ((kp.size() != kp_.size()) || (kd.size() != kd_.size())) {
00174       status.ok = false;
00175       status.errstr = "gain size mismatch (maybe not initialized?)";
00176       return status;
00177     }
00178     kp_ = kp;
00179     kd_ = kd;
00180     return status;
00181   }
00182   
00183   
00184   Status GoalControllerBase::
00185   getGains(Vector & kp, Vector & kd) const
00186   {
00187     kp = kp_;
00188     kd = kd_;
00189     Status ok;
00190     return ok;
00191   }
00192   
00193   
00194   JointGoalController::
00195   JointGoalController(int compensation_flags,
00196           Vector const & default_kp,
00197           Vector const & default_kd)
00198     : GoalControllerBase(compensation_flags, default_kp, default_kd)
00199   {
00200   }
00201   
00202   
00203   Status JointGoalController::
00204   getActual(Vector & actual) const
00205   {
00206     actual = actual_;
00207     Status ok;
00208     return ok;
00209   }
00210   
00211   
00212   Status JointGoalController::
00213   latch(Model const & model)
00214   {
00215     goal_ = model.getState().position_;
00216     Status ok;
00217     return ok;
00218   }
00219   
00220   
00221   Status JointGoalController::
00222   computeCommand(Model const & model, Vector & tau)
00223   {
00224     ssize_t const ndof(model.getNDOF());
00225     Status status;
00226     if (ndof != goal_.size()) {
00227       status.ok = false;
00228       status.errstr = "ndof mismatch";
00229       return status;
00230     }
00231     
00232     State const & state(model.getState());
00233     actual_ = state.position_;
00234 
00235     Vector etau(ndof);
00236     for (ssize_t ii(0); ii < ndof; ++ii) {
00237       etau[ii] = - kp_[ii] * (actual_[ii] - goal_[ii]) - kd_[ii] * state.velocity_[ii];
00238     }
00239     
00240     if (compensation_flags_ & COMP_MASS_INERTIA) {
00241       Matrix AA;
00242       if ( ! model.getMassInertia(AA)) {
00243   status.ok = false;
00244   status.errstr = "model.getMassInertia() failed";
00245   return status;
00246       }
00247       etau = AA * etau;
00248     }
00249     
00250     if (compensation_flags_ & COMP_CORIOLIS) {
00251       Vector BB;
00252       if ( ! model.getCoriolisCentrifugal(BB)) {
00253   status.ok = false;
00254   status.errstr = "model.getCoriolisCentrifugal() failed";
00255   return status;
00256       }
00257       etau += BB;
00258     }
00259     
00260     if (compensation_flags_ & COMP_GRAVITY) {
00261       Vector GG;
00262       if ( ! model.getGravity(GG)) {
00263   status.ok = false;
00264   status.errstr = "model.getGravity() failed";
00265   return status;
00266       }
00267       etau += GG;
00268     }
00269     
00270     tau.resize(etau.rows());
00271     memcpy(&tau[0], etau.data(), etau.rows() * sizeof(double));
00272     
00273     return status;
00274   }
00275   
00276 }

Generated on Fri Aug 26 01:31:16 2011 for Stanford Whole-Body Control Framework by  doxygen 1.5.4