Class: ROS::ServiceClient

Inherits:
Service
  • Object
show all
Defined in:
lib/ros/service_client.rb

Overview

This is an interface of ROS Service. Node#service returns ServiceClient instance.

Examples:

node = ROS::Node.new('/rosruby/sample_service_client')
if node.wait_for_service('/add_two_ints', 1)
  service = node.service('/add_two_ints', Roscpp_tutorials::TwoInts)
  req = Roscpp_tutorials::TwoInts.request_class.new
  res = Roscpp_tutorials::TwoInts.response_class.new
  req.a = 1
  req.b = 2
  if service.call(req, res)
     p res.sum
   end
 end

Instance Attribute Summary

Attributes inherited from Service

#caller_id, #service_name, #service_type

Instance Method Summary collapse

Constructor Details

#initialize(master_uri, caller_id, service_name, service_type, persistent = false) ⇒ ServiceClient

Returns a new instance of ServiceClient.

Parameters:

  • master_uri (String)

    URI of ROS Master

  • caller_id (String)

    caller id of this node

  • service_name (String)

    name of service

  • service_type (Class)

    class of srv

  • persistent (Boolean) (defaults to: false)

    use persistent connection with server or not.



36
37
38
39
40
# File 'lib/ros/service_client.rb', line 36

def initialize(master_uri, caller_id, service_name, service_type, persistent=false)
  super(caller_id, service_name, service_type)
  @master_uri = master_uri
  @persistent = persistent
end

Instance Method Details

#call(srv_request, srv_response) ⇒ Boolean

call service

Parameters:

  • srv_request (Message)

    srv Request instance

  • srv_response (Message)

    srv Response instance

Returns:

  • (Boolean)

    result of call



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ros/service_client.rb', line 55

def call(srv_request, srv_response)
  if @persistent and @connection
    # not connect
  else
    master = XMLRPC::Client.new2(@master_uri)
    code, message, uri = master.call('lookupService',
                                     @caller_id,
                                     @service_name)
    case code
    when 1
      host, port = get_host_port_from_uri(uri)
      @connection = TCPROS::ServiceClient.new(host, port, @caller_id, @service_name, @service_type, @persistent)
      return @connection.call(srv_request, srv_response)
    when -1
      raise "master error ${message}"
    else
      puts "fail to lookup"
      nil
    end
  end
end

#get_host_port_from_uri(uri) ⇒ Array<String, Integer>

get hostname and port from uri

Parameters:

  • uri (String)

    decompose uri string to host and port

Returns:

  • (Array<String, Integer>)
    host, port


46
47
48
49
# File 'lib/ros/service_client.rb', line 46

def get_host_port_from_uri(uri) #:nodoc:
  uri_data = URI.split(uri)
  [uri_data[2], uri_data[3]]
end

#shutdownObject

shutdown this service client



78
79
80
# File 'lib/ros/service_client.rb', line 78

def shutdown
  @connection.close
end