/* 
 * 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.

#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 PRELIMITER_TARGETCIRCLERADIUS 31000

#define NUMBER_OF_CMDS_TO_INTERPRET 12

#define ALLOW_RESET_BY_CAN

#define CAN_PACKET_RATE 10  // Must be 10, 25, or 50
#define TORQUE_CONSTANT 0.62   //N-m/A
#define CURRENT_CMD_RATELIMIT 100 //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 DAC/SPI/WindingCurrentControl.
#define BATT_WINDING_CONTROL_PERIOD 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 VELOCITY_MODE 3 //
#define POWERUP_MODE PREINDEXPULSE_MODE

#define NO_ENCODER_SYNC_HAS_OCCURED 2
#define ONLY_HALL_TRANSITION_HAS_OCCURED 1
#define INDEX_PULSE_SEEN 0

#define CMD_TIMEOUT 30  //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_SCALE_FACTOR 0.7 //-RPM on Kollemogen is +RPM here and extension
#define MAX_SCALE_FACTOR 1.4
#define MIN_SCALE_FACTOR 0.5


#define DEFAULT_RETRACT_FACTOR 1.0
#define MAX_RETRACT_FACTOR 1.0
#define MIN_RETRACT_FACTOR 0.5

#define DEFAULT_TARGETVOLTAGE 300  //Volts, power-up setting
#define MAX_TARGETVOLTAGE 335  // Volts, limit on user setting. 
#define MIN_TARGETVOLTAGE 50   // Volts, limit on user setting and also minimum allowable after modifications for charge current, etc.
#define ABSOLUTEMAX_TARGETVOLTAGE 350  // Volts, this is the peak voltage the system can raise the bus voltage too to keep VqVd inside the target circle.
                                       
#define DEFAULT_BATTDRAWCURRENTLIMIT 5.0  // Sets limit in Amps on current flowing from batteries (specified in magnitude here, measured value < 0), power-up setting. 
#define MAX_BATTDRAWCURRENTLIMIT 10.0  // Limit on user setting

#define DEFAULT_BATTCHARGECURRENTLIMIT 10.0 //Sets limit in Amps on current flowing to the batteries., power up setting.
#define MAX_BATTCHARGECURRENTLIMIT 20.0 //Limit on User Setting.  Max allowable depends upon IBatt Calibration constants

#define DEFAULT_SCALE_INTEGRATION_GAIN 0  //  //Gain Schedule Mode Configuration.  0 is off, set to default if enabled at powerup is desired.
#define MAX_SCALE_INTEGRATION_GAIN 100  //  Needs to be set
#define GAIN_SCHEDULE_INTEGRATION_TIME 600  //Seconds

#define DEFAULT_RPM_STD_DEV_TARGET 500  //  Needs to be set
#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 15.0  //Maximum allowable winding bias current magnitude that can bre applied.

#define MAX_WINDCURRENTLIMIT 35 //Winding Current Limit, Amps.  Limit on internal target

#define DEFAULT_USERCOMMANDEDCURRENT 0
#define MAX_USERCOMMANDEDCURRENT 30

#define DEFAULT_RPM 0 //Power-up target RPM
#define MAX_RPM 6000 //RPM User Request Limit



#define VOLTAGE_HYSTERESIS 10  //Voltage Modification equivalent must drop this far under the target circle before the batteries are turned back on.
#define SLOWROTATECURRENT 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 40.0  //Current limit to cause total shutdown
#define DISABLETIME .001  //time that wining current must exceed DISABLECURRENT for shutdown to occur.


#define LOADDUMP_RESISTANCE 7.0 //Ohms 




#define BRIDGE_ENABLE _LATB1

#define BATT_SWITCH _LATD2

#define AUXIO_PIN2 _LATD1 
//#define AUXIO_PIN3 _LATD2  //Currently being used to drive load switch, so don't use!
#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.
#define BATTERYCONTROLLER_CAN_SRCID 0  //ID of battery controller, this is used in

/* 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 4
#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 M_PI 3.14159265359

#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

#endif	/* CONFIG_H */

