ROS学习(eight): Creating a ROS msg and srv

cd ..是返回上一层目录, cd -是返回到上一次的工作目录。

msg 文件:是一个简单的text文本文件, 该文本文件被用来描述ros message
常常被用来生成不同的源代码; 对应与 msg 文件夹

				int8, int16, int32, int64 (plus uint*)
				float32, float64
				string
				time, duration(持续时间)
				other msg files
				variable-length array[] and fixed-length array[C]

srv 文件: 描述一个服务 service, 该文件包含两个内容:request 和 response;这两个部分被 ‘—’ 分割开来,上面三request 下面对应的是 response ; 对应于 srv 文件夹

# 一个简单的 msg 文件
	Header header
	string child_frame_id
	geometry_msgs/PoseWithCovariance pose
	geometry_msgs/TwistWithCovariance twist

# 一个简单的 srv 文件
#  A and B are the request, and Sum is the response.
	int64 A
	int64 B
	---
	int64 Sum

创建一个msg文件

fengxuewei@fengxuewei:~/C++_myself/catkin_ws$ roscd beginner_tutorials
fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials$ mkdir msg
fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials$ ls
CMakeLists.txt  include  launch  msg  package.xml  src

# 将数据写入到 msg 文件中
fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials$ echo "int64 num" > msg/Num.msg
fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials$ cd msg/
fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials/msg$ ls
Num.msg

我们需要把 msg 文件转换为为 c++ 代码, python代码等

1. 在package.xml确保增加 两行 
	<!-- 在 build 的时候,只需要加上 message_generation -->
	<build_depend>message_generation</build_depend>
	<!-- 在 run 的时候,只需要加上 message_runtime -->  
 	<exec_depend>message_runtime</exec_depend>
2. 增加 message_generation 的依赖项添加到 find_package 的调用中(存在在CMakeLists.txt),方便接下来生成消息,为了这个可以简单的将 message_generation 添加到 下面的代码中(COMPONENTS 队列中)
# Do not just add this to your CMakeLists.txt, modify the existing text to add message_generation before the closing parenthesis
find_package(catkin REQUIRED COMPONENTS
   roscpp
   rospy
   std_msgs
   message_generation
)
3. 增加 message_runtime 依赖项到 catkin_package 中(在CMakeLists.txt中)
	catkin_package(
  ...
  CATKIN_DEPENDS message_runtime ...
  ...)
4. 在CMakeLists.txt中寻找 add_message_files, 取消其 “#” 注释, 添加 上面创建的消息文件到该点
	add_message_files(
  		FILES
  		Num.msg
	) 
	通过手动增加 .msg 文件, 确保 CMake 知道在增加其他的 .msg 文件的之后,什么时候重新初始化 该项目
	
5. 确保 generate_messages() 函数被调用
	generate_messages(
	  DEPENDENCIES
	  std_msgs
	)
(6. # rosbuild_genmsg() 在CMakeLists.txt文件中)

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

创建一个 srv 文件

$ roscd beginner_tutorials
$ mkdir srv

# 使用 copy 指令 从其他地方 复制一个service文件到srv文件夹下
roscp [package_name] [file_to_copy_path] [copy_path]
# 复制 rospy_tutorials 包下的 AddTwoInts.srv 文件到 该目录下的 srv/AddTwoInts.srv 文件中
roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials/srv$ cat AddTwoInts.srv 
	int64 a
	int64 b
	---
	int64 sum

1. package.xml 文件中
  <build_depend>message_generation</build_depend>
  <exec_depend>message_runtime</exec_depend>
2. # Do not just add this line to your CMakeLists.txt, modify the existing line
	find_package(catkin REQUIRED COMPONENTS
	  roscpp
	  rospy
	  std_msgs
	  message_generation
	)
3. add_service_files(
	  FILES
	  AddTwoInts.srv
	)

compile

编译:在 catkin_ws/ 下运行 catkin_make install 即可
# In your catkin workspace
$ roscd beginner_tutorials
$ cd ../..
$ catkin_make install
$ cd - # 返回上一次工作目录
# Any .msg file in the msg directory will generate code for use in all supported languages. 
# The C++ i消息的头文件:	 ~/catkin_ws/devel/include/beginner_tutorials/. 
# The Python 脚本:		 ~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg. 
# The lisp file(和标准输入输出有关系): ~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/msg/.

rosmsg

Commands:
	rosmsg show		Show message description
	rosmsg info		Alias for rosmsg show
	rosmsg list		List all messages
	rosmsg md5		Display message md5sum
	rosmsg package	List messages in a package
	rosmsg packages	List packages that contain messages


# rosmsg show [message type]
# 	beginner_tutorials -- the package where the message is defined
# 	Num -- The name of the msg Num
fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials/include/beginner_tutorials$ rosmsg show beginner_tutorials/Num
	string first_name
	string last_name
	uint8 age
	uint32 score
	uint64 num

# 如何不能记住所处的具体包,可以使用下面的指令
fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials/include/beginner_tutorials$ rosmsg show Num
	[beginner_tutorials/Num]:
	string first_name
	string last_name
	uint8 age
	uint32 score
	uint64 num

rossrv

Commands:
	rossrv show		Show service description
	rossrv info		Alias for rossrv show
	rossrv list		List all services
	rossrv md5		Display service md5sum
	rossrv package	List services in a package
	rossrv packages	List packages that contain services


# rossrv show <service type>
fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials$ rossrv show beginner_tutorials/AddTwoInts
	int64 a
	int64 b
	---
	int64 sum

# rossrv show service_name
fengxuewei@fengxuewei:~/C++_myself/catkin_ws/src/beginner_tutorials$ rossrv show AddTwoInts
	[beginner_tutorials/AddTwoInts]:
	int64 a
	int64 b
	---
	int64 sum

	[rospy_tutorials/AddTwoInts]:
	int64 a
	int64 b
	---
	int64 sum
rospack = ros+pack(age) : 
	provides information related to ROS packages
roscd = ros+cd :
	changes directory to a ROS package or stack
rosls = ros+ls : 
	lists files in a ROS package
roscp = ros+cp : 
	copies files from/to a ROS package

消息和服务
rosmsg = ros+msg : 
	provides information related to ROS message definitions
rossrv = ros+srv : 
	provides information related to ROS service definitions
	
编译一个 ros 包
catkin_make : 
	makes (compiles) a ROS package
rosmake = ros+make : 
	makes (compiles) a ROS package (if you're not using a catkin workspace)
发布了29 篇原创文章 · 获赞 0 · 访问量 1723

猜你喜欢

转载自blog.csdn.net/fengxuewei123/article/details/104081228