/* **********************************************************************
 * *	                                                                *
 * *               Antenna motion example Utility                       * 
 * *                                                                    *
 * ********************************************************************** 
 * File: utils/examples/ant_example.C
 *
 *            COPYRIGHT (c) 1995, 1997, 1998, 1999, 2003 BY
 *         SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A. 
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED. 
 */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "sigtypes.h"
#include "user_lib.h"
#include "antenna_lib.h"

int main( int argc_a, char **argv_a )
{
  MESSAGE istatus;
  UINT2 iazimuth, ielevation;
  SINT2 iaz_error, iel_error;
  UINT4 itime;
  FLT4 fangle;

  /* Protect against the wrong people running it.
   */
  if ( !liris_operator(sig_username()))
    {
      printf("Sorry, you must be an operator to run ANT_EXAMPLE.\n");
      exit(EXIT_FAILURE);
    }

  /* Power up the antenna driver
   */
  istatus = AntPowerUpMain( ANT_IAM_CLIENT, 0 ) ;

  if ( istatus != SS_NORMAL )
    {
      sig_signal( istatus );
      exit(EXIT_FAILURE);
    }

  /* This is an example program to show how a user might write a
   * program to control the antenna.  In this example, the antenna is
   * first positioned at az=0, el=5.  It waits until it gets within
   * 1/2 degree of there.
   *
   * Then it runs for 60 seconds, issuing a new position command every
   * 1/10 of a second.  It sets the azimuth to 180 * (time/60).  It
   * sets elevation to 5.0 + 50 * sin( pi * time/60 )
   */
  printf( "Setting Initial position.\n");
  fangle = 0.0;
  iazimuth = iBin2FromFDeg( fangle );
  ant_set_az_pos( iazimuth );
  fangle = 5.0;
  ielevation = iBin2FromFDeg( fangle );
  ant_set_el_pos( ielevation );

  ant_get_azel_err( (BIN2 *)&iaz_error, (BIN2 *)&iel_error );
  /* Wait until we are within 1 degree in azimuth, and 0.5 degrees in elevation */
  while( (abs(iaz_error) > 182) || (abs(iel_error) > 91 ) )
    {
      sig_microSleep( 100000 );
      ant_get_azel_err( (BIN2 *)&iaz_error, (BIN2 *)&iel_error );
    }

  printf( "Starting Antenna motion.\n");
  itime = 0;
  while (itime<60000)
    {
      fangle = (180.0 * ((FLT4)itime))/ 60000.0;
      iazimuth = iBin2FromFDeg( fangle );
      ant_set_az_pos( iazimuth );

      fangle = 5.0 + (50.0 * sin(FPI_P * ((FLT4)itime)/ 60000.0 ) );
      ielevation = iBin2FromFDeg( fangle );
      ant_set_el_pos( ielevation );

      sig_microSleep( 100000 );
      itime += 100;
    }
  
  istatus = ant_close();

  if ( istatus != SS_NORMAL ) sig_signal( istatus );

  exit(EXIT_SUCCESS) ;
}
