wam_driver.cpp
Go to the documentation of this file.
00001 #include "wam_driver.h"
00002 //#include "wam_packet.h" // from wam low level driver
00003 
00004 using namespace std;
00005 using namespace XmlRpc;
00006 
00007 WamDriver::WamDriver() :
00008     force_request_(new ForceRequest)
00009 {
00010     ros::NodeHandle nh("~");
00011 
00012     XmlRpc::XmlRpcValue r_name;
00013 /*    XmlRpc::XmlRpcValue ip;
00014     XmlRpc::XmlRpcValue port;
00015     XmlRpc::XmlRpcValue rate;
00016 
00017     nh.getParam("wam_ip",ip);
00018     this->wamserver_ip=(std::string)ip;
00019 */
00020     if(!nh.hasParam("robot_name"))
00021     {
00022         ROS_WARN_STREAM("Robot name not defined. Default: " << "iri_wam");
00023         robot_name_="iri_wam";
00024     } 
00025     else
00026     {
00027         nh.getParam("robot_name", r_name);
00028         robot_name_=(std::string)r_name;
00029     }
00030  /*   if(!nh.hasParam("port"))
00031     {
00032         ROS_WARN_STREAM("Port not defined. Defaults: "<<4321);
00033         this->server_port=4321;
00034     } 
00035     else
00036     {
00037         nh.getParam("port",port);
00038         this->server_port=(int)port;
00039     }
00040     if(!nh.hasParam("refresh_rate"))
00041     {
00042         ROS_WARN_STREAM("Refresh Rate not defined. Defaults: "<<100);
00043         this->state_refresh_rate=100;
00044     } 
00045     else
00046     {
00047         nh.getParam("refresh_rate",rate);
00048         this->state_refresh_rate=(int)rate;
00049     }
00050 
00051     ROS_INFO_STREAM("Robot Name: "   << this->robot_name_);
00052     ROS_INFO_STREAM("IP Address: "   << this->wamserver_ip);
00053     ROS_INFO_STREAM("Port Server: "  << this->server_port);
00054     ROS_INFO_STREAM("Refresh Rate: " << this->state_refresh_rate);
00055 */}
00056 
00057 bool WamDriver::openDriver(void)
00058 {
00059     //setDriverId(driver string id);
00060     string input;
00061     //setDriverId(driver string id);
00062     try{
00063         if(this->state_ != OPENED){
00064             //this->wam_ = new wamDriver(this->wamserver_ip, this->server_port, this->state_refresh_rate);
00065             this->wam_ = new wamDriver();
00066             wam_->open();
00067             this->state_ = OPENED;
00068             ROS_INFO("Wam opened, press shift+idle and enter.");
00069             getchar();
00070 
00071             wam_->create(); 
00072             ROS_INFO("Wam created, press shift+activate and press enter.");
00073             getchar(); 
00074             wam_->activate();
00075             return true;
00076         }else{
00077             ROS_ERROR("WAM was already opened!");
00078             return false;
00079         }
00080     }catch(const std::exception &e){
00081         //ROS_ERROR("%s",e.what().c_str());
00082         ROS_ERROR("Exception in openDriver");
00083         return false;
00084     }
00085 }
00086 
00087 bool WamDriver::closeDriver(void)
00088 {
00089     wam_->close();
00090     ROS_INFO("[APP] Wait until wam gets home, idle the wam and press enter.");
00091     getchar();
00092     this->state_ = CLOSED;
00093     return true;
00094 }
00095 
00096 bool WamDriver::startDriver(void)
00097 {
00098     try{
00099         wam_->setGravityCompensation(1.1);
00100         ROS_INFO("Switching to Running and going to default position.");
00101         wam_->goToDefaultPosition();
00102         ROS_INFO("Waiting final position reach");
00103         wam_->waitTillMotionDone();
00104         ROS_INFO("All is ready to work now!");
00105         wam_->setGravityCompensation(1.1);
00106         this->state_ = RUNNING;
00107         return true;
00108     }catch(const std::exception &e){
00109         //ROS_ERROR("%s",e.what().c_str());
00110         ROS_ERROR("Exception in startDriver");
00111         return false;
00112     }
00113     return true;
00114 }
00115 
00116 bool WamDriver::stopDriver(void)
00117 {
00118     //return to home but keep waiting for messages
00119     wam_->home();
00120     wam_->waitTillMotionDone();
00121     this->state_ = OPENED;
00122     return true;
00123 }
00124 
00125 void WamDriver::config_update(const Config& new_cfg, uint32_t level)
00126 {
00127   this->lock();
00128   //update driver with new_cfg data
00129   if(isRunning()){
00130     // save the current configuration
00131     this->config_=new_cfg;
00132   }else{
00133     ROS_ERROR("[WAM_DRIVER] config update while driver is not running yet");
00134   }
00135   this->unlock();
00136 }
00137 
00138 WamDriver::~WamDriver()
00139 {
00140     std::cout << "WamDriver destructor" << std::endl;
00141 }
00142 
00143 std::string WamDriver::get_robot_name() {
00144 
00145     return this->robot_name_;
00146 }
00147 
00148 int WamDriver::get_num_joints() {
00149     if(this->wam_!=NULL) {
00150         return NJOINTS;
00151     } 
00152     return 0;
00153 }
00154 
00155 bool WamDriver::is_moving() {
00156     if (this->wam_ != NULL) {
00157 //        return this->wam_->isMoving();
00158     }
00159     return false;
00160 }
00161 
00162 bool WamDriver::is_joint_trajectory_result_succeeded(){
00163     if (this->wam_ != NULL){
00164       /* Not yet implemented */
00165       //return this->wam_->is_joint_trajectory_result_succeeded();
00166       ROS_INFO("joint_trajectory_result_succeeded: TRUE hardcoded");
00167       return true; // Until it 
00168     }
00169     return false;
00170 }
00171 
00172 void WamDriver::wait_move_end() {
00173     if(this->wam_!=NULL) {
00174         this->wam_->waitTillMotionDone();
00175     } 
00176 }
00177 
00178 void WamDriver::get_pose(std::vector<double> *pose) {
00179     if(this->wam_!=NULL) {
00180         this->wam_->getCartesianPose(pose);
00181     }
00182 }
00183 
00184 void WamDriver::get_joint_angles(std::vector<double> *angles) {
00185     if(this->wam_!=NULL) {
00186         this->wam_->getJointAngles(angles);
00187     } 
00188 }
00189 
00190 bool WamDriver::is_joints_move_request_valid(const std::vector<double> & angles){
00191     // Check number of joints sent to the robot
00192     if (static_cast<signed int>(angles.size()) != get_num_joints()) {
00193         ROS_ERROR("Invalid request to move. Joint vector size is %i while the robot has %i joints", 
00194                 (int) angles.size(), get_num_joints());
00195         return false;
00196     }
00197 
00198     // Check valid values of those angles
00199     for (std::vector<double>::const_iterator it = angles.begin(); it != angles.end(); it++) {
00200         // Until std::isNan (c++11 feature) reach compilers, we use the Nan propiety
00201         // of not returning true when comparing against itself
00202         if (* it != * it) {
00203             ROS_ERROR("Invalid request to move. Joint vector contain a Nan value.");
00204             return false;
00205         }
00206     }
00207 
00208     return true;
00209 }
00210 
00211 void WamDriver::move_in_joints(std::vector<double> *angles, std::vector<double>* vels, std::vector<double>* accs){
00212     uint16_t errormask = 0x00;
00213 
00214     if (this->wam_!=NULL) {
00215         if (! is_joints_move_request_valid(* angles)) {
00216             ROS_ERROR("Joints angles were not valid. Refuse to move.");
00217             return;
00218         }
00219 
00220         // Check if there are vels and accs
00221         if ((vels == NULL) || (accs == NULL)) {
00222             wam_->moveInJoints(&errormask, angles);
00223         }
00224         else {
00225             wam_->moveInJoints(&errormask, angles, vels, accs);
00226         }
00227         if(errormask > 0x00){
00228 //            string err_msg = wam_->errorToString(errormask);
00229 //            ROS_ERROR("%s",err_msg.c_str());
00230             errormask = 0x00;
00231         }
00232     }
00233 }
00234 
00235 void
00236 WamDriver::move_in_cartesian_pose(const geometry_msgs::Pose pose,const double vel,const double acc)
00237 {
00238     if (this->wam_ == NULL)
00239         return;
00240 
00241     std::vector<double> low_level_pose;
00242     low_level_pose.push_back(pose.position.x);
00243     low_level_pose.push_back(pose.position.y);
00244     low_level_pose.push_back(pose.position.z);
00245     low_level_pose.push_back(pose.orientation.x);
00246     low_level_pose.push_back(pose.orientation.y);
00247     low_level_pose.push_back(pose.orientation.z);
00248     low_level_pose.push_back(pose.orientation.w);
00249 
00250 //    this->wam_->moveInCartesianPose(&low_level_pose, vel, acc);
00251 }
00252 
00253 void
00254 WamDriver::hold_current_position(bool on)
00255 {
00256   if(this->wam_!=NULL){
00257     this->wam_->holdCurrentPosition(on);
00258   }
00259 }
00260 
00261 trajectory_msgs::JointTrajectoryPoint WamDriver::get_desired_joint_trajectory_point()
00262 {
00263     // TODO: get the current desired joint trajectory point from the low level driver
00264     return desired_joint_trajectory_point_;
00265 }
00266 
00267 void
00268 WamDriver::move_trajectory_in_joints(const trajectory_msgs::JointTrajectory & trajectory)
00269 {
00270 /*    uint16_t errormask = 0x00;
00271     WAMJointTrajectory low_level_trajectory;
00272     WAMTrajectoryPoint point_trajectory;
00273 
00274     for (std::vector<trajectory_msgs::JointTrajectoryPoint>::const_iterator it = trajectory.points.begin();
00275             it != trajectory.points.end(); it++) {
00276         if (! is_joints_move_request_valid(it->positions)) {
00277             ROS_ERROR("Joints angles were not valid. Refuse to move.");
00278             return;
00279         }
00280         // Now low_level_trajectory contains angles, velocities, accelerations and time_from_start
00281         point_trajectory.positions.clear();
00282         point_trajectory.velocities.clear();
00283         point_trajectory.accelerations.clear();
00284         point_trajectory.time_from_start.clear();
00285         point_trajectory.positions.insert(point_trajectory.positions.end(), it->positions.begin(), it->positions.end());
00286         point_trajectory.velocities.insert(point_trajectory.velocities.end(), it->velocities.begin(), it->velocities.end());
00287         point_trajectory.accelerations.insert(point_trajectory.accelerations.end(), it->accelerations.begin(), it->accelerations.end());
00288         point_trajectory.time_from_start.push_back(it->time_from_start.sec);
00289         point_trajectory.time_from_start.push_back(it->time_from_start.nsec);
00290         low_level_trajectory.push_back(point_trajectory);
00291 ROS_INFO("New point: %f %f %f %f %f %f %f",point_trajectory.positions[0],
00292 point_trajectory.positions[1],
00293 point_trajectory.positions[2],
00294 point_trajectory.positions[3],
00295 point_trajectory.positions[4],
00296 point_trajectory.positions[5],
00297 point_trajectory.positions[6]);
00298     }
00299     this->wam_->moveTrajectoryInJoints(&errormask, &low_level_trajectory);
00300 */
00301 
00302     uint16_t errormask = 0x00;
00303     WAMPositionsJointTrajectory low_level_trajectory;
00304     std::vector<double> point_trajectory;  
00305 ROS_INFO("Trajectory has no velocity data, %d", trajectory.points.begin()->velocities.size());
00306 if (trajectory.points.begin()->velocities.size()!=7) 
00307 {
00308 
00309     for (std::vector<trajectory_msgs::JointTrajectoryPoint>::const_iterator it = trajectory.points.begin();
00310             it != trajectory.points.end(); it++) 
00311     {
00312         if (! is_joints_move_request_valid(it->positions)) {
00313             ROS_ERROR("Joints angles were not valid. Refuse to move.");
00314             return;
00315         }
00316         point_trajectory.clear();    
00317         point_trajectory.insert(point_trajectory.end(),it->positions.begin(), it->positions.end());
00318         low_level_trajectory.push_back(point_trajectory);
00319     }
00320    this->wam_->moveTrajectoryInJoints(&errormask, &low_level_trajectory);
00321 }
00322 }
00323 
00324 void
00325 WamDriver::stop_trajectory_in_joints()
00326 {
00327 //    this->wam_->cancelTrajectoryInJoints();
00328 }
00329 
00330 void
00331 WamDriver::move_trajectory_learnt_and_estimate_force(const std::string model_filename,
00332                                                      const std::string points_filename)
00333 {
00334 /*    // TODO: implement error handling
00335     force_request_->init();
00336     double response = this->wam_->moveTrajectoryLearntAndEstimateForce(model_filename, points_filename);
00337     force_request_->success_response(response);
00338 */
00339     return;
00340 }
00341 
00342 void
00343 WamDriver::start_dmp_tracker(const std::vector<double> * initial, const std::vector<double> * goal)
00344 {
00345 /*    uint16_t errormask = 0x00;
00346 
00347     if (this->wam_!=NULL) {
00348         if (! is_joints_move_request_valid(* initial)) {
00349             ROS_ERROR("Initial joints angles were not valid. Refuse to move.");
00350             return;
00351         }
00352         if (! is_joints_move_request_valid(* goal)) {
00353             ROS_ERROR("Goal joints angles were not valid. Refuse to move.");
00354             return;
00355         }
00356 std::vector<double> nc_initial, nc_goal;
00357 nc_initial= *initial;
00358 nc_goal = *goal;
00359         ROS_DEBUG("Send values %f %f %f %f %f %f %f",nc_initial[0],nc_initial[0],nc_initial[0],nc_initial[0],nc_initial[0],nc_initial[0],nc_initial[0]);
00360         ROS_DEBUG("Send values %f %f %f %f %f %f %f",nc_goal[0],nc_goal[0],nc_goal[0],nc_goal[0],nc_goal[0],nc_goal[0],nc_goal[0]);
00361         this->wam_->trackGoalDMP(&errormask, &nc_initial, &nc_goal);
00362        
00363         if(errormask > 0x00){
00364             string err_msg = wam_->errorToString(errormask);
00365             ROS_ERROR("%s",err_msg.c_str());
00366             errormask = 0x00;
00367         }
00368    }
00369   /*
00370   uint16_t errormask = 0x00;
00371     std::vector<double> low_level_initial, low_level_goal;    
00372     for (std::vector<trajectory_msgs::JointTrajectoryPoint>::const_iterator it = initial.positions.begin();
00373          it != initial.positions.end(); it++) {
00374         low_level_initial.push_back(it->positions);
00375     }
00376     for (std::vector<trajectory_msgs::JointTrajectoryPoint>::const_iterator it = goal.positions.begin();
00377          it != goal.positions.end(); it++) {
00378         low_level_goal.push_back(it->positions);
00379     }
00380     */
00381  }
00382  
00383 void 
00384 WamDriver::dmp_tracker_new_goal(const std::vector<double> * new_goal)
00385 {
00386 /*    uint16_t errormask = 0x00;
00387 
00388     if (this->wam_!=NULL) { 
00389         if (! is_joints_move_request_valid(* new_goal)) {
00390             ROS_ERROR("Initial joints angles were not valid. Refuse to move.");
00391             return;
00392         }
00393         
00394         std::vector<double> nc_new_goal;
00395         nc_new_goal = *new_goal;
00396 
00397         this->wam_->trackGoalDMPNewGoal(&errormask, &nc_new_goal);
00398         
00399         if(errormask > 0x00){
00400             string err_msg = wam_->errorToString(errormask);
00401             ROS_ERROR("%s",err_msg.c_str());
00402             errormask = 0x00;
00403         }
00404     }
00405 */ }
00406 
00407 void
00408 WamDriver::jnt_pos_cmd_callback(const std::vector<float> * joints,
00409                                 const std::vector<float> * rate_limits)
00410 {
00411   //TODO: check timeout!
00412   //if (last_jnt_pos_msg_time + rt_msg_timeout > ros::Time::now()) // checking if a joint position message has been published and if it is within timeout
00413   //TODO: implement this function to evaluate RT loop providing new commands from
00414   // ROS control loop  as alternative to pass a complete list of points
00415   //this->wam_->jntPosNewCmd(joints, rate_limits);
00416 }


iri_wam_wrapper
Author(s): Pol Monso
autogenerated on Fri Dec 6 2013 21:47:20