/* 
 * File:   config.h
 * Author: hamilton
 *
 * Created on January 15, 2014, 4:29 PM
 */

#ifndef CONFIG_H
#define	CONFIG_H

#define DEBUG  //Define this to enable RS-232 outputs and Operating modes other than Battery Charging Mode.
#define ALLOW_CALIBRATION   // Allows raw = 1 to be set so the ADC's report raw counts.
//#define ALLOW_RESET_BY_CAN

#define EEPROM_WRITE_PROTECT _RA2
#define EEPROM_PAGE_SIZE 32
#define EEPROM_DEVICE_ADDR 0xA0
#define CAL_DATA_EEPROM_ADDR 0x00000000

#ifdef DEBUG
//Tuning Options: Options are: WINDING_QUADRATURE, WINDING_FLUX, SHUNT_REGULATOR, BATT_CHARGE_CURRENT, BATT_DRAW_CURRENT, VQVD_DISTANCE,VELOCITY
//#define TUNE_WINDING_QUADRATURE
//#define TUNE_WINDING_FLUX
//#define TUNE_SHUNT_REGULATOR
//#define TUNE_BATT_CHARGE_CURRENT
//#define TUNE_BATT_DRAW_CURRENT
//#define TUNE_VQVD_DISTANCE
#endif

#define ENC_CNTPERREV 4096 //16384 //4096  //Currently can be 4096 or 16384.

#define BRIDGE_DISABLE_CURRENT 0.200  //Amps  Setting either  BRIDGE_DISABLE_CURRENT or BRIDGE_DISABLE_RPM to zero disables this feature.
#define BRIDGE_DISABLE_RPM 0 //300 //RPM

#define NUM_CAL_SAMPLES 1024

#define U1_InputBufSize 64

#define PRELIMITER_TARGETCIRCLERADIUS 31000
#define RE_ENABLE_HYSTERIS 2000  //Distance inside targetcircle radius VqVd Magnitude must be to re-enable battery switch.  

#define NUMBER_OF_CMDS_TO_INTERPRET 12



#define VOLTAGE_CONSTANT 0.053   //0.040 VRMS/RPM  0.053 VPeak/RPM 
#define TORQUE_CONSTANT 0.438   //0.62 N-m/ARMS  0.428N-m/AMPS Flux Current
#define CURRENT_CMD_RATELIMIT 200 //A/second.  Set to zero to disable feature.

//*Interrupt timing:
#define WINDING_CONTROL_PERIOD 2  //The DAC/SPI/WindingControl will occur every WINDING_CONTROL_PERIOD iterations of the PWM interrupt
#define RPM_COMPUTATION_PERIOD 10  //RPM Computation will be performed every RPM_COMPUTATION_PERIOD iterations of PWM Interrupt.
#define BATT_WINDING_CONTROL_PERIOD 1 // 5  //The BATT current control loops will be performed every RPM_COMPUTATION_PERIOD iterations of DAC/SPI/WindingCurrentControl. 
#define ISR_UPDATE_RATE 1092   //Used to convert timeouts to seconds.  Approx:  = Fcy/(PreScaler*PR5) = 39.61375/(8*4952)
#define PWM_FREQ 20000    //PWM Rate, approx is OK, used for Disable time.

/******** Enable possible modes here and select which is active on power-up *********/
#define PREINDEXPULSE_MODE 0 //Slow rotation mode until index pulse is seen.
#define GENERATOR_MODE 1  //  Torque is selected to follow prescribed Speed-Power relation.
#define TORQUE_MODE 2  //  User Specifieds Motor Torque

#define NO_ENCODER_SYNC_HAS_OCCURED 2
#define ONLY_HALL_TRANSITION_HAS_OCCURED 1
#define INDEX_PULSE_SEEN 0

#define TORQUE_CMD_TIMEOUT 2  //Torque Command Timeut, in seconds.  Power-up setting.  Can't be larger than 65535/ISR_UPDATE_RATE (i.e. 60 seconds)  //Set to zero to disable timeout.
#define BIAS_CMD_TIMEOUT 10  //Bias Current Command Timeut, in seconds.  Power-up setting.  Can't be larger than 65535/ISR_UPDATE_RATE (i.e. 60 seconds)  //Set to zero to disable timeout.
          
#define DEFAULT_CAN_PACKET_RATE 10
#define MIN_CAN_PACKET_RATE 10
#define MAX_CAN_PACKET_RATE 50


#define DEFAULT_SCALE_FACTOR 1.0 //-RPM on Kollemogen is +RPM here and extension
#define MAX_SCALE_FACTOR 1.4
#define MIN_SCALE_FACTOR 0.5


#define DEFAULT_RETRACT_FACTOR 0.6
#define MAX_RETRACT_FACTOR 1.0
#define MIN_RETRACT_FACTOR 0.4
  

#define DEFAULT_TARGETVOLTAGE 290  //Volts, power-up setting

#define MAX_TARGETVOLTAGE 295  // Volts, limit on user setting. 
#define MIN_TARGETVOLTAGE 200   // Volts, limit on user setting and also minimum allowable after modifications for charge current, etc.


#define ABSOLUTEMAX_TARGETVOLTAGE 415  // Volts, this is the peak voltage the system can raise the bus voltage too to keep VqVd inside the target circle.  Disconnects output while doing this..
#define SHUTDOWN_VOLTAGE 450  // Volts, this is the voltage at which the bridge will shut down and become a rectifier
     

#define DEFAULT_BATTDRAWCURRENTLIMIT 8.0  // Sets limit in Amps on current flowing from batteries (specified in magnitude here, measured value < 0), power-up setting. 
#define MAX_BATTDRAWCURRENTLIMIT 12.0  // Limit on user setting
#define MIN_BATTDRAWCURRENTLIMIT 2.0  // Limit on user setting


#define DEFAULT_BATTCHARGECURRENTLIMIT 3.0 //Sets limit in Amps on current flowing to the batteries., power up setting.
#define MAX_BATTCHARGECURRENTLIMIT 12.0 //Limit on User Setting.  Max allowable depends upon IBatt Calibration constants
#define MIN_BATTCHARGECURRENTLIMIT 2.0 //Limit on User Setting.  Max allowable depends upon IBatt Calibration constants

#define DEFAULT_SCALE_INTEGRATION_GAIN 75  //  //Gain Schedule Mode Configuration.  0 is off, set to default if enabled at powerup is desired.  
                                              // Units are Scale value per 100 RPM StdDev error per hour,  i.e.  10 will change the scale factor by .1 after an hour of a 100RPM error in standard deviation measurement versus target.
#define MAX_SCALE_INTEGRATION_GAIN 200  //  Scale factor percentage points per 1kRPM StdDev error per hour.
#define MIN_SCALE_INTEGRATION_GAIN 0
#define GAIN_SCHEDULE_INTEGRATION_TIME 10  //Minutes  Mut be smaller than 20 minutes to avoid fixed point overflow elsewhere

#define DEFAULT_RPM_STD_DEV_TARGET 500  // In RPM
#define MIN_RPM_STD_DEV_TARGET 0     //In RPM
#define MAX_RPM_STD_DEV_TARGET 2000     //In RPM

#define DEFAULT_BIASCURRENT 0.0  //Start with zero bias current
#define MAX_BIASCURRENT 20.0  //Maximum allowable winding bias current Magnitude that can be applied.

#define MAX_WINDCURRENTLIMIT 35 //Winding Current Limit, Amps.  Limit on internal target

#define DEFAULT_USERCOMMANDEDCURRENT 0
#define MAX_USERCOMMANDEDCURRENT MAX_WINDCURRENTLIMIT

#define SLOWROTATECURRENT 0.0//4.0 //Current applied to slowly rotate rotor at powerup until Index Pulse is found.  Mostly for Lab testing as this may be too low to overcome forces in the application.

#define DISABLECURRENT 45.0  //Current limit to cause total shutdown
#define DISABLETIME .001  //time that wining current must exceed DISABLECURRENT for shutdown or Bus Voltage exceeds SHUTDOWN_VOLTAGE for shutdown to occur.

#define BATTERY_SLOW _LATG14
#define BATTERY_SWITCH _LATG15
#define EXT_VOLTAGE_DETECT _RB0

#define BATTSWITCH_ENABLE _LATG13
#define BRIDGE_ENABLE _LATB1

//SCOTT
#define VREF_ENABLE _LATB13 

#define AUXIO_PIN2 _LATD1 
#define AUXIO_PIN3 _LATD2  
#define AUXIO_PIN4 _LATD8
#define AUXIO_PIN5 _LATD9 

#define EEPROM_WP _LATD10

#define CAN_SILENT _LATA6
#define CAN_ENABLE _LATA7

/******** Configure CAN Bus identifier and speed *********/
//  CAN ID field width definitions, from least sig to most sig.
#define SEQ_WIDTH  9
#define ID_WIDTH  5
#define SRC_WIDTH 4

#define CAN_SRCID 2  //Specific ID identifying messages that originate from this hardware.

/* number of ECAN message buffers in DMA memory (must be greater or equal to the number of buffers used. */
#define ECAN1_MSG_BUF_LENGTH 16   /* 8 transmit buffers  8 recieve buffers */
#define ECAN1_NUM_TX_BUFFERS 8


/******** Configure Analog IO here  *********/
//These defines relate the analog channel numbers to the analog signals, they can be rearranged here without trouble later.
#define NUMAIO 2
#define INNERFILTERSAMPLES 1 //Must be 1,2,4,8,16,32,64,128   This is the number of ADC samples that are filtered to create every PWM period
#define OUTERFILTERSAMPLES 8 //Must match number of taps in OuterFilter.s  This is the number of samples that are filtered at the slower update rate, 8 is the closest power of two for a  for a 10kHz ADC Sample rate and 1000Hz outer loop update rate.
#define REPORTINGFILTERSAMPLES 48 //Must match number of taps in ReportingFilter.s  This is the number of samples that are filtered at the CAN update rate, 

#define AIOMASK_H 0xfffc  //Select AN16-AN17 to be sampled and scanned

//#define AIOMASK_L 0xf3ff  //Select AN10,AN11 to be sampled and scanned, total must match NUMAIO
#define AIOMASK_L 0xffff  //Select AN10,AN11 to be sampled and scanned, total must match NUMAIO

#define DEFAULT_ANGLEOFFSET 5461  //Motor Specific.  Equivalent to 30 degrees


//Command and Reporting Scale Factors (cnts/engineering unit)
#define CRSF_RPM 2        //cnts per RPM
#define CRSF_VOLTAGE 100  //cnts per Volt
#define CRSF_CURRENT 500  //cnts per Amp
#define CRSF_TIME   1000  //cnts per Second
#define CRSF_POWER  1     //cnts per Watt
#define CRSF_PERCENTAGES 100    //cnts per 100 percent
#define CRSF_PRESSURE 1000    //cnts per psi
#define CRSF_TORQUE 500    //cnts per N-m
#define CRSF_POSITION 10 //cnts per inch

#define SPRINGCONTROLLER_CAN_SRCID 1 
#define SC_RANGE_VALID_TIMEOUT 150  //milli-seconds.  //The ~1kHz interrupt counts ths down, so times out in ~150ms...
#define SC_RANGE_MIN 0  //Inches
#define SC_RANGE_MAX 80  //Inches
#define STOP_RANGE 10  //Inches from SC_RANGE_MIN and SC_RANGE_MAX to increase generator torque
#define MAX_RPM_ADJUSTMENT 5000 //Maximum amount to modify RPM in determining WindingCurrentLimit near ends of stroke.


#endif	/* CONFIG_H */

