wam_test.cpp
Go to the documentation of this file.
00001 #include "wam_test.h"
00002 
00003 /* wam_tutorial node code */
00004 
00005 WamTest::WamTest() {
00006   //init class attributes if necessary
00007   this->gocenter = true;
00008   this->targetid = 0;
00009 
00010   this->numtargets = 10;
00011   this->tfMessage_msg.transforms.resize(numtargets+2);
00012   this->angles.resize(5);
00013   this->angles[0] = 0.0;
00014   this->angles[1] = M_PI/4.0;
00015   this->angles[2] = 3.0*M_PI/4.0;
00016   this->angles[3] = 9.0/8.0*M_PI;
00017   this->angles[4] = 13.0/8.0*M_PI;
00018   //string for port names
00019   std::string port_name;
00020 
00021   // [init publishers]
00022   port_name = ros::names::append(ros::this_node::getName(), "tf"); 
00023   this->tf_publisher = this->nh_.advertise<tf::tfMessage>(port_name, 5);
00024   port_name = ros::names::append(ros::this_node::getName(), "pose_matrix"); 
00025   this->tf_publisher = this->nh_.advertise<tf::tfMessage>(port_name, 5);
00026   
00027   // [init subscribers]
00028   port_name = ros::names::append(ros::this_node::getName(), "pose"); 
00029   this->pose_subscriber = this->nh_.subscribe(port_name, 5, &WamTest::pose_callback, this);
00030   port_name = ros::names::append(ros::this_node::getName(), "joint_states"); 
00031   this->joint_states_subscriber = this->nh_.subscribe(port_name, 5, &WamTest::joint_states_callback, this);
00032   
00033   // [init services]
00034   
00035   // [init clients]
00036   port_name = ros::names::append(ros::this_node::getName(), "pose_move2"); 
00037   pose_move2_client = this->nh_.serviceClient<iri_wam_common_msgs::pose_move>(port_name);
00038 
00039   port_name = ros::names::append(ros::this_node::getName(), "pose_move"); 
00040   pose_move_client = this->nh_.serviceClient<iri_wam_common_msgs::pose_move>(port_name);
00041   port_name = ros::names::append(ros::this_node::getName(), "joints_move"); 
00042   joint_move_client = this->nh_.serviceClient<iri_wam_common_msgs::joints_move>(port_name);
00043   
00044   // [init action servers]
00045   
00046   // [init action clients]
00047 }
00048 
00049 void WamTest::mainLoop(void)
00050 {
00051   //lock access to driver if necessary
00052   //this->driver_.lock();
00053   tf::StampedTransform transform;
00054   geometry_msgs::TransformStamped msg;
00055   std::ostringstream oss;
00056   std::ostringstream oss2;
00057   std::ostringstream oss3;
00058   std::string targetname("target");
00059   // [fill msg Header if necessary]
00060   //<publisher_name>.header.stamp = ros::Time::now();
00061   //<publisher_name>.header.frame_id = "<publisher_topic_name>";
00062 
00063   // [fill msg structures]
00064   //this->tfMessage_msg.data = my_var;
00065   
00066 
00067   //joint_move_srv.request.resize(7);
00068   //joint_move_srv.request.joints[0] = 0.0; 
00069   //joint_move_srv.request.joints[1] = 0.0; 
00070   //joint_move_srv.request.joints[2] = 0.2; 
00071   //joint_move_srv.request.joints[3] = 0.0; 
00072   //joint_move_srv.request.joints[4] = 0.0; 
00073   //joint_move_srv.request.joints[5] = 0.0; 
00074   //joint_move_srv.request.joints[6] = 0.0; 
00075   //if (joint_move_client.call(joint_move_srv)) { 
00076   //  ROS_INFO(joint_move_srv.response.success);
00077   //} else { 
00078   //  ROS_ERROR("Failed to call service joint_move"); 
00079   //}
00080   
00081   // [fill action structure and make request to the action server]
00082 
00083   // [publish messages]
00084   this->tfMessage_msg.transforms[0].header.stamp = ros::Time::now();
00085   this->tfMessage_msg.transforms[0].header.frame_id = "wam0";
00086   this->tfMessage_msg.transforms[0].child_frame_id = "central";
00087   this->tfMessage_msg.transforms[0].transform.rotation = tf::createQuaternionMsgFromRollPitchYaw(0,0,0); 
00088 
00089   this->tfMessage_msg.transforms[0].transform.translation.x = 0.55; 
00090   this->tfMessage_msg.transforms[0].transform.translation.y = 0.2; 
00091   this->tfMessage_msg.transforms[0].transform.translation.z = 0.1; 
00092 
00093   this->tfMessage_msg.transforms[1].header.stamp = ros::Time::now();
00094   this->tfMessage_msg.transforms[1].header.frame_id = "central";
00095   this->tfMessage_msg.transforms[1].child_frame_id = "target0b";
00096   this->tfMessage_msg.transforms[1].transform.rotation = tf::createQuaternionMsgFromRollPitchYaw(0,0,0); 
00097 
00098   this->tfMessage_msg.transforms[1].transform.translation.x = 0.0; 
00099   this->tfMessage_msg.transforms[1].transform.translation.y = 0.0; 
00100   this->tfMessage_msg.transforms[1].transform.translation.z = 0.3; 
00101 
00102   for(uint i=2;i<tfMessage_msg.transforms.size();i+=2){
00103     this->tfMessage_msg.transforms[i].header.stamp = ros::Time::now();
00104     this->tfMessage_msg.transforms[i].header.frame_id = "central";
00105     std::ostringstream oss;
00106     oss << targetname << i/2;
00107     this->tfMessage_msg.transforms[i].child_frame_id = oss.str();
00108     this->tfMessage_msg.transforms[i].transform.rotation = tf::createQuaternionMsgFromRollPitchYaw(angles[(i+1)/2%5],0,0); 
00109     this->tfMessage_msg.transforms[i].transform.translation.x = 0.0; 
00110     this->tfMessage_msg.transforms[i].transform.translation.y = 0.0; 
00111     this->tfMessage_msg.transforms[i].transform.translation.z = 0.0; 
00112 
00113     this->tfMessage_msg.transforms[i+1].header.stamp = ros::Time::now();
00114     this->tfMessage_msg.transforms[i+1].header.frame_id = oss.str();
00115     oss << "b";
00116     this->tfMessage_msg.transforms[i+1].child_frame_id = oss.str();
00117     this->tfMessage_msg.transforms[i+1].transform.rotation = tf::createQuaternionMsgFromRollPitchYaw(0,0,0); 
00118 
00119     this->tfMessage_msg.transforms[i+1].transform.translation.x = 0.0; 
00120     this->tfMessage_msg.transforms[i+1].transform.translation.y = 0.0; 
00121     this->tfMessage_msg.transforms[i+1].transform.translation.z = 0.3; 
00122   } 
00123 
00124   tf::tfMessage tfMessage_msgaux(this->tfMessage_msg);
00125   this->tf_publisher.publish(tfMessage_msgaux);
00126 
00127   oss3 << targetname << targetid << "b";
00128   calculate_error(oss3.str());
00129 
00130     if(gocenter){
00131       gocenter = false;
00132       ROS_INFO("Going to central");
00133       try{
00134         listener.lookupTransform("wam0", "central", ros::Time(0), transform);
00135         tf::transformStampedTFToMsg(transform,msg);
00136         //Sembla increible, pero els valors estan desplaçats...
00138         pose_move_srv.request.pose.orientation.y = msg.transform.rotation.x;
00139         pose_move_srv.request.pose.orientation.z = msg.transform.rotation.y;
00140         pose_move_srv.request.pose.orientation.w = msg.transform.rotation.z;
00141         pose_move_srv.request.pose.orientation.x = msg.transform.rotation.w;
00142         
00143         pose_move_srv.request.pose.position.x = msg.transform.translation.x;
00144         pose_move_srv.request.pose.position.y = msg.transform.translation.y;
00145         pose_move_srv.request.pose.position.z = msg.transform.translation.z;
00146         std::cout << "moving to:" << msg << std::endl;
00147         sleep(1);
00148         //kaijen ik
00149         if (pose_move2_client.call(pose_move_srv)) { 
00150           ROS_INFO("pose_move2: %d", pose_move_srv.response.success); 
00151           calculate_error("central");
00152         } else { 
00153           ROS_ERROR("Failed to call service pose_move2"); 
00154         }
00155       } catch (tf::TransformException ex){
00156         ROS_ERROR("%s",ex.what());
00157       } 
00158     }else{ 
00159       gocenter = true;
00160       targetid++;
00161       targetid = targetid%6;
00162       oss2 << targetname << targetid << "b";
00163       ROS_INFO("Going to %s",oss2.str().c_str());
00164       try{
00165         listener.lookupTransform("wam0", oss2.str(), ros::Time(0), transform);
00166 
00167 //change this with http://www.ros.org/wiki/tf/Overview/Using%20Published%20Transforms
00168 
00169         tf::transformStampedTFToMsg(transform,msg);
00170         pose_move_srv.request.pose.orientation.y = msg.transform.rotation.x;
00171         pose_move_srv.request.pose.orientation.z = msg.transform.rotation.y;
00172         pose_move_srv.request.pose.orientation.w = msg.transform.rotation.z;
00173         pose_move_srv.request.pose.orientation.x = msg.transform.rotation.w;
00174         
00175         pose_move_srv.request.pose.position.x = msg.transform.translation.x;
00176         pose_move_srv.request.pose.position.y = msg.transform.translation.y;
00177         pose_move_srv.request.pose.position.z = msg.transform.translation.z;
00178           std::cout << "moving to:" << msg << std::endl;
00179         sleep(1);
00180         //kaijen ik
00181         if (pose_move2_client.call(pose_move_srv)) { 
00182           ROS_INFO("pose_move2: %d", pose_move_srv.response.success); 
00183         } else { 
00184           ROS_ERROR("Failed to call service pose_move2"); 
00185         }
00186       } catch (tf::TransformException ex){
00187         ROS_ERROR("%s",ex.what());
00188       } 
00189     }
00190   // [fill srv structure and make request to the server]
00191   
00192 //  pose_move_srv.request.pose.orientation = tf::createQuaternionMsgFromRollPitchYaw(0.785,0.785,0.785); 
00193 //
00194 //  pose_move_srv.request.pose.position.x = 0.4; 
00195 //  pose_move_srv.request.pose.position.y = 0.2; 
00196 //  pose_move_srv.request.pose.position.z = 0.1; 
00197 
00198 //  if (pose_move_client.call(pose_move_srv)) { 
00199 //    ROS_INFO("pose_move: %d", pose_move_srv.response.success); 
00200 //  } else { 
00201 //    ROS_ERROR("Failed to call service pose_move"); 
00202 //  }
00203 
00204   //unlock access to driver if previously blocked
00205   //this->driver_.unlock();
00206 }
00207 
00208 /*  [subscriber callbacks] */
00209 void WamTest::pose_callback(const geometry_msgs::PoseStamped::ConstPtr& msg) 
00210 { 
00211   //std::cout << msg->data << std::endl; 
00212 
00213 }
00214 void WamTest::joint_states_callback(const sensor_msgs::JointState::ConstPtr& msg) 
00215 { 
00216 
00217   //std::cout << msg->data << std::endl; 
00218 
00219 }
00220 
00221 /*  [service callbacks] */
00222 
00223 /*  [action callbacks] */
00224 
00225 /*  [action requests] */
00226 
00227 void WamTest::calculate_error(std::string target) {
00228 
00229   tf::StampedTransform transform;
00230   geometry_msgs::TransformStamped msg;
00231   try{
00232       listener.lookupTransform("wam7", target, ros::Time(0), transform);
00233       tf::transformStampedTFToMsg(transform,msg);
00234       std::cout << "transform error " << msg << std::endl;
00235   } catch (tf::TransformException ex){
00236     ROS_ERROR("%s",ex.what());
00237   } 
00238 }
00239 
00240 
00241 int main(int argc, char** argv) {
00242     ros::init(argc, argv, "wam_test");
00243     WamTest wam_test;
00244     ros::Rate loop_rate(10); 
00245     while(ros::ok()){
00246 //      wam_test.mainLoop();
00247       ros::spinOnce();
00248       loop_rate.sleep(); 
00249     }
00250 }


iri_wam_test
Author(s):
autogenerated on Fri Dec 6 2013 22:41:21