Rock

the Robot Construction Kit

Control through a Virtual Joystick

Tutorial Info

This tutorial will give you some hands-on experience on:

  • How to use a graphical widget to control the rolling rock.

TODO Rewrite the tutorial using the task inspector

Finding the right widget

TODO Add as soon as it is integrated into the package directory

Integrating it

Now that we found the widget we want to use, we need to integrate it. Again, we therefore copy the runscript from the ‘Simulate a Robot’ tutorial to a file called rockTutorial2.rb and modify it as follows.

We start by adding Vizkit to the section with the required packages:

require 'vizkit'

The Orocos.run block should be updated as follows:

## load and add the 3d plugin for the rock
Orocos.run 'rock_tutorial::RockTutorialControl' => 'rock_control' do
  
  rockControl = Orocos.name_service.get 'rock_control'  
  rockControl.start

  ## Create a sample writer for a port ##
  sampleWriter = rockControl.motion_command.writer
  
  # get a sample that can be written to the port
  # If you know the sample type (here, base::MotionCommand2D),
  # an alternative syntax is
  #   sample = Types::Base::MotionCommand2D.new
  sample = sampleWriter.new_sample

  # create a widget that emulates a joystick
  joystickGui = Vizkit.default_loader.create_widget('VirtualJoystick')

  #show it
  joystickGui.show
  
  ## glue the widget to the task writer
  joystickGui.connect(SIGNAL('axisChanged(double, double)')) do |x, y|
    sample.translation = x
    sample.rotation = - y.abs() * Math::atan2(y, x.abs())
    sampleWriter.write(sample)
  end

  Vizkit.display rockControl.pose, 
  :widget => Vizkit.default_loader.RigidBodyStateVisualization

  Vizkit.exec

end

The joystickGui part will open the desired widget and give us a handle to it. Using the handle, we can register a code block in the run-loop that gets executed whenever the widget emits a QT-Signal. Since this code needs a handle to the rockControl task, we need the the Orocos.name_service.get line that initializes rockControl.

Whenever the signal ‘axisChanged’ is emitted by the widget, the code inside the ‘do / end’ is executed. One can think of it like a callback function. We use this ‘callback’ to modify a MotionCommand2D in respect to the axis values and write it to our rockControl task. Thus, we provide the ‘glue’-code between the widget and the Task and also show the power of the scripting interface.

Run it

If you execute

ruby rockTutorial2.rb

two windows should pop up. One with our virtual joystick, the other one should be the well known visualization of the rock. Clicking and dragging the joystick should also make the rock move.