Go to the documentation of this file.00001
00060 #include <cob_people_detection/coordinator_node.h>
00061
00062 CoordinatorNode::CoordinatorNode(ros::NodeHandle nh) :
00063 node_handle_(nh)
00064 {
00065 sensor_message_gateway_open_ = false;
00066 last_detection_message_.header.stamp = ros::Time::now();
00067
00068
00069 namespace_gateway_ = "";
00070 std::cout << "\n---------------------------\nCoordinator Node Parameters:\n---------------------------\n";
00071 node_handle_.param("namespace_gateway", namespace_gateway_, namespace_gateway_);
00072 std::cout << "namespace_gateway = " << namespace_gateway_ << "\n";
00073
00074 detection_sub_ = node_handle_.subscribe("detection_array", 1, &CoordinatorNode::detectionsCallback, this);
00075
00076
00077 get_detections_server_ = new GetDetectionsServer(node_handle_, "get_detections_server", boost::bind(&CoordinatorNode::getDetectionsServerCallback, this, _1), false);
00078 get_detections_server_->start();
00079
00080
00081 service_server_start_recognition_ = node_handle_.advertiseService("start_recognition", &CoordinatorNode::startRecognitionCallback, this);
00082 service_server_stop_recognition_ = node_handle_.advertiseService("stop_recognition", &CoordinatorNode::stopRecognitionCallback, this);
00083
00084 std::cout << "CoordinatorNode initialized." << std::endl;
00085 }
00086
00087 CoordinatorNode::~CoordinatorNode()
00088 {
00089 if (get_detections_server_ != 0)
00090 delete get_detections_server_;
00091 }
00092
00093 void CoordinatorNode::detectionsCallback(const cob_perception_msgs::DetectionArray::ConstPtr& detection_array)
00094 {
00095
00096 boost::lock_guard < boost::mutex > lock(last_detection_mutex_);
00097
00098 last_detection_message_ = *detection_array;
00099 }
00100
00101 void CoordinatorNode::getDetectionsServerCallback(const cob_people_detection::getDetectionsGoalConstPtr& goal)
00102 {
00103
00104 boost::lock_guard < boost::mutex > lock(active_action_mutex_);
00105
00106 cob_people_detection::getDetectionsResult result;
00107 result.detections.detections.clear();
00108 if (sensor_message_gateway_open_ == false)
00109 {
00110
00111 std::string cmd = "rosrun dynamic_reconfigure dynparam set " + namespace_gateway_ + " target_publishing_rate 5.0";
00112 int retval = system(cmd.c_str());
00113 }
00114
00115 bool message_received = false;
00116 bool collect_messages = true;
00117 ros::Duration maximal_message_age(goal->maximum_message_age);
00118 ros::Duration timeout(goal->timeout);
00119 ros::Time start_time = ros::Time::now();
00120 while (collect_messages == true && (goal->timeout == 0 || (ros::Time::now() - start_time) < timeout))
00121 {
00122
00123 boost::lock_guard < boost::mutex > lock(last_detection_mutex_);
00124
00125 std::cout << "timeout " << (ros::Time::now() - start_time).toSec() << " maximal_age " << (ros::Time::now() - last_detection_message_.header.stamp).toSec()
00126 << std::endl;
00127 if ((ros::Time::now() - last_detection_message_.header.stamp) < maximal_message_age || goal->maximum_message_age == 0)
00128 {
00129
00130 if (message_received == false || (result.detections.detections.size() <= last_detection_message_.detections.size()))
00131 result.detections = last_detection_message_;
00132 message_received = true;
00133
00134 if (sensor_message_gateway_open_ == true)
00135 collect_messages = false;
00136 }
00137 ros::spinOnce();
00138 }
00139
00140 if (sensor_message_gateway_open_ == false)
00141 {
00142
00143 std::string cmd = "rosrun dynamic_reconfigure dynparam set " + namespace_gateway_ + " target_publishing_rate 0.0";
00144 int retval = system(cmd.c_str());
00145 }
00146
00147 if (message_received == true)
00148 get_detections_server_->setSucceeded(result, "Found a valid detection message.");
00149 else
00150 get_detections_server_->setAborted(result, "No valid detection message available.");
00151 }
00152
00153 bool CoordinatorNode::startRecognitionCallback(cob_people_detection::recognitionTrigger::Request &req, cob_people_detection::recognitionTrigger::Response &res)
00154 {
00155
00156 boost::lock_guard < boost::mutex > lock(active_action_mutex_);
00157
00158
00159 std::stringstream ss;
00160 ss << "rosrun dynamic_reconfigure dynparam set " << namespace_gateway_ << " target_publishing_rate " << req.target_frame_rate;
00161 int retval = system(ss.str().c_str());
00162
00163 sensor_message_gateway_open_ = true;
00164
00165 return true;
00166 }
00167
00168 bool CoordinatorNode::stopRecognitionCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
00169 {
00170
00171 boost::lock_guard < boost::mutex > lock(active_action_mutex_);
00172
00173
00174 std::string cmd = "rosrun dynamic_reconfigure dynparam set " + namespace_gateway_ + " target_publishing_rate 0.0";
00175 int retval = system(cmd.c_str());
00176
00177 sensor_message_gateway_open_ = false;
00178
00179 return true;
00180 }
00181
00182
00183
00184 int main(int argc, char** argv)
00185 {
00186
00187 ros::init(argc, argv, "coordinator_node");
00188
00189
00190 ros::NodeHandle nh;
00191
00192
00193 CoordinatorNode coordinator_node(nh);
00194
00195
00196
00197
00198
00199
00200 ros::spin();
00201
00202 return 0;
00203 }