ML5238

Committer:
gscuttari
Date:
Mon Apr 15 10:20:43 2019 +0000
Revision:
0:6a895c900427
Child:
1:9d8518740e5c
ML5238

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gscuttari 0:6a895c900427 1 /*****************************************************************************
gscuttari 0:6a895c900427 2 bms.h
gscuttari 0:6a895c900427 3
gscuttari 0:6a895c900427 4 Copyright (C) 2019 SQC Systems s.r.l.
gscuttari 0:6a895c900427 5 All rights reserved.
gscuttari 0:6a895c900427 6
gscuttari 0:6a895c900427 7 SQC Systems shall not be liable for any direct, indirect,
gscuttari 0:6a895c900427 8 consequential or incidental damages arising from using or modifying this
gscuttari 0:6a895c900427 9 program.
gscuttari 0:6a895c900427 10
gscuttari 0:6a895c900427 11 History
gscuttari 0:6a895c900427 12 2019.03.19 ver.1.00
gscuttari 0:6a895c900427 13
gscuttari 0:6a895c900427 14 ******************************************************************************/
gscuttari 0:6a895c900427 15 #ifndef _BMS_H_
gscuttari 0:6a895c900427 16 #define _BMS_H_
gscuttari 0:6a895c900427 17
gscuttari 0:6a895c900427 18
gscuttari 0:6a895c900427 19 /**********************************
gscuttari 0:6a895c900427 20 STATE OF BMS
gscuttari 0:6a895c900427 21 **********************************/
gscuttari 0:6a895c900427 22 // Define State of BMS
gscuttari 0:6a895c900427 23 #define START_MODE 1
gscuttari 0:6a895c900427 24 #define RUN_MODE 2
gscuttari 0:6a895c900427 25 #define STANDBY_MODE 3
gscuttari 0:6a895c900427 26 #define ERROR_MODE 4
gscuttari 0:6a895c900427 27 #define CHARGING_MODE 5
gscuttari 0:6a895c900427 28 #define RESTORE_MODE 6
gscuttari 0:6a895c900427 29 #define LOW_BATTERY_MODE 7
gscuttari 0:6a895c900427 30 #define POWER_DOWN_MODE 8
gscuttari 0:6a895c900427 31 #define SHORT_CIRCUIT_MODE 9
gscuttari 0:6a895c900427 32 #define HIGH_TEMPERATURE_CUTOFF 10
gscuttari 0:6a895c900427 33 #define LOW_TEMPERATURE_CUTOFF 11
gscuttari 0:6a895c900427 34
gscuttari 0:6a895c900427 35 /**********************************
gscuttari 0:6a895c900427 36 EVENTS
gscuttari 0:6a895c900427 37 **********************************/
gscuttari 0:6a895c900427 38
gscuttari 0:6a895c900427 39 // Define Events
gscuttari 0:6a895c900427 40 #define EVENT_POWER_ON 0
gscuttari 0:6a895c900427 41 #define EVENT_SHORT_CIRCUIT 1
gscuttari 0:6a895c900427 42 #define EVENT_OVER_TEMP 2
gscuttari 0:6a895c900427 43 #define EVENT_START_BMS 3
gscuttari 0:6a895c900427 44 #define EVENT_LOAD_DISCONNECTED 4
gscuttari 0:6a895c900427 45 #define EVENT_LOAD_RECONNECTED 5
gscuttari 0:6a895c900427 46 #define EVENT_CHARGER_DISCONNECTED 6
gscuttari 0:6a895c900427 47 #define EVENT_CHARGER_RECONNECTED 7
gscuttari 0:6a895c900427 48 #define EVENT_BATTERY_TOO_LOW 8
gscuttari 0:6a895c900427 49 #define EVENT_BATTERY_DOWN 9
gscuttari 0:6a895c900427 50 #define EVENT_BATTERY_IS_FULL 10
gscuttari 0:6a895c900427 51 #define EVENT_BATTERY_IS_OK 11
gscuttari 0:6a895c900427 52 #define EVENT_GOTO_RUN_FROM_RESTORE 12
gscuttari 0:6a895c900427 53
gscuttari 0:6a895c900427 54 /**********************************
gscuttari 0:6a895c900427 55 TALK and DONE
gscuttari 0:6a895c900427 56 **********************************/
gscuttari 0:6a895c900427 57 #define TALK (SS_PIN = 0); // Chip Select
gscuttari 0:6a895c900427 58 #define DONE (SS_PIN = 1); // Chip Deselect
gscuttari 0:6a895c900427 59
gscuttari 0:6a895c900427 60 #define SET_CAN_IN_HIGH_SPEED_MODE (RS = 0); // Set Can Bus in high speed mode
gscuttari 0:6a895c900427 61 #define SET_CAN_BUS_IN_LISTEN_MODE (RS = 1); // Set Can Bus in Listen Only Mode
gscuttari 0:6a895c900427 62
gscuttari 0:6a895c900427 63 char VMON_DATA_WRITE[] = {0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A , 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
gscuttari 0:6a895c900427 64 int STATE_OF_BMS;
gscuttari 0:6a895c900427 65 double VIM0;
gscuttari 0:6a895c900427 66 double VIM1;
gscuttari 0:6a895c900427 67 double VR;
gscuttari 0:6a895c900427 68 double Gim;
gscuttari 0:6a895c900427 69 double Vimz;
gscuttari 0:6a895c900427 70 double Vimon;
gscuttari 0:6a895c900427 71 double Isense;
gscuttari 0:6a895c900427 72 double V_ref = 3.4f;
gscuttari 0:6a895c900427 73 double Rsense = 0.003f;
gscuttari 0:6a895c900427 74 bool schort_circuit_detection = false;
gscuttari 0:6a895c900427 75 double instant_current_detected;
gscuttari 0:6a895c900427 76
gscuttari 0:6a895c900427 77 float LOWVOLTAGEWARNING = 34.0; // Define low voltage warning
gscuttari 0:6a895c900427 78 float LOWVOLTAGECUTOFF = 33.0; // Define low voltage cut off
gscuttari 0:6a895c900427 79 float HIVOLTAGESHUNT = 3.8; // Define high voltege limit for balancing
gscuttari 0:6a895c900427 80 float VOLTAGEALLOWANCE = 0.009; // Define the accuracy the balancing algorithm can balance it to.
gscuttari 0:6a895c900427 81 float HIGHVOLTAGECUTOFF = 39.0; // Define high voltage cut off
gscuttari 0:6a895c900427 82 int TOTALCELLS = 16; // Number Of cells
gscuttari 0:6a895c900427 83
gscuttari 0:6a895c900427 84
gscuttari 0:6a895c900427 85 float voltageHighest;
gscuttari 0:6a895c900427 86 float voltageLowest;
gscuttari 0:6a895c900427 87 int highestCellNumber;
gscuttari 0:6a895c900427 88 int lowestCellNumber;
gscuttari 0:6a895c900427 89
gscuttari 0:6a895c900427 90
gscuttari 0:6a895c900427 91 float cellVoltage[17];
gscuttari 0:6a895c900427 92 float difference[17];
gscuttari 0:6a895c900427 93
gscuttari 0:6a895c900427 94 float voltageTotal;
gscuttari 0:6a895c900427 95 float voltageAverage;
gscuttari 0:6a895c900427 96 float temp_read = 0;
gscuttari 0:6a895c900427 97 float temp_min = -5;
gscuttari 0:6a895c900427 98 float temp_warning = 40;
gscuttari 0:6a895c900427 99 float temp_cutoff = 60;
gscuttari 0:6a895c900427 100 float thr_val=0, thr_res=0, temperature=0;
gscuttari 0:6a895c900427 101 unsigned char read_buf,read_buf2,read_fet, read_power, rdata;
gscuttari 0:6a895c900427 102 double gain_correction = 0;
gscuttari 0:6a895c900427 103
gscuttari 0:6a895c900427 104
gscuttari 0:6a895c900427 105 // Status flags READ ONLY
gscuttari 0:6a895c900427 106 bool DF_STATUS = false; // Mosfet OUT
gscuttari 0:6a895c900427 107 bool CF_STATUS = false; // Mosfet CHARGER
gscuttari 0:6a895c900427 108 bool PSW_STATUS = false; // Power safe mode
gscuttari 0:6a895c900427 109 bool INT_STATUS = false; // INT bit indicates the /INTO pin output status.
gscuttari 0:6a895c900427 110 bool RPSL_STATUS = false; // RPSL bit indicates interrupt status of charger disconnecting interrupt if charge over-current detected.
gscuttari 0:6a895c900427 111 bool RPSH_STATUS = false; // RPSH bit indicates interrupt status of charger disconnecting interrupt if the status is power down.
gscuttari 0:6a895c900427 112 bool RRS_STATUS = false; // RRS bit indicates interrupt status of load disconnecting interrupt
gscuttari 0:6a895c900427 113 bool RSC_STATUS = false; // RSC bit indicates interrupt status of short current detecting interrupt.
gscuttari 0:6a895c900427 114
gscuttari 0:6a895c900427 115
gscuttari 0:6a895c900427 116
gscuttari 0:6a895c900427 117 // status flags of power status
gscuttari 0:6a895c900427 118 bool PSV_STATUS = false;
gscuttari 0:6a895c900427 119 bool PDWN_STATUS = false;
gscuttari 0:6a895c900427 120 bool PUPIN_STATUS = false;
gscuttari 0:6a895c900427 121
gscuttari 0:6a895c900427 122
gscuttari 0:6a895c900427 123
gscuttari 0:6a895c900427 124 // Setup ML5238
gscuttari 0:6a895c900427 125 void START_UP_ML5238();
gscuttari 0:6a895c900427 126 bool SET_ENABLE = true;
gscuttari 0:6a895c900427 127 bool SET_DISABLE = false;
gscuttari 0:6a895c900427 128 bool SET_RUN = true;
gscuttari 0:6a895c900427 129 bool SET_STOP = false;
gscuttari 0:6a895c900427 130
gscuttari 0:6a895c900427 131
gscuttari 0:6a895c900427 132 void handle_interrupt();
gscuttari 0:6a895c900427 133 void read_instant_current();
gscuttari 0:6a895c900427 134 float read_temperature_of_battery();
gscuttari 0:6a895c900427 135 void events_of_bms();
gscuttari 0:6a895c900427 136 void actions();
gscuttari 0:6a895c900427 137 float vTotal();
gscuttari 0:6a895c900427 138 int highestCell();
gscuttari 0:6a895c900427 139 int lowestCell();
gscuttari 0:6a895c900427 140 float average_voltage();
gscuttari 0:6a895c900427 141
gscuttari 0:6a895c900427 142
gscuttari 0:6a895c900427 143
gscuttari 0:6a895c900427 144 typedef struct _Register_Map_structure {
gscuttari 0:6a895c900427 145 float Cell_1_voltage;
gscuttari 0:6a895c900427 146 float Cell_2_voltage;
gscuttari 0:6a895c900427 147 float Cell_3_voltage;
gscuttari 0:6a895c900427 148 float Cell_4_voltage;
gscuttari 0:6a895c900427 149 float Cell_5_voltage;
gscuttari 0:6a895c900427 150 float Cell_6_voltage;
gscuttari 0:6a895c900427 151 float Cell_7_voltage;
gscuttari 0:6a895c900427 152 float Cell_8_voltage;
gscuttari 0:6a895c900427 153 float Cell_9_voltage;
gscuttari 0:6a895c900427 154 float Cell_10_voltage;
gscuttari 0:6a895c900427 155 float Cell_11_voltage;
gscuttari 0:6a895c900427 156 float Cell_12_voltage;
gscuttari 0:6a895c900427 157 float Cell_13_voltage;
gscuttari 0:6a895c900427 158 float Cell_14_voltage;
gscuttari 0:6a895c900427 159 float Cell_15_voltage;
gscuttari 0:6a895c900427 160 float Cell_16_voltage;
gscuttari 0:6a895c900427 161 char Bms_uptime[32];
gscuttari 0:6a895c900427 162 uint32_t Estimated_time_left;
gscuttari 0:6a895c900427 163 float Battery_voltage;
gscuttari 0:6a895c900427 164 float Battery_current;
gscuttari 0:6a895c900427 165 float Min_cell_voltage;
gscuttari 0:6a895c900427 166 float Max_cell_voltage;
gscuttari 0:6a895c900427 167 float External_temp_sensor_1;
gscuttari 0:6a895c900427 168 float External_temp_sensor_2;
gscuttari 0:6a895c900427 169 uint16_t Distance_left;
gscuttari 0:6a895c900427 170 uint32_t State_of_charge;
gscuttari 0:6a895c900427 171 float On_board_temperature_sensor;
gscuttari 0:6a895c900427 172 uint16_t Battery_status;
gscuttari 0:6a895c900427 173 float Voltageaverage;
gscuttari 0:6a895c900427 174 float InstantPower;
gscuttari 0:6a895c900427 175 float Speed;
gscuttari 0:6a895c900427 176 } Register_Map_structure;
gscuttari 0:6a895c900427 177 Register_Map_structure Register_Map;
gscuttari 0:6a895c900427 178
gscuttari 0:6a895c900427 179
gscuttari 0:6a895c900427 180
gscuttari 0:6a895c900427 181 #endif /*_BMS_H_*/