1. 程式人生 > >ros中建立msg和srv檔案時,配置CMakeLists.txt檔案問題

ros中建立msg和srv檔案時,配置CMakeLists.txt檔案問題

作為一個ROS菜鳥,在按照ros wiki上的教程一步一步的走的過程中,在自己配置msg和srv檔案時,遇到了編譯的問題,分析問題,發現是package下的CMakeLists.txt檔案配置出現問題。

以下是建立並編譯一個新的package後生成的CMakeLists.txt檔案內容。

cmake_minimum_required(VERSION 2.8.3)
project(chloe)

## 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 roscpp rospy std_msgs)

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

## Uncomment this if the package has a setup.py. This macro ensures
## modules and scripts declared therein get installed
# catkin_python_setup()

#######################################
## Declare ROS messages and services ##
#######################################

## Generate messages in the 'msg' folder
# add_message_files(
#   FILES
#   Message1.msg
#   Message2.msg
# )

## Generate services in the 'srv' folder
# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )

## Generate added messages and services with any dependencies listed here
# generate_messages(
#   DEPENDENCIES
#   std_msgs
# )

###################################################
## Declare things to be passed to other projects ##
###################################################

## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
   INCLUDE_DIRS include
#  LIBRARIES chloe
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
include_directories(include
  ${catkin_INCLUDE_DIRS}
)

## Declare a cpp library
# add_library(chloe
#   src/${PROJECT_NAME}/chloe.cpp
# )

## Declare a cpp executable
# add_executable(chloe_node src/chloe_node.cpp)

## Add dependencies to the executable
# add_dependencies(chloe_node ${PROJECT_NAME})

## Specify libraries to link a library or executable target against
# target_link_libraries(chloe_node
#   ${catkin_LIBRARIES}
# )

#############
## Install ##
#############

## Mark executable scripts (Python etc.) for installation
## not required for python when using catkin_python_setup()
# install(PROGRAMS
#   scripts/my_python_script
#   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark executables and/or libraries for installation
# install(TARGETS chloe chloe_node
#   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
#   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
#   FILES_MATCHING PATTERN "*.h"
#   PATTERN ".svn" EXCLUDE
# )

## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
#   # myfile1
#   # myfile2
#   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )

#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_chloe.cpp)
# if(TARGET ${PROJECT_NAME}-test)
#   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)


在建立的package中建立資料夾msg和srv,在這兩個資料夾下分別編輯.msg和.srv檔案。

例如:進入自己的workspace的package下的msg資料夾,利用gedit編輯msg檔案。命令如下:

cd ~/wry_ws/src/chloe/msg
gedit Num.msg
在Num.msg檔案中輸入以下內容並儲存:
string first_name
string last_name
以上就建立了一個msg檔案,下面是最關鍵的,需要確保msg檔案被轉換成為C++,Python和其他語言的原始碼。

配置步驟如下:

首先檢視Chloe功能包中的package.xml檔案中是否包含以下兩行,若沒有,就加上:

<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
下面利用gedit開啟CMakeLists.txt檔案,並對其中的需要修改及補充的內容進行說明。
#find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs)
在這裡,指明瞭構建chloe這個package,所依賴的包,因為要使用catkin編譯功能包,所以包含catkin,另外,在編譯所建立的msg檔案時,需要依賴message_generation,因此,去掉前面的註釋,並更改為:
find_package(catkin REQUIRED COMPONENTS 
               roscpp
               rospy 
               std_msgs
               message_generation
)
下面修改message_files
#  add_message_files(
#   FILES
#   Message1.msg 
#   Message2.msg
# )
去掉其中的註釋,並將
Message1.msg 
修改為自己建立的msg檔案的名字。本人的修改後的程式碼為:
add_message_files(
    FILES
    Num.msg 
)
注意:如果建立了srv檔案,那麼就需要將
# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )
#   Service1.srv
#   Service2.srv
改為自己建立的srv檔案的名字,並去掉註釋。

下面修改:

#  generate_messages(
#   DEPENDENCIES
#   std_msgs
# )

需要將其註釋去掉,改為:

 generate_messages(
  DEPENDENCIES
  std_msgs
 )
最後,要確保自己運行了依賴:
catkin_package(
   INCLUDE_DIRS include
#  LIBRARIES chloe
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)

這裡,需要新增message_runtime,並且依賴其他的package,改為:

catkin_package(
   INCLUDE_DIRS include
   LIBRARIES chloe
   CATKIN_DEPENDS message_runtime
   roscpp
   rospy
   std_msgs
   DEPENDS system_lib
)


到此,檔案中需要配置和更改就完成了。由於重新配置了CMakeLists.txt檔案,因此需要對workspace盡心編譯。

需要注意的是:

find_package(...)

add_message_files(...)

add_service_files(...)

generate_message(...)

catkin_package(...)
這幾項的先後順序不能發生變化。

下面利用rosmsg show filename.ms和rossrv show filename.srv檔案就可以檢視其中的內容。

注意:所有在msg資料夾下的.msg檔案都將轉換為ROS所支援的語言的原始檔。生成的C++標頭檔案將會放置在~/wry_ws/devel/include/chloe/。Python指令碼語言在~/wry_ws/devel/lib/python2.7/dist-package/chloe/下。