zoukankan      html  css  js  c++  java
  • ros_cpp_topic

    在工作空间下创建的包,需要编译的c++的代码要放在src中,直接运行的python的代码要放在scripts中,

    在之前的python代码都是放在src中的,但也是可以运行的

    创建一个包catkin_create_pkg learning_com roscpp rospy std_msgs

    然后在目录下编译一下catkin_make ,设置环境变量 source ~/catkin_ws/devel/setup.bash

    创建一个发布者话题

    talker.cpp

     1 #include <sstream>
     2 #include "ros/ros.h"                                                              
     3 #include "std_msgs/String.h"
     4 
     5 int main(int argc, char **argv)      
     6 {
     7     // ROS节点初始化
     8     ros::init(argc, argv, "talker");     //前两个参数是命令行或者launch文件输入的参数, 第三个参数定义了Publisher节点的名称
     9 
    10     // 创建节点句柄 方便使用和管理
    11     ros::NodeHandle n; 
    12 
    13     // 创建一个Publisher,发布名为chatter的topic,消息类型为std_msgs::String
    14     ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);                       
    15 
    16     // 设置循环的频率
    17     ros::Rate loop_rate(10);
    18 
    19     int count = 0; 
    20     while (ros::ok())
    21     {  
    22         // 初始化std_msgs::String类型的消息
    23         std_msgs::String msg;
    24         std::stringstream ss;
    25         ss << "hello world " << count;
    26         msg.data = ss.str();
    27 
    28         // 发布消息
    29         ROS_INFO("%s", msg.data.c_str());
    30         chatter_pub.publish(msg);
    31 
    32         // 循环等待回调函数
    33         ros::spinOnce();
    34 
    35         // 按照循环频率延时
    36         loop_rate.sleep();
    37         ++count;
    38     }  
    39 
    40     return 0; 
    41 }

    创建订阅者话题

    listener.cpp

     1 #include "ros/ros.h"
     2 #include "std_msgs/String.h"
     3 
     4 // 接收到订阅的消息后,会进入消息回调函数
     5 void chatterCallback(const std_msgs::String::ConstPtr& msg)
     6 {
     7     // 将接收到的消息打印出来
     8     ROS_INFO("I heard: [%s]", msg->data.c_str());
     9 }
    10 
    11 int main(int argc, char **argv)
    12 {
    13     // 初始化ROS节点
    14     ros::init(argc, argv, "listener");
    15 
    16     // 创建节点句柄
    17     ros::NodeHandle n; 
    18 
    19     // 创建一个Subscriber,订阅名为chatter的topic,注册回调函数chatterCallback
    20     ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
    21 
    22     // 循环等待回调函数
    23     ros::spin();
    24 
    25     return 0; 
    26 }

    通过CMakeLists.txt文件进行配置。在该文件中找到以下配置,去掉注释稍作修改

     1 include_directories(
     2  include
     3   ${catkin_INCLUDE_DIRS}
     4 )
     5 add_executable(talker src/talker.cpp)
     6 target_link_libraries(talker ${catkin_LIBRARIES})
     7 add_dependencies(talker ${PROJECT_NAME}_generate_messages_cpp)
     8 
     9 add_executable(listener src/listener.cpp)
    10 target_link_libraries(listener ${catkin_LIBRARIES})
    11 add_dependencies(talker ${PROJECT_NAME}_generate_messages_cpp)

    1)include_directories

      用于设置头文件的相对路径,全局路径默认是功能包的所在目录,比如功能包的头文件一般会放到功能包根目录下的inclue文件夹中,所以此处需要添加该文件夹。

    此外该配置项还包含ROS catkin 编译器默认包含的其他头文件路径,比如ROS默认安装路径,Linux系统路径等。

    2)add_executable

      用于设置需要编译的代码和生成的可执行文件,第一个参数为期望生成的可执行文件的名称,后边的参数为参与编译的源代码文件,如果需要多个代码文件,则可以在后边依次列出,中间使用空格进行分隔

    3)target_link_libraries

      用于设置连接库,很多功能需要使用系统或者第三方的库函数,通过该选项可以配置执行文件链接的库

    4)add_dependencies

      用于设置依赖,很多应用中,我们需要定义语言无关的消息类型,消息类型会在编译过程中产生相应语言的代码,如果编译的可执行文件依赖这些动态生成的代码,则需要add_dependencies添加${PROJECT_NAME}_generate_messgae_cpp配置,即该功能包动态产生的消息代码,

    配置完成后在根目录下进行编译,

    在运行节点前要在终端中设置环境变量,否则无法找到功能呢个包最终编译生成的可执行文件。

    在不同的终端运行

    roscore

    rosrun learning_com talker

    rosrun learning_com listener

    miao@openlib:~$ rosrun learning_com talker 
    [ INFO] [1572526329.293468472]: hello world 0
    [ INFO] [1572526329.393635172]: hello world 1
    [ INFO] [1572526329.493622362]: hello world 2
    [ INFO] [1572526329.593622092]: hello world 3
    [ INFO] [1572526329.693623840]: hello world 4
    [ INFO] [1572526329.793585646]: hello world 5
    [ INFO] [1572526329.893624448]: hello world 6
    [ INFO] [1572526329.993626995]: hello world 7
    [ INFO] [1572526330.093623654]: hello world 8
    [ INFO] [1572526330.193635566]: hello world 9
    [ INFO] [1572526330.293624781]: hello world 10
    [ INFO] [1572526330.393627731]: hello world 11
    [ INFO] [1572526330.493625127]: hello world 12
    [ INFO] [1572526330.593635168]: hello world 13
    [ INFO] [1572526330.693635656]: hello world 14
    [ INFO] [1572526330.793628537]: hello world 15
    [ INFO] [1572526330.893623313]: hello world 16
    [ INFO] [1572526330.993636344]: hello world 17
    [ INFO] [1572526331.093629914]: hello world 18
    [ INFO] [1572526331.193626053]: hello world 19
    miao@openlib:~$ rosrun learning_com listener 
    [ INFO] [1572526329.594372987]: I heard: [hello world 3]
    [ INFO] [1572526329.694221074]: I heard: [hello world 4]
    [ INFO] [1572526329.794205081]: I heard: [hello world 5]
    [ INFO] [1572526329.894197325]: I heard: [hello world 6]
    [ INFO] [1572526329.994201157]: I heard: [hello world 7]
    [ INFO] [1572526330.094224113]: I heard: [hello world 8]
    [ INFO] [1572526330.194271534]: I heard: [hello world 9]
    [ INFO] [1572526330.294279250]: I heard: [hello world 10]
    [ INFO] [1572526330.394264930]: I heard: [hello world 11]
    [ INFO] [1572526330.494268337]: I heard: [hello world 12]
    [ INFO] [1572526330.594255727]: I heard: [hello world 13]
    [ INFO] [1572526330.694248934]: I heard: [hello world 14]
    [ INFO] [1572526330.794272888]: I heard: [hello world 15]
    [ INFO] [1572526330.894278416]: I heard: [hello world 16]
    [ INFO] [1572526330.994254203]: I heard: [hello world 17]
    [ INFO] [1572526331.094291986]: I heard: [hello world 18]
    [ INFO] [1572526331.194205766]: I heard: [hello world 19]
    [ INFO] [1572526331.294199813]: I heard: [hello world 20]
    [ INFO] [1572526331.394246518]: I heard: [hello world 21]
    [ INFO] [1572526331.494255522]: I heard: [hello world 22]
  • 相关阅读:
    [node.js学习]为node.js写的一个操作mysql的类
    极光IM简单接入步骤
    windows自带的netsh 端口转发
    nodejs 做的带管理后台的东东,主要学习到 ....我忘了学到什么了
    利用来JS控制页面控件显示和隐藏有两种方法
    phpstudy 出现You don't have permission to access / on this server.
    禁用input自动填充
    一般充值的流程
    jq传输json字符串
    ECSHOP更改后台顶部图片
  • 原文地址:https://www.cnblogs.com/miaorn/p/11774154.html
Copyright © 2011-2022 走看看