Service Client - acowley/roshask GitHub Wiki

roshask implements a simple non-persistent ROS service client.

For an example, look at Examples/AddTwoIntsClient. The code below is from the AddTwoIntsClient example.

Before you can call a service, you will need to generate the Haskell types for the service request and response. To do this, first make sure that manifest.xml in your roshask package has a dependency on the ROS package that defines the service. Then run roshask dep to generate the Haskell types. roshask will generate types for all .srv files in the /srv directory in the current roshask package and all ROS dependencies. This is similar to how normal roshask message types are generated. The service types are generated in the same package as the message types (see Building a Package). The names of service types follow the same rules as roshask message types, but requests have "Request" post-fixed, and responses have "Response" post-fixed. The service types can also be used as roshask message types (although doing this is probably not a good idea since it could make your code incompatible with other ROS client libraries).

Once the request and response messages are generated, you can call the service. Here is code from the AddTwoIntsClient example

import qualified Ros.Rospy_tutorials.AddTwoIntsRequest as Req
import qualified Ros.Rospy_tutorials.AddTwoIntsResponse as Res
import Ros.Service (callService)
import Ros.Service.ServiceTypes

type Response a = IO (Either ServiceResponseExcept a)

main :: IO ()
main = do
  response <- callService "/add_two_ints" Req.AddTwoIntsRequest{Req.a=10, Req.b=5} :: Response  Res.AddTwoIntsResponse
  print response

Here, the /add_two_ints service is being called with a request where a=5 and b=5. The service should send us 15.

callService is an IO action that takes the name of the service and a request, and returns either the response or an exception.

In most cases, if there is an exception, you will probably want to just print out the exception message. However, ServiceResponseExcept has several type constructors that could be used to perform different actions depending on the exception. For instance, you could use MasterExcept to wait for the ROS master to come online, and/or ConnectExcept to wait for the server to start while still producing an error or exception for other failure cases. callService may produce a Haskell IOError, although this should be rare since most of the fauilure cases are caught and re-thrown as ServiceResponseExceptions.

⚠️ **GitHub.com Fallback** ⚠️