rovio_head.cpp
Go to the documentation of this file.
00001 
00011 #include <ros/ros.h>
00012 #include <rovio_shared/head_ctrl.h>
00013 #include <rovio_ctrl/rovio_head.h>
00014 #include <rovio_shared/rovio_http.h>
00015 #include <sstream>
00016 #include <std_msgs/String.h>
00017 #include <string>
00018 
00019 using namespace std;
00020 
00021 head_controller::head_controller()
00022 {
00023   string user;
00024   string pass;
00025 
00026   // check for all the correct parameters
00027   if (!node.getParam(USER, user))
00028   {
00029     ROS_ERROR("Parameter %s not found.", USER);
00030     exit(-1);
00031   }
00032   if (!node.getParam(PASS, pass))
00033   {
00034     ROS_ERROR("Parameter %s not found.", PASS);
00035     exit(-1);
00036   }
00037   if (!node.getParam(HOST, host))
00038   {
00039     ROS_ERROR("Parameter %s not found.", HOST);
00040     exit(-1);
00041   }
00042 
00043   // create the communication object to talk to Rovio
00044   rovio = new rovio_http(user, pass);
00045 
00046   // add services and published topics
00047   head_ctrl = node.advertiseService("head_ctrl", &head_controller::head_ctrl_callback, this);
00048   head_sensor = node.advertise<std_msgs::String>("head_sensor", 8);
00049 
00050   ROS_INFO("Rovio Head Controller Initialized");
00051 }
00052 
00053 head_controller::~head_controller()
00054 {
00055   // free up the rovio_http object
00056   delete rovio;
00057 }
00058 
00059 bool head_controller::head_ctrl_callback(rovio_shared::head_ctrl::Request &req, rovio_shared::head_ctrl::Response &resp)
00060 {
00061 
00062   // make sure the head position value is valid
00063   if (req.head_pos != rovio_shared::head_ctrl::Request::HEAD_UP
00064       && req.head_pos != rovio_shared::head_ctrl::Request::HEAD_DOWN
00065       && req.head_pos != rovio_shared::head_ctrl::Request::HEAD_MIDDLE)
00066   {
00067     ROS_ERROR("Head position 'head_pos' value of %i is not valid.", req.head_pos);
00068     return false;
00069   }
00070 
00071   // build the URL command and send it
00072   stringstream ss;
00073   ss << "http://" << host.c_str() << "/rev.cgi?Cmd=nav&action=18&drive=" << (int)req.head_pos;
00074   rovio_response *buf = rovio->send(ss.str().c_str());
00075 
00076   // parse out the response
00077   int resp_code = -1;
00078   sscanf(strstr(buf->data, "responses = "), "responses = %i", &resp_code);
00079   resp.response = resp_code;
00080 
00081   rovio_response_clean(buf);
00082   return true;
00083 }
00084 
00085 void head_controller::pub_head_sensor()
00086 {
00087   // build the URL command and send it
00088   stringstream ss;
00089   ss << "http://" << host.c_str() << "/rev.cgi?Cmd=nav&action=1";
00090   rovio_response *buf = rovio->send(ss.str().c_str());
00091 
00092   // parse out the response
00093   int resp_code = -1;
00094   sscanf(strstr(buf->data, "head_position="), "head_position=%i", &resp_code);
00095 
00096   // decide which head position the Rovio is in
00097   ss.str("");
00098   if (resp_code > 140)
00099   {
00100     ss << "HEAD_DOWN";
00101   }
00102   else if (resp_code < 135)
00103   {
00104     ss << "HEAD_UP";
00105   }
00106   else
00107   {
00108     ss << "HEAD_MIDDLE";
00109   }
00110 
00111   std_msgs::String msg;
00112   msg.data = ss.str();
00113   head_sensor.publish(msg);
00114 
00115   rovio_response_clean(buf);
00116 }
00117 
00118 int main(int argc, char **argv)
00119 {
00120   // initialize ROS and the node
00121   ros::init(argc, argv, "rovio_head");
00122 
00123   // initialize the Rovio controller
00124   head_controller controller;
00125 
00126   // update at 5 Hz
00127   ros::Rate loop_rate(5);
00128   // continue until a ctrl-c has occurred
00129   while (ros::ok())
00130   {
00131     ros::spinOnce();
00132     //publish the topics
00133     controller.pub_head_sensor();
00134     loop_rate.sleep();
00135   }
00136 
00137   return EXIT_SUCCESS;
00138 }


rovio_ctrl
Author(s): Russell Toris
autogenerated on Mon Oct 6 2014 07:13:12