ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)

致谢源代码网址:https://github.com/Tutorgaming/kamtoa-simulation

kamtoa simulation学习与示例分析(一)





源码学习与分析是学习ROS,包括RVIZ和Gazebo等必须的过程,大量代码的阅读能够提高加快理解熟练使用ROS Kinetic。

首先,先看文件组织:

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第1张图片

一般README中有详细的使用说明,包括安装,使用和示例教程。

CMakeLists.txt:

# toplevel CMakeLists.txt for a catkin workspace
# catkin/cmake/toplevel.cmake

cmake_minimum_required(VERSION 2.8.3)

set(CATKIN_TOPLEVEL TRUE)

# search for catkin within the workspace
set(_cmd "catkin_find_pkg" "catkin" "${CMAKE_SOURCE_DIR}")
execute_process(COMMAND ${_cmd}
  RESULT_VARIABLE _res
  OUTPUT_VARIABLE _out
  ERROR_VARIABLE _err
  OUTPUT_STRIP_TRAILING_WHITESPACE
  ERROR_STRIP_TRAILING_WHITESPACE
)
if(NOT _res EQUAL 0 AND NOT _res EQUAL 2)
  # searching fot catkin resulted in an error
  string(REPLACE ";" " " _cmd_str "${_cmd}")
  message(FATAL_ERROR "Search for 'catkin' in workspace failed (${_cmd_str}): ${_err}")
endif()

# include catkin from workspace or via find_package()
if(_res EQUAL 0)
  set(catkin_EXTRAS_DIR "${CMAKE_SOURCE_DIR}/${_out}/cmake")
  # include all.cmake without add_subdirectory to let it operate in same scope
  include(${catkin_EXTRAS_DIR}/all.cmake NO_POLICY_SCOPE)
  add_subdirectory("${_out}")

else()
  # use either CMAKE_PREFIX_PATH explicitly passed to CMake as a command line argument
  # or CMAKE_PREFIX_PATH from the environment
  if(NOT DEFINED CMAKE_PREFIX_PATH)
    if(NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "")
      string(REPLACE ":" ";" CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
    endif()
  endif()

  # list of catkin workspaces
  set(catkin_search_path "")
  foreach(path ${CMAKE_PREFIX_PATH})
    if(EXISTS "${path}/.catkin")
      list(FIND catkin_search_path ${path} _index)
      if(_index EQUAL -1)
        list(APPEND catkin_search_path ${path})
      endif()
    endif()
  endforeach()

  # search for catkin in all workspaces
  set(CATKIN_TOPLEVEL_FIND_PACKAGE TRUE)
  find_package(catkin QUIET
    NO_POLICY_SCOPE
    PATHS ${catkin_search_path}
    NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
  unset(CATKIN_TOPLEVEL_FIND_PACKAGE)

  if(NOT catkin_FOUND)
    message(FATAL_ERROR "find_package(catkin) failed. catkin was neither found in the workspace nor in the CMAKE_PREFIX_PATH. One reason may be that no ROS setup.sh was sourced before.")
  endif()
endif()

catkin_workspace()

这里,先看kamtoa_gazebo文件夹下:

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第2张图片

CMakeLists.txt中包含需要的功能包,如下截取部分代码片段:

cmake_minimum_required(VERSION 2.8.3)
project(kamtoa_gazebo)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  gazebo_msgs
  gazebo_plugins
  gazebo_ros
  gazebo_ros_control
  kamtoa_description
)

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)

需要的功能包如果不全,可以使用 sudo apt-get install ros-kinetic-gazebo-msgs,以类似方式安装(catkin)。当然编译需要对应的功能包,运行也需要,这里缺一不可,请看package.xml:



  kamtoa_gazebo
  0.0.0
  The kamtoa_gazebo package

   
  
  
  c3mx


  
  
  
  TODO


  
  
  
  


  
  
  
  


  
  
  
  
  
  
  
  
  
  
  
  catkin
  gazebo_msgs
  gazebo_plugins
  gazebo_ros
  gazebo_ros_control
  kamtoa_description
  gazebo_msgs
  gazebo_plugins
  gazebo_ros
  gazebo_ros_control
  kamtoa_description


  
  
    

  

注意这里的,分别对应编译和运行, 有时编译没有问题,运行却报错,请务必注意。

world文件夹下存放gazebo模型,打开可以看到:

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第3张图片

building1.world和whiz_ex.world,具体解析可以参考博客中GazeboSim中内容,具体的模型文件为sdf 1.6对应Gazebo7,以whiz_ex_window为例,分为model.config和model.sdf如下:

model.sdf:



  
    0.232213 1.38881 0 0 -0 0
    
      
        
          
            0.9 0.17 2
          
        
        0 0 1 0 -0 0
      
      
        0 0 1 0 -0 0
        
          
            0.9 0.17 2
          
        
        
          
        
      
      0.29567 1.0429 0 0 0 -3.12055
    
    
      
        
          
            0.949838 0.2 2
          
        
        0 0 1 0 -0 0
      
      
        0 0 1 0 -0 0
        
          
            0.949838 0.2 2
          
        
        
          
        
      
      -0.776238 1.0429 0 0 -0 3.14159
    
    
      
        
          
            0.9 0.17 2
          
        
        0 0 1 0 -0 0
      
      
        0 0 1 0 -0 0
        
          
            0.9 0.17 2
          
        
        
          
        
      
      -4.14363 1.09228 0 0 -0 0
    
    
      
        
          
            0.9 0.17 2
          
        
        0 0 1 0 -0 0
      
      
        0 0 1 0 -0 0
        
          
            0.9 0.17 2
          
        
        
          
        
      
      -1.75347 -0.819856 0 0 0 -3.12358
    
    
      -1.6817 -3.67821 0 0 -0 0
      
        -1.17971 0 1.25 0 -0 0
        
          
            5.89059 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            5.89059 0.15 2.5
          
        
        -1.17971 0 1.25 0 -0 0
      
      
        3.9453 0 1.25 0 -0 0
        
          
            0.35941 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.35941 0.15 2.5
          
        
        3.9453 0 1.25 0 -0 0
      
      
        2.76559 0 0 0 -0 0
        
          
            2 0.15 0
          
        
        
          
          1 1 1 1
        
      
      
        
          
            2 0.15 0
          
        
        2.76559 0 0 0 -0 0
      
      
        2.76559 0 2.45 0 -0 0
        
          
            2 0.15 0.1
          
        
        
          
          1 1 1 1
        
      
      
        
          
            2 0.15 0.1
          
        
        2.76559 0 2.45 0 -0 0
      
    
    
      
        
          
            3.75 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            3.75 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      2.3683 -1.87821 0 0 -0 1.5708
    
    
      
        
          
            9.25 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            9.25 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -1.19255 3.74668 0 0 -0 3.14159
    
    
      -4.65794 2.07148 0 0 0 -1.5708
      
        -1.67569 0 1.25 0 -0 0
        
          
            0.148611 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.148611 0.15 2.5
          
        
        -1.67569 0 1.25 0 -0 0
      
      
        1.07431 0 1.25 0 -0 0
        
          
            1.35139 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            1.35139 0.15 2.5
          
        
        1.07431 0 1.25 0 -0 0
      
      
        -0.601389 0 2.4 0 -0 0
        
          
            2 0.15 0.2
          
        
        
          
          1 1 1 1
        
      
      
        
          
            2 0.15 0.2
          
        
        -0.601389 0 2.4 0 -0 0
      
    
    
      
        
          
            1.25 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            1.25 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -5.20794 0.396477 0 0 -0 3.14159
    
    
      -5.75794 -1.65352 0 0 0 -1.5708
      
        -1.07794 0 1.25 0 -0 0
        
          
            2.09412 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            2.09412 0.15 2.5
          
        
        -1.07794 0 1.25 0 -0 0
      
      
        1.04706 0 0.25 0 -0 0
        
          
            2.15588 0.15 0.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            2.15588 0.15 0.5
          
        
        1.04706 0 0.25 0 -0 0
      
      
        2.01762 0 1.5 0 -0 0
        
          
            0.214766 0.15 2
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.214766 0.15 2
          
        
        2.01762 0 1.5 0 -0 0
      
      
        0.939679 0 1.9 0 -0 0
        
          
            1.94111 0.15 1.2
          
        
        
          
          1 1 1 1
        
      
      
        
          
            1.94111 0.15 1.2
          
        
        0.939679 0 1.9 0 -0 0
      
    
    
      
        
          
            2.5 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            2.5 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -2.53404 1.06604 0 0 -0 0
    
    
      
        
          
            2 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            2 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -1.35904 0.141035 0 0 0 -1.5708
    
    
      
        
          
            0.25 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            0.25 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -1.40904 -0.783965 0 0 -0 3.14159
    
    
      
        
          
            3.5 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            3.5 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -3.91607 -0.818528 0 0 -0 3.14159
    
    
      
        
          
            0.25 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            0.25 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -5.64107 -0.818528 0 0 -0 3.14159
    
    
      
        
          
            2 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            2 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -3.31229 0.121197 0 0 0 -1.5708
    
    
      
        
          
            1.75 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            1.75 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -1.86043 2.95447 0 0 0 -1.5708
    
    
      
        
          
            0.75 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            0.75 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -1.56043 2.15447 0 0 -0 0
    
    
      
        
          
            2.75 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            2.75 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -0.228595 2.39065 0 0 0 -1.5708
    
    
      
        
          
            2.75 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            2.75 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      1.29304 2.34692 0 0 0 -1.5708
    
    
      
        
          
            1.5 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            1.5 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      1.36463 1.03216 0 0 -0 0
    
    
      4.05869 -2.66379 0 0 -0 0
      
        -1.38368 0 1.25 0 -0 0
        
          
            0.732645 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.732645 0.15 2.5
          
        
        -1.38368 0 1.25 0 -0 0
      
      
        1.61632 0 1.25 0 -0 0
        
          
            0.267355 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.267355 0.15 2.5
          
        
        1.61632 0 1.25 0 -0 0
      
      
        0.232645 0 2.45 0 -0 0
        
          
            2.5 0.15 0.1
          
        
        
          
          1 1 1 1
        
      
      
        
          
            2.5 0.15 0.1
          
        
        0.232645 0 2.45 0 -0 0
      
    
    
      
        
          
            0.25 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            0.25 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -0.228595 1.04065 0 0 0 -1.5708
    
    
      
        
          
            3.75 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            3.75 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      -0.233629 -1.95772 0 0 0 -1.5708
    
    
      5.74567 0.386841 0 0 0 -1.57472
      
        -2.83254 0 1.25 0 -0 0
        
          
            0.586231 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.586231 0.15 2.5
          
        
        -2.83254 0 1.25 0 -0 0
      
      
        1.30211 0 1.25 0 -0 0
        
          
            3.64708 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            3.64708 0.15 2.5
          
        
        1.30211 0 1.25 0 -0 0
      
      
        -1.53042 0 2.4 0 -0 0
        
          
            2.018 0.15 0.2
          
        
        
          
          1 1 1 1
        
      
      
        
          
            2.018 0.15 0.2
          
        
        -1.53042 0 2.4 0 -0 0
      
    
    
      4.58265 3.43747 0 0 -0 3.14159
      
        -0.530284 0 1.25 0 -0 0
        
          
            1.43943 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            1.43943 0.15 2.5
          
        
        -0.530284 0 1.25 0 -0 0
      
      
        1.16972 0 1.25 0 -0 0
        
          
            0.160568 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.160568 0.15 2.5
          
        
        1.16972 0 1.25 0 -0 0
      
      
        0.639432 0 2.25 0 -0 0
        
          
            0.9 0.15 0.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.9 0.15 0.5
          
        
        0.639432 0 2.25 0 -0 0
      
    
    
      
        
          
            0.463251 0.15 2.5
          
        
        0 0 1.25 0 -0 0
      
      
        0 0 1.25 0 -0 0
        
          
            0.463251 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      3.38255 3.59207 0 0 -0 1.73174
    
    
      1.06733 -0.11797 0 0 -0 0.030549
      
        -1.28558 0 1.25 0 -0 0
        
          
            0.181978 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.181978 0.15 2.5
          
        
        -1.28558 0 1.25 0 -0 0
      
      
        0.540989 0 1.25 0 -0 0
        
          
            1.67116 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            1.67116 0.15 2.5
          
        
        0.540989 0 1.25 0 -0 0
      
      
        -0.744593 0 2.25 0 -0 0
        
          
            0.9 0.15 0.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.9 0.15 0.5
          
        
        -0.744593 0 2.25 0 -0 0
      
    
    
      -0.766361 -0.161298 0 0 -0 0.006708
      
        -0.534301 0 1.25 0 -0 0
        
          
            0.146885 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.146885 0.15 2.5
          
        
        -0.534301 0 1.25 0 -0 0
      
      
        0.511599 0 1.25 0 -0 0
        
          
            0.19229 0.15 2.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.19229 0.15 2.5
          
        
        0.511599 0 1.25 0 -0 0
      
      
        -0.022702 0 2.25 0 -0 0
        
          
            0.876312 0.15 0.5
          
        
        
          
          1 1 1 1
        
      
      
        
          
            0.876312 0.15 0.5
          
        
        -0.022702 0 2.25 0 -0 0
      
    
    1
  

主要是Gazebo仿真场景中各类物体的具体属性参数。

model.config:



    whiz_ex_window
    1.0
    model.sdf
    
        
        
    
    

可以做一些署名,邮箱,说明等。

launch:

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第4张图片

以gazebo_kamtoa.launch为例,具体说明:






  
  
  
  

  
  

  
  
  

  
  
      
  

  
  
  
      
      
  


分为基本参数,机器人描述(RVIZ和Gazebo),RVIZ初始化,Gazebo初始化等。这时,如果在终端运行:

~$ roslaunch kamtoa_gazebo gazebo_kamtoa.launch

可以启动roscore,rviz和gazebo如下:

relaybotbox@relaybotbox-desktop:~$ roslaunch kamtoa_gazebo gazebo_kamtoa.launch ... logging to /home/relaybotbox/.ros/log/e9deba26-9786-11e6-be4d-00e0b4159b08/roslaunch-relaybotbox-desktop-5005.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://relaybotbox-desktop:41694/

SUMMARY
========

PARAMETERS
 * /robot_description: , set to ""
[ INFO] [1477051645.459096731, 1288.802000000]: Laser Plugin (robotNamespace = /), Info: Using the 'robotNamespace' param: '/'
[ INFO] [1477051645.459748522, 1288.802000000]: Starting Laser Plugin (ns = /)!
[ INFO] [1477051645.476788533, 1288.802000000]: Laser Plugin (ns = /)  , set to ""
[ INFO] [1477051645.565133751, 1288.802000000]: Starting plugin DiffDrive(ns = //)!
[ INFO] [1477051645.565451828, 1288.802000000]: DiffDrive(ns = //):  = Debug
[ INFO] [1477051645.568106826, 1288.802000000]: DiffDrive(ns = //):  = 
[DEBUG] [1477051645.568619326, 1288.802000000]: DiffDrive(ns = //):  = cmd_vel
[DEBUG] [1477051645.568724979, 1288.802000000]: DiffDrive(ns = //):  = odom
[DEBUG] [1477051645.568867307, 1288.802000000]: DiffDrive(ns = //):  = odom
[DEBUG] [1477051645.568927646, 1288.802000000]: DiffDrive(ns = //):  = base_footprint
[DEBUG] [1477051645.569256884, 1288.802000000]: DiffDrive(ns = //):  = false
[DEBUG] [1477051645.569338164, 1288.802000000]: DiffDrive(ns = //):  = false
[DEBUG] [1477051645.569393655, 1288.802000000]: DiffDrive(ns = //):  = ture
[DEBUG] [1477051645.569843607, 1288.802000000]: DiffDrive(ns = //):  = 0.29999999999999999
[DEBUG] [1477051645.569951337, 1288.802000000]: DiffDrive(ns = //):  = 0.082000000000000003
[DEBUG] [1477051645.570012696, 1288.802000000]: DiffDrive(ns = //):  = 1.8
[DEBUG] [1477051645.570077175, 1288.802000000]: DiffDrive(ns = //):  = 30
[DEBUG] [1477051645.570139351, 1288.802000000]: DiffDrive(ns = //):  = 100
[ WARN] [1477051645.570811579, 1288.802000000]: DiffDrive(ns = //):  no matching key to 1
[DEBUG] [1477051645.571414503, 1288.802000000]: DiffDrive(ns = //):  = default := 1
[DEBUG] [1477051645.572053861, 1288.802000000]: DiffDrive(ns = //):  = front_left_wheel_joint
[DEBUG] [1477051645.572185748, 1288.802000000]: DiffDrive(ns = //):  = front_right_wheel_joint
[ INFO] [1477051645.576771098, 1288.802000000]: DiffDrive(ns = //): Try to subscribe to cmd_vel!
[ INFO] [1477051645.593900394, 1288.802000000]: DiffDrive(ns = //): Subscribe to cmd_vel!
[ INFO] [1477051645.597711053, 1288.802000000]: DiffDrive(ns = //): Advertise odom on odom !
[ INFO] [1477051645.648506913, 1288.825000000]: waitForService: Service [/gazebo/set_physics_properties] is now available.
[ INFO] [1477051645.836105586, 1288.862000000]: Physics dynamic reconfigure ready.

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第5张图片


ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第6张图片


可以用如下工具进行分析:

rqt_action           rqt_logger_level     rqt_robot_monitor
rqt_bag              rqt_moveit           rqt_robot_steering
rqt_bag_plugins      rqt_msg              rqt_runtime_monitor
rqt_console          rqt_nav_view         rqt_rviz
rqt_dep              rqt_plot             rqt_service_caller
rqt_graph            rqt_pose_view        rqt_shell
rqt_gui              rqt_publisher        rqt_srv
rqt_gui_cpp          rqt_py_common        rqt_tf_tree
rqt_gui_py           rqt_py_console       rqt_top
rqt_image_view       rqt_reconfigure      rqt_topic
rqt_launch           rqt_robot_dashboard  rqt_web

以rqt_topic为例,可以参看全部主题:

~$ rosrun rqt_topic rqt_topic
ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第7张图片
如果需要查看camera_rgb,可以运行如下命令:

~$ rosrun rqt_image_view rqt_image_view

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第8张图片

运行rosrun rqt_graph rqt_graph

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第9张图片

此时,gazebo与其他节点并无通信,然后启动如下命令:

~$ roslaunch kamtoa_teleop teleop_keyboard.launch

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第10张图片


    


这时,就可以使用键盘通过/cmd_vel控制gazebo中的机器人在环境中运动,当然键盘控制还有其他一些功能,请看如下源码。

#!/usr/bin/env python

# Copyright (c) 2011, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#    * Neither the name of the Willow Garage, Inc. nor the names of its
#      contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import rospy

from geometry_msgs.msg import Twist

import sys, select, termios, tty

msg = """
Control Your Turtlebot!
---------------------------
Moving around:
   u    i    o
   j    k    l
   m    ,    .
q/z : increase/decrease max speeds by 10%
w/x : increase/decrease only linear speed by 10%
e/c : increase/decrease only angular speed by 10%
space key, k : force stop
anything else : stop smoothly
CTRL-C to quit
"""
lightSwitches = {
    '1':'light0',
    '2':'light1',
    '3':'light2',
    '4':'light3',
    '5':'light4'
}

moveBindings = {
        'i':(1,0),
        'o':(1,-1),
        'j':(0,1),
        'l':(0,-1),
        'u':(1,1),
        ',':(-1,0),
        '.':(-1,1),
        'm':(-1,-1),
           }

speedBindings={
        'q':(1.1,1.1),
        'z':(.9,.9),
        'w':(1.1,1),
        'x':(.9,1),
        'e':(1,1.1),
        'c':(1,.9),
          }

def getKey():
    tty.setraw(sys.stdin.fileno())
    rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
    if rlist:
        key = sys.stdin.read(1)
    else:
        key = ''

    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
    return key

speed = .2
turn = 1

def vels(speed,turn):
    return "currently:\tspeed %s\tturn %s " % (speed,turn)

if __name__=="__main__":
    settings = termios.tcgetattr(sys.stdin)

    rospy.init_node('Kamtoa_teleop_keyboard')
    pub = rospy.Publisher('cmd_vel', Twist, queue_size=5)

    light_select = ''
    x = 0
    th = 0
    status = 0
    count = 0
    acc = 0.1
    target_speed = 0
    target_turn = 0
    control_speed = 0
    control_turn = 0
    try:
        print msg
        print vels(speed,turn)
        while(1):
            key = getKey()
            if key in moveBindings.keys():
                x = moveBindings[key][0]
                th = moveBindings[key][1]
                count = 0
            elif key in speedBindings.keys():
                speed = speed * speedBindings[key][0]
                turn = turn * speedBindings[key][1]
                count = 0

                print vels(speed,turn)
                if (status == 14):
                    print msg
                status = (status + 1) % 15
            elif key == ' ' or key == 'k' :
                x = 0
                th = 0
                control_speed = 0
                control_turn = 0
            elif key in lightSwitches.keys():
                print "lightSwitch pressed!"

            else:
                count = count + 1
                if count > 4:
                    x = 0
                    th = 0
                if (key == '\x03'):
                    break

            target_speed = speed * x
            target_turn = turn * th

            if target_speed > control_speed:
                control_speed = min( target_speed, control_speed + 0.02 )
            elif target_speed < control_speed:
                control_speed = max( target_speed, control_speed - 0.02 )
            else:
                control_speed = target_speed

            if target_turn > control_turn:
                control_turn = min( target_turn, control_turn + 0.1 )
            elif target_turn < control_turn:
                control_turn = max( target_turn, control_turn - 0.1 )
            else:
                control_turn = target_turn

            twist = Twist()
            twist.linear.x = control_speed; twist.linear.y = 0; twist.linear.z = 0
            twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = control_turn
            pub.publish(twist)

    except:
        print e

    finally:
        twist = Twist()
        twist.linear.x = 0; twist.linear.y = 0; twist.linear.z = 0
        twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = 0
        pub.publish(twist)

    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)


使用手机控制机器人运动也很方便,先查询一下地址:

relaybotbox@relaybotbox-desktop:~$ ifconfig -a
enp3s0    Link encap:以太网  硬件地址 00:e0:b4:15:9b:08  
          inet 地址:192.168.3.23  广播:192.168.3.255  掩码:255.255.255.0
          inet6 地址: fe80::3805:e5fe:19f0:19e2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  跃点数:1
          接收数据包:32257 错误:0 丢弃:0 过载:0 帧数:0
          发送数据包:41654 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:1000 
          接收字节:23057526 (23.0 MB)  发送字节:17828352 (17.8 MB)

enp4s0    Link encap:以太网  硬件地址 00:e0:b4:15:9b:09  
          UP BROADCAST MULTICAST  MTU:1500  跃点数:1
          接收数据包:0 错误:0 丢弃:0 过载:0 帧数:0
          发送数据包:0 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:1000 
          接收字节:0 (0.0 B)  发送字节:0 (0.0 B)

lo        Link encap:本地环回  
          inet 地址:127.0.0.1  掩码:255.0.0.0
          inet6 地址: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  跃点数:1
          接收数据包:8314866 错误:0 丢弃:0 过载:0 帧数:0
          发送数据包:8314866 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:1 
          接收字节:671600054 (671.6 MB)  发送字节:671600054 (671.6 MB)

在手机设置IP:192.168.3.23。连接成功后可以看到:

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第11张图片

如何让机器人在环境中进行导航呢?运行下面命令:

~$ roslaunch kamtoa_navigation kamtoa_navigation.launch

其中代码如下:





  
  
  


  
  
  
  

  
  
    
    
    
  

  
  

  
  



是不是有很多眼熟的内容,导入Map,机器人位置初值,AMCL,速度平滑,导航等。

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第12张图片

这是再看下rqt_graph:

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第13张图片

各个节点之间联系清晰可见。

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第14张图片

路径规划效果如下:

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第15张图片

ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)_第16张图片

其他内容后续补充。

附加内容:

http://rosclub.cn/post-569.html

参考如下:

ROS与Android的窃窃私语

2016-12-2 12:01| 发布者: dajianli| 查看: 79| 评论: 0|来自: 奥斯卡的个人剧场

摘要: ROS (Robot Operating System, 机器人操作系统) 提供一系列程序库和工具以帮助软件开发者创建机器人应用软件。它提供了硬件抽象、设备驱动、函数库、可视化工具、消息传递和软件包管理等诸多功能。ROS遵循BSD开源许 ...

# 简介

ROS (Robot Operating System, 机器人操作系统) 提供一系列程序库和工具以帮助软件开发者创建机器人应用软件。它提供了硬件抽象、设备驱动、函数库、可视化工具、消息传递和软件包管理等诸多功能。ROS遵循BSD开源许可协议

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发

ROS严格来说并不算OS,而是一套运行于linux之上,连接机器人软硬件的软件包,而Android是针对于移动端的定制Linux.我们主要来聊聊ROS与Android之间的通信

# 通信方式

目前,ROS与Android主要的通信方式有两种:

  1. rosjava&android_ndk

    ROS的java语言实现,借助该类库,能在Android上运行ROS 节点,功能强大,但是编译比较麻烦,最大的问题是ROS对其支持只到indigo版本!

  2. rosbridge

    通过Websocket以JSON格式的API为非ROS环境提供ROS通信支持,包括对Topic和Service的各种操作,这种通信方式轻量级,跨平台

接下来我们主要讨论第二种方式的实现过程

# 具体实现

1、了解通信协议 rosbridge protocol

2、ROS端安装对应版本rosbridge-suite

1 sudo apt-get install ros--rosbridge-suite

3、ROS端启动launch,你也可以include到自己的launch文件

1 roslaunch rosbridge_server rosbridge_websocket.launch

4、Android端需要一个websocket的java实现,然后就是按协议解析json,这好像也挺麻烦的嘛,不过好在找到一个开源项目ROSBridgeClient,貌似是一个netbeans project,我是直接把源码Copy到自己项目中来,该库依赖于java_websocket 和 json_simple两个library,这样就可以开始写你自己的逻辑代码了

# APK Demo

目前APK已实现功能有:

  1. 根据IP和连接到ROS
  2. 获取ROS端所有Node list, Topic list, Service list
  3. 获取指定Topic或者Service的请求参数,并提供请求接口
  4. 针对Topic /cmd_vel 做了一个方向摇杆控制
  5. 针对Topic /map做了一个地图的展示

理论上来说,应该能在Android端实现rviz类似的功能,大家可以体验一下我实现的Demo (下载地址点我) ,最后放几张截图





你可能感兴趣的:(ROS,Kinetic,学习笔记)