Estimated time to complete this lab: 1 hour Table of Contents Table of Contents
Objectives At the end of this self-learning lab, you should be able to:
PreparationsWe will continue to use the lab5 package used in the previous lab. If you have downloaded the package into your workspace, you can skip the preparation. See 05 L1 :: Preparations for the preparations. Communication between nodes via ROS Services: Basic concepts
Example Server-Client demo
rosrun lab5 adder.py [INFO] [1577085365.200784]: Ready to add two ints. [INFO] [1577085376.393572]: Received [5, 6], returning 11 [INFO] [1577085377.401102]: Received [1, 1], returning 2 [INFO] [1577085378.413771]: Received [4, 4], returning 8 ...(omitted) rosrun lab5 caller.py [INFO] [1577085376.386713]: Generated [5, 6], sending addition request... ExplanationsThe following will become apparent in the code, but we show the basic flow below:
TODO: Insert Diagram Section 1. Server
The code
ExplanationsMost portions of the code should be familiar as they are used for ROS nodes in general, and they appeared in the publisher/subscriber codes shown in previous labs. The new parts highlighted are those which are related to servers. from lab5.srv import * lab5.srv import allows us to use the lab5/AddTwoInts service type. This import also includes the classes lab5/AddTwoIntsRequest and lab5/AddTwoIntsResponse .def callback(req): The
callback function is called whenever a new service request is received. Similar to subscriber callback, the request contents are stored in
resp = AddTwoIntsResponse() Creates an
rospy.Service('calc', AddTwoInts, callback) This declares the node provides a calc service which is of type AddTwoInts . When new service requests are received, callback is invoked with the service request (an
AddTwoIntsRequest object) as the first argument.rospy.spin() Prevent the node from exiting until the node has been shutdown. Without this line, the node will terminate by running to the end of main , and callback will not be invoked even if there are service requests to calc .Running the node
rosservice call /calc "first: 1 second: 2" sum: 3 $ rosrun lab5 adder.py [INFO] [1563187743.875656]: Ready to add two ints. [INFO] [1563188150.886430]: Received [1, 2], returning 3
Section 2. Client
The code
ExplanationsLet's look at the new parts related to service clients. calc_client = rospy.ServiceProxy('calc', AddTwoInts) This creates a service client object in calc_client that issues service requests to the service calc which is of srv type AddTwoInts .req = AddTwoIntsRequest(); req.first = a; req.second = b Creates an AddTwoIntsRequest object in req and assigns values to the variable fields inside AddTwoIntsRequest .resp = calc_client(req) call the service client for the service
rospy.loginfo("Received response: %d" % resp.sum) The fields inside the AddTwoIntsResponse object can also be accessed via the . (dot) operator. Running the node
rosrun lab5 caller.py [INFO] [1563201313.949032]: Generated [1, 3], sending addition request... Traceback (most recent call last): File "/home/m2/catkin_ws/src/m2cs_ros_tutorial/lab5/src/caller.py", line 23, in resp = calc_client(req) ... (omitted) rospy.service.ServiceException: service [/calc] unavailable Bug The error is
due to the service To solve this error:
rosrun lab5 caller.py [INFO] [1563201592.473211]: Generated [3, 3], sending addition request... [INFO] [1563201592.477805]: Received response: 6 [INFO] [1563201593.474373]: Generated [4, 2], sending addition request... [INFO] [1563201593.479907]: Received response: 6 [INFO] [1563201594.474587]: Generated [2, 5], sending addition request... [INFO] [1563201594.480513]: Received response: 7 ... (omitted) rosrun lab5 adder.py [INFO] [1563201590.020832]: Ready to add two ints. [INFO] [1563201592.477067]: Received [3, 3], returning 6 [INFO] [1563201593.478826]: Received [4, 2], returning 6 [INFO] [1563201594.480082]: Received [2, 5], returning 7 ... (omitted)
[INFO] [1563201595.589237]: Generated [2, 5], sending addition request... [INFO] [1563201595.597013]: Received response: 7 Traceback (most recent call last): File "/home/m2/catkin_ws/src/m2cs_ros_tutorial/lab5/src/caller.py", line 23, in resp = calc_client(req) ... (omitted) rospy.service.ServiceException: service [/calc] unavailable
Extra (optional): Difference between communication via topics and services
Checklist and AssignmentCompletion Congratulations! You have successfully run a service server and client and make two nodes communicate with each other! Now, you should have enough knowledge to complete the remaining tasks in Assignment 1 (m2_ps4 manual control).
References
Struktur data Python apa saja?Sekarang kita akan mempelajari struktur data List, Tuple, Set, dan Dictionary.. 1. List. List adalah salah satu struktur data dalam bahasa pemrograman python yang mampu menyimpan kumpulan data (objek/nilai), yang disebut elemen list. Elemen pada list tersimpan menurut urutan (sequence) tertentu. ... . 2. Tuple. ... . 3. Set.. Apa fungsi def di Python?def pada bahasa python adalah suatu cara untuk mendifinisikan sebuah method atau fungsi.
Apa itu list dalam pemrograman?List adalah tipe data yang paling serbaguna yang tersedia dalam bahasa Python, yang dapat ditulis sebagai daftar nilai yang dipisahkan koma (item) antara tanda kurung siku. Hal penting tentang daftar adalah item dalam list tidak boleh sama jenisnya.
Apa yang dimaksud dengan tipe data tuple?Sebuah tupel adalah urutan objek Python yang tidak berubah. Tupel adalah urutan, seperti daftar. Perbedaan utama antara tupel dan daftarnya adalah bahwa tupel tidak dapat diubah tidak seperti List Python. Tupel menggunakan tanda kurung, sedangkan List Python menggunakan tanda kurung siku.
|