00001 #include "moped_handler_alg_node.h"
00002
00003 MopedHandlerAlgNode::MopedHandlerAlgNode(void) :
00004 algorithm_base::IriBaseAlgorithm<MopedHandlerAlgorithm>(),
00005 moped_client_("moped", true)
00006 {
00007
00008 this->working = false;
00009
00010
00011 this->outputOPL_publisher_ = this->public_node_handle_.advertise<pr_msgs::ObjectPoseList>("outputOPL", 10);
00012
00013
00014 this->subImage_.subscribe(this->public_node_handle_, "/camera/rgb/image_mono", 1);
00015 this->subPointCloud_.subscribe(this->public_node_handle_, "/camera/rgb/points", 1);
00016
00017 typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::PointCloud2> MySyncPolicy;
00018 this->sync_ = new message_filters::Synchronizer<MySyncPolicy>(MySyncPolicy(10), subImage_, subPointCloud_);
00019 sync_->registerCallback(boost::bind(&MopedHandlerAlgNode::callback, this, _1, _2));
00020
00021
00022 this->enable_server_ = this->public_node_handle_.advertiseService("enable", &MopedHandlerAlgNode::enableCallback, this);
00023
00024
00025
00026
00027
00028
00029 }
00030
00031 MopedHandlerAlgNode::~MopedHandlerAlgNode(void)
00032 {
00033
00034 }
00035
00036 void MopedHandlerAlgNode::mainNodeThread(void)
00037 {
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 }
00049
00050
00051
00052
00053 bool MopedHandlerAlgNode::enableCallback(iri_moped_handler::enable::Request &req, iri_moped_handler::enable::Response &res)
00054 {
00055 ROS_INFO("MopedHandlerAlgNode::enableCallback: New Request Received!");
00056
00057 if(req.enable)
00058 {
00059 this->subImage_.subscribe(this->public_node_handle_, "/camera/rgb/image_mono", 1);
00060 this->subPointCloud_.subscribe(this->public_node_handle_, "/camera/rgb/points", 1);
00061
00062 typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::PointCloud2> MySyncPolicy;
00063 this->sync_ = new message_filters::Synchronizer<MySyncPolicy>(MySyncPolicy(10), subImage_, subPointCloud_);
00064 sync_->registerCallback(boost::bind(&MopedHandlerAlgNode::callback, this, _1, _2));
00065 }
00066 else
00067 {
00068 this->subImage_.subscribe(this->public_node_handle_, "falseTopic", 1);
00069 this->subPointCloud_.subscribe(this->public_node_handle_, "falseTopic2", 1);
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 return true;
00092 }
00093
00094
00095 void MopedHandlerAlgNode::mopedDone(const actionlib::SimpleClientGoalState& state, const iri_moped_actionserver::mopedResultConstPtr& result)
00096 {
00097 if( state.toString().compare("SUCCEEDED") == 0 )
00098 ROS_INFO("MopedHandlerAlgNode::mopedDone: Goal Achieved!");
00099 else
00100 {
00101 ROS_INFO("MopedHandlerAlgNode::mopedDone: %s", state.toString().c_str());
00102 return;
00103 }
00104
00105
00106 pcl::fromROSMsg(this->inputPointCloud, this->pcl_);
00107
00108
00109 pr_msgs::ObjectPoseList outputMsg = result->pose;
00110
00111
00112 vector<pr_msgs::ObjectPose> objects = outputMsg.object_list;
00113 vector<int> bad;
00114 for(size_t i=0; i<objects.size(); ++i)
00115 {
00116 float col = objects[i].pose2D.x;
00117 float row = objects[i].pose2D.y;
00118
00119
00120 if( col < 0 or col >= 640 or row < 0 or row >= 480 )
00121 bad.push_back(i);
00122 }
00123
00124
00125 for(int i=bad.size()-1; i>=0; --i)
00126 outputMsg.object_list.erase( outputMsg.object_list.begin() + bad[i] );
00127
00128
00129 for(size_t i=0; i < outputMsg.object_list.size(); ++i)
00130 {
00131
00132 float col = outputMsg.object_list[i].pose2D.x;
00133 float row = outputMsg.object_list[i].pose2D.y;
00134
00135
00136 pcl::PointXYZ p = this->pcl_.at(col, row);
00137
00138
00139
00140
00141
00142 outputMsg.object_list[i].pose.position.x = p.x;
00143 outputMsg.object_list[i].pose.position.y = p.y;
00144 outputMsg.object_list[i].pose.position.z = p.z;
00145
00146
00147 ROS_INFO("Pixel (%f, %f) is at position (%f, %f, %f).", col, row, p.x, p.y, p.z);
00148 }
00149
00150
00151 this->outputOPL_publisher_.publish(outputMsg);
00152
00153 ROS_INFO("Sent final result to topic");
00154
00155 this->working = false;
00156 }
00157
00158 void MopedHandlerAlgNode::mopedActive()
00159 {
00160
00161 }
00162
00163 void MopedHandlerAlgNode::mopedFeedback(const iri_moped_actionserver::mopedFeedbackConstPtr& feedback)
00164 {
00165
00166
00167 bool feedback_is_ok = true;
00168
00169
00170
00171
00172
00173 if( !feedback_is_ok )
00174 {
00175 moped_client_.cancelGoal();
00176
00177 }
00178 }
00179
00180
00181 void MopedHandlerAlgNode::mopedMakeActionRequest()
00182 {
00183 ROS_INFO("MopedHandlerAlgNode::mopedMakeActionRequest: Starting New Request!");
00184
00185
00186
00187 moped_client_.waitForServer();
00188 ROS_INFO("MopedHandlerAlgNode::mopedMakeActionRequest: Server is Available!");
00189
00190
00191 moped_goal_.image = this->inputImage;
00192 moped_client_.sendGoal(moped_goal_,
00193 boost::bind(&MopedHandlerAlgNode::mopedDone, this, _1, _2),
00194 boost::bind(&MopedHandlerAlgNode::mopedActive, this),
00195 boost::bind(&MopedHandlerAlgNode::mopedFeedback, this, _1));
00196 ROS_INFO("MopedHandlerAlgNode::mopedMakeActionRequest: Goal Sent. Wait for Result!");
00197 }
00198
00199 void MopedHandlerAlgNode::node_config_update(Config &config, uint32_t level)
00200 {
00201 this->alg_.lock();
00202
00203 this->alg_.unlock();
00204 }
00205
00206 void MopedHandlerAlgNode::callback(const ImageConstPtr& image, const PointCloud2ConstPtr& pcl)
00207 {
00208 if( this->working )
00209 return;
00210 else
00211 this->working = true;
00212
00213 ROS_INFO("Received image and PCL with the same timestamp!");
00214
00215
00216 this->inputImage = *image;
00217 this->inputPointCloud = *pcl;
00218
00219
00220 mopedMakeActionRequest();
00221 }
00222
00223 void MopedHandlerAlgNode::addNodeDiagnostics(void)
00224 {
00225 }
00226
00227
00228 int main(int argc,char *argv[])
00229 {
00230 return algorithm_base::main<MopedHandlerAlgNode>(argc, argv, "moped_handler_alg_node");
00231 }