Class: ROS::Node

Inherits:
Object
  • Object
show all
Includes:
Name
Defined in:
lib/ros/node.rb

Overview

main interface of rosruby.

Examples:

Sample for Publisher

node = ROS::Node.new('/rosruby/sample_publisher')
publisher = node.advertise('/chatter', Std_msgs::String)
sleep(1)
msg = Std_msgs::String.new
i = 0
while node.ok?
  msg.data = "Hello, rosruby!: #{i}"
  publisher.publish(msg)

Sample for Subscriber

node = ROS::Node.new('/rosruby/sample_subscriber')
node.subscribe('/chatter', Std_msgs::String) do |msg|
  puts "message come! = \'#{msg.data}\'"
end

while node.ok?
  node.spin_once
  sleep(1)
end

Constant Summary collapse

@@shutdown_hook =

for node shutdown hook

[]

Constants included from Name

ROS::Name::SEP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Name

#anonymous_name, #canonicalize_name, #expand_local_name, #resolve_name_with_call_id

Constructor Details

#initialize(node_name, options = {}) ⇒ Node

initialization of ROS node get env, parse args, and start slave xmlrpc servers.

Parameters:

  • node_name (String)

    name of this node

  • options (Hash) (defaults to: {})

    options

Options Hash (options):

  • :anonymous (Boolean) — default: false

    use anonymous name if true. anonymous node generates a unique name



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ros/node.rb', line 64

def initialize(node_name, options={})
  @remappings = {}
  # @host is rewrited by ARGS[ROS_IP] or ARGS[ROS_HOSTNAME]
  @host = Socket.gethostname
  get_env
  if options[:anonymous]
    node_name = anonymous_name(node_name)
  end
  @node_name = resolve_name(node_name)
  @remappings = parse_args(ARGV)
  if not @master_uri
    raise 'ROS_MASTER_URI is nos set. please check environment variables'
  end

  @manager = GraphManager.new(@node_name, @master_uri, @host)
  @parameter = ParameterManager.new(@master_uri, @node_name, @remappings)
  if not options[:nologger]
    @logger = ::ROS::Log.new(self)
  end
  # because xmlrpc server use signal trap, after serve, it have to trap sig      trap_signals
  ObjectSpace.define_finalizer(self, proc {|id| self.shutdown})
end

Instance Attribute Details

#hostString (readonly)

hostname of this node.

Returns:

  • (String)

    host name



102
103
104
# File 'lib/ros/node.rb', line 102

def host
  @host
end

#master_uriString (readonly)

URI of master

Returns:

  • (String)

    uri string of master



98
99
100
# File 'lib/ros/node.rb', line 98

def master_uri
  @master_uri
end

#node_nameString (readonly)

name of this node (caller_id).

Returns:

  • (String)

    name of this node (=caller_id)



106
107
108
# File 'lib/ros/node.rb', line 106

def node_name
  @node_name
end

Instance Method Details

start publishing the topic.

Parameters:

  • topic_name (String)

    name of topic (string)

  • topic_type (Class)

    topic class

  • options (Hash) (defaults to: {})

    :latched, :resolve

Options Hash (options):

  • :latched (Boolean) — default: false

    latched topic

  • :resolve (Boolean) — default: true

    resolve topic_name or not. This is for publish /rosout with namespaced node.

Returns:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ros/node.rb', line 178

def advertise(topic_name, topic_type, options={})
  if options[:no_resolve]
    name = topic_name
  else
    name = resolve_name(topic_name)
  end
  publisher = Publisher.new(@node_name,
                            name,
                            topic_type,
                            options[:latched],
                            @manager.host)
  @manager.add_publisher(publisher)
  trap_signals
  publisher
end

start service server.

Parameters:

  • service_name (String)

    name of this service (string)

  • service_type (Service)

    service class

  • callback (Proc)

    service definition

Returns:



201
202
203
204
205
206
207
208
209
# File 'lib/ros/node.rb', line 201

def advertise_service(service_name, service_type, &callback)
  server = ::ROS::ServiceServer.new(@node_name,
                                    resolve_name(service_name),
                                    service_type,
                                    callback)
  @manager.add_service_server(server)
  trap_signals
  server
end

#delete_param(key) ⇒ Boolean

delete the parameter for ‘key’

Parameters:

  • key (String)

    key for delete

Returns:

  • (Boolean)

    true if success, false if it is not exist



156
157
158
# File 'lib/ros/node.rb', line 156

def delete_param(key)
  @parameter.delete_param(expand_local_name(@node_name, key))
end

#get_param(key, default = nil) ⇒ String, ...

get the param for key. You can set default value. That is uesed when the key is not set yet.

Parameters:

  • key (String)

    key for search the parameters

  • default (String, Integer, Float, Boolean) (defaults to: nil)

    default value

Returns:

  • (String, Integer, Float, Boolean)

    parameter value for key



124
125
126
127
128
129
130
131
132
# File 'lib/ros/node.rb', line 124

def get_param(key, default=nil)
  key = expand_local_name(@node_name, key)
  param = @parameter.get_param(key)
  if param
    param
  else
    default
  end
end

#get_param_namesArray

get all parameters.

Returns:

  • (Array)

    all parameter list



139
140
141
# File 'lib/ros/node.rb', line 139

def get_param_names
  @parameter.get_param_names
end

#get_published_topicsArray

get all topics by this node.

Returns:

  • (Array)

    topic names



351
352
353
354
355
# File 'lib/ros/node.rb', line 351

def get_published_topics
  @manager.publishers.map do |pub|
    pub.topic_name
  end
end

#has_param(key) ⇒ Boolean

check if the parameter server has the param for ‘key’.

Parameters:

  • key (String)

    key for check

Returns:

  • (Boolean)

    true if exits



147
148
149
# File 'lib/ros/node.rb', line 147

def has_param(key)
  @parameter.has_param(expand_local_name(@node_name, key))
end

#logdebug(message) ⇒ Node

outputs log message for DEBUG

Parameters:

  • message (String)

    message for output

Returns:



306
307
308
309
310
# File 'lib/ros/node.rb', line 306

def logdebug(message)
  file, line, function = caller[0].split(':')
  @logger.log('DEBUG', message, file, function, line.to_i)
  self
end

#logerror(message) ⇒ Node Also known as: logerr

outputs log message for ERROR.

Parameters:

  • message (String)

    message for output

Returns:



328
329
330
331
332
# File 'lib/ros/node.rb', line 328

def logerror(message)
  file, line, function = caller[0].split(':')
  @logger.log('ERROR', message, file, function, line.to_i)
  self
end

#logfatal(message) ⇒ Node

outputs log message for FATAL.

Parameters:

  • message (String)

    message for output

Returns:



341
342
343
344
345
# File 'lib/ros/node.rb', line 341

def logfatal(message)
  file, line, function = caller[0].split(':')
  @logger.log('FATAL', message, file, function, line.to_i)
  self
end

#loginfo(message) ⇒ Node

outputs log message for INFO (INFORMATION).

Parameters:

  • message (String)

    message for output

Returns:



296
297
298
299
300
# File 'lib/ros/node.rb', line 296

def loginfo(message)
  file, line, function = caller[0].split(':')
  @logger.log('INFO', message, file, function, line.to_i)
  self
end

#logwarn(message) ⇒ Node

outputs log message for WARN (WARING).

Parameters:

  • message (String)

    message for output

Returns:



317
318
319
320
321
# File 'lib/ros/node.rb', line 317

def logwarn(message)
  file, line, function = caller[0].split(':')
  @logger.log('WARN', message, file, function, line.to_i)
  self
end

#ok?Boolean

Is this node running? Please use for ‘while loop’ and so on..

Returns:

  • (Boolean)

    true if node is running.



92
93
94
# File 'lib/ros/node.rb', line 92

def ok?
  @manager.is_ok?
end

#resolve_name(name) ⇒ String

resolve the name by this node’s remapping rule.

Parameters:

  • name (String)

    name for resolved

Returns:

  • (String)

    resolved name



113
114
115
# File 'lib/ros/node.rb', line 113

def resolve_name(name)
  resolve_name_with_call_id(@node_name, @ns, name, @remappings)
end

#service(service_name, service_type) ⇒ ServiceClient

create service client.

Parameters:

  • service_name (String)

    name of this service (string)

  • service_type (Class)

    service class

Returns:



225
226
227
228
229
230
# File 'lib/ros/node.rb', line 225

def service(service_name, service_type)
  ROS::ServiceClient.new(@master_uri,
                         @node_name,
                         resolve_name(service_name),
                         service_type)
end

#set_param(key, value) ⇒ Boolean

set parameter for ‘key’.

Parameters:

  • key (String)

    key of parameter

  • value (String, Integer, Float, Boolean)

    value of parameter

Returns:

  • (Boolean)

    return true if succeed



165
166
167
# File 'lib/ros/node.rb', line 165

def set_param(key, value)
  @parameter.set_param(expand_local_name(@node_name, key), value)
end

#shutdownNode

unregister to master and shutdown all connections.

Returns:



280
281
282
283
284
285
286
287
288
289
290
# File 'lib/ros/node.rb', line 280

def shutdown
  if ok?
    begin
      @manager.shutdown
    rescue => message
      p message
      puts 'ignoring errors while shutdown'
    end
  end
  self
end

#spinObject

spin forever.



270
271
272
273
274
275
# File 'lib/ros/node.rb', line 270

def spin
  while ok?
    spin_once
    sleep(0.01)
  end
end

#spin_onceObject

spin once. This invoke subscription/service_server callbacks



263
264
265
# File 'lib/ros/node.rb', line 263

def spin_once
  @manager.spin_once
end

#subscribe(topic_name, topic_type, &callback) ⇒ Subscriber

start to subscribe a topic.

Parameters:

  • topic_name (String)

    name of topic (string)

  • topic_type (Class)

    Topic instance

Returns:



238
239
240
241
242
243
244
245
246
# File 'lib/ros/node.rb', line 238

def subscribe(topic_name, topic_type, &callback)
  sub = Subscriber.new(@node_name,
                       resolve_name(topic_name),
                       topic_type,
                       callback)
  @manager.add_subscriber(sub)
  trap_signals
  sub
end

#subscribe_parameter(param, &callback) ⇒ ParameterSubscriber

subscribe to the parameter.

Parameters:

  • param (String)

    name of parameter to subscribe

  • callback (Proc)

    callback when parameter updated

Returns:



254
255
256
257
258
# File 'lib/ros/node.rb', line 254

def subscribe_parameter(param, &callback)
  sub = ParameterSubscriber.new(param, callback)
  @manager.add_parameter_subscriber(sub)
  sub
end

#wait_for_service(service_name, timeout_sec = nil) ⇒ Boolean

wait until start the service.

Parameters:

  • service_name (String)

    name of service for waiting

  • timeout_sec (Float) (defaults to: nil)

    time out seconds. default infinity.

Returns:

  • (Boolean)

    true if success, false if timeouted



216
217
218
# File 'lib/ros/node.rb', line 216

def wait_for_service(service_name, timeout_sec=nil)
  @manager.wait_for_service(service_name, timeout_sec)
end