说明
介绍
创建包
$ cd ~/catkin_ws/src
$ catkin_create_pkg my_pkg message_generation rospy
cmake_minimum_required(VERSION 2.8.3)
project(my_pkg)
find_package(catkin REQUIRED COMPONENTS message_generation rospy ...)
catkin_package()
添加message和service
<build_depend>message_generation</build_depend>
cmake_minimum_required(VERSION 2.8.3)
project(my_pkg)
find_package(catkin REQUIRED COMPONENTS message_generation rospy)
add_message_files(
FILES # e.g. Floats.msg HeaderString.msg
)
add_service_files(
DIRECTORY srv
FILES AddTwoInts.srv BadTwoInts.srv
)
## Generate services in the 'srv' folder
# add_service_files(
# FILES # e.g. Floats.srv HeaderString.srv
#)
## Generate added messages and services with any dependencies
generate_messages()
catkin_package(
CATKIN_DEPENDS message_runtime
)
安装脚本和导出模块
$ cd ~/catkin_ws/src/my_pkg # new catkin package, in the workspace
$ mkdir bin
$ mkdir src
$ mkdir src/tutorial_package
$ touch src/tutorial_package/__init__.py
def say(name):
print('Hello ' + name)
#! /usr/bin/env python
import tutorial_package.hello
if __name__ == '__main__':
tutorial_package.hello.say('my friend!')
$ chmod u+x bin/hello
$ bin/hello
Traceback (most recent call last):
File "bin/hello", line 3, in <module>
import tutorial_package.hello
ImportError: No module named tutorial_package.hello
## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup
# fetch values from package.xml
setup_args = generate_distutils_setup(
packages=['tutorial_package'],
package_dir={'': 'src'},
)
setup(**setup_args)
## Uncomment if the package has a setup.py
catkin_python_setup()
catkin_install_python(PROGRAMS bin/hello
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
$ cd ~/catkin_ws
$ catkin_make
$ . devel/setup.bash
$ rosrun my_pkg hello
Hello my friend!
在beginner_tutorials包里测试
$ roscd beginner_tutorials
$ mkdir bin
$ mkdir src
$ mkdir src/tutorial_package
$ touch src/tutorial_package/__init__.py
$ chmod +x src/tutorial_package/__init__.py
$ touch src/tutorial_package/hello.py
$ chmod +x src/tutorial_package/hello.py
def say(name):
print('Hello' + name)
$ roscd beginner_tutorials/bin
$ touch hello
$ chmod +x hello
$ rosed beginner_tutorials hello
#! /usr/bin/env python
import tutorial_package.hello
if __name__ == '__main__':
tutorial_package.hello.say('my friend!')
$ roscd beginner_tutorials
$ touch setup.py
$ chmod +x setup.py
$ rosed beginner_tutorials setup.py
## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup
# fetch values from package.xml
setup_args = generate_distutils_setup(
packages=['tutorial_package'],
package_dir={'': 'src'},
)
setup(**setup_args)
## Uncomment if the package has a setup.py
catkin_python_setup()
catkin_install_python(PROGRAMS bin/hello
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
$ cd ~/catkin_ws
$ catkin_make
$ . devel/setup.bash
$ rosrun beginner_tutorials hello
Hello my friend!