I just note for the important line of code from the ROS wiki.
The code below is used to define publisher in ROS system.
Publisher
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 100);
chatter_put is Publisher object.
n.advertise
To publish message, we can use the following code:
msg.data = datatobepublish; // assign message for publish
chatter_put.publish(msg); // Call publish method of Publisher class
Subscriber
A subscriber must define callback function that will get called when a new message have arrived on the chatter topic.
Define callback function:
void chatterCallback(const std_msgs::StringConstPtr& msg)
{
ROS_INFO("Received [%s]",msg->data.c_str());
}
Define nodeHandle and call subscribe function
ros::NodeHandle n;
ros::Subscriber chatter_sub = n.subscribe("chatter", 100, chatterCallback);
The line above means that
nodeObject.subscribe("publisherTopicName",numberOfMsgInQue,callbackFunctionName);
Reference: http://www.ros.org/wiki/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29
ไม่มีความคิดเห็น:
แสดงความคิดเห็น