tutorials/tut06_eepos.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 The Board of Trustees of The Leland Stanford Junior University. All rights reserved.
00003  *
00004  * Author: Roland Philippsen
00005  *         http://cs.stanford.edu/group/manips/
00006  *
00007  * This program is free software: you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation, either version 3 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this program.  If not, see
00019  * <http://www.gnu.org/licenses/>
00020  */
00021 
00050 #include "tutsim.hpp"
00051 #include <opspace/task_library.hpp>
00052 #include <opspace/skill_library.hpp>
00053 #include <opspace/ClassicTaskPostureController.hpp>
00054 #include <jspace/test/sai_util.hpp>
00055 #include <boost/shared_ptr.hpp>
00056 #include <FL/fl_draw.H>
00057 #include <err.h>
00058 
00059 
00060 static std::string model_filename(TUTROB_XML_PATH_STR);
00061 static boost::shared_ptr<jspace::Model> model;
00062 static boost::shared_ptr<opspace::ClassicTaskPostureController> controller;
00063 static boost::shared_ptr<opspace::GenericSkill> skill;
00064 static boost::shared_ptr<opspace::CartPosTask> eetask;
00065 static boost::shared_ptr<opspace::JPosTask> jtask;
00066 static opspace::Parameter * eegoalpos;
00067 static opspace::Parameter * eegoalvel;
00068 static opspace::Parameter * jgoalpos;
00069 static opspace::Parameter * jgoalvel;
00070 static size_t mode;
00071 
00072 
00073 static bool servo_cb(size_t toggle_count,
00074          double wall_time_ms,
00075          double sim_time_ms,
00076          jspace::State & state,
00077          jspace::Vector & command)
00078 {
00079   mode = toggle_count % 5;
00080   static size_t prevmode(42);
00081   static size_t iteration(0);
00082   
00083   jspace::Vector jpos(state.position_.rows());
00084   jspace::Vector jvel(state.velocity_.rows());
00085   for (int ii(0); ii < state.position_.rows(); ++ii) {
00086     static double const amplitude(0.5 * M_PI);
00087     double const omega(1.0 + 0.1 * ii);
00088     double const phase(omega * 1e-3 * wall_time_ms);
00089     jpos[ii] =         amplitude * sin(phase);
00090     jvel[ii] = omega * amplitude * cos(phase);
00091   }
00092   
00093   if (0 == mode) {
00094     
00096     // Re-initialize simulator
00097     
00098     state.position_ = jpos;
00099     state.velocity_ = jvel;
00100     
00101     prevmode = mode;
00102     return false;
00103     
00104   }
00105   
00107   // Update the model to reflect the current robot state.
00108   
00109   model->update(state);
00110   
00111   if (1 != prevmode) {
00112     jspace::Status st(skill->init(*model));
00113     if ( ! st) {
00114       errx(EXIT_FAILURE, "skill->init() failed: %s", st.errstr.c_str());
00115     }
00116     st = controller->init(*model);
00117     if ( ! st) {
00118       errx(EXIT_FAILURE, "controller->init() failed: %s", st.errstr.c_str());
00119     }
00120   }
00121   
00122   if ((3 == mode) || (4 == mode)) {
00123     static jspace::Vector pos(3), vel(3);
00124     static double const oy(0.2);
00125     static double const oz(0.37);
00126     static double const amp(2.5);
00127     double const py(oy * 1e-3 * wall_time_ms);
00128     double const pz(oz * 1e-3 * wall_time_ms);
00129     pos << 0.0,      amp * sin(py),      amp * sin(pz);
00130     vel << 0.0, oy * amp * cos(py), oz * amp * cos(pz);
00131     if ( ! eegoalpos->set(pos)) {
00132       errx(EXIT_FAILURE, "failed to set end-effector goal position");
00133     }
00134     if ( ! eegoalvel->set(vel)) {
00135       errx(EXIT_FAILURE, "failed to set end-effector goal velocity");
00136     }
00137   }
00138   
00139   if ((2 == mode) || (4 == mode)) {
00140     if ( ! jgoalpos->set(jpos)) {
00141       errx(EXIT_FAILURE, "failed to set joint goal position");
00142     }
00143     if ( ! jgoalvel->set(jvel)) {
00144       errx(EXIT_FAILURE, "failed to set joint goal velocity");
00145     }
00146   }
00147   
00148   if ( ! skill->update(*model)) {
00149     errx(EXIT_FAILURE, "skill update failed");
00150   }
00151   
00152   if ( ! controller->computeCommand(*model, *skill, command)) {
00153     errx(EXIT_FAILURE, "controller update failed");
00154   }
00155   
00156   if (0 == (iteration % 100)) {
00157     controller->dbg(std::cerr, "**************************************************", "");
00158   }
00159   
00160   ++iteration;
00161   prevmode = mode;
00162   
00163   return true;
00164 }
00165 
00166 
00167 static void draw_cb(double x0, double y0, double scale)
00168 {
00169   if (0 != mode) {
00170     
00171     tutsim::draw_delta_jpos(*jgoalpos->getVector(), 1, 100, 80, 80, x0, y0, scale);
00172     
00174     // Remember: we plot the YZ plane, X is sticking out of the screen
00175     // but the robot is planar anyway.
00176     
00177     fl_color(255, 100, 100);
00178     fl_line_style(FL_SOLID, 1, 0);
00179     
00180     double const gx(eegoalpos->getVector()->y());
00181     double const gy(eegoalpos->getVector()->z());
00182     int const rr(ceil(0.15 * scale));
00183     int const dd(2 * rr);
00184     fl_arc(int(x0 + gx * scale) - rr, int(y0 - gy * scale) - rr, dd, dd, 0.0, 360.0);
00185     
00186     double const vx(eegoalvel->getVector()->y());
00187     double const vy(eegoalvel->getVector()->z());
00188     double const px(gx + vx * 0.2);
00189     double const py(gy + vy * 0.2);
00190     fl_line(x0 + (gx + 0.2) * scale, y0 - gy * scale,
00191       x0 + (gx - 0.2) * scale, y0 - gy * scale);
00192     fl_line(x0 + gx * scale, y0 - (gy + 0.2) * scale,
00193       x0 + gx * scale, y0 - (gy - 0.2) * scale);
00194     fl_color(255, 255, 100);
00195     fl_line(x0 + gx * scale, y0 - gy * scale,
00196       x0 + px * scale, y0 - py * scale);
00197   }
00198 }
00199 
00200 
00201 int main(int argc, char ** argv)
00202 {
00203   try {
00204     
00205     model.reset(jspace::test::parse_sai_xml_file(model_filename, true));
00206     
00207     eetask.reset(new opspace::CartPosTask("tut06-eepos"));
00208     jspace::Vector kp(1), kd(1), maxvel(1), ctrlpt(3);
00209     kp << 400.0;
00210     kd << 40.0;
00211     maxvel << 1.0;
00212     ctrlpt << 0.0, 0.0, -1.0;
00213     eetask->quickSetup(kp, kd, maxvel, "link4", ctrlpt);
00214     eegoalpos = eetask->lookupParameter("goalpos", opspace::PARAMETER_TYPE_VECTOR);
00215     if ( ! eegoalpos) {
00216       errx(EXIT_FAILURE, "failed to find appropriate end-effector goalpos parameter");
00217     }
00218     eegoalvel = eetask->lookupParameter("goalvel", opspace::PARAMETER_TYPE_VECTOR);
00219     if ( ! eegoalvel) {
00220       errx(EXIT_FAILURE, "failed to find appropriate end-effector goalvel parameter");
00221     }
00222     
00223     jtask.reset(new opspace::JPosTask("tut06-jtask"));
00224     kp << 100.0;
00225     kd << 20.0;
00226     maxvel << M_PI;
00227     jtask->quickSetup(kp, kd, maxvel);
00228     jgoalpos = jtask->lookupParameter("goalpos", opspace::PARAMETER_TYPE_VECTOR);
00229     if ( ! jgoalpos) {
00230       errx(EXIT_FAILURE, "failed to find appropriate joint-posture goalpos parameter");
00231     }
00232     jgoalvel = jtask->lookupParameter("goalvel", opspace::PARAMETER_TYPE_VECTOR);
00233     if ( ! jgoalvel) {
00234       errx(EXIT_FAILURE, "failed to find appropriate joint-posture goalvel parameter");
00235     }
00236 
00237     skill.reset(new opspace::GenericSkill("tut06-skill"));
00238     skill->appendTask(eetask);
00239     skill->appendTask(jtask);
00240 
00241     controller.reset(new opspace::ClassicTaskPostureController("tut06-ctrl"));
00242     
00243   }
00244   catch (std::runtime_error const & ee) {
00245     errx(EXIT_FAILURE, "%s", ee.what());
00246   }
00247   tutsim::set_draw_cb(draw_cb);
00248   return tutsim::run(servo_cb);
00249 }

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