Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00048 #include <ros/ros.h>
00049 #include <rovio_shared/head_ctrl.h>
00050 #include <rovio_ctrl/rovio_head.h>
00051 #include <rovio_shared/rovio_http.h>
00052 #include <sstream>
00053 #include <std_msgs/String.h>
00054 #include <string>
00055 
00056 using namespace std;
00057 
00058 head_controller::head_controller()
00059 {
00060   string user;
00061   string pass;
00062 
00063   
00064   if (!node.getParam(USER, user))
00065   {
00066     ROS_ERROR("Parameter %s not found.", USER);
00067     exit(-1);
00068   }
00069   if (!node.getParam(PASS, pass))
00070   {
00071     ROS_ERROR("Parameter %s not found.", PASS);
00072     exit(-1);
00073   }
00074   if (!node.getParam(HOST, host))
00075   {
00076     ROS_ERROR("Parameter %s not found.", HOST);
00077     exit(-1);
00078   }
00079 
00080   
00081   rovio = new rovio_http(user, pass);
00082 
00083   
00084   head_ctrl = node.advertiseService("head_ctrl", &head_controller::head_ctrl_callback, this);
00085   head_sensor = node.advertise<std_msgs::String>("head_sensor", 8);
00086 
00087   ROS_INFO("Rovio Head Controller Initialized");
00088 }
00089 
00090 head_controller::~head_controller()
00091 {
00092   
00093   delete rovio;
00094 }
00095 
00096 bool head_controller::head_ctrl_callback(rovio_shared::head_ctrl::Request &req, rovio_shared::head_ctrl::Response &resp)
00097 {
00098 
00099   
00100   if (req.head_pos != rovio_shared::head_ctrl::Request::HEAD_UP
00101       && req.head_pos != rovio_shared::head_ctrl::Request::HEAD_DOWN
00102       && req.head_pos != rovio_shared::head_ctrl::Request::HEAD_MIDDLE)
00103   {
00104     ROS_ERROR("Head position 'head_pos' value of %i is not valid.", req.head_pos);
00105     return false;
00106   }
00107 
00108   
00109   stringstream ss;
00110   ss << "http://" << host.c_str() << "/rev.cgi?Cmd=nav&action=18&drive=" << (int)req.head_pos;
00111   rovio_response *buf = rovio->send(ss.str().c_str());
00112 
00113   
00114   int resp_code = -1;
00115   sscanf(strstr(buf->data, "responses = "), "responses = %i", &resp_code);
00116   resp.response = resp_code;
00117 
00118   rovio_response_clean(buf);
00119   return true;
00120 }
00121 
00122 void head_controller::pub_head_sensor()
00123 {
00124   
00125   stringstream ss;
00126   ss << "http://" << host.c_str() << "/rev.cgi?Cmd=nav&action=1";
00127   rovio_response *buf = rovio->send(ss.str().c_str());
00128 
00129   
00130   int resp_code = -1;
00131   sscanf(strstr(buf->data, "head_position="), "head_position=%i", &resp_code);
00132 
00133   
00134   ss.str("");
00135   if (resp_code > 140)
00136   {
00137     ss << "HEAD_DOWN";
00138   }
00139   else if (resp_code < 135)
00140   {
00141     ss << "HEAD_UP";
00142   }
00143   else
00144   {
00145     ss << "HEAD_MIDDLE";
00146   }
00147 
00148   std_msgs::String msg;
00149   msg.data = ss.str();
00150   head_sensor.publish(msg);
00151 
00152   rovio_response_clean(buf);
00153 }
00154 
00155 int main(int argc, char **argv)
00156 {
00157   
00158   ros::init(argc, argv, "rovio_head");
00159 
00160   
00161   head_controller controller;
00162 
00163   
00164   ros::Rate loop_rate(5);
00165   
00166   while (ros::ok())
00167   {
00168     ros::spinOnce();
00169     
00170     controller.pub_head_sensor();
00171     loop_rate.sleep();
00172   }
00173 
00174   return EXIT_SUCCESS;
00175 }