Home Examples Using Data from an Input File to Drive Exam Behaviour
AdvancedCoding TipsHand Feedback

Using Data from an Input File to Drive Exam Behaviour

This example demonstrates how to generate a binary file of data before a task is run and use that data to influence how your task behaves. In this example the hand paths from a previous exam are used to generate visuals. It would also be possible to extend this example to use the hand paths of a previous exam to guide the hand in a new exam. This example includes a Sample Task available for download.

The problems that need to be solved here are related to the starting and stopping of data reading as well as how to properly write and read the binary information.

When numbers are stored on a computer they are typically either in integer or floating point format. There is also the concept of byte-order or endianess when storing any type of number. Numbers can be big-endian or little-endian byte order. Without going into great detail on this topic, the default for the Robot Computer is little-endian, which means it is essential to write your data in little-endian format. All programming languages allow you to specify the endianess of the numerical data you write.

Attention: If you plan to use the hand trajectories from a previous exam to drive the position controller in another exam it is essential to properly filter the data before writing it to a binary file. Without filtering the movement will be noisy at a level that will make the motion of the Kinarm jerky.

Note: The Input Files feature requires an accessible hard drive on the Robot Computer. Robot Computers with PN 13107 or higher will work with Input Files.

10.22.1      MATLAB Code for Creating an Input File

The following MATLAB code can be used to read data from a previous exam and use it to generate an Input File that can be used by a Task Program.

Attention: Filtering the position data is ESSENTIAL if you plan to feed it into the position controller.

data = exam_load;
  data = filter_double_pass(data, 'standard', 'fc', 10); 
  pathX = [];
  pathY = [];
  % Pull the hand position from each trial and combine into single vector 
  for n=1:length(data.c3d)
    pathX = [pathX; data.c3d(n).Right_HandX];
    pathY = [pathY; data.c3d(n).Right_HandY];end
  % Store the hand position as X1,Y1, X2 Y2... 
  xy = [pathX pathY];
  out = reshape(xy', length(pathX) * 2, 1); 
  fileID = fopen('path.dat', 'w');
  % The following 'l' specifies to write the data in little-endian format, which is
  % essential for reading the data later. fwrite(fileID, out, 'single', 'l'); fclose(fileID);

10.22.2      Simulink Code for Reading Input Files

The Simulink code for this example is based on a task (available on our website) that takes the hand paths from a previous exam and draws them in the current exam. The contents of the enabled subsystem are shown below. The Input File block is set to read File 1. File 1 will therefore need to be one of the inputs specified in Dexterit-E when creating the Task Protocol. See the Dexterit-E User Guide for how to add an Input File to a Task Protocol. In the example below, 8 bytes of data are read from the file at a time and then converted into 2 singles which are then converted to 2 doubles. The doubles are used to create the X,Y position of the hand.

The subsystem is an enabled subsystem so that the task can control when reading the input file starts and stops. Without the enabled subsystem, as soon as the task start running it will start reading the Input File.

Figure 10.33: Simulink code for the Input File block (Note figure may not match sample exactly.)

Related Tasks

Coding Tips
Persistent Variables
Persistent variables are ones that hold their value between calls to the embedded MATLAB code they are defined in.
Coding Tips
Custom Control of the Trial Protocol Order
Normally, Dexterit-E manages and controls the order in which Trial Protocols (TPs) execute based on what is defined in the block table of the Task Protocol (see the Dexterit-E User Guide for more details).