日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

ROS基本概念 文件系统 创建ROS软件包 ROS中的一些命令

發(fā)布時間:2025/3/12 windows 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ROS基本概念 文件系统 创建ROS软件包 ROS中的一些命令 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

ROS基本概念 文件系統(tǒng) 創(chuàng)建ROS軟件包 ROS中的一些命令

    • ROS是什么
    • ROS文件系統(tǒng)
      • 文件系統(tǒng)工具:rospack、roscd、rosls
    • 創(chuàng)建ROS 軟件包
      • catkin是什么
      • 創(chuàng)建和構建一個catkin工作空間:
      • catkin軟件包的結(jié)構
      • catkin工作空間中存放軟件包的結(jié)構
      • 創(chuàng)建catkin軟件包-catkin_create_pkg
      • 構建一個catkin工作區(qū)并生效配置文件
      • package.xml文件內(nèi)容
    • ROS一些基本概念
      • 節(jié)點:
      • 客戶端庫:
      • roscore:
      • rosnode:獲取節(jié)點信息的ROS工具
      • rosrun:運行給定的軟件包中的節(jié)點
      • ROS話題:節(jié)點之間是通過一個ROS話題來相互通信的
      • rqt_graph:顯示當前運行的節(jié)點和話題
      • rostopic:獲取ROS話題的信息
      • rostopic echo:顯示在某個話題上發(fā)布的數(shù)據(jù)
      • rostopic list:列出當前已被訂閱和發(fā)布的所有話題。
      • ROS消息:話題的通信是通過節(jié)點間發(fā)送ROS消息實現(xiàn)的
      • rostopic type:查看所發(fā)布話題的消息類型
      • rostopic pub:把數(shù)據(jù)發(fā)布到當前某個正在廣播的話題上
      • rostopic hz:報告數(shù)據(jù)發(fā)布的速率
      • rqt_plot:在滾動時間圖上顯示發(fā)布到某個話題上的數(shù)據(jù)
      • ROS服務:節(jié)點之間通訊的另一種方式
      • rosservice:有許多可用于服務的命令
      • rosservice list:顯示節(jié)點提供的服務
      • rosservice type:查看服務的類型
      • rosservice call:調(diào)用服務
      • rosparam:在ROS參數(shù)服務器上存儲和操作數(shù)據(jù)
      • rosparam list:查看參數(shù)服務器上都有哪些參數(shù)
      • rosparam set:改變參數(shù)服務器上參數(shù)的值
      • rosparam get:查看參數(shù)服務器上參數(shù)的值
      • rqt_console:連接ROS的日志框架,顯示節(jié)點的輸出信息
      • rqt_logger_level:在節(jié)點運行時改變輸出信息的詳細級別
      • roslaunch:啟動定義在launch(啟動)文件中的節(jié)點
      • launch XML文件的內(nèi)容
      • 使用roslaunch:運行l(wèi)aunch文件

ROS是什么

中間件,連接了真正的操作系統(tǒng)和程序,提供了類似操作系統(tǒng)的功能。

它提供了操作系統(tǒng)應有的服務,包括硬件抽象,底層設備控制,常用函數(shù)的實現(xiàn),進程間消息傳遞,以及包管理。

ROS包括:框架+工具+功能+社區(qū)

框架:分布式、進程管理、進程間通信

ros采用分布式架構,可以同時運行多個進程,每個進程可以單獨設計,并且組合起來。

ros用節(jié)點Node代表進程。

機器人控制是一個Node,激光雷達laser是一個node。ros提供了一套框架管理這些node,并且提供他們之間相互通信的橋梁。

分布式架構,擴展性好,軟件復用率高。如果換一個高級激光雷達,只用修改節(jié)點,不需要修改通信。

工具:仿真、數(shù)據(jù)可視化、圖形界面、數(shù)據(jù)記錄

提供一些工具 比如:Gazebo、Rviz等

功能:控制、規(guī)劃、視覺、建圖

有一些功能包

社區(qū):軟件包管理、文檔、教程

ROS文件系統(tǒng)

軟件包(Packages):包是ROS代碼的軟件組織單元,每個軟件包都可以包含程序庫、可執(zhí)行文件、腳本或其他構件。

Manifests (package.xml): 清單(Manifest)是對軟件包的描述。它用于定義軟件包之間的依賴關系,并記錄有關軟件包的元信息,如版本、維護者、許可證等。

文件系統(tǒng)工具:rospack、roscd、rosls

1.rospack

rospack獲取軟件包的有關信息,rospack find,可以返回軟件包的所在路徑。

用法:

$ rospack find [package_name]

例子:

jym@ubuntu:~$ rospack find roscpp /opt/ros/noetic/share/roscpp

2.roscd

roscd接切換目錄到某個軟件包或者軟件包集當中。

例子:

jym@ubuntu:~$ roscd roscpp jym@ubuntu:/opt/ros/noetic/share/roscpp$

roscd只能切換到那些路徑已經(jīng)包含在ROS_PACKAGE_PATH環(huán)境變量中的軟件包。

使用echo $ROS_PACKAGE_PATH查看查看ROS_PACKAGE_PATH中包含的路徑。

jym@ubuntu:~$ echo $ROS_PACKAGE_PATH /opt/ros/noetic/share

打開這個文件夾,可以找到這些包。

roscd也可以切換到一個軟件包或軟件包集的子目錄中。

jym@ubuntu:~$ roscd roscpp/cmake jym@ubuntu:/opt/ros/noetic/share/roscpp/cmake$ pwd /opt/ros/noetic/share/roscpp/cmake

3.rosls

允許直接按軟件包的名稱執(zhí)行 ls 命令(而不必輸入絕對路徑)。

jym@ubuntu:~$ rosls roscpp_tutorials cmake launch package.xml srv

創(chuàng)建ROS 軟件包

catkin是什么

代碼變成可執(zhí)行文件,叫做編譯(compile);先編譯這個,還是先編譯那個(即編譯的安排),叫做構建(build)。

catkin是ros定制的編譯構建系統(tǒng)。

ros代碼都放在catkin workspace中,這個工作空間需要通過指令catkin_make創(chuàng)建。

創(chuàng)建和構建一個catkin工作空間:

jym@ubuntu:~$ source /opt/ros/noetic/setup.bash jym@ubuntu:~$ mkdir -p ~/catkin_ws/src jym@ubuntu:~$ cd ~/catkin_ws/ jym@ubuntu:~/catkin_ws$ catkin_make

終端:

jym@ubuntu:~$ source /opt/ros/noetic/setup.bash jym@ubuntu:~$ mkdir -p ~/catkin_ws/src jym@ubuntu:~$ cd ~/catkin_ws/ jym@ubuntu:~/catkin_ws$ catkin_make Base path: /home/jym/catkin_ws Source space: /home/jym/catkin_ws/src Build space: /home/jym/catkin_ws/build Devel space: /home/jym/catkin_ws/devel Install space: /home/jym/catkin_ws/install Creating symlink "/home/jym/catkin_ws/src/CMakeLists.txt" pointing to "/opt/ros/noetic/share/catkin/cmake/toplevel.cmake" #### #### Running command: "cmake /home/jym/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/jym/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/jym/catkin_ws/install -G Unix Makefiles" in "/home/jym/catkin_ws/build" #### -- The C compiler identification is GNU 9.3.0 -- The CXX compiler identification is GNU 9.3.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Using CATKIN_DEVEL_PREFIX: /home/jym/catkin_ws/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/noetic -- This workspace overlays: /opt/ros/noetic -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Found PY_em: /usr/lib/python3/dist-packages/em.py -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/jym/catkin_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under '/usr/src/googletest': gtests will be built -- Found gmock sources under '/usr/src/googletest': gmock will be built -- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") -- Found Threads: TRUE -- Using Python nosetests: /usr/bin/nosetests3 -- catkin 0.8.10 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- Configuring done -- Generating done -- Build files have been written to: /home/jym/catkin_ws/build #### #### Running command: "make -j2 -l2" in "/home/jym/catkin_ws/build" ####

最終可以在Home-catkin_ws中找到:

接下來source一下新生成的setup.*sh文件:

$ source devel/setup.bash
要保證工作區(qū)被安裝腳本正確覆蓋,需確定ROS_PACKAGE_PATH環(huán)境變量包含當前的工作空間目錄:

$ echo $ROS_PACKAGE_PATH

jym@ubuntu:~/catkin_ws$ source devel/setup.bash jym@ubuntu:~/catkin_ws$ echo $ROS_PACKAGE_PATH /home/jym/catkin_ws/src:/opt/ros/noetic/share

catkin軟件包的結(jié)構

一個包要想稱為catkin軟件包,必須符合以下要求:

1.這個包必須有一個符合catkin規(guī)范的package.xml文件

這個package.xml文件提供有關該軟件包的元信息

2.這個包必須有一個catkin版本的CMakeLists.txt文件

如果它是個Catkin元包的話,則需要有一個CMakeList.txt文件的相關樣板

3.每個包必須有自己的目錄
這意味著在同一個目錄下不能有嵌套的或者多個軟件包存在

my_package/CMakeLists.txtpackage.xml

catkin工作空間中存放軟件包的結(jié)構

workspace_folder/ -- WORKSPACEsrc/ -- SOURCE SPACECMakeLists.txt -- 'Toplevel' CMake file, provided by catkinpackage_1/CMakeLists.txt -- CMakeLists.txt file for package_1package.xml -- Package manifest for package_1...package_n/CMakeLists.txt -- CMakeLists.txt file for package_npackage.xml -- Package manifest for package_n

創(chuàng)建catkin軟件包-catkin_create_pkg

1.切換到剛才創(chuàng)建的空白catkin工作空間中的源文件空間目錄

$ cd ~/catkin_ws/src

2.使用catkin_create_pkg命令創(chuàng)建一個名為beginner_tutorials的新軟件包,這個軟件包依賴于std_msgs、roscpp和rospy

$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

終端:

jym@ubuntu:~/catkin_ws$ cd ~/catkin_ws/src jym@ubuntu:~/catkin_ws/src$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp Created file beginner_tutorials/package.xml Created file beginner_tutorials/CMakeLists.txt Created folder beginner_tutorials/include/beginner_tutorials Created folder beginner_tutorials/src Successfully created files in /home/jym/catkin_ws/src/beginner_tutorials. Please adjust the values in package.xml.

可以觀察到對應的文件夾:

構建一個catkin工作區(qū)并生效配置文件

在上面已經(jīng)有介紹過創(chuàng)建和構建一個catkin工作空間,那邊主要目的是創(chuàng)建一個空白的catkin工作空間

到了這個地方主要目的是構建一個catkin工作空間。

1.在catkin工作區(qū)中構建軟件包

$ cd ~/catkin_ws $ catkin_make

2.工作空間構建完成后,要將這個工作空間添加到ROS環(huán)境中,需要source一下生成的配置文件

$ . ~/catkin_ws/devel/setup.bash jym@ubuntu:~/catkin_ws/src$ cd ~/catkin_ws jym@ubuntu:~/catkin_ws$ catkin_make Base path: /home/jym/catkin_ws Source space: /home/jym/catkin_ws/src Build space: /home/jym/catkin_ws/build Devel space: /home/jym/catkin_ws/devel Install space: /home/jym/catkin_ws/install #### #### Running command: "cmake /home/jym/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/jym/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/jym/catkin_ws/install -G Unix Makefiles" in "/home/jym/catkin_ws/build" #### -- Using CATKIN_DEVEL_PREFIX: /home/jym/catkin_ws/devel -- Using CMAKE_PREFIX_PATH: /home/jym/catkin_ws/devel;/opt/ros/noetic -- This workspace overlays: /home/jym/catkin_ws/devel;/opt/ros/noetic -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/jym/catkin_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under '/usr/src/googletest': gtests will be built -- Found gmock sources under '/usr/src/googletest': gmock will be built -- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") -- Using Python nosetests: /usr/bin/nosetests3 -- catkin 0.8.10 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 1 packages in topological order: -- ~~ - beginner_tutorials -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ processing catkin package: 'beginner_tutorials' -- ==> add_subdirectory(beginner_tutorials) -- Configuring done -- Generating done -- Build files have been written to: /home/jym/catkin_ws/build #### #### Running command: "make -j2 -l2" in "/home/jym/catkin_ws/build" #### jym@ubuntu:~/catkin_ws$ . ~/catkin_ws/devel/setup.bash

package.xml文件內(nèi)容

描述標簽:內(nèi)容是描述信息。

<description>The beginner_tutorials package</description>

維護者標簽:它能夠讓其他人聯(lián)系到軟件包的相關人員。

<!-- One maintainer tag required, multiple allowed, one person per tag --><!-- Example: --><!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> --><maintainer email="jym@todo.todo">jym</maintainer>

許可證標簽:些常見的開源許可協(xié)議有BSD、MIT、Boost Software License、GPLv2、GPLv3、LGPLv2.1和LGPLv3

<!-- One license tag required, multiple allowed, one license per tag --><!-- Commonly used license strings: --><!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 --><license>TODO</license>

依賴項標簽:標簽描述了軟件包的依賴關系,這些依賴項分為build_depend、buildtool_depend、run_depend、test_depend。

<?xml version="1.0"?> <package format="2"><name>beginner_tutorials</name><version>0.0.0</version><description>The beginner_tutorials package</description><!-- One maintainer tag required, multiple allowed, one person per tag --><!-- Example: --><!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> --><maintainer email="jym@todo.todo">jym</maintainer><!-- One license tag required, multiple allowed, one license per tag --><!-- Commonly used license strings: --><!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 --><license>TODO</license><!-- Url tags are optional, but multiple are allowed, one per tag --><!-- Optional attribute type can be: website, bugtracker, or repository --><!-- Example: --><!-- <url type="website">http://wiki.ros.org/beginner_tutorials</url> --><!-- Author tags are optional, multiple are allowed, one per tag --><!-- Authors do not have to be maintainers, but could be --><!-- Example: --><!-- <author email="jane.doe@example.com">Jane Doe</author> --><!-- The *depend tags are used to specify dependencies --><!-- Dependencies can be catkin packages or system dependencies --><!-- Examples: --><!-- Use depend as a shortcut for packages that are both build and exec dependencies --><!-- <depend>roscpp</depend> --><!-- Note that this is equivalent to the following: --><!-- <build_depend>roscpp</build_depend> --><!-- <exec_depend>roscpp</exec_depend> --><!-- Use build_depend for packages you need at compile time: --><!-- <build_depend>message_generation</build_depend> --><!-- Use build_export_depend for packages you need in order to build against this package: --><!-- <build_export_depend>message_generation</build_export_depend> --><!-- Use buildtool_depend for build tool packages: --><!-- <buildtool_depend>catkin</buildtool_depend> --><!-- Use exec_depend for packages you need at runtime: --><!-- <exec_depend>message_runtime</exec_depend> --><!-- Use test_depend for packages you need only for testing: --><!-- <test_depend>gtest</test_depend> --><!-- Use doc_depend for packages you need only for building documentation: --><!-- <doc_depend>doxygen</doc_depend> --><buildtool_depend>catkin</buildtool_depend><build_depend>roscpp</build_depend><build_depend>rospy</build_depend><build_depend>std_msgs</build_depend><build_export_depend>roscpp</build_export_depend><build_export_depend>rospy</build_export_depend><build_export_depend>std_msgs</build_export_depend><exec_depend>roscpp</exec_depend><exec_depend>rospy</exec_depend><exec_depend>std_msgs</exec_depend><!-- The export tag contains other, unspecified, tags --><export><!-- Other tools can request additional information be placed here --></export> </package>

可以結(jié)合具體情況進行修改 ,修改后的package.xml

<?xml version="1.0"?> <package format="2"><name>beginner_tutorials</name><version>0.1.0</version><description>The beginner_tutorials package</description><maintainer email="jym@todo.todo">jym</maintainer><license>BSD</license><buildtool_depend>catkin</buildtool_depend><build_depend>roscpp</build_depend><build_depend>rospy</build_depend><build_depend>std_msgs</build_depend><build_export_depend>roscpp</build_export_depend><build_export_depend>rospy</build_export_depend><build_export_depend>std_msgs</build_export_depend><exec_depend>roscpp</exec_depend><exec_depend>rospy</exec_depend><exec_depend>std_msgs</exec_depend><!-- The export tag contains other, unspecified, tags --><export><!-- Other tools can request additional information be placed here --></export> </package>

ROS一些基本概念

計算圖(Computation Graph)是一個由ROS進程組成的點對點網(wǎng)絡,它們能夠共同處理數(shù)據(jù)。

ROS的基本計算圖概念有節(jié)點(Nodes)、主節(jié)點(Master)、參數(shù)服務器(Parameter Server)、消息(Messages)、服務(Services)、話題(Topics)和袋(Bags),它們都以不同的方式向圖(Graph)提供數(shù)據(jù)。

節(jié)點(Nodes):節(jié)點是一個可執(zhí)行文件,它可以通過ROS來與其他節(jié)點進行通信。

消息(Messages):訂閱或發(fā)布話題時所使用的ROS數(shù)據(jù)類型。

話題(Topics):節(jié)點可以將消息發(fā)布到話題,或通過訂閱話題來接收消息。

主節(jié)點(Master):ROS的命名服務,例如幫助節(jié)點發(fā)現(xiàn)彼此。

rosout:在ROS中相當于stdout/stderr(標準輸出/標準錯誤)。

roscore:主節(jié)點 + rosout + 參數(shù)服務器。

節(jié)點:

節(jié)點實際上只不過是ROS軟件包中的一個可執(zhí)行文件。ROS節(jié)點使用ROS客戶端庫與其他節(jié)點通信。節(jié)點可以發(fā)布或訂閱話題,也可以提供或使用服務。

機器人和遙控器開始工作后,就是兩個節(jié)點。機器人是一個節(jié)點、遙控器也是一個節(jié)點。

遙控器起到了下達指 令的作用;機器人負責監(jiān)聽遙控器下達的指令,完成相應動作。

節(jié)點是一個能執(zhí)行特定工作任務的工作單元,并且能夠相互通信,從而實現(xiàn)一個機器人系統(tǒng)整體的功能。

客戶端庫:

ROS客戶端庫可以讓用不同編程語言編寫的節(jié)點進行相互通信:

  • rospy = Python客戶端庫
  • roscpp = C++客戶端庫

roscore:

roscore = ros+core:主節(jié)點(為ROS提供命名服務) + rosout (stdout/stderr) + 參數(shù)服務器

roscore是你在運行所有ROS程序前首先要運行的命令。

rosnode:獲取節(jié)點信息的ROS工具

打開一個新終端,可以使用rosnode看看roscore運行時干了些什么。

要保持以前的終端開著。

rosnode顯示當前正在運行的ROS節(jié)點信息。rosnode list命令會列出這些活動的節(jié)點。

jym@ubuntu:~$ roscore ... logging to /home/jym/.ros/log/13b5ebe2-3581-11ec-8099-99fb076f9407/roslaunch-ubuntu-4683.log Checking log directory for disk usage. This may take a while. Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB.started roslaunch server http://ubuntu:41325/ ros_comm version 1.15.13SUMMARY ========PARAMETERS* /rosdistro: noetic* /rosversion: 1.15.13NODESauto-starting new master process[master]: started with pid [4694] ROS_MASTER_URI=http://ubuntu:11311/setting /run_id to 13b5ebe2-3581-11ec-8099-99fb076f9407 process[rosout-1]: started with pid [4710] started core service [/rosout] jym@ubuntu:~$ rosnode list /rosout

rosout這個節(jié)點用于收集和記錄節(jié)點的調(diào)試輸出,所以它總是在運行的。

rosnode info命令返回的是某個指定節(jié)點的信息。

可以進一步查看rosout的信息, 比如說實際上它是發(fā)布了一個/rosout_agg話題。

jym@ubuntu:~$ rosnode info /rosout -------------------------------------------------------------------------------- Node [/rosout] Publications: * /rosout_agg [rosgraph_msgs/Log]Subscriptions: * /rosout [unknown type]Services: * /rosout/get_loggers* /rosout/set_logger_levelcontacting node http://ubuntu:45389/ ... Pid: 4710

rosrun:運行給定的軟件包中的節(jié)點

rosrun可以用包名直接運行軟件包內(nèi)的節(jié)點。

開一個新終端,運行turtlesim包中的turtlesim_node。

rosrun turtlesim turtlesim_node

會看到turtlesim窗口。

再開一個新終端,輸入rosnode list,會看到下面的輸出信息:

jym@ubuntu:~$ rosnode list /rosout /turtlesim

停止節(jié)點:關閉turtlesim窗口以停止節(jié)點(或回到rosrun turtlesim的終端并按Ctrl+C)。

改變節(jié)點名稱:使用重映射參數(shù)來改變節(jié)點名稱:

$ rosrun turtlesim turtlesim_node __name:=my_turtle

測試節(jié)點是否正常運行:ping,來測試它是否正常。

rosnode ping my_turtle jym@ubuntu:~$ rosnode ping my_turtle rosnode: node is [/my_turtle] pinging /my_turtle with a timeout of 3.0s xmlrpc reply from http://ubuntu:45307/ time=0.414848ms xmlrpc reply from http://ubuntu:45307/ time=1.438141ms xmlrpc reply from http://ubuntu:45307/ time=1.352310ms xmlrpc reply from http://ubuntu:45307/ time=0.378132ms xmlrpc reply from http://ubuntu:45307/ time=0.369549ms xmlrpc reply from http://ubuntu:45307/ time=0.383854ms xmlrpc reply from http://ubuntu:45307/ time=0.408173ms ^Cping average: 0.677858ms

ROS話題:節(jié)點之間是通過一個ROS話題來相互通信的

三個終端輸入:

$ roscore $ rosrun turtlesim turtlesim_node $ rosrun turtlesim turtle_teleop_key

選中turtle_teleop_key的終端窗口以確保按鍵輸入能夠被捕獲。

可以使用鍵盤上的方向鍵來控制turtle運動了。

turtlesim_node節(jié)點和turtle_teleop_key節(jié)點之間是通過一個ROS話題來相互通信的。

turtle_teleop_key在話題上發(fā)布鍵盤按下的消息,turtlesim則訂閱該話題以接收消息。

rqt_graph:顯示當前運行的節(jié)點和話題

使用rqt_graph來顯示當前運行的節(jié)點和話題。

打開一個新終端:

$ rosrun rqt_graph rqt_graph

會看到一個窗口:

如果把鼠標放在/turtle1/command_velocity上方,相應的ROS節(jié)點(這里是藍色和綠色)和話題(這里是紅色)就會高亮顯示。可以看到,turtlesim_node和turtle_teleop_key節(jié)點正通過一個名為/turtle1/command_velocity的話題來相互通信。

rostopic:獲取ROS話題的信息

rostopic命令工具能讓你獲取ROS話題的信息。

使用幫助選項查看可用的rostopic的子命令

rostopic -h jym@ubuntu:~$ rostopic -h rostopic is a command-line tool for printing information about ROS Topics.Commands:rostopic bw display bandwidth used by topicrostopic delay display delay of topic from timestamp in headerrostopic echo print messages to screenrostopic find find topics by typerostopic hz display publishing rate of topic rostopic info print information about active topicrostopic list list active topicsrostopic pub publish data to topicrostopic type print topic or field typeType rostopic <command> -h for more detailed usage, e.g. 'rostopic echo -h'

接下來,將使用其中的一些子命令來了解turtlesim

rostopic echo:顯示在某個話題上發(fā)布的數(shù)據(jù)

rostopic echo可以顯示在某個話題上發(fā)布的數(shù)據(jù)。

打開新終端輸入:

rostopic echo /turtle1/cmd_vel

可以通過按下鍵盤方向鍵讓turtle_teleop_key節(jié)點發(fā)布數(shù)據(jù)。

然后按下向上鍵時可以看到:

jym@ubuntu:~$ rostopic echo /turtle1/cmd_vel linear: x: 2.0y: 0.0z: 0.0 angular: x: 0.0y: 0.0z: 0.0 --- linear: x: 2.0y: 0.0z: 0.0 angular: x: 0.0y: 0.0z: 0.0 ---

此時再看一下rqt_graph:

rostopic echo現(xiàn)在也訂閱了turtle1/command_velocity話題。

rostopic list:列出當前已被訂閱和發(fā)布的所有話題。

rostopic list能夠列出當前已被訂閱和發(fā)布的所有話題。

開一個新終端:查看一下list子命令需要的參數(shù)

rostopic list -h jym@ubuntu:~$ rostopic list -h Usage: rostopic list [/namespace]Options:-h, --help show this help message and exit-b BAGFILE, --bag=BAGFILElist topics in .bag file-v, --verbose list full details about each topic-p list only publishers-s list only subscribers--host group by host name

在rostopic list中使用verbose選項:

$ rostopic list -v

會列出所有發(fā)布和訂閱的主題及其類型的詳細信息。

jym@ubuntu:~$ rostopic list -vPublished topics:* /rosout_agg [rosgraph_msgs/Log] 1 publisher* /rosout [rosgraph_msgs/Log] 4 publishers* /turtle1/pose [turtlesim/Pose] 1 publisher* /turtle1/color_sensor [turtlesim/Color] 1 publisher* /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisherSubscribed topics:* /rosout [rosgraph_msgs/Log] 1 subscriber* /turtle1/cmd_vel [geometry_msgs/Twist] 2 subscribers* /statistics [rosgraph_msgs/TopicStatistics] 1 subscriber

ROS消息:話題的通信是通過節(jié)點間發(fā)送ROS消息實現(xiàn)的

話題的通信是通過節(jié)點間發(fā)送ROS消息實現(xiàn)的。為了使發(fā)布者(turtle_teleop_key)和訂閱者(turtulesim_node)進行通信,發(fā)布者和訂閱者必須發(fā)送和接收相同類型的消息。這意味著話題的類型是由發(fā)布在它上面消息的類型決定的。使用rostopic type命令可以查看發(fā)布在話題上的消息的類型

rostopic type:查看所發(fā)布話題的消息類型

rostopic type命令用來查看所發(fā)布話題的消息類型。

運行:

$ rostopic type /turtle1/cmd_vel

可以使用rosmsg查看消息的詳細信息

$ rosmsg show geometry_msgs/Twist jym@ubuntu:~$ rostopic type /turtle1/cmd_vel geometry_msgs/Twist jym@ubuntu:~$ rosmsg show geometry_msgs/Twist geometry_msgs/Vector3 linearfloat64 xfloat64 yfloat64 z geometry_msgs/Vector3 angularfloat64 xfloat64 yfloat64 z

現(xiàn)在已經(jīng)知道了turtlesim節(jié)點想要的消息類型,然后就可以發(fā)布命令給turtle了。

rostopic pub:把數(shù)據(jù)發(fā)布到當前某個正在廣播的話題上

rostopic pub可以把數(shù)據(jù)發(fā)布到當前某個正在廣播的話題上。

$ rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

以上命令會發(fā)送一條消息給turtlesim,告訴它以2.0大小的線速度和1.8大小的角速度移動。

jym@ubuntu:~$ rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]' publishing and latching message for 3.0 seconds
  • rostopic pub命令將消息發(fā)布到指定的話題
  • -1 這一選項會讓rostopic只發(fā)布一條消息,然后退出
  • /turtle1/cmd_vel是要發(fā)布到的話題的名稱
  • geometry_msgs/Twist是發(fā)布到話題時要使用的消息的類型
  • – 這一選項(兩個破折號)用來告訴選項解析器,表明之后的參數(shù)都不是選項。如果參數(shù)前有破折號(-)比如負數(shù),那么這是必需的。
  • 如前所述,一個turtlesim/Velocity消息有兩個浮點型元素:linear和angular。在本例中,'[2.0, 0.0, 0.0]'表示linear的值為x=2.0, y=0.0, z=0.0,而'[0.0, 0.0, 1.8]'是說angular的值為x=0.0, y=0.0, z=1.8

這行命令發(fā)出后,turtle移動了一下就停了。這是因為turtle需要一個穩(wěn)定的頻率為1Hz的指令流才能保持移動狀態(tài)。我們可以使用rostopic pub -r命令來發(fā)布源源不斷的命令:

$ rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

將以1 Hz的速度發(fā)布velocity指令到velocity話題上。

rqt_graph中。可以看到rostopic pub節(jié)點正在與rostopic echo節(jié)點進行通信:

新終端中通過rostopic echo命令來查看turtlesim所發(fā)布的數(shù)據(jù):rostopic echo /turtle1/pose

--- linear: x: 2.0y: 0.0z: 0.0 angular: x: 0.0y: 0.0z: -1.8 --- linear: x: 2.0y: 0.0z: 0.0 angular: x: 0.0y: 0.0z: -1.8 ---

rostopic hz:報告數(shù)據(jù)發(fā)布的速率

rostopic hz報告數(shù)據(jù)發(fā)布的速率。

看一下turtlesim_node發(fā)布/turtle/pose得有多快:

$ rostopic hz /turtle1/pose jym@ubuntu:~$ rostopic hz /turtle1/pose subscribed to [/turtle1/pose] average rate: 62.521min: 0.014s max: 0.018s std dev: 0.00067s window: 63 average rate: 62.479min: 0.014s max: 0.018s std dev: 0.00056s window: 125 average rate: 62.486min: 0.014s max: 0.018s std dev: 0.00057s window: 188

可以知道,turtlesim正以大約60Hz的頻率發(fā)布有關烏龜?shù)臄?shù)據(jù)。

rqt_plot:在滾動時間圖上顯示發(fā)布到某個話題上的數(shù)據(jù)

rqt_plot命令可以在滾動時間圖上顯示發(fā)布到某個話題上的數(shù)據(jù)。這里我們將使用rqt_plot命令來繪制正被發(fā)布到/turtle1/pose話題上的數(shù)據(jù)。

新終端輸入rosrun rqt_plot rqt_plot

可以在左上角的文本框里面添加任何想要繪制的話題。在里面輸入/turtle1/pose/x后,之前不能按下的加號按鈕將會變亮。按一下該按鈕,并對/turtle1/pose/y重復相同的過程。現(xiàn)在你會在圖中看到turtle的x-y位置。

ROS服務:節(jié)點之間通訊的另一種方式

服務(Services)是節(jié)點之間通訊的另一種方式。服務允許節(jié)點發(fā)送一個請求(request)并獲得一個響應(response)。

rosservice:有許多可用于服務的命令

rosservice可以很容易地通過服務附加到ROS客戶端/服務器框架上。rosservice有許多可用于服務的命令,如下所示:

rosservice list 輸出活躍服務的信息 rosservice call 用給定的參數(shù)調(diào)用服務 rosservice type 輸出服務的類型 rosservice find 按服務的類型查找服務 rosservice uri 輸出服務的ROSRPC uri

rosservice list:顯示節(jié)點提供的服務

jym@ubuntu:~$ rosservice list /clear /kill /reset /rosout/get_loggers /rosout/set_logger_level /spawn /teleop_turtle/get_loggers /teleop_turtle/set_logger_level /turtle1/set_pen /turtle1/teleport_absolute /turtle1/teleport_relative /turtlesim/get_loggers /turtlesim/set_logger_level

rosservice type:查看服務的類型

使用rosservice type命令進一步查看clear(清除)服務:

jym@ubuntu:~$ rosservice type /clear std_srvs/Empty

服務的類型為empty(空),這表明調(diào)用這個服務時不需要參數(shù)(即,它在發(fā)出請求時不發(fā)送數(shù)據(jù),在接收響應時也不接收數(shù)據(jù))。

查看有參服務:

$ rosservice type /spawn | rossrv show jym@ubuntu:~$ rosservice type /spawn | rossrv show float32 x float32 y float32 theta string name --- string name

這個服務能讓我們可以在給定的位置和角度生成一只新的烏龜。name字段是可選的。

rosservice call:調(diào)用服務

調(diào)用無參服務:

$ rosservice call /clear

清除了turtlesim_node背景上的軌跡。

調(diào)用有參服務:

jym@ubuntu:~$ rosservice call /spawn 2 2 2 "" name: "turtle2"

該調(diào)用返回了新產(chǎn)生的烏龜?shù)拿帧H缓髏urtlesim里面出現(xiàn)了個新烏龜。

rosparam:在ROS參數(shù)服務器上存儲和操作數(shù)據(jù)

rosparam能在ROS參數(shù)服務器(Parameter Server)上存儲和操作數(shù)據(jù)。參數(shù)服務器能夠存儲整型(integer)、浮點(float)、布爾(boolean)、字典(dictionaries)和列表(list)等數(shù)據(jù)類型。

rosparam使用YAML標記語言的語法。一般而言,YAML的表述很自然:1是整型,1.0是浮點型,one是字符串,true是布爾型,[1, 2, 3]是整型組成的列表,{a: b, c: d}是字典。

rosparam有很多命令可以用來操作參數(shù),如下所示:

rosparam set 設置參數(shù) rosparam get 獲取參數(shù) rosparam load 從文件中加載參數(shù) rosparam dump 向文件中轉(zhuǎn)儲參數(shù) rosparam delete 刪除參數(shù) rosparam list 列出參數(shù)名

rosparam list:查看參數(shù)服務器上都有哪些參數(shù)

jym@ubuntu:~$ rosparam list /rosdistro /roslaunch/uris/host_ubuntu__36577 /rosversion /run_id /turtlesim/background_b /turtlesim/background_g /turtlesim/background_r

rosparam set:改變參數(shù)服務器上參數(shù)的值

修改背景顏色的紅色通道值:

$ rosparam set /turtlesim/background_r 150

上述指令修改了參數(shù)的值,現(xiàn)在我們需要調(diào)用clear服務使得參數(shù)的修改能生效:

$ rosservice call /clear

rosparam get:查看參數(shù)服務器上參數(shù)的值

查看參數(shù)服務器上其他參數(shù)的值。獲取背景的綠色通道的值:

$ rosparam get /turtlesim/background_g

也可以用rosparam get /來顯示參數(shù)服務器上的所有內(nèi)容:

$ rosparam get / jym@ubuntu:~$ rosparam get / rosdistro: 'noetic' roslaunch:uris:host_ubuntu__36577: http://ubuntu:36577/ rosversion: '1.15.13' run_id: 7f07518e-3585-11ec-8099-99fb076f9407 turtlesim:background_b: 255background_g: 86background_r: 150

rqt_console:連接ROS的日志框架,顯示節(jié)點的輸出信息

rqt_console連接到了ROS的日志框架,以顯示節(jié)點的輸出信息。

rqt_logger_level:在節(jié)點運行時改變輸出信息的詳細級別

rqt_logger_level允許我們在節(jié)點運行時改變輸出信息的詳細級別,包括Debug、Info、Warn和Error`。

接下來,看一下turtlesim在rqt_console中輸出的信息,同時在使用turtlesim時切換rqt_logger_level中的日志級別。

在啟動turtlesim之前先在兩個終端中運行rqt_console和rqt_logger_level:

$ rosrun rqt_console rqt_console $ rosrun rqt_logger_level rqt_logger_level

在另一個終端中啟動turtlesim:

$ rosrun turtlesim turtlesim_node

因為默認的日志級別是Info,所以你會看到turtlesim啟動后發(fā)布的所有信息。

把烏龜撞到墻上,rqt_console上會顯示:

日志級別的優(yōu)先級按以下順序排列:

Fatal (致命) Error (錯誤) Warn (警告) Info (信息) Debug (調(diào)試)

Fatal是最高優(yōu)先級,Debug是最低優(yōu)先級。通過設置日志級別,你可以獲得所有優(yōu)先級級別,或只是更高級別的消息。比如,將日志級別設為Warn時,你會得到Warn、Error和Fatal這三個等級的日志消息。

roslaunch:啟動定義在launch(啟動)文件中的節(jié)點

roslaunch可以用來啟動定義在launch(啟動)文件中的節(jié)點。

首先,按照創(chuàng)建catkin工作空間后面的步驟使環(huán)境變量生效

然后,切換到之前創(chuàng)建和構建的beginner_tutorials軟件包目錄下

然后,創(chuàng)建一個launch目錄

jym@ubuntu:~$ cd ~/catkin_ws jym@ubuntu:~/catkin_ws$ source devel/setup.bash jym@ubuntu:~/catkin_ws$ roscd beginner_tutorials jym@ubuntu:~/catkin_ws/src/beginner_tutorials$ mkdir launch jym@ubuntu:~/catkin_ws/src/beginner_tutorials$ cd launch jym@ubuntu:~/catkin_ws/src/beginner_tutorials/launch$

創(chuàng)建一個名為turtlemimic.launch的launch文件

$ gedit turtlemimic.launch

然后粘貼進去

<launch><group ns="turtlesim1"><node pkg="turtlesim" name="sim" type="turtlesim_node"/></group><group ns="turtlesim2"><node pkg="turtlesim" name="sim" type="turtlesim_node"/></group><node pkg="turtlesim" name="mimic" type="mimic"><remap from="input" to="turtlesim1/turtle1"/><remap from="output" to="turtlesim2/turtle1"/></node></launch>

launch XML文件的內(nèi)容

<launch>

首先用launch標簽開頭,以表明這是一個launch文件。

<group ns="turtlesim1"><node pkg="turtlesim" name="sim" type="turtlesim_node"/></group><group ns="turtlesim2"><node pkg="turtlesim" name="sim" type="turtlesim_node"/></group>

此處我們創(chuàng)建了兩個分組,并以命名空間(namespace)標簽來區(qū)分,其中一個名為turtulesim1,另一個名為turtlesim2,兩個分組中都有相同的名為sim的turtlesim節(jié)點。這樣可以讓我們同時啟動兩個turtlesim模擬器,而不會產(chǎn)生命名沖突。

<node pkg="turtlesim" name="mimic" type="mimic"><remap from="input" to="turtlesim1/turtle1"/><remap from="output" to="turtlesim2/turtle1"/></node>

在這里我們啟動模仿節(jié)點,話題的輸入和輸出分別重命名為turtlesim1和turtlesim2,這樣就可以讓turtlesim2模仿turtlesim1了。

</launch>

這一行使得launch文件的XML標簽閉合。

使用roslaunch:運行l(wèi)aunch文件

通過roslaunch命令來運行l(wèi)aunch文件:

$ roslaunch beginner_tutorials turtlemimic.launch

現(xiàn)在將會有兩個turtlesim被啟動,然后我們在一個新終端中使用rostopic命令發(fā)送:

$ rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

兩個turtlesims同時開始移動,雖然發(fā)布命令只發(fā)送給了turtlesim1。

實現(xiàn)過程中,有三個終端:

第一個:輸入roscore

jym@ubuntu:~$ roscore ... logging to /home/jym/.ros/log/bd42545e-3598-11ec-8099-99fb076f9407/roslaunch-ubuntu-10960.log Checking log directory for disk usage. This may take a while. Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB.started roslaunch server http://ubuntu:43275/ ros_comm version 1.15.13SUMMARY ========PARAMETERS* /rosdistro: noetic* /rosversion: 1.15.13NODESauto-starting new master process[master]: started with pid [10968] ROS_MASTER_URI=http://ubuntu:11311/

第二個:運行l(wèi)aunch文件

jym@ubuntu:~$ cd ~/catkin_ws jym@ubuntu:~/catkin_ws$ source devel/setup.bash jym@ubuntu:~/catkin_ws$ roscd beginner_tutorials jym@ubuntu:~/catkin_ws/src/beginner_tutorials$ roslaunch beginner_tutorials turtlemimic.launch ... logging to /home/jym/.ros/log/bd42545e-3598-11ec-8099-99fb076f9407/roslaunch-ubuntu-11191.log Checking log directory for disk usage. This may take a while. Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB.started roslaunch server http://ubuntu:36829/SUMMARY ========PARAMETERS* /rosdistro: noetic* /rosversion: 1.15.13NODES/mimic (turtlesim/mimic)/turtlesim1/sim (turtlesim/turtlesim_node)/turtlesim2/sim (turtlesim/turtlesim_node)ROS_MASTER_URI=http://localhost:11311process[turtlesim1/sim-1]: started with pid [11205] process[turtlesim2/sim-2]: started with pid [11206] process[mimic-3]: started with pid [11208]

第三個終端:使用rostopic命令發(fā)送

jym@ubuntu:~$ rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

總結(jié)

以上是生活随笔為你收集整理的ROS基本概念 文件系统 创建ROS软件包 ROS中的一些命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。