Module: UREST

Defined in:
lib/urest.rb

Defined Under Namespace

Classes: ForkProg, GetProg, GetValue, GetValues, SetInt, WaitProg

Constant Summary collapse

SERVER =
File.expand_path(File.join(__dir__,'urest.xml'))

Class Method Summary collapse

Class Method Details

.download_program(opts, name) ⇒ Object

}}}



99
100
101
102
103
104
105
106
107
108
# File 'lib/urest.rb', line 99

def self::download_program(opts,name) #{{{
  counter = 0
  begin
    opts['ssh'].scp.download! name
  rescue => e
    counter += 1
    UREST::ssh_start opts
    retry if counter < 3
  end
end

.get_robot_programs(opts) ⇒ Object

}}}



122
123
124
125
126
127
128
129
130
131
# File 'lib/urest.rb', line 122

def self::get_robot_programs(opts) #{{{
  progs = []
  begin
    progs = opts['ssh'].exec!('ls ' + File.join(opts['dir'],'*.urp') + ' 2>/dev/null').split("\n")
    progs.shift if progs[0] =~ /^bash:/
  rescue => e
    UREST::ssh_start opts
  end
  progs
end

.implementation(opts) ⇒ Object

}}}



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/urest.rb', line 221

def self::implementation(opts)
  Proc.new do
    startup do #{{{ # Init RTDE and DASH
      opts['sshport'] ||= 22
      opts['frequency'] ||= 0.5

      opts['rtde_config'] ||= File.join(__dir__,'rtde.conf.xml')
      opts['rtde_config_recipe_base'] ||= 'out'
      opts['rtde_config_recipe_speed'] ||= 'speed'
      opts['rtde_config_recipe_inbit'] ||= 'inbit'
      opts['rtde_config_recipe_inint'] ||= 'inint'
      opts['rtde_config_recipe_indoub'] ||= 'indoub'

      opts['dash'] = nil
      opts['rtde'] = nil
      opts['programs'] = nil
      opts['psi'] = nil

      ### Connecting to universal robot
      UREST::start_rtde opts
      UREST::start_dash opts
      UREST::start_psi opts

      ### check if interfaces are ok
      raise if !opts['dash'] || !opts['rtde'] || !opts['psi']

      # Functionality for threading in loop
      opts['doit_state'] = Time.now.to_i
      opts['doit_progs'] = Time.now.to_i
      opts['doit_rtde'] = Time.now.to_i

      # Serious comment (we do the obvious stuff)
      opts['speed'] = {}
      opts['sn'] = opts['dash'].get_serial_number
      opts['model'] = opts['dash'].get_robot_model

      ### Manifest programs
      opts['semaphore'] = Mutex.new
      opts['progs'] = []
    rescue Errno::ECONNREFUSED => e
      print 'ECONNREFUSED: '
      puts e.message
    rescue UR::Dash::Reconnect => e
      UREST::start_dash opts
      puts e.message
      puts e.backtrace
    rescue UR::Psi::Reconnect => e
      UREST::start_psi opts
      puts e.message
      puts e.backtrace
    rescue => e
      puts e.message
      puts e.backtrace
      raise
    end # }}}

    parallel do #{{{ periodic reading of data from UR
      EM.add_periodic_timer(opts[:frequency]) do
        if Time.now.to_i - 1 > opts['doit_state']
          opts['doit_state'] = Time.now.to_i
          opts['cp'] = opts['dash'].get_loaded_program
          opts['rs'] = opts['dash'].get_program_state.split(' ')[0]
          # update remote control state from dashboard server
          opts['mo'] = opts['dash'].is_in_remote_control
          opts['op'] = opts['dash'].get_operational_mode
        end

        if Time.now.to_i - 10 > opts['doit_progs']
          opts['doit_progs'] = Time.now.to_i
          Thread.new do
            opts['semaphore'].synchronize do
              # Content of thread
              # check every 10 seconds for new programs
              opts['progs'] = UREST::get_robot_programs(opts)
            end unless opts['semaphore'].locked?
          end
        end

        data = opts['rtde'].receive
        if data
          opts['ss'] = data['speed_scaling']

          # State objects
          opts['rm'] = UR::Rtde::ROBOTMODE[data['robot_mode']]
          opts['sm'] = UR::Rtde::SAFETYMODE[data['safety_mode']]
          opts['ps'] = UR::Rtde::PROGRAMSTATE[data['runtime_state']]

          #speed slider or override
          if opts['ov'] != (data['target_speed_fraction'] * 100).to_i
            opts['ov'] = (data['target_speed_fraction'] * 100).to_i
          end
        else
          if Time.now.to_i - 10 > opts['doit_rtde']
            opts['doit_rtde'] = Time.now.to_i
            UREST::start_rtde opts
          end
        end
      rescue Errno::ECONNREFUSED => e
        print 'ECONNREFUSED: '
        puts e.message
      rescue UR::Dash::Reconnect => e
        UREST::start_dash opts
        puts e.message
        puts e.backtrace
      rescue UR::Psi::Reconnect => e
        UREST::start_psi opts
        puts e.message
        puts e.backtrace
      rescue => e
        puts e.message
        puts e.backtrace
        raise
      end
    end #}}}

    on resource do
      run GetValues, [ 'messages', 'registers', 'model', 'serialnumber', 'state', 'programs'] if get
      on resource 'model' do #{{{
        run GetValue, opts['model'] if get
      end #}}}
      on resource 'serialnumber' do #{{{
        run GetValue, opts['sn'] if get
      end #}}}
      on resource 'state' do #{{{
        run GetValues, {
          :mode => opts['rm'].downcase,
          :power => opts['rm'] == 'Running' ? 'on' : 'off',
          :remote => opts['mo'] || 'false',
          :program => File.basename(opts['cp'] || ''),
          :program_state => (opts['rs'] || 'stopped').downcase,
          :safety_mode => opts['sm'].downcase,
          :speed => opts['ov'],
          :speed_scaling => opts['ss']
        } if get
        on resource 'mode' do
          run GetValue, opts['rm'].downcase if get
        end
        on resource 'power' do
          run GetValue, opts['rm'] == 'Running' ? 'on' : 'off' if get
        end
        on resource 'remote' do
          run GetValue, opts['mo'] || 'false' if get
        end
        on resource 'program' do
          run GetValue, File.basename(opts['cp'] || '') if get
        end
        on resource 'program_state' do
          run GetValue, (opts['rs'] || 'stopped').downcase if get
        end
        on resource 'safety_mode' do
          run GetValue, opts['sm'].downcase if get
        end
        on resource 'speed' do
          run GetValue, opts['ov'] if get
        end
        on resource 'speed_scaling' do
          run GetValue, opts['ss'] if get
        end
      end #}}}
      on resource 'programs' do
        run GetValues, opts['progs'].map{|f|File.basename(f,'.urp')} if get
        on resource do
          run GetProg, opts if get
          on resource 'fork' do
            run ForkProg, opts if put
          end
          on resource 'wait' do
            run WaitProg, opts if put
          end
        end
      end
      on resource 'registers' do
        on resource 'input' do
          on resource 'int' do
            on resource '\d+' do
              run SetInt, opts if put 'tint'
            end
          end
        end
      end
    end
  end
end

.protect_reconnect_run(opts) ⇒ Object

}}}



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/urest.rb', line 70

def self::protect_reconnect_run(opts) #{{{
  tries = 0
  begin
    yield
  rescue UR::Dash::Reconnect => e
    puts e.message
    tries += 1
    if tries < 2
      UREST::start_dash opts
      retry
    end
  rescue UR::Psi::Reconnect => e
    puts e.message
    tries += 1
    if tries < 2
      UREST::start_psi opts
      retry
    end
  end
end

.robotprogram_running?(opts) ⇒ Boolean

}}}

Returns:

  • (Boolean)


133
134
135
# File 'lib/urest.rb', line 133

def self::robotprogram_running?(opts) #{{{
  opts['ps']== 'Playing'
end

.ssh_start(opts) ⇒ Object

}}}



91
92
93
94
95
96
97
# File 'lib/urest.rb', line 91

def self::ssh_start(opts) #{{{
  if opts['certificate']
    opts['ssh'] = Net::SSH.start(opts['ipadress'], opts['username'], :port => opts['sshport'], :keys => [ opts['certificate'] ])
  else
    opts['ssh'] = opts['password'] ? Net::SSH.start(opts['ipadress'], opts['username'], :port => opts['sshport'], auth_methods: ['password'], password: opts['password']) : Net::SSH.start(opts['ipadress'], opts['username'], :port => opts['sshport'])
  end
end

.start_dash(opts) ⇒ Object

{{{



15
16
17
# File 'lib/urest.rb', line 15

def self::start_dash(opts) #{{{
  opts['dash'] = UR::Dash.new(opts['ipadress']).connect rescue nil
end

.start_program(opts, fname) ⇒ Object

}}}



137
138
139
140
141
142
143
144
# File 'lib/urest.rb', line 137

def self::start_program(opts,fname) #{{{
  unless UREST::robotprogram_running?(opts)
    UREST::protect_reconnect_run(opts) do
      opts['dash'].load_program(fname)
      opts['dash'].start_program
    end
  end
end

.start_psi(opts) ⇒ Object

}}}



19
20
21
# File 'lib/urest.rb', line 19

def self::start_psi(opts) #{{{
  opts['psi'] = UR::Psi.new(opts['ipadress']).connect rescue nil
end

.start_rtde(opts) ⇒ Object

}}}



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/urest.rb', line 23

def self::start_rtde(opts) #{{{
  ### Loading config file
  conf = UR::XMLConfigFile.new opts['rtde_config']
  output_names, output_types = conf.get_recipe opts['rtde_config_recipe_base']
  opts['rtde'] = UR::Rtde.new(opts['ipadress']).connect

  ### Set Speed
  if opts['rtde_config_recipe_speed']
    speed_names, speed_types = conf.get_recipe opts['rtde_config_recipe_speed']
    opts['speed'] = opts['rtde'].send_input_setup(speed_names, speed_types)
    opts['speed']['speed_slider_mask'] = 1
  end

  ### Set register
  if opts['rtde_config_recipe_inbit']
    bit_names, bit_types = conf.get_recipe opts['rtde_config_recipe_inbit']
    opts['inbit'] = opts['rtde'].send_input_setup(bit_names,bit_types)
  end
  if opts['rtde_config_recipe_inint']
    int_names, int_types = conf.get_recipe opts['rtde_config_recipe_inint']
    opts['inint'] = opts['rtde'].send_input_setup(int_names,int_types)
  end
  if opts['rtde_config_recipe_indoub']
    doub_names, doub_types = conf.get_recipe opts['rtde_config_recipe_indoub']
    opts['indoub'] = opts['rtde'].send_input_setup(doub_names,doub_types)
  end

  ### Setup output
  if not opts['rtde'].send_output_setup(output_names, output_types,10)
    puts 'Unable to configure output'
  end
  if not opts['rtde'].send_start
    puts 'Unable to start synchronization'
  end

  ###Initialize all inputs
  bit_names.each do |i|
    opts['inbit'][i] = false
  end
  int_names.each do |i|
    opts['inint'][i] = 0
  end
  doub_names.each do |i|
    opts['indoub'][i] = 0.0
  end
end

.upload_program(opts, name, program) ⇒ Object

}}}



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/urest.rb', line 110

def self::upload_program(opts,name,program) #{{{
  counter = 0
  begin
    opts['ssh'].scp.upload StringIO.new(program), File.join(opts['dir'],name)
  rescue => e
    counter += 1
    UREST::ssh_start opts
    retry if counter < 3
  end
  nil
end