/****************************************************************************/
/* Copyright (c) 2016 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Wave Energy Buoy navigation library for flapping-foil buoy.   */
/* Filename : webnav.h                                                      */
/* Author   : Henthorn                                                      */
/* Project  : Wave Energy Buoy (formerly PowerBuoy)                         */
/* Version  : 1.0                                                           */
/* Created  : 08/22/2016                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */

/*


main()
{
   WebNavData wnd;

   // Initialize mission and GPS comms.
   // Run mission with 5 second GPS updates.
   // 
   if (0 == webnav_init("mission-file", &wnd))
   {
      while (0 == webnav_gps_update(&wnd)) sleep(5);
   }
   webnav_close(WebNavData &wnd);

   // Initialize mission and GPS comms.
   // Run mission with inserted gps positions (interactive test).
   // 
   if (0 <= webnav_init("mission-file", &wnd))
   {
      while (1) {
         // Get lat and long from somewhere
         //
         if (0 != webnav_pos_update(&wnd, lat, lon)) break;
      }
   }
   webnav_close(WebNavData &wnd);

   // Initialize NO mission and GPS comms.
   // Station-keep at current position with 5 second GPS updates.
   // 
   if (0 <= webnav_init(NULL, &wnd))
   {
      while (0 == webnav_gps_update(&wnd)) sleep(5);
   }
   webnav_close(WebNavData &wnd);

}

/****************************************************************************/
#ifndef _WEBNAV_H
#define _WEBNAV_H

#include <gps.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

#define  MAX_WAYPOINTS   20
#define  DEFAULT_RADIUS 100   // meters
#define  DEFAULT_REPEAT   1
#define  REPEAT_FOREVER   0

//****************************************************************************/
// Data structures and types
// 

typedef struct _waypoint
{
   unsigned int radius;
   float  latitude;
   float  longitude;
} Waypoint;

typedef struct _mission
{
   unsigned int nwaypoints;
   Waypoint waypoints[MAX_WAYPOINTS];
   unsigned int repeat; 
} Mission;

typedef struct _webnav_data
{
   Mission           mission;      // The WEB mission (if any)
   unsigned          wp;           // Waypoint counter into the mission
   unsigned          reps;         // Mission repeat setting
   float             f_lat, f_lon; // Official current lat/lon after filtering
   float             distance;     // Distance and bearing to target waypoint
   float             bearing;      // Distance and bearing to target waypoint
   struct gps_data_t gps_data;     // Interfacing to gpsd

   unsigned status;

} WebNavData;

int webnav_init(const char* mission_file, WebNavData *wnd);
int webnav_close(WebNavData *wnd);
int webnav_gps_update(WebNavData *wnd);
int webnav_pos_update(WebNavData *wnd, float lat_degrees, float long_degrees);
int webnav_run(WebNavData *wnd);
int webnav_filter(WebNavData *wnd, float param1, float param2);
int webnav_get_gps(WebNavData *wnd, struct gps_data_t *gps_data);
int webnav_get_position(WebNavData *wnd, float *lat_degrees, float *long_degrees);
int webnav_get_distance(WebNavData *wnd, float *meters);
int webnav_get_bearing(WebNavData *wnd, float *degrees);
int webnav_get_waypoint(WebNavData *wnd, Waypoint *wp);

#endif
