ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Thanks, the code works as intended. It still gives few warnings at compilation.

Original Tutorial: link text

listener.cpp

#include "ros/ros.h"
// #include "std_msgs/String.h"
#include "beginner_tutorials/Num.h"
void chatterCallback(const beginner_tutorials::Num::ConstPtr &num)
{
ROS_INFO("I heard: [%d]",  num->num);
}
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 "beginner_tutorials/Num.h"

int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker");
  ros::NodeHandle n;
      ros::Publisher chatter_pub = n.advertise<beginner_tutorials::Num>("chatter", 1000);
  ros::Rate loop_rate(10);
  int count = 0;
  while(ros::ok())
  {
      beginner_tutorials::Num num;
    // std::stringstream ss;
    // ss <<"hello world" << count;
    num.num = 123456789;
        ROS_INFO("%d",num);
    chatter_pub.publish(num);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
   }
   return 0;
}

Num.msg

int64 num

Thanks, the code works as intended. It still gives few warnings at compilation.

Original Tutorial: link textWiki: Writing Publisher and Subscriber

listener.cpp

#include "ros/ros.h"
// #include "std_msgs/String.h"
#include "beginner_tutorials/Num.h"
void chatterCallback(const beginner_tutorials::Num::ConstPtr &num)
{
ROS_INFO("I heard: [%d]",  num->num);
}
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 "beginner_tutorials/Num.h"

int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker");
  ros::NodeHandle n;
      ros::Publisher chatter_pub = n.advertise<beginner_tutorials::Num>("chatter", 1000);
  ros::Rate loop_rate(10);
  int count = 0;
  while(ros::ok())
  {
      beginner_tutorials::Num num;
    // std::stringstream ss;
    // ss <<"hello world" << count;
    num.num = 123456789;
        ROS_INFO("%d",num);
    chatter_pub.publish(num);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
   }
   return 0;
}

Num.msg

int64 num