一、发布者
#include <ros/ros.h>
int main(int argc, char** argv)
{
ros::init(argc, argv, "publish_velocity");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise <>("", 1000);
while (ros::ok())
{
pub.publish();
ros::spinOnce();
}
return 0;
}
二、接收者
#include "ros/ros.h"
#include <sensor_msgs/LaserScan.h>
void cb(const sensor_msgs::LaserScan::ConstPtr &scan)
{
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("scan", 100, cb);
ros::spin();
return 0;
}
三、接收并发布
#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
#include <sensor_msgs/PointCloud2.h>
class SubsPub {
public:
void cb(const sensor_msgs::LaserScan::ConstPtr& scan);
SubsPub(ros::NodeHandle n) {
sub_ = n.subscribe("scan", 1, &SubsPub::cb, this);
pub_ = n.advertise<sensor_msgs::PointCloud2>("cloud", 10);
}
ros::Subscriber sub_;
ros::Publisher pub_;
};
void SubsPub::cb(const sensor_msgs::LaserScan::ConstPtr& scan) {
sensor_msgs::PointCloud2 msg;
pub_.publish(msg);
}
int main(int argc, char** argv) {
ros::init(argc, argv, "sp");
ros::NodeHandle n;
SubsPub sp(n);
ros::spin();
return 0;
}