ROS机器人开发实践(1)——ROS基础

小乌龟Demo

我们的学习就从一个生动有趣的Demo开始启程吧。
运行ROS的第一步是要在终端输入roscore,这是使用ROS的前提条件。
我们要分别打开三个窗口,逐一运行下面三行指令。

$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun turtlesim turtle_teleop_key

ROS机器人开发实践(1)——ROS基础_第1张图片(看着第二个窗口里弹跳出来的警告,我只想说,ROS里的小乌龟很有可能是个双子座话痨,他太能说了)

发布者(Publisher)与订阅者(Subscriber)

发布者(Publisher)与订阅者(Subscriber)是ROS里面最基础的通信方式。
我们打开一个新的终端。输入如下命令

$ rqt_graph

ROS机器人开发实践(1)——ROS基础_第2张图片通过上面这一幅图,我们可以知道我们创建了两个节点,根据箭头的方向,/teleop_turtle为发布者用于发布键盘的指令,/turtlesim 为订阅者用于接受指令,箭头上方的内容是话题。

创建工作空间以及编译

$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_init_workspace
Creating symlink "/home/warmtree/catkin_ws/src/CMakeLists.txt" pointing to "/opt/ros/melodic/share/catkin/cmake/toplevel.cmake"
$ cd ~/catkin_ws/
$ catkin_make

编译之后,我们发现devel里面自动生成脚本文件。
ROS机器人开发实践(1)——ROS基础_第3张图片之后我们进行环境变量设置以及查看是否设置成功。

$ source devel/setup.bash
$ echo $ROS_PACKAGE_PATH
/home/warmtree/catkin_ws/src:/opt/ros/melodic/share

结果能打印出来包含当前工作空间的路径证明环境变量设置成功。
在这里面,我们遇到了一个Linux系统常见的一个命令echo。TA的基本用法就是命令后面加上输出文本。

(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ echo 1234
1234
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ echo test message
test message
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ echo "www.ec-nantes.fr"
www.ec-nantes.fr
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ echo -n "www.ec-nantes.fr"
www.ec-nantes.fr(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~$ 

更多输出彩色字体的用法详见链接echo用法

创建功能包

(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~/catkin_ws$ cd src
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~/catkin_ws/src$ catkin_create_pkg learning_communication std_msgs rospy roscpp
Created file learning_communication/CMakeLists.txt
Created file learning_communication/package.xml
Created folder learning_communication/include/learning_communication
Created folder learning_communication/src
Successfully created files in /home/warmtree/catkin_ws/src/learning_communication. Please adjust the values in package.xml.
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~/catkin_ws/src$ cd ~/catkin_make
bash: cd: /home/warmtree/catkin_make: 没有那个文件或目录
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~/catkin_ws/src$ cd ~/catkin_ws
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~/catkin_ws$ catkin_make
Base path: /home/warmtree/catkin_ws
Source space: /home/warmtree/catkin_ws/src
Build space: /home/warmtree/catkin_ws/build
Devel space: /home/warmtree/catkin_ws/devel
Install space: /home/warmtree/catkin_ws/install
####
#### Running command: "cmake /home/warmtree/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/warmtree/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/warmtree/catkin_ws/install -G Unix Makefiles" in "/home/warmtree/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/warmtree/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/warmtree/catkin_ws/devel;/opt/ros/melodic
-- This workspace overlays: /home/warmtree/catkin_ws/devel;/opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.15", minimum required is "2") 
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/warmtree/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python2 (found version "2.7.15") 
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.19
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - learning_communication
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'learning_communication'
-- ==> add_subdirectory(learning_communication)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/warmtree/catkin_ws/build
####
#### Running command: "make -j4 -l4" in "/home/warmtree/catkin_ws/build"
####
(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~/catkin_ws$ source devel/setup.bash

你可能感兴趣的:(ROS基础)