/********************************  servobits.h  *******************************
 * $Source: /home/cvs/ESP/gen2/software/msp430/dwarves/servobits.h,v $
 *  Copyright (C) 2003 MBARI
 *
 *  MBARI Proprietary Information. All rights reserved.
 * $Id: servobits.h,v 1.17 2005/11/24 00:23:28 brent Exp $
 *
 * ESP "dwarf" node servo control and status bit masks
 *
 *  These bit masks are common to the interrupt handlers and the background
 *  message processing.  See espmsg.h
 *
 *****************************************************************************/

#ifndef SERVOBITS_H
#define SERVOBITS_H

//return true iff message tag byte is valid
#define validTag(tag) ((unsigned)((tag)-1) < (unsigned)0x7f)

#ifndef abs
#define abs(x) ((x)<0 ? -(x) : (x))
#endif

typedef byte servoPosition[3];  //a 24-bit signed integer
typedef byte msg16PID[3*2];     //3 16-bit PID gains

typedef byte msg16[2];          //16-bit signed integer
typedef byte umsg16[2];         //16-bit signed integer


enum servoOption {  //optionMask Bits:

  servoOptionEnable       = 0x1,  //Enable encoder power and servo loop
  servoOptionEncPolarity  = 0x2,  //increase count clockwise(0) or counterclkwse
  servoOptionThreshold    = 0x4,  //Alter goal with threshold reached
  servoOptionThresPolarity= 0x8,  //Threshold flag polarity (set for rising)
  servoOptionHomePwr      = 0x10, //home sensor power ON
  servoOptionHomePolarity = 0x20, //reset position on rising(0) or falling edge
  servoOptionHomeDir      = 0x40, //reserved
  servoOptionJog          = 0x80  //jog until position reset by home flag
};

enum servoStatusBits {  //servoStatus  bits:
  servoOn           = 0x1,  //servo is ON in servoStatus
  servoLostHome     = 0x2,  //lost track of axis home position (encoder invalid)
  servoHomeFlag     = 0x4,  //home flag active
  servoPastThreshold= 0x8,  //exceeded threshold (goal was altered)
  servoRLS          = 0x10, //at or past reverse limit
  servoFLS          = 0x20  //at or past forward limit
};

enum servoReason { //causes of servo command aborts
  noServoError, invalidServoChannel, invalidServoGoal,
  servoAborted, servoOvercurrent, servoPressureError,
  servoNotReady, servoPositionErr,servoSpeedError, servoTrajectoryErr
};

enum shaftReason {
  noShaftError, invalidShaftNumber, invalidShaftAngle,
  shaftAborted, shaftOvercurrent
};

enum thermalReason {
  noThermalError, invalidThermalProgram, thermalAborted,
  thermalTempError, thermalHardFault
};

struct servoStatus { //servo status for host
  enum servoStatusBits status; //status bits
  servoPosition    position;   //current encoder counter value
  msg16            velocity;   //current counts/tic
  int16            current;    //current motor current in milliamps (signed)
  uint8            inPress;    //inlet pressure (PSI)
  uint8            outPress;   //outlet pressure (PSI)
};

#endif
