/*
 * File:   types.h
 * Author: hamilton
 *
 * Created on July 19, 2015, 11:21 AM
 */
#include "dsp.h"

#ifndef TYPES_H
#define	TYPES_H

struct BatteryControllerStatusBits {  //This structure must match the battery controller code!
    unsigned int Balancing : 5;   // Indicates number of batteries that are shunting power
    unsigned int StopCharge : 5;  // Indicates number of batteries requesting stop-charge
    unsigned int PowerSupply : 1;  //Indicates state of 24V load switch. 0 = off, 1 = on
    unsigned int Bender : 2;      // Indicates state of Bender operation.  0 = off, 1 = on, 2 = auto (cycling)
    unsigned int BenderRequest : 2;   //Indicates the Bender State request, changed only by CAN rcvd interrupt except for initialization.
    unsigned int BenderSwitchStatus : 1;  //Indicates a fault with the bender power switch
};

typedef  union
{
    unsigned int status;
    struct BatteryControllerStatusBits bits;
}  BATTERY_CONTROLLER_STATUS;


struct SpringControllerStatusBits {
    unsigned int ReliefValveRequest : 7;  //Request to open/close valve
    unsigned int ReliefValveStatus : 1;      // Status of Relief valve open/close
    unsigned int PumpRequest : 1;   // Request to turn pump on or off
    unsigned int PumpStatus : 1;  // Status of pump switch
    unsigned int PumpOverTemp : 1;  // Status of pump OverTemp signal
    unsigned int PumpToggle : 1;  // Status of pump Toggle.
    unsigned int TetherPowerRequest : 1;  //Request to turn tether power on or off
    unsigned int TetherPowerStatus : 1;  //Status of tether power relay
    unsigned int LR_Fault : 1;  //Status of LRF fault input
    unsigned int AUX_Fault : 1;  //Status of AUX fault input
};

typedef  union
{
    unsigned int status;
    struct SpringControllerStatusBits bits;
}  SPRING_CONTROLLER_STATUS;




struct PowerConverterStatusBits {
    unsigned int Mode : 2;  //controller mode.  specified by defines in config.h (GENERATOR_MODE, TORQUE_MODE)
    unsigned int TorqueCmdLimited : 1;  //defines what is limited the Torque Command (0 = Not limited, 1 = Rate, 2 = Min, 3 = Max).
    unsigned int BattSwitchRequest : 1;  //Indicates state of battery switch that the user desires. 0 = off, 1 = on
    unsigned int BattSwitchSetting : 1;  //Indicates instantaneous setting of Battery Switch
    unsigned int SlowSwitchSetting : 1;  //Indicates instantaneous setting of Bettery Slow Switch
    unsigned int EXT_Voltage_Detect : 1; //Indicates if >170V is available on outside connector
    unsigned int GainScheduleMode : 1;      // 0 = off.  1 = on.
    unsigned int OverCurrentShutdown : 1;   //1 indicates drive is in overcurrent shutdown.
    unsigned int OverVoltageShutdown : 1;   //1 indicates drive is in overvoltage shutdown.
    unsigned int SpringRangeValid : 1;  //1 indicates the current SC_Range value is valid (received within approximately SC_RANGE_VALID_TIMEOUT milliseconds)
    unsigned int PermissiveMode : 1;   //1 indicates user has selected "permissive mode" which allows WindingCurrent commands to be set even if SpringController Range packets aren't arriving

};

typedef  union
{
    unsigned int word;
    struct PowerConverterStatusBits bitfields;
}  POWER_CONVERTER_STATUS;


typedef struct tagReceivedCmdParams {
    unsigned char id;  //Command ID
    void (*CmdFunc)(unsigned char *);   //Function to call.
}  ReceivedCmdParams;


typedef struct tagGainScalingAndLimits {
    int GainScaleFactor;
    fractional LowerActuatorLimit;
    fractional UpperActuatorLimit;
    int LowerLimitActive;
    int UpperLimitActive;
} tGSAL;

typedef struct tagCmdRepScalingAndLimits {
    fractional Value;
    fractional LowerLimit;
    fractional UpperLimit;
} tCRSAL;

typedef struct tagCalibrationConstants {
       
    int IWind_cnt[2]; 
    float IWind_eng[2];  //Amps

    int VBus_cnt[2];
    float VBus_eng[2];  //Volts

    int IBatt_cnt[2];
    float IBatt_eng[2]; //Amps

    int DiffPress_cnt[2];
    float DiffPress_eng[2]; //AN10 Calibrated in PSI where 5PSI = 10V

    int AuxTorque_cnt[2];
    float AuxTorque_eng[2];  //Volts   
    
    int PhaseA_Offset_cnts;
    int PhaseB_Offset_cnts;
    
    float LoadDumpResistance;
    
    unsigned int CRC;  //Must be last element in structure.
    
} CalibrationConstants;


#endif	/* TYPES_H */

