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

Using Num.msg from Beginner tutorial

asked 2015-11-19 10:10:19 -0600

arttp2 gravatar image

updated 2015-11-19 10:24:17 -0600

Please modify or guide me to where I can learn to modify the talker.cpp and listener.cpp given in the beginner_tutorials to send the message Num.msg defined in the same series of tutorials. This is to help understand how to use complex msgs later on.

I replaced the example code by the following lines wherever I felt was necessary but this is not getting compiled.

talker.cpp

#include "beginner_tutorials/Num.h"
ros::Publisher chatter_pub = n.advertise<beginner_tutorials::Num>("chatter", 1000);
beginner_tutorials::Num num;
// std::stringstream ss;
// ss <<"hello world" << count;
num = 123456789;
ROS_INFO("%d",num);
chatter_pub.publish(num);

listener.cpp

#include "beginner_tutorials/Num.h"
void chatterCallback(const beginner_tutorials::ConstPtr &num)
{
    ROS_INFO("I heard: [%d]",  num->data);
}

Also please suggest a way to understand the Num.h, std_msgs/String.h file generated by ROS, if it is to be understood at the first place to use ROS.

edit retag flag offensive close merge delete

Comments

1

What is the error message?

dornhege gravatar image dornhege  ( 2015-11-19 10:21:17 -0600 )edit

In function ‘int main(int, char**)’: /home/catkin_ws/src/beginner_tutorials/src/talker.cpp:25:7: error: no match for ‘operator=’ (operand types are ‘beginner_tutorials::Num’ and ‘int’) num = 123456789;

one of the error messages, the first one.

arttp2 gravatar image arttp2  ( 2015-11-19 10:24:58 -0600 )edit

Thanks for editing my question.

arttp2 gravatar image arttp2  ( 2015-11-19 10:27:15 -0600 )edit

2 Answers

Sort by » oldest newest most voted
2

answered 2015-11-19 11:01:32 -0600

dornhege gravatar image

The type of the message is beginner_tutorials::Num, not int. However, it probably has a member of type in, usually named data or something. You need to set that instead of the message directly.

Usually you refer to the message definition file for that and not the ROS message headers. These can be read and understood, but are not meant to be.

edit flag offensive delete link more

Comments

1

In other words, the line num = 123456789; should probably be changed to something like num.data = 123456789;

jarvisschultz gravatar image jarvisschultz  ( 2015-11-19 11:04:57 -0600 )edit

after adding num.data, it again says no member such as data (because .data may be defined for the String message) , so where can I find the members for the message, and a comprehensive usage details for the custom msg.

arttp2 gravatar image arttp2  ( 2015-11-19 11:19:23 -0600 )edit
1

As @dornhege said, you need to be looking at the "Num.msg" file to see the message definition. E.g. see how the std_msgs/String has a single field of type "string" named "data". You need to find the equivalent file for your message.

jarvisschultz gravatar image jarvisschultz  ( 2015-11-19 13:06:55 -0600 )edit
2

If your workspace is setup correctly, you should be able to see the message definition at the command line using rosmsg show. E.g. rosmsg show std_msgs/String

jarvisschultz gravatar image jarvisschultz  ( 2015-11-19 13:07:56 -0600 )edit
0

answered 2015-11-19 14:10:07 -0600

arttp2 gravatar image

updated 2015-11-19 14:13:32 -0600

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

Original Tutorial: Wiki: 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
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-11-19 10:10:19 -0600

Seen: 1,023 times

Last updated: Nov 19 '15