![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
all bms comunicating with nucleo board and balancing
Fork of Can_sniffer_BMS_GER by
main.cpp@30:1bc79ae58a42, 2016-10-25 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Tue Oct 25 12:50:07 2016 +0000
- Revision:
- 30:1bc79ae58a42
- Parent:
- 29:3af76325f86a
- Child:
- 31:6427569cadbe
not reading voltages during discharge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:c5e5d0df6f2a | 1 | /* |
hudakz | 16:a86f339d1c25 | 2 | * An example showing how to use the CANnucleo library: |
hudakz | 0:c5e5d0df6f2a | 3 | * |
ser1516 | 21:13ac27349025 | 4 | * Two affordable (less than $3 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash), |
ser1516 | 21:13ac27349025 | 5 | * (see [https://developer.mbed.org/users/hudakz/code/STM32F103C8T6_Hello/] for more details) |
ser1516 | 21:13ac27349025 | 6 | * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.). |
ser1516 | 21:13ac27349025 | 7 | * CAN transceivers are not part of NUCLEO boards, therefore must be added by you. |
hudakz | 6:7ff95ce72f6d | 8 | * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends. |
hudakz | 6:7ff95ce72f6d | 9 | * |
hudakz | 16:a86f339d1c25 | 10 | * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/> |
hudakz | 6:7ff95ce72f6d | 11 | * |
hudakz | 17:18d4d0ff26a6 | 12 | * NOTE: If you'd like to use the official NUCLEO boards comment out line 22 |
hudakz | 6:7ff95ce72f6d | 13 | * |
hudakz | 6:7ff95ce72f6d | 14 | * The same code is used for both NUCLEO boards, but: |
hudakz | 0:c5e5d0df6f2a | 15 | * For board #1 compile the example without any change. |
ser1516 | 21:13ac27349025 | 16 | * For board #2 comment out line 23 before compiling |
hudakz | 4:ccf4ac2deac8 | 17 | * |
hudakz | 6:7ff95ce72f6d | 18 | * Once the binaries have been downloaded to the boards reset board #1. |
hudakz | 0:c5e5d0df6f2a | 19 | * |
ser1516 | 21:13ac27349025 | 20 | */ |
hudakz | 0:c5e5d0df6f2a | 21 | |
ser1516 | 21:13ac27349025 | 22 | //#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards! |
ser1516 | 21:13ac27349025 | 23 | //#define BOARD1 1 // comment out this line when compiling for board #2 |
hudakz | 0:c5e5d0df6f2a | 24 | |
ser1516 | 21:13ac27349025 | 25 | |
Crazyaboutmachines | 29:3af76325f86a | 26 | |
Crazyaboutmachines | 26:3ac15dfbb66b | 27 | //CAN devices IDs and reserved ID's----------------------------------- |
Crazyaboutmachines | 23:1a29761becd2 | 28 | //ID|Device |
Crazyaboutmachines | 26:3ac15dfbb66b | 29 | //9|ECU|Key switch |
Crazyaboutmachines | 26:3ac15dfbb66b | 30 | //10|ECU|Charge/discharge |
Crazyaboutmachines | 26:3ac15dfbb66b | 31 | //11|BMS1|Cell voltages |
Crazyaboutmachines | 26:3ac15dfbb66b | 32 | //12|BMS2|Cell voltages |
Crazyaboutmachines | 26:3ac15dfbb66b | 33 | //13|BMS3|Cell voltages |
Crazyaboutmachines | 26:3ac15dfbb66b | 34 | //?|Charger| |
Crazyaboutmachines | 26:3ac15dfbb66b | 35 | // |
Crazyaboutmachines | 26:3ac15dfbb66b | 36 | //-------------------------------------------------------------------- |
Crazyaboutmachines | 23:1a29761becd2 | 37 | |
hudakz | 0:c5e5d0df6f2a | 38 | |
Crazyaboutmachines | 29:3af76325f86a | 39 | |
Crazyaboutmachines | 29:3af76325f86a | 40 | |
Crazyaboutmachines | 29:3af76325f86a | 41 | /*************Zivan NG3-G7MICB Charger(Input 230VAC-19A;Output 96 VDC-25A)***** |
Crazyaboutmachines | 29:3af76325f86a | 42 | FLuxograma do processo de carregamento: |
Crazyaboutmachines | 29:3af76325f86a | 43 | (1) Primeira parte do precharge |
Crazyaboutmachines | 29:3af76325f86a | 44 | (2) Conectar baterias |
Crazyaboutmachines | 29:3af76325f86a | 45 | (3) Segunda parte do precharge (inicio de carga) |
Crazyaboutmachines | 29:3af76325f86a | 46 | (4) Mensagens periodicas 0x6C1 (modulação da corrente) |
Crazyaboutmachines | 29:3af76325f86a | 47 | (4) Carregar a 25A atepremeira celula chegar aos 4.1V =>10A |
Crazyaboutmachines | 29:3af76325f86a | 48 | (5) Parar carregamento quando tensao de todas as celulas= 4.1V ou de uma das celulas for 4.2V |
Crazyaboutmachines | 29:3af76325f86a | 49 | (6) Desligar carregador e desconectar bateria ???(ver manual) |
Crazyaboutmachines | 29:3af76325f86a | 50 | |
Crazyaboutmachines | 29:3af76325f86a | 51 | Tarefas: |
Crazyaboutmachines | 29:3af76325f86a | 52 | -acabar de formatar o codigo do BMS encurtando interrupts, programando todas as BMS's e aumentando fiabilidade, etc |
Crazyaboutmachines | 29:3af76325f86a | 53 | -ligar carregador e ver que tensoes de CAN e de saida manda cá para fora+ ver que mensagens |
Crazyaboutmachines | 29:3af76325f86a | 54 | -experimentar leer valores programados no charger com SDO messages (ver o que é um lsb) |
Crazyaboutmachines | 29:3af76325f86a | 55 | -experimentar fazer um procedimento de precharge |
Crazyaboutmachines | 29:3af76325f86a | 56 | -se a play and drive nao responder a tempo entao fazer logo com precharge |
Crazyaboutmachines | 29:3af76325f86a | 57 | |
Crazyaboutmachines | 29:3af76325f86a | 58 | Mensagens: |
Crazyaboutmachines | 29:3af76325f86a | 59 | (charger CAN node is 11 by default. So the Id of the message will be 0x60B in |
Crazyaboutmachines | 29:3af76325f86a | 60 | hexadecimal) |
Crazyaboutmachines | 29:3af76325f86a | 61 | |
Crazyaboutmachines | 29:3af76325f86a | 62 | "node id" [1;20]*4. ex. if node=1then 0x280+1*4=0x284 |
Crazyaboutmachines | 29:3af76325f86a | 63 | |
Crazyaboutmachines | 29:3af76325f86a | 64 | Charger->BMS (normally reserved id's) |
Crazyaboutmachines | 29:3af76325f86a | 65 | 0x380+node_id (period: 1,000S): actual output current, voltage and Ah |
Crazyaboutmachines | 29:3af76325f86a | 66 | 0x280+node_id (period: 4,000S): alarms flags, hardware start/stop status |
Crazyaboutmachines | 29:3af76325f86a | 67 | 0x180+node_id (answer for 0x776 message): tambem é uma das respostas durante o precharge |
Crazyaboutmachines | 29:3af76325f86a | 68 | 0x580+node_id: answer to 0x600 SDO message |
Crazyaboutmachines | 29:3af76325f86a | 69 | |
Crazyaboutmachines | 29:3af76325f86a | 70 | BMS->Charger |
Crazyaboutmachines | 29:3af76325f86a | 71 | 0x6C1: current output modulation(simplest implmentation) |
Crazyaboutmachines | 29:3af76325f86a | 72 | 0x776: (broadcast; from BMS to chargers): current and voltage setpoints, battery detection setpoint |
Crazyaboutmachines | 29:3af76325f86a | 73 | 0x6C1: (broadcast message; from BMS to charger): only mandatory message BMS need to implement (4s timeout) |
Crazyaboutmachines | 29:3af76325f86a | 74 | 0x600+node_id: SDO messages |
Crazyaboutmachines | 29:3af76325f86a | 75 | |
Crazyaboutmachines | 29:3af76325f86a | 76 | Reserved id's: |
Crazyaboutmachines | 29:3af76325f86a | 77 | 0x48A |
Crazyaboutmachines | 29:3af76325f86a | 78 | 0x68A |
Crazyaboutmachines | 29:3af76325f86a | 79 | 0x69A |
Crazyaboutmachines | 29:3af76325f86a | 80 | 0x777 |
Crazyaboutmachines | 29:3af76325f86a | 81 | 0x380+node_id |
Crazyaboutmachines | 29:3af76325f86a | 82 | 0x180+node_id |
Crazyaboutmachines | 29:3af76325f86a | 83 | 0x600+node_id |
Crazyaboutmachines | 29:3af76325f86a | 84 | 0x580+node_id |
Crazyaboutmachines | 29:3af76325f86a | 85 | ...remote display default node is 21... |
Crazyaboutmachines | 29:3af76325f86a | 86 | |
Crazyaboutmachines | 29:3af76325f86a | 87 | Precharge: |
Crazyaboutmachines | 29:3af76325f86a | 88 | (ID|Message) |
Crazyaboutmachines | 29:3af76325f86a | 89 | To charger:(0x600+node_id | 22-60-60-01-V_HI-V_LO-0-0) |
Crazyaboutmachines | 29:3af76325f86a | 90 | From charger:(0x580+node_id | 60-60-60-01-0-0-0-0) |
Crazyaboutmachines | 29:3af76325f86a | 91 | From charger:(0x580+node_id | 80-60-60-01-XX-XX-XX-XX) |
Crazyaboutmachines | 29:3af76325f86a | 92 | From charger:(0x180+node_id | I_HI-I_LO-V_HI-V_LO-01-'O'-'K'-n) |
Crazyaboutmachines | 29:3af76325f86a | 93 | To charger:(0x600+node_id | 22-60-60-02-0-0-0-0) |
Crazyaboutmachines | 29:3af76325f86a | 94 | From charger:(0x580+node_id | 60-60-60-02-0-0-0-0) |
Crazyaboutmachines | 29:3af76325f86a | 95 | From charger:(0x580+node_id | 80-60-60-02-XX-XX-XX-XX) |
Crazyaboutmachines | 29:3af76325f86a | 96 | |
Crazyaboutmachines | 29:3af76325f86a | 97 | ******************************************************************************/ |
Crazyaboutmachines | 29:3af76325f86a | 98 | |
Crazyaboutmachines | 29:3af76325f86a | 99 | |
ser1516 | 21:13ac27349025 | 100 | const unsigned int RX_ID = 0x100; |
ser1516 | 21:13ac27349025 | 101 | const unsigned int TX_ID = 0x101; |
hudakz | 6:7ff95ce72f6d | 102 | |
hudakz | 19:872e304d7e17 | 103 | #include "CANnucleo.h" |
hudakz | 16:a86f339d1c25 | 104 | #include "mbed.h" |
hudakz | 16:a86f339d1c25 | 105 | |
ser1516 | 21:13ac27349025 | 106 | /* |
hudakz | 17:18d4d0ff26a6 | 107 | * To avaoid name collision with the CAN and CANMessage classes built into the mbed library |
hudakz | 17:18d4d0ff26a6 | 108 | * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace. |
hudakz | 20:eb1a8042605e | 109 | * Remember to qualify them with the CANnucleo namespace. |
hudakz | 17:18d4d0ff26a6 | 110 | */ |
hudakz | 17:18d4d0ff26a6 | 111 | CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
hudakz | 17:18d4d0ff26a6 | 112 | CANnucleo::CANMessage rxMsg; |
hudakz | 17:18d4d0ff26a6 | 113 | CANnucleo::CANMessage txMsg; |
ser1516 | 21:13ac27349025 | 114 | CANnucleo::CANMessage throttle_txMsg; |
ser1516 | 21:13ac27349025 | 115 | |
ser1516 | 21:13ac27349025 | 116 | |
ser1516 | 21:13ac27349025 | 117 | DigitalOut led(PA_5); |
ser1516 | 21:13ac27349025 | 118 | |
hudakz | 18:22977a898fe9 | 119 | Timer timer; |
hudakz | 17:18d4d0ff26a6 | 120 | volatile bool msgAvailable = false; |
ser1516 | 21:13ac27349025 | 121 | volatile bool to_send = false; |
Crazyaboutmachines | 25:76c6f417eb48 | 122 | float cellsv[36]; |
hudakz | 16:a86f339d1c25 | 123 | /** |
hudakz | 16:a86f339d1c25 | 124 | * @brief 'CAN receive-complete' interrup handler. |
hudakz | 16:a86f339d1c25 | 125 | * @note Called on arrival of new CAN message. |
hudakz | 16:a86f339d1c25 | 126 | * Keep it as short as possible. |
ser1516 | 21:13ac27349025 | 127 | * @param |
ser1516 | 21:13ac27349025 | 128 | * @retval |
hudakz | 16:a86f339d1c25 | 129 | */ |
ser1516 | 21:13ac27349025 | 130 | void onMsgReceived() |
ser1516 | 21:13ac27349025 | 131 | { |
hudakz | 16:a86f339d1c25 | 132 | msgAvailable = true; |
hudakz | 16:a86f339d1c25 | 133 | } |
hudakz | 16:a86f339d1c25 | 134 | |
hudakz | 16:a86f339d1c25 | 135 | /** |
hudakz | 16:a86f339d1c25 | 136 | * @brief Main |
hudakz | 16:a86f339d1c25 | 137 | * @note |
ser1516 | 21:13ac27349025 | 138 | * @param |
hudakz | 16:a86f339d1c25 | 139 | * @retval |
hudakz | 16:a86f339d1c25 | 140 | */ |
ser1516 | 21:13ac27349025 | 141 | |
ser1516 | 21:13ac27349025 | 142 | bool key_switch = 0; |
ser1516 | 21:13ac27349025 | 143 | |
Crazyaboutmachines | 25:76c6f417eb48 | 144 | |
Crazyaboutmachines | 30:1bc79ae58a42 | 145 | void refresh() |
ser1516 | 21:13ac27349025 | 146 | { |
ser1516 | 21:13ac27349025 | 147 | key_switch = !key_switch; |
Crazyaboutmachines | 30:1bc79ae58a42 | 148 | // led = key_switch; |
ser1516 | 21:13ac27349025 | 149 | to_send=1; |
ser1516 | 21:13ac27349025 | 150 | //printf("controller switch\r\n"); |
ser1516 | 21:13ac27349025 | 151 | // to_send = 1; |
ser1516 | 21:13ac27349025 | 152 | } |
ser1516 | 21:13ac27349025 | 153 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 154 | void cvprint(){ |
Crazyaboutmachines | 25:76c6f417eb48 | 155 | int n; |
Crazyaboutmachines | 25:76c6f417eb48 | 156 | for(n=35; n>=0; n--){ |
Crazyaboutmachines | 25:76c6f417eb48 | 157 | //printf("cellsv0: %f cvprint\r\n", cellsv[0]); |
Crazyaboutmachines | 25:76c6f417eb48 | 158 | printf("cell: %d voltage: %f \r\n", n+1,cellsv[n]); |
Crazyaboutmachines | 25:76c6f417eb48 | 159 | } |
Crazyaboutmachines | 25:76c6f417eb48 | 160 | printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n"); |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 161 | } |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 162 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 163 | |
Crazyaboutmachines | 30:1bc79ae58a42 | 164 | Ticker refresher; |
Crazyaboutmachines | 25:76c6f417eb48 | 165 | Ticker printer; |
ser1516 | 21:13ac27349025 | 166 | |
ser1516 | 21:13ac27349025 | 167 | typedef union can_union { |
ser1516 | 21:13ac27349025 | 168 | int i[2]; |
ser1516 | 21:13ac27349025 | 169 | char bytes[8]; |
ser1516 | 21:13ac27349025 | 170 | float f[2]; |
ser1516 | 21:13ac27349025 | 171 | } data; |
ser1516 | 21:13ac27349025 | 172 | |
Crazyaboutmachines | 30:1bc79ae58a42 | 173 | //bool to_charge_or_not_to_charge=true; // false = discharge |
Crazyaboutmachines | 30:1bc79ae58a42 | 174 | bool charging = false; |
Crazyaboutmachines | 30:1bc79ae58a42 | 175 | |
Crazyaboutmachines | 29:3af76325f86a | 176 | uint8_t motostate=0; |
Crazyaboutmachines | 27:21239801cfd3 | 177 | |
ser1516 | 21:13ac27349025 | 178 | int main() |
ser1516 | 21:13ac27349025 | 179 | |
ser1516 | 21:13ac27349025 | 180 | { |
hudakz | 16:a86f339d1c25 | 181 | can.frequency(1000000); // set bit rate to 1Mbps |
hudakz | 16:a86f339d1c25 | 182 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
Crazyaboutmachines | 30:1bc79ae58a42 | 183 | refresher.attach(&refresh, 5); // turn on or off |
Crazyaboutmachines | 25:76c6f417eb48 | 184 | |
Crazyaboutmachines | 25:76c6f417eb48 | 185 | printer.attach(&cvprint, 20); // turn on o |
ser1516 | 21:13ac27349025 | 186 | led=key_switch; |
hudakz | 10:66da8731bdb6 | 187 | timer.start(); // start timer |
ser1516 | 21:13ac27349025 | 188 | |
ser1516 | 21:13ac27349025 | 189 | printf("started\r\n"); |
ser1516 | 21:13ac27349025 | 190 | while(true) { |
ser1516 | 21:13ac27349025 | 191 | |
ser1516 | 21:13ac27349025 | 192 | if(msgAvailable) { |
ser1516 | 21:13ac27349025 | 193 | data data; |
ser1516 | 21:13ac27349025 | 194 | int len = can.read(rxMsg); |
ser1516 | 21:13ac27349025 | 195 | data.bytes[0] = rxMsg.data[0]; |
ser1516 | 21:13ac27349025 | 196 | data.bytes[1] = rxMsg.data[1]; |
ser1516 | 21:13ac27349025 | 197 | data.bytes[2] = rxMsg.data[2]; |
ser1516 | 21:13ac27349025 | 198 | data.bytes[3] = rxMsg.data[3]; |
ser1516 | 21:13ac27349025 | 199 | msgAvailable = false; // reset flag for next use |
Crazyaboutmachines | 25:76c6f417eb48 | 200 | // printf(" Id: %d, data: %f, counter : %d\n", rxMsg.id, data.f[0],rxMsg.data[4]); |
Crazyaboutmachines | 25:76c6f417eb48 | 201 | cellsv[(rxMsg.id-11)*12+rxMsg.data[4]-1]=data.f[0]; |
Crazyaboutmachines | 25:76c6f417eb48 | 202 | //printf("cell: %d\r\n", rxMsg.data[4]); |
ser1516 | 21:13ac27349025 | 203 | /* |
ser1516 | 21:13ac27349025 | 204 | printf("\r\nreceived message ID: \t%d\n\r", rxMsg.id); |
ser1516 | 21:13ac27349025 | 205 | for(int i=0; i<len; i++) { |
ser1516 | 21:13ac27349025 | 206 | printf("\t%x",rxMsg.data[i]); |
ser1516 | 21:13ac27349025 | 207 | }*/ |
Crazyaboutmachines | 25:76c6f417eb48 | 208 | // printf("\r\n"); |
Crazyaboutmachines | 25:76c6f417eb48 | 209 | /* if(rxMsg.data[4] == 1) { //counter == 12 |
ser1516 | 21:13ac27349025 | 210 | |
ser1516 | 21:13ac27349025 | 211 | printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n"); |
ser1516 | 21:13ac27349025 | 212 | } |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 213 | */ |
ser1516 | 21:13ac27349025 | 214 | // Filtering performed by software: |
hudakz | 0:c5e5d0df6f2a | 215 | } |
ser1516 | 21:13ac27349025 | 216 | if(to_send) { |
ser1516 | 21:13ac27349025 | 217 | to_send = 0; |
Crazyaboutmachines | 27:21239801cfd3 | 218 | |
Crazyaboutmachines | 27:21239801cfd3 | 219 | |
Crazyaboutmachines | 27:21239801cfd3 | 220 | //------------------------------------------------ |
Crazyaboutmachines | 27:21239801cfd3 | 221 | //ECU to BMS State |
Crazyaboutmachines | 26:3ac15dfbb66b | 222 | |
Crazyaboutmachines | 27:21239801cfd3 | 223 | //motostate: (0|0|0|0|0|0|to_charge_or_not_to_charge|key_switch) |
Crazyaboutmachines | 27:21239801cfd3 | 224 | txMsg.clear(); |
Crazyaboutmachines | 27:21239801cfd3 | 225 | txMsg.id = 9; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. |
Crazyaboutmachines | 27:21239801cfd3 | 226 | txMsg.len = 1; |
Crazyaboutmachines | 30:1bc79ae58a42 | 227 | motostate = (0b00000001 & key_switch)|((0b00000001 & charging)<<1); |
Crazyaboutmachines | 29:3af76325f86a | 228 | txMsg.data[0] = motostate; |
Crazyaboutmachines | 27:21239801cfd3 | 229 | //------------------------------------------------ |
Crazyaboutmachines | 27:21239801cfd3 | 230 | |
ser1516 | 21:13ac27349025 | 231 | if(can.write(txMsg)) { |
ser1516 | 21:13ac27349025 | 232 | printf("sent message\r\n"); |
ser1516 | 21:13ac27349025 | 233 | } else { |
Crazyaboutmachines | 27:21239801cfd3 | 234 | static char count = 0; //desta maneira o count é sempre zero e assim nunca chega a 3?? |
ser1516 | 21:13ac27349025 | 235 | count++; |
ser1516 | 21:13ac27349025 | 236 | printf("transmission error\n\r overflow: %x\n\r", count); |
ser1516 | 21:13ac27349025 | 237 | if(count == 3) { |
Crazyaboutmachines | 23:1a29761becd2 | 238 | count = 0; |
Crazyaboutmachines | 23:1a29761becd2 | 239 | NVIC_SystemReset(); //faz reset se estiver a falhar o envio de mensagens |
ser1516 | 21:13ac27349025 | 240 | // attach 'CAN receive-complete' interrupt handler |
ser1516 | 21:13ac27349025 | 241 | |
ser1516 | 21:13ac27349025 | 242 | } |
ser1516 | 21:13ac27349025 | 243 | |
hudakz | 0:c5e5d0df6f2a | 244 | } |
hudakz | 0:c5e5d0df6f2a | 245 | } |
hudakz | 0:c5e5d0df6f2a | 246 | } |
hudakz | 0:c5e5d0df6f2a | 247 | } |
hudakz | 7:2dce8ed51091 | 248 | |
hudakz | 12:e91e44924194 | 249 | |
hudakz | 17:18d4d0ff26a6 | 250 |