#ifndef _ECHOSOUNDER_H
#define _ECHOSOUNDER_H

#include "NavSensor.h"
#include "EchoSounderIF.h"
#include "NavigationIF.h"
#include <time.h>

/*
CLASS 
EchoSounder

DESCRIPTION
EchoSounder NavSensor

AUTHOR
Rob McEwen, based on Tom O'Reilly's code.
*/
class EchoSounder : public NavSensor {

public:

  EchoSounder(const char *name, NavSensors *sensors, int maxBad = 3);

  ///////////////////////////////////////////////////////////////////
  // Mounting angle in x-z plane
  double xzMountAngle();

  ///////////////////////////////////////////////////////////////////
  // Maximum range measurable with this sensor
  double maxRange();

  ///////////////////////////////////////////////////////////////////
  // Latest range
  double range();

  ///////////////////////////////////////////////////////////////////
  // True if echo received on latest reading
  Boolean echoReceived();

  double altitude(NavigationIF::Attitude *attitude,
		  NavigationIF::Position *position );

  //
  // Cartesian distance in the Local Level Body (LLB) frame.  This frame has
  // it's x axis aligned with the vehicle centerline, but horizontal.  The z
  // axis is vertical.  
  struct RangeToObstacle
  {
	double horizMin;
	double vertMin;
	double horizLast;
	double vertLast;
	short numberOfPings;
  };

  void obstacleDetect(NavigationIF::Attitude *attitude,
		      NavigationIF::Position *position,
                      RangeToObstacle *_rangeToObstacle);


  Boolean isScanning();

  EchoSounderIF::Data _data;

protected:

  virtual TaskInterface *createTaskIF(int timeout);

  virtual DeviceIF::Status readTaskIF(Boolean *valid, 
				      TimeIF::TimeSpec *sampleTime);

  double _xzMountAngle;

  double _maxRange;
  
  EchoSounderIF *_echoSounderIF;

  double _rangeEng;

  Boolean _echoReceived;

  double _depth, _depthLast, _xLast, _yLast;

  unsigned long _time, _timeLast;
  //TimeIF::TimeSpec _timeLast;

  double _dtrainArray[EchoSounderIF::ScanArraySize];
  double _altitudeArray[EchoSounderIF::ScanArraySize];
  double _hRangeArray[EchoSounderIF::ScanArraySize];

  timespec _localEpoch;
  long _epochOffset;

  short _tindxMinAltitude, _tindxLast, _numberOfPings;

  RangeToObstacle _rangeToObstacle;


};


#endif

