Skip to content

Instantly share code, notes, and snippets.

@surinoel
Last active October 23, 2019 02:47
Show Gist options
  • Save surinoel/13dbc2e686d29cb4e0825abb1b036da2 to your computer and use it in GitHub Desktop.
Save surinoel/13dbc2e686d29cb4e0825abb1b036da2 to your computer and use it in GitHub Desktop.
#include "ros/ros.h"
#include "ros_tutorials_topic/MsgTutorial.h"
int main(int argc, char **argv) {
ros::init(argc, argv, "topic_publisher");
ros::NodeHandle nh;
// 메세지 타입을 템플릿 타입으로 입력
ros::Publisher ros_tutorial_pub =
nh.advertise<ros_tutorials_topic::MsgTutorial>("ros_tutorial_msg", 100);
ros::Rate loop_rate(10);
ros_tutorials_topic::MsgTutorial msg;
int count = 0;
while(ros::ok())
{
msg.stamp = ros::Time::now();
msg.data = count;
ROS_INFO("send msg = %d", msg.stamp.sec);
ROS_INFO("send msg = %d", msg.stamp.nsec);
ROS_INFO("send msg = %d", msg.data);
ros_tutorial_pub.publish(msg);
loop_rate.sleep();
++count;
}
return 0;
}
#include "ros/ros.h"
#include "ros_tutorials_topic/MsgTutorial.h"
void msgCallback(const ros_tutorials_topic::MsgTutorial::ConstPtr& msg) {
ROS_INFO("receive data = %d", msg->stamp.sec);
ROS_INFO("receive data = %d", msg->stamp.nsec);
ROS_INFO("receive data = %d", msg->data);
}
int main(int argc, char **argv) {
ros::init(argc, argv, "topic_subscriber");
ros::NodeHandle nh;
ros::Subscriber ros_tutorial_sub =
nh.subscribe("ros_tutorial_msg", 100, msgCallback);
ros::spin();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment