Anyone can help? i made a simple node where it publish Int8 and subsribe to that Int8 but it didnt work
this is the talker and listener i modified . when i compile. it says my stdmsgs::Int8 number is not derived from 'const std::istreamiterator..' 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;
}
Asked by lolz733 on 2016-10-20 08:12:37 UTC
Answers
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/WritingPublisherSubscriber%28c%2B%2B%29
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;
}
Asked by Tyrone Nowell on 2016-10-20 10:01:21 UTC
Comments
Do I use ROS::spinOnce() or ROS::spin() ? What's the difference ?
Asked by lolz733 on 2016-10-20 11:20:02 UTC
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
Asked by lolz733 on 2016-10-20 11:22:49 UTC
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.
Asked by Tyrone Nowell on 2016-10-20 11:43:29 UTC
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.
Asked by Tyrone Nowell on 2016-10-20 11:46:52 UTC
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.
Asked by lolz733 on 2016-10-20 18:42:28 UTC
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
Asked by lolz733 on 2016-10-20 18:44:11 UTC
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 ?
Asked by lolz733 on 2016-10-20 18:51:16 UTC
compilation error though :(
Asked by lolz733 on 2016-10-21 00:06:52 UTC
i changed the
// Declare number here
std_msgs::Int8 number;
to
int number;
and it worked if i rosrun the executable directly but when i roslaunch the two files it doesnt work.why is it like that?
Asked by lolz733 on 2016-10-21 00:11:58 UTC
It's ok I got it to work I forgot to put on the launch file
Asked by lolz733 on 2016-10-21 01:02:41 UTC
Comments