ROS之自定义msg

     一、

配置步骤,建立文件过程参考古月居的书,修改xml文件和CMakeList文件参考下面链接。注意不使用  message_runtime,  而是使用 message_runtime ,CMakeList文件中取消注释即可。

        

在ROS中定义并使用自己的消息类型

二、自定义的msg如何使用

 

发布订阅-ROS自定义消息类型

把msg类的各个参数设好,然后一句     pub.publish(msg);   就OK了
1.新建一个ros功能包   
$cd ~/catkin_ws/src/
$catkin_create_pkg auto_charge_pub  roscpp std_msgs message_generation

编辑package.xml:
添加:
message_generation

message_runtime    

编辑CMakeLists.txt:
添加:
catkin_package(
  ...
  CATKIN_DEPENDS message_runtime ...
  ...)

add_message_files(
  FILES
  AutoChargeMsg.msg
)

generate_messages(
   DEPENDENCIES
   std_msgs
)

2.新建msg消息   
$cd catkin_ws/src/auto_charge_pub
$mkdir msg
在新建的msg文件夹中新建AutoChargeMsg.msg,文件中填入:
int32 A
int32 B
int32 C

3.创建发布和订阅节点源文件

4.功能包源码
(1)文件结构:  
CMakeLists.txt  include  msg  package.xml  src
(2)package.xml:


  auto_charge_pub
  0.0.0
  The auto_charge_pub package
  zhanghu
  TODO
   
  catkin
  message_generation
  roscpp
  std_msgs
  roscpp
  std_msgs

  message_generation
  message_runtime

  

  


(3)CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(auto_charge_pub)

find_package(catkin REQUIRED COMPONENTS
  message_generation
  roscpp
  std_msgs
)
add_message_files(  
   FILES
   AutoChargeMsg.msg
 )
generate_messages(
   DEPENDENCIES
   std_msgs
 )
catkin_package(
  CATKIN_DEPENDS message_runtime roscpp std_msgs
)
include_directories(
  ${catkin_INCLUDE_DIRS}
)
add_executable(auto_charge_pub src/auto_charge_pub.cpp)
add_executable(auto_charge_sub src/auto_charge_sub.cpp)
target_link_libraries(auto_charge_pub ${catkin_LIBRARIES})
target_link_libraries(auto_charge_sub ${catkin_LIBRARIES})

(4)AutoChargeMsg.msg:
int32 A
int32 B
int32 C

(5)auto_charge_pub.cpp:
#include "ros/ros.h"
#include "auto_charge_pub/AutoChargeMsg.h"
#include 

int main(int argc, char **argv)
{
  ros::init(argc, argv, "example_charge_pub");
  ros::NodeHandle n;
  ros::Publisher pub = n.advertise("chargedata", 1000);
  ros::Rate loop_rate(10);
  while (ros::ok())
  {
    auto_charge_pub::AutoChargeMsg msg;
    msg.A = 1;
    msg.B = 2;
    msg.C = 3;
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
  }
  return 0;
}

(6)auto_charge_sub.cpp:
#include "ros/ros.h"
#include "auto_charge_pub/AutoChargeMsg.h"

void messageCallback(const auto_charge_pub::AutoChargeMsg::ConstPtr& msg)
{
  ROS_INFO("I heard: [%d] [%d] [%d]", msg->A, msg->B, msg->C);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "example_charge_sub");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("chargedata", 1000, messageCallback);
  ros::spin();
  return 0;
}

5.编译与运行
$cd ~/catkin_ws/
$catkin_make

$roscore
$rosrun auto_charge_pub auto_charge_pub
$rosrun auto_charge_pub auto_charge_sub

结果输出
[ INFO] [1496804922.698230235]: I heard: [1] [2] [3]
[ INFO] [1496804922.797224084]: I heard: [1] [2] [3]
[ INFO] [1496804922.896686623]: I heard: [1] [2] [3]
[ INFO] [1496804922.996713962]: I heard: [1] [2] [3]
[ INFO] [1496804923.107267357]: I heard: [1] [2] [3]
[ INFO] [1496804923.197747661]: I heard: [1] [2] [3]
[ INFO] [1496804923.296960871]: I heard: [1] [2] [3]
[ INFO] [1496804923.396757136]: I heard: [1] [2] [3]
[ INFO] [1496804923.496417082]: I heard: [1] [2] [3]
[ INFO] [1496804923.597160904]: I heard: [1] [2] [3]
[ INFO] [1496804923.705754664]: I heard: [1] [2] [3]
...

转载请注明出处:爱学习的长河落日 http://www.cnblogs.com/hiram-zhang/ 商业用途获取版权请发邮件至:[email protected]

在两个功能包里分别发布和订阅自定义msg,两个功能包都需要配置CMakeList文件和xml文件。

三、

ROS入门 创建msg(消息)和svr(服务)遇到的问题

The manifest (with format version 2) must not contain the following tags: run_depend

的警告,这个的主要解决方法是使用catkin方式时在package.xml文件中将教程中添加的两行语句

 message_generation
  message_runtime

要改成

  message_generation

  message_runtime

或者

message_generation
message_runtime

这样就得以解决问题

 

四、

Could not find messages which '/home/flying/catkin_ws/src/beginner_tutorials/msg/Num.msg' depends

问题:Could not find messages which
  '/home/flying/catkin_ws/src/beginner_tutorials/msg/Num.msg' depends on.
  Did you forget to specify generate_messages(DEPENDENCIES ...)?

 

解决

Num.msg

中的内容应该为

int64 num

如果有其他的出错 先删除掉其他的 只保留这个

你可能感兴趣的:(Ubuntu/ROS)