![](/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@40:54d07c794d87, 2016-11-25 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Fri Nov 25 23:26:37 2016 +0000
- Revision:
- 40:54d07c794d87
- Parent:
- 39:90a56ddcdf4a
- Child:
- 41:dcd47ac5ffdf
mask and grades implemented
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 | * |
Crazyaboutmachines | 38:3e01f81f0808 | 4 | * Two affordable (less than $3 on ebay) STM32F103C8T6 boardss (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 | 32:977a1b5728e4 | 42 | Processo de carregamento: |
Crazyaboutmachines | 32:977a1b5728e4 | 43 | (1) Desconectar carregador da rede AC |
Crazyaboutmachines | 32:977a1b5728e4 | 44 | (2) Conectar carregador as baterias |
Crazyaboutmachines | 32:977a1b5728e4 | 45 | (3) Conectar carregador da rede AC(talves mais pratico ter botao?) |
Crazyaboutmachines | 32:977a1b5728e4 | 46 | (4) Carga iniciase automaticamente(?) até uma tensao maxima de 4*27=108V |
Crazyaboutmachines | 32:977a1b5728e4 | 47 | (5) Para desligar carregador desconectar da rede AC |
Crazyaboutmachines | 29:3af76325f86a | 48 | |
Crazyaboutmachines | 29:3af76325f86a | 49 | Tarefas: |
Crazyaboutmachines | 32:977a1b5728e4 | 50 | -ver celulas em que o balanceamento nao esta a funcionar |
Crazyaboutmachines | 32:977a1b5728e4 | 51 | -ver porque as tensões nao se estão a actualizar (por a fazer display da soma das tenções) |
Crazyaboutmachines | 32:977a1b5728e4 | 52 | -ver porque o charger nao responde as mensagens CAN |
Crazyaboutmachines | 32:977a1b5728e4 | 53 | |
Crazyaboutmachines | 29:3af76325f86a | 54 | -acabar de formatar o codigo do BMS encurtando interrupts, programando todas as BMS's e aumentando fiabilidade, etc |
Crazyaboutmachines | 29:3af76325f86a | 55 | -ligar carregador e ver que tensoes de CAN e de saida manda cá para fora+ ver que mensagens |
Crazyaboutmachines | 32:977a1b5728e4 | 56 | |
Crazyaboutmachines | 32:977a1b5728e4 | 57 | Atenção: |
Crazyaboutmachines | 32:977a1b5728e4 | 58 | -obrigatorio conectar e desconectar as baterias do carregador quando este esta desconectado da rede AC |
Crazyaboutmachines | 32:977a1b5728e4 | 59 | -em caso de algum problema desligar primeiro ficha da redeAC do carregador e depois desconectar ficha das baterias |
Crazyaboutmachines | 29:3af76325f86a | 60 | |
Crazyaboutmachines | 29:3af76325f86a | 61 | Mensagens: |
Crazyaboutmachines | 29:3af76325f86a | 62 | (charger CAN node is 11 by default. So the Id of the message will be 0x60B in |
Crazyaboutmachines | 29:3af76325f86a | 63 | hexadecimal) |
Crazyaboutmachines | 29:3af76325f86a | 64 | |
Crazyaboutmachines | 29:3af76325f86a | 65 | Charger->BMS (normally reserved id's) |
Crazyaboutmachines | 29:3af76325f86a | 66 | 0x380+node_id (period: 1,000S): actual output current, voltage and Ah |
Crazyaboutmachines | 29:3af76325f86a | 67 | 0x280+node_id (period: 4,000S): alarms flags, hardware start/stop status |
Crazyaboutmachines | 29:3af76325f86a | 68 | 0x180+node_id (answer for 0x776 message): tambem é uma das respostas durante o precharge |
Crazyaboutmachines | 29:3af76325f86a | 69 | 0x580+node_id: answer to 0x600 SDO message |
Crazyaboutmachines | 29:3af76325f86a | 70 | |
Crazyaboutmachines | 29:3af76325f86a | 71 | BMS->Charger |
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 | 32:977a1b5728e4 | 87 | Precharge(not used): |
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 | 32:977a1b5728e4 | 97 | |
Crazyaboutmachines | 32:977a1b5728e4 | 98 | testar:------------------------------------ |
Crazyaboutmachines | 32:977a1b5728e4 | 99 | |
Crazyaboutmachines | 32:977a1b5728e4 | 100 | -ver se BMS esta realmente a actualizar tensoes comparando tensoes a ser ou nao carregadas |
Crazyaboutmachines | 32:977a1b5728e4 | 101 | |
Crazyaboutmachines | 32:977a1b5728e4 | 102 | test3:---------------------------- |
Crazyaboutmachines | 32:977a1b5728e4 | 103 | -checkar que o balanceamento de todas as celulas estao a funcionar |
Crazyaboutmachines | 32:977a1b5728e4 | 104 | -ajustar bms para carga final e amostra da soma total das tensoes |
Crazyaboutmachines | 32:977a1b5728e4 | 105 | |
Crazyaboutmachines | 32:977a1b5728e4 | 106 | test4:----------------------------- |
Crazyaboutmachines | 32:977a1b5728e4 | 107 | -carregamento final |
Crazyaboutmachines | 32:977a1b5728e4 | 108 | |
Crazyaboutmachines | 32:977a1b5728e4 | 109 | test5:----------------------------- |
Crazyaboutmachines | 32:977a1b5728e4 | 110 | -descarregamento ate metade da tensão p testar aquecimento do balanceamento |
Crazyaboutmachines | 32:977a1b5728e4 | 111 | |
Crazyaboutmachines | 29:3af76325f86a | 112 | ******************************************************************************/ |
Crazyaboutmachines | 29:3af76325f86a | 113 | |
Crazyaboutmachines | 29:3af76325f86a | 114 | |
ser1516 | 21:13ac27349025 | 115 | const unsigned int RX_ID = 0x100; |
ser1516 | 21:13ac27349025 | 116 | const unsigned int TX_ID = 0x101; |
hudakz | 6:7ff95ce72f6d | 117 | |
hudakz | 19:872e304d7e17 | 118 | #include "CANnucleo.h" |
hudakz | 16:a86f339d1c25 | 119 | #include "mbed.h" |
hudakz | 16:a86f339d1c25 | 120 | |
ser1516 | 21:13ac27349025 | 121 | /* |
hudakz | 17:18d4d0ff26a6 | 122 | * To avaoid name collision with the CAN and CANMessage classes built into the mbed library |
hudakz | 17:18d4d0ff26a6 | 123 | * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace. |
hudakz | 20:eb1a8042605e | 124 | * Remember to qualify them with the CANnucleo namespace. |
hudakz | 17:18d4d0ff26a6 | 125 | */ |
hudakz | 17:18d4d0ff26a6 | 126 | CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
hudakz | 17:18d4d0ff26a6 | 127 | CANnucleo::CANMessage rxMsg; |
hudakz | 17:18d4d0ff26a6 | 128 | CANnucleo::CANMessage txMsg; |
ser1516 | 21:13ac27349025 | 129 | CANnucleo::CANMessage throttle_txMsg; |
ser1516 | 21:13ac27349025 | 130 | |
ser1516 | 21:13ac27349025 | 131 | |
ser1516 | 21:13ac27349025 | 132 | DigitalOut led(PA_5); |
ser1516 | 21:13ac27349025 | 133 | |
hudakz | 18:22977a898fe9 | 134 | Timer timer; |
hudakz | 17:18d4d0ff26a6 | 135 | volatile bool msgAvailable = false; |
ser1516 | 21:13ac27349025 | 136 | volatile bool to_send = false; |
Crazyaboutmachines | 25:76c6f417eb48 | 137 | float cellsv[36]; |
Crazyaboutmachines | 37:1e1f3131fef5 | 138 | float cellst[96]; |
hudakz | 16:a86f339d1c25 | 139 | /** |
hudakz | 16:a86f339d1c25 | 140 | * @brief 'CAN receive-complete' interrup handler. |
hudakz | 16:a86f339d1c25 | 141 | * @note Called on arrival of new CAN message. |
hudakz | 16:a86f339d1c25 | 142 | * Keep it as short as possible. |
ser1516 | 21:13ac27349025 | 143 | * @param |
ser1516 | 21:13ac27349025 | 144 | * @retval |
hudakz | 16:a86f339d1c25 | 145 | */ |
ser1516 | 21:13ac27349025 | 146 | void onMsgReceived() |
ser1516 | 21:13ac27349025 | 147 | { |
hudakz | 16:a86f339d1c25 | 148 | msgAvailable = true; |
hudakz | 16:a86f339d1c25 | 149 | } |
hudakz | 16:a86f339d1c25 | 150 | |
hudakz | 16:a86f339d1c25 | 151 | /** |
hudakz | 16:a86f339d1c25 | 152 | * @brief Main |
hudakz | 16:a86f339d1c25 | 153 | * @note |
ser1516 | 21:13ac27349025 | 154 | * @param |
hudakz | 16:a86f339d1c25 | 155 | * @retval |
hudakz | 16:a86f339d1c25 | 156 | */ |
ser1516 | 21:13ac27349025 | 157 | |
ser1516 | 21:13ac27349025 | 158 | bool key_switch = 0; |
ser1516 | 21:13ac27349025 | 159 | |
Crazyaboutmachines | 25:76c6f417eb48 | 160 | |
Crazyaboutmachines | 30:1bc79ae58a42 | 161 | void refresh() |
ser1516 | 21:13ac27349025 | 162 | { |
ser1516 | 21:13ac27349025 | 163 | key_switch = !key_switch; |
Crazyaboutmachines | 36:00ea3ae2cead | 164 | led = key_switch; |
ser1516 | 21:13ac27349025 | 165 | to_send=1; |
ser1516 | 21:13ac27349025 | 166 | //printf("controller switch\r\n"); |
ser1516 | 21:13ac27349025 | 167 | // to_send = 1; |
ser1516 | 21:13ac27349025 | 168 | } |
ser1516 | 21:13ac27349025 | 169 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 170 | void cvprint(){ |
Crazyaboutmachines | 25:76c6f417eb48 | 171 | int n; |
Crazyaboutmachines | 32:977a1b5728e4 | 172 | float soma=0; |
Crazyaboutmachines | 25:76c6f417eb48 | 173 | for(n=35; n>=0; n--){ |
Crazyaboutmachines | 25:76c6f417eb48 | 174 | //printf("cellsv0: %f cvprint\r\n", cellsv[0]); |
Crazyaboutmachines | 25:76c6f417eb48 | 175 | printf("cell: %d voltage: %f \r\n", n+1,cellsv[n]); |
Crazyaboutmachines | 36:00ea3ae2cead | 176 | |
Crazyaboutmachines | 32:977a1b5728e4 | 177 | if(cellsv[n]>=0.1) |
Crazyaboutmachines | 32:977a1b5728e4 | 178 | { |
Crazyaboutmachines | 32:977a1b5728e4 | 179 | soma=cellsv[n]+soma; |
Crazyaboutmachines | 32:977a1b5728e4 | 180 | } |
Crazyaboutmachines | 32:977a1b5728e4 | 181 | } |
Crazyaboutmachines | 32:977a1b5728e4 | 182 | |
Crazyaboutmachines | 36:00ea3ae2cead | 183 | printf("Total battery voltage: %f \r\n", soma); |
Crazyaboutmachines | 25:76c6f417eb48 | 184 | printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n"); |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 185 | } |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 186 | |
Crazyaboutmachines | 36:00ea3ae2cead | 187 | void ctprint(){ |
Crazyaboutmachines | 36:00ea3ae2cead | 188 | int n; |
Crazyaboutmachines | 39:90a56ddcdf4a | 189 | float temp; |
Crazyaboutmachines | 39:90a56ddcdf4a | 190 | float current; |
Crazyaboutmachines | 39:90a56ddcdf4a | 191 | float r; |
Crazyaboutmachines | 39:90a56ddcdf4a | 192 | |
Crazyaboutmachines | 39:90a56ddcdf4a | 193 | |
Crazyaboutmachines | 39:90a56ddcdf4a | 194 | |
Crazyaboutmachines | 36:00ea3ae2cead | 195 | for(n=95; n>=0; n--){ |
Crazyaboutmachines | 40:54d07c794d87 | 196 | |
Crazyaboutmachines | 40:54d07c794d87 | 197 | |
Crazyaboutmachines | 40:54d07c794d87 | 198 | //mascara:----------------------------- |
Crazyaboutmachines | 40:54d07c794d87 | 199 | if(cellst[n]>125||cellst[n]<-40){ |
Crazyaboutmachines | 40:54d07c794d87 | 200 | cellst[n]=273.15*-1; |
Crazyaboutmachines | 40:54d07c794d87 | 201 | //cellst[n]=1; |
Crazyaboutmachines | 40:54d07c794d87 | 202 | } |
Crazyaboutmachines | 40:54d07c794d87 | 203 | |
Crazyaboutmachines | 40:54d07c794d87 | 204 | //------------------------------------- |
Crazyaboutmachines | 40:54d07c794d87 | 205 | |
Crazyaboutmachines | 40:54d07c794d87 | 206 | |
Crazyaboutmachines | 40:54d07c794d87 | 207 | // temp=cellst[n]/1000; |
Crazyaboutmachines | 39:90a56ddcdf4a | 208 | |
Crazyaboutmachines | 40:54d07c794d87 | 209 | // r=(temp*10000)/(3.3-temp); |
Crazyaboutmachines | 39:90a56ddcdf4a | 210 | |
Crazyaboutmachines | 39:90a56ddcdf4a | 211 | /* |
Crazyaboutmachines | 39:90a56ddcdf4a | 212 | current=(temp/10000); |
Crazyaboutmachines | 39:90a56ddcdf4a | 213 | r = (3300/current)-10000; |
Crazyaboutmachines | 39:90a56ddcdf4a | 214 | */ |
Crazyaboutmachines | 39:90a56ddcdf4a | 215 | |
Crazyaboutmachines | 40:54d07c794d87 | 216 | // temp = 3380/log(r/0.119228); |
Crazyaboutmachines | 40:54d07c794d87 | 217 | // temp = temp-273.15; |
Crazyaboutmachines | 39:90a56ddcdf4a | 218 | |
Crazyaboutmachines | 39:90a56ddcdf4a | 219 | |
Crazyaboutmachines | 39:90a56ddcdf4a | 220 | //printf("celtemplsv0: %f cvprint\r\n", cellsv[0]); |
Crazyaboutmachines | 40:54d07c794d87 | 221 | //printf("ntc: %d temperature: %f \r\n", n+1,temp); |
Crazyaboutmachines | 40:54d07c794d87 | 222 | printf("ntc: %d temperature: %f \r\n", n+1,cellst[n]); |
Crazyaboutmachines | 36:00ea3ae2cead | 223 | } |
Crazyaboutmachines | 36:00ea3ae2cead | 224 | printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n"); |
Crazyaboutmachines | 36:00ea3ae2cead | 225 | } |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 226 | |
Crazyaboutmachines | 30:1bc79ae58a42 | 227 | Ticker refresher; |
Crazyaboutmachines | 25:76c6f417eb48 | 228 | Ticker printer; |
Crazyaboutmachines | 36:00ea3ae2cead | 229 | Ticker printer2; |
ser1516 | 21:13ac27349025 | 230 | |
ser1516 | 21:13ac27349025 | 231 | typedef union can_union { |
ser1516 | 21:13ac27349025 | 232 | int i[2]; |
ser1516 | 21:13ac27349025 | 233 | char bytes[8]; |
ser1516 | 21:13ac27349025 | 234 | float f[2]; |
ser1516 | 21:13ac27349025 | 235 | } data; |
ser1516 | 21:13ac27349025 | 236 | |
Crazyaboutmachines | 30:1bc79ae58a42 | 237 | //bool to_charge_or_not_to_charge=true; // false = discharge |
Crazyaboutmachines | 35:a8940aaa101f | 238 | bool charging = false; |
Crazyaboutmachines | 30:1bc79ae58a42 | 239 | |
Crazyaboutmachines | 29:3af76325f86a | 240 | uint8_t motostate=0; |
Crazyaboutmachines | 27:21239801cfd3 | 241 | |
Crazyaboutmachines | 36:00ea3ae2cead | 242 | int main(){ |
hudakz | 16:a86f339d1c25 | 243 | can.frequency(1000000); // set bit rate to 1Mbps |
hudakz | 16:a86f339d1c25 | 244 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
Crazyaboutmachines | 30:1bc79ae58a42 | 245 | refresher.attach(&refresh, 5); // turn on or off |
Crazyaboutmachines | 25:76c6f417eb48 | 246 | |
Crazyaboutmachines | 25:76c6f417eb48 | 247 | printer.attach(&cvprint, 20); // turn on o |
Crazyaboutmachines | 36:00ea3ae2cead | 248 | printer2.attach(&ctprint, 50); // turn on o |
Crazyaboutmachines | 36:00ea3ae2cead | 249 | |
Crazyaboutmachines | 36:00ea3ae2cead | 250 | |
ser1516 | 21:13ac27349025 | 251 | led=key_switch; |
hudakz | 10:66da8731bdb6 | 252 | timer.start(); // start timer |
ser1516 | 21:13ac27349025 | 253 | |
ser1516 | 21:13ac27349025 | 254 | printf("started\r\n"); |
ser1516 | 21:13ac27349025 | 255 | while(true) { |
ser1516 | 21:13ac27349025 | 256 | |
ser1516 | 21:13ac27349025 | 257 | if(msgAvailable) { |
ser1516 | 21:13ac27349025 | 258 | data data; |
ser1516 | 21:13ac27349025 | 259 | int len = can.read(rxMsg); |
Crazyaboutmachines | 37:1e1f3131fef5 | 260 | msgAvailable = false; |
Crazyaboutmachines | 36:00ea3ae2cead | 261 | |
Crazyaboutmachines | 36:00ea3ae2cead | 262 | if((rxMsg.id==11)||(rxMsg.id==12)||(rxMsg.id==13)){ |
Crazyaboutmachines | 36:00ea3ae2cead | 263 | data.bytes[0] = rxMsg.data[0]; |
Crazyaboutmachines | 36:00ea3ae2cead | 264 | data.bytes[1] = rxMsg.data[1]; |
Crazyaboutmachines | 36:00ea3ae2cead | 265 | data.bytes[2] = rxMsg.data[2]; |
Crazyaboutmachines | 36:00ea3ae2cead | 266 | data.bytes[3] = rxMsg.data[3]; |
Crazyaboutmachines | 36:00ea3ae2cead | 267 | // printf(" Id: %d, data: %f, counter : %d\n", rxMsg.id, data.f[0],rxMsg.data[4]); |
Crazyaboutmachines | 36:00ea3ae2cead | 268 | cellsv[(rxMsg.id-11)*12+rxMsg.data[4]-1]=data.f[0]; |
Crazyaboutmachines | 36:00ea3ae2cead | 269 | //printf("cell: %d\r\n", rxMsg.data[4]); |
Crazyaboutmachines | 36:00ea3ae2cead | 270 | /* |
Crazyaboutmachines | 36:00ea3ae2cead | 271 | printf("\r\nreceived message ID: \t%d\n\r", rxMsg.id); |
Crazyaboutmachines | 36:00ea3ae2cead | 272 | for(int i=0; i<len; i++) { |
Crazyaboutmachines | 36:00ea3ae2cead | 273 | printf("\t%x",rxMsg.data[i]); |
Crazyaboutmachines | 36:00ea3ae2cead | 274 | }*/ |
Crazyaboutmachines | 36:00ea3ae2cead | 275 | // printf("\r\n"); |
Crazyaboutmachines | 36:00ea3ae2cead | 276 | /* if(rxMsg.data[4] == 1) { //counter == 12 |
Crazyaboutmachines | 36:00ea3ae2cead | 277 | |
Crazyaboutmachines | 36:00ea3ae2cead | 278 | printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n"); |
Crazyaboutmachines | 36:00ea3ae2cead | 279 | } |
Crazyaboutmachines | 36:00ea3ae2cead | 280 | */ |
Crazyaboutmachines | 36:00ea3ae2cead | 281 | // Filtering performed by software: |
Crazyaboutmachines | 37:1e1f3131fef5 | 282 | }else if((rxMsg.id==21)||(rxMsg.id==22)||(rxMsg.id==23)){ |
Crazyaboutmachines | 36:00ea3ae2cead | 283 | // cellst[(rxMsg.id-21)*32+rxMsg.data[4]-1]=data.f[0]; |
Crazyaboutmachines | 37:1e1f3131fef5 | 284 | |
Crazyaboutmachines | 37:1e1f3131fef5 | 285 | data.bytes[0] = rxMsg.data[0]; |
Crazyaboutmachines | 37:1e1f3131fef5 | 286 | data.bytes[1] = rxMsg.data[1]; |
Crazyaboutmachines | 37:1e1f3131fef5 | 287 | data.bytes[2] = rxMsg.data[2]; |
Crazyaboutmachines | 37:1e1f3131fef5 | 288 | data.bytes[3] = rxMsg.data[3]; |
Crazyaboutmachines | 37:1e1f3131fef5 | 289 | |
Crazyaboutmachines | 37:1e1f3131fef5 | 290 | |
Crazyaboutmachines | 37:1e1f3131fef5 | 291 | cellst[(rxMsg.id-21)*32+rxMsg.data[4]-1]=data.f[0]; |
Crazyaboutmachines | 36:00ea3ae2cead | 292 | |
Crazyaboutmachines | 36:00ea3ae2cead | 293 | //printf("temp: %d \r\n", rxMsg.data[1]); |
Crazyaboutmachines | 36:00ea3ae2cead | 294 | } |
Crazyaboutmachines | 36:00ea3ae2cead | 295 | |
hudakz | 0:c5e5d0df6f2a | 296 | } |
ser1516 | 21:13ac27349025 | 297 | if(to_send) { |
ser1516 | 21:13ac27349025 | 298 | to_send = 0; |
Crazyaboutmachines | 27:21239801cfd3 | 299 | |
Crazyaboutmachines | 27:21239801cfd3 | 300 | |
Crazyaboutmachines | 27:21239801cfd3 | 301 | //------------------------------------------------ |
Crazyaboutmachines | 27:21239801cfd3 | 302 | //ECU to BMS State |
Crazyaboutmachines | 26:3ac15dfbb66b | 303 | |
Crazyaboutmachines | 27:21239801cfd3 | 304 | //motostate: (0|0|0|0|0|0|to_charge_or_not_to_charge|key_switch) |
Crazyaboutmachines | 27:21239801cfd3 | 305 | txMsg.clear(); |
Crazyaboutmachines | 27:21239801cfd3 | 306 | txMsg.id = 9; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. |
Crazyaboutmachines | 27:21239801cfd3 | 307 | txMsg.len = 1; |
Crazyaboutmachines | 30:1bc79ae58a42 | 308 | motostate = (0b00000001 & key_switch)|((0b00000001 & charging)<<1); |
Crazyaboutmachines | 29:3af76325f86a | 309 | txMsg.data[0] = motostate; |
Crazyaboutmachines | 27:21239801cfd3 | 310 | //------------------------------------------------ |
Crazyaboutmachines | 27:21239801cfd3 | 311 | |
ser1516 | 21:13ac27349025 | 312 | if(can.write(txMsg)) { |
ser1516 | 21:13ac27349025 | 313 | printf("sent message\r\n"); |
ser1516 | 21:13ac27349025 | 314 | } else { |
Crazyaboutmachines | 27:21239801cfd3 | 315 | static char count = 0; //desta maneira o count é sempre zero e assim nunca chega a 3?? |
ser1516 | 21:13ac27349025 | 316 | count++; |
ser1516 | 21:13ac27349025 | 317 | printf("transmission error\n\r overflow: %x\n\r", count); |
ser1516 | 21:13ac27349025 | 318 | if(count == 3) { |
Crazyaboutmachines | 23:1a29761becd2 | 319 | count = 0; |
Crazyaboutmachines | 23:1a29761becd2 | 320 | NVIC_SystemReset(); //faz reset se estiver a falhar o envio de mensagens |
ser1516 | 21:13ac27349025 | 321 | // attach 'CAN receive-complete' interrupt handler |
ser1516 | 21:13ac27349025 | 322 | |
ser1516 | 21:13ac27349025 | 323 | } |
ser1516 | 21:13ac27349025 | 324 | |
hudakz | 0:c5e5d0df6f2a | 325 | } |
hudakz | 0:c5e5d0df6f2a | 326 | } |
hudakz | 0:c5e5d0df6f2a | 327 | } |
hudakz | 0:c5e5d0df6f2a | 328 | } |
hudakz | 7:2dce8ed51091 | 329 | |
hudakz | 12:e91e44924194 | 330 | |
hudakz | 17:18d4d0ff26a6 | 331 |