自定义msg
消息
Header header
int32 num
float64[] data
修改CMakeLists.txt
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
add_message_files(
FILES
test.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
)
修改package.xml
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
代码示例
#include <ros/ros.h>
#include <iostream>
#include "ros_learning/test.h"
using namespace std;
using namespace Eigen;
using namespace cv;
int main(int argc, char** argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<ros_learning::test>("chatting", 10);
ros::Rate loop_rate(10);
double arr[3] = {14, 15, 16};
std::vector<double> vi(arr, arr+3);
ros_learning::test input_msg;
input_msg.header.stamp = ros::Time::now();
input_msg.header.seq = 0;
input_msg.header.frame_id = "base_frame";
input_msg.num = 0;
input_msg.data = vi;
while(ros::ok())
{
input_msg.header.seq++;
input_msg.header.stamp = ros::Time::now();
for (auto & i : input_msg.data)
std::cout << i << " ";
std::cout << std::endl;
pub.publish(input_msg);
loop_rate.sleep();
}
}
是否使用仿真时间
$ rosparam get use_sim_time
true
$ rosparam set use_sim_time false