Ask Your Question
0

Modifing chatterCallback for a simple int and plot in multi-machine

asked 2020-08-21 15:52:09 -0500

VictorBarth gravatar image

Hi everyone, I'm new to ROS and C++ and I'm not actually understand the function chatterCallback. To learn about it I just remade the talker and listener (C++) using a uint32 called score (at data.msg) instead of string, And I'm trying to plot it in two different machines. I did the talker works nice, as in this code:

#include "ros/ros.h"
#include "firstplot/data.h"
#include <sstream>

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

  ros::NodeHandle n;

  ros::Publisher chatter_pub = n.advertise<firstplot::data>("num", 1000);
  ros::Rate loop_rate(10);

  int count = 0;
  while (ros::ok())
  {
    firstplot::data msg;
    msg.score = count;

    ROS_INFO("Count: %d", count);

    chatter_pub.publish(msg);

    ros::spinOnce();

    loop_rate.sleep();
    ++count;
  }
  return 0;
}

The main for subscriber is just:

int main(int argc, char **argv)
{    
  ros::init(argc, argv, "listener");
  ros::NodeHandle n;    
  ros::Subscriber sub = n.subscribe("num", 1000, chatterCallback);    
  ros::spin();    
  return 0;
}

But I'm really stuck for this chatterCallback, mostly because of the ConstPtr (that I didn't get whats for) and the '->' that I don't know what means.

The second question is: is it really necessary to use the listener to plot in another machine? I mean, I'm using a talker in a raspberry, and I want to plot this uint32 in my PC. I tried it, the topics are working, but rqt_plot dont

If anyone could explain for a really newbie programmer with examples, I would be grateful

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-08-22 10:02:06 -0500

fergs gravatar image

When creating a subscriber, you tell it what function to call with each new message received. Since ROS messages may be be very large, we want to pass by-reference rather than by-value. To accomplish this, ROS uses smart pointers (in particular boost::shared_ptr in ROS1), which are a modern C++ improvement over regular old pointers.

As for the Const part - see this other ROS Answers

edit flag offensive delete link more

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: 2020-08-21 15:52:09 -0500

Seen: 74 times

Last updated: Aug 22 '20