QITH FLAGS
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of TF_conops_BAE1_3 by
EPS.cpp@1:7185136654ce, 2015-07-06 (annotated)
- Committer:
- sakthipriya
- Date:
- Mon Jul 06 07:34:00 2015 +0000
- Revision:
- 1:7185136654ce
- Parent:
- 0:246d1b5f11ae
varun - to modify acs part of this code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 0:246d1b5f11ae | 1 | #include "EPS.h" |
sakthipriya | 0:246d1b5f11ae | 2 | #include "pin_config.h" |
sakthipriya | 1:7185136654ce | 3 | /***********************************************global variable declaration***************************************************************/ |
sakthipriya | 1:7185136654ce | 4 | extern uint32_t BAE_STATUS; |
sakthipriya | 1:7185136654ce | 5 | extern uint32_t BAE_ENABLE; |
sakthipriya | 1:7185136654ce | 6 | const char RCOMP0= 0x97; //? |
sakthipriya | 1:7185136654ce | 7 | SensorData Sensor; |
sakthipriya | 1:7185136654ce | 8 | SensorDataQuantised SensorQuantised; |
sakthipriya | 1:7185136654ce | 9 | ShortBeacy Shortbeacon; |
sakthipriya | 1:7185136654ce | 10 | int power_mode = 1; |
sakthipriya | 1:7185136654ce | 11 | |
sakthipriya | 1:7185136654ce | 12 | /***********************************************Configuring Peripherals*******************************************************************/ |
sakthipriya | 1:7185136654ce | 13 | Serial pc_eps(USBTX,USBRX); |
sakthipriya | 0:246d1b5f11ae | 14 | |
sakthipriya | 1:7185136654ce | 15 | I2C BG_I2C(D14,D15); //i2c btwn bae and battery gauge |
sakthipriya | 1:7185136654ce | 16 | DigitalOut SelectLinesA[] = {PIN43,PIN44,PIN45,PIN46}; //to mux1=>voltage mux , PTA 13-16 , CHNGE TO PIN43 LATER |
sakthipriya | 1:7185136654ce | 17 | DigitalOut SelectLinesB[] = {PIN56,PIN57,PIN58,PIN59}; //to mux2=>current mux(differential mux) , PTB 3,7,8,9 |
sakthipriya | 1:7185136654ce | 18 | //MSB is SelectLines[0],LSB is SelectLines[3] |
sakthipriya | 1:7185136654ce | 19 | AnalogIn CurrentInput(PIN53); // output from Current Mux PTB0 |
sakthipriya | 1:7185136654ce | 20 | AnalogIn VoltageInput(PIN54); // output from Voltage Multiplexer PTB1 |
sakthipriya | 1:7185136654ce | 21 | AnalogIn VBATT(VBATT); //VBATT of battery gauge |
sakthipriya | 0:246d1b5f11ae | 22 | |
sakthipriya | 1:7185136654ce | 23 | SPI BTemp_spi(PTD6,PTD7,PTD5); //MOSI,MISO,SLK |
sakthipriya | 1:7185136654ce | 24 | DigitalOut BTemp_ssn1(PTD4); //Slave select1 |
sakthipriya | 1:7185136654ce | 25 | DigitalOut BTemp_ssn2(PTD2);//Slave select2 |
sakthipriya | 1:7185136654ce | 26 | DigitalOut BTemp_PS(PTB0); |
sakthipriya | 1:7185136654ce | 27 | DigitalOut BTemp_HS(PTB1); |
sakthipriya | 1:7185136654ce | 28 | |
sakthipriya | 1:7185136654ce | 29 | DigitalOut TRXY(TRXY_DR_EN); //active high |
sakthipriya | 1:7185136654ce | 30 | DigitalOut TRZ(TRZ_DR_EN); //active high |
sakthipriya | 0:246d1b5f11ae | 31 | //----------------------------------------------------EPS INIT---------------------------------------------------------------------------// |
sakthipriya | 0:246d1b5f11ae | 32 | void FCTN_EPS_INIT() |
sakthipriya | 0:246d1b5f11ae | 33 | { |
sakthipriya | 1:7185136654ce | 34 | BAE_STATUS |= 0x00010000; //set EPS_INIT_STATUS flag |
sakthipriya | 1:7185136654ce | 35 | FCTN_EPS_BG_INIT(); |
sakthipriya | 1:7185136654ce | 36 | FCTN_EPS_BTEMP_INIT(); |
sakthipriya | 1:7185136654ce | 37 | 3V3AENBL = 1; //enable dc dc converter A |
sakthipriya | 1:7185136654ce | 38 | // if battery gauge is initialised |
sakthipriya | 1:7185136654ce | 39 | Sensor.soc = BG_soc(); |
sakthipriya | 1:7185136654ce | 40 | Sensor.Vbatt = VBATT.read()*3.3; |
sakthipriya | 1:7185136654ce | 41 | FCTN_EPS_POWERMODE(Sensor.soc); |
sakthipriya | 1:7185136654ce | 42 | BAE_STATUS |= 0x00020000; //set EPS_BATTERY_GAUGE_STATUS |
sakthipriya | 1:7185136654ce | 43 | //else |
sakthipriya | 1:7185136654ce | 44 | power_level = 1; |
sakthipriya | 1:7185136654ce | 45 | BAE_STATUS &= 0xFFFDFFFF ///set EPS_BATTERY_GAUGE_STATUS |
sakthipriya | 1:7185136654ce | 46 | BAE_STATUS &= 0xFFFEFFFF; //clear EPS_INIT_STATUS flag |
sakthipriya | 1:7185136654ce | 47 | } |
sakthipriya | 1:7185136654ce | 48 | |
sakthipriya | 1:7185136654ce | 49 | //---------------------------------------------battery Temp sensor code------------------------------------------------------------------// |
sakthipriya | 1:7185136654ce | 50 | void FCTN_EPS_BTEMP_INIT() |
sakthipriya | 1:7185136654ce | 51 | { |
sakthipriya | 1:7185136654ce | 52 | BTemp_ssn1=1;BTemp_ssn2=1; |
sakthipriya | 1:7185136654ce | 53 | BTemp_PS=0; //power switch control enable |
sakthipriya | 1:7185136654ce | 54 | BTemp_HS=0; //heater switch |
sakthipriya | 1:7185136654ce | 55 | BTemp_spi.format(8,3); |
sakthipriya | 1:7185136654ce | 56 | BTemp_spi.frequency(1000000); |
sakthipriya | 1:7185136654ce | 57 | } |
sakthipriya | 1:7185136654ce | 58 | |
sakthipriya | 1:7185136654ce | 59 | void FCTN_EPS_BTEMP_MAIN(float btry_temp[2]) |
sakthipriya | 1:7185136654ce | 60 | { |
sakthipriya | 1:7185136654ce | 61 | |
sakthipriya | 1:7185136654ce | 62 | uint8_t MSB, LSB; |
sakthipriya | 1:7185136654ce | 63 | int16_t bit_data; |
sakthipriya | 1:7185136654ce | 64 | float sensitivity=0.0078125; //1 LSB = sensitivity degree celcius |
sakthipriya | 1:7185136654ce | 65 | wait_ms(320); |
sakthipriya | 1:7185136654ce | 66 | BTemp_ssn1=0; |
sakthipriya | 1:7185136654ce | 67 | BTemp_spi.write(0x80);//Reading digital data from Sensor 1 |
sakthipriya | 1:7185136654ce | 68 | LSB = BTemp_spi.write(0x00);//LSB first |
sakthipriya | 1:7185136654ce | 69 | MSB = BTemp_spi.write(0x00); |
sakthipriya | 1:7185136654ce | 70 | printf("%d %d\n",MSB,LSB); |
sakthipriya | 1:7185136654ce | 71 | bit_data= ((uint16_t)MSB<<8)|LSB; |
sakthipriya | 1:7185136654ce | 72 | btry_temp[0]=(float)bit_data*sensitivity;//Converting into decimal value |
sakthipriya | 1:7185136654ce | 73 | BTemp_ssn1=1; |
sakthipriya | 1:7185136654ce | 74 | BTemp_ssn2=0;//Reading data from sensor 2 |
sakthipriya | 1:7185136654ce | 75 | BTemp_spi.write(0x80); |
sakthipriya | 1:7185136654ce | 76 | LSB = BTemp_spi.write(0x00); |
sakthipriya | 1:7185136654ce | 77 | MSB = BTemp_spi.write(0x00); |
sakthipriya | 1:7185136654ce | 78 | bit_data= ((int16_t)MSB<<8)|LSB; |
sakthipriya | 1:7185136654ce | 79 | btry_temp[1]=(float)bit_data*sensitivity; |
sakthipriya | 1:7185136654ce | 80 | BTemp_ssn2=1; |
sakthipriya | 1:7185136654ce | 81 | printf("\n\r battery temperature %f %f ",btry_temp[0],btry_temp[1]); |
sakthipriya | 1:7185136654ce | 82 | } |
sakthipriya | 1:7185136654ce | 83 | |
sakthipriya | 1:7185136654ce | 84 | |
sakthipriya | 1:7185136654ce | 85 | |
sakthipriya | 1:7185136654ce | 86 | //----------------------------------------------------Battery Gauge code-----------------------------------------------------------------// |
sakthipriya | 1:7185136654ce | 87 | |
sakthipriya | 1:7185136654ce | 88 | unsigned short BG_readReg(char reg) |
sakthipriya | 1:7185136654ce | 89 | { |
sakthipriya | 1:7185136654ce | 90 | //Create a temporary buffer |
sakthipriya | 1:7185136654ce | 91 | char buff[2]; |
sakthipriya | 1:7185136654ce | 92 | |
sakthipriya | 1:7185136654ce | 93 | //Select the register |
sakthipriya | 1:7185136654ce | 94 | BG_I2C.write(BG_ADDR, ®, 1, true); |
sakthipriya | 1:7185136654ce | 95 | |
sakthipriya | 1:7185136654ce | 96 | //Read the 16-bit register |
sakthipriya | 1:7185136654ce | 97 | BG_I2C.read(BG_ADDR, buff, 2); |
sakthipriya | 1:7185136654ce | 98 | |
sakthipriya | 1:7185136654ce | 99 | //Return the combined 16-bit value |
sakthipriya | 1:7185136654ce | 100 | return (buff[0] << 8) | buff[1]; |
sakthipriya | 1:7185136654ce | 101 | } |
sakthipriya | 1:7185136654ce | 102 | |
sakthipriya | 1:7185136654ce | 103 | void BG_writeReg(char reg, unsigned short data) |
sakthipriya | 1:7185136654ce | 104 | { |
sakthipriya | 1:7185136654ce | 105 | //Create a temporary buffer |
sakthipriya | 1:7185136654ce | 106 | char buff[3]; |
sakthipriya | 1:7185136654ce | 107 | |
sakthipriya | 1:7185136654ce | 108 | //Load the register address and 16-bit data |
sakthipriya | 1:7185136654ce | 109 | buff[0] = reg; |
sakthipriya | 1:7185136654ce | 110 | buff[1] = data >> 8; |
sakthipriya | 1:7185136654ce | 111 | buff[2] = data; |
sakthipriya | 1:7185136654ce | 112 | |
sakthipriya | 1:7185136654ce | 113 | //Write the data |
sakthipriya | 1:7185136654ce | 114 | BG_I2C.write(BG_ADDR, buff, 3); |
sakthipriya | 1:7185136654ce | 115 | } |
sakthipriya | 1:7185136654ce | 116 | |
sakthipriya | 1:7185136654ce | 117 | |
sakthipriya | 1:7185136654ce | 118 | |
sakthipriya | 1:7185136654ce | 119 | // Command the MAX17049 to perform a power-on reset |
sakthipriya | 1:7185136654ce | 120 | void BG_reset() |
sakthipriya | 1:7185136654ce | 121 | { |
sakthipriya | 1:7185136654ce | 122 | //Write the POR command |
sakthipriya | 1:7185136654ce | 123 | BG_writeReg(REG_CMD, 0x5400); |
sakthipriya | 1:7185136654ce | 124 | } |
sakthipriya | 1:7185136654ce | 125 | |
sakthipriya | 1:7185136654ce | 126 | // Command the MAX17049 to perform a QuickStart |
sakthipriya | 1:7185136654ce | 127 | void BG_quickStart() |
sakthipriya | 1:7185136654ce | 128 | { |
sakthipriya | 1:7185136654ce | 129 | //Read the current 16-bit register value |
sakthipriya | 1:7185136654ce | 130 | unsigned short value = BG_readReg(REG_MODE); |
sakthipriya | 1:7185136654ce | 131 | |
sakthipriya | 1:7185136654ce | 132 | //Set the QuickStart bit |
sakthipriya | 1:7185136654ce | 133 | value |= (1 << 14); |
sakthipriya | 1:7185136654ce | 134 | |
sakthipriya | 1:7185136654ce | 135 | //Write the value back out |
sakthipriya | 1:7185136654ce | 136 | BG_writeReg(REG_MODE, value); |
sakthipriya | 1:7185136654ce | 137 | } |
sakthipriya | 1:7185136654ce | 138 | |
sakthipriya | 1:7185136654ce | 139 | |
sakthipriya | 1:7185136654ce | 140 | //disable sleep |
sakthipriya | 1:7185136654ce | 141 | void BG_disableSleep() |
sakthipriya | 1:7185136654ce | 142 | { |
sakthipriya | 1:7185136654ce | 143 | unsigned short value = BG_readReg(REG_MODE); |
sakthipriya | 1:7185136654ce | 144 | value &= ~(1 << 13); |
sakthipriya | 1:7185136654ce | 145 | BG_writeReg(REG_MODE, value); |
sakthipriya | 1:7185136654ce | 146 | } |
sakthipriya | 1:7185136654ce | 147 | |
sakthipriya | 1:7185136654ce | 148 | //disable the hibernate of the MAX17049 |
sakthipriya | 1:7185136654ce | 149 | void BG_disableHibernate() |
sakthipriya | 1:7185136654ce | 150 | { |
sakthipriya | 1:7185136654ce | 151 | BG_writeReg(REG_HIBRT, 0x0000); |
sakthipriya | 1:7185136654ce | 152 | } |
sakthipriya | 1:7185136654ce | 153 | |
sakthipriya | 1:7185136654ce | 154 | |
sakthipriya | 1:7185136654ce | 155 | // Enable or disable the SOC 1% change alert on the MAX17049 |
sakthipriya | 1:7185136654ce | 156 | void BG_socChangeAlertEnabled(bool enabled) |
sakthipriya | 1:7185136654ce | 157 | { |
sakthipriya | 1:7185136654ce | 158 | //Read the current 16-bit register value |
sakthipriya | 1:7185136654ce | 159 | unsigned short value = BG_readReg(REG_CONFIG); |
sakthipriya | 1:7185136654ce | 160 | |
sakthipriya | 1:7185136654ce | 161 | //Set or clear the ALSC bit |
sakthipriya | 1:7185136654ce | 162 | if (enabled) |
sakthipriya | 1:7185136654ce | 163 | value |= (1 << 6); |
sakthipriya | 1:7185136654ce | 164 | else |
sakthipriya | 1:7185136654ce | 165 | value &= ~(1 << 6); |
sakthipriya | 1:7185136654ce | 166 | |
sakthipriya | 1:7185136654ce | 167 | //Write the value back out |
sakthipriya | 1:7185136654ce | 168 | BG_writeReg(REG_CONFIG, value); |
sakthipriya | 1:7185136654ce | 169 | } |
sakthipriya | 1:7185136654ce | 170 | |
sakthipriya | 1:7185136654ce | 171 | float BG_compensation() |
sakthipriya | 1:7185136654ce | 172 | { |
sakthipriya | 1:7185136654ce | 173 | //Read the 16-bit register value |
sakthipriya | 1:7185136654ce | 174 | unsigned short value = BG_readReg(REG_CONFIG); |
sakthipriya | 1:7185136654ce | 175 | |
sakthipriya | 1:7185136654ce | 176 | //Return only the upper byte |
sakthipriya | 1:7185136654ce | 177 | return (char)(value >> 8); |
sakthipriya | 0:246d1b5f11ae | 178 | } |
sakthipriya | 0:246d1b5f11ae | 179 | |
sakthipriya | 1:7185136654ce | 180 | void BG_compensation(char rcomp) |
sakthipriya | 1:7185136654ce | 181 | { |
sakthipriya | 1:7185136654ce | 182 | //Read the current 16-bit register value |
sakthipriya | 1:7185136654ce | 183 | unsigned short value = BG_readReg(REG_CONFIG); |
sakthipriya | 1:7185136654ce | 184 | |
sakthipriya | 1:7185136654ce | 185 | //Update the register value |
sakthipriya | 1:7185136654ce | 186 | value &= 0x00FF; |
sakthipriya | 1:7185136654ce | 187 | value |= rcomp << 8; |
sakthipriya | 1:7185136654ce | 188 | |
sakthipriya | 1:7185136654ce | 189 | //Write the value back out |
sakthipriya | 1:7185136654ce | 190 | BG_writeReg(REG_CONFIG, value); |
sakthipriya | 1:7185136654ce | 191 | } |
sakthipriya | 1:7185136654ce | 192 | |
sakthipriya | 1:7185136654ce | 193 | |
sakthipriya | 1:7185136654ce | 194 | void BG_tempCompensation(float temp) |
sakthipriya | 1:7185136654ce | 195 | { |
sakthipriya | 1:7185136654ce | 196 | //Calculate the new RCOMP value |
sakthipriya | 1:7185136654ce | 197 | char rcomp; |
sakthipriya | 1:7185136654ce | 198 | if (temp > 20.0) { |
sakthipriya | 1:7185136654ce | 199 | rcomp = RCOMP0 + (temp - 20.0) * -0.5; |
sakthipriya | 1:7185136654ce | 200 | } else { |
sakthipriya | 1:7185136654ce | 201 | rcomp = RCOMP0 + (temp - 20.0) * -5.0; |
sakthipriya | 1:7185136654ce | 202 | } |
sakthipriya | 1:7185136654ce | 203 | |
sakthipriya | 1:7185136654ce | 204 | //Update the RCOMP value |
sakthipriya | 1:7185136654ce | 205 | BG_compensation(rcomp); |
sakthipriya | 1:7185136654ce | 206 | } |
sakthipriya | 1:7185136654ce | 207 | |
sakthipriya | 1:7185136654ce | 208 | // Determine whether or not the MAX17049 is asserting the ALRT pin |
sakthipriya | 1:7185136654ce | 209 | bool BG_alerting() |
sakthipriya | 1:7185136654ce | 210 | { |
sakthipriya | 1:7185136654ce | 211 | //Read the 16-bit register value |
sakthipriya | 1:7185136654ce | 212 | unsigned short value = BG_readReg(REG_CONFIG); |
sakthipriya | 1:7185136654ce | 213 | |
sakthipriya | 1:7185136654ce | 214 | //Return the status of the ALRT bit |
sakthipriya | 1:7185136654ce | 215 | if (value & (1 << 5)) |
sakthipriya | 1:7185136654ce | 216 | return true; |
sakthipriya | 1:7185136654ce | 217 | else |
sakthipriya | 1:7185136654ce | 218 | return false; |
sakthipriya | 1:7185136654ce | 219 | } |
sakthipriya | 1:7185136654ce | 220 | |
sakthipriya | 1:7185136654ce | 221 | // Command the MAX17049 to de-assert the ALRT pin |
sakthipriya | 1:7185136654ce | 222 | void BG_clearAlert() |
sakthipriya | 1:7185136654ce | 223 | { |
sakthipriya | 1:7185136654ce | 224 | //Read the current 16-bit register value |
sakthipriya | 1:7185136654ce | 225 | unsigned short value = BG_readReg(REG_CONFIG); |
sakthipriya | 1:7185136654ce | 226 | |
sakthipriya | 1:7185136654ce | 227 | //Clear the ALRT bit |
sakthipriya | 1:7185136654ce | 228 | value &= ~(1 << 5); |
sakthipriya | 1:7185136654ce | 229 | |
sakthipriya | 1:7185136654ce | 230 | //Write the value back out |
sakthipriya | 1:7185136654ce | 231 | BG_writeReg(REG_CONFIG, value); |
sakthipriya | 1:7185136654ce | 232 | } |
sakthipriya | 1:7185136654ce | 233 | |
sakthipriya | 1:7185136654ce | 234 | |
sakthipriya | 1:7185136654ce | 235 | //Set the SOC empty alert threshold of the MAX17049 |
sakthipriya | 1:7185136654ce | 236 | void BG_emptyAlertThreshold(char threshold) |
sakthipriya | 1:7185136654ce | 237 | { |
sakthipriya | 1:7185136654ce | 238 | //Read the current 16-bit register value |
sakthipriya | 1:7185136654ce | 239 | unsigned short value = BG_readReg(REG_CONFIG); |
sakthipriya | 1:7185136654ce | 240 | |
sakthipriya | 1:7185136654ce | 241 | //Update the register value |
sakthipriya | 1:7185136654ce | 242 | value &= 0xFFE0; |
sakthipriya | 1:7185136654ce | 243 | value |= 32 - threshold; |
sakthipriya | 1:7185136654ce | 244 | |
sakthipriya | 1:7185136654ce | 245 | //Write the 16-bit register |
sakthipriya | 1:7185136654ce | 246 | BG_writeReg(REG_CONFIG, value); |
sakthipriya | 1:7185136654ce | 247 | } |
sakthipriya | 1:7185136654ce | 248 | |
sakthipriya | 1:7185136654ce | 249 | // Set the low and high voltage alert threshold of the MAX17049 |
sakthipriya | 1:7185136654ce | 250 | void BG_vAlertMinMaxThreshold() |
sakthipriya | 1:7185136654ce | 251 | { |
sakthipriya | 1:7185136654ce | 252 | //Read the current 16-bit register value |
sakthipriya | 1:7185136654ce | 253 | unsigned short value = BG_readReg(REG_VALRT); |
sakthipriya | 1:7185136654ce | 254 | |
sakthipriya | 1:7185136654ce | 255 | //Mask off the old value |
sakthipriya | 1:7185136654ce | 256 | value = 0x7FFF; //??????????????? |
sakthipriya | 1:7185136654ce | 257 | |
sakthipriya | 1:7185136654ce | 258 | //Write the 16-bit register |
sakthipriya | 1:7185136654ce | 259 | BG_writeReg(REG_VALRT, value); |
sakthipriya | 1:7185136654ce | 260 | } |
sakthipriya | 1:7185136654ce | 261 | |
sakthipriya | 1:7185136654ce | 262 | |
sakthipriya | 1:7185136654ce | 263 | // Set the reset voltage threshold of the MAX17049 |
sakthipriya | 1:7185136654ce | 264 | void BG_vResetThresholdSet() |
sakthipriya | 1:7185136654ce | 265 | { |
sakthipriya | 1:7185136654ce | 266 | //Read the current 16-bit register value |
sakthipriya | 1:7185136654ce | 267 | unsigned short value = BG_readReg(REG_VRESET_ID); |
sakthipriya | 1:7185136654ce | 268 | |
sakthipriya | 1:7185136654ce | 269 | //Mask off the old //value |
sakthipriya | 1:7185136654ce | 270 | value &= 0x00FF;//Dis=0 |
sakthipriya | 1:7185136654ce | 271 | |
sakthipriya | 1:7185136654ce | 272 | value |= 0x7C00;//corresponding to 2.5 V |
sakthipriya | 1:7185136654ce | 273 | |
sakthipriya | 1:7185136654ce | 274 | |
sakthipriya | 1:7185136654ce | 275 | //write the 16-bit register |
sakthipriya | 1:7185136654ce | 276 | BG_writeReg(REG_VRESET_ID, value); |
sakthipriya | 1:7185136654ce | 277 | } |
sakthipriya | 1:7185136654ce | 278 | |
sakthipriya | 1:7185136654ce | 279 | |
sakthipriya | 1:7185136654ce | 280 | // Enable or disable the voltage reset alert on the MAX17049 |
sakthipriya | 1:7185136654ce | 281 | void BG_vResetAlertEnabled(bool enabled) |
sakthipriya | 1:7185136654ce | 282 | { |
sakthipriya | 1:7185136654ce | 283 | //Read the current 16-bit register value |
sakthipriya | 1:7185136654ce | 284 | unsigned short value = BG_readReg(REG_STATUS); |
sakthipriya | 1:7185136654ce | 285 | |
sakthipriya | 1:7185136654ce | 286 | //Set or clear the EnVR bit |
sakthipriya | 1:7185136654ce | 287 | if (enabled) |
sakthipriya | 1:7185136654ce | 288 | value |= (1 << 14); |
sakthipriya | 1:7185136654ce | 289 | else |
sakthipriya | 1:7185136654ce | 290 | value &= ~(1 << 14); |
sakthipriya | 1:7185136654ce | 291 | |
sakthipriya | 1:7185136654ce | 292 | //Write the value back out |
sakthipriya | 1:7185136654ce | 293 | BG_writeReg(REG_STATUS, value); |
sakthipriya | 1:7185136654ce | 294 | } |
sakthipriya | 1:7185136654ce | 295 | |
sakthipriya | 1:7185136654ce | 296 | //Get the current alert flags on the MAX17049 |
sakthipriya | 1:7185136654ce | 297 | //refer datasheet-status registers section to decode it. |
sakthipriya | 1:7185136654ce | 298 | char BG_alertFlags() |
sakthipriya | 1:7185136654ce | 299 | { |
sakthipriya | 1:7185136654ce | 300 | //Read the 16-bit register value |
sakthipriya | 1:7185136654ce | 301 | unsigned short value = BG_readReg(REG_STATUS); |
sakthipriya | 1:7185136654ce | 302 | |
sakthipriya | 1:7185136654ce | 303 | //Return only the flag bits |
sakthipriya | 1:7185136654ce | 304 | return (value >> 8) & 0x3F; |
sakthipriya | 1:7185136654ce | 305 | } |
sakthipriya | 1:7185136654ce | 306 | |
sakthipriya | 1:7185136654ce | 307 | // Clear all the alert flags on the MAX17049 |
sakthipriya | 1:7185136654ce | 308 | void BG_clearAlertFlags() |
sakthipriya | 1:7185136654ce | 309 | { |
sakthipriya | 1:7185136654ce | 310 | //Read the current 16-bit register value |
sakthipriya | 1:7185136654ce | 311 | unsigned short value = BG_readReg(REG_STATUS); |
sakthipriya | 1:7185136654ce | 312 | |
sakthipriya | 1:7185136654ce | 313 | //Clear the specified flag bits |
sakthipriya | 1:7185136654ce | 314 | value &= ~( 0x3F<< 8); |
sakthipriya | 1:7185136654ce | 315 | |
sakthipriya | 1:7185136654ce | 316 | //Write the value back out |
sakthipriya | 1:7185136654ce | 317 | BG_writeReg(REG_STATUS, value); |
sakthipriya | 1:7185136654ce | 318 | } |
sakthipriya | 1:7185136654ce | 319 | |
sakthipriya | 1:7185136654ce | 320 | // Get the current cell voltage measurement of the MAX17049 |
sakthipriya | 1:7185136654ce | 321 | float BG_vcell() |
sakthipriya | 1:7185136654ce | 322 | { |
sakthipriya | 1:7185136654ce | 323 | //Read the 16-bit raw Vcell value |
sakthipriya | 1:7185136654ce | 324 | unsigned short value = BG_readReg(REG_VCELL); |
sakthipriya | 1:7185136654ce | 325 | |
sakthipriya | 1:7185136654ce | 326 | //Return Vcell in volts |
sakthipriya | 1:7185136654ce | 327 | return value * 0.000078125*2; |
sakthipriya | 1:7185136654ce | 328 | } |
sakthipriya | 1:7185136654ce | 329 | |
sakthipriya | 1:7185136654ce | 330 | // Get the current state of charge measurement of the MAX17049 as a float |
sakthipriya | 1:7185136654ce | 331 | float BG_soc() |
sakthipriya | 1:7185136654ce | 332 | { |
sakthipriya | 1:7185136654ce | 333 | //Read the 16-bit raw SOC value |
sakthipriya | 1:7185136654ce | 334 | unsigned short value = BG_readReg(REG_SOC); |
sakthipriya | 1:7185136654ce | 335 | |
sakthipriya | 1:7185136654ce | 336 | //Return SOC in percent |
sakthipriya | 1:7185136654ce | 337 | return value * 0.00390625; |
sakthipriya | 1:7185136654ce | 338 | } |
sakthipriya | 1:7185136654ce | 339 | |
sakthipriya | 1:7185136654ce | 340 | |
sakthipriya | 1:7185136654ce | 341 | |
sakthipriya | 1:7185136654ce | 342 | // Get the current C rate measurement of the MAX17049 |
sakthipriya | 1:7185136654ce | 343 | float BG_crate() |
sakthipriya | 1:7185136654ce | 344 | { |
sakthipriya | 1:7185136654ce | 345 | //Read the 16-bit raw C/Rate value |
sakthipriya | 1:7185136654ce | 346 | short value = BG_readReg(REG_CRATE); |
sakthipriya | 1:7185136654ce | 347 | |
sakthipriya | 1:7185136654ce | 348 | //Return C/Rate in %/hr |
sakthipriya | 1:7185136654ce | 349 | return value * 0.208; |
sakthipriya | 1:7185136654ce | 350 | } |
sakthipriya | 1:7185136654ce | 351 | |
sakthipriya | 1:7185136654ce | 352 | |
sakthipriya | 1:7185136654ce | 353 | void FCTN_EPS_BG_INIT() |
sakthipriya | 1:7185136654ce | 354 | { |
sakthipriya | 1:7185136654ce | 355 | BG_disableSleep(); |
sakthipriya | 1:7185136654ce | 356 | BG_disableHibernate(); |
sakthipriya | 1:7185136654ce | 357 | BG_socChangeAlertEnabled(true); //enabling alert on soc changing by 1% |
sakthipriya | 1:7185136654ce | 358 | BG_emptyAlertThreshold(32);//setting empty alert threshold to 32% soc |
sakthipriya | 1:7185136654ce | 359 | BG_vAlertMinMaxThreshold();//set min, max value of Valrt register |
sakthipriya | 1:7185136654ce | 360 | BG_vResetThresholdSet();//set threshold voltage for reset |
sakthipriya | 1:7185136654ce | 361 | BG_vResetAlertEnabled(true);//enable alert on reset for V < Vreset |
sakthipriya | 1:7185136654ce | 362 | } |
sakthipriya | 1:7185136654ce | 363 | |
sakthipriya | 1:7185136654ce | 364 | void FCTN_EPS_BG_MAIN() |
sakthipriya | 1:7185136654ce | 365 | { |
sakthipriya | 1:7185136654ce | 366 | float temp=Sensor.BatteryTemperature ; //(from temp sensor on battery board) //value of battery temperature in degree Celsius. Should be updated everytime. |
sakthipriya | 1:7185136654ce | 367 | float Battery_parameters[4]; |
sakthipriya | 1:7185136654ce | 368 | BG_tempCompensation(temp); |
sakthipriya | 1:7185136654ce | 369 | Battery_parameters[0]=BG_vcell(); |
sakthipriya | 1:7185136654ce | 370 | Battery_parameters[1]=BG_soc(); |
sakthipriya | 1:7185136654ce | 371 | Battery_parameters[2]=BG_crate(); |
sakthipriya | 1:7185136654ce | 372 | FCTN_EPS_POWERMODE(Battery_parameters[1]) ; //updating power mode |
sakthipriya | 1:7185136654ce | 373 | printf("\nVcell=%f",BG_vcell()); //remove this for final code |
sakthipriya | 1:7185136654ce | 374 | printf("\nSOC=%f",BG_soc()); //remove this for final code |
sakthipriya | 1:7185136654ce | 375 | printf("\nC_rate=%f",BG_crate()); //remove this for final code |
sakthipriya | 1:7185136654ce | 376 | if (BG_alerting()== true) //alert is on |
sakthipriya | 1:7185136654ce | 377 | { |
sakthipriya | 1:7185136654ce | 378 | Battery_parameters[3]=BG_alertFlags(); |
sakthipriya | 1:7185136654ce | 379 | BG_clearAlert();//clear alert |
sakthipriya | 1:7185136654ce | 380 | BG_clearAlertFlags();//clear all alert flags |
sakthipriya | 1:7185136654ce | 381 | } |
sakthipriya | 1:7185136654ce | 382 | } |
sakthipriya | 1:7185136654ce | 383 | |
sakthipriya | 1:7185136654ce | 384 | //----------------------------------------------------Power algo code--------------------------------------------------------------------// |
sakthipriya | 1:7185136654ce | 385 | int FCTN_EPS_POWERMODE(float soc) //dummy algo |
sakthipriya | 1:7185136654ce | 386 | { |
sakthipriya | 1:7185136654ce | 387 | if(soc >= 80) |
sakthipriya | 1:7185136654ce | 388 | power_mode = 4; |
sakthipriya | 1:7185136654ce | 389 | else if(soc >= 70 & soc < 80) |
sakthipriya | 1:7185136654ce | 390 | power_mode = 3; |
sakthipriya | 1:7185136654ce | 391 | else if(soc >= 60 & soc < 70) |
sakthipriya | 1:7185136654ce | 392 | power_mode = 2; |
sakthipriya | 1:7185136654ce | 393 | else if(soc < 60) |
sakthipriya | 1:7185136654ce | 394 | power_mode = 1; |
sakthipriya | 1:7185136654ce | 395 | } |
sakthipriya | 1:7185136654ce | 396 | |
sakthipriya | 1:7185136654ce | 397 | extern int beacon_sc; |
sakthipriya | 1:7185136654ce | 398 | extern int acs_pflag; |
sakthipriya | 1:7185136654ce | 399 | void FCTN_EPS_CTRLPOWER(int power_mode) //algo has to be changed based on report from eps team |
sakthipriya | 1:7185136654ce | 400 | { |
sakthipriya | 1:7185136654ce | 401 | printf("Entered Power Management \n"); |
sakthipriya | 1:7185136654ce | 402 | printf("Battery Level %d \n",btrylvl); |
sakthipriya | 1:7185136654ce | 403 | switch(power_mode) |
sakthipriya | 1:7185136654ce | 404 | { |
sakthipriya | 1:7185136654ce | 405 | case 1: beacon_sc = 3; //high power mode : everything is on |
sakthipriya | 1:7185136654ce | 406 | acs_pflag = 1; |
sakthipriya | 1:7185136654ce | 407 | TRXY = 1; |
sakthipriya | 1:7185136654ce | 408 | TRZ = 1; |
sakthipriya | 1:7185136654ce | 409 | break; |
sakthipriya | 1:7185136654ce | 410 | |
sakthipriya | 1:7185136654ce | 411 | case 2: beacon_sc = 3; |
sakthipriya | 1:7185136654ce | 412 | acs_pflag = 0; //stops control algo and pwmgen in code |
sakthipriya | 1:7185136654ce | 413 | TRXY = 0; |
sakthipriya | 1:7185136654ce | 414 | TRZ = 0; |
sakthipriya | 1:7185136654ce | 415 | break; |
sakthipriya | 1:7185136654ce | 416 | |
sakthipriya | 1:7185136654ce | 417 | case 3: beacon_sc = 3; |
sakthipriya | 1:7185136654ce | 418 | acs_pflag = 0; //stops control algo and pwmgen in code |
sakthipriya | 1:7185136654ce | 419 | TRXY = 0; |
sakthipriya | 1:7185136654ce | 420 | TRZ = 0; |
sakthipriya | 1:7185136654ce | 421 | break; |
sakthipriya | 1:7185136654ce | 422 | |
sakthipriya | 1:7185136654ce | 423 | default : beacon_sc = 30; //least power mode : beacon is in low power mode : |
sakthipriya | 1:7185136654ce | 424 | acs_pflag = 0; //stops control algo and pwmgen in code |
sakthipriya | 1:7185136654ce | 425 | TRXY = 0; |
sakthipriya | 1:7185136654ce | 426 | TRZ = 0; |
sakthipriya | 1:7185136654ce | 427 | } |
sakthipriya | 1:7185136654ce | 428 | |
sakthipriya | 1:7185136654ce | 429 | //---------------------------------------------------func to quantize--------------------------------------------------------------------// |
sakthipriya | 0:246d1b5f11ae | 430 | |
sakthipriya | 0:246d1b5f11ae | 431 | int FCTN_QUANTIZE(float start,float step,float input_data) // accepts min and measured values and step->quantises on a scale 0-15..(4 bit quantisation) |
sakthipriya | 0:246d1b5f11ae | 432 | { |
sakthipriya | 0:246d1b5f11ae | 433 | int quant_data = (input_data - start)/step; |
sakthipriya | 0:246d1b5f11ae | 434 | if(quant_data <= 0) |
sakthipriya | 0:246d1b5f11ae | 435 | quant_data = 0; |
sakthipriya | 0:246d1b5f11ae | 436 | if(quant_data >= 15) |
sakthipriya | 0:246d1b5f11ae | 437 | quant_data = 15; |
sakthipriya | 0:246d1b5f11ae | 438 | return quant_data; |
sakthipriya | 0:246d1b5f11ae | 439 | } |
sakthipriya | 1:7185136654ce | 440 | |
sakthipriya | 1:7185136654ce | 441 | //---------------------------------------------------func to fill beac structure---------------------------------------------------------// |
sakthipriya | 1:7185136654ce | 442 | |
sakthipriya | 0:246d1b5f11ae | 443 | |
sakthipriya | 1:7185136654ce | 444 | void FCTN_WRITE_BEASTRUCT(ShortBeacy* bea_struct,SensorDataQuantised quant_data) |
sakthipriya | 0:246d1b5f11ae | 445 | { |
sakthipriya | 1:7185136654ce | 446 | (*bea_struct).Voltage[0] = 2; //quantised value |
sakthipriya | 1:7185136654ce | 447 | (*bea_struct).Temp[0] = quant_data.Temperature[0]; //quantised value |
sakthipriya | 1:7185136654ce | 448 | (*bea_struct).Temp[1] = quant_data.Temperature[1]; //quantised value |
sakthipriya | 1:7185136654ce | 449 | (*bea_struct).AngularSpeed[0] = quant_data.AngularSpeed[0]; |
sakthipriya | 1:7185136654ce | 450 | (*bea_struct).AngularSpeed[1] = quant_data.AngularSpeed[1]; |
sakthipriya | 0:246d1b5f11ae | 451 | |
sakthipriya | 1:7185136654ce | 452 | (*bea_struct).SubsystemStatus[0] = 145; //dummy values----------to be changed------------------- |
sakthipriya | 1:7185136654ce | 453 | (*bea_struct).ErrorFlag[0] = 3; //dummy values----------to be changed------------------- |
sakthipriya | 0:246d1b5f11ae | 454 | } |
sakthipriya | 0:246d1b5f11ae | 455 | |
sakthipriya | 1:7185136654ce | 456 | //---------------------------------------------------HK_MAIN-----------------------------------------------------------------------// |
sakthipriya | 0:246d1b5f11ae | 457 | |
sakthipriya | 1:7185136654ce | 458 | void FCTN_EPS_HK_MAIN() |
sakthipriya | 0:246d1b5f11ae | 459 | { |
sakthipriya | 0:246d1b5f11ae | 460 | SelectLinesA[0] = SelectLinesA[1] = SelectLinesA[2] = SelectLinesA[3] = 0; |
sakthipriya | 1:7185136654ce | 461 | SelectLinesB[3]= SelectLinesB[2] = SelectLinesB[1]=SelectLinesB[0] = 0; //initialise all selectlines to zeroes->1st line of muxes selected |
sakthipriya | 0:246d1b5f11ae | 462 | int loop_iterator = 0; |
sakthipriya | 0:246d1b5f11ae | 463 | int select_line_iterator = 3; |
sakthipriya | 0:246d1b5f11ae | 464 | |
sakthipriya | 0:246d1b5f11ae | 465 | for(loop_iterator = 0; loop_iterator < 16; loop_iterator++) //measurement from voltage sensor=> 16 sensors in place |
sakthipriya | 0:246d1b5f11ae | 466 | { |
sakthipriya | 1:7185136654ce | 467 | if(loop_iterator<15) |
sakthipriya | 1:7185136654ce | 468 | { |
sakthipriya | 1:7185136654ce | 469 | //read the sensor values and stores them in 'SensorData' structure's variable 'Sensor' |
sakthipriya | 1:7185136654ce | 470 | Sensor.Voltage[loop_iterator] = (VoltageInput.read()*3.3*5.545454);//resistors in voltage divider=>15Mohm,3.3Mohm |
sakthipriya | 0:246d1b5f11ae | 471 | |
sakthipriya | 1:7185136654ce | 472 | if(loop_iterator%2 == 0) |
sakthipriya | 1:7185136654ce | 473 | SensorQuantised.Voltage[loop_iterator/2] = FCTN_QUANTIZE(vstart,vstep,Sensor.Voltage[loop_iterator]); |
sakthipriya | 0:246d1b5f11ae | 474 | |
sakthipriya | 1:7185136654ce | 475 | else |
sakthipriya | 1:7185136654ce | 476 | SensorQuantised.Voltage[(loop_iterator)/2] = SensorQuantised.Voltage[(loop_iterator)/2]<<4 + FCTN_QUANTIZE(vstart,vstep,Sensor.Voltage[loop_iterator]); |
sakthipriya | 1:7185136654ce | 477 | } |
sakthipriya | 0:246d1b5f11ae | 478 | else |
sakthipriya | 1:7185136654ce | 479 | { |
sakthipriya | 1:7185136654ce | 480 | Sensor.Temperature[1] = (VoltageInput.read()*3.3*(-90.7)+ 190.1543); |
sakthipriya | 1:7185136654ce | 481 | SensorQuantised.Temperature[0]=FCTN_QUANTIZE(tstart,tstep,Sensor.Temperature[0]); |
sakthipriya | 1:7185136654ce | 482 | } |
sakthipriya | 0:246d1b5f11ae | 483 | //iterate the select lines from 0 to 15 |
sakthipriya | 0:246d1b5f11ae | 484 | for(select_line_iterator = 3;select_line_iterator >= 0;select_line_iterator--) |
sakthipriya | 0:246d1b5f11ae | 485 | { |
sakthipriya | 0:246d1b5f11ae | 486 | if(SelectLinesA[select_line_iterator] == 0) |
sakthipriya | 0:246d1b5f11ae | 487 | { |
sakthipriya | 0:246d1b5f11ae | 488 | SelectLinesA[select_line_iterator] = 1; |
sakthipriya | 0:246d1b5f11ae | 489 | break; |
sakthipriya | 0:246d1b5f11ae | 490 | } |
sakthipriya | 0:246d1b5f11ae | 491 | else SelectLinesA[select_line_iterator] = 0; |
sakthipriya | 0:246d1b5f11ae | 492 | } |
sakthipriya | 0:246d1b5f11ae | 493 | |
sakthipriya | 0:246d1b5f11ae | 494 | wait_us(10.0); // A delay of 10 microseconds between each sensor output. Can be changed. |
sakthipriya | 0:246d1b5f11ae | 495 | } |
sakthipriya | 0:246d1b5f11ae | 496 | |
sakthipriya | 0:246d1b5f11ae | 497 | |
sakthipriya | 0:246d1b5f11ae | 498 | //measurement from current sensor=> 8 sensors in place |
sakthipriya | 1:7185136654ce | 499 | for(loop_iterator = 0; loop_iterator < 7; loop_iterator++) |
sakthipriya | 0:246d1b5f11ae | 500 | { |
sakthipriya | 0:246d1b5f11ae | 501 | //read the sensor values and stores them in 'SensorData' structure variable 'Sensor' |
sakthipriya | 0:246d1b5f11ae | 502 | Sensor.Current[loop_iterator] = (CurrentInput.read()*3.3/(50*rsens)); |
sakthipriya | 0:246d1b5f11ae | 503 | if(loop_iterator%2 == 0) |
sakthipriya | 0:246d1b5f11ae | 504 | SensorQuantised.Current[loop_iterator/2] = FCTN_QUANTIZE(cstart,cstep,Sensor.Current[loop_iterator]); |
sakthipriya | 0:246d1b5f11ae | 505 | else |
sakthipriya | 0:246d1b5f11ae | 506 | SensorQuantised.Current[(loop_iterator)/2] = SensorQuantised.Current[(loop_iterator)/2]<<4 + FCTN_QUANTIZE(cstart,cstep,Sensor.Current[loop_iterator]); |
sakthipriya | 1:7185136654ce | 507 | |
sakthipriya | 0:246d1b5f11ae | 508 | //iterate the select lines from 0 to 7 |
sakthipriya | 1:7185136654ce | 509 | for(select_line_iterator = 3;select_line_iterator >= 0;select_line_iterator--) |
sakthipriya | 0:246d1b5f11ae | 510 | { |
sakthipriya | 0:246d1b5f11ae | 511 | if(SelectLinesB[select_line_iterator] == 0) |
sakthipriya | 0:246d1b5f11ae | 512 | { |
sakthipriya | 0:246d1b5f11ae | 513 | SelectLinesB[select_line_iterator] = 1; |
sakthipriya | 0:246d1b5f11ae | 514 | break; |
sakthipriya | 0:246d1b5f11ae | 515 | } |
sakthipriya | 0:246d1b5f11ae | 516 | else SelectLinesB[select_line_iterator] = 0; |
sakthipriya | 0:246d1b5f11ae | 517 | |
sakthipriya | 0:246d1b5f11ae | 518 | } |
sakthipriya | 0:246d1b5f11ae | 519 | |
sakthipriya | 0:246d1b5f11ae | 520 | wait_us(10.0); // A delay of 10 microseconds between each sensor output. Can be changed. |
sakthipriya | 0:246d1b5f11ae | 521 | } |
sakthipriya | 0:246d1b5f11ae | 522 | |
sakthipriya | 0:246d1b5f11ae | 523 | |
sakthipriya | 0:246d1b5f11ae | 524 | |
sakthipriya | 0:246d1b5f11ae | 525 | //update magnetometer data-> |
sakthipriya | 0:246d1b5f11ae | 526 | //populate values in structure variable 'Sensor' from data to be given by Green |
sakthipriya | 0:246d1b5f11ae | 527 | SensorQuantised.AngularSpeed[0] = FCTN_QUANTIZE(AngularSpeed_start,AngularSpeed_step,Sensor.AngularSpeed[0]); |
sakthipriya | 0:246d1b5f11ae | 528 | SensorQuantised.AngularSpeed[0] = SensorQuantised.AngularSpeed[0]<<4 + FCTN_QUANTIZE(AngularSpeed_start,AngularSpeed_step,Sensor.AngularSpeed[1]); |
sakthipriya | 0:246d1b5f11ae | 529 | SensorQuantised.AngularSpeed[1] = FCTN_QUANTIZE(AngularSpeed_start,AngularSpeed_step,Sensor.AngularSpeed[2]); |
sakthipriya | 0:246d1b5f11ae | 530 | |
sakthipriya | 0:246d1b5f11ae | 531 | //update gyro data-> |
sakthipriya | 0:246d1b5f11ae | 532 | //populate values in structure variable 'Sensor' from data to be given by Green |
sakthipriya | 0:246d1b5f11ae | 533 | SensorQuantised.Bnewvalue[0] = FCTN_QUANTIZE(Bnewvalue_start,Bnewvalue_step,Sensor.Bnewvalue[0]); |
sakthipriya | 0:246d1b5f11ae | 534 | SensorQuantised.Bnewvalue[0] = SensorQuantised.Bnewvalue[0]<<4 + FCTN_QUANTIZE(Bnewvalue_start,Bnewvalue_step,Sensor.Bnewvalue[1]); |
sakthipriya | 0:246d1b5f11ae | 535 | SensorQuantised.Bnewvalue[1] = FCTN_QUANTIZE(Bnewvalue_start,Bnewvalue_step,Sensor.Bnewvalue[2]); |
sakthipriya | 1:7185136654ce | 536 | |
sakthipriya | 0:246d1b5f11ae | 537 | //update beacon structure |
sakthipriya | 1:7185136654ce | 538 | FCTN_WRITE_BEASTRUCT(&Shortbeacon,SensorQuantised);//Shortbeacon is passed |
sakthipriya | 1:7185136654ce | 539 | |
sakthipriya | 0:246d1b5f11ae | 540 | } |
sakthipriya | 0:246d1b5f11ae | 541 | |
sakthipriya | 0:246d1b5f11ae | 542 |