tibi_dabo_laser_2d_to_3d_alg_node.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_laser_2d_to_3d_alg_node.h"
00002 
00003 TibiDaboLaser2dTo3dAlgNode::TibiDaboLaser2dTo3dAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<TibiDaboLaser2dTo3dAlgorithm>(),
00005   servo_angle_client_("servo_angle", true)
00006 {
00007   //init class attributes if necessary
00008   this->loop_rate_ = 20;//in [Hz]
00009 
00010   // [init publishers]
00011   this->vertical_laser_pan_publisher_ = this->public_node_handle_.advertise<sensor_msgs::JointState>("vertical_laser_pan", 100);
00012   this->pointcloud_publisher_ = this->public_node_handle_.advertise<sensor_msgs::PointCloud>("pointcloud", 100);
00013   
00014   this->JointState_msg_.name.resize(1);
00015   this->JointState_msg_.name[0] = "base_link2vertical_laser";
00016   this->JointState_msg_.position.resize(1); 
00017   
00018   // [init subscribers]
00019   
00020   // [init services]
00021   
00022   // [init clients]
00023   assemble_scans_client_ = this->public_node_handle_.serviceClient<laser_assembler::AssembleScans>("assemble_scans");
00024   
00025   // [init action servers]
00026   
00027   // [init action clients]
00028   
00029   this->public_node_handle_.getParam("scan_vel", scan_vel);
00030   this->public_node_handle_.getParam("min_angle", angle_min);
00031   this->public_node_handle_.getParam("max_angle", angle_max);
00032   
00033   angle=0.0;
00034   angle_margin=3.0;
00035   previous_time = ros::Time::now();
00036   minimum_time_between_scans = ros::Duration(1/900.0*scan_vel*scan_vel-7.0/60.0*scan_vel+3.5);
00037   
00038   
00039   this->servo_angle_goal_.trajectory.joint_names.resize(1);
00040   this->servo_angle_goal_.trajectory.points.resize(1);
00041   this->servo_angle_goal_.trajectory.points[0].positions.resize(1);
00042   this->servo_angle_goal_.trajectory.points[0].velocities.resize(1);
00043   
00044   this->servo_angle_goal_.trajectory.joint_names[0]="vertical_laser_servo";
00045   this->servo_angle_goal_.trajectory.points[0].positions[0]=angle_min-angle_margin;
00046   this->servo_angle_goal_.trajectory.points[0].velocities[0]=scan_vel;
00047   
00048   servo_angleMakeActionRequest();
00049   servo_going_right=false;
00050   
00051 }
00052 
00053 TibiDaboLaser2dTo3dAlgNode::~TibiDaboLaser2dTo3dAlgNode(void)
00054 {
00055   // [free dynamic memory]
00056 }
00057 
00058 void TibiDaboLaser2dTo3dAlgNode::mainNodeThread(void)
00059 {
00060   // [fill msg structures]
00061   //this->JointState_msg_.data = my_var;
00062   //this->PointCloud_msg_.data = my_var;
00063   
00064   // [fill srv structure and make request to the server]
00065   
00066   
00067   
00068   
00069   //When servo arrives at boundaries, publish assembled scans
00070   if((angle>angle_max || angle<angle_min) && (ros::Time::now()-previous_time>minimum_time_between_scans)){
00071     
00072     assemble_scans_srv_.request.begin = previous_time;
00073     assemble_scans_srv_.request.end   = ros::Time::now();
00074     previous_time = ros::Time::now();
00075     
00076     //ROS_INFO("TibiDaboLaser2dTo3dAlgNode:: Sending New Request!"); 
00077     if (assemble_scans_client_.call(assemble_scans_srv_)) 
00078     { 
00079       //ROS_INFO("TibiDaboLaser2dTo3dAlgNode:: Response: %s", assemble_scans_srv_.response.result);
00080       //ROS_INFO("Scan assembled and published");
00081       this->pointcloud_publisher_.publish(assemble_scans_srv_.response.cloud);
00082     } 
00083     else 
00084     { 
00085       ROS_ERROR("TibiDaboLaser2dTo3dAlgNode:: Failed to Call Server on topic assemble_scans "); 
00086     }
00087     
00088     //Move servo
00089     if(angle>angle_max){
00090       this->servo_angle_goal_.trajectory.points[0].positions[0]=angle_min-angle_margin;
00091       servo_going_right=false;
00092     }else if(angle<angle_min){
00093       this->servo_angle_goal_.trajectory.points[0].positions[0]=angle_max+angle_margin;
00094       servo_going_right=true;
00095     }
00096     
00097     this->servo_angle_goal_.trajectory.points[0].velocities[0]=scan_vel;
00098     //ROS_INFO("Moving servo");
00099     servo_angleMakeActionRequest();
00100   }else if(ros::Time::now()-previous_time>minimum_time_between_scans){}
00101 
00102   
00103   
00104   // [fill action structure and make request to the action server]
00105 
00106   // [publish messages]
00107   
00108   
00109 }
00110 
00111 /*  [subscriber callbacks] */
00112 
00113 /*  [service callbacks] */
00114 
00115 /*  [action callbacks] */
00116 void TibiDaboLaser2dTo3dAlgNode::servo_angleDone(const actionlib::SimpleClientGoalState& state,  const control_msgs::FollowJointTrajectoryResultConstPtr& result) 
00117 { 
00118   if( state.toString().compare("SUCCEEDED") == 0 ) 
00119     ROS_INFO("TibiDaboLaser2dTo3dAlgNode::servo_angleDone: Goal Achieved!"); 
00120   else 
00121     ROS_INFO("TibiDaboLaser2dTo3dAlgNode::servo_angleDone: %s", state.toString().c_str()); 
00122 
00123   //copy & work with requested result 
00124 } 
00125 
00126 void TibiDaboLaser2dTo3dAlgNode::servo_angleActive() 
00127 { 
00128   //ROS_INFO("TibiDaboLaser2dTo3dAlgNode::servo_angleActive: Goal just went active!"); 
00129 } 
00130 
00131 void TibiDaboLaser2dTo3dAlgNode::servo_angleFeedback(const control_msgs::FollowJointTrajectoryFeedbackConstPtr& feedback) 
00132 { 
00133   //ROS_INFO("TibiDaboLaser2dTo3dAlgNode::servo_angleFeedback: Got Feedback!"); 
00134 
00135   //bool feedback_is_ok = true; 
00136 
00137   //analyze feedback 
00138   //my_var = feedback->var; 
00139 
00140   //if feedback is not what expected, cancel requested goal 
00141   //if( !feedback_is_ok ) 
00142   //{ 
00143     //servo_angle_client_.cancelGoal(); 
00144     //ROS_INFO("TibiDaboLaser2dTo3dAlgNode::servo_angleFeedback: Cancelling Action!"); 
00145   //}
00146   
00147   double correction=-scan_vel/10.0;
00148   
00149   angle=feedback->actual.positions[0];
00150   if(servo_going_right) angle=angle+correction;
00151   //ROS_INFO_STREAM("Servo at " << angle);
00152   
00153   //Refresh laser frame
00154   this->JointState_msg_.header.stamp = ros::Time::now();
00155   this->JointState_msg_.position[0]=angle*TO_RAD;
00156   this->vertical_laser_pan_publisher_.publish(this->JointState_msg_);
00157   
00158   if(servo_going_right) angle=angle-correction;
00159   
00160   
00161   //ROS_INFO_STREAM("Actual servo angle: " << angle);
00162 }
00163 
00164 /*  [action requests] */
00165 void TibiDaboLaser2dTo3dAlgNode::servo_angleMakeActionRequest() 
00166 { 
00167   //ROS_INFO("TibiDaboLaser2dTo3dAlgNode::servo_angleMakeActionRequest: Starting New Request!"); 
00168 
00169   //wait for the action server to start 
00170   //will wait for infinite time 
00171   servo_angle_client_.waitForServer();  
00172   //ROS_INFO("TibiDaboLaser2dTo3dAlgNode::servo_angleMakeActionRequest: Server is Available!"); 
00173 
00174   //send a goal to the action 
00175   //servo_angle_goal_.data = my_desired_goal; 
00176   servo_angle_client_.sendGoal(servo_angle_goal_, 
00177               boost::bind(&TibiDaboLaser2dTo3dAlgNode::servo_angleDone,     this, _1, _2), 
00178               boost::bind(&TibiDaboLaser2dTo3dAlgNode::servo_angleActive,   this), 
00179               boost::bind(&TibiDaboLaser2dTo3dAlgNode::servo_angleFeedback, this, _1)); 
00180   //ROS_INFO("TibiDaboLaser2dTo3dAlgNode::servo_angleMakeActionRequest: Goal Sent. Wait for Result!"); 
00181 }
00182 
00183 void TibiDaboLaser2dTo3dAlgNode::node_config_update(Config &config, uint32_t level)
00184 {
00185   this->alg_.lock();
00186   
00187   scan_vel=config.scan_velocity;
00188   minimum_time_between_scans = ros::Duration(1/900.0*scan_vel*scan_vel-7.0/60.0*scan_vel+3.5);
00189   angle_min=config.min_angle;
00190   angle_max=config.max_angle;
00191 
00192   this->alg_.unlock();
00193 }
00194 
00195 void TibiDaboLaser2dTo3dAlgNode::addNodeDiagnostics(void)
00196 {
00197 }
00198 
00199 /* main function */
00200 int main(int argc,char *argv[])
00201 {
00202   return algorithm_base::main<TibiDaboLaser2dTo3dAlgNode>(argc, argv, "tibi_dabo_laser_2d_to_3d_alg_node");
00203 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


tibi_dabo_laser_2d_to_3d
Author(s): dblasco
autogenerated on Thu Sep 12 2013 10:26:05