ROS学习笔记2编写简易publisher和subscriber

如何写一个publisher

Publishers需要23个参数:topic typetopic namequeue size.

minimal_publisher代码如下:

#include <ros/ros.h>

#include <std_msgs/Float64.h>

int main(int argc, char **argv) {

    ros::init(argc, argv, "minimal_publisher");

ros::NodeHandle n;

    ros::Publisher my_publisher_object = n.advertise<std_msgs::Float64>("topic1", 1);

    std_msgs::Float64 input_float;

    input_float.data = 0.0;

    while (ros::ok())

    {

        input_float.data = input_float.data + 0.001;

        my_publisher_object.publish(input_float);

    }

}

/*以上为代码*/

initros里的一个方法,NodeHandlePbulisher是两种ros类型。

ros::init是创建的一个node的最开始的一步,当这行代码运行,ROS就意识到这是一段node的代码

NodeHandle n(“minimal_nodes”)可以给n建立一个namespace.这样topic的名字前面将加上这一命名空间,编程 minimal_nodes/topic1

但是在运行的时候还是可以改变的

rosrun minimal_nodes minimal_publisher minimal_nodes/topic1:=topic1

代码中所用的关键点:

ROS headers

ros::int master建立联系,与graph建立联系,创建node名称与内存单位

ros::NodeHandle

ros::Publisher-publisher object-advertise

publisher object-publish

下面是subscriber的代码:

#include<ros/ros.h>

#include<std_msgs/Float64.h>

void myCallback(const std_msgs::Float64& message_holder)

{

  ROS_INFO("received value is: %f",message_holder.data);

}

int main(int argc, char **argv)

{

  ros::init(argc,argv,"minimal_subscriber");  

  ros::NodeHandle n;  

  ros::Subscriber my_subscriber_object= n.subscribe("topic1",1,myCallback);

  ros::spin();  

  

  return 0;  

}

subscribe需要3个参数:topic typetopic namecallback functions.

ppt1.27的链接

In this application all user callbacks will be called from within the ros::spin() call.  ros::spin() will not return until the node has been shutdown, either through a call to ros::shutdown() or a Ctrl-C.

spin()是轮询函数,自带while(ros::ok())结构在其内部,一旦运行无限次运行只能强制结束;

对应地,spinOnce()则是单次的函数。

当我们写了新的代码后,比如src文件夹下的minimal publisher.cpp,我们需要更新一下CMmakeLists.txt,使编译的时候将新文件编译。

此处对每个新cpp文件添加两个语句

第一个语句是

add_executable(minimal_publisher src/minimal_publisher.cpp) 括号中

第一个参数是所取的名字,第二个参数是cpp文件的位置

第二个语句是

target_link_libraries(minimal_publisher ${catkin_LIBRARIES}) 选择其它的库来连接,对应cpp一开头的include

默认都是连接catkin_LIBRARIES,这个库包含了所有的dependencies,比如roscpp

使用catkin_make编译的时候要在顶层的ws文件夹

如果想要只编译一个Package,那么使用以下指令

catkin_make -DCATKIN_WHITELIST_PACKAGES=”minimal_nodes”

whitelist的意思是优良者名单。

运行nodes的过程

首先roscore 开启各种ROS进程:启动master、启动parameter server、启动调试功能

然后rourun + package namadde +node name

    

可以通过调用rqt_graph包中的rqt_graph Node来观察当前运行的各种Node关系

rosrun rqt_graph rqt_graph

课程中有一个py写的新的创建package的工具cs_create_pkg,包含一系列指令,可以简化创建和编译一个新package的过程

创建:cs_create_pkg my_minimal_node roscpp std_msgs

修改CMakeLists.txt时 只需要添加仪表

cs_add_executable(minimal_publisher_3 src/minimal_publisher.cpp)

而不用写两条

debug工具:gdb

自己从零写一组Node并运行的流程

 创建package 编写cpp文件 修改CMakeList.txt Catkin_make -DCATKINMAKE  :

  1. catkin_create_pkg my_package roscpp std::msg

创建名称为my_packagePackage文件在src文件夹中

  1. my_package文件夹下的src文件夹中,新编辑cpp文件。

my_publisher.cpp my_subscriber.cpp

然后修改CMakeLists.txt.

add_executable(my_publisher src/minimal_publisher.cpp)  

add_executable(my_subscriber src/minimal_publisher.cpp)  

target_link_libraries(my_publisher ${catkin_LIBRARIES})

target_link_libraries(my_subscriber ${catkin_LIBRARIES})

  1. ws工程文件夹下进行编译。

catkin_make -DCATKIN_WHITELIST_PACKAGES=”my_package”

可以使用rostopic工具直接发布topic的值:

rostopic  pub  -r  10  vel_cmd   std_msgs/Float64  0.1

其中 -r 为参数,10为频率(此处10Hz),vel_cmd topic的名字,后面是类型,最后是发布的值。

原文地址:https://www.cnblogs.com/xuhaoforwards/p/8401193.html