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

#ifndef _PUMA_COM_H_
#define _PUMA_COM_H_

/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

			     PUMA INPUT FORMAT

	FOR ERRORS:

			1 word		-- error code

	FOR INITIALIZATION:
	
			1 word		-- 0x5555 bit test
			1 word		-- 0xaaaa bit test
			1 word		-- how.robotID
			1 word		-- how.controllerID
			1 word		-- how.options (using 'OK' flags)
			1 word		-- how.numAdc and how.numDac
			1 word		-- how.numImport and how.numOutport
			1 word		-- how.numSlu and how.integration
			1 word	        -- how.calibmode and how.markfound
		#if READ_ENCSET
			6 words		-- initial encoder counts
		#endif

	FOR REGULAR COMMUNICATION:

			6 words		-- how.enc [6]
			1 word		-- how.hand
			1 word		-- how.handForce
			1 word		-- mode flags (how.poweron ... )
			1 word		-- read flags (how.torqueOK ..., plus
					      new settings for how.markfound)
	       <numPioIn> words		-- how.pioInput
	      <numPioOut> words		-- how.pioOutput

		        [1 word]	-- new how.markset value
		       [6 words]	-- how.mtorque
		       [6 words]	-- how.potInput
		       [3 words]	-- how.pendantInput
		       [12 words]	-- how.forceInput (6 floats)
		       [6 words]	-- how.jstickInput
		       [12 words]	-- how.teleopInput (6 floats)

		   [<adc> words]	-- how.adcInput

	  [for each active slu]:
		
		          1 word	-- how.sluInput[i].num
	       (count+1)/2 words	-- how.sluInput[i].buf

   -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */

#define READ_ENCSET	0	/* read in robot position when we start up */

#if READ_ENCSET
#define STARTUP_COUNT	 30	/* no. of bytes in startup packet */
#else
#define STARTUP_COUNT	 18	/* no. of bytes in startup packet */
#endif
#define MAX_DATABUF	128	/* max no. of bytes per transfer */

/* ---- flags describing whether optional data is present or not */

#define TORQUE_OK	0x0001
#define POT_OK		0x0002
#define PENDNT_OK	0x0004
#define FORCE_OK	0x0008
#define JSTICK_OK	0x0010
#define MARK_FOUND	0x0020
#define TELEOP_OK       0x0040  /* TSL */

#define TEST_OK		0x4000
#define FORCE_DATA_FLOAT 0x8000	/* force data supplied in float format */

/* ---- flags describing the general robot state ---- */

#define POWER_ON	0x0002
#define IS_CALIBRATED	0x0004

/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

			     PUMA COMMAND FORMAT

	Each command is described by one byte. There are hence 256
	commands possible.

	Commands can have no data, 1 char of data, 1 word of data, or
	1 long word of data. There are different structure to describe
	these.

   -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */

#define	MOVE_ENC_C	0x00	/* low 3 bits for jnt spec. */
#define MOVE_CUR_C	0x08	/* low 3 bits for jnt spec. */
#define LOAD_ENC_C	0x10	/* low 3 bits for jnt spec. */
#define STOP_JNT_C	0x18	/* low 3 bits for jnt spec. */
#define BIAS_CUR_C	0xa0	/* low 3 bits for jnt spec. */
#define JNT_MASK_C	0x07	/* joint select mask */

#define STOP_ALL_C	0x20
#define HAND_POS_C	0x21
#define HAND_FORCE_C	0x22

#define SET_RATE_C	0x30
#define SET_RUNREQ_C	0x31
#define SET_PARAM_C	0x32
#define SET_INTEGRATION_C 0x33
#define SET_CALMODE_C	0x34
#define ON_POWER_C	0x35
#define OFF_POWER_C	0x36
#define SET_CALFLAG_C   0x37

#define TORQUE_READ_C	0x50
#define POT_READ_C	0x51
#define PENDNT_READ_C	0x52
#define FORCE_READ_C	0x53
#define JSTICK_READ_C	0x54
#define ADC_CHAN_C	0x55
#define SLU_CHAN_C	0x56
#define TELEOP_READ_C	0x57

#define PUT_VECTOR_C    0x58   

#define TEST_READ_C	0x59
#define PUT_TEST_C	0x5a

#define PUT_PIO_C	0x70	/* low 3 bits for channel */
#define SET_PIO_C	0x78	/* low 3 bits for channel */
#define CLEAR_PIO_C	0x80	/* low 3 bits for channel */
#define SET_DAC_C	0x88	/* low 3 bits for channel */
#define SET_SLU_C	0x90	/* low 3 bits for channel */
#define CHAN_MASK_C	0x07	/* channel select mask */

#define SET_PENDANT_C	0xa0

#define END_C		0xa1
#define START_C		0xa2

#define SET_MAINT_COM_C	0xa3

/* FOR_SIMULATOR: tell it how many robots are being attached: */
#define ROBOT_IDS_C	0xb0	/* low 4 bits are for number of robots */

/* ---- startup flags ---- */

#define MASTER_F	0x01	/* robot is timing master */

/* ---- robot error codes ---- */

/* error codes 1 - 20 reserved for moper level errors */

#define MPR_BADRATE_E	2   /* can't support sample rate */
#define MPR_TIMEOUT_E	3   /* timeout waiting for host */
#define MPR_MEMINIT_E	4   /* error intializing memory */
#define MPR_LIMIT_E	5   /* moper level joint limit error */
#define MPR_ESTOP_E	6   /* emergency stop switch set */
#define MPR_GAINS_E	7   /* illegal controller gain values */
#define MPR_INTERNAL_E	8   /* moper internal software error */

/* error codes 21 - 30 reserved for host communication */

#define HST_TIMEOUT_E	21   /* timeout talking to host */
#define HST_HEADER_E	22   /* bad header from host */
#define HST_CHKSUM_E	23   /* checksum error on data from host */
#define HST_COUNT_E	24   /* bad count on packet from host */
#define HST_SYNC_E	25   /* lost sync on communication with host */
#define HST_BADCMD_E	26   /* garbage command or ACK from host */

/* error codes 31 - 40 reserved for servo communication */ 

#define SRV_TIMEOUT_E	31   /* timeout talking to servo level */
#define SRV_HEADER_E	32   /* bad header from servo level */
#define SRV_CHKSUM_E	33   /* checksum error on data from servo level */
#define SRV_COUNT_E	34   /* bad count on packet from servo level */
#define SRV_SYNC_E	35   /* lost sync on communication with servos */
#define SRV_BADCMD_E	36   /* garbage command or ACK from servo level */

/* error codes 41 - 60 reserved for joint servo level errors */

#define SLE_TIMEOUT_E	41   /* joint servo timed out talking to moper */
#define SLE_HEADER_E	42   /* servo level got bad header from moper */
#define SLE_CHKSUM_E	43   /* servo level checksum error on data from moper*/
#define SLE_COUNT_E	44   /* servo level got bad packet count from moper*/
#define SLE_SYNC_E	45   /* servo level lost sync with moper */
#define SLE_BADCMD_E	46   /* servo level got bad command from moper */

#define SLE_LIMIT_E	51   /* servo level joint limit error */
#define SLE_POWERAMP_E	52   /* power amp failure at servo level */
#define SLE_ENCODERS_E	53   /* encoder failure detected at servo level */
#define SLE_BRAKES_E	54   /* brakes release is enabled */

/* diagnostic command codes, passed through SET_MAINT_COM_C. The lower
   byte is reserved for information */
   
#define FSE_ICTON	0x1000	/* fake soft error in rbtIcton() */
#define FSE_GATHER	0x1100	/* fake soft error in rbtGather() */
#define FSE_COMMAND	0x1200	/* fake soft error in rbtCommands() */
#define FSE_RELEASE	0x1300	/* fake soft error in release() */

#define FHE_READ	0x1400	/* fake hard error in hostRead() */
#define FHE_WRITE	0x1500	/* fake hard error in hostWrite() */
#define FHE_ATTACH	0x1600	/* fake hard error in hostAttach() */
#define FHE_RELWRITE	0x1700	/* fake hard error in hostReleaseWrite() */
#define FHE_RELREAD	0x1800	/* fake hard error in hostReleaseRead() */

#define FHE_UCLGAINS	0x2000	/* fake hard error in UC, loading gains */
#define FHE_UCSTARTUP   0x2100	/* fake hard error in UC, rbtStartup() */
#define FHE_UCGATHER	0x2200	/* fake hard error in UC I/O */
	
#endif
