创建ROS的msg和srv

Creating a ROS msg and srv


Introduction to msg and srv

  • msg: msg files are simple text files that describe the fields of a ROS message. They are used to generate source code for messages in different languages.

  • srv: an srv file describes a service. It is composed of two parts: a request and a response.

msg files are stored in the msg directory of a package, and srv files are stored in the srv directory.

msgs are just simple text files with a field type and field name per line. The field types you can use are:

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

There is also a special type in ROS:Header, the header contains a timestamp and coordinate frame information that are commonly used in ROS. You will commonly see the first line in a msg file haveHeader header.

Here is an example of a msg that uses a Header, a string primitive, and two other msgs :

  Header header
  string child_frame_id
  geometry_msgs/PoseWithCovariance pose
  geometry_msgs/TwistWithCovariance twist

srv files are just like msg files, except they contain two parts: a request and a response. The two parts are separated by a '---' line. Here is an example of a srv file:

int64 A
int64 B
---
int64 Sum

In the above example, A andB are the request, andSum is the response.

Creating a msg

Let's define a new msg in the package that was created in the previous tutorial.

$ roscd beginner_tutorials
$ mkdir msg
$ echo "int64 num" > msg/Num.msg

There's one more step, though. We need to make sure that the msg files are turned into source code for C++, Python, and other languages:

Open CMakeLists.txt in your favorite text editor. Remove# to uncomment the following line:

#rosbuild_genmsg()

Using rosmsg

That's all you need to do to create a msg. Let's make sure that ROS can see it using therosmsg show command.

Usage:

$ rosmsg show [message type]

Example:

$ rosmsg show beginner_tutorials/Num

You will see:

  • int64 num

In the previous example, the message type consists of two parts:

  • beginner_tutorials -- the package where the message is defined

  • Num -- The name of the msgNum.

If you can't remember which Package a msg is in, you can leave out the package name. Try:

$ rosmsg show Num

You will see:

  • [beginner_tutorials/Num]:
    int64 num

Creating a srv

Let's use the package we just created to create a srv:

$ roscd beginner_tutorials
$ mkdir srv

Instead of creating a new srv definition by hand, we will copy an existing one from another package.

Using roscp

roscp is a useful commandline tool for copying files from one package to another.

Usage:

$ roscp [package_name] [file_to_copy_path] [copy_path]

Now we can copy a service from the rospy_tutorials package:

$ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

There's one more step, though. We need to make sure that the srv files are turned into source code for C++, Python, and other languages:

Open CMakeLists.txt in your favorite text editor. Remove# to uncomment the following line:

#rosbuild_gensrv()

Using rossrv

That's all you need to do to create a srv. Let's make sure that ROS can see it using therossrv show command.

Usage:

$ rossrv show <service type>

Example:

$ rossrv show beginner_tutorials/AddTwoInts

You will see:

  • int64 a
    int64 b
    ---
    int64 sum

Now that we have made some new messages we need to make our package again

$ rosmake beginner_tutorials

Getting Help

We've seen quite a few ROS tools already. It can be difficult to keep track of what arguments each command requires. Luckily, most ROS tools provide their own help.

Try:

$ rosmsg -h
  • You should see a list of differentrosmsg subcommands.

    Commands:
            rosmsg show     Show message description
            rosmsg users    Find files that use message
            rosmsg md5      Display message md5sum
            rosmsg package  List messages in a package
            rosmsg packages List packages that contain messages

You can also get help for subcommands

$ rosmsg show -h
This shows the arguments that are needed for rosmsg show:
Usage: rosmsg show [options] <message type>

Options:
  -h, --help  show this help message and exit
  -r, --raw   show raw message text, including comments

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用ROS实现服务器和客户端通信程序的实验总结: 1. 实验目的 通过本次实验,学生应该掌握ROS的基本概念和使用方法,了解ROS中的节点、话题、服务等概念,能够使用ROS编写简单的服务器和客户端通信程序。 2. 实验环境 本实验使用的ROS版本为ROS Kinetic,操作系统为Ubuntu 16.04。 3. 实验步骤 (1)创建ROS工作空间 在终端中执行以下命令: ``` mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src catkin_init_workspace cd .. catkin_make source devel/setup.bash ``` (2)创建ROS包和节点 在终端中执行以下命令: ``` cd ~/catkin_ws/src catkin_create_pkg my_ros_pkg rospy cd my_ros_pkg mkdir scripts cd scripts touch my_ros_node.py chmod +x my_ros_node.py ``` 在my_ros_node.py文件中编写节点代码,代码如下: ``` #!/usr/bin/env python import rospy from std_msgs.msg import String def callback(data): rospy.loginfo("Received message: %s", data.data) def my_ros_node(): rospy.init_node('my_ros_node', anonymous=True) rospy.Subscriber("my_topic", String, callback) rospy.spin() if __name__ == '__main__': try: my_ros_node() except rospy.ROSInterruptException: pass ``` (3)创建ROS服务 在my_ros_pkg包中创建srv文件夹,并在其中创建一个名为MyService.srv的服务文件,文件内容如下: ``` string request --- string response ``` (4)编写服务端代码 在scripts文件夹中编写my_ros_server.py文件,文件内容如下: ``` #!/usr/bin/env python import rospy from my_ros_pkg.srv import MyService def handle_request(req): rospy.loginfo("Received request: %s", req.request) response = "Hello, " + req.request return response def my_ros_server(): rospy.init_node('my_ros_server', anonymous=True) rospy.Service('my_service', MyService, handle_request) rospy.loginfo("Ready to handle requests.") rospy.spin() if __name__ == '__main__': my_ros_server() ``` (5)编写客户端代码 在scripts文件夹中编写my_ros_client.py文件,文件内容如下: ``` #!/usr/bin/env python import rospy from my_ros_pkg.srv import MyService def my_ros_client(): rospy.wait_for_service('my_service') try: my_service = rospy.ServiceProxy('my_service', MyService) request = raw_input("Enter your name: ") response = my_service(request) rospy.loginfo("Received response: %s", response.response) except rospy.ServiceException as e: rospy.logerr("Service call failed: %s", e) if __name__ == '__main__': rospy.init_node('my_ros_client', anonymous=True) my_ros_client() ``` (6)运行程序 在终端中执行以下命令: ``` cd ~/catkin_ws catkin_make source devel/setup.bash roscore ``` 在新的终端中执行以下命令: ``` cd ~/catkin_ws source devel/setup.bash rosrun my_ros_pkg my_ros_node.py ``` 在新的终端中执行以下命令: ``` cd ~/catkin_ws source devel/setup.bash rosrun my_ros_pkg my_ros_server.py ``` 在新的终端中执行以下命令: ``` cd ~/catkin_ws source devel/setup.bash rosrun my_ros_pkg my_ros_client.py ``` 在客户端终端中输入您的名字,服务端会返回“Hello, 您的名字”。 4. 实验心得 本次实验通过使用ROS编写简单的服务器和客户端通信程序,让我更深入了解了ROS的基本概念和使用方法,掌握了ROS中的节点、话题、服务等概念,并且学会了如何使用ROS编写服务器和客户端通信程序。同时,在实验过程中,我也学会了如何使用ROS的一些常用命令和工具,如catkin_make、roscd、rosrun等,这些命令和工具对于ROS开发非常重要。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值