/********************************  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.11 2004/10/27 19:15:45 alana 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

//Data types for use in message structs
//Note that multi-byte scalar values (like int, msg16 or in32) must
//not appear in messages because the compiler may insert pad byte
//around them to insure they align on machine word boundaries.
//Enumerations (enums) are generally OK in messages, as
//must compilers will represent enumerations where all values
//are between 0 and 255 as single bytes

typedef byte servoPosition[3];  //a 24-bit signed integer
typedef byte msg16[2];          //16-bit signed integer
typedef byte umsg16[2];         //16-bit signed integer
typedef byte msg32[4];          //32-bit signed integer
typedef byte umsg32[4];         //32-bit signed integer
typedef byte msg16PID[3*2];     //3 16-bit PID gains


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, servoOverpressure,
  servoNotReady, servoPositionErr,servoSpeedError
};

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

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

#endif
