mock_agent.cpp
Go to the documentation of this file.
00001 
00011 #include <iostream>
00012 #include <lfd_common/demonstration.h>
00013 #include <lfd_common/action_complete.h>
00014 #include <std_msgs/Int32.h>
00015 #include <ros/ros.h>
00016 #include <time.h>
00017 
00018 using namespace std;
00019 
00020 bool dem; 
00021 ros::ServiceClient a_complete; 
00028 void execute_callback(const std_msgs::Int32::ConstPtr &msg)
00029 {
00030   // let's execute the command
00031   cout << "Execution command '" << msg->data << "' received... ";
00032 
00033   // sleep some time and then report the action finished
00034   usleep(msg->data * 10000);
00035 
00036   // report the action now done
00037   cout << "execution finished!" << endl;
00038 
00039   lfd_common::action_complete srv;
00040   // randomly send a correction
00041   if ((rand() % 10) == 0)
00042   {
00043     // based on what the mock classifier is going to do, give a different correction value
00044     srv.request.a = -100;
00045 
00046     cout << "Correction given: '" << srv.request.a << "'." << endl;
00047     // set the valid flag
00048     srv.request.valid_correction = true;
00049   }
00050   else
00051     // set the valid flag
00052     srv.request.valid_correction = false;
00053   // send the service
00054   a_complete.call(srv);
00055 }
00056 
00064 bool demonstration_callback(lfd_common::demonstration::Request &req, lfd_common::demonstration::Response &resp)
00065 {
00066   // only send a demonstration every other request
00067   if (dem)
00068   {
00069     // based on what the mock classifier is going to do, give a demonstration
00070     if (req.s.state_vector.at(2) < 5)
00071       resp.a = 5;
00072     else if (req.s.state_vector.at(2) < 10)
00073       resp.a = 10;
00074     else if (req.s.state_vector.at(2) < 15)
00075       resp.a = 15;
00076     else if (req.s.state_vector.at(2) < 20)
00077       resp.a = 20;
00078     else
00079       resp.a = 25;
00080 
00081     cout << "Request for demonstration received -- giving demonstration '" << resp.a << "'." << endl;
00082   }
00083 
00084   // set the valid flag and flip the demonstration boolean
00085   resp.valid = dem;
00086   dem = !dem;
00087 
00088   return true;
00089 }
00090 
00098 int main(int argc, char **argv)
00099 {
00100   // initialize ROS and the node
00101   ros::init(argc, argv, "mock_agent");
00102   // a handle for this ROS node
00103   ros::NodeHandle node;
00104 
00105   // create services and topics
00106   ros::Subscriber execute = node.subscribe<std_msgs::Int32>("execute", 1, execute_callback);
00107   a_complete = node.serviceClient<lfd_common::action_complete>("a_complete");
00108   ros::ServiceServer dem = node.advertiseService("demonstration", demonstration_callback);
00109 
00110   ROS_INFO("Mock Agent Initialized");
00111 
00112   // used to randomly provide corrections
00113   srand(time(NULL));
00114 
00115   ros::spin();
00116 
00117   return EXIT_SUCCESS;
00118 }


cba
Author(s): Russell Toris
autogenerated on Thu Jan 2 2014 11:23:56