wam_controller_driver.cpp
Go to the documentation of this file.
00001 #include "wam_controller_driver.h"
00002 
00003 WamControllerDriver::WamControllerDriver(void)
00004 {
00005   //get the robot name to name the links accordingly
00006     ros::NodeHandle nh("~");
00007 
00008     XmlRpc::XmlRpcValue r_name;
00009     if(!nh.hasParam("robot_name"))
00010     {
00011         ROS_WARN_STREAM("Robot name not defined. Default: " << "iri_wam");
00012         robot_name_="iri_wam";
00013     }
00014     else
00015     {
00016         nh.getParam("robot_name", r_name);
00017         robot_name_=(std::string)r_name;
00018     }
00019 }
00020 
00021 //TODO: fer que no calgui esperar els getchar
00022 bool WamControllerDriver::openDriver(void)
00023 {
00024     try{
00025         if(this->state_ != OPENED){
00026             this->wam_ = new wamDriver();
00027             wam_->open();
00028             this->state_ = OPENED;
00029             ROS_INFO("Wam opened, press shift+idle and enter.");
00030             getchar();
00031 
00032             wam_->create();
00033             ROS_INFO("Wam created, press shift+activate and press enter.");
00034             getchar();
00035             wam_->activate();
00036             return true;
00037         }else{
00038             ROS_ERROR("WAM was already opened!");
00039             return false;
00040         }
00041     }catch(const std::exception &e){
00042         //ROS_ERROR("%s",e.what().c_str());
00043         ROS_ERROR("Exception in openDriver");
00044         return false;
00045     }
00046 }
00047 
00048 bool WamControllerDriver::closeDriver(void)
00049 {
00050   wam_->close();
00051   ROS_INFO("[APP] Wait until wam gets home, idle the wam and press enter.");
00052   getchar();
00053   this->state_ = CLOSED;
00054   return true;
00055 }
00056 
00057 bool WamControllerDriver::startDriver(void)
00058 {
00059     try{
00060         wam_->setGravityCompensation(1.1);
00061         ROS_INFO("Switching to Running and going to default position.");
00062         wam_->goToDefaultPosition();
00063         ROS_INFO("Waiting final position reach");
00064         wam_->waitTillMotionDone();
00065         ROS_INFO("All is ready to work now!");
00066         wam_->setGravityCompensation(1.1);
00067         this->state_ = RUNNING;
00068         return true;
00069     }catch(const std::exception &e){
00070         //ROS_ERROR("%s",e.what().c_str());
00071         ROS_ERROR("Exception in startDriver");
00072         return false;
00073     }
00074 
00075   return true;
00076 }
00077 
00078 bool WamControllerDriver::stopDriver(void)
00079 {
00080   wam_->home();
00081   wam_->waitTillMotionDone();
00082   this->state_ = OPENED;
00083   return true;
00084 }
00085 
00086 void WamControllerDriver::config_update(Config& new_cfg, uint32_t level)
00087 {
00088   this->lock();
00089   
00090   // depending on current state
00091   // update driver with new_cfg data
00092   switch(this->getState())
00093   {
00094     case WamControllerDriver::CLOSED:
00095       break;
00096 
00097     case WamControllerDriver::OPENED:
00098       break;
00099 
00100     case WamControllerDriver::RUNNING:
00101       break;
00102   }
00103 
00104   // save the current configuration
00105   this->config_=new_cfg;
00106 
00107   this->unlock();
00108 }
00109 
00110 WamControllerDriver::~WamControllerDriver(void)
00111 {
00112 }
00113 
00114 bool WamControllerDriver::is_joints_move_request_valid(const std::vector<double> & angles){
00115     // Check number of joints sent to the robot
00116     if (static_cast<unsigned int>(angles.size()) != get_num_joints()) {
00117         ROS_ERROR("Invalid request to move. Joint vector size is %d while the robot has %u joints",
00118                 (int)angles.size(), (unsigned int)get_num_joints());
00119         return false;
00120     }
00121 
00122     // Check valid values of those angles
00123     for (std::vector<double>::const_iterator it = angles.begin(); it != angles.end(); it++) {
00124         // Until std::isNan (c++11 feature) reach compilers, we use the Nan propiety
00125         // of not returning true when comparing against itself
00126         if (* it != * it) {
00127             ROS_ERROR("Invalid request to move. Joint vector contain a Nan value.");
00128             return false;
00129         }
00130     }
00131 
00132     return true;
00133 }
00134 
00135 void 
00136 WamControllerDriver::get_pose(std::vector<double> *pose) 
00137 {
00138   if(this->wam_!=NULL)  this->wam_->getCartesianPose(pose);
00139 }
00140 
00141 void 
00142 WamControllerDriver::get_joint_angles(std::vector<double> *angles) 
00143 {
00144     if(this->wam_!=NULL) this->wam_->getJointAngles(angles);
00145 }
00146 
00147 std::string 
00148 WamControllerDriver::get_robot_name() 
00149 {
00150   return this->robot_name_;
00151 }
00152 
00153 //TODO:
00154 bool
00155 WamControllerDriver::is_moving_trajectory()
00156 {
00157   if (this->wam_ != NULL) {
00158     return this->wam_->isMovingTrajectory();
00159   }
00160   return false;
00161 }
00162 
00163 //TODO
00164 bool
00165 WamControllerDriver::is_moving()
00166 {
00167   if (this->wam_ != NULL) {
00168     return this->wam_->isMoving();
00169   }
00170   return false;
00171 }
00172 
00173 //TODO
00174 bool
00175 WamControllerDriver::is_joint_trajectory_result_succeeded()
00176 {
00177    if (this->wam_ != NULL){
00178       /* Not yet implemented */
00179       //return this->wam_->is_joint_trajectory_result_succeeded();
00180       ROS_INFO("joint_trajectory_result_succeeded: TRUE hardcoded");
00181       return true; // Until it 
00182     }
00183     return false;
00184 
00185 }
00186 
00187 // TODO: get the current desired joint trajectory point from the low level driver
00188 trajectory_msgs::JointTrajectoryPoint 
00189 WamControllerDriver::get_desired_joint_trajectory_point()
00190 {
00191   return desired_joint_trajectory_point_;
00192 }
00193 
00194 void 
00195 WamControllerDriver::move_trajectory_in_joints(const trajectory_msgs::JointTrajectory & trajectory)
00196 {
00197   uint16_t errormask = 0x00;
00198   //message with positions, no velocities are present
00199   if (trajectory.points.begin()->velocities.size()==0)
00200   {
00201     WAMPositionsJointTrajectory low_level_trajectory;
00202     std::vector<double> point_trajectory;
00203 
00204 
00205     for (std::vector<trajectory_msgs::JointTrajectoryPoint>::const_iterator it = trajectory.points.begin();
00206             it != trajectory.points.end(); it++)
00207     {
00208         if (! is_joints_move_request_valid(it->positions)) {
00209             ROS_ERROR("Joints angles were not valid. Refuse to move.");
00210             return;
00211         }
00212         point_trajectory.clear();
00213         point_trajectory.insert(point_trajectory.end(),it->positions.begin(), it->positions.end());
00214         low_level_trajectory.push_back(point_trajectory);
00215     }
00216    this->wam_->moveTrajectoryInJoints(&errormask, &low_level_trajectory);
00217 
00218   }
00219   //message with positions, velocities (and accelerations)
00220   else if (trajectory.points.begin()->velocities.size()==7) {
00221     WAMJointTrajectory low_level_trajectory;
00222     WAMTrajectoryPoint point_trajectory;
00223 
00224     for (std::vector<trajectory_msgs::JointTrajectoryPoint>::const_iterator 
00225          it = trajectory.points.begin(); it != trajectory.points.end(); it++) {
00226         if (! is_joints_move_request_valid(it->positions)) {
00227             ROS_ERROR("Joints angles were not valid. Refuse to move.");
00228             return;
00229         }
00230         // Now low_level_trajectory contains angles, velocities, accelerations and time_from_start
00231         point_trajectory.positions.clear();
00232         point_trajectory.velocities.clear();
00233         point_trajectory.accelerations.clear();
00234         point_trajectory.time_from_start.clear();
00235         point_trajectory.positions.insert(point_trajectory.positions.end(), 
00236                                           it->positions.begin(), 
00237                                           it->positions.end());
00238         point_trajectory.velocities.insert(point_trajectory.velocities.end(), 
00239                                            it->velocities.begin(), 
00240                                            it->velocities.end());
00241         point_trajectory.accelerations.insert(point_trajectory.accelerations.end(), 
00242                                               it->accelerations.begin(), 
00243                                               it->accelerations.end());
00244         point_trajectory.time_from_start.push_back(it->time_from_start.sec);
00245         point_trajectory.time_from_start.push_back(it->time_from_start.nsec);
00246         low_level_trajectory.push_back(point_trajectory);
00247         ROS_DEBUG("New point: %f %f %f %f %f %f %f",point_trajectory.positions[0],
00248                                                     point_trajectory.positions[1],
00249                                                     point_trajectory.positions[2],
00250                                                     point_trajectory.positions[3],
00251                                                     point_trajectory.positions[4],
00252                                                     point_trajectory.positions[5],
00253                                                     point_trajectory.positions[6]);
00254     }
00255     this->wam_->moveTrajectoryInJoints(&errormask, &low_level_trajectory);
00256 
00257   }
00258   else
00259     ROS_ERROR("Error in trajectory formatting: invalid velocity vector. Refuse to move.");
00260     
00261 }
00262 
00263 void
00264 WamControllerDriver::stop_trajectory_in_joints(){
00265   ROS_INFO("Cancel Trajectory");
00266   this->wam_->stopTrajectoryInJoints();
00267 }
00268 
00269 void
00270 WamControllerDriver::move_in_joints(std::vector<double> *angles, std::vector<double>* vels, std::vector<double>* accs){
00271     uint16_t errormask = 0x00;
00272 
00273     if (this->wam_!=NULL)
00274     {
00275         if (! this->is_joints_move_request_valid(* angles))
00276         {
00277             ROS_ERROR("Joints angles were not valid. Refuse to move.");
00278             return;
00279         }
00280 
00281         // Check if there are vels and accs
00282         bool blocking(false);
00283         if ((vels == NULL) || (accs == NULL))
00284         {
00285             this->wam_->moveInJoints(angles, blocking);
00286         }
00287         else
00288         {
00289             this->wam_->moveInJoints(angles, blocking, vels, accs);
00290         }
00291         if(errormask > 0x00)
00292         {
00293             ROS_ERROR("%u", errormask);
00294         }
00295     }
00296 }
00297 
00298 void
00299 WamControllerDriver::wait_move_end()
00300 {
00301     if(this->wam_!=NULL)
00302     {
00303         this->wam_->waitTillMotionDone();
00304     }
00305 }
00306 
00307 void
00308 WamControllerDriver::hold_on()
00309 {
00310     if(this->wam_!=NULL)
00311     {
00312         this->wam_->holdOnCurrentPosition();
00313     }
00314 }
00315 
00316 void
00317 WamControllerDriver::hold_off()
00318 {
00319     if(this->wam_!=NULL)
00320     {
00321         this->wam_->holdOffCurrentPosition();
00322     }
00323 }
00324 


iri_wam_controller
Author(s): galenya
autogenerated on Fri Dec 6 2013 20:08:29