ROS 自定义msg进行收发

1.建工作空间
2.建功能包
3.建msg文件
4.建收发相关cpp
5.改配置文件
6.实践

1.建工作空间

mkdir -p new_ws/src
cd new_ws/src
catkin_init_workspace

2.建功能包

catkin_create_pkg mymsgtest std_msgs roscpp

3.建msg文件

cd msgtest/
mkdir msg
cd msg/
touch mymsg.msg
gedit mymsg.msg

msg文件中写下面内容

uint8 dataA
uint8[2] dataB

4.建收发相关cpp

cd ..
cd src/
touch pub.cpp
touch sub.cpp

pub.cpp内容

#include "ros/ros.h"
#include "mymsgtest/mymsg.h" //mymsgtest是功能包名,mymsg是msg文件名,编译成功后会在devel/include自动生成一个头文件

int main(int argc, char **argv)
{
  ros::init(argc, argv, "msgpub");//这个名随意,和cmakelist中节点名一致会好点
  ros::NodeHandle nh;//创建一个句柄

  ros::Publisher chatter_pub = nh.advertise<mymsgtest::mymsg>("mymsg", 100);
  //新建一个pub对象,<>中是自定义的msg文件,后面的mymsg很重要,pub和sub中这个名字要一致

  ros::Rate loop_rate(10);
  while (ros::ok())
  {
    mymsgtest::mymsg msg;//自定义的msg类的一个新变量
    msg.dataA = 0xFE;
    msg.dataB[0]=0x12;
    msg.dataB[1]=0x14;

    chatter_pub.publish(msg);//那个pub对象发送
    ROS_INFO("Publish Person Info: name:%d age:%d  sex:%d",
              msg.dataA,msg.dataB[0],msg.dataB[1]);//ROS的printf
    ros::spinOnce();//循环刷新

    loop_rate.sleep();//等睡完
  }

  return 0;
}

sub.cpp内容

#include "ros/ros.h"
#include "mymsgtest/mymsg.h"

void chatterCallback(const mymsgtest::mymsg::ConstPtr& msg)//这里msg前面是自定义的那一套
{
  ROS_INFO("I heard: [%d] [%d] [%d]", msg->dataA,msg->dataB[0],msg->dataB[1]);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "mysub");
  ros::NodeHandle nh;

  ros::Subscriber sub = nh.subscribe("mymsg", 1000, chatterCallback);//新建一个sub,mymsg要一致,chatterCallback是回调函数名

  ros::spin();

  return 0;
}

5.改配置文件
CMakelists.txt

find_package(catkin REQUIRED COMPONENTS
  std_msgs roscpp//这个是建功能包时自己加的
  message_generation//这个是多加的
)
//下面这些部分都要单独加上
 add_message_files(
   FILES
   mymsg.msg//msg文件名
 )
 generate_messages(
   DEPENDENCIES
   std_msgs  
 )

add_executable(msgpub src/pub.cpp)//msgpub是自定义节点名,pub.cpp是文件名
target_link_libraries(msgpub ${catkin_LIBRARIES})//msgpub是节点名
add_executable(msgsub src/sub.cpp)//msgsub是自定义节点名,sub.cpp是文件名
target_link_libraries(msgsub ${catkin_LIBRARIES})//msgsub是节点名

package.xml

<build_depend>message_generation</build_depend>
 <exec_depend>message_runtime</exec_depend>
  //新版的ROS用exec_depend可能报错,要改成run_depend

6.实践

//进入工作空间文件夹
catkin_make
rosocre
//再开两个工作台,分别运行下面两个命令
rosrun mymsgtest msgpub
rosrun mymsgtest msgsub

效果是pub发出去的东西,sub能够接到。
msgpub发的

[ INFO] [1596034677.353663569]: Publish Person Info: name:254 age:18  sex:20
[ INFO] [1596034677.453849881]: Publish Person Info: name:254 age:18  sex:20
[ INFO] [1596034677.553874647]: Publish Person Info: name:254 age:18  sex:20
[ INFO] [1596034677.653787901]: Publish Person Info: name:254 age:18  sex:20
[ INFO] [1596034677.753832777]: Publish Person Info: name:254 age:18  sex:20
[ INFO] [1596034677.853840520]: Publish Person Info: name:254 age:18  sex:20
[ INFO] [1596034677.953950262]: Publish Person Info: name:254 age:18  sex:20
[ INFO] [1596034678.053926643]: Publish Person Info: name:254 age:18  sex:20

msgsub接的

[ INFO] [1596034690.654220292]: I heard: [254] [18] [20]
[ INFO] [1596034690.754144889]: I heard: [254] [18] [20]
[ INFO] [1596034690.854182586]: I heard: [254] [18] [20]
[ INFO] [1596034690.954339156]: I heard: [254] [18] [20]
[ INFO] [1596034691.054177678]: I heard: [254] [18] [20]
[ INFO] [1596034691.154200954]: I heard: [254] [18] [20]
[ INFO] [1596034691.254146588]: I heard: [254] [18] [20]

你可能感兴趣的:(ROS)