构建功能包
代码:
#include <ros/ros.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <sstream>
#include <string.h>
typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
PointCloudT::Ptr cloud(new PointCloudT);
//std::string world_frame_id;
std::string frame_id;
std::string path;
void cloud_cb (const PointCloudT::ConstPtr& callback_cloud)
{
*cloud = *callback_cloud;
frame_id = cloud->header.frame_id;
std::stringstream ss;
std::string s1;
ss<<ros::Time::now();
s1=ss.str();
// cloud saving:
std::string file_name=path+"cloud_"+frame_id.substr(0, frame_id.length())+"_"+s1+".pcd";
std::cout<<file_name<<std::endl;
pcl::io::savePCDFileBinary(file_name,*cloud);
}
int main (int argc, char** argv)
{
ros::init(argc, argv, "save_cloud");
ros::NodeHandle nh("~");
// std::cout << "node successfully created!" << std::endl;
//Read some parameters from launch file:
std::string pointcloud_topic = "/camera/depth_registered/points";
path = "/home/suyang/pointtest/";
/* nh.param("pointcloud_topic", pointcloud_topic, std::string("/camera/depth_registered/points"));
nh.param("world_frame_id", world_frame_id, std::string("/odom"));
nh.param("path", path, std::string("/home/ubuntu1/pointtest/"));
std::cout << "Read some parameters from launch file." << std::endl;*/
// Subscribers:
ros::Subscriber sub = nh.subscribe(pointcloud_topic, 1, cloud_cb);
std::cout<<"receive messages successfully!"<<std::endl;
ros::spin();
return 0;
}
1.#include<sstream>
stringstream 对象用于输入一行字符串
std::stringstream ss;
ss<<ros::Time::now();
另:
string str= "hello world I am very happy!";
stringstream sstream(str); //sstream<<
while (sstream)
{
string substr;
sstream>>substr;
cout << substr << endl; //也可vec.push_back(substr);
}
类型转换:
stringstream stream;
string result;
int i = 1000;
stream << i; //将int输入流
stream >> result; //从stream中抽取前面插入的int值
cout << result << endl; // cout the string "1000"
另 to_string():
s1=to_string(10.5); //double到string
s2=to_string(123); //int到string
2.typedef
C 语言提供了 typedef 关键字,用它来为类型取个新名字。下面的实例为单字节数字定义了一个术语 BYTE:
typedef unsigned char BYTE;
在这个类型定义之后,标识符 BYTE 可作为类型 unsigned char 的缩写,例如:
BYTE b1, b2;
3..substr()
截取字符串:frame_id.substr(0, frame_id.length())
报错:
[rosrun] Couldn't find executable named save_cloud below /home/carl/my_savecloud_tutorial/src/my_savecloud_tutorial
找到该文件,勾选执行。
4.ros::NodeHandle nh加("~")
一.使用节点句柄nh("~")
ros::NodeHandle nh("~");
ros::Publisher joint_pub = nh.advertise<sensor_msgs::JointState>(“test_joint_states”, 1);
则发布的话题名字为
节点名字+话题名字
/replay_jnt_path_node/test_joint_states
二.使用节点句柄nh
ros::NodeHandle nh;
ros::Publisher joint_pub = nh.advertise<sensor_msgs::JointState>(“test_joint_states”, 1);
则发布的话题名字为:话题名字
/test_joint_states