////////////////////////////////////////////////////////////////////////////////
//
// PURPOSE:  Dvl Driver Class.
// AUTHOR:   Rob McEwen, based on  O'Reilly's methane sensor.
// DATE:     01/3/9
// COMMENTS: 
//
////////////////////////////////////////////////////////////////////////////////
//
#ifndef _DVL_H
#define _DVL_H

#define BAUD_RATE 9600
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY "NONE"

#define NBYTES              2		/* number of bytes                    */
#define MM_TO_METRES        0.001	/* mm to meters conversion            */
#define RAD_TO_DEG          180.0 / PI	/* radians to degrees converion       */
#define EPSILON             0.00001	/* floating point zero                */
#define DOPPLER_LSB	    0		/* least significant byte             */
#define DOPPLER_MSB	    1		/* most significant byte              */
#define NUM_BEAMS           4		/* number of beams                    */
#define BEAM_OK                     0	/* beam status ok                     */
#define BOTTOM_BEAM1_CORRELATION    1	/* bottom-referenced correlation and  */
#define BOTTOM_BEAM1_ECHO_AMPLITUDE 2	/* echo amplitude status              */
#define BOTTOM_BEAM2_CORRELATION    4
#define BOTTOM_BEAM2_ECHO_AMPLITUDE 8
#define BOTTOM_BEAM3_CORRELATION    16
#define BOTTOM_BEAM3_ECHO_AMPLITUDE 32
#define BOTTOM_BEAM4_CORRELATION    64
#define BOTTOM_BEAM4_ECHO_AMPLITUDE 128
#define WATER_BEAM1_CORRELATION     1	/* water-referenced depth and         */
#define WATER_BEAM2_CORRELATION     4	/* correlation status                 */
#define WATER_BEAM3_CORRELATION     8
#define WATER_BEAM4_CORRELATION     16 
#define ALTITUDE_TOO_SHALLOW        32
					/* bad velocity value returned        */
#define BAD_VELOCITY                -32768

                                        /* dvlDataStatus & SerialStatus flags */
#define DVL_DATA_GOOD               0   /* Good data received from DVL        */
#define BAD_BOTTOM_TRACK_VELOCITY   1	/* bad bottom track velocity          */
#define BAD_WATER_MASS_VELOCITY     2	/* bad water track velocity           */
#define NO_DVL_RESPONSE             4	/* No response from DVL serial line   */
#define BAD_SERIAL_READ             8	/* Problem reading serial line        */
#define CHECKSUM_WRONG              16	/* Incorrect checksum from serial     */

#define CM_TO_METRES        0.01	/* cm to meters conversion            */
#define cos30 0.86602540378444

enum beamIndx {BM1, BM2, BM3, BM4};     /* for indexing the 4 beams in rawRange*/
enum timeIndx {HRS, MIN, SEC, CENTISEC};/* For indexing the time bytes        */
					/* cartesian degrees of freedom       */
typedef enum { X_INDEX, Y_INDEX, Z_INDEX, E_INDEX } cartDof;

class Dvl {

public:

  ////////////////////////////////////////////////////////////////////
  // Constructor
  // [input] serialDevice: Associated SerialDevice
  Dvl();
  virtual ~Dvl();

protected:

  //
  // Convert the raw readings from the 4 beams into distance in Engineering 
  // units.
  //
  B cvtRange(long rawRange[NUM_BEAMS][NBYTES], double *dvlAltitude );
  //
  // Convert the raw velocity reading into distance in Engineering units.
  //
  Boolean mmToMetres(long xVelocity[NBYTES], long yVelocity[NBYTES], 
		     long zVelocity[NBYTES], long eVelocity[NBYTES], 
		     double velocity[NXE]);

  DvlLog *_dvlLog;
  Boolean m_verbose;
  int m_timeout;
  DvlOutput *m_output;
  int m_sos;

  unsigned char  bottomTrackVelocityStatus;	/* velocity status            */
  unsigned char  waterMassVelocityStatus;
  long xBottomTrackVelocity[NBYTES];		/* velocity                 */
  long yBottomTrackVelocity[NBYTES];
  long zBottomTrackVelocity[NBYTES];
  long eBottomTrackVelocity[NBYTES];
  long xWaterMassVelocity[NBYTES];
  long yWaterMassVelocity[NBYTES];
  long zWaterMassVelocity[NBYTES];
  long eWaterMassVelocity[NBYTES];
  long hour;
  long min;
  long sec;
  long centisec;
  long rawRange[NUM_BEAMS][NBYTES];             /* raw DVL range in centimetrs*/
  long rawTemp[NBYTES];                         /* temperature, .01 Deg C/bit */
  long rawPitch[NBYTES];                        /* Pitch tilt .01 Deg/count   */
  long rawRoll[NBYTES];                         /* Roll  tilt .01 Deg/count   */
  long rawHeading[NBYTES];                      /* Magnetic Heading, .01 Deg/c*/
  short dof;	                                /* dof counter                */
  Boolean status;                               /* General function call status*/
  //
  // The following pointers must be initialized to point to the "data" structure
  // defined in DvlOutput.h (which comes from DvlIF.idl).
  //
#define USE_POINTERS 0
#if USE_POINTERS
  long   *waterStatus;
  long   *bottomStatus;
  double *bottomTrackVelocity;                  /* 4 element vector           */
  double *waterMassVelocity;                    /* 4 element vector           */
  double *dvlPingTime;                 
  double *dvlRange;                    
  double *dvlTemp;
  double *dvlPitch;
  double *dvlRoll;
  double *dvlHeading;
#else
  long waterStatus[1];                         /* Init this to the output array*/
  long bottomStatus[1];                        /* Init this to the output array*/
  double bottomTrackVelocity[NXE];             /* 4 element vector           */
  double waterMassVelocity[NXE];               /* 4 element vector           */
  double dvlPingTime[1];                 
  double dvlRange[1];                    
  double dvlTemp[1];
  double dvlPitch[1];
  double dvlRoll[1];
  double dvlHeading[1];
#endif

  unsigned char dvlDataStatus[1];
  unsigned char dvlSerialStatus;                /* Status of DVL serial comms */
  //
  // m_badComms is a long to be consistant with the idl types.  It need only
  // be a bool.
  //
  long m_badComms;

};

#endif
