/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd and Vincent Hayward.	    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include "../h/moper_com.h"
#define MARKIII		1

/* external I/O bit definitions, as readable in ``how.exio''.		*/
/* Most of these bits are controller dependant, so beware!		*/

/* Error condition bits in the arm control register  */

#define  BRAKE_RELEASE	01	/* brake release has been enabled	*/
#define  HARDWARE_FAULT	04	/* One on the joint amps is bad ...	*/
#define  ENCODERS_OK   020	/* Optical encoders are OK */
#define  LIMIT_FAULT  0100	/* One of the joints is at a hard limit */
#define  E_STOP       0200	/* Emergency stop condition has been set */

/* other condition bits in the arm control register */

#define  VAL_RESET	02	/* Not too sure what this bit does ...  */
#define  REMOTE	       010	/* Not sure about this bit either ...   */
#define  TEACH_ERROR   040	/* Generated by the teach restrict board */
/* #define  ARMPWR      0400	(defined in jointInterface.h) */
#define  OFFL	     01000	/* external low signal to stop the arm	*/
#define  RUN	     02000	/* front panel switch - run bit low	*/
#define  RESTART     04000	/* front panel switch - restart bit low */
#define  AUTOSTART  010000	/* Auto start button has been hit       */
#define  COMM_RESET 020000      /* Communication line reset		*/

/* ----- routine to detect hardware failures in the arm ----- */

int encodersOK = ENCODERS_OK;

armError()
{	
	register int acr = GET_ACR();

	if (encodersOK && !(acr & encodersOK))
	 { return (SLE_ENCODERS_E);
	 }
	if (acr & (E_STOP | HARDWARE_FAULT | LIMIT_FAULT | BRAKE_RELEASE))
	 { if (acr & E_STOP)
	    { printf ("ESTOP\n");
	      return (MPR_ESTOP_E);
	    }
	   else if (acr & LIMIT_FAULT)
	    { return (SLE_LIMIT_E);
	    }
	   else if (acr & HARDWARE_FAULT)
	    { return (SLE_POWERAMP_E);
	    }
	   else if (acr & BRAKE_RELEASE)
	    { return (SLE_BRAKES_E);
	    }
	 }
	else
	 { return (0);
	 }
}

/* if 0, INTSCL=log2(NINTER); if 1, INTSCL = 8-log2(NINTER) */
int intsclBias = 0;	
/* no. of bits in potentiometer readings */
int numPotBits = 10;
/* no. of times we have to read each pot to be sure of a good value */
int potRdCnt = 1;
