Class CaptureData

java.lang.Object
  |
  +--CaptureData
All Implemented Interfaces:
SerialConnectionListener

public class CaptureData
extends java.lang.Object
implements SerialConnectionListener

A class to deal with the raw Bird data that comes out. One of things it does is take the raw data and write it to a file.

NOTE This code assumes that all of the birds in the flock return data in the same format, eg. quaternions. Technically, different birds could return different kinds of data. So, if we want to know the kind of data coming in, we need look only at the first (or any one) bird.

ANOTHER NOTE The SerialConnectionListener interface has a callback that gets called by another thread. So far, none of the variables are synchronized in this application. We are just trusting in timing to make everything work correctly. We will add sychronization when it seems absolutely necessary.

ISSUES: Not sure if the way that we check phase bits is the best way. Maybe it should be that we read in the next byte, we know if the next byte is supposed to have a 1 or 0 phase bit, and we check that it really does.

Why is it that the capture file often doesn't have a complete last line? What is getting dropped and why?

In this version, the data are recorded as floats.


Inner Class Summary
(package private)  class CaptureData.ReadableSensorFile
          An Inner class for writing out the raw sensor data in a human readable format.
(package private)  class CaptureData.SensorFile
          An inner class for writing the raw bird records to a binary file.
 
Field Summary
private  int acount
          The number of valid addresses stored in the address array
private  byte[] address
          An array to hold the addresses corresponding to the data
private  int badBytes
          Keep track of number of bytes with wrong phasing bit
private  int bcount
          A counter to keep track of the bytes in a record
private  Bird[] birds
          Reference to the birds in the flock
private  int bparity
          A counter to keep track of the bytes in a short
private  boolean captureMode
          Indicates whether the data coming in is meant to be saved as a capture file or not.
private  Flock flock
          A reference to the flock that this data comes from
static java.lang.String l_elbow
           
static java.lang.String l_shoulder
           
static java.lang.String l_wrist
           
private  int maxAddrs
          Maximum entries in address array
private  int maxData
          Maximum number of values in data array
private  int npoints
          A count of the number of orientation points are read in.
private  short nsteps
          The number of records reported by each bird
private  int ocount
          Size of valid data in orientation array
private  float[] orientation
          An array to hold the orientation values returned by the sensors.
private  Capture parent
          A reference to the application Capture
private  float[] pointData
          Holds the last point data that came from the birds
private  short rate
          The rate at which the birds measured data
private  byte[] records
          The unprocessed data from the birds
private  int recordSize
          The size of the record of the current data format
private  int tcount
          The size of the array transform.
private  short totalTime
          The total time for this caputure
private  float[] transform
          The array to hold the transformed records
 
Constructor Summary
(package private) CaptureData()
          Default contructor starts out empty
(package private) CaptureData(Capture parent, Flock fl, Bird[] birds)
          Constructor that has all the information it needs to process data.
 
Method Summary
static float[] birdQuatToJava(float[] v)
          Rotates an array of floats from wxyz format to xyzw format.
private  float[] birdReferenceFrameToVRML(float[] q)
          Takes a an array that represents a quaternion in Java order, ie x y z w, and changes its reference frame from the Flock transmitter frame of reference to the avatar's.
static short bytePairToShort(byte msbyte, byte lsbyte)
          Convert a pair of raw bytes which have been right shifted into a single raw short.
private  boolean checkPhaseBit(byte b)
           
 void clearData()
          Destroy all the previous capture data, and make new clean arrays to hold a new capture.
 void dataAvailable(java.io.InputStream str)
          Reads the raw records as they come in from the birds.
static short floatAngleToShort(float angle)
          Converts an angle value in degrees into a short.
static float[] javaQuatToBird(float[] v)
          Rotates an array of floats from xyzw format to wxyz format.
private  int nextAddress(java.io.DataInputStream in)
          Reads the next byte from the input stream and interprets it as an addres.
private  float[] nextAngles(java.io.DataInputStream in)
          Reads the next angles record from the input stream.
private  byte nextByte(java.io.DataInputStream in)
          Reads and validates the next byte to come from the input stream.
private  float[] nextQuaternion(java.io.DataInputStream in)
          Reads the next quaterion from the input stream.
 int numBadBytes()
           
private  void printQuaternion(java.io.DataInputStream in)
          Reads the next record from the stream, then prints it to the message window
private  void readQuaternionIntoArray(java.io.DataInputStream in)
          Read the next 8 (or 9 if in group mode) bytes from the data, interpret as a quaternion, and put it into the orientation array.
static short[] recordsToShorts(byte[] rec, int dataSize)
          Converts the raw bird data from record bytes to integer words; assuming that the bytes coming back have been shifted right.
 void setCaptureMode(boolean mode)
          Set captureMode.
 void setRate(short r)
           
 void setSteps(short s)
           
 void setTotalTime(short time)
           
static float shortAngleToFloat(short val)
          Converts short value to degrees.
static byte shortLSB(short val)
          Extracts the Least Significant Byte of this short
static float shortMatrixToFloat(short val)
           
static byte shortMSB(short val)
          Extracts the Most Significant Byte of this short.
static float shortPosToInches(short val)
           
private static float shortQuaternionToFloat(short val)
          Converts a short quaternion field value to its float equivalent
private  void storeAddress(byte a)
          Adds a value to the address array
private  void storeOrientation(float[] o)
          Adds values to the orientation array.
private  void storeTransform(float[] t, byte a)
          Adds values to the transform and address arrays.
 boolean writeOrientationsToFile(java.lang.String filename)
          Write out the orientations in regular text.
 boolean writeReadableSensorDataToFile(java.lang.String filename)
          A method that writes the transoformation data in human readable format.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

parent

private Capture parent
A reference to the application Capture

flock

private Flock flock
A reference to the flock that this data comes from

birds

private Bird[] birds
Reference to the birds in the flock

records

private byte[] records
The unprocessed data from the birds

recordSize

private int recordSize
The size of the record of the current data format

transform

private float[] transform
The array to hold the transformed records

tcount

private int tcount
The size of the array transform.

orientation

private float[] orientation
An array to hold the orientation values returned by the sensors. Orientations are in the VRML reference frame. They are stored in the order they came in.

ocount

private int ocount
Size of valid data in orientation array

maxData

private final int maxData
Maximum number of values in data array

address

private byte[] address
An array to hold the addresses corresponding to the data

acount

private int acount
The number of valid addresses stored in the address array

captureMode

private boolean captureMode
Indicates whether the data coming in is meant to be saved as a capture file or not.

maxAddrs

private final int maxAddrs
Maximum entries in address array

pointData

private float[] pointData
Holds the last point data that came from the birds

nsteps

private short nsteps
The number of records reported by each bird

totalTime

private short totalTime
The total time for this caputure

rate

private short rate
The rate at which the birds measured data

bcount

private int bcount
A counter to keep track of the bytes in a record

bparity

private int bparity
A counter to keep track of the bytes in a short

badBytes

private int badBytes
Keep track of number of bytes with wrong phasing bit

npoints

private int npoints
A count of the number of orientation points are read in. Mainly a sanity check since it should be proportional the the size of the orientation array

l_shoulder

public static java.lang.String l_shoulder

l_elbow

public static java.lang.String l_elbow

l_wrist

public static java.lang.String l_wrist
Constructor Detail

CaptureData

CaptureData()
Default contructor starts out empty

CaptureData

CaptureData(Capture parent,
            Flock fl,
            Bird[] birds)
Constructor that has all the information it needs to process data.
Parameters:
parent - A reference to the parent application.
fl - The flock.
birds - And array of references to the birds in the flock.
Method Detail

clearData

public void clearData()
Destroy all the previous capture data, and make new clean arrays to hold a new capture. By default, start out in streaming mode.

setTotalTime

public void setTotalTime(short time)

setSteps

public void setSteps(short s)

setRate

public void setRate(short r)

numBadBytes

public int numBadBytes()

setCaptureMode

public void setCaptureMode(boolean mode)
Set captureMode.
See Also:
captureMode

writeReadableSensorDataToFile

public boolean writeReadableSensorDataToFile(java.lang.String filename)
A method that writes the transoformation data in human readable format.
Parameters:
filename - A string which is the name of a file to write to.
Returns:
a boolean that indicates whether or not the file was written successfully.

writeOrientationsToFile

public boolean writeOrientationsToFile(java.lang.String filename)
Write out the orientations in regular text.

printQuaternion

private void printQuaternion(java.io.DataInputStream in)
                      throws java.io.IOException
Reads the next record from the stream, then prints it to the message window

nextByte

private byte nextByte(java.io.DataInputStream in)
               throws java.io.IOException
Reads and validates the next byte to come from the input stream. It checks the phase bit of the byte, and also updates the bcount counter.

nextQuaternion

private float[] nextQuaternion(java.io.DataInputStream in)
                        throws java.io.IOException
Reads the next quaterion from the input stream.
Returns:
an array of four floats representing the quaternion

nextAngles

private float[] nextAngles(java.io.DataInputStream in)
                    throws java.io.IOException
Reads the next angles record from the input stream. The angle order is Z, Y, X.
Parameters:
an - array of three floats representing the angles.

nextAddress

private int nextAddress(java.io.DataInputStream in)
                 throws java.io.IOException
Reads the next byte from the input stream and interprets it as an addres.
Returns:
the address.

readQuaternionIntoArray

private void readQuaternionIntoArray(java.io.DataInputStream in)
                              throws java.io.IOException
Read the next 8 (or 9 if in group mode) bytes from the data, interpret as a quaternion, and put it into the orientation array. If the flock is in group mode, then this will also read the address array. Also puts data into array transform.

birdReferenceFrameToVRML

private float[] birdReferenceFrameToVRML(float[] q)
Takes a an array that represents a quaternion in Java order, ie x y z w, and changes its reference frame from the Flock transmitter frame of reference to the avatar's.

The Flock frame has +X to the front, +Z going down and +Y going to the right.

The VRML hanim avatar has +Z to the front, +Y going up and +X to the left.

We rearrange the axes as follows, where the new axes are the avatar's:

       X' <- -Y
       Z' <- +X
       Y' <- -Z
Parameters:
q - An array of floats in the flock's reference frame.

storeAddress

private void storeAddress(byte a)
Adds a value to the address array

storeTransform

private void storeTransform(float[] t,
                            byte a)
Adds values to the transform and address arrays. This should be the only place where they are touched.

storeOrientation

private void storeOrientation(float[] o)
Adds values to the orientation array. This should be the only place where they are touched.

shortMSB

public static byte shortMSB(short val)
Extracts the Most Significant Byte of this short.
Parameters:
val - The short to be decomposed.
Returns:
the byte value of the MSB/

shortLSB

public static byte shortLSB(short val)
Extracts the Least Significant Byte of this short

bytePairToShort

public static short bytePairToShort(byte msbyte,
                                    byte lsbyte)
Convert a pair of raw bytes which have been right shifted into a single raw short.

recordsToShorts

public static short[] recordsToShorts(byte[] rec,
                                      int dataSize)
Converts the raw bird data from record bytes to integer words; assuming that the bytes coming back have been shifted right.

shortAngleToFloat

public static float shortAngleToFloat(short val)
Converts short value to degrees. The scaling of the angles is a full 180 degrees. That is, +179.99 deg = 7FFF Hex; 0 deg = 0 Hex; -180 deg = 8000 Hex.

See Also:
"ANGLES command in the FLock of Birds Operation Guide"

floatAngleToShort

public static short floatAngleToShort(float angle)
Converts an angle value in degrees into a short.
See Also:
shortAngleToFloat(short)

shortMatrixToFloat

public static float shortMatrixToFloat(short val)

shortQuaternionToFloat

private static float shortQuaternionToFloat(short val)
Converts a short quaternion field value to its float equivalent

shortPosToInches

public static float shortPosToInches(short val)

checkPhaseBit

private boolean checkPhaseBit(byte b)
Returns:
true if the phase bit is good, and false if it is wrong

birdQuatToJava

public static float[] birdQuatToJava(float[] v)
Rotates an array of floats from wxyz format to xyzw format.
Parameters:
v - an array of floats to convert.
Returns:
a rotated array.

javaQuatToBird

public static float[] javaQuatToBird(float[] v)
Rotates an array of floats from xyzw format to wxyz format.
Parameters:
v - an array of floats to convert.
Returns:
the array rotated.

dataAvailable

public void dataAvailable(java.io.InputStream str)
Reads the raw records as they come in from the birds.
Note: We assume that we are in quaternion mode.

Quaternion format:
The quaternion record consists of four words which represent the four parameters of the quaternion: q0 is the scalar parameter; q1-q3 are the vector components. Each word consists of two bytes.

Phasing bits:
The very first bit of the first byte of the record is a 1. The first bit of all other bytes is 0. If there is an address byte appended to the end of the record (because the flock is in GROUP mode) this byte will have no phasing bit.

Specified by:
dataAvailable in interface SerialConnectionListener