ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I didn't pay much attention to your code, not really sure of what you are trying to accomplish, but I don't think it is a good idea to declare the publishers inside a subscriber callback. As you will be creating the publisher every time you get data on the subscriber, I don't know how this would behave.
Try something like this:
using namespace std;
//REMEMBER! EVERY TIME YOU USE A GLOBAL VARIABLE... GOD KILLS A KITTEN!!!
ros::Publisher chatter_pubs;
ros::ServiceClient client;
// Messege recieved callback
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
// Variables
std_msgs::String actionstr;
std::stringstream ss;
// APPL NODE SHIZ
appl::appl_request srv;
//Send to POMDP Node
srv.request.cmd=2;
srv.request.obs=msg->data.c_str();
client.call(srv);
int action=srv.response.action;
// Get action string
if (action == 0) {ss << "Listen";}
if (action == 1) {ss << "OpenRight";}
if (action == 2) {ss << "OpenLeft";}
// Publish String
actionstr.data = ss.str();
chatter_pubs.publish(actionstr);
printf("Sent\n");
printf("Obs: %s Action: %d - %s\n", msg->data.c_str(), action, actionstr.data.c_str());
}
/* ------------------------------------------------ */
/* ---------------------******--------------------- */
/* ---------------------*MAIN*--------------------- */
/* ---------------------******--------------------- */
/* ------------------------------------------------ */
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
chatter_pubs = n.advertise<std_msgs::String>("actionstr", 1000);
// Set to Appl client
client = n.serviceClient<appl::appl_request>("appl_request");
srand((unsigned)time(0));
int tiger;//0=left 1=right
tiger=rand()%2;
// Reset Appl Controller.
appl::appl_request srv;
srv.request.cmd=1; //reset the controller first
client.call(srv);
int action=srv.response.action;
ROS_INFO("I Listen");
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
ros::spin();
return 0;
}