Ask Your Question
0

Anyone can help? i made a simple node where it publish Int8 and subsribe to that Int8 but it didnt work

asked 2016-10-20 08:12:37 -0500

lolz733 gravatar image

updated 2016-10-21 00:20:44 -0500

this is the talker and listener i modified . when i compile. it says my std_msgs::Int8 number is not derived from 'const std::istream_iterator..' am i doing something wrong for the variable declaration? i tried int but when i compiled and ran the while(ros::ok()) loop didn't work. the "21!!!!!" did not show up. But it worked fine as in that the listener is subsribed to the talker's topic " chatter" but it was publising the data:21. Anyone have any suggestion or reason as to why i cant use the data or why the programme didn't work?

listener.cpp:

#include "ros/ros.h"
#include "std_msgs/Int8.h"

using namespace std;

void chatterCallback(const std_msgs::Int8::ConstPtr& msg)
{
  // Declare number here
  std_msgs::Int8 number;

  ROS_INFO("I heard: [%d]", msg->data);
  number = msg->data;

  // Check if the number received is 21
  if(number==21)
   {
    ROS_INFO("21!!!!!");
   }
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");

  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

  ros::spin();

  return 0;
}

talker.cpp:

#include "ros/ros.h"
#include "std_msgs/Int8.h"

#include <sstream>


int main(int argc, char **argv)
{

  ros::init(argc, argv, "talker");


  ros::NodeHandle n;

  ros::Publisher chatter_pub = n.advertise<std_msgs::Int8>("chatter", 1000);

  ros::Rate loop_rate(10);


  int count = 0;
  while (ros::ok())
  {

    std_msgs::Int8 msg;
    msg.data = 21;
    ROS_INFO("%d", msg.data);

    chatter_pub.publish(msg);

    ros::spinOnce();

    loop_rate.sleep();
    ++count;
  }


  return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-10-20 10:01:21 -0500

Tyrone Nowell gravatar image

updated 2016-10-20 11:56:34 -0500

If you launch them at the same time, chances are, on the first pass, nothing has been published to "chatter" so the listener will not call the chatterCallback function. Then, it would get stuck in that while(ros::()) so it will never call chatterCallback or update 'number' again.

That while loop should be in your talker.cpp since you want it to publish repeatedly as fast as it can or at a given rate (ros::Rate).

Your listener.cpp should just spin waiting for messages to be published.

Have a look at the tutorial examples for the basic publisher/subscriber formats: http://wiki.ros.org/ROS/Tutorials/Wri...

EDIT:

This is how you want your listener to look (I haven't compiled this so there could be some syntax errors):

#include "ros/ros.h"
#include "std_msgs/Int8.h"

using namespace std;

void chatterCallback(const std_msgs::Int8::ConstPtr& msg)
{
  // Declare number here
  std_msgs::Int8 number;

  ROS_INFO("I heard: [%d]", msg->data);
  number = msg->data;

  // Check if the number received is 21
  if(number==21)
   {
    ROS_INFO("21!!!!!");
   }
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");

  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

  ros::spin();

  return 0;
}
edit flag offensive delete link more

Comments

Do I use ROS::spinOnce() or ROS::spin() ? What's the difference ?

lolz733 gravatar image lolz733  ( 2016-10-20 11:20:02 -0500 )edit

And the delcaration of the variable in which I wanna put my data:21 into. Do I just delcaration as global variable int number ? Or do i have declare number as a string and then input the data into the string if I just want to use it for an if statement like as above if the number == 21 thig happen

lolz733 gravatar image lolz733  ( 2016-10-20 11:22:49 -0500 )edit

ros::spin() will not return until the node has been shutdown. ros::spinOnce() works in conjunction with the ros::Rate and .sleep() functions to determine the spin rate.

Tyrone Nowell gravatar image Tyrone Nowell  ( 2016-10-20 11:43:29 -0500 )edit

Why don't you add the if statement:

if(number==21)
{
   ROS_INFO("21!!!!!");
}

to your callback function? That way the variable would never need to leave the function. It would save you adding the while loop which is where your program is probably getting stuck.

Tyrone Nowell gravatar image Tyrone Nowell  ( 2016-10-20 11:46:52 -0500 )edit

Oh, this is because I want to test the thing to see how do I actually receive the variable from a topic. As I am currently doing a project in which my part is already done. What is left is integration of the projects.

lolz733 gravatar image lolz733  ( 2016-10-20 18:42:28 -0500 )edit

And the way integration is gonna work is by subscribing to topic. This way each group can just forcus on their own pkg and there no need for someone to integrate the project by creating a package to have all the Programmes of the Pkg from all the teams

lolz733 gravatar image lolz733  ( 2016-10-20 18:44:11 -0500 )edit

And I was able to get string variables but idk why it seems like I'm not able to get integer type variables . So do I store the msg->data into a integer variable that is a global variable ?

lolz733 gravatar image lolz733  ( 2016-10-20 18:51:16 -0500 )edit

compilation error though :(

lolz733 gravatar image lolz733  ( 2016-10-21 00:06:52 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-10-20 08:12:37 -0500

Seen: 276 times

Last updated: Oct 21 '16