-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_subscriber.cpp
More file actions
50 lines (43 loc) · 1.78 KB
/
simple_subscriber.cpp
File metadata and controls
50 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* This program does the following
* 1. Demonstrates how to create a publisher using the NodeHandle
*/
// Include ROS header file
#include <ros/ros.h>
// Include the header file for the message content
#include <std_msgs/String.h>
// Other header files
#include <iostream>
using namespace std;
// Create a callback function
/*
* This function will be called whenever a message is published on a given topic (with a constant pointer
* to the message as a parameter)
*/
void subscriber_callback(const std_msgs::String::ConstPtr& msg) {
// Get the contents of the message
string st = msg->data; // Get the data into a string
ROS_INFO_STREAM("Received a message: '" << st << "'");
}
int main(int argc, char **argv) {
// Initialize the node
ros::init(argc, argv, "simple_cpp_subscriber");
// Initialize the NodeHandle
ros::NodeHandle nh("~");
// Create a subscriber request
/*
* Here, the nodehandle tells the master to route the messages from a topic to this node. A handling function
* has to deal with the messages. The call returns an object of the Subscriber class
* First parameter is the topic name (it will be scoped inside the node name)
* Second parameter is the queue size
* Third parameter is the callback function
* Note that the actual callback will happen only in the spin function
*
* Link: http://docs.ros.org/en/latest/api/roscpp/html/classros_1_1NodeHandle.html#a317fe4c05919e0bf3fb5162ccb2f7c28
* Link (Subscriber class): http://docs.ros.org/en/latest/api/roscpp/html/classros_1_1Subscriber.html
*/
ros::Subscriber msg_subs = nh.subscribe("subs_str", 1000, subscriber_callback);
// Spin the node (call callbacks when messages are received, etc.)
ros::spin();
return 0;
}