pythonでROS1のpub/sub_part1

やること

  • ROS1の作業フォルダ作成
  • ROS1でメッセージのやり取り(pub/sub) (pub/subは失敗)

    ROS1の作業フォルダの作成

    フォルダの作成及び、melodicのpath設定

mkdir -p rospython/src
cd rospython
source /opt/ros/melodic/setup.bash
tree .

実行結果

.
└── src

1 directory, 0 files

makeで必要なファイルを作成

catkin_make

実行結果

Base path: /home/hogehoge/rospython
Source space: /home/hogehoge/rospython/src
Build space: /home/hogehoge/rospython/build
Devel space: /home/hogehoge/rospython/devel
Install space: /home/hogehoge/rospython/install
Creating symlink "/home/hogehoge/rospython/src/CMakeLists.txt" pointing to "/opt/ros/melodic/share/catkin/cmake/toplevel.cmake"
####
#### Running command: "cmake /home/hogehoge/rospython/src -DCATKIN_DEVEL_PREFIX=/home/hogehoge/rospython/devel -DCMAKE_INSTALL_PREFIX=/home/hogehoge/rospython/install -G Unix Makefiles" in "/home/hogehoge/rospython/build"
####
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working 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
-- 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using CATKIN_DEVEL_PREFIX: /home/hogehoge/rospython/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/melodic
-- This workspace overlays: /opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.17", 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/hogehoge/rospython/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.17")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.23
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hogehoge/rospython/build
####
#### Running command: "make -j8 -l8" in "/home/hogehoge/rospython/build"
####

するとファイルが作成される

├── build
│   ├── CMakeFiles
│   │   ├── 3.10.2
│   │   │   ├── CompilerIdC
│   │   │   │   └── tmp
│   │   │   └── CompilerIdCXX
│   │   │       └── tmp
│   │   ├── CMakeTmp
│   │   ├── clean_test_results.dir
│   │   ├── download_extra_data.dir
│   │   ├── doxygen.dir
│   │   ├── run_tests.dir
│   │   └── tests.dir
│   ├── atomic_configure
│   ├── catkin
│   │   └── catkin_generated
│   │       └── version
│   ├── catkin_generated
│   │   ├── installspace
│   │   └── stamps
│   │       └── Project
│   ├── gtest
│   │   ├── CMakeFiles
│   │   └── googlemock
│   │       ├── CMakeFiles
│   │       │   ├── gmock.dir
│   │       │   │   ├── __
│   │       │   │   │   └── googletest
│   │       │   │   │       └── src
│   │       │   │   └── src
│   │       │   └── gmock_main.dir
│   │       │       ├── __
│   │       │       │   └── googletest
│   │       │       │       └── src
│   │       │       └── src
│   │       └── gtest
│   │           └── CMakeFiles
│   │               ├── gtest.dir
│   │               │   └── src
│   │               └── gtest_main.dir
│   │                   └── src
│   └── test_results
├── devel
│   └── lib
└── src

必要なパッケージを追加して、オリジナルのパッケージを作る

cd src
catkin_create_pkg ros_pubsubtest roscpp rospy std_msgs

出力結果

Created file ros_pubsubtest/package.xml
Created file ros_pubsubtest/CMakeLists.txt
Created folder ros_pubsubtest/include/ros_pubsubtest
Created folder ros_pubsubtest/src
Successfully created files in /home/hogehoge/rospython/src/ros_pubsubtest. Please adjust the values in package.xml.

すると、以下の要はフォルダーが出来上がります

/rospython/src$ tree .
.
├── CMakeLists.txt -> /opt/ros/melodic/share/catkin/cmake/toplevel.cmake
└── ros_pubsubtest
    ├── CMakeLists.txt
    ├── include
    │   └── ros_pubsubtest
    ├── package.xml
    └── src

最後に、もう一度catkin_makeをrospython内で行うとフォルダの完成です

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - ros_pubsubtest
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'ros_pubsubtest'
-- ==> add_subdirectory(ros_pubsubtest)

こんなログが出てきます

pub/sub作成

/rospython/src/ros_pubsubtest/msg/にpubsub.msgというファイルを作り、以下を記述する

int32 arg_x
int32 arg_y

14行目ぐらいのfind_packageにmessage_generationを追加
51行目ぐらいのadd_message_filesをコメント解除し、pubsub.msgを追加
73行目ぐらいのgenerate_messagesをすべてコメント解除
107行目ぐらいのcatkin_packageのLIBRARIES ros_pubsubtest, CATKIN_DEPENDS roscpp rospy std_msgs, DEPENDS system_libをコメント解除

rospythonに戻って、catkin_makeを実行すると

Base path: /home/hogehoge/rospython
Source space: /home/hogehoge/rospython/src
Build space: /home/hogehoge/rospython/build
Devel space: /home/hogehoge/rospython/devel
Install space: /home/hogehoge/rospython/install
####
#### Running command: "make cmake_check_build_system" in "/home/hogehoge/rospython/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/hogehoge/rospython/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/melodic
-- This workspace overlays: /opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.17", 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/hogehoge/rospython/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.17")
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.23
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - ros_pubsubtest
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'ros_pubsubtest'
-- ==> add_subdirectory(ros_pubsubtest)
-- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy
-- ros_pubsubtest: 1 messages, 0 services
CMake Warning at /opt/ros/melodic/share/catkin/cmake/catkin_package.cmake:166 (message):
  catkin_package() DEPENDS on 'system_lib' but neither
  'system_lib_INCLUDE_DIRS' nor 'system_lib_LIBRARIES' is defined.
Call Stack (most recent call first):
  /opt/ros/melodic/share/catkin/cmake/catkin_package.cmake:102 (_catkin_package)
  ros_pubsubtest/CMakeLists.txt:107 (catkin_package)


-- Configuring done
-- Generating done
-- Build files have been written to: /home/hogehoge/rospython/build
####
#### Running command: "make -j8 -l8" in "/home/hogehoge/rospython/build"
####
Scanning dependencies of target std_msgs_generate_messages_eus
Scanning dependencies of target _ros_pubsubtest_generate_messages_check_deps_pubsub
Scanning dependencies of target std_msgs_generate_messages_nodejs
Scanning dependencies of target std_msgs_generate_messages_py
Scanning dependencies of target std_msgs_generate_messages_lisp
Scanning dependencies of target std_msgs_generate_messages_cpp
[  0%] Built target std_msgs_generate_messages_eus
[  0%] Built target std_msgs_generate_messages_nodejs
[  0%] Built target std_msgs_generate_messages_py
[  0%] Built target std_msgs_generate_messages_cpp
[  0%] Built target std_msgs_generate_messages_lisp
[  0%] Built target _ros_pubsubtest_generate_messages_check_deps_pubsub
Scanning dependencies of target ros_pubsubtest_generate_messages_eus
Scanning dependencies of target ros_pubsubtest_generate_messages_py
Scanning dependencies of target ros_pubsubtest_generate_messages_nodejs
Scanning dependencies of target ros_pubsubtest_generate_messages_cpp
Scanning dependencies of target ros_pubsubtest_generate_messages_lisp
[ 14%] Generating EusLisp code from ros_pubsubtest/pubsub.msg
[ 28%] Generating Python from MSG ros_pubsubtest/pubsub
[ 57%] Generating EusLisp manifest code for ros_pubsubtest
[ 57%] Generating Javascript code from ros_pubsubtest/pubsub.msg
[ 71%] Generating C++ code from ros_pubsubtest/pubsub.msg
[ 85%] Generating Lisp code from ros_pubsubtest/pubsub.msg
[ 85%] Built target ros_pubsubtest_generate_messages_nodejs
[ 85%] Built target ros_pubsubtest_generate_messages_lisp
[ 85%] Built target ros_pubsubtest_generate_messages_cpp
[100%] Generating Python msg __init__.py for ros_pubsubtest
[100%] Built target ros_pubsubtest_generate_messages_py
[100%] Built target ros_pubsubtest_generate_messages_eus
Scanning dependencies of target ros_pubsubtest_generate_messages
[100%] Built target ros_pubsubtest_generate_messages

というログが出る

次に、pub/subをするpythonコードを作成する
ros_pubsubtestフォルダ内に、scriptフォルダを作成し、その中にコードを作る publish.py

#! /usr/bin/python
# -*- cording: utf-8 -*-

import rospy

from ros_pubsubtest.msg import pubsub

def pub():
    rospy.init_node('pub', anonymous=True)

    pub = rospy.Publisher('input_data', pubsub, queue_size=100)

    r = rospy.Rate(10)

    para_x = 0
    para_y = 2

    msg = pubsub()

    while not rospy.is_shutdown():
        msg.arg_x = para_x
        msg.arg_y = para_y

        pub.publish(msg)
        print "published arg_x=%d arg_y=%d"%(msg.arg_x, msg.arg_y)
        para_x += 1
        para_y += 1

        r.sleep()

if __name__=='__main__':
    try:
        pub()
    except rospy.ROSInterruptException:
        pass

subscribe.py

#! /usr/bin/python
# -*- coding: utf-8 -*-

import rospy
from ros_pubsubtest.msg import pubsub

def callback(data):
    print data.arg_x + data.arg_y

def sub():
    rospy.init_node('sub', anonymous = True)

    rospy.Subscriber('input_data', pubsub, callback)

    rospy.spin()

if __name__ == '__main__':
    sub()

~/rospython/でcatkin_makeを実行

Base path: /home/hogehoge/rospython
Source space: /home/hogehoge/rospython/src
Build space: /home/hogehoge/rospython/build
Devel space: /home/hogehoge/rospython/devel
Install space: /home/hogehoge/rospython/install
####
#### Running command: "make cmake_check_build_system" in "/home/hogehoge/rospython/build"
####
####
#### Running command: "make -j8 -l8" in "/home/hogehoge/rospython/build"
####
[  0%] Built target std_msgs_generate_messages_eus
[  0%] Built target std_msgs_generate_messages_py
[  0%] Built target std_msgs_generate_messages_nodejs
[  0%] Built target std_msgs_generate_messages_cpp
[  0%] Built target std_msgs_generate_messages_lisp
[  0%] Built target _ros_pubsubtest_generate_messages_check_deps_pubsub
[ 42%] Built target ros_pubsubtest_generate_messages_eus
[ 57%] Built target ros_pubsubtest_generate_messages_py
[ 85%] Built target ros_pubsubtest_generate_messages_nodejs
[ 85%] Built target ros_pubsubtest_generate_messages_cpp
[100%] Built target ros_pubsubtest_generate_messages_lisp
[100%] Built target ros_pubsubtest_generate_messages

ビルドが完了したので実行してみる

terminal1

cd ~/rospython; source devel/setup.bash
roscore

無事にroscore起動

terminal2

cd ~/rospython; source devel/setup.bash
rosrun ros_pubsubtest publish.py

エラーが出る

[rosrun] Couldn't find executable named publish.py below /home/hogehoge/rospython/src/ros_pubsubtest
[rosrun] Found the following, but they're either not files,
[rosrun] or not executable:
[rosrun]   /home/hogehoge/rospython/src/ros_pubsubtest/script/publish.py

長くなったので、エラー解析は次回ということで。。。