/****************************************************************************/
/* Copyright 1998 MBARI                                                     */
/****************************************************************************/
/* Summary  : SemiPower Motor Controller Driver for vxWorks                 */
/* Filename : semiPower.h                                                   */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon                                                       */
/* Version  : Version 1.0                                                   */
/* Created  : 08/31/98                                                      */
/* Modified : 10/02/98                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header:
 * $Log:
 */
/****************************************************************************/

#ifndef _SEMIPOWER_H_
#define _SEMIPOWER_H_

#define BIT_SET         0x8000
#define BIT_CLEAR       0x0000

#define setBit(word, bit)    (word |   (1 << bit))
#define clearBit(word, bit)  (word & (~(1 << bit)))

#define MOTOR_PARAM_ID_LEN      4
#define MOTOR_VALUE_LEN         6

#define DRIVE_ID '1'                /* Tiburon controllers all use ID 1     */
#define FAULT_QUEUE_LENGTH 4

typedef enum                        /* Motor Control Modes                  */
{
    CURRENT,                        /* Basic current mode                   */
    TORQUE,                         /* Torque mode (uncompensated)          */
    VELOCITY,                       /* Velocity mode                        */
    SEMIVELOCITY                    /* Semipower controller velocity mode   */
} motorCtrlMode;

#define VELOCITY_GAIN_SIZE      3   /* Number of gains for velocity mode    */

#define VEL_PROP_GAIN           0   /* Velocity Mode Proportional Gain Index*/
#define VEL_DERIV_GAIN          1   /* Velocity Mode Derivative Gain Index  */
#define VEL_POWER_GAIN          2   /* Velocity Mode Power Gain Index       */

                                    /* Velocity control mode gains          */
typedef Int16 velocity_gains[VELOCITY_GAIN_SIZE];

typedef struct
{
   char  paramID[MOTOR_PARAM_ID_LEN];
   char  value[MOTOR_VALUE_LEN];
} semiPowerCmd;

typedef enum                     /* C00 drive_command bit field definitions */
{
    stopRequest         = 0,
    startRequest        = 1,
    commissionRequest   = 2,
    clearExternalFault  = 4,
    externalFaultCmd    = 5,
    clearFaultQueue     = 6,
    resetSerialComs     = 7,
    setFactoryDefault   = 8,
    jogStartRequest     = 9,
    ioRequest           = 11,
    calcMotor           = 12,
    processorReset      = 15,
} semiPowerDriveCmdBits;

typedef enum                    /* C01 drive_mode bit field definitions    */
{
    useStopMode         =  0 | BIT_SET,
    coastToStop         =  0 | BIT_CLEAR,

    speedRegulatorMode  =  1 | BIT_SET,
    torqueRegulatorMode =  1 | BIT_CLEAR,

    flyingStartsDisable =  2 | BIT_SET,
    flyingStartsEnable  =  2 | BIT_CLEAR,

    efficiencyDisable   =  3 | BIT_SET,
    efficiencyEnable    =  3 | BIT_CLEAR,

    calcMotorParams     =  4 | BIT_SET,
    dontCalcMotorParams =  4 | BIT_CLEAR,

    forwardDirectionCmd =  5 | BIT_SET,
    reverseDirectionCmd =  5 | BIT_CLEAR,

    normalCommMode      =  7 | BIT_SET,
    commIgnoreCksum     =  7 | BIT_CLEAR,

    noExtendCommission  =  8 | BIT_SET,
    extendCommission    =  8 | BIT_CLEAR,

    acMode              =  9 | BIT_SET,
    BLDCMode            =  9 | BIT_CLEAR,

    closedLoopMode      = 10 | BIT_SET,
    openLoopMode        = 10 | BIT_CLEAR,
} semiPowerDriveMode;

typedef enum                    /* D00 drive_state bit field definitions   */
{
    stateMachine        = 0x00ff,

    reverseDirection    = 0x0100,
    stopping            = 0x0200,
    running             = 0x0400,
    reversing           = 0x0800,
    commissioning       = 0x1000,
    closedLoop          = 0x2000,
    jogging             = 0x4000,
    speedMode           = 0x8000,
} semiPowerDriveStateBits;

typedef enum                    /* D01 drive_status bit field definitions  */
{
    systemFauled        = 0x0001,
    invertorSatFault    = 0x0002,
    DBOverload          = 0x0004,
    phaseOverCurrent    = 0x0008,
    singlePhaseInput    = 0x0010,
    DCLinkUnderVoltage  = 0x0020,
    DCLinkOverVoltage   = 0x0040,
    baseDriveFault      = 0x0080,
    atodOffsetError     = 0x0100,
    motorFault          = 0x0200,
    heatSinkTemperature = 0x0400,
    motorThermalOvld    = 0x0800,
    communicationsFault = 0x1000,
    externalFault       = 0x2000,
    EEPromFault         = 0x4000,
    motorParameterFault = 0x8000,
} semiPowerDriveStatusBits;

typedef enum
{
  motorNoFault_FC        =  0x0,
  invertorSatFault_FC    =  0x1,
  dbOverload_FC          =  0x2,
  phaseOverCurrent_FC    =  0x3,
  singlePhaseInput_FC    =  0x4,
  dcUndervoltage_FC      =  0x5,
  dcOvervoltage_FC       =  0x6,
  baseDriveFault_FC      =  0x7,
  adOffsetTolerance_FC   =  0x8,
  motorFault_FC          =  0x9,
  heatSinkOverTemp_FC    =  0xa,
  thermalOverload_FC     =  0xb,
  communicationsFault_FC =  0xc,
  externalFault_FC       =  0xd,
  motorParamFault_FC     =  0xe,
  driveReset_FC          = 0xff
} semiPowerFaultCode;

struct faultQueueEntry
{
  Word  driveStatus;
  DWord elapsedTime;
  Nat16 i_mag;
  Nat16 vdc;
  Int16 rpm;
  Nat16 paRiscPad;              /* Ensures correct alignment for pa risc  */
};

#endif /* _SEMIPOWER_H_ */
