SolverNode.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /*
00036   Author: Daniel Hennes
00037  */
00038 
00039 #include "collision_detection/SolverNode.h"
00040  
00041 void SolverNode::lookupJointIDs(const std::vector<std::string>& names) {
00042   uint j, i;
00043   bool found = false;
00044   for (j = 0; j < joint_names.size(); j++) {
00045     found = false;
00046     for (i = 0; i < names.size(); i++)
00047       if (names[i] == joint_names[j]) {
00048         joint_ids.push_back(i);
00049         found = true;
00050         break;
00051       }
00052     if (!found)
00053       break;
00054   }
00055   
00056   if (!found) {
00057     ROS_ERROR("did not find joint %s in joint_state msg.", joint_names[j].c_str());
00058   }
00059 }
00060 
00061 void SolverNode::js_callback(const sensor_msgs::JointState& msg)
00062 {
00063   if (0 == joint_ids.size())
00064     lookupJointIDs(msg.name);
00065 
00066   std::vector<double> pos(ndof);
00067   std::vector<double> vel(ndof);
00068   std::vector<double> acc(ndof);
00069   std::vector<double> effort(ndof);
00070   std::vector<double> computed_effort(ndof);
00071 
00072   double dt = (msg.header.stamp - last_time).toSec();
00073   
00074   for (uint i = 0; i < ndof; i++) {
00075     pos[i] = msg.position[joint_ids[i]]; // pos_filter[i]->getNextFilteredValue(msg.position[joint_ids[i]]);
00076     vel[i] = (pos[i] - last_pos[i]) / dt;
00077     //vel[i] = msg.velocity[joint_ids[i]];
00078 
00079     // acc[i] = (pos[i] - 2.0 * last_pos[i] + sec_last_pos[i]) / (dt * dt); // pos_filter[i]->getNextFilteredValue((vel[i] - last_vel[i]) / dt);
00080     computed_effort[i] = (vel[i] - last_vel[i]) / dt;
00081     acc[i] = acc_filter[i]->getNextFilteredValue((vel[i] - last_vel[i]) / dt ); // pos_filter[i]->getNextFilteredValue((vel[i] - last_vel[i]) / dt);
00082 
00083 
00084     last_pos[i] = pos[i];
00085     last_vel[i] = vel[i];
00086 
00087     // acc[i] = (double) acc_filter->
00088     //   getNextFilteredValue((float) msg.velocity[(vel[i] - last_vel[i]) / dt^2]);
00089 
00090     // vel[i] = (double) vel_filter->
00091     //   getNextFilteredValue((float) msg.velocity[joint_ids[i]]);
00092     // acc[i] = (double) acc_filter->
00093     //   getNextFilteredValue((float) msg.velocity[(vel[i] - last_vel[i]) / dt^2]);
00094 
00095 
00096     effort[i] = eff_filter[i]->getNextFilteredValue(msg.effort[joint_ids[i]]);
00097     
00098 
00099   }
00100   last_time = msg.header.stamp;
00101 
00102   // last_pos = std::vector<double>(ndof);
00103   // std::copy(pos.begin(), pos.end(), last_pos.begin()); 
00104 
00105   // last_vel = std::vector<double>(ndof);
00106   // std::copy(vel.begin(), vel.end(), last_vel.begin()); 
00107 
00108   solver->updateState(pos, vel, acc);
00109   solver->getTorques(computed_effort);
00110 
00111   std_msgs::Float64 sigma_msg;
00112   sigma_msg.data = solver->sigma;
00113   sigma_pub.publish(sigma_msg);
00114 }
00115 
00116 SolverNode::SolverNode(InverseDynamicsSolverWBC& id_solver, 
00117                              std::vector<std::string>& joints) {
00118   solver = &id_solver;
00119   joint_names = joints;
00120   ndof = joint_names.size();
00121   if (ndof != solver->getNumDOF()) {
00122     ROS_ERROR("expected solver with %zu DOF but got %zu", ndof, solver->getNumDOF());
00123     exit(-1);
00124   }
00125 
00126   last_time = ros::Time::now();
00127   last_pos.resize(ndof);
00128   sec_last_pos.resize(ndof);
00129   last_vel.resize(ndof);
00130 
00131   // double filt_pos_b[] = {0.0009,    0.0019,    0.0009};
00132   // double filt_pos_a[] = {1.0000,   -1.8782,    0.8819};
00133   double filt_pos_b[] = {0.0035,    0.0070,    0.0035}; // this one is pretty good
00134   double filt_pos_a[] = {1.0000,   -1.7635,    0.7775};
00135   
00136   pos_filter.resize(ndof);
00137   for (uint i = 0; i < pos_filter.size(); i++) {
00138     pos_filter[i] = new digitalFilter(2, true, &filt_pos_b[0], &filt_pos_a[0]);
00139   }
00140 
00141   double filt_vel_b[] = {0.0035,    0.0070,    0.0035}; // this one is pretty good
00142   double filt_vel_a[] = {1.0000,   -1.7635,    0.7775};
00143   vel_filter.resize(ndof);
00144   for (uint i = 0; i < pos_filter.size(); i++) {
00145     vel_filter[i] = new digitalFilter(2, true, &filt_vel_b[0], &filt_vel_a[0]);
00146   }
00147 
00148 
00149   double filt_acc_b[] = {0.0035,    0.0070,    0.0035}; // this one is pretty good
00150   double filt_acc_a[] = {1.0000,   -1.7635,    0.7775};
00151   acc_filter.resize(ndof);
00152   for (uint i = 0; i < acc_filter.size(); i++) {
00153     acc_filter[i] = new digitalFilter(2, true, filt_acc_b, filt_acc_a);
00154   }
00155 
00156   double filt_eff_b[] = {0.0035,    0.0070,    0.0035}; // this one is pretty good
00157   double filt_eff_a[] = {1.0000,   -1.7635,    0.7775};
00158   eff_filter.resize(ndof);
00159   for (uint i = 0; i < eff_filter.size(); i++) {
00160     eff_filter[i] = new digitalFilter(2, true, filt_eff_b, filt_eff_a);
00161   }
00162 
00163 
00164 
00165   ros::NodeHandle n;
00166   joint_state_pub = 
00167     n.advertise<collision_detection::JointState>("dynamics_joint_states", 1);
00168 
00169   joint_state_sub = 
00170     n.subscribe("/joint_states", 20, &SolverNode::js_callback, this);  
00171 
00172   sigma_pub = 
00173     n.advertise<std_msgs::Float64>("collision_sigma", 1);
00174 }


kelsey_sandbox
Author(s): kelsey
autogenerated on Wed Nov 27 2013 11:52:04