HAPTIX Matlab and Octave API


Overview

This tutorial will explain how to use Matlab in Windows or Octave in Linux for requesting a description of the hand, sending new joint commands, and receiving state updates. The Matlab and Octave systems have the same API, but the steps to run each system differ slightly.

We assume that you have already done the installation step.

Start the Gazebo simulation

Double-click on the haptixStart desktop icon.

Run your controller in Matlab

Before opening Matlab you should make sure that the environment variable IGN_IP is properly set. Go to this this tutorial, and scroll to the Network Configuration section for detailed instructions.

Open Matlab and click on the Browse for folder icon.


A new pop-up window will appear. Browse to the folder matlab\ inside the directory where you unzipped the HAPTIX client library SDK.

The HAPTIX client library SDK includes one mex file that allows you to call the functions hx_connect(), hx_robot_info(), hx_update(), hx_read_sensors() and hx_close() from your Matlab console or from a .m file.

Open the file hx_matlab_controller.m in Matlab. Then, type in the Matlab Command Window:

hx_matlab_controller

You should see the arm move like in the controller visualization video below.

Run your controller in Octave

First, install Octave if you haven't already:

sudo apt-get install octave liboctave-dev

Then, change directories to the octave subdirectory of the haptix-comm install directory. This directory should contain several .m files.

cd /usr/lib/x86_64-linux-gnu/haptix-comm/octave

If that directory does not exist, try:

cd /usr/lib/haptix-comm/octave

Start Octave:

octave

You should be able to call hx_connect(), hx_robot_info(), hx_update(), hx_read_sensors(), and hx_close() from Octave (the parentheses are optional).

To run a controller for the simulated arm, type:

hx_matlab_controller

You should see the arm move like in the controller visualization video below.

Controller visualization

While your controller is running, you should see your fingers following a smooth trajectory in Gazebo.

The code explained

counter = 0;

hx_connect();

deviceInfo = hx_robot_info();

% Uncomment this block to start logging.
% hxs_start_logging('/tmp/log/')

while counter < 250
  cmdSent = tic;

  % Initialize the command scalar structure.
  cmd.ref_pos = [];
  cmd.ref_vel = [];
  cmd.ref_vel_max = [];
  cmd.gain_pos = [];
  cmd.gain_vel = [];

  % Indicate that the positions we set should be used.
  cmd.ref_pos_enabled = 1;
  % We're not setting it, so indicate that ref_vel should be ignored.
  cmd.ref_vel_enabled = 0;
  % We're not setting it, so indicate that ref_vel_max should be ignored.
  cmd.ref_vel_max_enabled = 0;
  % We're not setting it, so indicate that gain_pos should be ignored.
  cmd.gain_pos_enabled = 0;
  % We're not setting it, so indicate that gain_vel should be ignored.
  cmd.gain_vel_enabled = 0;

  % Create a new command based on a sinusoidal wave.
  for n = 0:deviceInfo.motor_count
    cmd.ref_pos(end + 1) = 350 * 0.5 * sin(0.05 * 2.0 * pi * counter * 0.08);
    % We could set a desired maximum velocity
    % cmd.ref_vel(end + 1) = 1.0;
    % cmd.ref_vel_max(end + 1) = 1.0;
    % We could set a desired controller position gain
    % cmd.gain_pos(end + 1) = 1.0;
    % We could set a desired controller velocity gain
    % cmd.gain_vel(end + 1) = 1.0;
  end

  % Send the new joint command and receive the state update.
  state = hx_update(cmd);

  counter = counter + 1;

  % Busy wait. pause() is not accurate enough on Windows.
  elapsedCmd = toc(cmdSent);
  while elapsedCmd < 0.02
    elapsedCmd = toc(cmdSent);
  end
end

% Uncomment this block to stop logging.
% hxs_stop_logging()

hx_close();

The HAPTIX Matlab/Octave API is composed of five functions: hx_connect(), hx_robot_info(), hx_update(), hx_read_sensors() and hx_close(). hx_connect() and hx_close() are optional for the Gazebo simulator, but are included for compatibility with MuJoCo.

hx_robot_info() requests information from a given device. In this tutorial, our device is a hand simulated in Gazebo. Note that this call blocks until the response is received.

The result value of hx_robot_info() is a struct containing the number of motors, joints, contact sensors, IMUs and joint limits for the requested device. It also contains the update rate, which is the frequency at which the device is updated.

Once we confirm the device information, we can start sending commands for controlling the hand. The function hx_update() is in charge of sending a new command and receiving the current state of the hand.

We need to fill a command struct that contains the positions, velocities, and gains for each joint. It is important to use the same names for the fields that we are using in this example.

We then call the function hx_update(), which returns a struct representing the sensor state of the command after applying the command.