ROS入门记录 [3]

来源:互联网 发布:查看网络延时命令 编辑:程序博客网 时间:2024/06/05 02:13

Tutorial——Beginner Level 1

10. Creating a ROS msg and srv

  • msg : message, package文件夹–> msg文件夹–> “xxxx.msg”文件
  int64 A  int64 B  int64 C
  • srv: service, 请求 + 回应
  int64 A  int64 B  ---  int64 SUM

使用msg

$ roscd beginner_tutorials$ mkdir msg$ echo "int64 num" > msg/Num.msg
# 也能在Num.msg中添加别的:string first_namestring last_nameuint8 ageuint32 score
#在package.xml中添加依赖项<build_depend>message_generation</build_depend><run_depend>message_runtime</run_depend>#在CMakeLists.txt中修改find_package(catkin REQUIRED COMPONENTS   roscpp   rospy   std_msgs   message_generation)catkin_package(  ...  CATKIN_DEPENDS message_runtime ...  ...)add_message_files(  FILES  Num.msg)generate_messages(  DEPENDENCIES  std_msgs)

可以查看msg

$ rosmsg show Numor$ rosmsg show beginner_tutorials/Numint64 num

使用srv

$ roscd beginner_tutorials$ mkdir srv  # 创建srv文件夹
#  roscp [package_name] [file_to_copy_path] [copy_path]  # 从别的package拷贝service过来$ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

同样需要修改package.xml文件,这步之前做过就不用做了

<build_depend>message_generation</build_depend><run_depend>message_runtime</run_depend>

然后,需要修改CMakeLists.txt文件

add_service_files(  FILES  AddTwoInts.srv)

可以查看srv

$ rossrv show AddTwoIntsor$ rossrv show beginner_tutorials/AddTwoIntsint64 aint64 b---int64 sum

commom step for msg and srv

$ roscd beginner_tutorials$ cd ../..$ catkin_make install$ cd -

Review

  • 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
  • catkin_make : makes (compiles) a ROS package
    • rosmake = ros+make : makes (compiles) a ROS package (if you’re not using a catkin workspace)

11. Writing a Simple Publisher and Subscriber (Python)

$ roscd beginner_tutorials$ mkdir scripts$ cd scripts$ wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/talker.py$ chmod +x talker.py

关于talker.py的解释,链接

# talker.pyimport rospyfrom std_msgs.msg import Stringdef talker():    pub = rospy.Publisher('chatter', String, queue_size=10)    rospy.init_node('talker', anonymous=True)    rate = rospy.Rate(10) # 10hz    while not rospy.is_shutdown():        hello_str = "hello world %s" % rospy.get_time()        rospy.loginfo(hello_str)        pub.publish(hello_str)        rate.sleep()if __name__ == '__main__':    try:        talker()    except rospy.ROSInterruptException:        pass

以上创造了一个node,不断的发送 “hello world time()”
还需要创造一个接收的node:

$ roscd beginner_tutorials/scripts/$ wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/listener.py$ chmod +x listener.py

关于listener.py的解释,链接

# listener.pyimport rospyfrom std_msgs.msg import Stringdef callback(data):    rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)def listener():    # In ROS, nodes are uniquely named. If two nodes with the same    # name are launched, the previous one is kicked off. The    # anonymous=True flag means that rospy will choose a unique    # name for our 'listener' node so that multiple listeners can    # run simultaneously.    rospy.init_node('listener', anonymous=True)    rospy.Subscriber('chatter', String, callback)    # spin() simply keeps python from exiting until this node is stopped    rospy.spin()if __name__ == '__main__':    listener()

Building your nodes

$ cd ~/catkin_ws$ catkin_make

13. Examining the Simple Publisher and Subscriber

$ 准备工作$ cd ~/catkin_ws$ source devel/setup.bash  # 环境变量$ roscore                  # 打开master,如果之前已经有,会报error
$ 启动talker$ rosrun beginner_tutorials talker    #如果是C++$ rosrun beginner_tutorials talker.py #如果是python
$ 在另一个terminal启动listener$ rosrun beginner_tutorials listener    #如果是C++$ rosrun beginner_tutorials listener.py #如果是python

可以看到两个窗口的log,talker发出的消息,listener在延迟一点点时间后收到。

# talker[INFO] [WallTime: 1492089752.305381] hello world 1492089752.31
# listener[INFO] [WallTime: 1492089752.306185] /listener_26673_1492089744027I heard hello world 1492089752.31
0 0
原创粉丝点击