00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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 }