SendAutoselectRequest.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 
00003 #include <opencv/cv.h>
00004 #include <opencv/highgui.h>
00005 #include <sensor_msgs/Image.h>
00006 #include <cv_bridge/CvBridge.h>
00007 #include <image_transport/image_transport.h>
00008 
00009 //service definitions
00010 #include <portrait_robot_msgs/alubsc_node_instr.h>
00011 #include <portrait_robot_msgs/alubsc_status_msg.h>
00012 
00013 //std
00014 #include <vector>
00015 
00016 bool gotAnwser;
00017 
00018 bool MessageResultRequest(portrait_robot_msgs::alubsc_status_msg::Request& req,
00019                 portrait_robot_msgs::alubsc_status_msg::Response& res) {
00020         if (req.ID == 2) {
00021                 ROS_INFO("Status: [%s]", req.message.c_str());
00022         }
00023         return true;
00024 }
00025 
00026 bool HandleResultReqest(portrait_robot_msgs::alubsc_node_instr::Request& req,
00027                 portrait_robot_msgs::alubsc_node_instr::Response& res) {
00028 
00029         gotAnwser = true;
00030         res.success = true;
00031 
00032         try {
00033 
00034                 if (req.images.size() != 1) {
00035                         ROS_ERROR(
00036                                         "The response request has %i images, but it should have only 1", int(req.images.size()));
00037                         res.success = false;
00038                         return false;
00039                 }
00040 
00041                 sensor_msgs::CvBridge cvBridge;
00043                 if (!cvBridge.fromImage(req.images[0])) {
00044                         throw sensor_msgs::CvBridgeException(
00045                                         "Conversion to OpenCV image failed");
00046                 }ROS_INFO(
00047                                 "got image with widht=%i and height=%i", req.images[0].width, req.images[0].height);
00048                 cv::Mat img = cv::Mat(cvBridge.toIpl(), true);
00049                 cv::imwrite("send_autoselect_reques_result.png", img);
00050                 ROS_INFO("saved response to send_autoselect_reques_result.png");
00051         } catch (sensor_msgs::CvBridgeException& error) {
00052                 ROS_ERROR("could not convert the image with the cvBridge to a cv::Mat");
00053                 res.success = false;
00054                 return false;
00055         }
00056 
00057         return true;
00058 }
00059 
00061 int main(int argc, char* argv[]) {
00062 
00063         gotAnwser = false;
00064 
00065         ros::init(argc, argv, "send_autoselect_request");
00066         ros::NodeHandle n;
00067 
00068         if (argc < 3) {
00069                 ROS_ERROR("there must be an image to transfer");
00070 
00071         }
00072 
00073         std::string topic = "/contour_detector/contour_detector";
00074         if (argc >= 4) {
00075                 topic = argv[3];
00076         }
00077 
00078         //setting up the service for the anwser
00079         ros::ServiceServer resultService = n.advertiseService<>(
00080                         "/alubsc/edge_image", HandleResultReqest);
00081 
00082         //provide the service for the status messages
00083         ros::ServiceServer messageService = n.advertiseService<>(
00084                         "/alubsc/status_msg", MessageResultRequest);
00085 
00086         portrait_robot_msgs::alubsc_node_instr srv;
00087         srv.request.abort = false;
00088 
00089         //load image
00090         cv::Mat img = cv::imread(argv[1]);
00091         cv::Mat mask = cv::imread(argv[2]);
00092 
00093         try {
00094                 sensor_msgs::CvBridge cvBridge;
00095                 IplImage ipl = img;
00096                 srv.request.images.push_back(*(cvBridge.cvToImgMsg(&ipl)));
00097         } catch (sensor_msgs::CvBridgeException& error) {
00098                 ROS_ERROR(
00099                                 "could not convert the image with the cvBridge to a sensor_msgs/Image");
00100                 return 1;
00101         }
00102 
00103         try {
00104                 sensor_msgs::CvBridge cvBridge;
00105                 IplImage ipl = mask;
00106                 srv.request.images.push_back(*(cvBridge.cvToImgMsg(&ipl)));
00107         } catch (sensor_msgs::CvBridgeException& error) {
00108                 ROS_ERROR(
00109                                 "could not convert the mask with the cvBridge to a sensor_msgs/Image");
00110                 return 1;
00111         }
00112 
00113         ros::ServiceClient client = n.serviceClient<
00114                         portrait_robot_msgs::alubsc_node_instr>(topic);
00115         ROS_INFO("Service request send");
00116         if (client.call(srv)) {
00117                 ROS_INFO("got response!");
00118         } else {
00119                 ROS_ERROR("Failed to call service %s", topic.c_str());
00120                 return 1;
00121         }
00122 
00123         ROS_INFO("Waiting for the answer service request...");
00124         //setting up the service
00125         ros::Rate loopRate(10);
00126         while (ros::ok() && !gotAnwser) {
00127                 ros::spinOnce();
00128                 loopRate.sleep();
00129         }
00130         if (gotAnwser) {
00131                 ROS_INFO("Got response");
00132         }
00133 
00134         return 0;
00135 
00136 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends


face_contour_detector
Author(s): Fabian Wenzelmann and Julian Schmid
autogenerated on Wed Dec 26 2012 16:18:17