 /*
 * This procedures provide the interface "glue" between the wish
 * interpreter and our own C++ code.
 */

/*
 * Include details of the Tcl/Tk library
 */

extern "C" {
#include "tk.h"
}

//***** The following is from From MBARI for the station keeping interface
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>

#include <tcl.h>
#include <tk.h>

#include "auto_rov.h"
#define DEBUG 1

///***********************************************************************


/*  Include details of our own C++ code */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <tk.h>
#include <string>
#include <sys/time.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <pthread.h>
#include "togl.h"
#include "veh_ogl.h"
#include "c++/control.h"
#include "c++/configuration.h"

/********************** include files for ventana c++ ********************/

#include "./c++/vehicle.h"
#include "./c++/telemetry.h"
#include "./c++/telemetry_string.h"
#include "./c++/serial_device.h"
#include "./c++/serial_deviceMG.h"
#include "./c++/ser_devices/tms_device.h"
#include "./c++/ser_devices/logging_device.h"
#include "./c++/ser_devices/vision_device.h"
#include "./c++/ser_devices/gyro_device.h"
#include "./c++/ser_devices/rovhead_device.h"
#include "./c++/ser_devices/shiphead_device.h"
#include "./c++/ser_devices/overlay_device.h"
#include "./c++/configuration.h"
#include "./c++/depth.h"
#include "./c++/alarm.h"
#include "./c++/io_poll.h"
#include "./c++/graphic.h"

/************************************************************************/


/* The maximum size of a result string being passed back to wish */
/*const int result_size = 1024;*/

#define result_size 1024
#define FOG_GRAPH 100
#define BACKUP_GRAPH 101

float tester_angle;

extern int send_status;
extern int globalStatus;
extern float auto_heading_setpoint; 	
extern float auto_depth_setpoint;
extern float auto_cruise_setpoint;
extern float auto_cruise_lat_setpoint;
extern int auto_cruise_use_bottom_lock;
extern int globalAutoCruiseOn;
extern int telemetry_status;
extern int bellypack_value;
extern int parse_error;
extern int teleos_enable;
extern int deviation_fog, deviation_compass, declination_fog, declination_compass;
extern char vision_tx[];
extern int armByte, camByte; //From Telemetry.cpp and is set when the arm is locked
int arm_time_constant, cam_time_constant;  //Used in configuration.cpp for config file
float dvl_heading = 0;
float backup_heading = 0;

int teleos_string_status, ship_gyro_string_status, tms_string_status;
char enable_overlay_string[100], insert_overlay_string[100];

/* A string buffer for storing the result */
char result[result_size];
char depth_result[result_size];
char head_result[result_size];
char tx_buffer[result_size];
int conn_type;
char *ptr;
extern string page_string[4];	// from configuration.cpp
static char armStr[512];
static char camStr[512];
//char ipc_message[result_size];
int ground_fault_16_status = 1;		
int old_16_channel = 0;
int ground_fault_8_status = 1;		
int old_8_channel = 0;

/****************** created global ventena objects *********************/

Configuration conf;
char visionRxBuffer[501];

/****************** Thread timers**************************************/
int serial_device_transmit_thread_timer = 0;    //send_t
int telemetry_receive_thread_timer = 0;         //tty_recv_routine
int head_hist_and_telem_timer_thread_timer = 0; //depth_head_routine
int depth_hist_thread_timer = 0;                //depth_routine
int io_poll_analogs_thread_timer = 0;		//pollIO
int io_poll_digitals_thread_timer = 0;          //pollDigital
int disk_logging_thread_timer = 0;              //disk_logging
int serial_port_receive_thread_timer = 0;       //runSensors

/**********************************************************************/




/*======================================================================*
 *                      ArgError                                        *
 *======================================================================*/

int ArgError(char *command, Tcl_Interp *interp)
{
  /*
   * Provide an error reporting mechanism. If a command is called
   * with the wrong number of arguments, we create a suitable
   * error message and pass back the TCL_ERROR code to show that
   * the command didn't complete properly.
   */
  sprintf(result, "Wrong number of arguments for function: %s", command);

  /*
   * This is the method used for passing results back to the
   * wish interpreter
   */
  interp->result = result;
  return TCL_OK;
}




/***********************************************************************
 *                   OpenGL  specific commands                     *
 *                                                                     *
 ***********************************************************************/
// This function is to used by vehicle graphic to display lateral thruster
int Lateral_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )			
    	{
      	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
       	return TCL_ERROR;	
    	}	
    lateral = atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}


// This function is to used by vehicle graphic to display port thruster
int Port_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )
    	{
     	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
      	return TCL_ERROR;	
   	}		
    port = atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}


// This function is to used by vehicle graphic to display stbd thruster
int Stbd_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )
   	{	
      	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
      	return TCL_ERROR;	
   	}		
    stbd = atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}


// This function is to used by vehicle graphic to display vertical thruster
int Vertical_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )			
    	{
      	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
       	return TCL_ERROR;	
    	}	
    vertical =  atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}


// This function is to used by vehicle graphic to display main camera horizontal movement (tilt)
int Camh1_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )			
	{
      	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
      	return TCL_ERROR;	
   	}	
    camh1 = atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}


// This function is to used by vehicle graphic to display main camera vertical movement (pan)

int Camv1_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )			
    	{
      	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
       	return TCL_ERROR;	
    	}	
    camv1 =  atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}


// This function is to used by vehicle graphic to display second camera horizontal movement (tilt)

int Camh2_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )			
    	{
      	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
      	return TCL_ERROR;	
   	}	
    camh2 = atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}



// This function is to used by vehicle graphic to display main camera vertical movement (pan)

int Camv2_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )			
    	{
      	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
       	return TCL_ERROR;	
    	}	
    camv2 =  atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}


// This function is to used by vehicle graphic to display vehicle rolling

int Roll_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )			
    	{
      	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
       	return TCL_ERROR;	
    	}	
    current_zrot =  atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}


// This function is to used by vehicle graphic to display vehicle pitching

int Pitch_Cmd ( struct Togl *togl, int argc, char *argv[] )
{
    Tcl_Interp *interp = Togl_Interp(togl);
    if ( argc != 3 )			
    	{
      	Tcl_SetResult( interp, "wrong # args: ", TCL_STATIC );
       	return TCL_ERROR;	
    	}	
    current_xrot =  atof( argv[2] );
    Togl_PostRedisplay( togl );
    return TCL_OK;
}


/*======================================================================*
 *			makeElement					*
 *======================================================================*/

/*======================================================================*
 * send_io_elem_info_cmd				
 *					
 * This function will send an ioelement and push it into the DB
 *
 *======================================================================*/

int send_io_elem_info_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 22 ) return ArgError(argv[0], interp);

   int surfVehicle = atoi(argv[1]);
   int digAnalog = atoi(argv[2]); 
   int element_num = atoi(argv[3]);
   string element_name(argv[4]);
   string defaults(argv[5]);			
   int default_level = atoi(argv[6]);	
   bool enable_channel = atoi(argv[7]);
   bool	auto_calibrate_enable = atoi(argv[8]);
   bool enable_graphics = atoi(argv[9]);	// removed
   int digital_value = atoi(argv[10]); 
   float analog_value = atof(argv[11]); 
   float analog_raw = atof(argv[12]);
   float offset = atof(argv[13]);
   float gain = atof(argv[14]);			// GAIN 
   float max = atof(argv[15]);			// MAX_ANALOG
   float min = atof(argv[16]);			// MIN_ANALOG
   string graphic_type(argv[17]);
   int color = atoi(argv[18]);			// removed
   int x = atoi(argv[19]);
   int y = atoi(argv[20]);
   int vga_screen = atoi(argv[21]);

  // create a new element
  IOElement newElement;
  newElement.make( surfVehicle, digAnalog, element_num, element_name, defaults, default_level, enable_channel,
		   auto_calibrate_enable, enable_graphics, digital_value, analog_value, analog_raw, offset,
		   gain,max, min, graphic_type, color, x, y, vga_screen );

   cout << "------------ Element ----------------\n";
   cout << "surfVehicle: " << surfVehicle << endl;
   cout << "digAnalog: " << digAnalog << endl;
   cout << "element_num " << element_num << endl;
   cout << "element name " << element_name << endl;
   cout << "---------------------------------\n";

  if(ioElementControl.pushElement(newElement) == false)
	{
     	//cout <<"commands.c: fail to push Element \n";
     	sprintf(result, "%d", -1);
  	}
  if (argv[1] == NULL) return -1;
  conn_type = strtol(argv[1], &ptr, 10);
  if (conn_type == 0 && ptr == argv[1]) return -1;

  // IN_ANALOG_GAINOFFSET = 5
  if (  surfVehicle == 0 && digAnalog == 0 )
  	{
     	char gain_offset[50];
     	extern int ack_in_analog_gainoff;
        int loop_timeout = 0;
        ack_in_analog_gainoff = 0;

  	snprintf (gain_offset, 50, "%d|%d|%4.2f|%4.2f\n", 5, element_num, gain, offset);
  	tty_send_request (gain_offset);
  	usleep(400000);
  	cout << "commands.c : Ack Analog Gainoff" << ack_in_analog_gainoff << endl;
    	while(ack_in_analog_gainoff == 0)
    		{
    		tty_send_request(gain_offset);
    		usleep(400000);
    		cout << "commands.c : Ack Analog Gainoff" << ack_in_analog_gainoff << endl;
    		loop_timeout ++;
    		if (loop_timeout == 10)
    			{
    			alarm_obj.set_other_alarm(37); //generate alarm
    			ack_in_analog_gainoff = 2;
    			}
		}
    	}
   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}


/*======================================================================*
 * ret_all_info_cmd				
 *					
 * This function will return the element properties such as raw analog, and analog value,
 * gain, offset, max, min autocalibrate.
 * more comments added later...
 *======================================================================*/

int ret_all_info_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 5 ) return ArgError(argv[0], interp);

   int surfVehicle = atoi(argv[1]);
   int digAnalog = atoi(argv[2]); 
   int element_num = atoi(argv[3]);
   int screen = atoi(argv[4]);

   if (!(ioElementControl.retIOString(surfVehicle, digAnalog, element_num, screen, result)))
   cout << "commands.c: Error in ret_all_info string result\n";
   interp->result = result;
   return TCL_OK;
}


// This function is called when user decided to save the configuration file
// returns -1 if fail and 0 otherwise

int saving_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
   if ( argc != 2 ) return ArgError(argv[0], interp);
   if (argv[1] == NULL) return -1;

   string filename(argv[1]);

   if ( conf.writing(ioElementControl, filename) == -1)	sprintf(result, "%d", -1);
   else	sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}

// This function is called when the user wants to open a user configuration file
// returns -1 if fail and 0 otherwise

int opening_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
   if ( argc != 2 ) return ArgError(argv[0], interp);
   if (argv[1] == NULL) return -1;

   string filename(argv[1]);

   // Clean up the database for reading new configuration file
   ioElementControl.clean_db();
   if ( conf.reading(ioElementControl, filename) == -1) sprintf(result, "%d", -1);
   else	sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}

/*======================================================================*
 * database_dump_cmd				
 *					
 * This function will display all the IO element in database that has been created
 *
 *======================================================================*/
int database_dump_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 )return ArgError(argv[0], interp);
   ioElementControl.dump();
   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}


// send the output nodes from selected from GUI to C++ database (output database)

int send_out_list_info_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 5 ) return ArgError(argv[0], interp);

   int surfVehicle = atoi(argv[1]);
   int digAnalog = atoi(argv[2]);
   int element_num = atoi(argv[3]);

   ioElementControl.make_input_output(surfVehicle, digAnalog, element_num, argv[4]);
   //ioElementControl.dump_output();
   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}


// Sending the status of alternate button clicked by user down to the sub

int send_alternate_but_down_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 3 ) return ArgError(argv[0], interp);

      	int board = atoi(argv[1]);
      	int elem_num = atoi(argv[2]);
      	int state = ioElementControl.get_but_state (board, elem_num);
      	
      	cout <<"commands.c: state1 : "<<state<<endl;
        if ( state == -1 )
      		{
     		snprintf (result, 100, "%d", state);
   		interp->result = result;
   		return TCL_OK;
		}
        if(state == 1)
	      	{
      		state = 0;
	       	ioElementControl.set_but_state (board, elem_num, 0);       	
      		}
	else
	      	{
      		state = 1;
       		ioElementControl.set_but_state (board, elem_num, 1);       	
		}
        snprintf(tx_buffer, 1024, "%d|%d|%d\n", board, elem_num, state);
   	cout <<"commands.c: alternate state : "<<tx_buffer<<endl;
        tty_send_request (tx_buffer);
        interp->result = result;
        return TCL_OK;
}


// Sending the status of momentary button clicked by user down to the sub

int send_momentary_but_down_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	if ( argc != 4 ) return ArgError(argv[0], interp);

      	int board = atoi(argv[1]);
      	int elem_num = atoi (argv[2]);
      	int state = atoi(argv[3]);

      	// command should be IO_ELEM = 1 , surveh = 1, board, elem#, state/value
      	//sprintf(result, "%d|%d|%d|%d|%d", board, elem_num, state);

      	ioElementControl.set_but_state (board, elem_num, state);
      	snprintf(tx_buffer, 1024, "%d|%d|%d\n", board, elem_num, state);
      	cout <<"commands.c: state : "<<state<<endl;
        tty_send_request(tx_buffer);
        interp->result = result;
        return TCL_OK;
}


// Sending the value of a slidebar set by user down to the sub

int send_slide_down_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	if ( argc != 4 ) return ArgError(argv[0], interp);

      	int board = atoi(argv[1]);
      	int elem_num = atoi(argv[2]);
        float value = atof(argv[3]);

      	// command should be IO_ELEM = 1 , surveh = 1, board, elem#, value

      	snprintf(tx_buffer, 1024, "%d|%d|%3.2f\n", board, elem_num, value);
      	cout <<"commands.c : sending down : "<<tx_buffer<<endl;
        tty_send_request (tx_buffer);
        sprintf(result, "%d", 0);
   	interp->result = result;
        return TCL_OK;
}


// This function will display the all the output created and exist in database
int display_output_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 4 ) return ArgError(argv[0], interp);

   	int surfVehicle = atoi(argv[1]);
   	int digAnalog = atoi(argv[2]);
   	int element_num = atoi(argv[3]);
        string the_output;

   	// this function will get the outputs as string and return it as string
   	the_output = ioElementControl.display_output( surfVehicle, digAnalog, element_num);
        snprintf(result, 1024, "%s", the_output.c_str());
   	interp->result = result;
        return TCL_OK;
}


// This function is called whenever the user is trying to create a button / slidebar /
// or a new IO element so that user can not accidentally map the same button / IO
// element / slidebar at the same time

int check_out_db_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 4 ) return ArgError(argv[0], interp);

    string name(argv[1]);
    int board = atoi(argv[2]);
    int elem_num = atoi(argv[3]);
    int type = 0;

    if ( ioElementControl.check_io_output_db (board, elem_num)  > 0 ) type = ioElementControl.check_io_output_db (board, elem_num);
    if ( ioElementControl.check_button_db (name, board, elem_num) > 0 ) type =  ioElementControl.check_button_db (name, board, elem_num);
    if ( ioElementControl.check_slidebar_db (name, board, elem_num) > 0 ) type =  ioElementControl.check_slidebar_db (name, board, elem_num);

    /*--------------------------------------------
     * if it Doesn't find it in db yet, return 0, else return the type :
     * IO element 	returns 1 - 8
     * Button 	returns 9
     * Slidebar 	returns 10
     --------------------------------------------*/

    sprintf(result, "%d", type);
    interp->result = result;
    return TCL_OK;
}


// This function will remove the button from specified screen
// If the button is mapped on both screen, and the user only wants to delete
// the one on the first screen, then the button would still exist in database
// only when BOTH button from BOTH screen are deleted / not exists, then
// the button element would be deleted / removed from the Button database

int remove_button_from_db_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 4 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);
   int screen = atoi(argv[3]);

   cout <<"commands.c: Removing button from screen : "<< screen<<endl;
   ioElementControl.remove_button_from_db (board, elem_num, screen);
   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}

// This will add a button element to database with information for
// the name, type, position, where it is connected to, color, and which
// screen it is displayed on

int add_to_button_db_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 10 ) return ArgError(argv[0], interp);

    string name(argv[1]);
    string type(argv[2]);
    int board = atoi(argv[3]);
    int elem_num = atoi(argv[4]);
    int x = atoi(argv[5]);
    int y = atoi(argv[6]);
    string color(argv[7]);
    int state = atoi(argv[8]);
    int screen = atoi(argv[9]);

    if (ioElementControl.add_to_button_db (name, type, board, elem_num, x, y, color, state, screen) == 1 )
     	sprintf(result, "%d", 1); 	
    else sprintf(result, "%d", 0);
    interp->result = result;
    return TCL_OK;
}



// This function will remove the slidebar from specified screen
// If the slidebar is mapped on both screen, and the user only wants to delete
// the one on the first screen, then the slidebar would still exist in database
// only when BOTH slidebar from BOTH screen are deleted / not exists, then
// the slidebar element would be deleted / removed from the Slidebar database



int remove_slidebar_from_db_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 4 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi (argv[2]);
   int screen = atoi(argv[3]);

   cout <<"commands.c: board : "<<board<<endl;
   cout <<"commands.c: elem_num : " <<elem_num<<endl;
   cout <<"commands.c: Removing slidebar from screen : "<< screen<<endl;
   ioElementControl.remove_slidebar_from_db (board, elem_num, screen);
   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}



// This will add a slidebar element to database with information for
// the name, type, position, where it is connected to, color, and which
// screen it is displayed on

int add_to_slidebar_db_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 9 ) return ArgError(argv[0], interp);

    string name(argv[1]);
    int board = atoi(argv[2]);
    int elem_num = atoi(argv[3]);
    float value = atof(argv[4]);
    int x = atoi(argv[5]);
    int y = atoi(argv[6]);
    string color(argv[7]);
    int screen = atoi(argv[8]);

    cout <<"commands.c: adding to slidebar database \n";
    if (ioElementControl.add_to_slidebar_db (name, board, elem_num, value, x, y, color, screen) == 1 )
     	sprintf(result, "%d", 1); 	
    else sprintf(result, "%d", 0);
    interp->result = result;
    return TCL_OK;
}



int show_connection_existence_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   int type= atoi(argv[1]);

   strcpy( result, ioElementControl.show_connection_existence (type) );
   interp->result = result;
   return TCL_OK;
}

// This function is used to set the value for slidebar when the enable button
// is enabled

int set_slide_value_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 4 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);
   float value = atof(argv[3]);

   ioElementControl.set_slide_value(board, elem_num, value);
   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}



// This function is called in order to display the slide value set by user
// when the enable button for the slidebar is enabled

int get_slide_value_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 3 )return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);
   float value = ioElementControl.get_slide_value(board, elem_num);

   sprintf(result, "%d", value);
   interp->result = result;
   return TCL_OK;
}



int get_button_state_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 3 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);
   int state = ioElementControl.get_but_state(board, elem_num);

   sprintf(result, "%d", state);
   interp->result = result;
   return TCL_OK;
}


int set_button_state_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 4 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);
   int state = atoi(argv[3]);

   ioElementControl.set_but_state(board, elem_num, state);
   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}


int get_button_count_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   sprintf(result, "%d", ioElementControl.bcount );
   interp->result = result;
   return TCL_OK;
}



int get_slidebar_count_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   sprintf(result, "%d", ioElementControl.scount );
   interp->result = result;
   return TCL_OK;
}


int get_button_string_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   int index = atoi(argv[1]);

   strcpy(result, ioElementControl.get_button_string(index) );
   interp->result = result;
   return TCL_OK;
}


int get_slidebar_string_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   int index = atoi(argv[1]);
   //cout<<"commands.c: index is : "<<index <<endl;

   strcpy(result, ioElementControl.get_slidebar_string(index) );
   interp->result = result;
   return TCL_OK;
}


int clean_up_screen_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	if ( argc != 1 ) return ArgError(argv[0], interp);
        // clean up  io element and its ouputs (and buttond and slidebar)
        ioElementControl.clean_all_ioelem();
	// clean up overlay db
	graph.delete_all_overlay_db();
	// clean up log db
      	telstring.delete_all_log_item ();
   	return TCL_OK;
}

int get_button_connection_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   string name(argv[1]);

   strcpy(result, ioElementControl.get_button_connection(name) );
   interp->result = result;
   return TCL_OK;
}


int get_button_name_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 3 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);

   strcpy(result, ioElementControl.get_button_name(board, elem_num) );
   interp->result = result;
   return TCL_OK;
}

int get_button_color_type_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 3 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);

   strcpy(result, ioElementControl.get_button_color_type(board, elem_num) );
   interp->result = result;
   return TCL_OK;
}


int get_slidebar_name_cmd ( ClientData clientData,  Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 3 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);

   strcpy(result, ioElementControl.get_slidebar_name(board, elem_num) );
   interp->result = result;
   return TCL_OK;
}



int get_slide_connection_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   string name(argv[1]);

   strcpy(result, ioElementControl.get_slide_connection(name) );
   interp->result = result;
   return TCL_OK;
}


/********************************************************************
* Starts the thread to receive the serial data for the telemetry
*
*
*********************************************************************/
int spawn_receive_thread_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   int		status;
   pthread_t	thread;
   pthread_attr_t detached_attr;
   struct sched_param param;

   status = pthread_attr_init (&detached_attr);
   if (status != 0) err_abort (status, "Init attributes object for sending");

   // set thread to detached state 			
   status = pthread_attr_setdetachstate(&detached_attr, PTHREAD_CREATE_DETACHED);
   if (status != 0) err_abort (status, "Set detach state for sending");
		
   // tty_recv_routine is in telemetry .cpp
   status = pthread_create (&thread, &detached_attr, tty_recv_routine, NULL);
   if (status != 0) err_abort (status, "Create Telemetry String Receive Routine");
  	
   return TCL_OK;
}

/********************************************************************
* Depth _Head_routine:  Updates Heading History array
* 			Times the received data from the telemetry to
*			display an error on timeout
* Depth_routine:	Updates Depth History arrays. There are 3 arrays	
*
*********************************************************************/
int spawn_depth_thread_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   int		status;
   pthread_t	thread;
   pthread_attr_t detached_attr;
   pthread_t	thread_depth;
   pthread_attr_t detached_attr_depth;

   status = pthread_attr_init (&detached_attr);
   if (status != 0) err_abort (status, "Init attributes object for sending");
	
   status = pthread_attr_setdetachstate(&detached_attr, PTHREAD_CREATE_DETACHED);
   if (status != 0) err_abort (status, "Set detach state for sending");
   // depth_head_routine is in depth.cpp
   status = pthread_create (&thread, &detached_attr, depth_head_routine, NULL);
   if (status != 0) err_abort (status, "Create Telemetry String Routine");


   //--------------- To Get The DEPTH -----------------------
   // inside vehicle class


   status = pthread_attr_init (&detached_attr_depth);
   if (status != 0) err_abort (status, "Init attributes object for sending");
		
   status = pthread_attr_setdetachstate(&detached_attr_depth, PTHREAD_CREATE_DETACHED);
   if (status != 0) err_abort (status, "Set detach state for sending");
	
   // depth_routine is in vehicle.cpp
   status = pthread_create (&thread_depth, &detached_attr_depth, depth_routine, NULL);
   if (status != 0) err_abort (status, "Create Telemetry String Routine");

   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}



int get_depth_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   sprintf(result, "%4.1f", vehicle.curr_depth);
   interp->result = result;
   return TCL_OK;
}


int get_depth_hist_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);
	
   int which_array = atoi(argv[1]);

   if (!vehicle.get_depth_hist(which_array,depth_result)) cout << "commands.c: Error in 'get_depth_hist' result\n";
   interp->result = depth_result;
   return TCL_OK;
}


int get_head_hist_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
	
   if (!td.get_head_hist(head_result)) cout << "commands.c: Error in 'get_head_hist' result\n";
   interp->result = head_result;
   return TCL_OK;
}



int get_head_cmd ( ClientData clientData, Tcl_Interp *interp, int argc,	char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
	
   // obtained from vehicle.cpp file
   snprintf(result, 20, "%4.1f", vehicle.curr_heading);
   interp->result = result;
   return TCL_OK;
}


int get_pitch_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   float pitch = vehicle.curr_pitch;

   //cout << "commands.c: current pitch is: " << pitch << endl;
   sprintf(result, "%4.1f", pitch);
   interp->result = result;
   return TCL_OK;
}


int get_roll_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   float roll = vehicle.curr_roll;

   //cout << "commands.c: current roll is: " << roll << endl;
   sprintf(result, "%4.1f", roll);
   interp->result = result;
   return TCL_OK;
}


int add_alarm_to_db_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 4 )return ArgError(argv[0], interp);
      		
      	int diganal = atoi(argv[1]);
      	int elem_num = atoi(argv[2]);	
     	string name(argv[3]);
     	
        alarm_obj.add_alarm_to_db(diganal, elem_num, name);
     	sprintf(result, "%d", 0);
   	interp->result = result;
   	return TCL_OK;
}


int reset_alarm_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 1 ) return ArgError(argv[0], interp);

      	alarm_obj.reset_alarm();
     	sprintf(result, "%d", 0);
   	interp->result = result;
   	return TCL_OK;
}


int check_alarm_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 1 ) return ArgError(argv[0], interp);
      	
      	sprintf(result, "%d", alarm_obj.alarm_status);
   	interp->result = result;
   	return TCL_OK;
}


/* Kenny - added Nov. 20, 2002
 * used for testing alarm
 */
/*
int generate_alarm_cmd ( ClientData clientData,  Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 1 ) return ArgError(argv[0], interp);
	alarm_obj.alarm_status = 1;
}
*/


int check_ana_status_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 2 ) return ArgError(argv[0], interp);

     	int elem_num = atoi(argv[1]);
      	int status = alarm_obj.analog_alarm[elem_num].status;
      	
      	sprintf(result, "%d", status);
   	interp->result = result;
   	return TCL_OK;
}
     	

int check_dig_status_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 2 ) return ArgError(argv[0], interp);

     	int elem_num = atoi(argv[1]);
      	int status = alarm_obj.digital_alarm[elem_num].status;
      	
      	sprintf(result, "%d", status);
   	interp->result = result;
   	return TCL_OK;
}	


int check_other_status_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 2 ) return ArgError(argv[0], interp);

     	int elem_num = atoi(argv[1]);
     	int status = alarm_obj.other_alarm[elem_num].status;
      	
      	sprintf(result, "%d", status);
   	interp->result = result;
   	return TCL_OK;
}	



int get_ana_alarm_name_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 2 ) return ArgError(argv[0], interp);

     	int elem_num = atoi(argv[1]);
     	string name = alarm_obj.analog_alarm[elem_num].name;
      	
      	sprintf(result, "%s", name.c_str());
   	interp->result = result;
   	return TCL_OK;
}
     	


int get_dig_alarm_name_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 2 ) return ArgError(argv[0], interp);

     	int elem_num = atoi(argv[1]);
     	string name = alarm_obj.digital_alarm[elem_num].name;
      	
      	sprintf(result, "%s", name.c_str());
   	interp->result = result;
   	return TCL_OK;
}	


int get_other_alarm_name_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 2 ) return ArgError(argv[0], interp);

     	int index = atoi(argv[1]);
      	string name = alarm_obj.other_alarm[index].name;
      	
      	sprintf(result, "%s", name.c_str());
   	interp->result = result;
   	return TCL_OK;
}	


int get_alarm_descr_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);

     	string name(argv[1]);
     	string descr = alarm_obj.get_alarm_descr(name);
      	
      	snprintf(result, 512, "%s", descr.c_str());
   	interp->result = result;
   	return TCL_OK;

}	


int get_alarm_name_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
     	
      	string name = alarm_obj.alarm_name;
      	
      	snprintf(result, 512, "%s", name.c_str());
   	interp->result = result;
   	return TCL_OK;
}	


int set_ana_alarm_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);

     	int elem_num = atoi(argv[1]);
       	alarm_obj.set_ana_alarm(elem_num);
      	
      	sprintf(result, "%d", 0);
   	interp->result = result;
   	return TCL_OK;
}	


int set_other_alarm_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);

     	int type = atoi(argv[1]);
     	alarm_obj.set_other_alarm(type);
      	
      	sprintf(result, "%d", 0);
   	interp->result = result;
   	return TCL_OK;
}	


int clear_alarm_list_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);

     	// Call the desctructor that will set all status back to 0 (OFF)
      	alarm_obj.clear_alarm_list();
      	
      	sprintf(result, "%d", 0);
   	interp->result = result;
   	return TCL_OK;
}	


int send_clear_list_to_bottom_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);

     	// Call the desctructor that will set all status back to 0 (OFF)
 
	snprintf(tx_buffer, 1024, "%d\n", 3);    	
      	tty_send_request(tx_buffer);
      	sprintf(result, "%d", 0);
   	interp->result = result;
   	return TCL_OK;
}	


int stop_poll_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   /*****************************************************
    * test_poll thread should have the highest priority *
    * using the RMS method, however we still need to    *
    * find out the cpu usage to analyze whether it will *
    * be schedulable.                                   *
    *****************************************************/
   if ( argc != 1 ) return ArgError(argv[0], interp);

   // stop the polling to send down to the sub
   // (but it will NOT KILL the polling thread)
   send_status = 0;
   return TCL_OK;
}


/*************************************************************************
* pollIO thread gets the analog inputs form the io boards
* polldig thread gets the digitals from the io boards
* These are both in io_poll.cpp
*
**************************************************************************/

int test_poll_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   /*****************************************************
    * test_poll thread should have the highest priority *
    * using the RMS method, however we still need to    *
    * find out the cpu usage to analyze whether it will *
    * be schedulable.                                   *
    *****************************************************/   

	int		status;	
	pthread_t	thread;
	pthread_t	thread_2;
	pthread_attr_t   etached_attr;

   	if ( argc != 1 ) return ArgError(argv[0], interp);
   	globalStatus = 1;

   status = pthread_attr_init (&etached_attr);
   if (status != 0) err_abort (status, "Init attributes object for sending");
		
   status = pthread_attr_setdetachstate(&etached_attr, PTHREAD_CREATE_DETACHED);
   if (status != 0) err_abort (status, "Set detach state for sending");

   //pollIO is in io_poll.cpp
   status = pthread_create (&thread, &etached_attr, pollIO, NULL);
   if (status != 0) err_abort (status, "Create io_poll Routine");
	
   status = pthread_attr_init (&etached_attr);
   if (status != 0) err_abort (status, "Init attributes object for sending");
   status = pthread_attr_setdetachstate(&etached_attr, PTHREAD_CREATE_DETACHED);
   if (status != 0) err_abort (status, "Set detach state for sending");

   //polldig is in io_poll.cpp
   status = pthread_create (&thread_2, &etached_attr, pollDig, NULL);
   if (status != 0) err_abort (status, "Create poll() Routine");

   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}



int set_light_db_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
     if ( argc != 24 ) return ArgError(argv[0], interp);

     int x[12];
     int y[12];

     int screen = atoi (argv[1]);
     int xpos = atoi (argv[2]);
     int ypos = atoi (argv[3]);

     for (int i = 0; i < 10; i++) x[i] = atoi( argv[i+4] );
     for (int i = 0; i < 10; i++) y[i] = atoi( argv[i+14] );
     graph.set_light_db (screen, xpos, ypos,
     		x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9],     		
     		y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8], y[9] );
    sprintf(result, "%d", 0);
    interp->result = result;
    return TCL_OK;
}


int send_switches_down_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 4 ) return ArgError(argv[0], interp);

    int board = atoi(argv[1]);
    int channel = atoi(argv[2]);
    int state = atoi(argv[3]);
      	      			
    snprintf(tx_buffer, 1024, "%d|%d|%d\n", board,  channel,  state );
    //cout <<"commands.c: send_switches_down : "<<tx_buffer<<endl;
    tty_send_request (tx_buffer);
    interp->result = result;
    return TCL_OK;
}


int set_graphic_info_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
     if ( argc != 6 ) return ArgError(argv[0], interp);
      	
    string type(argv[1]);
    string name(argv[2]);
    int x = atoi(argv[3]);
    int y = atoi(argv[4]);
    int screen = atoi(argv[5]);

   // tty_ ......( int priority, string command)
   graph.set_graphic_info( type, name, x, y, screen );
   interp->result = result;
   return TCL_OK;
}


int make_O_item_cmd  (ClientData clientData,  Tcl_Interp *interp, int argc, char *argv[])	
{
    char line_string[30];
    char unit[16];
    
    if ( argc != 7 ) return ArgError(argv[0], interp);
	
   int type = atoi(argv[1]);
   int channel = atoi(argv[2]);
   int line = atoi(argv[3]);
   int column = atoi(argv[4]);
   int color = atoi(argv[5]);
   strncpy(unit, argv[6], 16); 
	//----------------------------------------------
	// if element has already exist  in database
	// do not add it to overlay again
	//----------------------------------------------
	if (graph.check_overlay_db(type, channel) > 0)
		{
		cout <<"commands.c : Item has already exist in overlay database\n";
		sprintf(result, "%d", -1);
		interp->result = result;
		return TCL_OK;
		}
   // Save the information to the overlay database
   int exist = graph.add_overlay_db(type, channel, line, column, color, unit);

   sprintf(result, "%d", exist);
   interp->result = result;
   return TCL_OK;
}


int delete_O_item_cmd  (ClientData clientData, Tcl_Interp *interp,  int argc, char *argv[])	
{
    if ( argc != 3 ) return ArgError(argv[0], interp);
	
   int type = atoi(argv[1]);
   int channel = atoi(argv[2]);
   char line[10];
   // Delete the information from the overlay database
   graph.delete_overlay_db(type, channel);
   sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}


int get_page_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
   if ( argc != 2 ) return ArgError(argv[0], interp);
   int page = atoi(argv[1]);
   if (page_string != NULL)
   	{
      	snprintf(result, 1024, "%s", page_string[page-1].c_str());
      	interp->result = result;
      	return TCL_OK;
   	}
   sprintf(result, "%d", -1);
   interp->result = result;
   return TCL_OK;
}


int save_page_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 3 ) return ArgError(argv[0], interp);
   	
  	int page = atoi(argv[1]);
	char temp[512];

	// we want to remove the "\n" line character first
	int len = strlen(argv[2]);
	snprintf (temp, len, "%s", argv[2]);
	page_string[page-1] = string(temp);
	return TCL_OK;
}


int Detritus_select_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
   int ret;

   if ( argc != 2 ) return ArgError(argv[0], interp);
   int selection = atoi(argv[1]);
   if ((ret = ioElementControl.Detritus_select(selection)) < 0) sprintf(result, "%d", -1);
   else sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}


int get_light_string_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
      if ( argc != 2 ) return ArgError(argv[0], interp);
      int screen = atoi (argv[1]);
      graph.get_light_string ( screen, result );
      interp->result = result;
      return TCL_OK;
}

// This one returns the type of compass, x-y position,
// and which screen the compass is displayed on
int get_compass_string_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
      if ( argc != 1 ) return ArgError(argv[0], interp);
      graph.get_compass_string ( result );
      interp->result = result;
      return TCL_OK;
}

// This returns the x, y position and the screen where the vehicle
// is displayed on
int get_vehicle_string_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
      if ( argc != 1 ) return ArgError(argv[0], interp);
      graph.get_vehicle_string ( result );
      interp->result = result;
      return TCL_OK;
}


int get_graph_info_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
      if ( argc != 2 ) return ArgError(argv[0], interp);
      int index = atoi( argv[1]);
      ioElementControl.get_graph_info (index, result );
      interp->result = result;
      return TCL_OK;
}


// This function returns the size of sub analog element to the
// GUI so that when we open a file, we know how many bargraphs
// there are and also the depth graph
int retSubAnaSize_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
      if ( argc != 1 ) return ArgError(argv[0], interp);
      sprintf( result, "%d", ioElementControl.retSubAnaSize( ) );
      interp->result = result;
      return TCL_OK;
}

// This function returns the size of top analog out element 
int retAnaOutSize_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
      if ( argc != 1 ) return ArgError(argv[0], interp);
      sprintf( result, "%d", ioElementControl.retAnaOutSize( ) );
      interp->result = result;
      return TCL_OK;
}


int setting_bar_detail_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
      if ( argc != 7 ) return ArgError(argv[0], interp);
	
      int elem_num = atoi (argv[1]);
      float max = atof(argv[2]);
      float min = atof(argv[3]);
      float ave = atof(argv[4]);
      float yrange = atof(argv[5]);
      float rrange = atof(argv[6]);

      // cout<<"commands.c : maximum is " <<max<<endl;
      // cout<<"commands.c : minimum is " <<min <<endl;
      ioElementControl.setting_bar_detail (elem_num, max, min, ave, yrange, rrange);
      return TCL_OK;
}


int getting_bar_detail_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
      if ( argc != 2 ) return ArgError(argv[0], interp);
      int elem_num = atoi(argv[1]);
      ioElementControl.getting_bar_detail (elem_num, result);
      interp->result = result;
      return TCL_OK;
}


int get_bar_voltage_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
   /* surfVehicle: 	0 - vehicle
    * 		   	1 - surface
    * digitalAnalog:	0 - Analog
    * 			1 - digital
    */
	float analogValue, analogRaw, gain, offset, max, min;

	if (argc != 2) return  ArgError(argv[0], interp);
	int elem_num = atoi(argv[1]);
      	if (!ioElementControl.readVoltage(0, 0, elem_num, analogValue, analogRaw, gain, offset, max, min))
      		{
      		analogValue = -100;
      		analogRaw = -100;
   		}
	snprintf(result, 1024, "%3.1f", analogValue);
	interp->result = result;
	return TCL_OK;
}


int get_element_name_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 4 ) return ArgError(argv[0], interp);

   int surveh = atoi(argv[1]);
   int diganal = atoi(argv[2]);
   int elem_num = atoi(argv[3]);

   snprintf(result, 1024, "%s", ioElementControl.get_element_name(surveh, diganal, elem_num) );
   interp->result = result;
   return TCL_OK;
}

// This function is to get the total number of  button exist
// in the database
int get_bcount_cmd  ( ClientData clientData,  Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   snprintf(result, 1024, "%d", ioElementControl.bcount);
   interp->result = result;
   return TCL_OK;
}

// This will get the button name for specified index
int get_bname_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   int index = atoi(argv[1]);

   snprintf(result, 1024, "%s", ioElementControl.get_bname(index) );
   interp->result = result;
   return TCL_OK;
}

// This function is to get the total number of  slidebar exist
// in the database
int get_scount_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   snprintf(result, 1024, "%d", ioElementControl.scount);
   interp->result = result;
   return TCL_OK;
}

// This will get the slidebar name for specified index
int get_sname_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   int index = atoi(argv[1]);

   snprintf(result, 1024, "%s", ioElementControl.get_sname(index) );
   interp->result = result;
   return TCL_OK;
}

//////////////////////////////////////////////////////////////////////////
// Called by the auto_pilot_popup.tcl and auto_pilot_popup2
int set_autopilot_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
     if ( argc != 29 ) return ArgError(argv[0], interp);

     	vehicle.autopilot.foreaft_ch = atoi(argv[1]);
     	vehicle.autopilot.turns_ch = atoi(argv[2]);

     	/* digital input */
     	vehicle.autopilot.autoHead_ch = atoi(argv[3]);
     	vehicle.autopilot.autoDepth_ch = atoi(argv[4]);
     	vehicle.autopilot.autoAltitude_ch = atoi(argv[5]);
     	vehicle.autopilot.autoCruise_ch = atoi(argv[6]);
     	vehicle.autopilot.depthJogUp_ch = atoi(argv[7]);
     	vehicle.autopilot.depthJogDown_ch = atoi(argv[8]);

      	/**** the following are coming from the bottom ***/
      	vehicle.autopilot.headingSource = atoi(argv[9]);	// 0 for gyro, 1 for compass
      	vehicle.autopilot.depthTransducer = atoi(argv[10]);	// set to -1
      	vehicle.autopilot.AltitudeSource = atoi(argv[11]);	// 0 for alt #1, 1 for #2, 2 for DVL
      	vehicle.autopilot.dvlInput = atoi(argv[12]);            // set to -1
      	
      	/**** the following are the pid's ****************/
      	vehicle.autopilot.p_autoHead = atof(argv[13]);
      	vehicle.autopilot.i_autoHead = atof(argv[14]);
      	vehicle.autopilot.d_autoHead = atof(argv[15]);
      	vehicle.autopilot.p_autoDepth = atof(argv[16]);
      	vehicle.autopilot.i_autoDepth = atof(argv[17]);
      	vehicle.autopilot.d_autoDepth = atof(argv[18]);
      	vehicle.autopilot.p_autoAlt = atof(argv[19]);
      	vehicle.autopilot.i_autoAlt = atof(argv[20]);
      	vehicle.autopilot.d_autoAlt = atof(argv[21]);
      	vehicle.autopilot.p_autoCruise = atof(argv[22]);
      	vehicle.autopilot.i_autoCruise = atof(argv[23]);
      	vehicle.autopilot.d_autoCruise = atof(argv[24]);

      	/**** the following are the vehicle output channels ****/
      	vehicle.autopilot.portThruster_ch = atoi(argv[25]);
      	vehicle.autopilot.STBDThruster_ch = atoi(argv[26]);
      	vehicle.autopilot.vertThruster_ch = atoi(argv[27]);
      	vehicle.autopilot.latThruster_ch = atoi(argv[28]);
        		
        extern int ack_autopilot;
	int loop_timeout = 0;
	ack_autopilot = 0;

        //vehicle_append_auto_string() formats the string for the telemetry stream	
        tty_send_request(vehicle.append_auto_string());
    	usleep(400000);
    	cout << "commands.c : Ack Auto Pilot Setup" << ack_autopilot << endl;
	while(ack_autopilot == 0)
		{
		tty_send_request(vehicle.append_auto_string());
		usleep(400000);
		cout << "commands.c : Ack Auto Pilot Setup" << ack_autopilot << endl;
		loop_timeout ++;
	    	if (loop_timeout == 10)
    			{
	    		ack_autopilot = 2;
	    		alarm_obj.set_other_alarm(39); //generate alarm
    			}
		}
	sprintf(result, "%d", 0);
    	interp->result = result;
    	return TCL_OK;
}

// Called by arm_control_popup.tcl and arm_control_popup2.tcl
// Makes the elements if they don't exist
// Send the string to the sub
int set_arm_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
	if ( argc != 51 ) return ArgError(argv[0], interp);

    /* the following are bottom output channels */
    int sin_shoulder_channel = atoi(argv[1]);
    int cos_shoulder_channel = atoi(argv[2]);
    float offset_shoulder = atof(argv[3]);
    /* the following are the top input channels */
    int command_shoulder_channel_top = atoi(argv[4]);
    float master_shoulder = atof(argv[5]);
    float feedback_shoulder = atof(argv[6]); 
    int shoulder_servo_channel_bot = atoi(argv[7]);
    float p_shoulder = atof(argv[8]);
    float i_shoulder = atof(argv[9]);
    float d_shoulder = atof(argv[10]);
    
    /* the following are bottom output channels */
    int sin_swing_channel= atoi(argv[11]);
    int cos_swing_channel = atoi(argv[12]);
    float offset_swing = atof(argv[13]);
    /* the following are the top input channels */
    int command_swing_channel_top = atoi(argv[14]);
    float master_swing = atof(argv[15]);
    float feedback_swing = atof(argv[16]); 
    int swing_servo_channel_bot = atoi(argv[17]);
    float p_swing = atof(argv[18]);
    float i_swing = atof(argv[19]);
    float d_swing = atof(argv[20]);


    /* the following are bottom output channels */
    int sin_elbow_channel= atoi(argv[21]);
    int cos_elbow_channel = atoi(argv[22]);
    float offset_elbow = atof(argv[23]);
    /* the following are the top input channels */
    int command_elbow_channel_top = atoi(argv[24]);
    float master_elbow = atof(argv[25]);
    float feedback_elbow = atof(argv[26]); 
    int elbow_servo_channel_bot = atoi(argv[27]);
    float p_elbow = atof(argv[28]);
    float i_elbow = atof(argv[29]);
    float d_elbow = atof(argv[30]);


    /* the following are bottom output channels */
    int sin_pitch_channel= atoi(argv[31]);
    int cos_pitch_channel = atoi(argv[32]);
    float offset_pitch = atof(argv[33]);
    /* the following are the top input channels */
    int command_pitch_channel_top = atoi(argv[34]);
    float master_pitch = atof(argv[35]);
    float feedback_pitch = atof(argv[36]); 
    int pitch_servo_channel_bot = atoi(argv[37]);
    float p_pitch = atof(argv[38]);
    float i_pitch = atof(argv[39]);
    float d_pitch = atof(argv[40]);

  
    /* the following are bottom output channels */
    int sin_yaw_channel= atoi(argv[41]);
    int cos_yaw_channel = atoi(argv[42]);
    float offset_yaw = atof(argv[43]);
    /* the following are the top input channels */
    int command_yaw_channel_top = atoi(argv[44]);
    float master_yaw = atof(argv[45]);
    float feedback_yaw = atof(argv[46]); 
    int yaw_servo_channel_bot = atoi(argv[47]);
    float p_yaw = atof(argv[48]);
    float i_yaw = atof(argv[49]);
    float d_yaw = atof(argv[50]);

    /*** now we must make the internal io_elements ******/
    /*** and set the information in the vehicle class ***/
    /* surfVehicle: 	0 - vehicle
    * 		   	1 - surface
    * digitalAnalog:	0 - Analog
    * 			1 - digital
    */

    int surfVehicle = 1;
    int digAnalog = 0; 
    int element_num = command_shoulder_channel_top;
    string element_name("arm - shoulder command"); 
    string defaults("");		// removed	
    int default_level = 0;	
    bool enable_channel = true;
    bool auto_calibrate_enable = true;
    bool enable_graphics = true;	// removed
    int digital_value = 0;
    float analog_value = 0.0;
    float analog_raw = 0.0;
    float analog_offset = 0.0;
    float gain = 0.0;			// GAIN
    float max = 0.0;			// MAX_ANALOG
    float min = 0.0;			// MIN_ANALOG
    string graphic_type("NONE");
    int color = 0;			// removed
    int x = 0;
    int y = 0;
    int vga_screen = 1;

  IOElement command_shoulder;
  command_shoulder.make( surfVehicle, digAnalog, element_num, element_name, defaults, default_level,
		   enable_channel, auto_calibrate_enable, enable_graphics, digital_value, analog_value,
		   analog_raw, analog_offset, gain, max, min, graphic_type, color, x, y, vga_screen );
		
  // if element has not been created yet
  if(ioElementControl.inDataBase(command_shoulder))
  	{     	
	sprintf(result, "%d", -1);
	interp->result = result;
	}
  else if(ioElementControl.pushElement(command_shoulder) == false) sprintf(result, "%d", -1);


//-----------------------------
/* command_swing element */
//-----------------------------
    element_num = command_swing_channel_top;
    element_name = "arm - swing command";

 // create a new element
  IOElement command_swing;
  command_swing.make( surfVehicle, digAnalog, element_num, element_name, defaults, default_level, enable_channel,
		   auto_calibrate_enable, enable_graphics, digital_value, analog_value, analog_raw,
		   analog_offset, gain, max, min, graphic_type, color, x, y, vga_screen );

  if(ioElementControl.inDataBase(command_swing))
  	{
    	sprintf(result, "%d", -1);
	interp->result = result;
  	}
  else	if(ioElementControl.pushElement(command_swing) == false) sprintf(result, "%d", -1);


//-----------------------------
/* command_elbow element */
//-----------------------------
    element_num = command_elbow_channel_top;
    element_name = "arm - elbow command";

  // create a new element
  IOElement command_elbow;
  command_elbow.make( surfVehicle, digAnalog, element_num, element_name, defaults, default_level, enable_channel,
		   auto_calibrate_enable, enable_graphics, digital_value, analog_value, analog_raw,
		   analog_offset, gain, max, min, graphic_type, color, x, y, vga_screen );

  if(ioElementControl.inDataBase(command_elbow))
  	{
        sprintf(result, "%d", -1);
	interp->result = result;
  	}
  else if(ioElementControl.pushElement(command_elbow) == false) sprintf(result, "%d", -1);

//-----------------------------
/* command_pitch element */
//-----------------------------
    element_num = command_pitch_channel_top;
    element_name = "arm - pitch command";

  // create a new element
  IOElement command_pitch;
  command_pitch.make( surfVehicle, digAnalog, element_num, element_name, defaults, default_level, enable_channel,
		   auto_calibrate_enable, enable_graphics, digital_value, analog_value, analog_raw,
		   analog_offset, gain, max, min, graphic_type, color, x, y, vga_screen );

  if(ioElementControl.inDataBase(command_pitch))
	{
        sprintf(result, "%d", -1);
	interp->result = result;
	}
  else if(ioElementControl.pushElement(command_pitch) == false) sprintf(result, "%d", -1);
  	
//-----------------------------
/* command_yaw element */
//-----------------------------
    element_num = command_yaw_channel_top;
    element_name = "arm - yaw command";

  // create a new element
  IOElement command_yaw;
  command_yaw.make( surfVehicle, digAnalog, element_num, element_name, defaults, default_level, enable_channel,
		   auto_calibrate_enable, enable_graphics, digital_value, analog_value, analog_raw,
		   analog_offset, gain, max, min, graphic_type, color, x, y, vga_screen );

  if(ioElementControl.inDataBase(command_yaw))
	{
    	sprintf(result, "%d", -1);
	interp->result = result;
  	}
  else  if(ioElementControl.pushElement(command_yaw) == false) sprintf(result, "%d", -1);

  /*************************************/
  /* the following are bottom output channels */
    vehicle.arm.sin_shoulder_channel = atoi(argv[1]);
    vehicle.arm.cos_shoulder_channel = atoi(argv[2]);
    vehicle.arm.offset_shoulder = atof(argv[3]);
    /* the following are the top input channels */
    vehicle.arm.command_shoulder_channel_top = atoi(argv[4]);
    vehicle.arm.master_shoulder = atof(argv[5]);
    vehicle.arm.feedback_shoulder = atof(argv[6]);
    vehicle.arm.shoulder_servo_channel_bot = atoi(argv[7]);
    vehicle.arm.p_shoulder = atof(argv[8]);
    vehicle.arm.i_shoulder = atof(argv[9]);
    vehicle.arm.d_shoulder = atof(argv[10]);

    /* the following are bottom output channels */
    vehicle.arm.sin_swing_channel= atoi(argv[11]);
    vehicle.arm.cos_swing_channel = atoi(argv[12]);
    vehicle.arm.offset_swing = atof(argv[13]);
    /* the following are the top input channels */
    vehicle.arm.command_swing_channel_top = atoi(argv[14]);
    vehicle.arm.master_swing = atof(argv[15]);
    vehicle.arm.feedback_swing = atof(argv[16]);
    vehicle.arm.swing_servo_channel_bot = atoi(argv[17]);
    vehicle.arm.p_swing = atof(argv[18]);
    vehicle.arm.i_swing = atof(argv[19]);
    vehicle.arm.d_swing = atof(argv[20]);

    /* the following are bottom output channels */
    vehicle.arm.sin_elbow_channel= atoi(argv[21]);
    vehicle.arm.cos_elbow_channel = atoi(argv[22]);
    vehicle.arm.offset_elbow = atof(argv[23]);
    /* the following are the top input channels */
    vehicle.arm.command_elbow_channel_top = atoi(argv[24]);
    vehicle.arm.master_elbow = atof(argv[25]);
    vehicle.arm.feedback_elbow = atof(argv[26]);
    vehicle.arm.elbow_servo_channel_bot = atoi(argv[27]);
    vehicle.arm.p_elbow = atof(argv[28]);
    vehicle.arm.i_elbow = atof(argv[29]);
    vehicle.arm.d_elbow = atof(argv[30]);

    /* the following are bottom output channels */
    vehicle.arm.sin_pitch_channel= atoi(argv[31]);
    vehicle.arm.cos_pitch_channel = atoi(argv[32]);
    vehicle.arm.offset_pitch = atof(argv[33]);
    /* the following are the top input channels */
    vehicle.arm.command_pitch_channel_top = atoi(argv[34]);
    vehicle.arm.master_pitch = atof(argv[35]);
    vehicle.arm.feedback_pitch = atof(argv[36]);
    vehicle.arm.pitch_servo_channel_bot = atoi(argv[37]);
    vehicle.arm.p_pitch = atof(argv[38]);
    vehicle.arm.i_pitch = atof(argv[39]);
    vehicle.arm.d_pitch = atof(argv[40]);

    /* the following are bottom output channels */
    vehicle.arm.sin_yaw_channel= atoi(argv[41]);
    vehicle.arm.cos_yaw_channel = atoi(argv[42]);
    vehicle.arm.offset_yaw = atof(argv[43]);
    /* the following are the top input channels */
    vehicle.arm.command_yaw_channel_top = atoi(argv[44]);
    vehicle.arm.master_yaw = atof(argv[45]);
    vehicle.arm.feedback_yaw = atof(argv[46]);
    vehicle.arm.yaw_servo_channel_bot = atoi(argv[47]);
    vehicle.arm.p_yaw = atof(argv[48]);
    vehicle.arm.i_yaw = atof(argv[49]);
    vehicle.arm.d_yaw = atof(argv[50]);
	
    	extern int ack_arm;
    	int loop_timeout = 0;
    	ack_arm = 0;

    	tty_send_request(vehicle.append_arm_string());
    	usleep(400000);
    	cout << "commands.c : Ack Arm Setup" << ack_arm << endl;
	while(ack_arm == 0)
		{
		tty_send_request(vehicle.append_arm_string());
		usleep(400000);
		cout << "commands.c : Ack Arm Setup" << ack_arm << endl;
		loop_timeout ++;
	    	if (loop_timeout == 10)
    			{
	    		ack_arm = 2;
	    		alarm_obj.set_other_alarm(41); //generate alarm
    			}
		}
    sprintf(result, "%d", 0);
    interp->result = result;
    return TCL_OK;
}
// Called from camera_popup.tcl and camera_popup2.tcl
// Makes the elements if they don't exist
// Send the string to the sub
int set_camera_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{

    if ( argc != 31 ) return ArgError(argv[0], interp);

    /* the following are bottom output channels */
    int sin_shoulder_channel = atoi(argv[1]);
    int cos_shoulder_channel = atoi(argv[2]);
    float offset_shoulder = atof(argv[3]);
    /* the following are the top input channels */
    int command_shoulder_channel_cam = atoi(argv[4]);
    float master_shoulder = atof(argv[5]);
    float feedback_shoulder = atof(argv[6]);
    int shoulder_servo_channel_bot = atoi(argv[7]);
    float p_shoulder = atof(argv[8]);
    float i_shoulder = atof(argv[9]);
    float d_shoulder = atof(argv[10]);

    /* the following are bottom output channels */
    int sin_pan_channel= atoi(argv[11]);
    int cos_pan_channel = atoi(argv[12]);
    float offset_pan = atof(argv[13]);
    /* the following are the top input channels */
    int command_pan_channel_cam = atoi(argv[14]);
    float master_pan = atof(argv[15]);
    float feedback_pan = atof(argv[16]);
    int pan_servo_channel_bot = atoi(argv[17]);
    float p_pan = atof(argv[18]);
    float i_pan = atof(argv[19]);
    float d_pan = atof(argv[20]);


    /* the following are bottom output channels */
    int sin_tilt_channel= atoi(argv[21]);
    int cos_tilt_channel = atoi(argv[22]);
    float offset_tilt = atof(argv[23]);
    /* the following are the top input channels */
    int command_tilt_channel_cam = atoi(argv[24]);
    float master_tilt = atof(argv[25]);
    float feedback_tilt = atof(argv[26]);
    int tilt_servo_channel_bot = atoi(argv[27]);
    float p_tilt = atof(argv[28]);
    float i_tilt = atof(argv[29]);
    float d_tilt = atof(argv[30]);

/* the following are bottom output channels */
    vehicle.camera.sin_shoulder_channel = atoi(argv[1]);
    vehicle.camera.cos_shoulder_channel = atoi(argv[2]);
    vehicle.camera.offset_shoulder = atof(argv[3]);
    /* the following are the top input channels */
    vehicle.camera.command_shoulder_channel_top = atoi(argv[4]);
    vehicle.camera.master_shoulder = atof(argv[5]);
    vehicle.camera.feedback_shoulder = atof(argv[6]);
    vehicle.camera.shoulder_servo_channel_bot = atoi(argv[7]);
    vehicle.camera.p_shoulder = atof(argv[8]);
    vehicle.camera.i_shoulder = atof(argv[9]);
    vehicle.camera.d_shoulder = atof(argv[10]);

/* the following are bottom output channels */
    vehicle.camera.sin_pan_channel = atoi(argv[11]);
    vehicle.camera.cos_pan_channel = atoi(argv[12]);
    vehicle.camera.offset_pan = atof(argv[13]);
    /* the following are the top input channels */
    vehicle.camera.command_pan_channel_top = atoi(argv[14]);
    vehicle.camera.master_pan = atof(argv[15]);
    vehicle.camera.feedback_pan = atof(argv[16]);
    vehicle.camera.pan_servo_channel_bot = atoi(argv[17]);
    vehicle.camera.p_pan = atof(argv[18]);
    vehicle.camera.i_pan = atof(argv[19]);
    vehicle.camera.d_pan = atof(argv[20]);

/* the following are bottom output channels */
    vehicle.camera.sin_tilt_channel = atoi(argv[21]);
    vehicle.camera.cos_tilt_channel = atoi(argv[22]);
    vehicle.camera.offset_tilt = atof(argv[23]);
    /* the following are the top input channels */
    vehicle.camera.command_tilt_channel_top = atoi(argv[24]);
    vehicle.camera.master_tilt = atof(argv[25]);
    vehicle.camera.feedback_tilt = atof(argv[26]);
    vehicle.camera.tilt_servo_channel_bot = atoi(argv[27]);
    vehicle.camera.p_tilt = atof(argv[28]);
    vehicle.camera.i_tilt = atof(argv[29]);
    vehicle.camera.d_tilt = atof(argv[30]);

//-----------------------------
/* command_shoulder element */
//-----------------------------
    int surfVehicle = 1;
    int digAnalog = 0;
    int element_num = command_shoulder_channel_cam;
    string element_name("cam - shoulder command");
    string defaults("");		// removed	
    int default_level = 0;	
    bool enable_channel = true;
    bool auto_calibrate_enable = true;
    bool enable_graphics = true;	// removed
    int digital_value = 0;
    float analog_value = 0.0;
    float analog_raw = 0.0;
    float analog_offset = 0.0;
    float gain = 0.0;			// GAIN
    float max = 0.0;			// MAX_ANALOG
    float min = 0.0;			// MIN_ANALOG
    string graphic_type("NONE");
    int color = 0;			// removed
    int x = 0;
    int y = 0;
    int vga_screen = 1;

  IOElement command_shoulder;
  command_shoulder.make( surfVehicle, digAnalog, element_num, element_name, defaults, default_level, enable_channel,
		   auto_calibrate_enable, enable_graphics, digital_value, analog_value, analog_raw,
		   analog_offset, gain, max, min, graphic_type, color, x, y, vga_screen );

  // If element has already exist, do not overwrite it
  if(ioElementControl.inDataBase(command_shoulder))
  	{
	sprintf(result, "%d", -1);
	interp->result = result;
	}
  else  if(ioElementControl.pushElement(command_shoulder) == false) sprintf(result, "%d", -1);
	

//-----------------------------
/* command_pan element */
//-----------------------------
    element_num = command_pan_channel_cam;
    element_name = "cam - pan command";

  // create a new element
  IOElement command_pan;
  command_pan.make( surfVehicle, digAnalog, element_num, element_name, defaults, default_level, enable_channel,
		   auto_calibrate_enable, enable_graphics, digital_value, analog_value, analog_raw,
		   analog_offset, gain, max, min, graphic_type, color, x, y, vga_screen );

  if(ioElementControl.inDataBase(command_pan))
  	{
  	sprintf(result, "%d", -1);
	interp->result = result;
	}
  else 	if(ioElementControl.pushElement(command_pan) == false) sprintf(result, "%d", -1);
  	

//-----------------------------
/* command_tilt element */
//-----------------------------
    element_num = command_tilt_channel_cam;
    element_name = "cam - tilt command";

  // create a new element
  IOElement command_tilt;
  command_tilt.make( surfVehicle, digAnalog, element_num, element_name, defaults, default_level, enable_channel,
		   auto_calibrate_enable, enable_graphics, digital_value, analog_value, analog_raw,
		   analog_offset, gain, max, min, graphic_type, color, x, y, vga_screen );

    // if the element has not been created yet	
    if(ioElementControl.inDataBase(command_tilt))
  	{
	sprintf(result, "%d", -1);
	interp->result = result;
	}
    else  if(ioElementControl.pushElement(command_tilt) == false) sprintf(result, "%d", -1);
  	
    //cout <<"commands.c: the camStr is "<<vehicle.camera.camStr<<endl;

    	extern int ack_cam;
    	int loop_timeout = 0;
    	ack_cam = 0;

    	tty_send_request(vehicle.append_cam_string());
    	usleep(400000);
    	cout << "commands.c : Ack Cam Setup" << ack_cam << endl;
	while(ack_cam == 0)
		{
		tty_send_request(vehicle.append_cam_string());
		usleep(400000);
		cout << "commands.c : Ack Cam Setup" << ack_cam << endl;
		loop_timeout ++;
	    	if (loop_timeout == 10)
    			{
	    		ack_cam = 2;
	    		alarm_obj.set_other_alarm(40); //generate alarm
    			}
		}

    interp->result = result;
    return TCL_OK;
}

/****************** graphic accessor functions *******************************/
int get_compass_info_cmd (ClientData clientData,  Tcl_Interp *interp, int argc,	char *argv[])	
{
    // pass in 0 for no reset, 1 for reset
    if ( argc != 2 ) return ArgError(argv[0], interp);
    sprintf(result, "%d", 0);
    interp->result = result;
    return TCL_OK;
}


int get_vehicle_info_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    sprintf(result, "%d", 0);
    interp->result = result;
    return TCL_OK;
}


int get_depth_graph_info_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    sprintf(result, "%d", 0);
    interp->result = result;
    return TCL_OK;
}


int auto_calibrate_max_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
   if ( argc != 5 ) return ArgError(argv[0], interp);

   int status;
   int surfVehicle = atoi(argv[1]);
   int digAnalog = atoi(argv[2]); 
   int element_num = atoi(argv[3]);
   float max_analog = atof(argv[4]);

   status = ioElementControl.autoCalibrateMax(surfVehicle, digAnalog, element_num, max_analog);
   sprintf(result, "%d", status);
   interp->result = result;
   return TCL_OK;
}


int auto_calibrate_min_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
   if ( argc != 5 ) return ArgError(argv[0], interp);

   int status;
   int surfVehicle = atoi(argv[1]);
   int digAnalog = atoi(argv[2]); 
   int element_num = atoi(argv[3]);
   float min_analog = atof(argv[4]);

   status = ioElementControl.autoCalibrateMin(surfVehicle, digAnalog, element_num, min_analog);
   sprintf(result, "%d", status);
   interp->result = result;
   return TCL_OK;
}

/*======================================================================*
 * ret_io_elem_value_cmd				
 *					
 * This function will send an ioelement and push it into the DB
 * more comments added later...
 *======================================================================*/
int ret_io_elem_value_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	/* surfVehicle: 	0 - vehicle
    	* 		   	1 - surface
    	* digitalAnalog:	0 - Analog
    	* 			1 - digital
    	*/

   	int 	digitalValue;
   	float	analogValue;
   	float	analogRaw;
   	float	gain;
   	float	offset;
   	float 	max;
   	float 	min;
   	IOElement    tempElement;

  	if ( argc != 4 ) return ArgError(argv[0], interp);

   	int surfVehicle = atoi(argv[1]);
  	int digAnalog = atoi(argv[2]);
   	int element_num = atoi(argv[3]);

   	// retrieving digital value
   	digitalValue = -1;
   	digitalValue = ioElementControl.readFromChannel(surfVehicle, digAnalog, element_num);
  	// retrieving analog value
  	if (!ioElementControl.readVoltage(surfVehicle, digAnalog, element_num, analogValue, analogRaw, gain, offset, max, min))
   		{
                analogValue = -100;
      		analogRaw = -100;
      		gain = 0.0;
      		offset = 0.0;
      		max = 0.0;
      		min = 0.0;
  		}
   	snprintf(result, 512, "%d|%f|%f|%f|%f|%f|%f", digitalValue, analogValue, analogRaw, gain, offset, max, min);
   	interp->result = result;
   	return TCL_OK;
}

/*   removed aug 20 2004 Dave
int ret_auto_pilot_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
 	return TCL_OK;
}
*/

//Called by arm_control_popup and arm_control_popup2
int ret_arm_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
	snprintf(result, 1024, "%s", ioElementControl.get_master_feedback_arm());
	interp->result = result;
 	return TCL_OK;
}

//Called by cam_control_popup and cam_control_popup2
int ret_camera_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
	snprintf(result, 1024, "%s", ioElementControl.get_master_feedback_cam());
   	interp->result = result;
 	return TCL_OK;
}


int get_xy_pos_cmd  ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 3 ) return ArgError(argv[0], interp);

   int elem_num = atoi(argv[1]);
   int screen = atoi(argv[2]);

   // it is a FOG compass graph
   if (elem_num == FOG_GRAPH)
   	{
    	if (screen == 1) snprintf(result, 1024, "%d|%d", graph.comp1.x, graph.comp1.y );
    	else snprintf(result, 1024, "%d|%d", graph.comp2.x, graph.comp2.y  );
   	}
   // it is a Backup compass graph
   else if (elem_num == BACKUP_GRAPH)
   	{
    	if (screen == 1)snprintf(result, 1024, "%d|%d", graph.comp1.xb, graph.comp1.yb );
   	else snprintf(result, 1024, "%d|%d", graph.comp2.xb, graph.comp2.yb );
   	}
   // it is bar type
   else snprintf(result, 1024, "%s", ioElementControl.get_xy_pos(elem_num, screen) );
   interp->result = result;
   return TCL_OK;
}

//Called by cam_control cam_popup and cam_popup2 to update data base
/***************************************************************/
int saving_cam_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);
      		
 	string str(argv[1]);
 	vehicle.camera.cam_string = str;
 	//cout <<"What is string here : "<<vehicle.camera.cam_string<<endl;
 	return TCL_OK;
}

//Called by arm_control_popup and arm_control_popup2 to update data base
int saving_arm_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);
    		
 	string str(argv[1]);
 	vehicle.arm.arm_string = str;
 	return TCL_OK;
}

//Called by auot_pilot_popup and auto_pilot_popup2 to update data base
int saving_auto_pilot_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);
      		
 	string str(argv[1]);
 	vehicle.autopilot.auto_pilot_string = str;
 	//cout <<"What is string here : "<<vehicle.autopilot.auto_pilot_string<<endl;
 	return TCL_OK;
}

// called by the file.tcl for initialise tcl variables
int loading_cam_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);

      	snprintf(result, 1024, "%s", vehicle.camera.cam_string.c_str());
        interp->result = result;
 	return TCL_OK;
}


// called by the file.tcl for initialise tcl variables
int loading_arm_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
      	snprintf(result, 1024, "%s", vehicle.arm.arm_string.c_str());
        interp->result = result;
 	return TCL_OK;
}

// called by the file.tcl for initialise tcl variables
int loading_auto_pilot_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
 	snprintf(result, 1024, "%s", vehicle.autopilot.auto_pilot_string.c_str());
        interp->result = result;
 	return TCL_OK;
}


int set_light_conn_db_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);
 	string temp(argv[1]);            	
        graph.output_light_string = temp;
        //cout <<"commands.c: output_light_string " <<graph.output_light_string<<endl;
 	return TCL_OK;
}


int get_light_conn_db_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
     	snprintf(result, 1024, "%s", graph.output_light_string.c_str());
        interp->result = result;
 	return TCL_OK;
}


int Tool_select_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
   int ret;

   if ( argc != 2 ) return ArgError(argv[0], interp);
	
   int selection = atoi(argv[1]);

   if ((ret = ioElementControl.Tool_select(selection)) < 0) sprintf(result, "%d", -1);
   else sprintf(result, "%d", 0);
   interp->result = result;
   return TCL_OK;
}


int get_auto_setpt_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
   int ret;

   if ( argc != 1 ) return ArgError(argv[0], interp);
   sprintf(result, "%4.2f", auto_heading_setpoint);
   interp->result = result;
   return TCL_OK;
}


int get_auto_head_status_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    snprintf(result, 20, "%d", ioElementControl.get_auto_head_status());   	
    interp->result = result;
    return TCL_OK;
}


int get_auto_depth_status_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    snprintf(result, 20, "%d", ioElementControl.get_auto_depth_status());   	
    interp->result = result;
    return TCL_OK;
}


int get_auto_altitude_status_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    snprintf(result, 20, "%d", ioElementControl.get_auto_altitude_status());   	
    interp->result = result;
    return TCL_OK;
}


int get_auto_depth_setpt_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    snprintf(result, 128, "%4.1f", auto_depth_setpoint);   	
    interp->result = result;
    return TCL_OK;
}


int sending_camera_enable_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 2 ) return ArgError(argv[0], interp);

    int status = atoi(argv[1]);
    extern int ack_cam_enable;
    int loop_timeout = 0;
    ack_cam_enable = 0;

    vehicle.camera.camera_enable = status;
    cout <<"commands.c : sending the camera enable : "<<vehicle.camera.camera_enable<<endl;
    	// sending the camera info
    snprintf(result, 1024, "%s\n",vehicle.camera.camStr);
    tty_send_request(result);

	//sending the enable camera
    snprintf(result, 1024, "%d|%d\n", SET_CAM_ENABLE, vehicle.camera.camera_enable);
    tty_send_request(result);
    usleep(400000);
    cout << "commands.c : Ack Cam Enable" << ack_cam_enable << endl;
    while(ack_cam_enable == 0)
    	{
    	tty_send_request(result);
    	usleep(400000);
    	cout << "commands.c : Ack Cam Enable" << ack_cam_enable << endl;
    	loop_timeout ++;
    	if (loop_timeout == 10)
    		{
    		alarm_obj.set_other_alarm(31); //generate alarm
    		ack_cam_enable = 2;
    		}
	}
    return TCL_OK;
}


int switch_arm_enable_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 2 ) return ArgError(argv[0], interp);

    int status = atoi(argv[1]);
	
    // This will update the "enable_channel" button on the BPM
    int arm_enab = ioElementControl.switch_arm_enable(status);
    //cout<<"commands.c: The arm enable : " <<arm_enab<<endl;
    return TCL_OK;
}


int get_sit_camera_angle_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    snprintf(result, 1024, "%s", ioElementControl.get_sit_camera_angle());   	
    interp->result = result;
    return TCL_OK;
}

//******************************************************************************
// This function will return the number that are going to be displayed
// as NUMBERS in vehicle display. Therefore it has to include the
// current heading for the pan, and the current pitch of the vechile
// for the tilt
//******************************************************************************
int get_sony_camera_angle_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 1 ) return ArgError(argv[0], interp);

    // The feedback_pan and feedback_tilt is substracted by 180
    float pan = vehicle.camera.feedback_pan + vehicle.curr_heading;
    float tilt = vehicle.camera.feedback_tilt + vehicle.curr_pitch + vehicle.camera.feedback_shoulder;
    float shoulder = vehicle.camera.feedback_shoulder;

    snprintf(result, 1024, "%4.1f|%4.1f|%4.1f", pan , tilt, shoulder);   	
    interp->result = result;
    return TCL_OK;
}

//******************************************************************************
// This function is different from the above, because we just want to return
// the pan and tilt of the sony camera only, since the way we draw the camera
// in the vehicle that has already include the pitch and heading of the
// vehicle. Therefore, there is no need to add it again in our display
// This functon is called by the file "vehicle.tcl"
//******************************************************************************
int get_sony_camera_display_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])	
{
    if ( argc != 1 ) return ArgError(argv[0], interp);

    float pan = vehicle.camera.feedback_pan ;
    float tilt = vehicle.camera.feedback_tilt ;//+ vehicle.camera.feedback_shoulder;
    float shoulder = vehicle.camera.feedback_shoulder;

    snprintf(result, 1024, "%4.1f|%4.1f|%4.1f", pan , tilt, shoulder);   	
    interp->result = result;
    return TCL_OK;
}

int enabling_channel_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 5 )return ArgError(argv[0], interp);

    int surveh = atoi(argv[1]);
    int diganal = atoi(argv[1]);
    int elem_num = atoi(argv[1]);
    int status = atoi(argv[1]);

    if (status == 0) ioElementControl.send_output_down(surveh, diganal, elem_num, 0, 0.0);
    return TCL_OK;
}


int get_vehicle_act_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
      	
    float port = vehicle.port_thruster;
    float stbd = vehicle.stbd_thruster;
    float vertical = vehicle.vert_thruster;
    float lateral = vehicle.lat_thruster;
      	
    snprintf(result, 1024, "%4.2f|%4.2f|%4.2f|%4.2f", port, stbd, vertical, lateral );
    interp->result = result;
    return TCL_OK;
}


int get_telemetry_status_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    snprintf(result, 1024, "%d", telemetry_status );
    interp->result = result;
    return TCL_OK;
}


//---------------------------------------------------------------------
int bad_packet_counter_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    snprintf(result, 1024, "%d", telemetryObj.bad_packet_counter );
    interp->result = result;
    return TCL_OK;
}


// This function is a testing function to test the defaults
/* //The funcion is in Telemetry.cpp and is commented out
int generate_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 1 ) return ArgError(argv[0], interp);
    tty_lineout("GENERATE_ERR\n");  	
    return TCL_OK;
}
*/

int switch_teleos_enable_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   char temp[12];
   extern int ack_teleos_enable;
   int loop_timeout = 0;
   ack_teleos_enable = 0;

   if ( argc != 2 ) return ArgError(argv[0], interp);
   teleos_enable = atoi(argv[1]);
   cout<<"commands.c : teleos is : " <<teleos_enable<<endl;

   snprintf(temp, 12, "%d|%d\n", 10, teleos_enable);
   tty_send_request(temp);
   usleep(400000);
   cout << "commands.c : Ack Teleos Enable" << ack_teleos_enable << endl;
   while(ack_teleos_enable == 0)
   	{
   	tty_send_request(temp);
   	usleep(400000);
   	cout << "commands.c : Ack Teleos Enable" << ack_teleos_enable << endl;
	loop_timeout ++;
	if (loop_timeout == 10)
		{
		alarm_obj.set_other_alarm(27); //generate alarm
		ack_teleos_enable = 2;
		}
	}
   sprintf(result, "%d", teleos_enable);
   interp->result = result;
   return TCL_OK;
}


int set_tms_display_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 4 ) return ArgError(argv[0], interp);

   int screen = atoi(argv[3]);

   if (screen == 1)
	{
	graph.tmsx1 = atoi(argv[1]);
	graph.tmsy1 = atoi(argv[2]);
 	}
   else
    	{
       	graph.tmsx2 = atoi(argv[1]);
       	graph.tmsy2 =  atoi(argv[2]);
     	}
   return TCL_OK;
}


int get_tms_display_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 100, "%d|%d|%d|%d", graph.tmsx1, graph.tmsy1, graph.tmsx2, graph.tmsy2);
   interp->result = result;
   return TCL_OK;
}


int check_bypass_status_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   // get the digital value from surface ( 1 ) and element num #39
   snprintf(result, 100, "%d", ioElementControl.get_digital_value(1, 39));
   interp->result = result;
   return TCL_OK;
}


int get_altitude_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   if (vehicle.autopilot.AltitudeSource == 0) snprintf(result, 100, "%4.1f", ioElementControl.get_analog_value(0, 17));
   else if (vehicle.autopilot.AltitudeSource == 1) snprintf(result, 100, "%4.1f", ioElementControl.get_analog_value(0, 32));
   else if (vehicle.autopilot.AltitudeSource == 2) snprintf(result, 100, "%4.1f", vehicle.dvl_altitude);
   interp->result = result;
   return TCL_OK;
}


int get_8_ground_value_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    	if ( argc != 2 ) return ArgError(argv[0], interp);

        int channel = atoi(argv[1]);
        char temp[100];

    	for (int i = 41; i <= 43; i++)
	    	{
    		memset(temp, 0, 100);
    		// board, elem_num, state
   	 	snprintf(temp, 100, "%d|%d|%d\n", 1, i, 0);
      		tty_send_request(temp);
      		}
    	// Now, turn on the channel selected manually
    	// TURN CHANNEL 43
    	if ( channel >= 5 && channel <= 8  )
	    	{
    		cout <<"commands.c : turning on channel 43\n";
    		memset(temp, 0, 100);
    		snprintf(temp, 100, "%d|%d|%d\n", 1, 43, 1);
      		tty_send_request(temp);
    		}	
   	// TURN CHANNEL 42
    	if ( (channel % 4) == 0 || channel == 3 ||  channel == 7  )
	    	{
    		cout <<"commands.c : turning on channel 42\n";
    		memset(temp, 0, 100);
    		snprintf(temp, 100, "%d|%d|%d\n", 1, 42, 1);
      		tty_send_request(temp);
    		}	
  	// TURN CHANNEL 41
    	if ( (channel % 2) == 0 )
	    	{
    		cout <<"commands.c : turning on channel 41\n";
    		memset(temp, 0, 100);
    		snprintf(temp, 100, "%d|%d|%d\n", 1, 41, 1);
      		tty_send_request(temp);
    		}	
    	// read it from input sub analog 37
   snprintf(result, 100, "%4.3f", ioElementControl.get_analog_value(0, 37));
   interp->result = result;
   return TCL_OK;
}


int select_ground_mode_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   int mode = atoi(argv[1]);
   char temp[50];
   extern int ack_gf_select;
   int loop_timeout = 0;
   ack_gf_select = 0;

   cout <<"commands.c : Ground Mode  = "<<mode<<endl;

   // mode = 6 -> SELECT_MAN_GF
   // mode = 7 -> SELECT_AUTO_GF

   snprintf(temp, 50, "%d\n", mode);
   tty_send_request(temp);
   usleep(400000);
   cout << "commands.c : Ack GF Select" << ack_gf_select << endl;
   while(ack_gf_select == 0)
	{
	tty_send_request(temp);
	usleep(400000);
	cout << "commands.c : Ack GF Select" << ack_gf_select << endl;
	loop_timeout ++;
    	if (loop_timeout == 10)
    		{
    		alarm_obj.set_other_alarm(24); //generate alarm
    		ack_gf_select = 2;
    		}
	}
   return TCL_OK;
}


int get_16_ground_value_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
    if ( argc != 3 ) return ArgError(argv[0], interp);

    int mode = atoi(argv[1]);
    int channel = atoi(argv[2]);
    char temp[100];

    // ------------- MANUAL MODE -------------------
    if (mode == SELECT_MAN_GF)
    	{
        // check if the user select different channel to be displayed
    	// then set the status to be 1 so that we wlll turn on diff channel
    	if (old_16_channel != channel )	
    		{
    		ground_fault_16_status = 1;
    		old_16_channel = channel;
    		}	
    	//cout <<"commands.c : selecting manual GF\n";
    	// if we have already turn the channels on, jsut
    	// get the ground value and return
    	if (ground_fault_16_status == 0)
    		{
    		ground_value = ioElementControl.get_analog_value (0,38);
    		snprintf(result, 100, "%4.3f",  ground_value);
    		interp->result = result;
    		return TCL_OK;
    		}
    	
    	// Turn off  all the 4 MPL channels which are not selected
    	for (int i = 45; i <= 48; i++)
    		{
    		memset(temp, 0, 100);
    		// board, elem_num, state
   	 	snprintf(temp, 100, "%d|%d|%d\n", 1, i, 0);
      		tty_send_request(temp);
      		}
      	// Now, turn on the channel selected manually
    	// TURN CHANNEL 48
    	if (channel >= 9 ) // if(channel & 0x08)
    		{
    		cout <<"commands.c : turning on channel 48\n";
    		memset(temp, 0, 100);
    		snprintf(temp, 100, "%d|%d|%d\n", 1, 48, 1);
      		tty_send_request(temp);
    		}	
    	// TURN CHANNEL 47
    	if ( (channel >= 5 && channel <= 8 ) || channel >= 13 )
    		{
    		cout <<"commands.c : turning on channel 47\n";
    		memset(temp, 0, 100);
    		snprintf(temp, 100, "%d|%d|%d\n", 1, 47, 1);
      		tty_send_request(temp);
    		}	
    	// TURN CHANNEL 46
    	if ( (channel % 4) == 0 || channel == 3 ||  channel == 7 || channel == 11 || channel == 15  )
    		{
    		cout <<"commands.c : turning on channel 46\n";
    		memset(temp, 0, 100);
    		snprintf(temp, 100, "%d|%d|%d\n", 1, 46, 1);
      		tty_send_request(temp);
    		}	
    	// TURN CHANNEL 45
    	if ( (channel % 2) == 0 )
	    	{
    		cout <<"commands.c : turning on channel 45\n";
    		memset(temp, 0, 100);
    		snprintf(temp, 100, "%d|%d|%d\n", 1, 45, 1);
      		tty_send_request(temp);
    		}	
    	//---------------------------------------------------------
    	// set the global status to be 1 so we wont keep on
    	// turning on the channels all the time
    	//---------------------------------------------------------
    	ground_fault_16_status = 0;
    	
        // After we turn on the channel, read the analog value from channel 38
        // and return the value as ground value to be displayed in GUI
    	ground_value = ioElementControl.get_analog_value (0,38);
    	snprintf(result, 100, "%4.3f",  ground_value);
    	}

    // else, it is automatic mode, we dont have to worry about turning on/off
    // channel
    else
    	{
    	//cout <<"commands.c : selecting automatic mode\n";
    	ground_fault_16_status = 1;
    	snprintf(result, 100, "%d|%4.3f", ground_channel, ground_value);
    	}
    //cout <<"commands.c: result : "<<result<<endl;
    interp->result = result;
    return TCL_OK;
}

//***************************************************************
// This procedure will set the dive number that is going to be
// sent to logging data and teleos system
//***************************************************************

int set_dive_num_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);
   // this variable "dive_num" is initialized and declared
   // inside "telemetry_string.h"
   dive_num = atoi(argv[1]);
   cout <<"commands.c : setting dive number : "<<dive_num<<endl;
   return TCL_OK;
}

// Kenny - added whole function Nov. 18 2002
//***************************************************************
// This procedure will get the dive number that is going to be
// sent to logging data and teleos system
//***************************************************************
int get_dive_num_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   // this variable "dive_num" is initialized and declared
   // inside "telemetry_string.h"
   snprintf(result, 100, "%d", dive_num );
   interp->result = result;
   return TCL_OK;
}


//***************************************************************
// This procedure will set the compass and fog declination
//
//***************************************************************
int set_compass_deviation_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 5 ) return ArgError(argv[0], interp);

   // these variables are initialized and declared
   // inside "configuration.cpp"

   deviation_fog = atoi(argv[1]);
   deviation_compass = atoi(argv[2]);
   declination_fog = atoi(argv[3]);
   declination_compass = atoi(argv[4]);
   return TCL_OK;
}

// Dave - added whole function Mar. 4 2003
//***************************************************************
// This procedure will get the deviations for the compass and fog
//***************************************************************
int get_compass_deviation_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   // these variables are initialized and declared
   // inside "configuration.cpp"
   snprintf(result, 100, "%d|%d|%d|%d", deviation_fog, deviation_compass, declination_fog, declination_compass );
   interp->result = result;
   return TCL_OK;
}



//***************************************************************
// This procedure will set the RPM interval that is going to be
// sent to logging data and teleos system. Maybe; Dave 10/10/02
//***************************************************************
int set_rpm_interval_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   extern int ack_rpm_setup;
   int loop_timeout = 0;
   ack_rpm_setup = 0;

   // this variable "rpm_interval" is initialized and declared
   // inside "telemetry_string.h Dave 10/10/02"
   // set rpm interval in vehicle for teleos

   vehicle.rpm_interval = atoi(argv[1]);
   snprintf(tx_buffer, 1024, "%d|%d\n", RPM_SETUP, vehicle.rpm_interval);
   tty_send_request(tx_buffer);
   cout <<"commands.c : Setting RPM Interval : "<<tx_buffer<<endl;
   usleep(400000);
   cout << "commands.c : Ack RPM Setup" << ack_rpm_setup << endl;
   while(ack_rpm_setup == 0)
	{
	tty_send_request(tx_buffer);
	usleep(400000);
	cout << "commands.c : Ack RPM Setup" << ack_rpm_setup << endl;
	loop_timeout ++;
    	if (loop_timeout == 10)
    		{
    		alarm_obj.set_other_alarm(29); //generate alarm
    		ack_rpm_setup = 2;
    		}
	}
   return TCL_OK;
}



//***************************************************************
// This procedure will get the RPM interval that is going to be
// sent to logging data and teleos system. Kenny Nov.18 2002 
//***************************************************************

int get_rpm_interval_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 100, "%d", vehicle.rpm_interval );
   interp->result = result;
   return TCL_OK;
}

//***************************************************************
// This procedure will get the RPM speed that is going to be
// sent to logging data and teleos system. Maybe; Dave 10/10/02
//***************************************************************
int get_rpm_speed_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 100, "%d", vehicle.rpm_speed );
   interp->result = result;
   return TCL_OK;
}


int get_aux_heading_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   float angle;
   /*
   tester_angle -= 10;
   if(tester_angle < 0) tester_angle = 360;
   snprintf(result, 100, "%4.1f", tester_angle );
   interp->result = result;
   return TCL_OK;
   */
   // First of all we need to check if the KVH switch (MPL 44) is On / OFF	
   int state = ioElementControl.get_but_state(1, 44);

   if (state == 0)  angle = 999;
   else
	{	
        // Sine and cosine analog value are from channel 41 and 42 from sub
   	float input_sin = ioElementControl.get_analog_value(0, 41 );
   	float input_cos = ioElementControl.get_analog_value(0, 42 );

   	if(input_sin == 0) input_sin = .001;  //Limit sine and Cosine
   	if(input_cos == 0) input_cos = .001;
   	if(input_sin >  1) input_sin = 1;
   	if(input_sin < -1) input_sin = -1;
   	if(input_cos >  1) input_cos = 1;
   	if(input_cos < -1) input_cos = -1;

   	angle = atan(input_sin/input_cos)*360/6.28;
   	
   	if(input_cos < 0) angle = 180 + angle; 			// 90 to 270
   	if(input_sin < 0 && input_cos > 0) angle = 360 + angle;   //270 to 360
	}
   backup_heading = angle;
   snprintf(result, 100, "%4.1f", angle );
   interp->result = result;
   return TCL_OK;
}


int get_ov_count_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 100, "%d", graph.ov_count );
   interp->result = result;
   return TCL_OK;
}


int get_overlay_info_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   int index = atoi(argv[1]);

   snprintf(result, 512, "%s", graph.get_overlay_info(index) );
   interp->result = result;
   return TCL_OK;
}


int get_overlay_channel_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   string name(argv[1]);

   snprintf(result, 512, "%d", ioElementControl.get_overlay_channel(name) );
   interp->result = result;
   return TCL_OK;
}

// SUB OUTPUT
// This function get the digital status coming UP from the sub
int get_digital_status_cmd  (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 3 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);

   //cout <<"commmands.c : elem_num for digital status : "<<elem_num<<endl;
   // Gesout -> 64 channel s
   // (board == 0)
   
   if (board == 0)	
	{
  	if (elem_num > 64 || elem_num < 0 ) snprintf(result, 100, "%d", -1);	// out of range
   	else snprintf(result, 100, "%d", vehicle.gesout_digitals[elem_num] );
   	}
   // mpl -> 48 channels
   // (board == 1)
   else
   	{
   	if (elem_num > 48 || elem_num < 0 ) snprintf(result, 100, "%d", -1);		// out of range
	else snprintf(result, 100, "%d", vehicle.mpl_digitals[elem_num] );
   	}
   interp->result = result;
   return TCL_OK;
}


int get_button_pos_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 4 ) return ArgError(argv[0], interp);

   int board = atoi(argv[1]);
   int elem_num = atoi(argv[2]);
   int screen = atoi(argv[3]);

   strcpy(result, ioElementControl.get_button_pos(board, elem_num, screen) );
   interp->result = result;
   return TCL_OK;
}


int get_slidebar_pos_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 3 ) return ArgError(argv[0], interp);

   int elem_num = atoi(argv[1]);
   int screen = atoi(argv[2]);

   strcpy(result, ioElementControl.get_slidebar_pos(elem_num, screen) );
   interp->result = result;
   return TCL_OK;
}


int set_teleos_setup_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 18 ) return ArgError(argv[0], interp);

   vehicle.teleos.kf1_foreaft = atof(argv[1]);
   vehicle.teleos.kf1_turns = atof(argv[2]);
   vehicle.teleos.kf1_lateral = atof(argv[3]);
   vehicle.teleos.kf1_vertical = atof(argv[4]);

   vehicle.teleos.kf2_foreaft = atof(argv[5]);
   vehicle.teleos.kf2_turns = atof(argv[6]);
   vehicle.teleos.kf2_lateral = atof(argv[7]);
   vehicle.teleos.kf2_vertical = atof(argv[8]);

   vehicle.teleos.db_foreaft = atof(argv[9]);
   vehicle.teleos.db_turns = atof(argv[10]);
   vehicle.teleos.db_lateral = atof(argv[11]);
   vehicle.teleos.db_vertical = atof(argv[12]);

   vehicle.teleos.st_foreaft = atof(argv[13]);
   vehicle.teleos.st_turns = atof(argv[14]);
   vehicle.teleos.st_lateral = atof(argv[15]);
   vehicle.teleos.st_vertical = atof(argv[16]);

   vehicle.teleos.curve_mode = atoi(argv[17]);

   //-----------------------------------------------------
   // Save it to database as a single string (used for
   // writing and reading config file )
   //-----------------------------------------------------

   snprintf(vehicle.teleos.teleos_string, 1024, "%4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %d",
   	vehicle.teleos.kf1_foreaft,
   	vehicle.teleos.kf1_turns,
   	vehicle.teleos.kf1_lateral,
   	vehicle.teleos.kf1_vertical,
   	vehicle.teleos.kf2_foreaft,
   	vehicle.teleos.kf2_turns,
   	vehicle.teleos.kf2_lateral,
   	vehicle.teleos.kf2_vertical,
   	vehicle.teleos.db_foreaft,
   	vehicle.teleos.db_turns,
   	vehicle.teleos.db_lateral,
   	vehicle.teleos.db_vertical,
   	vehicle.teleos.st_foreaft,
   	vehicle.teleos.st_turns,
   	vehicle.teleos.st_lateral,
   	vehicle.teleos.st_vertical,
   	vehicle.teleos.curve_mode );

   	//cout<<"teleos_string : "<<vehicle.teleos.teleos_string<<endl;

	ioElementControl.initializeLookup();
     	return TCL_OK;
}

/* Returns a srting with the teleos setup parameters such as thruster curves and constants
*/
int get_teleos_info_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   interp->result = vehicle.teleos.teleos_string;
   return TCL_OK;
}


int send_default_down_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 4 ) return ArgError(argv[0], interp);

   int surfVeh = atoi(argv[1]);
   int digAnal = atoi(argv[2]);
   int elem_num = atoi(argv[3]);

   ioElementControl.send_default_down(surfVeh, digAnal, elem_num);
   return TCL_OK;
}

// This function get the digital value from the top (polling)
int get_digital_value_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 3 ) return ArgError(argv[0], interp);

   int surveh = atoi(argv[1]);
   int elem_num = atoi(argv[2]);

   // get the digital value from surface/vehicle (1/0) and element num
   //snprintf(result, 100, "%d", ioElementControl.get_digital_value(surveh, elem_num));
   snprintf(result, 100, "%d", ioElementControl.readFromChannel(surveh, 1, elem_num));
   interp->result = result;
   return TCL_OK;
}


int get_analog_value_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	if ( argc != 3 ) return ArgError(argv[0], interp);

	int surveh = atoi(argv[1]);
	int elem_num = atoi(argv[2]);

   	snprintf(result, 512, "%4.1f", ioElementControl.get_analog_value(surveh, elem_num));
   	interp->result = result;
	return TCL_OK;
}

/* ADDED: Apr. 10, 2003.
 * function to return the top analog value, 
 * Used by list_all_element.tcl 
 */
int get_top_analog_value_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	float analog, raw, gain, offset, max, min;
	
   	if ( argc != 4 ) return ArgError(argv[0], interp);

	int surveh = atoi(argv[1]);
	int dig_anal = atoi(argv[2]);
	int elem_num = atoi(argv[3]);

	ioElementControl.readVoltage(surveh, dig_anal, elem_num, analog, raw, gain, offset, max, min);
	snprintf(result, 512, "%4.1f", analog);
   	interp->result = result;
	return TCL_OK;
}

/* ADDED: Apr. 10, 2003.
 * Useful function to return the raw analog value, 
 * Used by list_all_element.tcl 
 */
int get_raw_analog_value_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	float analog, raw, gain, offset, max, min;

   	if ( argc != 4 ) return ArgError(argv[0], interp);

	int surveh = atoi(argv[1]);
	int dig_anal = atoi(argv[2]);
	int elem_num = atoi(argv[3]);

	ioElementControl.readVoltage(surveh, dig_anal, elem_num, analog, raw, gain, offset, max, min);
	snprintf(result, 512, "%4.1f", raw);
   	interp->result = result;
	return TCL_OK;
}


int manipulate_auto_cruise_setpt_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	char command[100];
   	extern int ack_auto_cruise;
   	int loop_timeout = 0;
   	if ( argc != 4 ) return ArgError(argv[0], interp);

	// factor can be positive if the user choose to increment
	// and negative if decremented
    	float factor = atof(argv[1]);
    	auto_cruise_use_bottom_lock = atoi(argv[2]);
    	float setpoint = atof(argv[3])*100;
    	
        if(setpoint == 0) auto_cruise_setpoint = auto_cruise_setpoint + factor;
        else auto_cruise_setpoint = setpoint;
        
	/*
	snprintf(command, 100,"%d|%d|%d\n", AUTO_CRUISE_, auto_cruise_use_bottom_lock, globalAutoCruiseOn);
	tty_send_request(command);
	usleep(400000);
	while(ack_auto_cruise == 0)
	   	{
	   	tty_send_request(command);
	   	usleep(400000);
	   	loop_timeout ++;
	   	if (loop_timeout == 10)
		   		{
		   		alarm_obj.set_other_alarm(34); //generate alarm
		   		ack_auto_cruise = 2;
		   		}
		}	
	*/
	snprintf(result, 512, "%4.2f", auto_cruise_setpoint/100 );
   	interp->result = result;
   	return TCL_OK;
}

int manipulate_auto_cruise_lat_setpt_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	char command[100];
   	extern int ack_auto_cruise;
   	int loop_timeout = 0;
   	if ( argc != 4 ) return ArgError(argv[0], interp);

	// factor can be positive if the user choose to increment
	// and negative if decremented
    	float factor = atof(argv[1]);
    	auto_cruise_use_bottom_lock = atoi(argv[2]);
    	float setpoint = atof(argv[3])*100;
    	
        if(setpoint == 0) auto_cruise_lat_setpoint = auto_cruise_lat_setpoint + factor;
        else auto_cruise_lat_setpoint = setpoint;
        
	/*
	snprintf(command, 100,"%d|%d|%d\n", AUTO_CRUISE_, auto_cruise_use_bottom_lock, globalAutoCruiseOn);
	tty_send_request(command);
	usleep(400000);
	while(ack_auto_cruise == 0)
	   	{
	   	tty_send_request(command);
	   	usleep(400000);
	   	loop_timeout ++;
	   	if (loop_timeout == 10)
		   		{
		   		alarm_obj.set_other_alarm(34); //generate alarm
		   		ack_auto_cruise = 2;
		   		}
		}	
	*/
	snprintf(result, 512, "%4.2f", auto_cruise_lat_setpoint/100 );
   	interp->result = result;
   	return TCL_OK;
}

int set_latitude_val_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   extern int ack_latitude_setup;
   int loop_timeout = 0;
   ack_latitude_setup = 0;

   // This value is declared and initialized inside "vehicle.cpp"
   latitude = atof(argv[1]);

   // send it down to the sub
   char temp [50];
   snprintf (temp, 50, "%d|%f\n", LATITUDE, latitude);
   cout <<"commands.c : latitude command = "<<temp;
   tty_send_request(temp);
   usleep(400000);
   cout << "commands.c : Ack Latitude Setup" << ack_latitude_setup << endl;
   while(ack_latitude_setup == 0)
	{
	tty_send_request(temp);
	usleep(400000);
	cout << "commands.c : Ack Latitude Setup" << ack_latitude_setup << endl;
    	loop_timeout ++;
    	if (loop_timeout == 10)
    		{
    		alarm_obj.set_other_alarm(28); //generate alarm
    		ack_latitude_setup = 2;
    		}
	}
   return TCL_OK;
}


int get_latitude_val_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   // This value is declared and initialized inside "vehicle.cpp"
   snprintf(result, 100, "%4.2f", latitude );
   interp->result = result;
   return TCL_OK;
}


int get_tms_info_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   snprintf(result, 1024, "%d %d %d %d",
   telstring.TmsControlStringIn.cable_out,
   telstring.TmsControlStringIn.cable_speed,
   telstring.TmsControlStringIn.tension,
   telstring.TmsControlStringIn.pressure );
   interp->result = result;                    	
   return TCL_OK;
}






int get_raw_depth_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   snprintf(result, 1024, "%4.1f", vehicle.curr_raw_psi );
   interp->result = result;                    	
   return TCL_OK;
}


int add_log_item_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 7 ) return ArgError(argv[0], interp);

	string name(argv[1]);
	int type = atoi(argv[2]);
	int channel = atoi(argv[3]);
	string filename(argv[4]);
	int time = atoi(argv[5]);
	int interval = atoi(argv[6]);
	
       	snprintf(result, 1024, "%d", telstring.add_log_item(name, type, channel, filename, time, interval) );
   	interp->result = result;
    	return TCL_OK;
}



int display_log_db_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);

	telstring.display_log_db() ;
   	return TCL_OK;
}


int delete_log_item_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);
	
	int index = atoi(argv[1]);
	
        snprintf(result, 1024, "%d", telstring.delete_log_item(index));
   	interp->result = result;
    	return TCL_OK;
}


int check_log_db_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);

	string name(argv[1]);
	
	// call "check_log_db" inside file "telemetry_string.cpp"
        snprintf(result, 1024, "%d", telstring.check_log_db(name) );
   	interp->result = result;
    	return TCL_OK;
}


int get_log_count_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{

	if ( argc != 1 ) return ArgError(argv[0], interp);
        snprintf(result, 1024, "%d", telstring.log_count );
   	interp->result = result;
    	return TCL_OK;
}

int get_log_info_cmd (ClientData clientData, Tcl_Interp *interp, int argc,	char *argv[])		
{

   	if ( argc != 2 ) return ArgError(argv[0], interp);

   	int index = atoi(argv[1]);

   	snprintf(result, 512, "%s", telstring.get_log_info(index) );
   	interp->result = result;
   	return TCL_OK;
}


int disk_logging_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	if ( argc != 1 ) return ArgError(argv[0], interp);

   	int		count;
   	int		status;
   	pthread_t	disk_logging_thread;

   	if ( argc != 1 ) return ArgError(argv[0], interp);
   	status = pthread_create (&disk_logging_thread, NULL, disk_logging , NULL);	
   	if (status != 0) err_abort (status, "Create Disk Logging Routine");
   	sprintf(result, "%d", 0);
   	interp->result = result;
   	return TCL_OK;
}


int subout_make_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	if ( argc != 1 ) return ArgError(argv[0], interp);
     	ioElementControl.subout_make();
  	return TCL_OK;
}


int download_init_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	static char* param[] = { "-e", "/usr/bin/minicom", "-S", "ventana_download_script", NULL};
   	if ( argc != 1 ) return ArgError(argv[0], interp);

    	char temp[10];
    	snprintf(temp, 10, "9892\n");
    	tty_send_request(temp);
    	usleep(100);
    	tty_send_request(temp);
    	execv("/usr/bin/kvt", param);
     	//ioElementControl.subout_make();
  	return TCL_OK;
}

int set_disk_enable_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	if ( argc != 2 ) return ArgError(argv[0], interp);
	//This variable "disk_logging_status" is declared and initialized in "telemetry_string.cpp"
    	disk_logging_status = atoi(argv[1]);
    	cout <<"commands.c: disk_logging_status : "<<disk_logging_status<<endl;
       	return TCL_OK;
}


int update_log_variables_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   	if ( argc != 4 ) return ArgError(argv[0], interp);
      	log_filename = string(argv[1]);
      	cout<<"new_filename is : "<<log_filename<<endl;
	if ( atoi(argv[2]) != 0 ) log_time = atoi(argv[2]);
	else log_time = 1;
	if ( atoi(argv[3]) != 0 ) log_interval = atoi(argv[3]);
	else log_interval = 100;
      	return TCL_OK;
}

// Getting the values for the DAC  that are needed for diagnostic display
// this will be called from file "list_all_element.tcl"
int get_sub_output_cmd (ClientData clientData, Tcl_Interp *interp, int argc,	 char *argv[])		
{
      	if ( argc != 2 ) return ArgError(argv[0], interp);

	int ind = atoi(argv[1]);
	
	// Note : Here, index of the array starts from 0 to 15
	if (ind >= 0 && ind <= 15)
		{        		
 		float val = vehicle.gesdac_output[ind];
		snprintf (result, 100, "%4.2f", val);
		interp->result = result;	
		}
     	return TCL_OK;
}


int delete_single_input_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
      	if ( argc != 2 ) return ArgError(argv[0], interp);

	int elem_num = atoi(argv[1]);
		
	ioElementControl.delete_single_input( elem_num );
	return TCL_OK;
}




int get_io_connection_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   string name(argv[1]);

   strcpy(result, ioElementControl.get_io_connection(name) );
   interp->result = result;
   return TCL_OK;
}


int get_disk_status_cmd ( ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf (result, 1024, "%d", disk_logging_status);
   interp->result = result;
   return TCL_OK;
}



int get_vehicle_speed_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 100, "%4.2f|%4.2f", vehicle.curr_vel/100, vehicle.curr_lat_vel/100 );
   interp->result = result;
   return TCL_OK;
}




int set_auto_cruise_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   char command[100];
   extern int ack_auto_cruise;
   int loop_timeout = 0;
   ack_auto_cruise = 0;

   if ( argc != 2 ) return ArgError(argv[0], interp);
   auto_cruise_use_bottom_lock = atoi(argv[2]);
   return 0;  //this is no longer called by anyone  	
   auto_cruise_setpoint = vehicle.curr_vel;
   auto_cruise_lat_setpoint = vehicle.curr_lat_vel;
   snprintf(command, 100, "%d|%d|%d\n", AUTO_CRUISE_, auto_cruise_use_bottom_lock, 1);
   tty_send_request(command);
   usleep(400000);
   cout << "commands.c : Ack Auto Cruise" << ack_auto_cruise << endl;
   while(ack_auto_cruise == 0)
   	{
   	tty_send_request(command);
   	usleep(400000);
   	cout << "commands.c : Ack Auto Cruise" << ack_auto_cruise << endl;
   	loop_timeout ++;
   	if (loop_timeout == 10)
   		{
   		alarm_obj.set_other_alarm(34); //generate alarm
   		ack_auto_cruise = 2;
   		}
	}
   snprintf(result, 100, "%4.2f|%4.2f", auto_cruise_setpoint, auto_cruise_lat_setpoint);
   interp->result = result;
   return TCL_OK;
}

//Returns the status of auto cruise button on or off
int get_auto_cruise_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   snprintf(result, 10, "%d", globalAutoCruiseOn);
   interp->result = result;
   return TCL_OK;
}


/**************************************************************
 * This function read serial devices
 * Currently, Ventana reads :
 * 	tms
 * 	vision system
 *	gyro
 *	overlayed serial string
**************************************************************/
void *serialSenseThread(void *arg)
{
//return 0;

        SerialDevice* tms = new TmsDevice("/dev/ttyS1", 0, 0);
	SerialDevice* visionSensor = new VisionDevice("/dev/ttyR3", 50, 2);
        SerialDevice* gyro = new GyroDevice("/dev/ttyR4", 0, 0);
        SerialDevice* overlay = new OverlayDevice("/dev/ttyR5", 0, 0);

  	tms->initSerialPort(0, 1, 2, 1, 1);          //baudrate, chSize, parity,nStopBits,loption
	visionSensor->initSerialPort(1, 1, 2, 1, 1); //same function called below with exact setup
        gyro->initSerialPort(0, 1, 2, 1, 0);         //Gyro is not newline terminated
        overlay->initSerialPort(0, 1, 2, 1, 1);      //all others set 1 as the last variable

	serialSensors.registerDev(tms);
	serialSensors.registerDev(visionSensor);
	serialSensors.registerDev(gyro);
	serialSensors.registerDev(overlay);
	
	serialSensors.runSensors();
	// will block forever
	return NULL;
}

/**************************************************************
 * This function actuate serial devices for TX
 * CUrrently, Ventana sends out to logging and vision system
 * depth NEMNA, ROV Heading NEMA, Ships heading NEMA
**************************************************************/
void *serialActuateThread(void *arg)
{
//return 0;
	SerialDevice* logging = new LoggingDevice("/dev/ttyR2", 0, 1);
	SerialDevice* vision =  new VisionDevice("/dev/ttyR3", 50, 2);
	//SerialDevice* depth =   new DepthDevice("/dev/ttyR5", 0, 1);
	SerialDevice* rovhead = new RovheadDevice("/dev/ttyR6", 0, 1);
	SerialDevice* shiphead= new ShipheadDevice("/dev/ttyR7", 0, 1);
	
        logging->initSerialPort(0, 1, 2, 1, 1); //baudrate, chSize, parity,nStopBits,loption
   //     depth->initSerialPort(0, 1, 2, 1, 1);   //baudrate, chSize, parity,nStopBits,loption
        rovhead->initSerialPort(0, 1, 2, 1, 1); //baudrate, chSize, parity,nStopBits,loption
        shiphead->initSerialPort(0, 1, 2, 1, 1);//baudrate, chSize, parity,nStopBits,loption
	vision->initSerialPort(1, 1, 2, 1, 1);	// called again with exact same setup as above.
       
	serialActuators.registerDev(logging);
	//serialActuators.registerDev(depth);	
	serialActuators.registerDev(rovhead);	
	serialActuators.registerDev(shiphead);	
	serialActuators.registerDev(vision);
	
	serialActuators.runActuate();
	return NULL;
}



int serial_sensor_thread_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   int		count;
   int		status;
   pthread_t	ss_thread;
   pthread_t	sa_thread;
   
	if ( argc != 1 ) return ArgError(argv[0], interp);
	serialActuateThread(NULL);
  	status = pthread_create (&ss_thread, NULL, serialSenseThread, NULL);
  	if (status != 0) err_abort (status, "Create serial Receive");
 	
   	sprintf(result, "%d", 0);
   	interp->result = result;
   	return TCL_OK;
}



int
get_ship_head_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   snprintf(result, 100, "%4.1f", vehicle.ship_heading);
   interp->result = result;
   return TCL_OK;
}

/**********************************************************************************
*   Gets and returns on of two strings for the rx to the teleos
*
**********************************************************************************/
int get_teleos_rx_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   int string_index = atoi(argv[1]);  //we only call for 100 charactors at a time = 1 line

   if(string_index) snprintf(result, 100, "%s", &visionRxBuffer[50]);
   else snprintf(result, 100, "%s", visionRxBuffer);
   interp->result = result;
   return TCL_OK;
}


/**********************************************************************************
*   Gets and returns on of two strings for the tx to the teleos
*
**********************************************************************************/
int get_teleos_tx_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 2 ) return ArgError(argv[0], interp);

   int string_index = atoi(argv[1]);  //we only call for 100 charactors at a time

   if(string_index) snprintf(result, 100, "%s", &vision_tx[100]);
   else snprintf(result, 100, "%s", vision_tx);
   interp->result = result;
   return TCL_OK;
}

/**********************************************************************************
*   Gets and returns the tx to the teleos
*
**********************************************************************************/
int get_teleos_tx_ethernet_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);

   snprintf(result, 200, "%s", vision_tx);
   interp->result = result;
   return TCL_OK;
}

/**********************************************************************************
*   Gets and returns the dvl data for tx to vision system
*
**********************************************************************************/
int get_dvl_data_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
extern struct {
	long int bottom_x, bottom_y, bottom_z, bottom_e;
	long int raw_range[4];
	int bottom_status;
	long int water_x, water_y, water_z, water_e;
	int water_status;
	int hour, min, sec, centisec;
	long int raw_temp, raw_pitch, raw_roll, raw_yaw;
	long int raw_dmgb_x, raw_dmgb_y, raw_dmgb_z, raw_dmgb_e;
	long int raw_dmgw_x, raw_dmgw_y, raw_dmgw_z, raw_dmgw_e;
	}
	dvl;
   char result_dvl[500];

   if ( argc != 2 ) return ArgError(argv[0], interp);
   
   if(atoi(argv[1]) == 1) snprintf(result_dvl, 500, "%6ld,%6ld,%6ld,%6ld,%5ld,%5ld,%5ld,%5ld,%2d,%6ld,%6ld,%6ld,%6ld,%2d,%2d,%2d,%2d,%2d,%2ld,%5ld,%5ld,%5ld",
   		dvl.bottom_x, dvl.bottom_y, dvl.bottom_z, dvl.bottom_e,
		dvl.raw_range[0], dvl.raw_range[1], dvl.raw_range[2], dvl.raw_range[3],
		dvl.bottom_status,
		dvl.water_x, dvl.water_y, dvl.water_z, dvl.water_e,
		dvl.water_status,
		dvl.hour, dvl.min, dvl.sec, dvl.centisec,
		dvl.raw_temp, dvl.raw_pitch, dvl.raw_roll, dvl.raw_yaw);
   else	snprintf(result_dvl, 500, "%11ld,%11ld,%11ld,%11ld,%11ld,%11ld,%11ld,%11ld",
   		dvl.raw_dmgb_x, dvl.raw_dmgb_y, dvl.raw_dmgb_z, dvl.raw_dmgb_e,
		dvl.raw_dmgw_x, dvl.raw_dmgw_y, dvl.raw_dmgw_z, dvl.raw_dmgw_e);
		
   interp->result = result_dvl;
   return TCL_OK;
}


int dvl_altitude_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 100, "%4.1f", vehicle.dvl_altitude);
   interp->result = result;
   return TCL_OK;
}


int get_teleos_status_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 10, "%d", teleos_string_status);
   interp->result = result;
   return TCL_OK;
}


int get_ship_gyro_status_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 10, "%d", ship_gyro_string_status);
   interp->result = result;
   return TCL_OK;
}


int get_tms_status_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 10, "%d", tms_string_status);
   interp->result = result;
   return TCL_OK;
}

//Called by overlay_screen.tcl to display the time
//Format can be altered without affecting anything other than overlay
int get_current_time_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
  	time_t currentTime;
	time_t dummyTime;
	struct tm* dummyGM;
	
	if ( argc != 1 ) return ArgError(argv[0], interp);
	currentTime = time(&dummyTime);
	dummyGM = localtime(&dummyTime);
 	snprintf(result, 50, "%.2d:%.2d:%.2d %.2d/%.2d/%.2d", dummyGM->tm_hour, dummyGM->tm_min, dummyGM->tm_sec, dummyGM->tm_mon+1, dummyGM->tm_mday, dummyGM->tm_year-100);
	interp->result = result;
   	return TCL_OK;
}


int get_cam_display_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
#define PAN_LOCKED 		0x02
#define TILT_LOCKED		0x04
#define CAM_SHOULDER_LOCKED 	0x01	

	if ( argc != 1 ) return ArgError(argv[0], interp);
     		
	char lockedStr[20];
	static int cam_counter = 0;
	
	if ((camByte & 0x07) == 0x07) if(cam_counter < 1000) cam_counter++; // all locked, start counter
	else cam_counter = 0;
	memset(lockedStr, 0, 20);
 	if ((cam_counter < 40) &&  vehicle.camera.camera_enable)
 		{
                strcat(lockedStr, "PTS|");
		if ((camByte & PAN_LOCKED) == PAN_LOCKED) strcat(lockedStr, "*");
		else   		    	                  strcat(lockedStr, " ");
		if ((camByte & TILT_LOCKED) == TILT_LOCKED) strcat(lockedStr, "*");
		else                                        strcat(lockedStr, " ");
		if ((camByte & CAM_SHOULDER_LOCKED) == CAM_SHOULDER_LOCKED) strcat(lockedStr, "*");
		else                                                           strcat(lockedStr, " ");
		}	
        snprintf(result, 10, "%s", lockedStr);
        interp->result = result;
	if(vehicle.camera.camera_enable == 0) cam_counter = 0;
        return TCL_OK;
}


/***************************** arm ***********************************/

int get_arm_display_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
#define SWING_LOCKED 	0x01
#define SHOULDER_LOCKED	0x02
#define ELBOW_LOCKED 	0x04
#define PITCH_LOCKED 	0x08
#define YAW_LOCKED 	0x10

        if ( argc != 1 ) return ArgError(argv[0], interp);
     		
	char lockedStr[20];
	static int arm_counter = 0;
		
	/* note: we should get an arm control byte approximatedly every 50 ms. */	

	if ((armByte & 0x1F) == 0x1F) if(arm_counter < 1000) arm_counter++; // all locked, start counter
	else arm_counter = 0; // reset counter to zero
	memset(lockedStr, 0, 20 );
	if ((arm_counter < 40) )        //We can't use arm enable here. do it in tcl
		{
		strcat(lockedStr, "SSEPY|");
		if ((armByte & SHOULDER_LOCKED) == SHOULDER_LOCKED) strcat(lockedStr, "*");
		else                                             strcat(lockedStr, " ");
		
		if ((armByte & SWING_LOCKED) == SWING_LOCKED) strcat(lockedStr, "*");
		else                                             strcat(lockedStr, " ");
	
		if ((armByte & ELBOW_LOCKED) == ELBOW_LOCKED)    strcat(lockedStr, "*");
		else                                             strcat(lockedStr, " ");
			
		if ((armByte & PITCH_LOCKED) == PITCH_LOCKED)    strcat(lockedStr, "*");
		else                                             strcat(lockedStr, " ");
			
		if ((armByte & YAW_LOCKED) == YAW_LOCKED)        strcat(lockedStr, "*");
		else                                             strcat(lockedStr, " ");
		}
        snprintf(result, 20, "%s", lockedStr);
        interp->result = result;
        if(vehicle.arm.arm_enable == 0) arm_counter = 0;
return TCL_OK;	
}

int send_arm_time_constant_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);

        char temp[20];
        extern int ack_arm_samples_setup;
   	int loop_timeout = 0;
   	ack_arm_samples_setup = 0;           	

        arm_time_constant = atoi(argv[1]);
        if((arm_time_constant > 0) && (arm_time_constant < 50))
        	{
        	snprintf(temp, 10, "%d|%d\n", ARM_SAMPLE_SETUP, arm_time_constant);
    		tty_send_request(temp);
    		usleep(400000);
		cout << "commands.c : Ack Arm Samples Setup" << ack_arm_samples_setup << endl;
  		while(ack_arm_samples_setup == 0)
			{
			tty_send_request(temp);
			usleep(200000);
			cout << "commands.c : Ack Arm Samples Setup" << ack_arm_samples_setup << endl;
			loop_timeout ++;
		    	if (loop_timeout == 10)
		    		{
		    		alarm_obj.set_other_alarm(38); //generate alarm
    				ack_arm_samples_setup = 2;
		    		}
			}
    		}
   	return TCL_OK;
}


int send_cam_time_constant_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 2 ) return ArgError(argv[0], interp);
        char temp[20];    	
        cam_time_constant = atoi(argv[1]);
        if((cam_time_constant > 0) && (cam_time_constant < 50))
        	{
        	extern int ack_cam_samples_setup;
        	int loop_timeout = 0;
        	ack_cam_samples_setup = 0;
        	
    		snprintf(temp, 10, "%d|%d\n", CAM_SAMPLE_SETUP, cam_time_constant);
    		tty_send_request(temp);
    		usleep(400000);
    		cout << "commands.c : Ack Cam Samples" << ack_cam_samples_setup << endl;
    		while(ack_cam_samples_setup == 0)
    			{
    			tty_send_request(temp);
    			usleep(400000);
    			cout << "commands.c : Ack Cam Samples" << ack_cam_samples_setup << endl;
    			loop_timeout ++;
    			if (loop_timeout == 10)
    				{
    				alarm_obj.set_other_alarm(36); //generate alarm
    				ack_cam_samples_setup = 2;
    				}
			}
    		}
  	return TCL_OK;
}


int get_arm_time_constant_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
    	snprintf(result, 10, "%d", arm_time_constant);
        interp->result = result;      	
   	return TCL_OK;
}


int get_cam_time_constant_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
    	snprintf(result, 10, "%d", cam_time_constant);
        interp->result = result;      	
   	return TCL_OK;    	
}


int get_dvl_heading_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
    	snprintf(result, 10, "%4.1f", dvl_heading);
        interp->result = result;      	
   	return TCL_OK;    	
}


int get_software_statistics_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	if ( argc != 1 ) return ArgError(argv[0], interp);
    	snprintf(result, 200, "%d|%d|%d|%d|%d|%d|%d|%d", serial_device_transmit_thread_timer,
    		telemetry_receive_thread_timer, head_hist_and_telem_timer_thread_timer,
		depth_hist_thread_timer, io_poll_analogs_thread_timer, io_poll_digitals_thread_timer,
		disk_logging_thread_timer,serial_port_receive_thread_timer);
		
	serial_device_transmit_thread_timer = 0;    //send_t
	telemetry_receive_thread_timer = 0;         //tty_recv_routine
	head_hist_and_telem_timer_thread_timer = 0; //depth_head_routine
	depth_hist_thread_timer = 0;                //depth_routine
	io_poll_analogs_thread_timer = 0;	    //pollIO
	io_poll_digitals_thread_timer = 0;          //pollDigital
	disk_logging_thread_timer = 0;              //disk_logging
	serial_port_receive_thread_timer = 0;       //runSensors
        interp->result = result;      	
   	return TCL_OK;    	
}

/**********************************************************************************
*   Gets and returns the string from Comm 0 to the overlay
*
**********************************************************************************/
int get_overlay_string_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   extern char comm_overlay_string[];

   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 100, "%s", comm_overlay_string);
   interp->result = result;
   return TCL_OK;
}

/**********************************************************************************
*   Gets and returns the calculateed string from Comm 0 to the overlay
*   Uses the origional comm string, maskes off the unwanted bytes then insert user bytes
*   Aug 18 Dave 2004
**********************************************************************************/
int get_overlay_string_plus_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   char enablestring[100], insertstring[100], dummy[100];
   int i;
   extern char comm_overlay_string[];

   if ( argc != 3 ) return ArgError(argv[0], interp);
   snprintf(enablestring, 100, "%s", argv[1]);
   snprintf(insertstring, 100, "%s", argv[2]);
   snprintf(dummy, 100, "%s", comm_overlay_string);
   //Take the enable string and the insert string and combine the 3 to get the overlayed string
   if(strlen(insertstring) > strlen(dummy))
   	{
   	for (i=0; i<strlen(insertstring); i++)
   		{
   		if((i < strlen(dummy)) && (enablestring[i] == '0')) dummy[i] = ' ';
   		if(i >= strlen(dummy))
   			{
   			dummy[i] = ' ';
   			dummy[i+1] = '\0';
   			}
   		if(insertstring[i] != ' ') dummy[i] = insertstring[i];
   		}
   	dummy[strlen(insertstring)] = '\0';
   	}	
   else
   	{
   	for (i=0; i<strlen(dummy); i++)
   		{
   		if(enablestring[i] == '0') dummy[i] = ' ';
   		if((insertstring[i] != ' ') && (i < strlen(insertstring))) dummy[i] = insertstring[i];
   		}
        }
   snprintf(result, 100, "%s", dummy);
   interp->result = result;
   return TCL_OK;
}

/**********************************************************************************
*   Gets and returns the strings from the file system to be usedm in the config
*
**********************************************************************************/
int get_overlay_strings_from_file_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 1 ) return ArgError(argv[0], interp);
   snprintf(result, 100, "%s|%s", enable_overlay_string, insert_overlay_string);
   interp->result = result;
   return TCL_OK;
}

/**********************************************************************************
*   Gets and returns the strings from the file system to be usedm in the config
*
**********************************************************************************/
int put_overlay_strings_to_file_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
   if ( argc != 3 ) return ArgError(argv[0], interp);
   snprintf(enable_overlay_string, 100, "%s", argv[1]);
   snprintf(insert_overlay_string, 100, "%s", argv[2]);
   interp->result = result;
   return TCL_OK;
}
/**********************************************************************************
*   Sends the Teleos Commands to the Thrusters from the ethernet connection
*
**********************************************************************************/
int put_teleos_ethernet_to_telemetry_cmd (ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])		
{
	char teleos_ethernet_string[200];
	
 	if ( argc != 2 ) return ArgError(argv[0], interp);
  	snprintf(teleos_ethernet_string, 200, "%s", argv[1]);
  	telstring.parse_and_set_string(teleos_ethernet_string);
   	
   	interp->result = result;
   	return TCL_OK;
}

//*******************************************************************************/
// MBARI Functions
//*******************************************************************************/
int connect_auto_rov_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
    	if (argc != 3) 
    		{
		Tcl_SetResult(interp, "wrong # args: should be \"connect_auto_rov name port\"", 
                      TCL_STATIC);

        return TCL_ERROR;
	}
    
	connectAutoRov(argv[1], atoi(argv[2])); 
    	return TCL_OK;
}

int disconnect_auto_rov_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
    	disconnectAutoRov();
    	return TCL_OK;
}

int set_auto_rov_tmout_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
    	if (argc != 2) 
    		{
		Tcl_SetResult(interp, "wrong # args: should be \"set_auto_rov_tmout timeout\"", 
                      TCL_STATIC);
		return TCL_ERROR;
		}
    	sprintf(result, "%d", setAutoRovTimeOut(atoi(argv[1])));
    	interp->result = result;
	return TCL_OK;
}

int get_auto_rov_serv_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
	getAutoRovSrv(result);
   	interp->result = result;
   	return TCL_OK;
}

int get_auto_rov_port_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
    	sprintf(result, "%d", getAutoRovPort());
    	interp->result = result;
	return TCL_OK;
}

int get_auto_rov_connect_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
	sprintf(result, "%d", getAutoRovConnect());
	interp->result = result;
	return TCL_OK;
}

int send_auto_rov_cmd_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
	if (argc != 2) 
    		{
		Tcl_SetResult(interp, "wrong # args: should be \"send_auto_rov_cmd cmd\"", 
                      TCL_STATIC);
		return TCL_ERROR;
		}
    
	sendAutoRovCmd(argv[1], result);
	interp->result = result;
	return TCL_OK;
}


