## page was copied from ROS/Tutorials/WritingServiceClient(euslisp) ##This tutorial covers how to write a service and client node in python. #################################### ##FILL ME IN #################################### ## links to any required tutorials ## note.0= [[ROS/Tutorials/WritingPublisherSubscriber(Euslisp)|Writing a Simple Publisher and Subscriber (euslisp)]] ## descriptive title for the tutorial ## title = Writing Simple Service and Client (EusLisp) ## multi-line description to be displayed in search ## description = This tutorial covers how to write a service and client node in euslisp. ## the next tutorial description ## next = ## links to next tutorial ## next.0.link= [[rtmros_common/Tutorials/WorkingWithEusLisp| Using EusLisp (roseus) to control rtmtros robots]] ## next.1.link= ## what level user is this tutorial for ## level= BeginnerCategory #################################### <> <> ##startpage === Writing a Service Node === Here we'll create the service ("add_two_ints_server") node which will receive two ints and return the sum. Change directory into the beginner_tutorials package, you created in the earlier tutorial, [[http://wiki.ros.org/ROS/Tutorials/CreatingPackage?buildsystem=catkin|creating a package]]: {{{ $ roscd beginner_tutorials }}} Please make sure you have followed the directions in [[ROS/Tutorials/CreatingMsgAndSr|the previous tutorial for creating the service]] needed in this tutorial, creating the AddTwoInts.srv (be sure to choose the right version of build tool you're using at the top of wiki page in the link). ==== The Code ==== Create the '''euslsp/add-two-ints-server.py''' file within the beginner_tutorials package and paste the following inside it: {{{ #!python block=service #!/usr/bin/env roseus ;;; ;;; euslisp version of ros_tutorials/rospy_tutorials/005_add_two_ints ;;; (ros::load-ros-manifest "roseus") ;;; (defun add-two-ints (req) (let ((m (send req :response))) (format *error-output* "Returning [~d + ~d = ~d]~%" (send req :a) (send req :b) (+ (send req :a) (send req :b))) (send m :sum (+ (send req :a) (send req :b))) m)) ;;; ;;; (ros::roseus "add_two_ints_server") (ros::advertise-service "add_two_ints" roseus::AddTwoInts #'add-two-ints) (do-until-key (ros::spin-once)) }}} Don't forget to make the node executable: {{{ chmod +x euslisp/add-two-ints-server.l }}} ==== The Code Explained ==== Now, let's break the code down. There's very little to writing a service using [[roseus]]. We declare our node using {{{(ros::roseus "add_two_ints_server")}}} and then declare our service: <> This declares a new service named add_two_ints with the AddTwoInts service type. All requests are passed to handle_add_two_ints function. handle_add_two_ints is called with instances of AddTwoIntsRequest and returns instances of AddTwoIntsResponse. Just like with the subscriber example, <> keeps your code from exiting until the service is shutdown. === Writing the Client Node === ==== The Code ==== Create the {{{euslsp/add-two-ints-client.l}}}file within the beginner_tutorials package and paste the following inside it: {{{ #!python block=client #!/usr/bin/env roseus ;;; ;;; euslisp version of ros_tutorials/rospy_tutorials/005_add_two_ints ;;; (ros::load-ros-manifest "roseus") ;;; ;;; (ros::roseus "add_two_ints_client") (ros::wait-for-service "add_two_ints") (dotimes (i 100) (setq req (instance roseus::AddTwoIntsRequest :init)) (send req :a (random 10)) (send req :b (random 20)) (setq before (ros::time-now)) (case (mod i 3) (0 (setq res (ros::service-call "add_two_ints" req t))) (1 (setq res (ros::service-call "add_two_ints" req nil))) (2 (setq res (ros::service-call "add_two_ints" req)))) (setq after (ros::time-now)) (format t "~d + ~d = ~d~ (~A sec)~%" (send req :a) (send req :b) (send res :sum) (send (ros::time- after before) :to-sec)) (unix:sleep 1)) }}} Don't forget to make the node executable: {{{ $ chmod +x euslisp/add-two-ints-client.l }}} ==== The Code Explained ==== Now, let's break the code down. The client code for calling services is also simple. For clients you don't have to call {{{(ros::roseus "add_two_ints_client")}}}. We first call: <> This is a convenience method that blocks until the service named add_two_ints is available. Next we create a handle for calling the service: <> Because we've declared the type of the service to be {{{AddTwoInts}}}, it does the work of generating the {{{AddTwoIntsRequest}}} object for you (you're free to pass in your own instead). <> call {{{(add_two_ints)}}} funciton with argument of {{{req}}}. The return value is an {{{AddTwoIntsResponse}}} object === Building your nodes === We use CMake as our build system and, yes, you have to use it even for EusLisp nodes. This is to make sure that [[ROS/Tutorials/CreatingMsgAndSrv|the autogenerated EusLisp code for messages and services]] is created. {{{{{#!wiki buildsystem catkin Go to your catkin workspace and run `catkin_make`. {{{ # In the catkin workspace $ cd ~/catkin_ws $ catkin_make }}} }}}}} === Try it out! === ##=== 実行してみる! === In a '''new terminal''', run ##新しいターミナルの中で、サーバを実行します。 {{{ $ rosrun beginner_tutorials add_two_ints_server.l }}} In a '''new terminal''', run ##新しいターミナルの中で、以下のように実行します。 {{{ $ rosrun beginner_tutorials add_two_ints_client.l 4 5 }}} And you will get ##すると、このような出力を得ます。 {{{ Requesting 4+5 4 + 5 = 9 }}} And the server will print out ##そしてサーバ側は以下のように出力します。 {{{ Returning [4 + 5 = 9] }}} ##endpage ## ROSTutorialCategory