ubuntu中配置ROS详细教程

来源:互联网 发布:主机名和域名写法 编辑:程序博客网 时间:2024/06/18 16:55
本教程转至:
http://blog.csdn.net/linuxarmsummary/article/details/38899257
并自己增加了一些内容

为了方便新人学习ROS,我整理出来了配置ROS的教程,跟着我复制粘贴命令即可,同时最重要的是要了解ROS的运行机制。ROS全称是开源机器人操作系统。

安装虚拟机和ubuntu请看 http://note.youdao.com/yws/public/redirect/share?id=9fb6fd61c60efe1cae3ae04cce33da65&type=false
安装windows和ubuntu双系统请看  http://note.youdao.com/yws/public/redirect/share?id=5f3f833bba506b8125d2842cffc2ee81&type=false
这个是我整理的x-mind笔记文件 用x-mind软件打开

这个是我配好的ROS包  不懂的可以看这里,已经配好

基本命令:
打开终端:Ctrl+Alt+T
在终端中打开另一个终端:Ctrl+Shirt+T
查看文件里面的东西:ls
切换到某一个目录:cd +目录名
切换到上一个目录:cd ..
切换到上上个目录:cd ../..  (切换到多少就加多少个../)

vim编译器的基本命令:

安装vim编译器请查看:
http://note.youdao.com/yws/public/redirect/share?id=a62dcad19ff3362f4ac19c4909422a1c&type=false

创建一个文件:mkdir+文件名
新建一个程序:vim +xxx.c 或者 vim +xxx.cpp(.c后缀就是C程序,.cpp后缀就是c++程序)
vim中按i插入文本输入
写完代码按Esc,然后输入":x"(保存并退出程序)   输入“:q!”不保存直接退出程序
执行程序:gcc xxx.c
              ./a.out

强制退出命令:Ctrl+C

gedit编译器使用: (文本编译器)
进入程序:geditt +程序名

注意:一下教程每做完一个大步骤就关闭所有终端再重新接着下一个大步骤

1.安装ROS,这里选择indigo版本。

 添加源 sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

 添加钥匙 sudo apt-key adv --keyserver hkp://pool.sks-keyservers.net --recv-key 0xB01FA116

 更新一下 sudo apt-get update

 安装桌面版 sudo apt-get install ros-indigo-desktop-full

 查找软件包 apt-cache search ros-indigo

 初始化环境 
sudo rosdep init
           rosdep update(注意:两行是指输入两次命令,下同)

 设置环境 echo "source /opt/ros/indigo/setup.bash" >> ~/.bashrc
         source ~/.bashrc

 改变终端下的环境变量 source /opt/ros/indigo/setup.bash

 安装rosinstall命令工具 sudo apt-get install python-rosinstall

2.配置环境。

 查看环境变量 export | grep ROS


 source一下source /opt/ros/indigo/setup.bash

 创建工作空间 
mkdir -p ~/catkin_ws/src
             cd ~/catkin_ws/src
             cd ..
             catkin_make (完成后看到下图)

          
   source devel/setup.bash

  查看工作空间  echo $ROS_PACKAGE_PATH

3.介绍ROS文件系统。

安装ros-tutorials包 sudo apt-get install ros-indigo-ros-tutorials

使用rospack (该命令查看软件包的信息) rospack find roscpp

使用roscd (切换工作目录到软件包)roscd roscpp
                                pwd

查看包的路径 echo $ROS_PACKAGE_PATH

切换得到软件包的子目录 roscd roscpp/cmake
                     pwd

罗列目录 rosls roscpp_tutorials

Tab键可以自动补全输入

4.创建程序包 cd ~/catkin_ws/src
            catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

  查看包的依赖关系 rospack depends1 beginner_tutorials

                  roscd beginner_tutorials
                  cat package.xml


 间接依赖 rospack depends1 rospy

         rospack depends beginner_tutorials

 自定义程序包 gedit package.xml

 更改一下第15行的<license>TODO</license>为<license>BSD</license>

 在最后一行添加(有就不用加了) <run_depend>roscpp</run_depend>
                                               <run_depend>rospy</run_depend>
                                               <run_depend>std_msgs</run_depend>

(更改完保存退出)

5.编译ROS程序包 source /opt/ros/indigo/setup.bash(要cd catkin_ws)
               cd ~/catkin_ws/
               catkin_make

6.理解ROS节点

 安装模拟器 sudo apt-get install ros-indigo-ros-tutorials

 使用roscore命令 roscore(roscore 是你在运行所有ROS程序前首先要运行的命令) 
roscore完成后可以使用Ctrl+Shirt+T在终端中打开另一个终端

 使用rosnode命令  rosnode list

                 rosnode info /rosout

 使用rosrun命令 rosrun turtlesim turtlesim_node(需要在新的终端)

 更改节点名称 rosrun turtlesim turtlesim_node __name:=my_turtle
(更改完名字后退出前一个乌龟界面,留后一个)

 查看节点详细信息 rosnode ping my_turtle (做完这一步退出全部终端)
按Ctrl+C退出这个节点

7.理解ROS话题 roscore

  打开节点 rosrun turtlesim turtlesim_node(在新的终端)

  键盘控制乌龟运动 rosrun turtlesim turtle_teleop_key

这时候随便按方向键

  安装话题图形工具 sudo apt-get install ros-indigo-rqt

                  sudo apt-get install ros-indigo-rqt-common-plugins

  运行话题工具 rosrun rqt_graph rqt_graph(在新的终端)

  查看话题数据 rostopic echo /turtle1/cmd_vel

  查看话题消息类型 rostopic type /turtle1/cmd_vel

  查看消息详细情况 rosmsg show geometry_msgs/Twist

  使乌龟转一定角度 rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

  使乌龟转圈  rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

  查看乌龟转的频率 rostopic hz /turtle1/pose

  获取更深层次信息 rostopic type /turtle1/cmd_vel | rosmsg show

  使用坐标图 rosrun rqt_plot rqt_plot

在右上角Topic那里写/turtle1/pose就会显示相应的坐标信息

8.理解ROS服务和参数(这一步先自己roscore ,然后打开一个乌龟节点)

  查看服务类型  rosservice type clear

  清除节点背景轨迹 rosservice call clear

  查看服务的信息 rosservice type spawn| rossrv show

  自动生成一个新的乌龟 rosservice call spawn 2 2 0.2 ""

  修改背景颜色 rosparam set background_r 150
              rosservice call clear

  获取背景颜色通道的值 rosparam get background_g 

  显示参数服务器上的所有内容 rosparam get /

9.使用日志框架

  安装程序包 sudo apt-get install ros-indigo-rqt ros-indigo-rqt-common-plugins ros-indigo-turtlesim

  打开日志框架 rosrun rqt_console rqt_console

              rosrun rqt_logger_level rqt_logger_level(要在两个新的终端)


  启动节点 rosrun turtlesim turtlesim_node

  让乌龟撞墙 rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 0.0]'(可以看到图表上有警告的信息,做完这一步退出终端)

  启动多个节点 roscd beginner_tutorials (记得要roscore,然后cd catkin_ws ,接着source devel/setup.bash,再启动节点)

              mkdir launch
              cd launch

  创建文件把内同复制到文件里面  gedit turtlemimic.launch
   需要复制的内容 
 
<launch>  <group ns="turtlesim1">  <node pkg="turtlesim" name="sim" type="turtlesim_node"/>  </group>  <group ns="turtlesim2">  <node pkg="turtlesim" name="sim" type="turtlesim_node"/>  </group>  <node pkg="turtlesim" name="mimic" type="mimic">  <remap from="input" to="turtlesim1/turtle1"/>  <remap from="output" to="turtlesim2/turtle1"/>  </node> </launch>
启动launch文件 roslaunch beginner_tutorials turtlemimic.launch
让两个乌龟转圈 rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'
10.创建ROS消息和服务
 使用msg消息 cd ~/catkin_ws/src/beginner_tutorials  mkdir msg
 echo "int64 num" > msg/Num.msg
给package.xml增加语句 cd catkin_ws/src/beginner_tutorials
 gedit package.xml
复制进去package.xml的内容(蓝色字体不要动) 
 <build_depend>message_generation</build_depend>  <run_depend>message_runtime</run_depend>
更改CMakeLists.txt里面的东西 cd catkin_ws/src/beginner_tutorials
 gedit CMakeLists.txt
找到这个模块 
# add_message_files(#   FILES#   Message1.msg#   Message2.msg# )
然后更改为这样  
add_message_files(  FILES  Num.msg)
检查ROS能否识别消息  cd catkin_ws
 source devel/setup.bash
 rosmsg show beginner_tutorials/Num
使用服务roscd beginner_tutorials  mkdir srv (创建服务)  roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv(给服务复制文件)
修改CMakeList.txt文件
找到这个模块
 
# add_service_files(#   FILES#   Service1.srv#   Service2.srv# )
更改为
 
add_service_files(  FILES  AddTwoInts.srv)
把前面的find_package也改一下,增加message_generation
使用服务:
查看ROS能否识别服务 rossrv show beginner_tutorials/AddTwoInts
更改CMakeLists.txt里面的东西
找到这个模块
 
# generate_messages(#   DEPENDENCIES# #  std_msgs  # Or other packages containing msgs# )
更改为
 
generate_messages(  DEPENDENCIES  std_msgs)
重新编译乌龟包
cd ../..
catkin_make
11.编写简单的消息发不起和订阅器
cd ~/catkin_ws/src/beginner_tutorials/src
创建文件 gedit talker.cpp 并粘贴下面代码
 
 
#include "ros/ros.h" #include "std_msgs/String.h" #include <sstream> /**  * This tutorial demonstrates simple sending of messages over the ROS system.  */ int main(int argc, char **argv) {  /**  * The ros::init() function needs to see argc and argv so that it can perform  * any ROS arguments and name remapping that were provided at the command line. For programmatic  * remappings you can use a different version of init() which takes remappings  * directly, but for most command-line programs, passing argc and argv is the easiest  * way to do it.  The third argument to init() is the name of the node.  *  * You must call one of the versions of ros::init() before using any other  * part of the ROS system.  */  ros::init(argc, argv, "talker");  /**  * NodeHandle is the main access point to communications with the ROS system.  * The first NodeHandle constructed will fully initialize this node, and the last  * NodeHandle destructed will close down the node.  */  ros::NodeHandle n;  /**  * The advertise() function is how you tell ROS that you want to  * publish on a given topic name. This invokes a call to the ROS  * master node, which keeps a registry of who is publishing and who  * is subscribing. After this advertise() call is made, the master  * node will notify anyone who is trying to subscribe to this topic name,  * and they will in turn negotiate a peer-to-peer connection with this  * node.  advertise() returns a Publisher object which allows you to  * publish messages on that topic through a call to publish().  Once  * all copies of the returned Publisher object are destroyed, the topic  * will be automatically unadvertised.  *  * The second parameter to advertise() is the size of the message queue  * used for publishing messages.  If messages are published more quickly  * than we can send them, the number here specifies how many messages to  * buffer up before throwing some away.  */  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);  ros::Rate loop_rate(10);  /**  * A count of how many messages we have sent. This is used to create  * a unique string for each message.  */  int count = 0;  while (ros::ok())  {  /**  * This is a message object. You stuff it with data, and then publish it.  */  std_msgs::String msg;  std::stringstream ss;  ss << "hello world " << count;  msg.data = ss.str();  ROS_INFO("%s", msg.data.c_str());  /**  * The publish() function is how you send messages. The parameter  * is the message object. The type of this object must agree with the type  * given as a template parameter to the advertise<>() call, as was done  * in the constructor above.  */  chatter_pub.publish(msg);  ros::spinOnce();  loop_rate.sleep();  ++count;  }  return 0; }
创建listener,cpp文件 gedit listener.cpp 并粘贴下面代码
 
 
#include "ros/ros.h" #include "std_msgs/String.h" /**  * This tutorial demonstrates simple receipt of messages over the ROS system.  */ void chatterCallback(const std_msgs::String::ConstPtr& msg) {  ROS_INFO("I heard: [%s]", msg->data.c_str()); } int main(int argc, char **argv) {  /**  * The ros::init() function needs to see argc and argv so that it can perform  * any ROS arguments and name remapping that were provided at the command line. For programmatic  * remappings you can use a different version of init() which takes remappings  * directly, but for most command-line programs, passing argc and argv is the easiest  * way to do it.  The third argument to init() is the name of the node.  *  * You must call one of the versions of ros::init() before using any other  * part of the ROS system.  */  ros::init(argc, argv, "listener");  /**  * NodeHandle is the main access point to communications with the ROS system.  * The first NodeHandle constructed will fully initialize this node, and the last  * NodeHandle destructed will close down the node.  */  ros::NodeHandle n;  /**  * The subscribe() call is how you tell ROS that you want to receive messages  * on a given topic.  This invokes a call to the ROS  * master node, which keeps a registry of who is publishing and who  * is subscribing.  Messages are passed to a callback function, here  * called chatterCallback.  subscribe() returns a Subscriber object that you  * must hold on to until you want to unsubscribe.  When all copies of the Subscriber  * object go out of scope, this callback will automatically be unsubscribed from  * this topic.  *  * The second parameter to the subscribe() function is the size of the message  * queue.  If messages are arriving faster than they are being processed, this  * is the number of messages that will be buffered up before beginning to throw  * away the oldest ones.  */  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);  /**  * ros::spin() will enter a loop, pumping callbacks.  With this version, all  * callbacks will be called from within this thread (the main one).  ros::spin()  * will exit when Ctrl-C is pressed, or the node is shutdown by the master.  */  ros::spin();  return 0; }
编译节点
在CMakeLists.txt增加几条语句
cd ..
gedit 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_dependencies(talker ${catkin_EXPORTED_TARGETS})
add_executable(listener src/listener.cpp)target_link_libraries(listener ${catkin_LIBRARIES})
编译(在工作空间下 cd catkin_ws)
catkin_make
12.测试消息发布器和订阅器
启动发布器 roscore
 cd catkin_ws
         source ./devel/setup.bash
         rosrun beginner_tutorials talker
你会看到
在新的终端 cd catkin_ws
          source devel/setup.bash
         rosrun beginner_tutorials listener 
会看到
 13.编写简单的Service和Client 
编写service节点 cd ~/catkin_ws/src/beginner_tutorials
复制代码 cd src
       gedit add_two_ints_server.cpp
代码(代码需要自己看懂) 
 
#include "ros/ros.h" #include "beginner_tutorials/AddTwoInts.h" bool add(beginner_tutorials::AddTwoInts::Request &req,  beginner_tutorials::AddTwoInts::Response &res) {  res.sum = req.a + req.b;  ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);  ROS_INFO("sending back response: [%ld]", (long int)res.sum);  return true; } int main(int argc, char **argv) {  ros::init(argc, argv, "add_two_ints_server");  ros::NodeHandle n;  ros::ServiceServer service = n.advertiseService("add_two_ints", add);  ROS_INFO("Ready to add two ints.");  ros::spin();  return 0; }
 
编写client节点
gedit add_two_ints_client.cpp
复制代码
 
#include "ros/ros.h" #include "beginner_tutorials/AddTwoInts.h" #include <cstdlib> int main(int argc, char **argv) {  ros::init(argc, argv, "add_two_ints_client");  if (argc != 3)  {  ROS_INFO("usage: add_two_ints_client X Y");  return 1;  }  ros::NodeHandle n;  ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");  beginner_tutorials::AddTwoInts srv;  srv.request.a = atoll(argv[1]);  srv.request.b = atoll(argv[2]);  if (client.call(srv))  {  ROS_INFO("Sum: %ld", (long int)srv.response.sum);  }  else  {  ROS_ERROR("Failed to call service add_two_ints");  return 1;  }  return 0; }
编译节点 gedit CMakeLists.txt
在末尾增加
 
add_executable(add_two_ints_server src/add_two_ints_server.cpp) target_link_libraries(add_two_ints_server ${catkin_LIBRARIES}) add_dependencies(add_two_ints_server beginner_tutorials_gencpp) add_executable(add_two_ints_client src/add_two_ints_client.cpp) target_link_libraries(add_two_ints_client ${catkin_LIBRARIES}) add_dependencies(add_two_ints_client beginner_tutorials_gencpp)

 
开始编译 cd catkin_ws
 catkin_make
14.测试简单的Service和Client
 运行service命令 cd catkin_ws
 source devel/setup.bash
 rosrun beginner_tutorials add_two_ints_server 
运行Client命令 cd catkin_ws
 source devel/setup.bash
 rosrun beginner_tutorials add_two_ints_client 1 3 

 

0 0
原创粉丝点击