【ROS】 ros学习日记(1)
ros学习日记(1)
- ros安装
- 测试ros(小乌龟,启动!!)
- 启动小乌龟并测试
- 小乌龟不动错误排查
- HelloWorld
- 1.创建工作空间并初始化
- 2.进入 src 创建 ros 包并添加依赖
- 3.使用C++编写程序
ros的安装、测试和helloworld(学习的为赵虚左老师的教程 1.老师的文档 2.老师的视频)
ros安装
直接诶使用鱼香肉丝的ros一键安装
命令如下:
wgethttp://fishros.com/install-Ofishros&&.fishros测试ros(小乌龟,启动!!)
启动小乌龟并测试
首先启动三个命令行(ctrl + alt + T)
1.命令行1键入:
` ``bash
roscor
2.命令行2键入以下命令(此时会弹出图形化界面) ```bash rosrun turtlesim turtlesim_node可以输入tur后按Tab自动补全
3.命令行3键入以下命令(在3中可以通过上下左右控制2中乌龟的运动)
rosrun turtlesim turtle_teleop_key最终结果如下所示:
小乌龟不动错误排查
1.光标是否停留在rosrun turtlesim turtle_teleop_key界面
2.输入法是否为英文
HelloWorld
先创建一个工作空间→再创建一个功能包→编辑源文件 →编辑配置文件→编译并执行
1.创建工作空间并初始化
mkdir-p自定义空间名称/srccd自定义空间名称 catkin_make比如
mkdir-pdemo001_ws/srccddemo001_ws catkin_make上述命令,首先会创建一个工作空间以及一个 src 子目录,然后再进入工作空间调用 catkin_make命令编译。
2.进入 src 创建 ros 包并添加依赖
cdsrc catkin_create_pkg 自定义ROS包名 roscpp rospy std_msgs比如
cdsrc catkin_create_pkg helloworld roscpp rospy std_msg上述命令,会在工作空间下生成一个功能包,该功能包依赖于 roscpp、rospy 与 std_msgs,其中roscpp是使用C++实现的库,而rospy则是使用python实现的库,std_msgs是标准消息库,创建ROS功能包时,一般都会依赖这三个库实现。
运行效果如下
can@volcano:~$mkdir-pdemo001_ws/src can@volcano:~$cddemo001_ws/ can@volcano:~/demo001_ws$ catkin_make Base path: /home/can/demo001_ws Source space: /home/can/demo001_ws/src Build space: /home/can/demo001_ws/build Devel space: /home/can/demo001_ws/devel Install space: /home/can/demo001_ws/install Creating symlink"/home/can/demo001_ws/src/CMakeLists.txt"pointing to"/opt/ros/noetic/share/catkin/cmake/toplevel.cmake"######## Running command: "cmake /home/can/demo001_ws/src -DCATKIN_DEVEL_PREFIX=/home/can/demo001_ws/devel -DCMAKE_INSTALL_PREFIX=/home/can/demo001_ws/install -G Unix Makefiles" in "/home/can/demo001_ws/build"####-- The C compiler identification is GNU9.4.0 -- The CXX compiler identification is GNU9.4.0 -- Checkforworking C compiler: /usr/bin/cc -- Checkforworking C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info -done-- Detecting C compile features -- Detecting C compile features -done-- Checkforworking CXX compiler: /usr/bin/c++ -- Checkforworking CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info -done-- Detecting CXX compile features -- Detecting CXX compile features -done-- Using CATKIN_DEVEL_PREFIX: /home/can/demo001_ws/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/noetic -- This workspace overlays: /opt/ros/noetic -- Found PythonInterp: /usr/bin/python3(found suitable version"3.8.10", minimum required is"3")-- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Found PY_em: /usr/lib/python3/dist-packages/em.py -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing()-- Using CATKIN_TEST_RESULTS_DIR: /home/can/demo001_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under'/usr/src/googletest':gtests will be built -- Found gmock sources under'/usr/src/googletest':gmock will be built -- Found PythonInterp: /usr/bin/python3(found version"3.8.10")-- Found Threads: TRUE -- Using Python nosetests: /usr/bin/nosetests3 -- catkin0.8.12 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- Configuringdone-- Generatingdone-- Build files have been written to: /home/can/demo001_ws/build######## Running command: "make -j12 -l12" in "/home/can/demo001_ws/build"####can@volcano:~/demo001_ws$cdsrc can@volcano:~/demo001_ws/src$ catkin_create_pkg helloworld roscpp rospy std_msg Createdfilehelloworld/package.xml Createdfilehelloworld/CMakeLists.txt Created folder helloworld/include/helloworld Created folder helloworld/src Successfully created filesin/home/can/demo001_ws/src/helloworld. Please adjust the valuesinpackage.xml. can@volcano:~/demo001_ws/src$3.使用C++编写程序
1.进入 ros 包的 src 目录编辑源文件
cd自定义的包
我这里是helloworld
所以
cdhelloworldcdsrc新建一个cpp文档
touchhelloworld_c.cpp然后在文档中输入以下代码
#include"ros/ros.h"intmain(intargc,char*argv[]){//执行 ros 节点初始化ros::init(argc,argv,"hello");//创建 ros 节点句柄(非必须)ros::NodeHandle n;//控制台输出 hello worldROS_INFO("hello world!");return0;}2.编辑 ros 包下的 Cmakelist.txt文件
把下面两块的注释去掉并修改(一个是在136行,一个是149-151行)
136行的如下:
add_executable(步骤3的源文件名 src/步骤3的源文件名.cpp)改为
add_executable(haha src/helloworld_c.cpp)另一个如下:
target_link_libraries(步骤3的源文件名${catkin_LIBRARIES})改为
target_link_libraries(haha${catkin_LIBRARIES})3.进入工作空间目录并编译
cd自定义空间名称 catkin_make4.执行
(新的终端)
先启动命令行1:
roscore再启动命令行2:
(又一个新的终端)
cd工作空间source./devel/setup.bash rosrun 包名 C++节点ps.节点就是第二步的时候我们修改的那两行的括号里第一个参量,这是一种映射关系,haha和src/helloworld_c.cpp的映射关系。
最终结果如下:
