Depending on your learning style and preferences, you can take two approaches to learning ROS: 根據你的學習風格和喜愛,你能夠採起兩種方法來學習ROS:html
Start the Tutorials - Dive in right away and start working with ROS. 一種是直接開始上手操做ROS (教程:http://wiki.ros.org/ROS/Tutorials)node
Get an Overview - Read through this written overview of ROS and its capabilities. 另外一種是先學習概念再上手(ROS簡介:http://wiki.ros.org/ROS/Introduction)python
To get an immediate start with a pre-prepared ROS virtual machine, please see here: http://www.clearpathrobotics.com/blog/how-to-guide-ros-101/ linux
要想使用ROS虛擬機快速開始,請看這裏:http://www.clearpathrobotics.com/blog/how-to-guide-ros-101/c++
For more detailed information on the ROS framework, see the core ROS documentation. git
對於ROS框架的更多詳細信息,請看ROS內核教程:http://wiki.ros.org/ROS github
以上內容翻譯自:http://wiki.ros.org/ROS/StartGuideweb
There are three places to look for answers to your questions and problems. The first is this wiki. Try the Search feature located at the top right. shell
有三個地方能夠尋找問題的答案。第一個就是這裏wiki.ros.org。試着使用右上角的Search搜索功能。編程
If you can't find a solution to your problem, then try searching http://answers.ros.org or the archived mailing list. Ask a question on http://answers.ros.org/ask
若是你在wiki.ros.org搜索不到,那麼試一下到http://answers.ros.org搜索一下,或者在archived mailing list(http://lists.ros.org/lurker/list/ros-users.en.html)搜索一下。或着在http://answers.ros.org/ask提問。
Check out the Support page for more information.
看一下支持頁面尋找更多信息。
Wondering if there is already a ROS package out there that does what you need? The Browse Software tool also lets search for useful packages. You can also browse the list of public ROS repositories to look for groups doing similar work.
想知道是否已經有你所想要的ROS package開發包了?「瀏覽軟件」工具容許你搜索有用的源碼包:http://www.ros.org/browse/。你還能夠瀏覽公共ROS資料庫列表查找作相同工做的羣體:http://wiki.ros.org/RecommendedRepositoryUsage/CommonGitHubOrganizations。
See navigating the wiki tutorial for more information.
看wiki教程導航查看更多信息:http://wiki.ros.org/ROS/Tutorials/NavigatingTheWiki。
Non-Beginners: If you're already familiar enough with ROS fuerte or earlier versions and only want to explore the new build system introduced in groovy and used in hydro and later, called catkin, you can go through more in-depth catkin tutorial here. However, going over all basic Beginner Level tutorials is still recommended for all users to get exposed to new features. 非新手:若是你已經很熟悉ROS fuerte(第5版ROS系統)或者早先的版本了,只是但願探索groovy(第6版ROS系統)介紹的、在hydro(第7版ROS系統)及其後使用的新的創建系統,叫作catkin,你能夠瀏覽更多深度解析的catkin教程:http://wiki.ros.org/catkin/Tutorials。可是,仍然建議全部的使用者能夠瀏覽全部基本的初學級別的教程,從而掌握全部的新功能:http://wiki.ros.org/ROS/Tutorials#Beginner_Level。
If you are new to Linux: You may find it helpful to first do a quick tutorial on common command line tools for linux. A good one is here.
若是你是一個Linux新手:首先快速的學習基本的Linux命令行工具是有幫助的。一個好的教程請看這裏:http://www.ee.surrey.ac.uk/Teaching/Unix/。
如下內容來自:http://wiki.ros.org/ROS/Tutorials
1. Installing and Configuring Your ROS Environment 創建並配置你的ROS環境
This tutorial walks you through installing ROS and setting up the ROS environment on your computer.
該教程將帶你創建ROS並配置你電腦中的ROS環境。
(1)Install ROS 安裝ROS
Note:If you installed ROS from a package manager like apt, then those packages will not be write accessible and should not be edited by you the user. When working with ROS packages from source or when creating a new ROS package, you should always work in a directory that you have access to, like your home folder.
注意:若是你經過像apt這樣的包管理器創建ROS的話,那麼這些包將不能被寫並且不能被使用者所編輯。當跟源ROS包工做或者當建立一個新的 ROS包時,你應該老是在可以接觸到的路徑中工做,好比你的home文件夾中。
(2)Managing Your Environment管理你的環境
During the installation of ROS, you will see that you are prompted to source one of several setup.*sh files, or even add this 'sourcing' to your shell startup script. This is required because ROS relies on the notion of combining spaces using the shell environment. This makes developing against different versions of ROS or against different sets of packages easier.
在安裝ROS過程當中,你會看到你被建議創建幾個啓動.*sh文件中的一個,或者甚至添加這個sourcing到你的shell開始腳本中。這個是由於ROS依賴於使用shell環境來將多個空間結合起來的思想。這使得開發違背ROS的不一樣版本,或者違背不一樣包的集合更加容易。
If you are ever having problems finding or using your ROS packages make sure that you have your environment properly setup. A good way to check is to ensure that environment variables like ROS_ROOT and ROS_PACKAGE_PATH are set:
$ printenv | grep ROS
若是對查找和使用ROS包有問題的話,情確保你已經將你的環境恰當地創建起來了。一個好的檢查方式確保環境變量像ROS_ROOT和ROS_PACKAGE_PATH已經設置:
$ printenv | grep ROS
If they are not then you might need to 'source' some setup.*sh files.
若是它們尚未設置,那麼你可能須要‘源’這些setup.*sh文件:
Environment setup files are generated for you, but can come from different places: 環境設置文件是爲你生成的,可是可能來自不一樣的地方:
rosbuild workspaces provide setup.*sh files using tools like rosws rosbuild工做空間使用像rosws的工具提供setup.*sh文件。
Setup.*sh files are created as a by-product of building or installing catkin packages Setup.*sh文件是做爲一個創建或着安裝catkin包的by-product副產品建立的。
Note: Throughout the tutorials you will see references to rosbuild and catkin. These are the two available methods for organizing and building your ROS code. Generally, rosbuild is easy to use and simple, where as catkin uses more standard CMake conventions, so it is more sophisticated, but provides more flexibility especially for people wanting to integrate external code bases or who want to release their software. For a full break down visit catkin or rosbuild.
注意:貫穿本教程你將會看到關於rosbuild和catkin的參考。這些是兩個對於管理和創建你的ROS代碼可用的方法。通常來說,rosbuild是容易用並且簡單的,而catkin使用了更加標準的CMake協議,因此它更加複雜,可是它提供了更多彈性的尤爲是對於想要集成外部代碼底層的人們或者是想要發佈他們的軟件的人們來講。對於完整的解析請看:catkin or rosbuild:http://wiki.ros.org/catkin_or_rosbuild。
If you just installed ROS from apt on Ubuntu then you will have setup.*sh files in '/opt/ros/<distro>/', and you could source them like so:
若是你只是經過apt在Ubuntu上安裝ROS,那麼你將會在「/opt/ros/<distro>/」中擁有setup.*sh文件,而且你能夠將它們源起來像這樣:
$ source /opt/ros/<distro>/setup.bash
Using the short name of your ROS distribution instead of <distro> 使用你的ROS版本的短名稱代替<distro>,這裏distro是Kinetic(第十版的ROS版本)
If you installed ROS Kinetic, that would be:若是你安裝了ROS Kinetic,那麼將會是
$ source /opt/ros/kinetic/setup.bash
You will need to run this command on every new shell you open to have access to the ROS commands, unless you add this line to your .bashrc. This process allows you to install several ROS distributions (e.g. indigo and kinetic) on the same computer and switch between them.
你將須要在每個你打開的新的shell運行這個命令來運行ROS命令,除非你已經將這一行加進了你的.bashrc中了。這個過程容許你在一個電腦上安裝多個ROS包(好比indigo和kinetic),而且在他們中間切換。
On other platforms you will find these setup.*sh files wherever you installed ROS.
在其它平臺上,不管你在哪裏安裝ROS你都將發現這些setup.*sh文件。
(3)Create a ROS Workspace 創建你的一個ROS工做空間
These instructions are for ROS Groovy and later. For ROS Fuerte and earlier, select rosbuild.
這些指導是爲ROS Groovy及其之後的版本的。對於ROS Fuerte和早期版本,須要選擇rosbuild。
Let's create a catkin workspace: 讓咱們建立一個catkin工做空間:
$ mkdir -p ~/catkin_ws/src //主目錄下建立一個catkin_ws/src目錄
$ cd ~/catkin_ws/src //走到catkin_ws/src目錄下 $ catkin_init_workspace //初始化該工做空間
Even though the workspace is empty (there are no packages in the 'src' folder, just a single CMakeLists.txt link) you can still "build" the workspace: 儘管這個工做空間是空的(在這個'src'文件夾下沒有任何的包,只有一個CMakeLists.txt連接),可是你依然能夠「創建」這個工做空間:
$ cd ~/catkin_ws/ //走到catkin_ws/目錄下
$ catkin_make //catkin_make創建該工做空間
The catkin_make command is a convenience tool for working with catkin workspaces. If you look in your current directory you should now have a 'build' and 'devel' folder. Inside the 'devel' folder you can see that there are now several setup.*sh files. Sourcing any of these files will overlay this workspace on top of your environment. To understand more about this see the general catkin documentation: catkin. Before continuing source your new setup.*sh file:
catkin_make命令是一個在catkin workspaces工做空間工做中方便的工具。若是你看一下你當前的目錄你如今應該有了一個'build'和一個'devel'文件夾。在'devel'文件夾下你能夠看到有幾個setup.*sh文件。源這些文件中的任何一個將會將該工做空間鋪蓋到你環境的頂部。爲了理解更多,你能夠看一下通用的catkin參考資料:catkin:http://wiki.ros.org/catkin。在源你新建的setup.*sh文件以前:
$ source devel/setup.bash
To make sure your workspace is properly overlayed by the setup script, make sure ROS_PACKAGE_PATH environment variable includes the directory you're in.
爲了確保你的工做空間適當地由setup腳本覆蓋起來,請確保ROS_PACKAGE_PATH環境變量包括你所在的目錄。
$ echo $ROS_PACKAGE_PATH
/home/youruser/catkin_ws/src:/opt/ros/kinetic/share:/opt/ros/kinetic/stacks
Now that your environment is setup, continue with the ROS file system tutorial.
如今你的環境已經安裝成功了,那麼繼續ROS文件系統的教程吧。
2. Navigating the ROS Filesystem 瀏覽你的ROS文件系統
This tutorial introduces ROS filesystem concepts, and covers using the roscd, rosls, and rospack commandline tools.
該教程將介紹ROS文件系統的概念,而且覆蓋使用roscd,rosls,和rospack命令行工具。
(1)前提
For this tutorial we will inspect a package in ros-tutorials, please install it using 要完成這個教程咱們將檢查ros-tutorials中的一個包,請使用如下方法安裝它:
(2)文件系統概念快速瀏覽
Manifests (package.xml): A manifest is a description of a package. It serves to define dependencies between packages and to capture meta information about the package like version, maintainer, license, etc... Manifest文件(package.xml):一個Manifest文件是對於包的描述。它用於定義包之間的依賴,以及抓捕關於包的元信息,好比版本號、保持者、版權等等
(3)文件系統工具
Code is spread across many ROS packages. Navigating with command-line tools such as ls and cd can be very tedious which is why ROS provides tools to help you.
代碼是遍及許多ROS包的。使用命令行工具瀏覽好比ls和cd將多是很是冗長的,因此ROS提供了工具來幫助你。
1)使用rospack
rospack allows you to get information about packages. In this tutorial, we are only going to cover the find option, which returns the path to package.
rospack容許你獲取關於包的信息。在該教程中,咱們只是涉及find查找選項,用於返回包的路徑。
$ rospack find [package_name]
Example: 例子:
would return: 將返回:(這裏是:/opt/ros/kinetic/share/roscpp)
YOUR_INSTALL_PATH/share/roscpp
If you installed ROS from apt on Ubuntu Linux you would see exactly: 若是你經過apt在Ubuntu Linux上安裝的ROS,那麼你將看到:
/opt/ros/kinetic/share/roscpp
2)使用roscd
roscd is part of the rosbash suite. It allows you to change directory (cd) directly to a package or a stack.
roscd是rosbash包的一部分。它容許你更改目錄(cd)直接到一個包或者一個集中。
Usage: 用法
要驗證咱們已經更改到了roscpp包目錄下,運行這個例子:
$ roscd roscpp
Now let's print the working directory using the Unix command pwd:如今讓咱們使用Unix命令pwd來打印工做目錄:
$ pwd
You should see: 你將會看到:(這裏是:/opt/ros/kinetic/share/roscpp)
You can see that YOUR_INSTALL_PATH/share/roscpp is the same path that rospack find gave in the previous example.
你能夠看到YOUR_INSTALL_PATH/share/roscpp正是rospack find在以前的例子中給出的。
Note that roscd, like other ROS tools, will only find ROS packages that are within the directories listed in your ROS_PACKAGE_PATH. To see what is in your ROS_PACKAGE_PATH, type: 注意roscd,像其餘的ROS工具同樣,將只能查找在你的ROS_PACKAGE_PATH中列出的目錄中的ROS包。要查看在你的ROS_PACKAGE_PATH中有什麼,請輸入:
/opt/ros/kinetic/base/install/share:/opt/ros/kinetic/base/install/stacks
Similarly to other environment paths, you can add additional directories to your ROS_PACKAGE_PATH, with each path separated by a colon ':'.
相似於其餘的環境路徑,你能夠添加額外的路徑到你的ROS_PACKAGE_PATH中,每一個路徑由分割符號「:」隔開。
roscd can also move to a subdirectory of a package or stack. roscd還能移動到一個包或者集的子路徑。
$ roscd roscpp/cmake
You should see: 你應該能夠看到:
3)roscd log
roscd log will take you to the folder where ROS stores log files. Note that if you have not run any ROS programs yet, this will yield an error saying that it does not yet exist.
roscd log將帶你到ROS存儲log文件的文件夾中。注意若是你尚未運行任何ROS程序的話,這個將發出一個講它還不存在的錯誤。
若是你以前已經運行了一些ROS程序的話,嘗試:
$ roscd log
4)使用rosls
rosls is part of the rosbash suite. It allows you to ls directly in a package by name rather than by absolute path.
rosls是rosbash套件的一部分。它容許你在一個包中列出而不用絕對路徑。
Usage: 用法:
Example: 例子:
would return: 將會返回:
5)製表符完成
It can get tedious to type out an entire package name. In the previous example, roscpp_tutorials is a fairly long name. Luckily, some ROS tools support TAB completion.
它能夠從一個完整的包名中獲得冗長的類型。在以前的例子中,roscpp_tutorials是一個很長的名字。幸運的是,一些ROS工具支持「TAB完成」。
Start by typing: 試着敲下:
After pushing the TAB key, the command line should fill out the rest:
This works because roscpp_tutorials is currently the only ROS package that starts with roscpp_tut. 這工做了,由於roscpp_tutorials是當前ROS包中惟一以roscpp_tut開始的包。
$ roscd tur<<< now push the TAB key >>>
After pushing the TAB key, the command line should fill out as much as possible:
在你敲下TAB鍵時,命令行應該儘量地填充:
$ roscd turtle
However, in this case there are multiple packages that begin with turtle. Try typing TAB another time. This should display all the ROS packages that begin with turtle: 可是,在這種狀況下有多個包以turtle開頭。試着敲下TAB鍵。這可能會顯示出全部的以turtle開頭的ROS包:
turtle_actionlib/ turtlesim/ turtle_tf/
On the command line you should still have:在命令行你應該仍有:
$ roscd turtle
Now type an s after turtle and then push TAB:如今在turtle後面鍵入一個s,而後按TAB鍵:
Since there is only one package that starts with turtles, you should see: 既然只有一個包以turtles開頭,你應該會看到
(4)Review複習
Now that you can get around in ROS, let's create a package. 如今你已經對ROS有了瞭解了,那讓咱們建立一個包吧。
3. Creating a ROS Package 創建一個ROS包
This tutorial covers using roscreate-pkg or catkin to create a new package, and rospack to list package dependencies.
該教程包括使用roscreate-pkg或者catkin來建立一個新的包,或者使用rospack來列出包的依賴。
(1)什麼才能組成一個catkin包?
For a package to be considered a catkin package it must meet a few requirements: 要使一個包被當成一個catkin包,它須要知足幾個要求:
The package must contain a catkin compliant package.xml file. 這個包必須包含一個catkin compliant package.xml文件。
The package must contain a CMakeLists.txt which uses catkin. If it is a catkin metapackage it must have the relevant boilerplate CMakeLists.txt file. 該包必須包含一個使用catkin的CMakeLists.txt文件。若是這是一個catkin元包的話,它必須有相關的模板化CMakeLists.txt文件。
The simplest possible package might have a structure which looks like this: 最簡單的包多是有一個像這樣的結構:
my_package/
CMakeLists.txt package.xml
(2)在一個catkin工做空間中的包
The recommended method of working with catkin packages is using a catkin workspace, but you can also build catkin packages standalone. A trivial workspace might look like this: 跟catkin包工做建議的方法是使用一個catkin工做空間,可是你也能夠單獨生成catkin包。一個通常的工做空間多是像這樣:
workspace_folder/ -- WORKSPACE //工做空間
src/ -- SOURCE SPACE //源空間 CMakeLists.txt -- 'Toplevel' CMake file, provided by catkin //頂層的CMake文件,由catkin提供 package_1/ CMakeLists.txt -- CMakeLists.txt file for package_1 //包_1中的CMakeLists.txt package.xml -- Package manifest for package_1 //包_1中的包manifest ... package_n/ CMakeLists.txt -- CMakeLists.txt file for package_n //包_n中的CMakeLists.txt package.xml -- Package manifest for package_n //包_n中的包manifest
Before continuing with this tutorial create an empty catkin workspace by following the Creating a workspace for catkin tutorial.在繼續本教程以前建立一個空的catkin工做空間,按照建立一個catkin工做空間教程:http://wiki.ros.org/catkin/Tutorials/create_a_workspace。
(3)建立一個catkin包
This tutorial will demonstrate how to use the catkin_create_pkg script to create a new catkin package, and what you can do with it after it has been created.
本教程將會闡述如何使用catkin_create_pkg腳原本建立一個新的包,以及建立以後你用它作什麼。
First change to the source space directory of the catkin workspace you created in the Creating a Workspace for catkin tutorial:
首先更改到建立的catkin工做空間的源空間目錄中。
# You should have created this in the Creating a Workspace Tutorial
$ cd ~/catkin_ws/src
Now use the catkin_create_pkg script to create a new package called 'beginner_tutorials' which depends on std_msgs, roscpp, and rospy:
如今使用catkin_create_pkg腳原本建立一個新的包叫作「beginner_tutorials」,它依賴於std_msgs, roscpp和rospy。
$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
This will create a beginner_tutorials folder which contains a package.xml and a CMakeLists.txt, which have been partially filled out with the information you gave catkin_create_pkg. 這將會建立一個beginner_tutorials文件夾,其中包括一個package.xml和一個CMakeLists.txt文件,它們已經使用你給catkin_create_pkg的信息填充了一部分。
catkin_create_pkg requires that you give it a package_name and optionally a list of dependencies on which that package depends:
catkin_create_pkg要求你給它一個包的名字,可選擇性地一列依賴(該包所依賴的依賴)。
# This is an example, do not try to run this
# catkin_create_pkg <package_name> [depend1] [depend2] [depend3]
catkin_create_pkg also has more advanced functionalities which are described in catkin/commands/catkin_create_pkg.
catkin_create_pkg還有更多高級的功能,在catkin/commands/catkin_create_pkg(http://wiki.ros.org/catkin/commands/catkin_create_pkg)中描述。
(4)生成一個catkin工做空間,而且源setup文件
Now you need to build the packages in the catkin workspace:
如今你須要在catkin工做空間中生成包
$ cd ~/catkin_ws
$ catkin_make
After the workspace has been built it has created a similar structure in the devel subfolder as you usually find under /opt/ros/$ROSDISTRO_NAME.
在工做空間已經生成以後,它已經在devel子文件夾下建立了一個類似的結構,就像你常常在/opt/ros/$ROSDISTRO_NAME 下看到的同樣。
To add the workspace to your ROS environment you need to source the generated setup file:
要將該工做空間添加到你的ROS環境中,你須要將這些生成的setup文件源起來:
$ . ~/catkin_ws/devel/setup.bash
(5)包依賴
1)第一順序依賴
When using catkin_create_pkg earlier, a few package dependencies were provided. These first-order dependencies can now be reviewed with the rospack tool.
在早些時候使用catkin_create_pkg時,有幾個包依賴被提供了出來。這些第一順序的依賴如今可使用rospack工具回顧。
$ rospack depends1 beginner_tutorials
std_msgs
rospy roscpp
As you can see, rospack lists the same dependencies that were used as arguments when running catkin_create_pkg. These dependencies for a package are stored in the package.xml file: 就像你看到的同樣,rospack列出了運行catkin_create_pkg時被當作參數使用的相同的依賴。一個包的這些依賴存儲在package.xml文件中:
$ roscd beginner_tutorials
$ cat package.xml
<package>
... <buildtool_depend>catkin</buildtool_depend> <build_depend>roscpp</build_depend> <build_depend>rospy</build_depend> <build_depend>std_msgs</build_depend> ... </package>
2)間接依賴
In many cases, a dependency will also have its own dependencies. For instance, rospy has other dependencies.
在不少狀況下,一個依賴也會有它本身的依賴。好比,rospy有其餘的依賴。
$ rospack depends1 rospy
genpy
rosgraph rosgraph_msgs roslib std_msgs
A package can have quite a few indirect dependencies. Luckily rospack can recursively determine all nested dependencies.
一個包可能有好幾個間接的依賴。幸運地是,rospack能夠遞歸地決定全部嵌套的依賴。
$ rospack depends beginner_tutorials
cpp_common rostime roscpp_traits roscpp_serialization genmsg genpy message_runtime rosconsole std_msgs rosgraph_msgs xmlrpcpp roscpp rosgraph catkin rospack roslib rospy
(6)自定義你的包
This part of the tutorial will look at each file generated by catkin_create_pkg and describe, line by line, each component of those files and how you can customize them for your package.
這部分教程將檢查由catkin_create_pkg生成的每個文件,而後一行一行地描述這些文件的每個組件以及你如何才能爲你的包自定義它們。
1)自定義package.xml
The generated package.xml should be in your new package. Now lets go through the new package.xml and touch up any elements that need your attention.
生成的package.xml應該在你的新包裏。如今讓咱們看一下新的package.xml,對元素作一點符合你要求的小修改。
First update the description tag: 首先更新一下描述標籤:
5 <description>The beginner_tutorials package</description>
Change the description to anything you like, but by convention the first sentence should be short while covering the scope of the package. If it is hard to describe the package in a single sentence then it might need to be broken up.
將描述更改成你喜歡的任何內容,可是按照慣例第一行應該簡短描述包的覆蓋內容範圍。若是很難一句話描述包的話,那麼它可能須要斷句。
Next comes the maintainer tag: 接下來是維護者標籤:
7 <!-- One maintainer tag required, multiple allowed, one person per tag --> 8 <!-- Example: --> 9 <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> --> 10 <maintainer email="user@todo.todo">user</maintainer>
This is a required and important tag for the package.xml because it lets others know who to contact about the package. At least one maintainer is required, but you can have many if you like. The name of the maintainer goes into the body of the tag, but there is also an email attribute that should be filled out:
這是對package.xml來講須要的並且是重要的標籤,由於它讓其餘人知道向誰聯繫該包。至少須要一個維護者,可是你能夠加你想加的任意多個。維護者的名稱在標籤的body內部,可是還有一個email屬性你須要填寫一下:
7 <maintainer email="you@yourdomain.tld">Your Name</maintainer>
Next is the license tag, which is also required: 接下來是許可標籤,這也是須要的:
12 <!-- One license tag required, multiple allowed, one license per tag --> 13 <!-- Commonly used license strings: --> 14 <!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 --> 15 <license>TODO</license>
You should choose a license and fill it in here. Some common open source licenses are BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, and LGPLv3. You can read about several of these at the Open Source Initiative. For this tutorial we'll use the BSD license because the rest of the core ROS components use it already:
你應該選擇一個許可將它填在這裏。一些一般的開源許可有BSD,MIT,Boost Software License,GPLv2,GPLv3,LGPLv2.1,和LGPLv3。你能夠在Open Source Initiative讀到這幾個: http://opensource.org/licenses/alphabetical。對於本教程咱們將使用BSD許可由於核心的ROS組件已經使用了它:
8 <license>BSD</license>
The next set of tags describe the dependencies of your package. The dependencies are split into build_depend, buildtool_depend, run_depend, test_depend. For a more detailed explanation of these tags see the documentation about Catkin Dependencies. Since we passed std_msgs, roscpp, and rospy as arguments to catkin_create_pkg, the dependencies will look like this:
下一個標籤描述了你的包的依賴項。依賴項被分紅了build_depend(生成依賴),buildtool_depend(生成工具_依賴),run_depend(運行依賴),test_depend(測試依賴)。要想更詳細地瞭解這些標籤請看Catkin Dependencies(Catkin依賴)參考資料:http://wiki.ros.org/catkin/package.xml#Build.2C_Run.2C_and_Test_Dependencies。既然咱們傳遞了std_msgs, roscp,和rospy做爲catkin_create_pkg的參數,那麼依賴項將看上去像這個樣子:
27 <!-- The *_depend tags are used to specify dependencies --> 28 <!-- Dependencies can be catkin packages or system dependencies --> 29 <!-- Examples: --> 30 <!-- Use build_depend for packages you need at compile time: --> 31 <!-- <build_depend>genmsg</build_depend> --> 32 <!-- Use buildtool_depend for build tool packages: --> 33 <!-- <buildtool_depend>catkin</buildtool_depend> --> 34 <!-- Use run_depend for packages you need at runtime: --> 35 <!-- <run_depend>python-yaml</run_depend> --> 36 <!-- Use test_depend for packages you need only for testing: --> 37 <!-- <test_depend>gtest</test_depend> --> 38 <buildtool_depend>catkin</buildtool_depend> 39 <build_depend>roscpp</build_depend> 40 <build_depend>rospy</build_depend> 41 <build_depend>std_msgs</build_depend>
All of our listed dependencies have been added as a build_depend for us, in addition to the default buildtool_depend on catkin. In this case we want all of our specified dependencies to be available at build and run time, so we'll add a run_depend tag for each of them as well:
全部咱們列出的依賴都已經做爲build_depend(生成依賴)加進了,除了默認的buildtool_depend(生成工具依賴)填的是catkin。而由於咱們但願全部的特殊的依賴項均可以得到在build和run期間,因此咱們還要爲每個都加一個run_depend標籤:
12 <buildtool_depend>catkin</buildtool_depend> 13 14 <build_depend>roscpp</build_depend> 15 <build_depend>rospy</build_depend> 16 <build_depend>std_msgs</build_depend> 17 18 <run_depend>roscpp</run_depend> 19 <run_depend>rospy</run_depend> 20 <run_depend>std_msgs</run_depend>
As you can see the final package.xml, without comments and unused tags, is much more concise:
如你能看到的最終的package.xml,去掉了評論和沒用到的標籤以後,更加的簡潔了:
1 <?xml version="1.0"?> 2 <package> 3 <name>beginner_tutorials</name> 4 <version>0.1.0</version> 5 <description>The beginner_tutorials package</description> 6 7 <maintainer email="you@yourdomain.tld">Your Name</maintainer> 8 <license>BSD</license> 9 <url type="website">http://wiki.ros.org/beginner_tutorials</url> 10 <author email="you@yourdomain.tld">Jane Doe</author> 11 12 <buildtool_depend>catkin</buildtool_depend> 13 14 <build_depend>roscpp</build_depend> 15 <build_depend>rospy</build_depend> 16 <build_depend>std_msgs</build_depend> 17 18 <run_depend>roscpp</run_depend> 19 <run_depend>rospy</run_depend> 20 <run_depend>std_msgs</run_depend> 21 22 </package>
2)自定義CMakeLists.txt
Now that the package.xml, which contains meta information, has been tailored to your package, you are ready to move on in the tutorials. The CMakeLists.txt file created by catkin_create_pkg will be covered in the later tutorials about building ROS code.
既然package.xml,其中包含元信息,已經裁剪到你的包了,你如今能夠繼續接下來的教程了。對於由catkin_create_pkg建立的CMakeLists.txt文件將會在後續關於生成ROS代碼的時候涉及到。
Now that you've made a new ROS package, let's build our ROS package.
如今你已經制做了一個新的ROS包,那麼讓咱們開始生成咱們的ROS包吧。
4. Building a ROS Package 生成一個ROS包
This tutorial covers the toolchain to build a package.
該教程是介紹使用toolchain來生成一個包。
(1)生成包
As long as all of the system dependencies of your package are installed, we can now build your new package. 只要包的系統依賴都安裝了,那麼就能夠生成新包了。
Note: If you installed ROS using apt or some other package manager, you should already have all of your dependencies.若是你使用apt或其餘包管理器安裝的ROS,那麼依賴項均已經安裝好了
Before continuing remember to source your environment setup file if you have not already. On Ubuntu it would be something like this:
繼續以前記得源一下環境setup文件若是你尚未源的話。在Ubuntu上,它是這樣的:
$ source /opt/ros/%YOUR_ROS_DISTRO%/setup.bash
$ source /opt/ros/kinetic/setup.bash (For Kinetic for instance)
1)Using catkin_make使用catkin_make
catkin_make is a command line tool which adds some convenience to the standard catkin workflow. You can imagine that catkin_make combines the calls to cmake and make in the standard CMake workflow. catkin_make是一個爲標準catkin工做流帶來方便的命令行工具。
Usage: 用法:
# In a catkin workspace
$ catkin_make [make_targets] [-DCMAKE_VARIABLES=...]
For people who are unfamiliar with the standard CMake workflow, it breaks down as follows:
對於不熟悉標準CMake工做流的人來講,它能夠分解成以下:
Note: If you run the below commands it will not work, as this is just an example of how CMake generally works.
注意:若是你運行如下命令它將不能工做,由於這只是一個關於CMake大概如何工做的例子。
# In a CMake project
$ mkdir build $ cd build $ cmake .. $ make $ make install # (optionally)
This process is run for each CMake project. In contrast catkin projects can be built together in workspaces. Building zero to many catkin packages in a workspace follows this work flow:
這個過程適用於在每一個CMake工程中進行。相比之下多個catkin工程可以在工做空間中一塊兒生成。從一個工做空間中的0到許多catkin包生成來講能夠遵循這個工做流程:
# In a catkin workspace
$ catkin_make $ catkin_make install # (optionally)
The above commands will build any catkin projects found in the src folder. This follows the recommendations set by REP128. If your source code is in a different place, say my_src then you would call catkin_make like this:
以上命令將生成在src文件夾中的所能發現的任何catkin工程。這符合REP128的建議。若是你的源代碼放在不一樣的地方,好比說my_src那麼你能夠像如下這樣調用catkin_make:
Note: If you run the below commands it will not work, as the directory my_src does not exist.
注意:若是你運行如下命令它將不能工做,由於目錄my_src並不存在。
# In a catkin workspace
$ catkin_make --source my_src $ catkin_make install --source my_src # (optionally)
For more advanced uses of catkin_make see the documentation: catkin/commands/catkin_make
對於catkin_make的更多高級使用,請看資料:catkin/commands/catkin_make:http://wiki.ros.org/catkin/commands/catkin_make。
2)Building Your Package 生成你的包
For readers of this page who are about to build your own codes, please also take a look at later tutorial (C++)/(Python) since you may need to modify CMakeLists.txt.
對於想要生成本身代碼的文本的讀者,請再看一下後面的教程(C++)/(Python),由於你可能須要修改CMakeLists.txt。
You should already have a catkin workspace and a new catkin package called beginner_tutorials from the previous tutorial, Creating a Package. Go into the catkin workspace if you are not already there and look in the src folder:
經過以前的教程(建立一個包)你應該已經有一個catkin工做空間和一個叫beginner_tutorials的catkin包了 。進入catkin工做空間,若是你尚未在那裏的話,看一下src文件夾:
$ cd ~/catkin_ws/
$ ls src
beginner_tutorials/ CMakeLists.txt@
You should see that there is a folder called beginner_tutorials which you created with catkin_create_pkg in the previous tutorial. We can now build that package using catkin_make: 你應該看到有一個你以前用catkin_create_pkg建立的叫beginner_tutorials的文件夾。咱們如今可使用catkin_make生成那個包:
$ catkin_make
You should see a lot of output from cmake and them make, which should be similar to this:
你應該看到從cmake生成並輸出了不少內容, 它應該像這樣的:
Base path: /home/user/catkin_ws
Source space: /home/user/catkin_ws/src
Build space: /home/user/catkin_ws/build Devel space: /home/user/catkin_ws/devel Install space: /home/user/catkin_ws/install
####
#### Running command: "cmake /home/user/catkin_ws/src
-DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install" in "/home/user/catkin_ws/build" #### -- The C compiler identification is GNU 4.2.1 -- The CXX compiler identification is Clang 4.0.0 -- Checking whether C compiler has -isysroot -- Checking whether C compiler has -isysroot - yes -- Checking whether C compiler supports OSX deployment target flag -- Checking whether C compiler supports OSX deployment target flag - yes -- Check for working C compiler: /usr/bin/gcc -- Check for working C compiler: /usr/bin/gcc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - 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 -- Using CATKIN_DEVEL_PREFIX: /tmp/catkin_ws/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/kinetic -- This workspace overlays: /opt/ros/kinetic -- Found PythonInterp: /usr/bin/python (found version "2.7.1") -- Found PY_em: /usr/lib/python2.7/dist-packages/em.pyc -- Found gtest: gtests will be built -- catkin 0.5.51 -- BUILD_SHARED_LIBS is on -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing packages in topological order: -- ~~ - beginner_tutorials -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ add_subdirectory(beginner_tutorials) -- Configuring done -- Generating done -- Build files have been written to: /home/user/catkin_ws/build #### #### Running command: "make -j4" in "/home/user/catkin_ws/build" ####
Note that catkin_make first displays what paths it is using for each of the 'spaces'. The spaces are described in the REP128 and by documentation about catkin workspaces on the wiki: catkin/workspaces. The important thing to notice is that because of these default values several folders have been created in your catkin workspace. Take a look with ls:
注意catkin_make首先展現了什麼路徑它用來爲每個空間。這些空間在REP128裏以及在catkin wiki中關於catkin工做空間的參考資料中描述了。值得注意的重要事情是因爲這些默認的值幾個文件夾已經在你的catkin工做空間中建立了。使用ls來看一下吧:
$ ls
build
devel src
The build folder is the default location of the build space and is where cmake and make are called to configure and build your packages. The devel folder is the default location of the devel space, which is where your executables and libraries go before you install your packages.
build文件夾是生成空間的默認位置,是調用cmake和make來配置和生成你的包的地方。devel文件夾是devel空間的默認位置,是你的執行文件和類庫在你安裝你的包以前去的地方。
Now that you have built your ROS package let's talk more about ROS Nodes.
如今已經生成了你的ROS包,那麼讓咱們談論一下ROS節點吧。
5. Understanding ROS Nodes 理解ROS節點
This tutorial introduces ROS graph concepts and discusses the use of roscore, rosnode, and rosrun commandline tools.
該教程介紹ROS圖的概念,而且討論了roscore, rosnode和rosrun命令工具的用法。
(1)前提
For this tutorial we will use a lightweight simulator, please install it using
對於該教程咱們將使用一個輕量的模擬器,情使用如下命令安裝它:
$ sudo apt-get install ros-<distro>-ros-tutorials
Replace '<distro>' with the name of your ROS distribution (e.g. indigo, jade, kinetic)請用你的版本名稱代替其中的'<distro>'。
(2)快速瀏覽圖的概念
Nodes: A node is an executable that uses ROS to communicate with other nodes. 節點:節點是一個執行過程,它使用ROS來跟其餘節點交流。
Messages: ROS data type used when subscribing or publishing to a topic. 信息:當訂閱或者發佈到一個主題時使用的ROS數據類型。
Topics: Nodes can publish messages to a topic as well as subscribe to a topic to receive messages. 主題:節點除了能夠訂閱一個主題接收消息外還可以發佈消息到一個主題上。
Master: Name service for ROS (i.e. helps nodes find each other) 管理者:ROS的名稱服務。(好比,幫助節點尋找對方節點)
rosout: ROS equivalent of stdout/stderr rosout:等同於stdout/stderr的ROS。
roscore: Master + rosout + parameter server (parameter server will be introduced later) roscore:管理者+rosout+參數服務器(參數服務器後面會介紹)。
(3)節點
A node really isn't much more than an executable file within a ROS package. ROS nodes use a ROS client library to communicate with other nodes. Nodes can publish or subscribe to a Topic. Nodes can also provide or use a Service.
一個節點實際上只是一個ROS包內的執行文件。ROS節點使用一個ROS用戶類庫來與其餘節點交流。節點可以發佈或者訂閱主題。節點也能夠提供或者使用一個服務。
(4)用戶類庫
ROS client libraries allow nodes written in different programming languages to communicate:
ROS用戶類庫容許節點以不一樣的編程語言編寫,從而實現交流:
(5)roscore ROS內核
roscore is the first thing you should run when using ROS.
roscore是你使用ROS時你應該運行的第一個事情
Please run: 情運行:
$ roscore
You will see something similar to:你將會看到相似這樣的事情:
... logging to ~/.ros/log/9cf88ce4-b14d-11df-8a75-00251148e8cf/roslaunch-machine_name-13039.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB. started roslaunch server http://machine_name:33919/ ros_comm version 1.4.7 SUMMARY ====== PARAMETERS * /rosversion * /rosdistro NODES auto-starting new master process[master]: started with pid [13054] ROS_MASTER_URI=http://machine_name:11311/ setting /run_id to 9cf88ce4-b14d-11df-8a75-00251148e8cf process[rosout-1]: started with pid [13067] started core service [/rosout]
If roscore does not initialize, you probably have a network configuration issue. See Network Setup - Single Machine Configuration
若是roscore不可以初始化,你可能有一個網絡配置問題。請看網絡設置-單個機器配置:http://www.ros.org/wiki/ROS/NetworkSetup#Single_machine_configuration。
If roscore does not initialize and sends a message about lack of permissions, probably the ~/.ros folder is owned by root, change recursively the ownership of that folder with:
若是roscore不可以初始化,而且發送缺少容許的消息,也許~/.ros文件夾是由root擁有的,遞歸地改變那個文件夾的擁有權:
$ sudo chown -R <your_username> ~/.ros
(6)使用rosnode
Open up a new terminal, and let's use rosnode to see what running roscore did...
打開一個新的終端,讓咱們使用rosnode來看一下運行roscore是作什麼的...
Note: When opening a new terminal your environment is reset and your ~/.bashrc file is sourced. If you have trouble running commands like rosnode then you might need to add some environment setup files to your ~/.bashrc or manually re-source them.
注意:當打開一個新的終端時你的環境被重置了,你的~/.bashrc文件被源了。若是你運行像rosnode這樣的命令遇到了問題,那麼你可能須要添加一些環境setup文件到你的~/.bashrc中或者人工地重源它們。
rosnode displays information about the ROS nodes that are currently running. The rosnode list command lists these active nodes:
rosnode展現了你如今正在運行的ROS節點信息。
$ rosnode list
/rosout
This showed us that there is only one node running: rosout. This is always running as it collects and logs nodes' debugging output.
這向咱們展現了只有一個節點運行:rosout。這是總在運行着的,由於它收集而且將節點的調試輸出記錄到日誌中。
The rosnode info command returns information about a specific node.
rosnode info命令返回關於一個特殊節點的信息。
$ rosnode info /rosout
This gave us some more information about rosout, such as the fact that it publishes /rosout_agg.
這給咱們一些更多的關於rosout的信息,好比它發佈/rosout_agg的事情。
------------------------------------------------------------------------
Node [/rosout]
Publications: * /rosout_agg [rosgraph_msgs/Log] Subscriptions: * /rosout [unknown type] Services: * /rosout/set_logger_level * /rosout/get_loggers contacting node http://machine_name:54614/ ... Pid: 5092
Now, let's see some more nodes. For this, we're going to use rosrun to bring up another node.
如今,讓咱們看更多的節點。對於這個,咱們將使用rosrun來帶出另外一個節點。
(7)使用rosrun
rosrun allows you to use the package name to directly run a node within a package (without having to know the package path).
rosrun容許你使用包名來直接運行一個包中的一個節點(不須要知道包的路徑)。
Usage: 使用方法:
$ rosrun [package_name] [node_name]
So now we can run the turtlesim_node in the turtlesim package.
因此如今咱們能夠運行turtlesim包的turtlesim_node。
Then, in a new terminal:
而後,在一個新的終端:
$ rosrun turtlesim turtlesim_node
You will see the turtlesim window: 你會看到turtlesim的窗口:
NOTE: The turtle may look different in your turtlesim window. Don't worry about it - there are many types of turtle and yours is a surprise!
In a new terminal:
注意:烏龜可能在你的窗口中看上去不同。不要擔憂,有不少類型的烏龜,你的只是一個驚喜!
在新的終端:
$ rosnode list
You will see something similar to: 你會看到相似這樣的:
/rosout
/turtlesim
One powerful feature of ROS is that you can reassign Names from the command-line.
ROS一個強大的特色是你能夠從命令行重命名名字。
Close the turtlesim window to stop the node (or go back to the rosrun turtlesim terminal and use ctrl-C). Now let's re-run it, but this time use a Remapping Argument to change the node's name:
關掉turtlesim窗口中止該節點(或者返回到rosrun turtlesim終端,使用ctrl-C)。如今讓咱們重啓它,可是這一次使用一個Remapping Argument來更改節點的名字:
$ rosrun turtlesim turtlesim_node __name:=my_turtle
Now, if we go back and use rosnode list:如今,若是咱們回去,使用rosnode list:
$ rosnode list
/rosout
/my_turtle
Note: If you still see /turtlesim in the list, it might mean that you stopped the node in the terminal using ctrl-C instead of closing the window, or that you don't have the $ROS_HOSTNAME environment variable defined as described in Network Setup - Single Machine Configuration. You can try cleaning the rosnode list with: $ rosnode cleanup
注意:若是你仍是能在列表裏看到/turtlesim,那麼可能意味着你是使用ctrl -C中止的該終端節點而沒有關上窗口,或者你不具有「網絡Setup-單機器配置」中所描述的$ROS_HOSTNAME環境變量。你能夠試着使用$ rosnode cleanup清理rosnode列表。
We see our new /my_turtle node. Let's use another rosnode command, ping, to test that it's up:
咱們看到咱們新的/my_turtle節點。讓咱們使用另外一個rosnode命令,ping,來測試是否打開着:
$ rosnode ping my_turtle
rosnode: node is [/my_turtle]
pinging /my_turtle with a timeout of 3.0s xmlrpc reply from http://aqy:42235/ time=1.152992ms xmlrpc reply from http://aqy:42235/ time=1.120090ms xmlrpc reply from http://aqy:42235/ time=1.700878ms xmlrpc reply from http://aqy:42235/ time=1.127958ms
(8)複習
What was covered: 講了如下內容:
Now that you understand how ROS nodes work, let's look at how ROS topics work. Also, feel free to press Ctrl-C to stop turtlesim_node.
如今你已經理解了ROS節點工做的原理,那麼讓咱們來看一下ROS主題是如何工做的。並且,讓咱們儘管按Ctrl -C終止turtlesim_node吧。
6. Understanding ROS Topics 理解ROS主題
This tutorial introduces ROS topics as well as using the rostopic and rqt_plot commandline tools.
該教程不只介紹了使用rostopic和rqt_plot命令行工具,還介紹了ROS主題。
(1)安裝
1)roscore
Let's start by making sure that we have roscore running, in a new terminal:
讓咱們首先確保roscore運行着,在新的終端:
$ roscore
If you left roscore running from the last tutorial, you may get the error message:
若是你上一次教程遺留着roscore運行着的話,你可能會獲得錯誤的信息:
roscore cannot run as another roscore/master is already running.
Please kill other roscore/master processes before relaunching
This is fine. Only one roscore needs to be running.
這是能夠的。只有一個roscore須要運行着。
2)turtlesim
For this tutorial we will also use turtlesim. Please run in a new terminal:
對於本教程咱們仍是使用turtlesim。因此請在新的終端運行如下命令:
$ rosrun turtlesim turtlesim_node
3)turtle鍵盤遙控操做
We'll also need something to drive the turtle around with. Please run in a new terminal:
咱們還須要一點東西來使turtle啓動。請在新的終端輸入:
$ rosrun turtlesim turtle_teleop_key
[ INFO] 1254264546.878445000: Started node [/teleop_turtle], pid [5528], bound on [aqy], xmlrpc port [43918], tcpros port [55936], logging to [~/ros/ros/log/teleop_turtle_5528.log], using [real] time
Reading from keyboard --------------------------- Use arrow keys to move the turtle.
Now you can use the arrow keys of the keyboard to drive the turtle around. If you can not drive the turtle select the terminal window of the turtle_teleop_key to make sure that the keys that you type are recorded.
如今你可使用鍵盤的箭頭鍵來驅動來驅動烏龜運動。若是你不能驅動烏龜的話,請選擇turtle_teleop_key的終端窗口來確保你輸入的按鍵可以被記錄下來。
Now that you can drive your turtle around, let's look at what's going on behind the scenes.
如今你已經可以驅動烏龜運動了,讓咱們來看一下在畫面背後的所進行的事情吧。
(2)ROS主題
The turtlesim_node and the turtle_teleop_key node are communicating with each other over a ROS Topic. turtle_teleop_key is publishing the key strokes on a topic, while turtlesim subscribes to the same topic to receive the key strokes. Let's use rqt_graph which shows the nodes and topics currently running.
turtlesim_node和turtle_teleop_key節點之間是經過一個ROS主題進行交流的。turtle_teleop_key將鍵盤敲擊發送到一個主題上,turtlesim訂閱相同的主題來接收鍵盤敲擊。讓咱們使用rqt_graph,它展現了當前正在運行着的節點和主題。
Note: If you're using electric or earlier, rqt is not available. Use rxgraph instead.
注意:若是你正在使用electric或早先使用過,rqt是不可用的。使用rxgraph做爲替代。
1)使用rqt_graph
rqt_graph creates a dynamic graph of what's going on in the system. rqt_graph is part of the rqt package. Unless you already have it installed, run:
rqt_graph建立了一個系統內若是進行的動態圖。rqt_graph是rqt包的一部分。若是你尚未安裝它,運行:
$ sudo apt-get install ros-<distro>-rqt
$ sudo apt-get install ros-<distro>-rqt-common-plugins
replacing <distro> with the name of your ROS distribution (indigo, jade, kinetic) 將<distro>替換爲你的ROS包的版本如:kinetic.
In a new terminal:在一個新的終端:
$ rosrun rqt_graph rqt_graph
You will see something similar to: 你將會看到相似這樣的東西:
If you place your mouse over /turtle1/command_velocity it will highlight the ROS nodes (here blue and green) and topics (here red). As you can see, the turtlesim_node and the turtle_teleop_key nodes are communicating on the topic named /turtle1/command_velocity.
若是你將你的鼠標放到/turtle1/command_velocity,它將高亮顯示ROS節點(這裏是藍和綠)和主題(這裏是紅)。正如你所看到的,turtlesim_node和turtle_teleop_key節點之間經過主題/turtle1/command_velocity來進行交流。
2)介紹rostopic
The rostopic tool allows you to get information about ROS topics.
rostopic工具容許你獲取ROS主題的信息。
You can use the help option to get the available sub-commands for rostopic
你可使用幫助help選項來獲取可以使用的rostopic子命令。
$ rostopic -h
rostopic bw display bandwidth used by topic 顯示主題的帶寬
rostopic echo print messages to screen 打印消息到屏幕 rostopic hz display publishing rate of topic 顯示主題的發佈頻率 rostopic list print information about active topics 打印動態主題的信息 rostopic pub publish data to topic 將數據發佈到主題 rostopic type print topic type 打印主題類型
Let's use some of these topic sub-commands to examine turtlesim.
讓咱們使用這些主題子命令中的一些來測試turtlesim吧。
3)使用rostopic echo
rostopic echo shows the data published on a topic.
rostopic echo顯示發佈到主題上的數據。
Usage: 用法:
rostopic echo [topic]
Let's look at the command velocity data published by the turtle_teleop_key node.
讓咱們看一下由turtle_teleop_key節點發布的command velocity data。
For ROS Hydro and later, this data is published on the /turtle1/cmd_vel topic. In a new terminal, run:
對於ROS Hydro和以後的版本,數據是發佈到/turtle1/cmd_vel主題的。在一個新的終端,運行:
$ rostopic echo /turtle1/cmd_vel
For ROS Groovy and earlier, this data is published on the /turtle1/command_velocity topic. In a new terminal, run:
對於ROS Groovy和早先的版本,數據是發佈到/turtle1/command_velocity主題的。在一個新的終端,運行:
$ rostopic echo /turtle1/command_velocity
You probably won't see anything happen because no data is being published on the topic. Let's make turtle_teleop_key publish data by pressing the arrow keys. Remember if the turtle isn't moving you need to select the turtle_teleop_key terminal again. 你可能看不到有任何事情發生,由於尚未數據發佈到主題上呢。讓咱們經過按箭頭鍵使turtle_teleop_key發佈數據。
For ROS Hydro and later, you should now see the following when you press the up key:
對於ROS Hydro和以後的版本,當你按向上鍵的時候你如今應該看到如下的內容:
linear:
x: 2.0 y: 0.0 z: 0.0 angular: x: 0.0 y: 0.0 z: 0.0 --- linear: x: 2.0 y: 0.0 z: 0.0 angular: x: 0.0 y: 0.0 z: 0.0 ---
For ROS Groovy and earlier, you should now see the following when you press the up key:
對於ROS Groovy和早先的版本,當你按向上鍵的時候你如今應該看到如下的內容:
---
linear: 2.0 angular: 0.0 --- linear: 2.0 angular: 0.0 --- linear: 2.0 angular: 0.0 --- linear: 2.0 angular: 0.0 --- linear: 2.0 angular: 0.0
Now let's look at rqt_graph again. Press the refresh button in the upper-left to show the new node. As you can see rostopic echo, shown here in red, is now also subscribed to the turtle1/command_velocity topic.
如今讓咱們再次看一下rqt_graph。按一下左上方的刷新按鈕顯示新的節點。你可以看到rostopic echo,在這裏紅色顯示的,如今也被訂閱到了turtle1/command_velocity主題上。
4)使用rostopic list
rostopic list returns a list of all topics currently subscribed to and published.
rostopic list返回一列當前被訂閱的和發佈了的全部主題。
Let's figure out what argument the list sub-command needs. In a new terminal run:
讓咱們弄清楚list子命令須要什麼參數。在一個新終端運行:
$ rostopic list -h
Usage: rostopic list [/topic]
Options: -h, --help show this help message and exit -b BAGFILE, --bag=BAGFILE list topics in .bag file -v, --verbose list full details about each topic -p list only publishers -s list only subscribers
For rostopic list use the verbose option: 對於rostopic list使用verbose選項:
$ rostopic list -v
This displays a verbose list of topics to publish to and subscribe to and their type. 它顯示了一個詳細的發佈的和訂閱的主題列表。
Published topics:
* /turtle1/color_sensor [turtlesim/Color] 1 publisher * /turtle1/command_velocity [turtlesim/Velocity] 1 publisher * /rosout [roslib/Log] 2 publishers * /rosout_agg [roslib/Log] 1 publisher * /turtle1/pose [turtlesim/Pose] 1 publisher Subscribed topics: * /turtle1/command_velocity [turtlesim/Velocity] 1 subscriber * /rosout [roslib/Log] 1 subscriber
(3)ROS消息
Communication on topics happens by sending ROS messages between nodes. For the publisher (turtle_teleop_key) and subscriber (turtlesim_node) to communicate, the publisher and subscriber must send and receive the same type of message. This means that a topic type is defined by the message type published on it. The type of the message sent on a topic can be determined using rostopic type.
主題上的交流經過節點之間發送ROS消息產生。對於發佈者(turtle_teleop_key)和訂閱者(turtlesim_node)之間的交流,發佈者和訂閱者必須發送和接收同種類型的消息。這意味着一個主題類型是由發佈的消息類型定義的。發佈到一個主題的消息的類型能夠經過使用rostopic type來決定。
1)使用rostopic type
rostopic type returns the message type of any topic being published.
rostopic type返回發佈的任何主題的消息類型。
Usage: 用法:
rostopic type [topic]
For ROS Hydro and later,對於ROS Hydro和以後的版本,
$ rostopic type /turtle1/cmd_vel
geometry_msgs/Twist
We can look at the details of the message using rosmsg: 咱們能夠經過使用rosmsg看下消息的詳細信息:
$ rosmsg show geometry_msgs/Twist
geometry_msgs/Vector3 linear
float64 x float64 y float64 z geometry_msgs/Vector3 angular float64 x float64 y float64 z
For ROS Groovy and earlier, 對於ROS Groovy和早先的版本:
$ rostopic type /turtle1/command_velocity
turtlesim/Velocity
We can look at the details of the message using rosmsg:
$ rosmsg show turtlesim/Velocity
float32 linear
float32 angular
Now that we know what type of message turtlesim expects, we can publish commands to our turtle.
如今咱們已經知道了turtlesim所指望的消息的類型,咱們能夠將命令發佈到咱們的turtle上了。
(4)補充rostopic
Now that we have learned about ROS messages, let's use rostopic with messages.
如今咱們已經學習了ROS消息,讓咱們開始使用帶有消息的rostopic吧。
1)使用rostopic pub
rostopic pub publishes data on to a topic currently advertised.
rostopic pub發佈數據到一個當前公佈的主題上。
Usage: 用法:
rostopic pub [topic] [msg_type] [args]
For ROS Hydro and later, example: 對於ROS Hydro和以後的版本,例子:
$ rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
For ROS Groovy and earlier, example: 對於ROS Groovy和早先的版本,例子:
$ rostopic pub -1 /turtle1/command_velocity turtlesim/Velocity -- 2.0 1.8
The previous command will send a single message to turtlesim telling it to move with an linear velocity of 2.0, and an angular velocity of 1.8 .
以前的命令將發送一個消息給turtlesim告訴它要以線速度2.0,角速度1.8的速度來運動。
This is a pretty complicated example, so lets look at each argument in detail.
這是一個至關複雜的例子,讓咱們仔細地看下每個參數。
For ROS Hydro and later, 對於ROS Hydro和以後的版本,
This command will publish messages to a given topic: 這個命令將消息發佈到一個給定的主題上:
rostopic pub
This option (dash-one) causes rostopic to only publish one message then exit: 這個選項(-1)讓rostopic只發佈一個消息而後退出:
-1
This is the name of the topic to publish to: 這個是發佈到的主題的名字:
/turtle1/cmd_vel
This is the message type to use when publishing to the topic: 這個是發佈到主題時要使用的消息類型:
geometry_msgs/Twist
This option (double-dash) tells the option parser that none of the following arguments is an option. This is required in cases where your arguments have a leading dash -, like negative numbers.這個選項(雙破折號)告訴選項剖析器接下來的參數沒有一個是一個選項。這在你的參數有一個先導破折號-時是須要的,好比說負數。
--
As noted before, a geometry_msgs/Twist msg has two vectors of three floating point elements each: linear and angular. In this case, '[2.0, 0.0, 0.0]' becomes the linear value with x=2.0, y=0.0, and z=0.0, and '[0.0, 0.0, 1.8]' is the angular value with x=0.0, y=0.0, and z=1.8. These arguments are actually in YAML syntax, which is described more in the YAML command line documentation. 正如以前所提示的,一個geometry_msgs/Twist消息有兩個各有三個浮點型點要素的矢量:線性的和角度的。在這個例子中,'[2.0, 0.0, 0.0]'變成了x=2.0,y=0.0和z=0.0的線性值,'[0.0, 0.0, 1.8]'是一個x=0.0, y=0.0和z=1.8的角度值。這些參數其實是以YAML的語法寫成的,這在YAML命令行參考資料中描述的更多:http://wiki.ros.org/ROS/YAMLCommandLine。
'[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
For ROS Groovy and earlier, 若是是ROS Groovy及早先的版本的話,
This command will publish messages to a given topic:
rostopic pub
This option (dash-one) causes rostopic to only publish one message then exit:
-1
This is the name of the topic to publish to:
/turtle1/command_velocity
This is the message type to use when publishing to the topic:
turtlesim/Velocity
This option (double-dash) tells the option parser that none of the following arguments is an option. This is required in cases where your arguments have a leading dash -, like negative numbers.
--
As noted before, a turtlesim/Velocity msg has two floating point elements : linear and angular. In this case, 2.0 becomes the linear value, and 1.8 is the angular value. These arguments are actually in YAML syntax, which is described more in the YAML command line documentation.
2.0 1.8
You may have noticed that the turtle has stopped moving; this is because the turtle requires a steady stream of commands at 1 Hz to keep moving. We can publish a steady stream of commands using rostopic pub -r command:
你可能發現烏龜中止移動了;這是由於烏龜須要一個穩定的命令流以1Hz保持運動。咱們可使用rostopic pub -r命令發佈一個穩定命令流:
For ROS Hydro and later, 對於ROS Hydro和以後的版本,
$ rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'
For ROS Groovy and earlier,對於ROS Groovy和早先的版本,
$ rostopic pub /turtle1/command_velocity turtlesim/Velocity -r 1 -- 2.0 -1.8
This publishes the velocity commands at a rate of 1 Hz on the velocity topic. 它在速率主題上以1Hz的頻率發佈速率命令。
We can also look at what is happening in rqt_graph, The rostopic pub node (here in red) is communicating with the rostopic echo node (here in green):
咱們能夠在rqt_graph上看下發生了什麼,rostopic pub node(這裏是紅色)與rostopic echo node(這裏是綠色)交流。
As you can see the turtle is running in a continuous circle. In a new terminal, we can use rostopic echo to see the data published by our turtlesim:
正如你能夠看到烏龜是以持續的圓周運動。在一個新的節點上,咱們可使用rostopic echo來看下咱們的turtlesim發佈的數據:
2)使用rostopic hz
rostopic hz reports the rate at which data is published.
rostopic hz報告數據發佈的速率。
Usage: 用法:
rostopic hz [topic]
Let's see how fast the turtlesim_node is publishing /turtle1/pose: 讓咱們看下turtlesim_node發佈/turtle1/pose的速率多快:
$ rostopic hz /turtle1/pose
You will see: 你將會看到:
subscribed to [/turtle1/pose]
average rate: 59.354 min: 0.005s max: 0.027s std dev: 0.00284s window: 58 average rate: 59.459 min: 0.005s max: 0.027s std dev: 0.00271s window: 118 average rate: 59.539 min: 0.004s max: 0.030s std dev: 0.00339s window: 177 average rate: 59.492 min: 0.004s max: 0.030s std dev: 0.00380s window: 237 average rate: 59.463 min: 0.004s max: 0.030s std dev: 0.00380s window: 290
Now we can tell that the turtlesim is publishing data about our turtle at the rate of 60 Hz. We can also use rostopic type in conjunction with rosmsg show to get in depth information about a topic: 如今咱們能夠看出來turtlesim打印出了咱們的烏龜是以60Hz的速率的數據。咱們還可使用rostopic type結合rosmsg show來獲得一個主題的深度信息:
For ROS Hydro and later, 對於ROS Hydro和以後的版本,
$ rostopic type /turtle1/cmd_vel | rosmsg show
For ROS Groovy and earlier,
$ rostopic type /turtle1/command_velocity | rosmsg show
Now that we've examined the topics using rostopic let's use another tool to look at the data published by our turtlesim:
如今咱們已經經過使用rostopic測試了主題,讓咱們使用另外一個工具來看一下咱們的turtlesim所發佈的數據吧:
(6)使用rqt_plot
Note: If you're using electric or earlier, rqt is not available. Use rxplot instead.
注意:若是你使用electirc或早些時候使用過,rqt是不可用的。使用rxplot做爲替代。
rqt_plot displays a scrolling time plot of the data published on topics. Here we'll use rqt_plot to plot the data being published on the /turtle1/pose topic. First, start rqt_plot by typing
rqt_plot顯示了一個發佈到主題上的數據的捲動時間圖。這裏咱們使用rqt_plot來繪製發佈到/turtle1/pose主題上的數據。首先,在一個新的終端經過輸入如下內容來啓動rqt_plot:
$ rosrun rqt_plot rqt_plot
in a new terminal. In the new window that should pop up, a text box in the upper left corner gives you the ability to add any topic to the plot. Typing /turtle1/pose/x will highlight the plus button, previously disabled. Press it and repeat the same procedure with the topic /turtle1/pose/y. You will now see the turtle's x-y location plotted in the graph. 在這個新彈出的窗口中,左上角的文本框給你能夠向圖上添加任何主題的選項。輸入/turtle1/pose/x將會高亮顯示添加按鈕,以前是不可用的。按按鈕,而後重複添加主題/turtle1/pose/y。你將會看到烏龜的x-y位置圖。
Pressing the minus button shows a menu that allows you to hide the specified topic from the plot. Hiding both the topics you just added and adding /turtle1/pose/theta will result in the plot shown in the next figure. 按減號按鈕將容許你隱藏圖上的特定的主題。同時隱藏這兩個主題,添加/turtle1/pose/theta將出現下一個圖。
That's it for this section, use Ctrl-C to kill the rostopic terminals but keep your turtlesim running.
以上就是本教程的內容,使用Ctrl-C殺掉rostopic終端,可是保持你的turtlesim運行。
Now that you understand how ROS topics work, let's look at how services and parameters work.
如今你已經理解了ROS topic的工做原理,讓咱們看一下服務和參數是怎麼工做的吧。
The following video presents a small tutorial using turtlesim on ROS nodes and ROS topics.
如下視頻呈現了一個關於在ROS節點和ROS主題上使用turtlesim的小教程。
7. Understanding ROS Services and Parameters
This tutorial introduces ROS services, and parameters as well as using the rosservice and rosparam commandline tools.
該教程除了介紹rosservice和rosparam命令工具的使用方法以外,還介紹了ROS服務,以及參數。
Assuming your turtlesim_node is still running from the last tutorial, let's look at what services the turtlesim provides:
假設你上一節的教程的turtlesim_node節點還在運行着,讓咱們看一下turtlesim提供的服務吧:
(1)ROS服務
Services are another way that nodes can communicate with each other. Services allow nodes to send a request and receive a response.
服務是節點可以彼此之間交流的另外一種方式。服務容許節點發送一個請求並接收一個回覆。
(2)使用rosservice
rosservice can easily attach to ROS's client/service framework with services. rosservice has many commands that can be used on topics, as shown below:
rosservice可以經過服務輕鬆地依附上ROS的客戶端/服務框架。rosservice有不少用在主題的命令,以下面所顯示的:
Usage:用法:
rosservice list print information about active services 打印關於活動的服務的信息
rosservice call call the service with the provided args 呼叫指定參數的服務 rosservice type print service type 打印服務類型 rosservice find find services by service type 查找服務類型的服務 rosservice uri print service ROSRPC uri 打印服務RSRPC統一資源標識符
1)rosservice list
$ rosservice list
The list command shows us that the turtlesim node provides nine services: reset, clear, spawn, kill, turtle1/set_pen,/turtle1/teleport_absolute, /turtle1/teleport_relative, turtlesim/get_loggers, and turtlesim/set_logger_level. There are also two services related to the separate rosout node: /rosout/get_loggers and /rosout/set_logger_level.
list命令向咱們展現了turtlesim節點提供的九個服務:reset, clear, spawn, kill, turtle1/set_pen, /turtle1/teleport_absolute, /turtle1/teleport_relative, turtlesim/get_loggers,以及turtlesim/set_logger_level。
還有兩個跟分離rosout節點相關的服務:/rosout/get_loggers和/rosout/set_logger_level。
/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
Let's look more closely at the clear service using rosservice type:
讓咱們使用rosservice type來更進一步看清clear服務。
2)rosservice type
Usage: 用法:
rosservice type [service]
Let's find out what type the clear service is: 讓咱們看一下clear服務是什麼類型吧:
$ rosservice type /clear
std_srvs/Empty
This service is empty, this means when the service call is made it takes no arguments (i.e. it sends no data when making a request and receives no data when receiving a response). Let's call this service using rosservice call:
這個服務是空的,這意味着當這個服務被調用時,它不須要任何參數(例如,當發出一個請求時它不發送任何數據,收到一個回覆時不接收任何數據)。讓咱們使用rosservice call調用這個服務。
3)rosservice call
Usage: 用法
rosservice call [service] [args]
Here we'll call with no arguments because the service is of type empty: 如今咱們將經過無參數調用,由於該服務是空類型:
$ rosservice call /clear
This does what we expect, it clears the background of the turtlesim_node.
這將按照咱們指望的作,它清理了turtlesim_node的背景。
Let's look at the case where the service has arguments by looking at the information for the service spawn:
讓咱們經過service spawn查看信息看一下服務帶有參數的狀況。
$ rosservice type /spawn| rossrv show
float32 x
float32 y float32 theta string name --- string name
This service lets us spawn a new turtle at a given location and orientation. The name field is optional, so let's not give our new turtle a name and let turtlesim create one for us.
這個服務容許咱們在一個給定的位置和方向上spawn(渲染)一個新的烏龜。名字域是可選的,因此讓咱們不給咱們的新烏龜起名字,讓turtlesim爲咱們建立一個。
$ rosservice call /spawn 2 2 0.2 ""
The service call returns with the name of the newly created turtle
這個服務將返回新建立的烏龜的名字
name: turtle2
Now our turtlesim should look like this:
如今咱們的turtlesim應該看上去像這個樣子:
(3)使用rosparam
rosparam allows you to store and manipulate data on the ROS Parameter Server. The Parameter Server can store integers, floats, boolean, dictionaries, and lists. rosparam uses the YAML markup language for syntax. In simple cases, YAML looks very natural: 1 is an integer, 1.0 is a float, one is a string, true is a boolean, [1, 2, 3] is a list of integers, and {a: b, c: d} is a dictionary. rosparam has many commands that can be used on parameters, as shown below:
rosparam容許你在ROS參數服務器上存儲並操縱數據。參數服務器可以存儲整型,浮點型,布爾型,詞典,以及列數據。rosparam使用YAML標誌語言做爲語法規則。在簡單狀況下,YAML看起來很是天然:1是一個整型,1.0是浮點型,one是字符型,true是一個布爾型,[1, 2, 3]是一個整數列,{a:b, c:d}是一個詞典。rosparam有不少可以用在參數上的命令,以下所示。
Usage:
rosparam set set parameter 設置參數
rosparam get get parameter 獲取參數 rosparam load load parameters from file 從文件加載參數 rosparam dump dump parameters to file 將參數扔到文件中 rosparam delete delete parameter 刪除參數 rosparam list list parameter names 列出參數名字
Let's look at what parameters are currently on the param server:
讓咱們看一下如今在參數服務器上有什麼參數:
1)rosparam list
$ rosparam list
Here we can see that the turtlesim node has three parameters on the param server for background color:
這裏咱們可以看到turtlesim node在參數服務器上有三個關於背景色的參數:
/background_b
/background_g /background_r /roslaunch/uris/aqy:51932 /run_id
Let's change one of the parameter values using rosparam set:
讓咱們使用rosparam set更改其中的一個參數值:
2)rosparam set與rosparam get
Usage: 用法:
rosparam set [param_name]
rosparam get [param_name]
Here will change the red channel of the background color:
這裏將會更改背景顏色的紅色通道:
$ rosparam set /background_r 150
This changes the parameter value, now we have to call the clear service for the parameter change to take effect:
這將會更改參數值,如今咱們不得不調用clear服務使參數更改生效:
$ rosservice call /clear
Now our turtlesim looks like this:
如今咱們的turtlesim看起來是這樣的:
Now let's look at the values of other parameters on the param server. Let's get the value of the green background channel:
如今讓咱們看看參數服務器上的其餘參數的值。讓咱們獲取綠色背景通道的值:
$ rosparam get /background_g
86
We can also use rosparam get / to show us the contents of the entire Parameter Server.
咱們還能使用rosparam get /向咱們展現整個參數服務器的內容:
$ rosparam get /
background_b: 255
background_g: 86 background_r: 150 roslaunch: uris: {'aqy:51932': 'http://aqy:51932/'} run_id: e07ea71e-98df-11de-8875-001b21201aa8
You may wish to store this in a file so that you can reload it at another time. This is easy using rosparam:
你可能但願將這個存儲到一個文件中從而你可以在之後某個時間重載它。這經過使用rosparam能夠很容易實現:
3)rosparam dump和rosparam load
Usage: 用法:
rosparam dump [file_name] [namespace]
rosparam load [file_name] [namespace]
Here we write all the parameters to the file params.yaml
這裏咱們將全部的參數寫到params.yaml文件中
$ rosparam dump params.yaml
You can even load these yaml files into new namespaces, e.g. copy:
你甚至能夠將這些yaml文件加載到新的空間,好比copy:
$ rosparam load params.yaml copy
$ rosparam get /copy/background_b
255
Now that you understand how ROS services and params work, let's try using rqt_console and roslaunch
如今你已經理解了ROS服務和參數是怎麼工做的了,如今讓咱們試着使用rqt_console和roslaunch吧。
8. Using rqt_console and roslaunch
This tutorial introduces ROS using rqt_console and rqt_logger_level for debugging and roslaunch for starting many nodes at once. If you use ROS fuerte or ealier distros where rqt isn't fully available, please see this page with this page that uses old rx based tools.
該教程介紹了使用rqt_console和rqt_logger_level來調試ROS,使用roslaunch來一次啓動多個節點。若是你使用ROS fuerte或者更早的版本,那麼rqt是不徹底可用的,請查看這個頁面使用老的rx開頭的工具。
1)前提rqt和turtlesim包
The tutorial uses both the rqt and turtlesim packages. To do this tutorial, please install both packages, if you have not yet done so.
該教程使用rqt和turtlesim包。要學該教程,請安裝這兩個包,若是還沒安裝的話。
$ sudo apt-get install ros-<distro>-rqt ros-<distro>-rqt-common-plugins ros-<distro>-turtlesim
Replace <distro> with the name of your ROS distribution (e.g. indigo, jade, kinetic). 將<distro>替換成你的ROS版本。
NOTE: you may have already built rqt and turtlesim for one of the previous tutorials. If you are not sure, installing them again will not hurt anything.
注意:你可能已經在前面的教程中創建了rqt和turtlesim。若是你不肯定,再裝一次沒有任何害處。
(2)使用rqt_console和rqt_logger_level
rqt_console attaches to ROS's logging framework to display output from nodes. rqt_logger_level allows us to change the verbosity level (DEBUG, WARN, INFO, and ERROR) of nodes as they run.
rqt_console依附ROS的logging框架來顯示節點的輸出。rqt_logger_level容許咱們當它們運行時改變節點的冗長程度(DEBUG, WARN, INFO,和ERROR)。
Now let's look at the turtlesim output in rqt_console and switch logger levels in rqt_logger_level as we use turtlesim. Before we start the turtlesim, in two new terminals start rqt_console and rqt_logger_level:
如今讓咱們看一下rqt_console的turtlesim輸出,並當咱們使用turtlesim時切換rqt_logger_level的logger level。在咱們啓動turtlesim以前,在兩個新的終端啓動rqt_console和rqt_logger_level:
$ rosrun rqt_console rqt_console
$ rosrun rqt_logger_level rqt_logger_level
You will see two windows popup:
你將會看到兩個彈出的窗口:
Now let's start turtlesim in a new terminal:
如今讓咱們在一個新的終端開啓一個新的turtlesim:
$ rosrun turtlesim turtlesim_node
Since the default logger level is INFO you will see any info that the turtlesim publishes when it starts up, which should look like:
由於默認的logger level級別水平是INFO,你將會看到啓動時turtlesim發佈的任何信息,看上去就像這樣:
Now let's change the logger level to Warn by refreshing the nodes in the rqt_logger_level window and selecting Warn as shown below:
如今讓咱們在rqt_logger_level窗口刷新節點選擇Warn更改logger level級別爲Warn,以下圖所示:
Now let's run our turtle into the wall and see what is displayed in our rqt_console:
如今讓咱們運行咱們的烏龜到牆上,看看在咱們的rqt_console顯示的什麼:
For ROS Hydro and later,對於ROS Hydro和以後的版本,
rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0,y: 0.0,z: 0.0}}'
For ROS Groovy and earlier,對於ROS Groovy和以前的版本,
rostopic pub /turtle1/command_velocity turtlesim/Velocity -r 1 -- 2.0 0.0
1)關於logger levels級別快速筆記
Logging levels are prioritized in the following order:
Logging levels級別優先順序按以下排序:
Fatal
Error Warn Info Debug
Fatal has the highest priority and Debug has the lowest. By setting the logger level, you will get all messages of that priority level or higher. For example, by setting the level to Warn, you will get all Warn, Error, and Fatal logging messages.
Fatal是最高級別的,Debug是最低級別的。經過設置logger level級別,你將會獲得那個優先級或更高級別的全部信息。例如,將級別設置成Warn,你將會獲得全部Warn,Error以及Fatal的記錄信息。
Let's Ctrl-C our turtlesim and let's use roslaunch to bring up multiple turtlesim nodes and a mimicking node to cause one turtlesim to mimic another:
讓咱們按Ctrl-C終止咱們的turtlesim,讓咱們使用roslaunch來開啓多個turtlesim節點和一個模仿節點
2)使用roslaunch
roslaunch starts nodes as defined in a launch file.
roslaunch開啓一個launch文件中定義的節點。
Usage: 用法:
$ roslaunch [package] [filename.launch]
First go to the beginner_tutorials package we created and built earlier:
首先到咱們以前建立並生成的beginner_tutorials包中:
$ roscd beginner_tutorials
If roscd says similar to roscd: No such package/stack 'beginner_tutorials' , you will need to source the environment setup file like you did at the end of the create_a_workspace tutorial:
若是roscd說相似於roscd: No such package/stack 'beginner_tutorials',你須要像你在create_a_workspace教程結尾那樣源一下環境setup設置文件:
$ cd ~/catkin_ws
$ source devel/setup.bash $ roscd beginner_tutorials
Then let's make a launch directory: 而後讓咱們make一個launch目錄:
$ mkdir launch
$ cd launch
NOTE: The directory to store launch files don't necessarily have to be named as launch. In fact you don't even need to store them in a directory. roslaunch command automatically looks into the passed package and detect available launch files. However, it turned out to be a good practice.
注意:這個存儲launch文件的目錄不須要必定要命名爲launch。事實上你甚至不須要在一個路徑中存儲它們。roslaunch命令會自動查看過往的包檢測launch文件。可是,它會是一個好的實踐。
3)Launch文件
Now let's create a launch file called turtlemimic.launch and paste the following:
如今讓咱們建立一個叫turtlemimic的launch文件,粘貼以下內容:
1 <launch> 2 3 <group ns="turtlesim1"> 4 <node pkg="turtlesim" name="sim" type="turtlesim_node"/> 5 </group> 6 7 <group ns="turtlesim2"> 8 <node pkg="turtlesim" name="sim" type="turtlesim_node"/> 9 </group> 10 11 <node pkg="turtlesim" name="mimic" type="mimic"> 12 <remap from="input" to="turtlesim1/turtle1"/> 13 <remap from="output" to="turtlesim2/turtle1"/> 14 </node> 15 16 </launch>
5)launch文件解釋
Now, let's break the launch xml down.
如今,讓咱們將launch xml分解一下。
1 <launch>
Here we start the launch file with the launch tag, so that the file is identified as a launch file.
這裏咱們以launch標籤開始launch文件,從而該文件被定義爲一個launch文件。
Here we start two groups with a namespace tag of turtlesim1 and turtlesim2 with a turtlesim node with a name of sim. This allows us to start two simulators without having name conflicts.
這裏咱們開始兩個各有一個turtlesim節點命名爲sim的帶有turtlesim1和turtlesim2的命名空間標籤的羣。它容許咱們開始兩個模擬器而不會有名稱衝突。
11 <node pkg="turtlesim" name="mimic" type="mimic"> 12 <remap from="input" to="turtlesim1/turtle1"/> 13 <remap from="output" to="turtlesim2/turtle1"/> 14 </node>
Here we start the mimic node with the topics input and output renamed to turtlesim1 and turtlesim2. This renaming will cause turtlesim2 to mimic turtlesim1.
這裏咱們開始帶有主題輸入和輸出重命名爲turtlesim1和turtlesim2的模擬節點。這個重命名將引發turtlesim2模仿turtlesim1。
16 </launch>
This closes the xml tag for the launch file.
這個關閉launch文件的xml標籤。
5)roslaunching
Now let's roslaunch the launch file:
如今讓咱們roslaunch加載這個launch文件:
$ roslaunch beginner_tutorials turtlemimic.launch
Two turtlesims will start and in a new terminal send the rostopic command:
兩個turtlesim將會啓動,而且在一個新的終端發送rostopic命令:
For ROS Hydro and later,對於ROS Hydro和以後的版本,
$ rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'
For ROS Groovy and earlier,
$ rostopic pub /turtlesim1/turtle1/command_velocity turtlesim/Velocity -r 1 -- 2.0 -1.8
You will see the two turtlesims start moving even though the publish command is only being sent to turtlesim1.
你將會看到兩個turtlesim開始移動了雖然發佈命令只送給了turtlesim1。
We can also use rqt_graph to better understand what our launch file did. Run rqt's main window and select rqt_graph:
咱們還可使用rqt_graph來更好地理解咱們的launch文件作的事情。啓動rqt的主窗口,選擇rqt_graph:
$ rqt
Or simply: 或者簡單地:
$ rqt_graph
Now that you have successfully used rqt_console and roslaunch, let's learn about editor options for ROS. You can Ctrl-C all your turtlesims, as you will not need them for the next tutorials.
如今你已經成功地使用rqt_console和roslaunch了,讓咱們仍是學習ROS的編輯器選項吧。你能夠按Ctrl-C終止全部的turtlesim,由於你在接下來的教程中不須要用到它們了。
9. Using rosed to edit files in ROS 使用rosed在ROS中編輯文件
This tutorial shows how to use rosed to make editing easier.
該教程展現瞭如何使用rosed來讓編輯更加容易。
(1)使用rosed
rosed is part of the rosbash suite. It allows you to directly edit a file within a package by using the package name rather than having to type the entire path to the package.
rosed是rosbash套件的一部分。它容許你使用包名直接編輯一個包中的文件,而不用輸入包的整個路徑。
Usage: 用法:
$ rosed [package_name] [filename]
Example: 例子:
$ rosed roscpp Logger.msg
This example demonstrates how you would edit the Logger.msg file within the roscpp package.
這個例子展現了你是怎麼編輯roscpp包中的Loger.msg文件的。
If this example doesn't work is probably because you don't have the vim editor installed. Please refer to Editor section. If you don't know how to get out of vim, click here.
若是這個例子不能工做那多是由於你尚未安裝vim editor。請參考Editor一章。若是你不知道怎麼擺脫vim,請點擊這裏。
If the filename is not uniquely defined within the package, a menu will prompt you to choose which of the possible files you want to edit.
若是文件名不是惟一的,將會跳出一個菜單供你選擇你想編輯的哪一個文件。
(2)經過鍵入tab補全使用rosed
This way you can easily see and optionally edit all files from a package without knowing its exact name.
這種方式你能輕鬆地看到而且可選擇性地編輯一個包中的全部文件而不用知道它的確切名字。
Usage: 用法:
$ rosed [package_name] <tab><tab>
Example: 例子:
$ rosed roscpp <tab><tab>
Empty.srv package.xml
GetLoggers.srv roscpp-msg-extras.cmake Logger.msg roscpp-msg-paths.cmake SetLoggerLevel.srv roscpp.cmake genmsg_cpp.py roscppConfig-version.cmake gensrv_cpp.py roscppConfig.cmake msg_gen.py
(3)編輯器
The default editor for rosed is vim. The more beginner-friendly editor nano is included with the default Ubuntu install. You can use it by editing your ~/.bashrc file to include:
rosed默認的編輯器是vim。更適合初學者友好的編輯器nano在默認Ubuntu安裝時就已經包括了。你能夠經過編輯你的~/.bashrc文件加上這麼一段話使用它:
export EDITOR='nano -w'
To set the default editor to emacs you can edit your ~/.bashrc file to include: 要設置默認的編輯器爲emacs你能夠編輯你的~/.bashrc文件加上這麼一句:
export EDITOR='emacs -nw'
NOTE: changes in .bashrc will only take effect for new terminals. Terminals that are already open will not see the new environmental variable.
注意:.bashrc中的更改將只對新的終端有效。已經打開的終端是沒法看到新的環境變量的。
Open a new terminal and see if EDITOR is defined:
打開一個新的終端看一下編輯器是否已經已經設置了:
$ echo $EDITOR
nano -w
or emacs -nw
Now that you have successfully configured and used rosed, let's create a Msg and Srv.
如今你已經成功地設置和使用rosed了,讓咱們開始學習建立一個消息和服務吧。
10. Creating a ROS msg and srv 建立一個ROS消息和服務
This tutorial covers how to create and build msg and srv files as well as the rosmsg, rossrv and roscp commandline tools.
該教程除了介紹rosmsg, rossrv和roscp命令行工具以外,還介紹瞭如何建立和生成msg消息和srv服務文件。
(1)介紹msg和srv
msg: msg files are simple text files that describe the fields of a ROS message. They are used to generate source code for messages in different languages. msg文件是簡單的text文件,描述ROS的消息域。它們用來生成不一樣語言的消息的源代碼。
srv: an srv file describes a service. It is composed of two parts: a request and a response. 一個srv文件描述一個服務。它由兩部分組成:一個是請求和一個是回覆。
msg files are stored in the msg directory of a package, and srv files are stored in the srv directory.
msg文件存儲在一個包的msg路徑,srv文件存儲在srv路徑中。
msgs are just simple text files with a field type and field name per line. The field types you can use are:
msgs只是簡單的每行帶有域類型和域名的text文件。你能用的域類型有:
There is also a special type in ROS: Header, the header contains a timestamp and coordinate frame information that are commonly used in ROS. You will frequently see the first line in a msg file have Header header.
在ROS中還有一個特殊的類型:Header,header包含一個可以普遍用於ROS的時間戳和座標軸信息。你將會頻繁地看到msg文件的第一行有Header header。
Here is an example of a msg that uses a Header, a string primitive, and two other msgs :
這裏是一個msg的例子,使用了一個Header,一個string基元,和兩個其餘msgs:
Header header
string child_frame_id geometry_msgs/PoseWithCovariance pose geometry_msgs/TwistWithCovariance twist
srv files are just like msg files, except they contain two parts: a request and a response. The two parts are separated by a '---' line. Here is an example of a srv file:
srv文件就像msg文件,除了它們包含兩部分:一個請求和一個回覆。這兩部分是由一條「---」線分割開的。這裏是一個srv文件的例子:
int64 A
int64 B --- int64 Sum
In the above example, A and B are the request, and Sum is the response.
在上面的例子中,A和B是請求,Sum是回覆。
(2)使用msg消息
1)建立一個msg消息
Let's define a new msg in the package that was created in the previous tutorial.
讓咱們在以前教程中建立的包中定義一個新的msg消息。
$ roscd beginner_tutorials
$ mkdir msg $ echo "int64 num" > msg/Num.msg
The example .msg file above contains only 1 line. You can, of course, create a more complex file by adding multiple elements, one per line, like this:
上面的.msg例子中只包含一行。固然你能夠經過添加多個元素建立一個更加複雜的文件,1行一個,像這樣:
string first_name
string last_name uint8 age uint32 score
There's one more step, though. We need to make sure that the msg files are turned into source code for C++, Python, and other languages:
可是,要多一步。咱們須要確保msg文件轉換成源代碼如C++,Python和其它語言:
Open package.xml, and make sure these two lines are in it and uncommented:
打開package.xml,確保這兩行在其中而後消除註釋:
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
Note that at build time, we need "message_generation", while at runtime, we only need "message_runtime".
注意在生成的時候,咱們須要「message_generation」,而在運行的時候,咱們只須要「message_runtime」。
Open CMakeLists.txt in your favorite text editor (rosed from the previous tutorial is a good option).
在你喜歡的text編輯器(以前的教程中的rosed是一個好選擇)中打開CMakeLists.txt。
Add the message_generation dependency to the find_package call which already exists in your CMakeLists.txt so that you can generate messages. You can do this by simply adding message_generation to the list of COMPONENTS such that it looks like this:
添加message_generation依賴到已經在你的CMakeLists.txt中存在的find_package call中,從而你可以產生消息。你能夠經過簡單地添加message_generation到你的COMPONENTS列表中實現,就像這樣:
# Do not just add this to your CMakeLists.txt, modify the existing text to add message_generation before the closing parenthesis 不要只是添加這個到你的CMakeLists.txt中,修改已存在的文本在右括號以前添加message_generation
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation )
You may notice that sometimes your project builds fine even if you did not call find_package with all dependencies. This is because catkin combines all your projects into one, so if an earlier project calls find_package, yours is configured with the same values. But forgetting the call means your project can easily break when built in isolation.
你可能注意到有時候儘管你沒有調用帶有全部的依賴的find_package你的工程依然生成的很好。這是由於catkin將你的全部工程結合成了一個,因此若是一個早前的工程調用了find_package,你的也會使用相同的數值配置。可是省略調用意味着你的工程可以在單獨生成時能夠很容易地中斷。
Also make sure you export the message runtime dependency.
另外還須要確保你輸出消息運行時依賴。
catkin_package(
...
CATKIN_DEPENDS message_runtime ... ...)
Find the following block of code:
找到如下代碼模塊:
# add_message_files(
# FILES # Message1.msg # Message2.msg # )
Uncomment it by removing the # symbols and then replace the stand in Message*.msg files with your .msg file, such that it looks like this:
去註釋化,將#符號移除,而後用你的.msg文件取代替代品Message*.msg,看上去就像這樣的:
add_message_files(
FILES Num.msg )
By adding the .msg files manually, we make sure that CMake knows when it has to reconfigure the project after you add other .msg files.
經過人工地添加.msg文件,咱們能夠確保CMake知道在你添加了其餘了其餘.msg文件後何時從新裝配工程。
Now we must ensure the generate_messages() function is called.
如今咱們必須確保generate_messages()函數被調用。
For ROS Hydro and later, you need to uncomment these lines:
對於ROS Hydro和以後的版本,你須要去註釋化這些行:
# generate_messages(
# DEPENDENCIES # std_msgs # )
generate_messages(
DEPENDENCIES std_msgs )
In earlier versions, you may just need to uncomment one line: 在早期的版本,你可能須要去註釋化一行:
generate_messages()
Now you're ready to generate source files from your msg definition. If you want to do so right now, skip next sections to Common step for msg and srv.
如今你已經準備好從你的消息定義生成源文件了。若是你想如今就作這個話,跳過下一部分直接到msg和srv的通常步驟。
(2)使用rosmsg
That's all you need to do to create a msg. Let's make sure that ROS can see it using the rosmsg show command.
以上就是建立一個msg所須要的全部內容。讓咱們確保ROS可以經過使用rosmsg show命令看到它。
Usage: 用法:
$ rosmsg show [message type]
Example: 例子:
$ rosmsg show beginner_tutorials/Num
You will see: 你將會看到:
int64 num
In the previous example, the message type consists of two parts: 在早前的例子中,消息類型包含兩部分:
beginner_tutorials -- the package where the message is defined 消息定義的地方的包
Num -- The name of the msg Num. 消息的名字
If you can't remember which Package a msg is in, you can leave out the package name. Try: 若是你不記得msg是在哪一個包裏,你可以忽略包名。試一下:
$ rosmsg show Num
You will see: 你將會看到:
[beginner_tutorials/Num]:
int64 num
(4)使用srv
1)建立一個srv
Let's use the package we just created to create a srv:
讓咱們使用爲建立一個srv而建立的包:
$ roscd beginner_tutorials
$ mkdir srv
Instead of creating a new srv definition by hand, we will copy an existing one from another package.
不要手動建立一個新的srv定義,咱們從另外一個包中複製一個已存在的。
For that, roscp is a useful commandline tool for copying files from one package to another.
爲了實現上述操做,roscp是一個有用的從一個包複製文件到另外一個包的命令行工具。
Usage: 用法:
$ roscp [package_name] [file_to_copy_path] [copy_path]
Now we can copy a service from the rospy_tutorials package:
如今咱們能從rospy_tutorials包中複製一個服務:
$ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv
There's one more step, though. We need to make sure that the srv files are turned into source code for C++, Python, and other languages.
可是還有多一步。咱們須要確保srv文件轉換到源文件好比C++,Python和其餘語言。
Unless you have done so already, open package.xml, and make sure these two lines are in it and uncommented:
除非你已經作好了,打開package.xml,確保這兩行在其中,去註釋化:
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
As before, note that at build time, we need "message_generation", while at runtime, we only need "message_runtime".
如以前同樣,注意在生成的時候,咱們須要「message_generation」,而在運行的時候,咱們只須要「message_runtime」。
Unless you have done so already for messages in the previous step, add the message_generation dependency to generate messages in CMakeLists.txt:
若是你在以前的步驟中尚未作好消息,在CMakeLists.txt中添加message_generation依賴生成消息:
# Do not just add this line to your CMakeLists.txt, modify the existing line
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation )
(Despite its name, message_generation works for both msg and srv.) (儘管它的名稱,message_generation在msg和srv中都工做。)
Also you need the same changes to package.xml for services as for messages, so look above for the additional dependencies required.
並且你須要對package.xml對services作對messages相同的改變,因此看上面所需的多餘依賴。
Remove # to uncomment the following lines:
移除下面行的註釋#:
# add_service_files(
# FILES # Service1.srv # Service2.srv # )
And replace the placeholder Service*.srv files for your service files:
將佔位的Service*.srv文件替換爲你的service文件:
add_service_files(
FILES AddTwoInts.srv )
Now you're ready to generate source files from your service definition. If you want to do so right now, skip next sections to Common step for msg and srv.
如今已經準備好了生成源文件所需的服務定義。若是你想如今就想作的話,請跳過下面的環節直接到msg和srv的經常使用步驟。
2)使用rossrv
That's all you need to do to create a srv. Let's make sure that ROS can see it using the rossrv show command.
以上是你建立一個srv所需的所有內容。讓咱們使用rossrv show命令確保ROS能看到它。
Usage: 用法:
$ rossrv show <service type>
Example: 例子:
$ rossrv show beginner_tutorials/AddTwoInts
You will see: 你將會看到:
int64 a
int64 b --- int64 sum
Similar to rosmsg, you can find service files like this without specifying package name:
相似於rosmsg,你可以不用特製包名也能找到服務文件:
$ rossrv show AddTwoInts
[beginner_tutorials/AddTwoInts]: int64 a int64 b --- int64 sum [rospy_tutorials/AddTwoInts]: int64 a int64 b --- int64 sum
(5)msg和srv的通常步驟
Unless you have already done this in the previous steps, change in CMakeLists.txt. :
若是你以前的步驟還沒完成,請更改CMakeLists.txt文件:
# generate_messages(
# DEPENDENCIES # # std_msgs # Or other packages containing msgs # )
Uncomment it and add any packages you depend on which contain .msg files that your messages use (in this case std_msgs), such that it looks like this:
去註釋化,添加你依賴的任何包括你的消息使用的.msg文件(在該狀況下是std_msgs)的包,它看上去像這個樣子:
generate_messages(
DEPENDENCIES std_msgs )
Now that we have made some new messages we need to make our package again:
如今咱們已經制做了一些新消息咱們須要重建咱們的包:
# In your catkin workspace
$ roscd beginner_tutorials $ cd ../.. $ catkin_make install $ cd -
Any .msg file in the msg directory will generate code for use in all supported languages. The C++ message header file will be generated in ~/catkin_ws/devel/include/beginner_tutorials/. The Python script will be created in ~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg. The lisp file appears in ~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/msg/.
msg目錄中的任何.msg文件都會生成所支持的語言可用的代碼。C++消息頭文件將生成於~/catkin_ws/devel/include/beginner_tutorials/。Python腳本將建立在~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg。lisp文件將出如今~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/msg/。
Similarly, any .srv files in the srv directory will have generated code in supported languages. For C++, this will generate header files in the same directory as the message header files. For Python and Lisp, there will be an 'srv' folder beside the 'msg' folders.
相似地,srv目錄中的任何.srv文件都會生成支持語言下的代碼。對於C++,這個將生成如消息頭文件相同目錄中的頭文件。對於Python和Lisp,將會在'msg'文件夾旁邊有一個'srv'文件夾。
The full specification for the message format is available at the Message Description Language page.
消息格式的完整說明能夠在消息描述語言頁看到。
If you are building C++ nodes which use your new messages, you will also need to declare a dependency between your node and your message, as described in the catkin msg/srv build documentation.
若是你生成使用你新消息的C++節點,你還須要聲明一個你的節點和你的消息之間依賴,正如在catkin msg/srv build參考資料描述的那樣。
(6)獲取幫助
We've seen quite a few ROS tools already. It can be difficult to keep track of what arguments each command requires. Luckily, most ROS tools provide their own help.
咱們已經看到好幾個ROS工具了。可能很難記住每一個命令所須要的參數。幸運地是,大多數ROS工具提供了它們本身的幫助。
Try: 試一下:
$ rosmsg -h
You should see a list of different rosmsg subcommands. 你將會看到不一樣的rosmsg子命令
Commands:
rosmsg show Show message description rosmsg list List all messages rosmsg md5 Display message md5sum rosmsg package List messages in a package rosmsg packages List packages that contain messages
You can also get help for subcommands 你還能獲取子命令的幫助
$ rosmsg show -h
This shows the arguments that are needed for rosmsg show: 這個將會顯示rosmsg show所須要的參數:
Usage: rosmsg show [options] <message type>
Options: -h, --help show this help message and exit -r, --raw show raw message text, including comments
(7)複習
Let's just list some of the commands we've used so far: 讓咱們列出咱們目前使用過的一些命令:
roscd = ros+cd : changes directory to a ROS package or stack
rosls = ros+ls : lists files in a ROS package
roscp = ros+cp : copies files from/to a ROS package
(8)下一個教程
Now that you've made a new ROS msg and srv, let's look at writing a simple publisher and subscriber (python) (c++).
如今已經制做了一個新的ROS信息msg和服務srv,讓咱們寫一個簡單的發佈者和訂閱者(python)(c++)吧。
11. Writing a Simple Publisher and Subscriber (C++)
This tutorial covers how to write a publisher and subscriber node in C++.
該教程是介紹如何使用C++寫一個發佈器和訂閱器節點。
(1)寫發佈器節點
"Node" is the ROS term for an executable that is connected to the ROS network. Here we'll create a publisher ("talker") node which will continually broadcast a message.
「節點」是指跟ROS網絡有關的可執行文件的ROS術語。這裏咱們將建立一個發佈器(「talker」)節點,它將持續地發佈消息。
Change directories to your beginner_tutorials package you created in your catkin workspace previous tutorials:
更改目錄到你在以前教程中的catkin工做空間所建立的beginner_tutorials包。
roscd beginner_tutorials
1)代碼
Create a src directory in the beginner_tutorials package directory:
在beginner_tutorials包目錄中建立一個src目錄:
mkdir src
This directory will contain any source files for our beginner_tutorials package.
這個目錄將包含咱們的beginner_tutorials包所須要的任何資源文件。
Create the src/talker.cpp file within the beginner_tutorials package and paste the following inside it:
建立在beginner_tutorials包中建立src/talker.cpp文件,並在其中粘貼以下內容:
https://raw.github.com/ros/ros_tutorials/kinetic-devel/roscpp_tutorials/talker/talker.cpp
27 #include "ros/ros.h" 28 #include "std_msgs/String.h" 29 30 #include <sstream> 31 32 /** 33 * This tutorial demonstrates simple sending of messages over the ROS system. 34 */ 35 int main(int argc, char **argv) 36 { 37 /** 38 * The ros::init() function needs to see argc and argv so that it can perform 39 * any ROS arguments and name remapping that were provided at the command line. 40 * For programmatic remappings you can use a different version of init() which takes 41 * remappings directly, but for most command-line programs, passing argc and argv is 42 * the easiest way to do it. The third argument to init() is the name of the node. 43 * 44 * You must call one of the versions of ros::init() before using any other 45 * part of the ROS system. 46 */ 47 ros::init(argc, argv, "talker"); 48 49 /** 50 * NodeHandle is the main access point to communications with the ROS system. 51 * The first NodeHandle constructed will fully initialize this node, and the last 52 * NodeHandle destructed will close down the node. 53 */ 54 ros::NodeHandle n; 55 56 /** 57 * The advertise() function is how you tell ROS that you want to 58 * publish on a given topic name. This invokes a call to the ROS 59 * master node, which keeps a registry of who is publishing and who 60 * is subscribing. After this advertise() call is made, the master 61 * node will notify anyone who is trying to subscribe to this topic name, 62 * and they will in turn negotiate a peer-to-peer connection with this 63 * node. advertise() returns a Publisher object which allows you to 64 * publish messages on that topic through a call to publish(). Once 65 * all copies of the returned Publisher object are destroyed, the topic 66 * will be automatically unadvertised. 67 * 68 * The second parameter to advertise() is the size of the message queue 69 * used for publishing messages. If messages are published more quickly 70 * than we can send them, the number here specifies how many messages to 71 * buffer up before throwing some away. 72 */ 73 ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000); 74 75 ros::Rate loop_rate(10); 76 77 /** 78 * A count of how many messages we have sent. This is used to create 79 * a unique string for each message. 80 */ 81 int count = 0; 82 while (ros::ok()) 83 { 84 /** 85 * This is a message object. You stuff it with data, and then publish it. 86 */ 87 std_msgs::String msg; 88 89 std::stringstream ss; 90 ss << "hello world " << count; 91 msg.data = ss.str(); 92 93 ROS_INFO("%s", msg.data.c_str()); 94 95 /** 96 * The publish() function is how you send messages. The parameter 97 * is the message object. The type of this object must agree with the type 98 * given as a template parameter to the advertise<>() call, as was done 99 * in the constructor above. 100 */ 101 chatter_pub.publish(msg); 102 103 ros::spinOnce(); 104 105 loop_rate.sleep(); 106 ++count; 107 } 108 109 110 return 0; 111 }
2)代碼解釋
Now, let's break the code down.
如今,讓咱們分解代碼。
27 #include "ros/ros.h" 28
ros/ros.h is a convenience include that includes all the headers necessary to use the most common public pieces of the ROS system.
ros/ros.h是一個包括全部必須的ROS系統的公共部分的頭文件的便利引用。
28 #include "std_msgs/String.h" 29
This includes the std_msgs/String message, which resides in the std_msgs package. This is a header generated automatically from the String.msg file in that package. For more information on message definitions, see the msg page.
這包含了std_msgs/String消息,它是屬於std_msgs包。這是一個那個包中的String.msg文件自動生成的文件頭。要查看關於消息定義的更多的消息,請查看msg頁:http://wiki.ros.org/msg。
47 ros::init(argc, argv, "talker");
Initialize ROS. This allows ROS to do name remapping through the command line -- not important for now. This is also where we specify the name of our node. Node names must be unique in a running system.
初始化ROS。它容許ROS經過命令行作名稱重映射——目前不重要。這也是咱們指定咱們的節點的名字的地方。節點的名字對於一個運行的系統必須是惟一的。
The name used here must be a base name, ie. it cannot have a / in it.
這裏使用的名字必須是一個base name。它不能包含/號。
54 ros::NodeHandle n;
Create a handle to this process' node. The first NodeHandle created will actually do the initialization of the node, and the last one destructed will cleanup any resources the node was using.
建立一個這個處理節點的句柄。第一個建立的NodeHandle將會作節點的初始化工做,最後一個銷燬的NodeHandle將會清理該節點使用的任何資源。
73 ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
Tell the master that we are going to be publishing a message of type std_msgs/String on the topic chatter. This lets the master tell any nodes listening on chatter that we are going to publish data on that topic. The second argument is the size of our publishing queue. In this case if we are publishing too quickly it will buffer up a maximum of 1000 messages before beginning to throw away old ones.
告訴管理器咱們將在主題chatter上發佈一個std_msgs/String類型的消息。這讓管理器告訴監聽chatter的任何節點咱們將會在那個主題上發佈數據。第二個參數是咱們發佈隊列的尺寸大小。若是咱們發佈太快將會在開始扔掉舊的消息時緩衝最多1000個消息。
NodeHandle::advertise() returns a ros::Publisher object, which serves two purposes: 1) it contains a publish() method that lets you publish messages onto the topic it was created with, and 2) when it goes out of scope, it will automatically unadvertise.
NodeHandle::advertise()返回一個ros::Publisher對象,它有兩個目的:1)它包含一個publish()方法讓你發佈消息到伴隨它建立的主題上,以及2)當超出範圍時它將自動取消通知。
75 ros::Rate loop_rate(10);
A ros::Rate object allows you to specify a frequency that you would like to loop at. It will keep track of how long it has been since the last call to Rate::sleep(), and sleep for the correct amount of time.
一個ros:rate對象容許你指定一個你喜歡的循環頻率。它將追蹤自上次調用Rate::sleep()函數過去多長時間了,而且睡眠正確的時間長度。
In this case we tell it we want to run at 10Hz.
在本例中咱們告訴它咱們想運行在10Hz頻率。
81 int count = 0; 82 while (ros::ok()) 83 {
By default roscpp will install a SIGINT handler which provides Ctrl-C handling which will cause ros::ok() to return false if that happens.
默認狀況下roscpp將安裝一個SIGINT(中斷信號)句柄,它會提供Ctrl-C操做會觸發ros::ok()返回否若是那個發生了。
ros::ok() will return false if:
ros::ok()在如下狀況下將會返回否:
ros::shutdown() has been called by another part of the application. 應用程序的另外一部分調用ros::shutdown()
all ros::NodeHandles have been destroyed 全部的ros::NodeHandle被銷燬
Once ros::ok() returns false, all ROS calls will fail. 一旦ros::ok()返回否,全部的ROS調用都會失敗。
We broadcast a message on ROS using a message-adapted class, generally generated from a msg file. More complicated datatypes are possible, but for now we're going to use the standard String message, which has one member: "data".
咱們經過使用一個由msg文件產生的消息調節類在ROS上廣播消息。更多複雜的數據類型是可能的,可是如今咱們使用標準String消息,它有一個成員:"data"。
101 chatter_pub.publish(msg);
Now we actually broadcast the message to anyone who is connected.
如今咱們實際地將消息廣播給任何鏈接的對象。
93 ROS_INFO("%s", msg.data.c_str());
ROS_INFO and friends are our replacement for printf/cout. See the rosconsole documentation for more information.
ROS_INFO和friends友們是咱們對於printf/count的替代品。看一下rosconsole參考資料獲取更多信息:http://wiki.ros.org/rosconsole。
103 ros::spinOnce();
Calling ros::spinOnce() here is not necessary for this simple program, because we are not receiving any callbacks. However, if you were to add a subscription into this application, and did not have ros::spinOnce() here, your callbacks would never get called. So, add it for good measure.
這裏調用ros::spinOnce()對於這個簡單的程序並非必須的,由於咱們並無接收任何回調。可是,若是你向你的程序添加一個訂閱,而這裏並無ros::spinOnce(),你的回調將不會獲得調用。因此,添加一個。
105 loop_rate.sleep();
Now we use the ros::Rate object to sleep for the time remaining to let us hit our 10Hz publish rate.
如今咱們使用ros::Rate對象,讓它睡眠一段時間讓咱們達到10Hz發佈率。
Here's the condensed version of what's going on:
這裏是關於正在進行的簡約版:
Advertise that we are going to be publishing std_msgs/String messages on the chatter topic to the master 向管理器宣告咱們將發佈std_msgs/String類型的消息到chatter主題上
Loop while publishing messages to chatter 10 times a second 循環發佈消息到chatter,1秒10次。
Now we need to write a node to receive the messsages.
如今咱們須要寫一個節點來接收消息。
(2)寫訂閱者代碼
1)代碼
Create the src/listener.cpp file within the beginner_tutorials package and paste the following inside it:
在beginner_tutorials包內建立src/listener.cpp文件,並在其中粘貼如下內容:
https://raw.github.com/ros/ros_tutorials/kinetic-devel/roscpp_tutorials/listener/listener.cpp
28 #include "ros/ros.h" 29 #include "std_msgs/String.h" 30 31 /** 32 * This tutorial demonstrates simple receipt of messages over the ROS system. 33 */ 34 void chatterCallback(const std_msgs::String::ConstPtr& msg) 35 { 36 ROS_INFO("I heard: [%s]", msg->data.c_str()); 37 } 38 39 int main(int argc, char **argv) 40 { 41 /** 42 * The ros::init() function needs to see argc and argv so that it can perform 43 * any ROS arguments and name remapping that were provided at the command line. 44 * For programmatic remappings you can use a different version of init() which takes 45 * remappings directly, but for most command-line programs, passing argc and argv is 46 * the easiest way to do it. The third argument to init() is the name of the node. 47 * 48 * You must call one of the versions of ros::init() before using any other 49 * part of the ROS system. 50 */ 51 ros::init(argc, argv, "listener"); 52 53 /** 54 * NodeHandle is the main access point to communications with the ROS system. 55 * The first NodeHandle constructed will fully initialize this node, and the last 56 * NodeHandle destructed will close down the node. 57 */ 58 ros::NodeHandle n; 59 60 /** 61 * The subscribe() call is how you tell ROS that you want to receive messages 62 * on a given topic. This invokes a call to the ROS 63 * master node, which keeps a registry of who is publishing and who 64 * is subscribing. Messages are passed to a callback function, here 65 * called chatterCallback. subscribe() returns a Subscriber object that you 66 * must hold on to until you want to unsubscribe. When all copies of the Subscriber 67 * object go out of scope, this callback will automatically be unsubscribed from 68 * this topic. 69 * 70 * The second parameter to the subscribe() function is the size of the message 71 * queue. If messages are arriving faster than they are being processed, this 72 * is the number of messages that will be buffered up before beginning to throw 73 * away the oldest ones. 74 */ 75 ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback); 76 77 /** 78 * ros::spin() will enter a loop, pumping callbacks. With this version, all 79 * callbacks will be called from within this thread (the main one). ros::spin() 80 * will exit when Ctrl-C is pressed, or the node is shutdown by the master. 81 */ 82 ros::spin(); 83 84 return 0; 85 }
2)代碼解釋
Now, let's break it down piece by piece, ignoring some pieces that have already been explained above.
如今,讓咱們一片一片分解它,忽略以前已經解釋過的部分。
34 void chatterCallback(const std_msgs::String::ConstPtr& msg) 35 { 36 ROS_INFO("I heard: [%s]", msg->data.c_str()); 37 }
This is the callback function that will get called when a new message has arrived on the chatter topic. The message is passed in a boost shared_ptr, which means you can store it off if you want, without worrying about it getting deleted underneath you, and without copying the underlying data.
這是回調函數,它將會在當一個新消息到chatter主題時獲得調用。該消息傳遞到一個boost shared_ptr,這意味着只要你喜歡你能夠存儲起來,不須要擔憂它會不當心被刪除掉,不須要拷貝下層的數據。
75 ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
Subscribe to the chatter topic with the master. ROS will call the chatterCallback() function whenever a new message arrives. The 2nd argument is the queue size, in case we are not able to process messages fast enough. In this case, if the queue reaches 1000 messages, we will start throwing away old messages as new ones arrive.
向管理器訂閱chatter主題。每當一個新的消息到來時ROS將會調用chatterCallback()函數。第二個參數是序列尺寸大小,以防咱們不能足夠快地處理消息。在該狀況下,若是序列到達1000個消息的話,當新消息到來時咱們將開始扔掉舊消息。
NodeHandle::subscribe() returns a ros::Subscriber object, that you must hold on to until you want to unsubscribe. When the Subscriber object is destructed, it will automatically unsubscribe from the chatter topic.
NodeHandle::subscribe()將返回一個ros::Subscriber對象,你必需要堅持抓住它直到你想要取消訂閱。當訂閱對象被銷燬時,它將自動地從chatter主題上取消訂閱。
There are versions of the NodeHandle::subscribe() function which allow you to specify a class member function, or even anything callable by a Boost.Function object. The roscpp overview contains more information.
有多個版本的NodeHandle::subscribe()函數,它容許你指定一個類成員函數,或者甚至是一個Boost.Function對象的任何可調用的東西。更多信息請看roscpp概況:http://wiki.ros.org/roscpp/Overview。
82 ros::spin();
ros::spin() enters a loop, calling message callbacks as fast as possible. Don't worry though, if there's nothing for it to do it won't use much CPU. ros::spin() will exit once ros::ok() returns false, which means ros::shutdown() has been called, either by the default Ctrl-C handler, the master telling us to shutdown, or it being called manually.
ros::spin()儘量快地輸入一個循環、調用消息回調。不要擔憂是否沒有足夠的CPU,它用不了太多。ros::spin()將會退出一旦ros::ok()返回否,那意味着ros::shutdown()被調用了,或者是由默認的Ctrl-C句柄,是管理器告訴咱們關掉的,或者是人工調用的。
There are other ways of pumping callbacks, but we won't worry about those here. The roscpp_tutorials package has some demo applications which demonstrate this. The roscpp overview also contains more information.
也有其餘方法引發回調,可是咱們這裏不用擔憂這些。在roscpp_tutorials包有一些demo應用闡述這個:http://wiki.ros.org/roscpp_tutorials。在roscpp概覽有更多的消息:http://wiki.ros.org/roscpp/Overview。
Again, here's a condensed version of what's going on: 再一次,讓咱們看一下咱們進行的簡潔版:
Subscribe to the chatter topic 訂閱chatter主題
When a message arrives, the chatterCallback() function is called 當一個消息到來時,chatterCallback()函數被調用。
(3)生成你的代碼
You used catkin_create_pkg in a previous tutorial which created a package.xml and a CMakeLists.txt file for you.
你使用前面教程中的catkin_create_pkg,它曾爲你生成了package.xml和CMakeLists.txt文件。
The generated CMakeLists.txt should look like this (with modifications from the Creating Msgs and Srvs tutorial and unused comments and examples removed):
生成的CMakeLists.txt應該看上去像這樣(從Creating Msgs and Srvs教程作了更改,移除了無用的註釋和例子):
1 cmake_minimum_required(VERSION 2.8.3) 2 project(beginner_tutorials) 3 4 ## Find catkin and any catkin packages 5 find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg) 6 7 ## Declare ROS messages and services 8 add_message_files(DIRECTORY msg FILES Num.msg) 9 add_service_files(DIRECTORY srv FILES AddTwoInts.srv) 10 11 ## Generate added messages and services 12 generate_messages(DEPENDENCIES std_msgs) 13 14 ## Declare a catkin package 15 catkin_package()
Don't worry about modifying the commented (#) examples, simply add these few lines to the bottom of your CMakeLists.txt:
不要擔憂修改註釋的例子,只要添加幾行到底部就能夠了:
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp) target_link_libraries(talker ${catkin_LIBRARIES}) add_dependencies(talker beginner_tutorials_generate_messages_cpp) add_executable(listener src/listener.cpp) target_link_libraries(listener ${catkin_LIBRARIES}) add_dependencies(listener beginner_tutorials_generate_messages_cpp)
Your resulting CMakeLists.txt file should look like this:
你的結果CMakeLists.txt文件應該看上去像這樣:
1 cmake_minimum_required(VERSION 2.8.3) 2 project(beginner_tutorials) 3 4 ## Find catkin and any catkin packages 5 find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg) 6 7 ## Declare ROS messages and services 8 add_message_files(FILES Num.msg) 9 add_service_files(FILES AddTwoInts.srv) 10 11 ## Generate added messages and services 12 generate_messages(DEPENDENCIES std_msgs) 13 14 ## Declare a catkin package 15 catkin_package() 16 17 ## Build talker and listener 18 include_directories(include ${catkin_INCLUDE_DIRS}) 19 20 add_executable(talker src/talker.cpp) 21 target_link_libraries(talker ${catkin_LIBRARIES}) 22 add_dependencies(talker beginner_tutorials_generate_messages_cpp) 23 24 add_executable(listener src/listener.cpp) 25 target_link_libraries(listener ${catkin_LIBRARIES}) 26 add_dependencies(listener beginner_tutorials_generate_messages_cpp)
This will create two executables, talker and listener, which by default will go into package directory of your devel space, located by default at ~/catkin_ws/devel/lib/<package name>.
這將會生成2個執行文件,talker和listener,它們默認會進入你的devel space包路徑,位置默認在~/catkin_ws/devel/lib/<package name>。
Note that you have to add dependencies for the executable targets to message generation targets:
注意你必須添加消息生成目標的執行目標依賴。
add_dependencies(talker beginner_tutorials_generate_messages_cpp)
This makes sure message headers of this package are generated before being used. If you use messages from other packages inside your catkin workspace, you need to add dependencies to their respective generation targets as well, because catkin builds all projects in parallel. As of *Groovy* you can use the following variable to depend on all necessary targets:
這將保證該包的消息頭在使用以前就生成了。若是你使用你的catkin工做空間中的其餘包消息,你還須要添加它們的相應的生成目標依賴,由於catkin平行地生成全部的工程。對於Groovy版本你可使用以下的變量添加全部必須的目標的依賴。
target_link_libraries(talker ${catkin_LIBRARIES})
You can invoke executables directly or you can use rosrun to invoke them. They are not placed in '<prefix>/bin' because that would pollute the PATH when installing your package to the system. If you wish for your executable to be on the PATH at installation time, you can setup an install target, see: catkin/CMakeLists.txt
你能夠直接invoke執行文件,或者你可使用rosrun來invoke它們。它們不放在'<prefix>/bin',由於那樣的話當安裝你的包到系統時會污染路徑。若是你但願你的執行文件當安裝時在路徑中,你能夠設置一個安裝目標,看一下:catkin/CMakeLists.txt:http://wiki.ros.org/catkin/CMakeLists.txt。
For more detailed discription of the CMakeLists.txt file see: catkin/CMakeLists.txt
對於CMakeLists.txt文件的更多詳細描述請看:catkin/CMakeLists.txt:http://wiki.ros.org/catkin/CMakeLists.txt。
Now run catkin_make: 如今運行catkin_make:
# In your catkin workspace
$ catkin_make
Note: Or if you're adding as new pkg, you may need to tell catkin to force making by --force-cmake option. See catkin/Tutorials/using_a_workspace#With_catkin_make.
注意:或者若是你做爲新的pkg添加,你可能須要告訴catkin來經過--force-cake選項強制生成。看一下catkin/Tutorials/using_a_workspace#With_catkin_make:http://wiki.ros.org/catkin/Tutorials/using_a_workspace#With_catkin_make。
Now that you have written a simple publisher and subscriber, let's examine the simple publisher and subscriber.
如今你已經寫了一個簡單的發佈者和訂閱者了,讓咱們測試一下這個簡單的發佈器和訂閱器吧。
(4)額外的資源
Here are some additional resources contributed by the community:
這裏是一些由社區貢獻的額外的資源。
1)視頻教程
The following video presents a small tutorial explaining how to write and test a publisher and subscriber in ROS with C++ and Python based on the talker/listener example above
如下的視頻呈現了一個小教程,解釋瞭如何在ROS上用C++和Python基於上述的talker/listener例子寫並測試一個發佈器和訂閱器。
12. Writing a Simple Publisher and Subscriber (Python)
This tutorial covers how to write a publisher and subscriber node in python.
本教程介紹瞭如何使用Python寫一個發佈器和訂閱器節點。
13. Examining the Simple Publisher and Subscriber
This tutorial examines running the simple publisher and subscriber.
本教程測試了運行簡單的發佈器和訂閱器。
(1)運行發佈器
Make sure that a roscore is up and running: 確保roscore已經啓動並運行:
$ roscore
catkin specific If you are using catkin, make sure you have sourced your workspace's setup.sh file after calling catkin_make but before trying to use your applications: catkin肯定你是否使用catkin,確保你在你嘗試使用你的程序以前已經調用catkin_make源了你的工做空間的setup.sh文件。
# In your catkin workspace
$ cd ~/catkin_ws $ source ./devel/setup.bash
In the last tutorial we made a publisher called "talker". Let's run it: 在上一教程中咱們已經生成了一個發佈器叫「talker」。讓咱們運行它:
$ rosrun beginner_tutorials talker (C++)
$ rosrun beginner_tutorials talker.py (Python)
You will see something similar to: 你將會看到相似於這樣的東西:
[INFO] [WallTime: 1314931831.774057] hello world 1314931831.77
[INFO] [WallTime: 1314931832.775497] hello world 1314931832.77 [INFO] [WallTime: 1314931833.778937] hello world 1314931833.78 [INFO] [WallTime: 1314931834.782059] hello world 1314931834.78 [INFO] [WallTime: 1314931835.784853] hello world 1314931835.78 [INFO] [WallTime: 1314931836.788106] hello world 1314931836.79
The publisher node is up and running. Now we need a subscriber to receive messages from the publisher.發佈器節點已經開啓並運行了。如今咱們須要一個訂閱器接收發布器傳來的消息。
(2)運行訂閱器
In the last tutorial we made a subscriber called "listener". Let's run it: 在上一節教程中咱們建立了一個訂閱器叫「listener"。讓咱們運行它:
$ rosrun beginner_tutorials listener (C++)
$ rosrun beginner_tutorials listener.py (Python)
You will see something similar to: 你將會看到相似於這樣的東西:
[INFO] [WallTime: 1314931969.258941] /listener_17657_1314931968795I heard hello world 1314931969.26
[INFO] [WallTime: 1314931970.262246] /listener_17657_1314931968795I heard hello world 1314931970.26 [INFO] [WallTime: 1314931971.266348] /listener_17657_1314931968795I heard hello world 1314931971.26 [INFO] [WallTime: 1314931972.270429] /listener_17657_1314931968795I heard hello world 1314931972.27 [INFO] [WallTime: 1314931973.274382] /listener_17657_1314931968795I heard hello world 1314931973.27 [INFO] [WallTime: 1314931974.277694] /listener_17657_1314931968795I heard hello world 1314931974.28 [INFO] [WallTime: 1314931975.283708] /listener_17657_1314931968795I heard hello world 1314931975.28
Now that you have examined the simple publisher and subscriber, let's write a simple service and client (python) (c++).
如今你已經測試了簡單的發佈器和訂閱器,讓咱們寫一個簡單的服務和客戶端吧(Python)(C++)。
14. Writing a Simple Service and Client (C++)
This tutorial covers how to write a service and client node in C++.
這個教程介紹的是如何使用C++寫一個服務和客戶端節點。
(1)寫一個服務節點
Here we'll create the service ("add_two_ints_server") node which will receive two ints and return the sum.
這裏咱們將建立一個服務節點("add_two_ints_server"),它將會接收兩個int整數,而後返回一個和。
Change directories to your beginner_tutorials package you created in your catkin workspace previous tutorials:
更改路徑到以前教程你在你的catkin工做空間建立的beginner_tutorials包
roscd beginner_tutorials
Please make sure you have followed the directions in the previous tutorial for creating the service needed in this tutorial, creating the AddTwoInts.srv (be sure to choose the right version of build tool you're using at the top of wiki page in the link).
請確保你已經遵守以前的教程的指導建立了本教程所須要的服務,請參考:creating the AddTwoInts.srv(請在wiki頁頂部連接上確保選擇了正確的build工具版本)。
1)代碼
Create the src/add_two_ints_server.cpp file within the beginner_tutorials package and paste the following inside it:
在beginner_tutorials包中建立src/add_two_ints_server.cpp文件,並在其中粘貼如下內容:
1 #include "ros/ros.h" 2 #include "beginner_tutorials/AddTwoInts.h" 3 4 bool add(beginner_tutorials::AddTwoInts::Request &req, 5 beginner_tutorials::AddTwoInts::Response &res) 6 { 7 res.sum = req.a + req.b; 8 ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b); 9 ROS_INFO("sending back response: [%ld]", (long int)res.sum); 10 return true; 11 } 12 13 int main(int argc, char **argv) 14 { 15 ros::init(argc, argv, "add_two_ints_server"); 16 ros::NodeHandle n; 17 18 ros::ServiceServer service = n.advertiseService("add_two_ints", add); 19 ROS_INFO("Ready to add two ints."); 20 ros::spin(); 21 22 return 0; 23 }
2)代碼解釋
Now, let's break the code down.
如今,讓咱們分解代碼。
1 #include "ros/ros.h" 2 #include "beginner_tutorials/AddTwoInts.h" 3
beginner_tutorials/AddTwoInts.h is the header file generated from the srv file that we created earlier.
beginner_tutorials/AddTwoInts.h是咱們早些時候建立的srv文件產生的頭文件。
4 bool add(beginner_tutorials::AddTwoInts::Request &req, 5 beginner_tutorials::AddTwoInts::Response &res)
This function provides the service for adding two ints, it takes in the request and response type defined in the srv file and returns a boolean.
該方法提供了添加兩個int的服務,它吸取srv文件中定義的請求和回覆類型,返回一個布爾型。
Here the two ints are added and stored in the response. Then some information about the request and response are logged. Finally the service returns true when it is complete.
這裏兩個int整數相加並存儲在回覆中。而後一些關於請求和回覆的信息記錄了下來。最終當完成時服務返回真。
18 ros::ServiceServer service = n.advertiseService("add_two_ints", add);
Here the service is created and advertised over ROS.
這裏服務被建立了而且在ROS上展示了出來。
(2)寫客戶端代碼
1)代碼
Create the src/add_two_ints_client.cpp file within the beginner_tutorials package and paste the following inside it:
在beginner_tutorials包中建立src/add_two_ints_client.cpp文件,並在其中粘貼如下內容:
1 #include "ros/ros.h" 2 #include "beginner_tutorials/AddTwoInts.h" 3 #include <cstdlib> 4 5 int main(int argc, char **argv) 6 { 7 ros::init(argc, argv, "add_two_ints_client"); 8 if (argc != 3) 9 { 10 ROS_INFO("usage: add_two_ints_client X Y"); 11 return 1; 12 } 13 14 ros::NodeHandle n; 15 ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints"); 16 beginner_tutorials::AddTwoInts srv; 17 srv.request.a = atoll(argv[1]); 18 srv.request.b = atoll(argv[2]); 19 if (client.call(srv)) 20 { 21 ROS_INFO("Sum: %ld", (long int)srv.response.sum); 22 } 23 else 24 { 25 ROS_ERROR("Failed to call service add_two_ints"); 26 return 1; 27 } 28 29 return 0; 30 }
2)代碼解釋
Now, let's break the code down.
如今,讓咱們分解代碼。
15 ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");
This creates a client for the add_two_ints service. The ros::ServiceClient object is used to call the service later on.
這個建立一個相加兩個整數(」add_two_ints「)服務的客戶端。ros::ServiceClient對象用來晚些時候調用服務。
16 beginner_tutorials::AddTwoInts srv; 17 srv.request.a = atoll(argv[1]); 18 srv.request.b = atoll(argv[2]);
Here we instantiate an autogenerated service class, and assign values into its request member. A service class contains two members, request and response. It also contains two class definitions, Request and Response.
這裏咱們實例化一個自動生成的服務類,並把值賦給它的請求成員。一個服務類包含兩個成員,請求和回覆。它還包括兩個類定義,請求和回覆。
19 if (client.call(srv))
This actually calls the service. Since service calls are blocking, it will return once the call is done. If the service call succeeded, call() will return true and the value in srv.response will be valid. If the call did not succeed, call() will return false and the value in srv.response will be invalid.
這個其實是調用服務。若是服務調用正佔用着,它將在調用完成後返回。若是服務調用成功了,call()將返回真以及srv中的值。回覆將是有效的。若是調用不成功,call()將返回否以及srv中的值。回覆將是無效的。
(3)生成你的代碼
Again edit the beginner_tutorials CMakeLists.txt located at ~/catkin_ws/src/beginner_tutorials/CMakeLists.txt and add the following at the end:
再一次編輯位於~/catkin_ws/src/beginner_tutorials/CMakeLists.txt的beginner_tutorials CMakeLists.txt,在尾部添加以下內容:
27 add_executable(add_two_ints_server src/add_two_ints_server.cpp) 28 target_link_libraries(add_two_ints_server ${catkin_LIBRARIES}) 29 add_dependencies(add_two_ints_server beginner_tutorials_gencpp) 30 31 add_executable(add_two_ints_client src/add_two_ints_client.cpp) 32 target_link_libraries(add_two_ints_client ${catkin_LIBRARIES}) 33 add_dependencies(add_two_ints_client beginner_tutorials_gencpp)
This will create two executables, add_two_ints_server and add_two_ints_client, which by default will go into package directory of your devel space, located by default at ~/catkin_ws/devel/lib/<package name>. You can invoke executables directly or you can use rosrun to invoke them. They are not placed in '<prefix>/bin' because that would pollute the PATH when installing your package to the system. If you wish for your executable to be on the PATH at installation time, you can setup an install target, see: catkin/CMakeLists.txt
這將會生成兩個可執行文件,add_two_ints_server和add_two_ints_client,它們默認狀況下將會進入你的devel space包目錄中,默認路徑爲~/catkin_ws/devel/lib/<package name>。你能夠直接invoke可執行文件,或者你也可使用rosrun來invoke它們。它們並不放在'<prefix>/bin',由於那樣會在將你的包安裝到系統時污染路徑。若是你但願你的可執行文件在安裝時存在在該路徑中,你能夠設置一個安裝目標,看:
For more detailed description of the CMakeLists.txt file see: catkin/CMakeLists.txt
對於CMakeLists.txt文件的更多詳細描述請看:catkin/CMakeLists.txt:http://wiki.ros.org/catkin/CMakeLists.txt。
Now run catkin_make: 如今運行catkin_make:
# In your catkin workspace
cd ~/catkin_ws catkin_make
If your build fails for some reason: 若是你由於某些緣由生成失敗:
make sure you have followed the directions in the previous tutorial: creating the AddTwoInts.srv. 請保證你遵循以前教程的步驟:建立AddTwoInts.srv。
Now that you have written a simple service and client, let's examine the simple service and client.
如今你已經寫了一個簡單的服務和客戶端,讓咱們測試一下簡單服務和客戶端吧。
15. Writing a Simple Service and Client (Python)
This tutorial covers how to write a service and client node in python.
16. Examining the Simple Service and Client
This tutorial examines running the simple service and client.
該教程測試了運行簡單的服務和客戶端。
(1)運行服務
Let's start by running the service: 讓咱們開始運行服務:
$ rosrun beginner_tutorials add_two_ints_server (C++)
$ rosrun beginner_tutorials add_two_ints_server.py (Python)
You should see something similar to: 你將會看到相似於這樣的內容:
Ready to add two ints.
(2)運行客戶端
Now let's run the client with the necessary arguments: 如今讓咱們運行客戶端:
$ rosrun beginner_tutorials add_two_ints_client 1 3 (C++)
$ rosrun beginner_tutorials add_two_ints_client.py 1 3 (Python)
You should see something similar to: 你應該會看到相似這樣的東西:
Requesting 1+3
1 + 3 = 4
Now that you've successfully run your first server and client, let's learn how to record and play back data.
如今你已經成功地運行你的第一個服務器和客戶端了,讓咱們開始學習如何記錄和重放數據。
(3)關於服務和客戶端節點的更多的例子
If you want to investigate further and get a hands-on example, you can get one here. A simple Client and Service combination shows the use of custom message types. The Service node is written in C++ while the Client is available in C++, Python and LISP.
若是你想要探究的更遠並有一個能夠動手操做的例子, 那麼你在這裏能夠得到一個:https://github.com/fairlight1337/ros_service_examples/。一個簡單的客戶端和服務相結合展示了用戶消息類型的使用。服務節點是用C++寫的,而客戶端是能夠用C++,Python和LISP實現。
17. Recording and playing back data 記錄和回放數據
This tutorial will teach you how to record data from a running ROS system into a .bag file, and then to play back the data to produce similar behavior in a running system.
該教程將教給你如何從一個運行的ROS系統記錄數據到一個.bag文件,而後回放數據從而在運行系統中產生類似的行爲。
(1)記錄數據(建立一個bag文件)
This section of the tutorial will instruct you how to record topic data from a running ROS system. The topic data will be accumulated in a bag file.
這部分教程將指導你如何從一個運行的ROS系統中記錄主題數據。主題數據將收集在一個bag文件中。
First, execute the following two commands:
首先,執行下面的兩個命令:
roscore
rosrun turtlesim turtlesim_node rosrun turtlesim turtle_teleop_key
This will start two nodes - the turtlesim visualizer and a node that allows for the keyboard control of turtlesim using the arrows keys on the keyboard. If you select the terminal window from which you launched turtle_keyboard, you should see something like the following:
這將會啓動兩個節點 - turtlesim可視化器和一個容許使用鍵盤箭頭鍵鍵盤控制turtlesim的節點。若是你選擇了啓動turtle_keyboard的終端窗口,你將會看到相似如下內容的東西:
Reading from keyboard
--------------------------- Use arrow keys to move the turtle.
Pressing the arrow keys on the keyboard should cause the turtle to move around the screen. Note that to move the turtle you must have the terminal from which you launched turtlesim selected and not the turtlesim window.
按鍵盤上的箭頭鍵將引起烏龜在屏幕上移動。注意要讓烏龜移動你必須是在啓動turtlesim選擇的終端,而不是turtlesim窗口。
1)記錄發佈的主題
First lets examine the full list of topics that are currently being published in the running system. To do this, open a new terminal and execute the command:
首先讓咱們測試一下目前發佈在運行系統的全部主題列表。要作這個,請打開一個新終端而後執行命令:
rostopic list -v
This should yield the following output:
這將產生以下的輸出:
Published topics:
* /turtle1/color_sensor [turtlesim/Color] 1 publisher
* /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisher * /rosout [rosgraph_msgs/Log] 2 publishers * /rosout_agg [rosgraph_msgs/Log] 1 publisher * /turtle1/pose [turtlesim/Pose] 1 publisher Subscribed topics: * /turtle1/cmd_vel [geometry_msgs/Twist] 1 subscriber * /rosout [rosgraph_msgs/Log] 1 subscriber
The list of published topics are the only message types that could potentially be recorded in the data log file, as only published messages are recorded. The topic /turtle1/cmd_vel is the command message published by teleop_turtle that is taken as input by the turtlesim process. The messages /turtle1/color_sensor and /turtle1/pose are output messages published by turtlesim.
已發佈的主題列表是惟一的可以可能被記錄在數據log文件中的消息類型,由於只有已發佈的消息被記錄下來。主題/turtle1/cmd_vel是由teleop_turtle發佈的命令消息,做爲turtlesim處理的輸入。消息/turtle1/color_sensor和/turtle1/pose是由turtlesim發佈的輸出消息。
We now will record the published data. Open a new terminal window. In this window run the following commands:
咱們如今將記錄已發佈數據。打開一個新的終端窗口。在該窗口中運行以下命令:
mkdir ~/bagfiles
cd ~/bagfiles rosbag record -a
Here we are just making a temporary directory to record data and then running rosbag record with the option -a, indicating that all published topics should be accumulated in a bag file.
這裏咱們只是建立一個臨時的目錄用來記錄數據,而後運行帶有-a選項的rosbag record命令,代表全部已發佈主題都應該被收集到一個bag文件中。
Move back to the terminal window with turtle_teleop and move the turtle around for 10 or so seconds.
回到turtle_teleop終端窗口,移動烏龜10秒鐘左右。
In the window running rosbag record exit with a Ctrl-C. Now examine the contents of the directory ~/bagfiles. You should see a file with a name that begins with the year, data, and time and the suffix .bag. This is the bag file that contains all topics published by any node in the time that rosbag record was running.
在運行rosbag record的窗口中使用Ctrl-C退出。如今測試目錄~/bagfiles中的內容。你應該看到一個以年,數據和時間以及.bag後綴命名的文件。這是包含全部在rosbag record運行期間任何節點已發佈主題的包文件。
(2)測試並回放bag文件
Now that we've recorded a bag file using rosbag record we can examine it and play it back using the commands rosbag info and rosbag play. First we are going to see what's recorded in the bag file. We can do the info command -- this command checks the contents of the bag file without playing it back. Execute the following command from the bagfiles directory:
如今咱們已經使用rosbag record記錄了一個bag文件。咱們從而可使用命令rosbag info和rosbag play來測試它並回放它。首先咱們將看到在bag文件中記錄的是什麼。咱們可使用info命令--這個命令檢查bag文件中的內容,並無回放。從bag文件目錄執行如下命令:
rosbag info <your bagfile>
You should see something like:
path: 2014-12-10-20-08-34.bag
version: 2.0 duration: 1:38s (98s) start: Dec 10 2014 20:08:35.83 (1418270915.83) end: Dec 10 2014 20:10:14.38 (1418271014.38) size: 865.0 KB messages: 12471 compression: none [1/1 chunks] types: geometry_msgs/Twist [9f195f881246fdfa2798d1d3eebca84a] rosgraph_msgs/Log [acffd30cd6b6de30f120938c17c593fb] turtlesim/Color [353891e354491c51aabe32df673fb446] turtlesim/Pose [863b248d5016ca62ea2e895ae5265cf9] topics: /rosout 4 msgs : rosgraph_msgs/Log (2 connections) /turtle1/cmd_vel 169 msgs : geometry_msgs/Twist /turtle1/color_sensor 6149 msgs : turtlesim/Color /turtle1/pose 6149 msgs : turtlesim/Pose
This tells us topic names and types as well as the number (count) of each message topic contained in the bag file. We can see that of the topics being advertised that we saw in the rostopic output, four of the five were actually published over our recording interval. As we ran rosbag record with the -a flag it recorded all messages published by all nodes.
這個除了告訴咱們在bag文件中包含的每一個消息主題的數目以外,還告訴了咱們主題名字和類型。咱們能夠看到咱們在rostopic輸出中看到的展現的主題,5箇中的4個都在咱們的記錄間隔內實際地發佈。當咱們運行帶有-a標籤的rosbag record時它記錄了全部節點發布的全部消息。
The next step in this tutorial is to replay the bag file to reproduce behavior in the running system. First kill the teleop program that may be still running from the previous section - a Ctrl-C in the terminal where you started turtle_teleop_key. Leave turtlesim running. In a terminal window run the following command in the directory where you took the original bag file:
該教程的下一步就是回放bag文件來重製運行系統中的行爲。首先終止在以前部分可能還在運行着的teleop程序-在你啓動turtle_teleop_key的終端使用Ctrl-C。
rosbag play <your bagfile>
In this window you should immediately see something like: 在窗口你應該很快會看到相似這樣的東西:
[ INFO] [1418271315.162885976]: Opening 2014-12-10-20-08-34.bag
Waiting 0.2 seconds after advertising topics... done. Hit space to toggle paused, or 's' to step.
In its default mode rosbag play will wait for a certain period (.2 seconds) after advertising each message before it actually begins publishing the contents of the bag file. Waiting for some duration allows any subscriber of a message to be alerted that the message has been advertised and that messages may follow. If rosbag play publishes messages immediately upon advertising, subscribers may not receive the first several published messages. The waiting period can be specified with the -d option.
在默認模式下rosbag play將在它真正開始發佈bag文件內容以前在宣佈每一個消息以後等待一段時間(0.2秒)。等待一段時間容許任何消息的訂閱者注意消息已經被宣佈了並且消息將會追蹤。若是rosbag play公佈時當即發佈消息,訂閱者可能沒有收到開始的幾個發佈的消息。等待階段可使用-d選項指定。
Eventually the topic /turtle1/cmd_vel will be published and the turtle should start moving in turtlesim in a pattern similar to the one you executed from the teleop program. The duration between running rosbag play and the turtle moving should be approximately equal to the time between the original rosbag record execution and issuing the commands from the keyboard in the beginning part of the tutorial. You can have rosbag play not start at the beginning of the bag file but instead start some duration past the beginning using the -s argument. A final option that may be of interest is the -r option, which allows you to change the rate of publishing by a specified factor. If you execute:
最後主題/turlte1/cmd_vel將被髮布烏龜將開始在turtlesim中以相似於你teleop程序中執行的模式移動。運行rosbag play和烏龜移動之間的持續時間應該大約等於原來在開始教程中的rosbag record執行以及開始從鍵盤發出命令之間的時間。你可讓rosbag play不從bag文件的開始端開始而是使用-s參數從開始端過去一些時間開始。一個可能感興趣的終止選項會是-r選項,它容許你經過一個特定的元素更改發佈頻率。若是你執行:
rosbag play -r 2 <your bagfile>
You should see the turtle execute a slightly different trajectory - this is the trajectory that would have resulted had you issued your keyboard commands twice as fast.
你應該會看到烏龜執行有點不一樣的軌道 - 這是一個讓你鍵盤命令加快一倍的運行軌道。
(3)記錄數據的子集
When running a complicated system, such as the pr2 software suite, there may be hundreds of topics being published, with some topics, like camera image streams, potentially publishing huge amounts of data. In such a system it is often impractical to write log files consisting of all topics to disk in a single bag file. The rosbag record command supports logging only particular topics to a bag file, allowing a user to only record the topics of interest to them.
當運行一個複雜的系統,好比pr2軟件套件,將會有數百個主題被髮布,帶有一些主題好比相機圖像流,可能會發布大量的數據。在這樣的系統中將包含全部主題的日誌文件寫到單個bag文件中是不現實的。rosbag record命令支持只記錄部分的主題到一個bag文件中,容許一個用戶只記錄他們感興趣的主題。
If any turtlesim nodes are running exit them and relaunch the keyboard teleop launch file:
若是turtlesim節點還在運行着退出它們而後從新運行鍵盤遙控啓動文件:
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
In your bagfiles directory, run the following command:
在你的bag文件目錄中,運行以下的命令:
rosbag record -O subset /turtle1/cmd_vel /turtle1/pose
The -O argument tells rosbag record to log to a file named subset.bag, and the topic arguments cause rosbag record to only subscribe to these two topics. Move the turtle around for several seconds using the keyboard arrow commands, and then Ctrl-C the rosbag record.
-O參數告訴rosbag record記錄到一個叫subset.bag的文件中,而主題參數調用rosbag record只訂閱這兩個主題。使用鍵盤箭命令移動烏龜幾秒鐘,而後使用Ctrl-C結束記錄。
Now check the contents of the bag file (rosbag info subset.bag). You should see something like this, with only the indicated topics:
如今檢查一下bag文件的內容(rosbag info subset.bag)。你應該會看到像這樣的一些東西,只帶有指示的主題:
path: subset.bag
version: 2.0 duration: 12.6s start: Dec 10 2014 20:20:49.45 (1418271649.45) end: Dec 10 2014 20:21:02.07 (1418271662.07) size: 68.3 KB messages: 813 compression: none [1/1 chunks] types: geometry_msgs/Twist [9f195f881246fdfa2798d1d3eebca84a] turtlesim/Pose [863b248d5016ca62ea2e895ae5265cf9] topics: /turtle1/cmd_vel 23 msgs : geometry_msgs/Twist /turtle1/pose 790 msgs : turtlesim/Pose
(4)rosbag record/play的限制
In the previous section you may have noted that the turtle's path may not have exactly mapped to the original keyboard input - the rough shape should have been the same, but the turtle may not have exactly tracked the same path. The reason for this is that the path tracked by turtlesim is very sensitive to small changes in timing in the system, and rosbag is limited in its ability to exactly duplicate the behavior of a running system in terms of when messages are recorded and processed by rosrecord, and when messages are produced and processed when using rosplay. For nodes like turtlesim, where minor timing changes in when command messages are processed can subtly alter behavior, the user should not expect perfectly mimicked behavior.
在以前的小節你可能已經注意到了烏龜的路徑可能並不能準確地映射到原始的鍵盤輸入 - 大體的圖形應該是相同的,可是烏龜可能並不可以追蹤到相同的路徑。這個狀況的緣由是烏龜追蹤的路徑在系統中是對於定時的小改變很細微的。而rosbag在對於消息記錄並由rosrecord記錄、並當消息保存並由rosplay處理的嚴格複製運行系統的行爲能力上是有侷限性的。對於像turtlesim的節點,當命令消息處理時小的定時改變可能會改變行爲,用戶應該不能指望完美地複製行爲。
Now that you've learned how to record and play back data, let's learn how to troubleshoot with roswtf.
如今你已經學會了如何記錄並回放數據,讓咱們學習如何使用roswtf進行故障排除。
18. Getting started with roswtf 開始roswtf的使用
Basic introduction to the roswtf tool.
關於roswtf工具的基本使用
Before you start this tutorial, please make sure your roscore is NOT running.
開始學習本教程以前,請確保你的roscore不在運行中。
(1)檢查你的安裝
roswtf examines your system to try and find problems. Let's try it out:
roswtf能測試你的系統,努力查找錯誤。讓咱們試一下吧:
$ roscd
$ roswtf
You should see (detail of the output varies):
Stack: ros
====================================================== Static checks summary: No errors or warnings ====================================================== Cannot communicate with master, ignoring graph checks
If your installation ran correctly you should output similar to the above. The output is telling you:
若是你的安裝運行良好你應該會輸出相似於上面的東西。這個輸出是在告訴你:
"Stack: ros": roswtf uses whatever your current directory is to determine what checks it does. This is telling us that you started roswtf in the ros stack.你當前的路徑決定了roswtf使用什麼樣的檢查。這個是在告訴咱們你在ros stack啓動roswtf。
"Cannot communicate with master, ignoring graph checks": the roscore isn't running, so roswtf didn't do any online checks.roscore不在運行,因此roswtf不會作什麼在線檢查。
(2)在線嘗試
For this next step, we want a Master to be up, so go ahead and start a roscore.
對於下一步,咱們但願一個管理器上來,因此咱們開啓一個roscore。
Now, try running the same sequence again:
$ roscd
$ roswtf
You should see:
Stack: ros
====================================================== Static checks summary: No errors or warnings ====================================================== Beginning tests of your ROS graph. These may take awhile... analyzing graph... ... done analyzing graph running graph rules... ... done running graph rules Online checks summary: Found 1 warning(s). Warnings are things that may be just fine, but are sometimes at fault WARNING The following node subscriptions are unconnected: * /rosout: * /rosout
roswtf did some online examination of your graph now that your roscore is running. Depending on how many ROS nodes you have running, this can take a long time to complete. As you can see, this time it produced a warning:
WARNING The following node subscriptions are unconnected:
* /rosout: * /rosout
roswtf is warning you that the rosout node is subscribed to a topic that no one is publishing to. In this case, this is expected because nothing else is running, so we can ignore it.
roswtf是一個警告,是說rosout節點被訂閱到一個並無人發佈東西的主題上了。在這種狀況下,是合乎情理的由於尚未任何東西運行,因此咱們能夠忽略它。
(3)錯誤
roswtf will warn you about things that look suspicious but may be normal in your system. It can also report errors for problems that it knows are wrong.
roswtf將警告你那些系統中看上去有懷疑可是多是正常的。它還能報告它所知道是錯誤的問題錯誤。
For this part, we are going to set your ROS_PACKAGE_PATH to a bad value. We're also going to stop our roscore to simplify the output that you see.
對於這一部分,咱們將設置你的ROS_PACKAGE_PATH一個錯誤值。咱們將終止咱們的roscore來簡化你能看到的輸出。
$ roscd
$ ROS_PACKAGE_PATH=bad:$ROS_PACKAGE_PATH roswtf
This time we see: 這一次咱們將看到:
Stack: ros
====================================================== Static checks summary: Found 1 error(s). ERROR Not all paths in ROS_PACKAGE_PATH [bad] point to an existing directory: * bad ====================================================== Cannot communicate with master, ignoring graph checks
As you can see, roswtf now gives us an error about the ROS_PACKAGE_PATH setting.
正如你看到的,roswtf如今給咱們一個關於ROS_PACKAGE_PATH設置的錯誤提示。
There are many other types of problems that roswtf can find. If you find yourself stumped by a build or communication issue, try running it and seeing if it can point you in the right direction.
有不少roswtf能找到的其餘類型的問題。若是你發現你本身被一個生成或交流問題難倒了,試着運行它看一下是否它能指給你正確的方向。
Now that you know how to use roswtf, take sometime to learn more about how ros.org is structured and navigating the wiki.
如今你知道了如何使用roswtf,花點時間學習ros.org是如何架構的,瀏覽wiki:http://wiki.ros.org/ROS/Tutorials/NavigatingTheWiki。
19. Navigating the ROS wiki 瀏覽ROS wiki
This tutorial discusses the layout of the ROS wiki (ros.org) and talks about how to find what you want to know.
這個教程討論了ROS wiki的佈局,討論了關於如何找到你想要知道的東西。
This tutorial will look at the different headers, links, and sidebars through out the wiki to help you understand how ROS.org is laid out.
本教程將看一下wiki不一樣的頭、連接和邊欄幫助你理解ROS.org是如何擺設的。
(1)基本的
1) ROS.org登陸界面
The landing page is where you are directed to when you type www.ros.org into you browser. Let's look at the ROS wiki header that is displayed at the top of every wiki page.
這個登陸界面是你輸入www.ros.org進入的頁面。讓咱們看一下ROS wiki頭部,它是顯示在每個wiki頁面的頭部。
As you can see each package contains tutorials and troubleshooting specific to the package.
正如你看到的,每個包都包含着教程和該包特有的故障檢修。
2)ROS包頁面
Let's look at ros-pkg package wiki page for tf (www.ros.org/wiki/tf). The package header for each package is auto generated from the stack and package manifest.
讓咱們看一下tf的ros-pkg包wiki頁面:tf(www.ros.org/wiki/tf).每一個包的包頭是從棧和包的manifest元清單自動生成的。
3)ROS棧頁面
Let's look at ros stack wiki page for ROS (www.ros.org/wiki/ROS). The stack header for each stack is auto generated from the stack manifest.
讓咱們ROS的ros棧wiki頁面(www.ros.org/wiki/ROS)。每一個棧的棧頭是由棧manifest自動生成的。
As you can see each stack contains tutorials and troubleshooting specific to the stack.
正如你看到的,每一個棧都包含該棧特有的教程和故障檢修。
(2)高級
Beginners can skip this section.
初學者能夠略過這一節。
Once you have created your package page, open the URL with /tutorials at the tail of the URL of your package. For example, suppose your package is located at http://wiki.ros.org/foo_pkg. You should open http://wiki.ros.org/foo_pkg/tutorials. This way the wiki will create a new page. 在你的包的URL末尾加/tutorials。
The page will say This page does not exist yet. What type of page are you trying to create?. The wiki is correct, because there's no (hopefully) such page. ROS wiki now shows a list of templates, choose TutorialIndexTemplate. 這個頁面會說這個頁面不存在。你須要建立什麼類型的頁面?。這個wiki是正確的,由於尚未這樣的頁面。ROS wiki如今展現了一系列的模板,選擇TutorialIndexTemplate。
Now you are redirected to the wiki page editor. Add whatever change you think you need, and save it at the end. Using Preview often to check how it looks is a great idea. Notice that, however, there are some ROS wiki macros that do not get activated until you save the page (in that case you just have to pray that your edition works, but it's okay to try and error!). 如今你被重定向一個wiki頁面編輯器。添加你須要的任何更改,在尾部保存它。使用Preview預覽檢查一下它的樣子是個很好的主意。可是,主意,有一些ROS wiki macros不須要激活直到你保存這個頁面(那種狀況下你只是祈禱你的編輯起做用了,可是不要緊嘗試試錯)。
1]排序你的教程
By default, TutorialIndexTemplate uses a macro FullSearchWithDescriptionsCS, which searches all available tutorials under the URL hierarchy you chose (http://wiki.ros.org/foo_pkg/tutorials in this case). There the order of tutorials are based on the "links" between tutorials (next.0 attribute in each page's tutorial header).
默認狀況下,TutorialIndexTemplate使用一個宏FullSearchWithDescriptionsCS,它將在你選擇的URL層級之下(在本例子中是http://wiki.ros.org/foo_pkg/tutorials)搜索全部可用的教程。那樣教程的順序是基於教程之間的"links"(每一個頁面的教程頭中的next.0屬性)。
Often, you want to sort the tutorial in your own way, like ROS' basic tutorial top page does. To do so you use TutorialChain macro. See the example in ROS basic tutorials.
不少狀況下,你但願按照本身的方式排序教程,像ROS的基本教程頂頁那樣。要實現這個你須要使用TutorialChain宏。看ROS基礎教程的例子。
This tutorial discusses options for getting to know more about using ROS on real or simulated robots.
該教程討論了在真實或者模擬的機器人中使用ROS的更多知識。
At this point in the beginner's tutorials you should have an understanding of the core concepts of ROS.
到這裏你應該已經對ROS的核心概念有了一個理解了。
Given a robot that runs ROS, you could use this understanding to list topics published and subscribed by the robot, to identify the messages consumed by these topics and then write your own nodes that process sensor data and act in the world.
假設一個機器人運行ROS,你可使用這種理解:列出由機器人發佈和訂閱的主題,識別這些主題使用的消息,而後寫出你本身的節點來處理傳感器數據而且實際運行。
The real attraction of ROS is not the publish/subscribe middle-ware itself but that ROS provides a standard mechanism for developers around the world to share their code. The best "feature" of ROS is its enormous community.
ROS的真正的魅力並非發佈/訂閱中間件,而是ROS爲世界各地的開發者提供了一個標準的機械結構來共享他們的代碼。ROS最好的功能是它龐大的社區。
The number of packages available can be overwhelming. This tutorial attempts to give you an idea of what to explore next.
可用的包的數量可能很驚人的。本教程告訴你下一步如何挖掘。
(1)啓動一個模擬器
Even if you have a real robot, it is good to get started using a simulator so that if something goes wrong you don't injure yourself or damage an expensive robot.
雖然你有了一個真實的機器人,可是以一個模擬器開始是很好的,從而避免若是出現了某些錯誤你不會傷到本身或者傷害到一個昂貴的機器人。
You can get started with the PR2 Simulator or the Turtlebot Simulator. Alternately, you might search for your robot and check whether it has a simulator of its own.
你能夠用PR2模擬器(http://wiki.ros.org/pr2_simulator/Tutorials)開始或者是Turtlebot模擬器(http://wiki.ros.org/turtlebot_simulator/Tutorials)開始。另外,你能夠查找一個你的機器人(http://wiki.ros.org/Robots)檢查一下是否它有個它本身的模擬器。
At this point, you might try to control the simulated robot using a 'teleop' package (e.g., turtlebot_teleop) or use your understanding of ROS to find a topic and write code that sends an appropriate message to drive your robot.
到這裏,你能夠嘗試着使用一個'teleop'包(遙控器,例如turtlebot_teleop:http://wiki.ros.org/turtlebot_teleop/Tutorials/Teleoperation)控制模擬機器人,或者使用你本身對於ROS的理解查找一個主題或者寫一個節點可以發送一個合適的消息來驅動你的機器人。
(2)探索RViz
RViz is a powerful visualization tool that allows you to view the robot's sensors and internal state. The user guide will help you get started.
RViz是一個強大的可視化工具它能容許你查看機器人的傳感器以及內部狀態。用戶手冊將幫助你開始:http://wiki.ros.org/rviz/UserGuide。
(3)理解TF
The TF package transforms between different coordinate frames used by your robot and keeps track of these transforms over time. A good understanding of TF is essential when working with any real robot. It is worthwhile to work through the tutorials.
TF包能夠在由你的機器人使用的不一樣座標框架之間改變,而且隨時間追蹤這些改變。當協同任何真實的機器人工做時一個對TF好的理解是必要的。因此看一下該教程值得的。
If you're building your own robot, you might at this point consider constructing a URDF model for your robot. If you're using a "standard" robot then one has probably already being built for you. Nevertheless, it may be worthwhile to briefly familiarize yourself with the URDF package.
若是你是在創建你本身的機器人,你可能這個時候考慮爲你的機器人構建一個URDF模型:http://wiki.ros.org/urdf/Tutorials。若是你使用一個「標準的」機器人,那麼你可能已經有一個爲你建好的機器人模型了。無論怎樣,本身簡單地熟悉一下URDF包是值得的。
(4)深刻學習
At this point, you're probably ready to start getting your robot to perform more sophisticated tasks. The following pages may help you:
到這一步,你差很少能夠準備開始讓你的機器人執行復雜一些的任務了。如下頁面將會幫助你:
actionlib - The actionlib package provides a standardized interface for interfacing with preemptible tasks. This is widely used by "higher-level" packages in ROS. 該actionlib包爲你提供了一個標準和預置的任務j交接的接口。這在ROS中由「更高水平」的包普遍使用。
navigation - 2D navigation: map-building and path planning. 2D導航:地圖創建和路徑規劃。
MoveIt - To control the arms of your robot. 控制機器人的手臂。
Now that you have completed the beginner level tutorials please answer this short questionnaire.
如今你已經完成了初學者水平的教程,請回答這個簡單的問卷。