all bms comunicating with nucleo board and balancing

Dependencies:   CANnucleo mbed

Fork of Can_sniffer_BMS_GER by Joao Vieira

Committer:
Crazyaboutmachines
Date:
Tue Oct 25 17:28:55 2016 +0000
Revision:
31:6427569cadbe
Parent:
30:1bc79ae58a42
Child:
32:977a1b5728e4
all bms comunicating with nucleo board and balancing

Who changed what in which revision?

UserRevisionLine numberNew 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
Crazyaboutmachines 29:3af76325f86a 57 Mensagens:
Crazyaboutmachines 29:3af76325f86a 58 (charger CAN node is 11 by default. So the Id of the message will be 0x60B in
Crazyaboutmachines 29:3af76325f86a 59 hexadecimal)
Crazyaboutmachines 29:3af76325f86a 60
Crazyaboutmachines 29:3af76325f86a 61 "node id" [1;20]*4. ex. if node=1then 0x280+1*4=0x284
Crazyaboutmachines 29:3af76325f86a 62
Crazyaboutmachines 29:3af76325f86a 63 Charger->BMS (normally reserved id's)
Crazyaboutmachines 29:3af76325f86a 64 0x380+node_id (period: 1,000S): actual output current, voltage and Ah
Crazyaboutmachines 29:3af76325f86a 65 0x280+node_id (period: 4,000S): alarms flags, hardware start/stop status
Crazyaboutmachines 29:3af76325f86a 66 0x180+node_id (answer for 0x776 message): tambem é uma das respostas durante o precharge
Crazyaboutmachines 29:3af76325f86a 67 0x580+node_id: answer to 0x600 SDO message
Crazyaboutmachines 29:3af76325f86a 68
Crazyaboutmachines 29:3af76325f86a 69 BMS->Charger
Crazyaboutmachines 29:3af76325f86a 70 0x6C1: current output modulation(simplest implmentation)
Crazyaboutmachines 29:3af76325f86a 71 0x776: (broadcast; from BMS to chargers): current and voltage setpoints, battery detection setpoint
Crazyaboutmachines 29:3af76325f86a 72 0x6C1: (broadcast message; from BMS to charger): only mandatory message BMS need to implement (4s timeout)
Crazyaboutmachines 29:3af76325f86a 73 0x600+node_id: SDO messages
Crazyaboutmachines 29:3af76325f86a 74
Crazyaboutmachines 29:3af76325f86a 75 Reserved id's:
Crazyaboutmachines 29:3af76325f86a 76 0x48A
Crazyaboutmachines 29:3af76325f86a 77 0x68A
Crazyaboutmachines 29:3af76325f86a 78 0x69A
Crazyaboutmachines 29:3af76325f86a 79 0x777
Crazyaboutmachines 29:3af76325f86a 80 0x380+node_id
Crazyaboutmachines 29:3af76325f86a 81 0x180+node_id
Crazyaboutmachines 29:3af76325f86a 82 0x600+node_id
Crazyaboutmachines 29:3af76325f86a 83 0x580+node_id
Crazyaboutmachines 29:3af76325f86a 84 ...remote display default node is 21...
Crazyaboutmachines 29:3af76325f86a 85
Crazyaboutmachines 29:3af76325f86a 86 Precharge:
Crazyaboutmachines 29:3af76325f86a 87 (ID|Message)
Crazyaboutmachines 29:3af76325f86a 88 To charger:(0x600+node_id | 22-60-60-01-V_HI-V_LO-0-0)
Crazyaboutmachines 29:3af76325f86a 89 From charger:(0x580+node_id | 60-60-60-01-0-0-0-0)
Crazyaboutmachines 29:3af76325f86a 90 From charger:(0x580+node_id | 80-60-60-01-XX-XX-XX-XX)
Crazyaboutmachines 29:3af76325f86a 91 From charger:(0x180+node_id | I_HI-I_LO-V_HI-V_LO-01-'O'-'K'-n)
Crazyaboutmachines 29:3af76325f86a 92 To charger:(0x600+node_id | 22-60-60-02-0-0-0-0)
Crazyaboutmachines 29:3af76325f86a 93 From charger:(0x580+node_id | 60-60-60-02-0-0-0-0)
Crazyaboutmachines 29:3af76325f86a 94 From charger:(0x580+node_id | 80-60-60-02-XX-XX-XX-XX)
Crazyaboutmachines 29:3af76325f86a 95
Crazyaboutmachines 29:3af76325f86a 96 ******************************************************************************/
Crazyaboutmachines 29:3af76325f86a 97
Crazyaboutmachines 29:3af76325f86a 98
ser1516 21:13ac27349025 99 const unsigned int RX_ID = 0x100;
ser1516 21:13ac27349025 100 const unsigned int TX_ID = 0x101;
hudakz 6:7ff95ce72f6d 101
hudakz 19:872e304d7e17 102 #include "CANnucleo.h"
hudakz 16:a86f339d1c25 103 #include "mbed.h"
hudakz 16:a86f339d1c25 104
ser1516 21:13ac27349025 105 /*
hudakz 17:18d4d0ff26a6 106 * To avaoid name collision with the CAN and CANMessage classes built into the mbed library
hudakz 17:18d4d0ff26a6 107 * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace.
hudakz 20:eb1a8042605e 108 * Remember to qualify them with the CANnucleo namespace.
hudakz 17:18d4d0ff26a6 109 */
hudakz 17:18d4d0ff26a6 110 CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
hudakz 17:18d4d0ff26a6 111 CANnucleo::CANMessage rxMsg;
hudakz 17:18d4d0ff26a6 112 CANnucleo::CANMessage txMsg;
ser1516 21:13ac27349025 113 CANnucleo::CANMessage throttle_txMsg;
ser1516 21:13ac27349025 114
ser1516 21:13ac27349025 115
ser1516 21:13ac27349025 116 DigitalOut led(PA_5);
ser1516 21:13ac27349025 117
hudakz 18:22977a898fe9 118 Timer timer;
hudakz 17:18d4d0ff26a6 119 volatile bool msgAvailable = false;
ser1516 21:13ac27349025 120 volatile bool to_send = false;
Crazyaboutmachines 25:76c6f417eb48 121 float cellsv[36];
hudakz 16:a86f339d1c25 122 /**
hudakz 16:a86f339d1c25 123 * @brief 'CAN receive-complete' interrup handler.
hudakz 16:a86f339d1c25 124 * @note Called on arrival of new CAN message.
hudakz 16:a86f339d1c25 125 * Keep it as short as possible.
ser1516 21:13ac27349025 126 * @param
ser1516 21:13ac27349025 127 * @retval
hudakz 16:a86f339d1c25 128 */
ser1516 21:13ac27349025 129 void onMsgReceived()
ser1516 21:13ac27349025 130 {
hudakz 16:a86f339d1c25 131 msgAvailable = true;
hudakz 16:a86f339d1c25 132 }
hudakz 16:a86f339d1c25 133
hudakz 16:a86f339d1c25 134 /**
hudakz 16:a86f339d1c25 135 * @brief Main
hudakz 16:a86f339d1c25 136 * @note
ser1516 21:13ac27349025 137 * @param
hudakz 16:a86f339d1c25 138 * @retval
hudakz 16:a86f339d1c25 139 */
ser1516 21:13ac27349025 140
ser1516 21:13ac27349025 141 bool key_switch = 0;
ser1516 21:13ac27349025 142
Crazyaboutmachines 25:76c6f417eb48 143
Crazyaboutmachines 30:1bc79ae58a42 144 void refresh()
ser1516 21:13ac27349025 145 {
ser1516 21:13ac27349025 146 key_switch = !key_switch;
Crazyaboutmachines 30:1bc79ae58a42 147 // led = key_switch;
ser1516 21:13ac27349025 148 to_send=1;
ser1516 21:13ac27349025 149 //printf("controller switch\r\n");
ser1516 21:13ac27349025 150 // to_send = 1;
ser1516 21:13ac27349025 151 }
ser1516 21:13ac27349025 152
Crazyaboutmachines 24:c9c7dcdcbbc5 153 void cvprint(){
Crazyaboutmachines 25:76c6f417eb48 154 int n;
Crazyaboutmachines 25:76c6f417eb48 155 for(n=35; n>=0; n--){
Crazyaboutmachines 25:76c6f417eb48 156 //printf("cellsv0: %f cvprint\r\n", cellsv[0]);
Crazyaboutmachines 25:76c6f417eb48 157 printf("cell: %d voltage: %f \r\n", n+1,cellsv[n]);
Crazyaboutmachines 25:76c6f417eb48 158 }
Crazyaboutmachines 25:76c6f417eb48 159 printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n");
Crazyaboutmachines 24:c9c7dcdcbbc5 160 }
Crazyaboutmachines 24:c9c7dcdcbbc5 161
Crazyaboutmachines 24:c9c7dcdcbbc5 162
Crazyaboutmachines 30:1bc79ae58a42 163 Ticker refresher;
Crazyaboutmachines 25:76c6f417eb48 164 Ticker printer;
ser1516 21:13ac27349025 165
ser1516 21:13ac27349025 166 typedef union can_union {
ser1516 21:13ac27349025 167 int i[2];
ser1516 21:13ac27349025 168 char bytes[8];
ser1516 21:13ac27349025 169 float f[2];
ser1516 21:13ac27349025 170 } data;
ser1516 21:13ac27349025 171
Crazyaboutmachines 30:1bc79ae58a42 172 //bool to_charge_or_not_to_charge=true; // false = discharge
Crazyaboutmachines 31:6427569cadbe 173 bool charging = true;
Crazyaboutmachines 30:1bc79ae58a42 174
Crazyaboutmachines 29:3af76325f86a 175 uint8_t motostate=0;
Crazyaboutmachines 27:21239801cfd3 176
ser1516 21:13ac27349025 177 int main()
ser1516 21:13ac27349025 178
ser1516 21:13ac27349025 179 {
hudakz 16:a86f339d1c25 180 can.frequency(1000000); // set bit rate to 1Mbps
hudakz 16:a86f339d1c25 181 can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler
Crazyaboutmachines 30:1bc79ae58a42 182 refresher.attach(&refresh, 5); // turn on or off
Crazyaboutmachines 25:76c6f417eb48 183
Crazyaboutmachines 25:76c6f417eb48 184 printer.attach(&cvprint, 20); // turn on o
ser1516 21:13ac27349025 185 led=key_switch;
hudakz 10:66da8731bdb6 186 timer.start(); // start timer
ser1516 21:13ac27349025 187
ser1516 21:13ac27349025 188 printf("started\r\n");
ser1516 21:13ac27349025 189 while(true) {
ser1516 21:13ac27349025 190
ser1516 21:13ac27349025 191 if(msgAvailable) {
ser1516 21:13ac27349025 192 data data;
ser1516 21:13ac27349025 193 int len = can.read(rxMsg);
ser1516 21:13ac27349025 194 data.bytes[0] = rxMsg.data[0];
ser1516 21:13ac27349025 195 data.bytes[1] = rxMsg.data[1];
ser1516 21:13ac27349025 196 data.bytes[2] = rxMsg.data[2];
ser1516 21:13ac27349025 197 data.bytes[3] = rxMsg.data[3];
ser1516 21:13ac27349025 198 msgAvailable = false; // reset flag for next use
Crazyaboutmachines 25:76c6f417eb48 199 // printf(" Id: %d, data: %f, counter : %d\n", rxMsg.id, data.f[0],rxMsg.data[4]);
Crazyaboutmachines 25:76c6f417eb48 200 cellsv[(rxMsg.id-11)*12+rxMsg.data[4]-1]=data.f[0];
Crazyaboutmachines 25:76c6f417eb48 201 //printf("cell: %d\r\n", rxMsg.data[4]);
ser1516 21:13ac27349025 202 /*
ser1516 21:13ac27349025 203 printf("\r\nreceived message ID: \t%d\n\r", rxMsg.id);
ser1516 21:13ac27349025 204 for(int i=0; i<len; i++) {
ser1516 21:13ac27349025 205 printf("\t%x",rxMsg.data[i]);
ser1516 21:13ac27349025 206 }*/
Crazyaboutmachines 25:76c6f417eb48 207 // printf("\r\n");
Crazyaboutmachines 25:76c6f417eb48 208 /* if(rxMsg.data[4] == 1) { //counter == 12
ser1516 21:13ac27349025 209
ser1516 21:13ac27349025 210 printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n");
ser1516 21:13ac27349025 211 }
Crazyaboutmachines 24:c9c7dcdcbbc5 212 */
ser1516 21:13ac27349025 213 // Filtering performed by software:
hudakz 0:c5e5d0df6f2a 214 }
ser1516 21:13ac27349025 215 if(to_send) {
ser1516 21:13ac27349025 216 to_send = 0;
Crazyaboutmachines 27:21239801cfd3 217
Crazyaboutmachines 27:21239801cfd3 218
Crazyaboutmachines 27:21239801cfd3 219 //------------------------------------------------
Crazyaboutmachines 27:21239801cfd3 220 //ECU to BMS State
Crazyaboutmachines 26:3ac15dfbb66b 221
Crazyaboutmachines 27:21239801cfd3 222 //motostate: (0|0|0|0|0|0|to_charge_or_not_to_charge|key_switch)
Crazyaboutmachines 27:21239801cfd3 223 txMsg.clear();
Crazyaboutmachines 27:21239801cfd3 224 txMsg.id = 9; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13.
Crazyaboutmachines 27:21239801cfd3 225 txMsg.len = 1;
Crazyaboutmachines 30:1bc79ae58a42 226 motostate = (0b00000001 & key_switch)|((0b00000001 & charging)<<1);
Crazyaboutmachines 29:3af76325f86a 227 txMsg.data[0] = motostate;
Crazyaboutmachines 27:21239801cfd3 228 //------------------------------------------------
Crazyaboutmachines 27:21239801cfd3 229
ser1516 21:13ac27349025 230 if(can.write(txMsg)) {
ser1516 21:13ac27349025 231 printf("sent message\r\n");
ser1516 21:13ac27349025 232 } else {
Crazyaboutmachines 27:21239801cfd3 233 static char count = 0; //desta maneira o count é sempre zero e assim nunca chega a 3??
ser1516 21:13ac27349025 234 count++;
ser1516 21:13ac27349025 235 printf("transmission error\n\r overflow: %x\n\r", count);
ser1516 21:13ac27349025 236 if(count == 3) {
Crazyaboutmachines 23:1a29761becd2 237 count = 0;
Crazyaboutmachines 23:1a29761becd2 238 NVIC_SystemReset(); //faz reset se estiver a falhar o envio de mensagens
ser1516 21:13ac27349025 239 // attach 'CAN receive-complete' interrupt handler
ser1516 21:13ac27349025 240
ser1516 21:13ac27349025 241 }
ser1516 21:13ac27349025 242
hudakz 0:c5e5d0df6f2a 243 }
hudakz 0:c5e5d0df6f2a 244 }
hudakz 0:c5e5d0df6f2a 245 }
hudakz 0:c5e5d0df6f2a 246 }
hudakz 7:2dce8ed51091 247
hudakz 12:e91e44924194 248
hudakz 17:18d4d0ff26a6 249