/****************************************************************************/
/* Copyright (c) MIT Sea Grant                                              */
/* MIT Sea Grant Proprietary Information. All rights reserved.              */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : SimulatedLbl.h                                                */
/* Author   : Don Eickstedt                                                 */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 07/17/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _LblServer_H
#define _LblServer_H

#include <time.h>
#include "LblIF_SK.h"
#include "TimeIF.h"
#include "LblIF.h"
#include "SimulatorIF.h"


//Resolution of return times in seconds
//returned times will be correct to within +/- RESOLUTION/2
#define RESOLUTION 0.0001

//#define NOISY

#ifdef NOISY
   #define TOF_STD          3.3e-4
   #define TOF_NOT_SO_GOOD  6.6e-3
#else
   #define TOF_STD          0
   #define TOF_NOT_SO_GOOD  0
#endif

#define MAX_BEACONS 7

struct lbl_array
{
  double nb[MAX_BEACONS];
  double eb[MAX_BEACONS];
  double db[MAX_BEACONS];
  double turnaround[MAX_BEACONS];
  double sos;
  int num_beacons;
};

class LblLog
{
 public:

  LblLog::write(char *message);
  LblLog::LblLog();
  LblLog::~LblLog();

  char _fileName[256];
};

class SimLbl : public LblIF_SK {

public:

  SimLbl(const char *name);
  ~SimLbl();

  virtual long get_tof(LblIF::RTimes *times);

  virtual long get_ptime(TimeIF::TimeSpec *ping_time);

  virtual void ptime(TimeIF::TimeSpec *p_time);

  virtual int spawnAuxTasks();

  virtual long ping();

  virtual void times(LblIF::RTimes *rt);

  virtual long change_gate();

protected:

  //check to see if beacon returns have arrived
  void check_for_times();

  //one way tofs from vehicle to each beacon
  double ow_tof[MAX_BEACONS];

  //The time the ping was transmitted in TIMEIF format
  TimeIF::TimeSpec rp_time;

  //The return times for all 10 channels
  LblIF::RTimes    r_times;

  //new ping time available?
  Boolean          _valid_ptime;

  //new LBL data available?
  Boolean          _valid_times;

  LblLog           *_log;

  SimulatorIF      *_simulator;

  //time of PS8000 ping
  double ping_time,last_time;

  //used to map PS8000 channels to individual beacons
  short _beacon_channel[7];

  //structure containing info about current LBL array
  lbl_array lbl;

  double    ping_lockout,range_gate,returns[MAX_BEACONS];

  Boolean   _ping_pending;

  int       num_found,found[MAX_BEACONS];

  SimulatorIF::Vector last_position;


};

double dist3(double x1,double y1,double z1,double x2,double y2,double z2);
double uniform_noise();
double gaussian_noise(double m,double std);

#endif




