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 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;
}