rovio_head.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2012, Worcester Polytechnic Institute
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Worcester Polytechnic Institute nor the
00019  *     names of its contributors may be used to endorse or promote
00020  *     products derived from this software without specific prior
00021  *     written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
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   // check for all the correct parameters
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   // create the communication object to talk to Rovio
00081   rovio = new rovio_http(user, pass);
00082 
00083   // add services and published topics
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   // free up the rovio_http object
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   // make sure the head position value is valid
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   // build the URL command and send it
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   // parse out the response
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   // build the URL command and send it
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   // parse out the response
00130   int resp_code = -1;
00131   sscanf(strstr(buf->data, "head_position="), "head_position=%i", &resp_code);
00132 
00133   // decide which head position the Rovio is in
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   // initialize ROS and the node
00158   ros::init(argc, argv, "rovio_head");
00159 
00160   // initialize the Rovio controller
00161   head_controller controller;
00162 
00163   // update at 5 Hz
00164   ros::Rate loop_rate(5);
00165   // continue until a ctrl-c has occurred
00166   while (ros::ok())
00167   {
00168     ros::spinOnce();
00169     //publish the topics
00170     controller.pub_head_sensor();
00171     loop_rate.sleep();
00172   }
00173 
00174   return EXIT_SUCCESS;
00175 }


rovio_ctrl
Author(s): Russell Toris
autogenerated on Sat Dec 28 2013 17:38:53