all bms comunicating with nucleo board and balancing

Dependencies:   CANnucleo mbed

Fork of Can_sniffer_BMS_GER by Joao Vieira

Committer:
Crazyaboutmachines
Date:
Mon Oct 24 20:55:53 2016 +0000
Revision:
27:21239801cfd3
Parent:
26:3ac15dfbb66b
Child:
28:2329d581e394
conseguiu activar o balanceamento mas nao desactiva-lo

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 26:3ac15dfbb66b 26 //CAN devices IDs and reserved ID's-----------------------------------
Crazyaboutmachines 23:1a29761becd2 27 //ID|Device
Crazyaboutmachines 26:3ac15dfbb66b 28 //9|ECU|Key switch
Crazyaboutmachines 26:3ac15dfbb66b 29 //10|ECU|Charge/discharge
Crazyaboutmachines 26:3ac15dfbb66b 30 //11|BMS1|Cell voltages
Crazyaboutmachines 26:3ac15dfbb66b 31 //12|BMS2|Cell voltages
Crazyaboutmachines 26:3ac15dfbb66b 32 //13|BMS3|Cell voltages
Crazyaboutmachines 26:3ac15dfbb66b 33 //?|Charger|
Crazyaboutmachines 26:3ac15dfbb66b 34 //
Crazyaboutmachines 26:3ac15dfbb66b 35 //--------------------------------------------------------------------
Crazyaboutmachines 23:1a29761becd2 36
hudakz 0:c5e5d0df6f2a 37
ser1516 21:13ac27349025 38 const unsigned int RX_ID = 0x100;
ser1516 21:13ac27349025 39 const unsigned int TX_ID = 0x101;
hudakz 6:7ff95ce72f6d 40
hudakz 19:872e304d7e17 41 #include "CANnucleo.h"
hudakz 16:a86f339d1c25 42 #include "mbed.h"
hudakz 16:a86f339d1c25 43
ser1516 21:13ac27349025 44 /*
hudakz 17:18d4d0ff26a6 45 * To avaoid name collision with the CAN and CANMessage classes built into the mbed library
hudakz 17:18d4d0ff26a6 46 * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace.
hudakz 20:eb1a8042605e 47 * Remember to qualify them with the CANnucleo namespace.
hudakz 17:18d4d0ff26a6 48 */
hudakz 17:18d4d0ff26a6 49 CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
hudakz 17:18d4d0ff26a6 50 CANnucleo::CANMessage rxMsg;
hudakz 17:18d4d0ff26a6 51 CANnucleo::CANMessage txMsg;
ser1516 21:13ac27349025 52 CANnucleo::CANMessage throttle_txMsg;
ser1516 21:13ac27349025 53
ser1516 21:13ac27349025 54
ser1516 21:13ac27349025 55 DigitalOut led(PA_5);
ser1516 21:13ac27349025 56
hudakz 18:22977a898fe9 57 Timer timer;
hudakz 17:18d4d0ff26a6 58 volatile bool msgAvailable = false;
ser1516 21:13ac27349025 59 volatile bool to_send = false;
Crazyaboutmachines 25:76c6f417eb48 60 float cellsv[36];
hudakz 16:a86f339d1c25 61 /**
hudakz 16:a86f339d1c25 62 * @brief 'CAN receive-complete' interrup handler.
hudakz 16:a86f339d1c25 63 * @note Called on arrival of new CAN message.
hudakz 16:a86f339d1c25 64 * Keep it as short as possible.
ser1516 21:13ac27349025 65 * @param
ser1516 21:13ac27349025 66 * @retval
hudakz 16:a86f339d1c25 67 */
ser1516 21:13ac27349025 68 void onMsgReceived()
ser1516 21:13ac27349025 69 {
hudakz 16:a86f339d1c25 70 msgAvailable = true;
hudakz 16:a86f339d1c25 71 }
hudakz 16:a86f339d1c25 72
hudakz 16:a86f339d1c25 73 /**
hudakz 16:a86f339d1c25 74 * @brief Main
hudakz 16:a86f339d1c25 75 * @note
ser1516 21:13ac27349025 76 * @param
hudakz 16:a86f339d1c25 77 * @retval
hudakz 16:a86f339d1c25 78 */
ser1516 21:13ac27349025 79
ser1516 21:13ac27349025 80 bool key_switch = 0;
ser1516 21:13ac27349025 81
Crazyaboutmachines 25:76c6f417eb48 82
Crazyaboutmachines 25:76c6f417eb48 83 void flip()
ser1516 21:13ac27349025 84 {
ser1516 21:13ac27349025 85 key_switch = !key_switch;
ser1516 21:13ac27349025 86 led = key_switch;
ser1516 21:13ac27349025 87
ser1516 21:13ac27349025 88 to_send=1;
ser1516 21:13ac27349025 89 //printf("controller switch\r\n");
ser1516 21:13ac27349025 90
ser1516 21:13ac27349025 91
ser1516 21:13ac27349025 92
ser1516 21:13ac27349025 93 // to_send = 1;
ser1516 21:13ac27349025 94 }
ser1516 21:13ac27349025 95
Crazyaboutmachines 24:c9c7dcdcbbc5 96 void cvprint(){
Crazyaboutmachines 25:76c6f417eb48 97 int n;
Crazyaboutmachines 25:76c6f417eb48 98 for(n=35; n>=0; n--){
Crazyaboutmachines 25:76c6f417eb48 99 //printf("cellsv0: %f cvprint\r\n", cellsv[0]);
Crazyaboutmachines 25:76c6f417eb48 100 printf("cell: %d voltage: %f \r\n", n+1,cellsv[n]);
Crazyaboutmachines 25:76c6f417eb48 101 }
Crazyaboutmachines 25:76c6f417eb48 102 printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n");
Crazyaboutmachines 24:c9c7dcdcbbc5 103 }
Crazyaboutmachines 24:c9c7dcdcbbc5 104
Crazyaboutmachines 24:c9c7dcdcbbc5 105
Crazyaboutmachines 25:76c6f417eb48 106 Ticker flipper;
Crazyaboutmachines 25:76c6f417eb48 107 Ticker printer;
ser1516 21:13ac27349025 108
ser1516 21:13ac27349025 109 typedef union can_union {
ser1516 21:13ac27349025 110 int i[2];
ser1516 21:13ac27349025 111 char bytes[8];
ser1516 21:13ac27349025 112 float f[2];
ser1516 21:13ac27349025 113 } data;
ser1516 21:13ac27349025 114
Crazyaboutmachines 27:21239801cfd3 115 bool to_charge_or_not_to_charge=true; // false = discharge
Crazyaboutmachines 27:21239801cfd3 116
ser1516 21:13ac27349025 117 int main()
ser1516 21:13ac27349025 118
ser1516 21:13ac27349025 119 {
hudakz 16:a86f339d1c25 120 can.frequency(1000000); // set bit rate to 1Mbps
hudakz 16:a86f339d1c25 121 can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler
Crazyaboutmachines 25:76c6f417eb48 122 flipper.attach(&flip, 30); // turn on or off
Crazyaboutmachines 25:76c6f417eb48 123
Crazyaboutmachines 25:76c6f417eb48 124 printer.attach(&cvprint, 20); // turn on o
ser1516 21:13ac27349025 125 led=key_switch;
hudakz 10:66da8731bdb6 126 timer.start(); // start timer
ser1516 21:13ac27349025 127
ser1516 21:13ac27349025 128 printf("started\r\n");
ser1516 21:13ac27349025 129 while(true) {
ser1516 21:13ac27349025 130
ser1516 21:13ac27349025 131 if(msgAvailable) {
ser1516 21:13ac27349025 132 data data;
ser1516 21:13ac27349025 133 int len = can.read(rxMsg);
ser1516 21:13ac27349025 134 data.bytes[0] = rxMsg.data[0];
ser1516 21:13ac27349025 135 data.bytes[1] = rxMsg.data[1];
ser1516 21:13ac27349025 136 data.bytes[2] = rxMsg.data[2];
ser1516 21:13ac27349025 137 data.bytes[3] = rxMsg.data[3];
ser1516 21:13ac27349025 138 msgAvailable = false; // reset flag for next use
Crazyaboutmachines 25:76c6f417eb48 139 // printf(" Id: %d, data: %f, counter : %d\n", rxMsg.id, data.f[0],rxMsg.data[4]);
Crazyaboutmachines 25:76c6f417eb48 140 cellsv[(rxMsg.id-11)*12+rxMsg.data[4]-1]=data.f[0];
Crazyaboutmachines 25:76c6f417eb48 141 //printf("cell: %d\r\n", rxMsg.data[4]);
ser1516 21:13ac27349025 142 /*
ser1516 21:13ac27349025 143 printf("\r\nreceived message ID: \t%d\n\r", rxMsg.id);
ser1516 21:13ac27349025 144 for(int i=0; i<len; i++) {
ser1516 21:13ac27349025 145 printf("\t%x",rxMsg.data[i]);
ser1516 21:13ac27349025 146 }*/
Crazyaboutmachines 25:76c6f417eb48 147 // printf("\r\n");
Crazyaboutmachines 25:76c6f417eb48 148 /* if(rxMsg.data[4] == 1) { //counter == 12
ser1516 21:13ac27349025 149
ser1516 21:13ac27349025 150 printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n");
ser1516 21:13ac27349025 151 }
Crazyaboutmachines 24:c9c7dcdcbbc5 152 */
ser1516 21:13ac27349025 153 // Filtering performed by software:
hudakz 0:c5e5d0df6f2a 154 }
ser1516 21:13ac27349025 155 if(to_send) {
ser1516 21:13ac27349025 156 to_send = 0;
Crazyaboutmachines 27:21239801cfd3 157
Crazyaboutmachines 27:21239801cfd3 158
Crazyaboutmachines 27:21239801cfd3 159 //------------------------------------------------
Crazyaboutmachines 27:21239801cfd3 160 //ECU to BMS State
Crazyaboutmachines 26:3ac15dfbb66b 161
Crazyaboutmachines 27:21239801cfd3 162 //motostate: (0|0|0|0|0|0|to_charge_or_not_to_charge|key_switch)
Crazyaboutmachines 27:21239801cfd3 163 txMsg.clear();
Crazyaboutmachines 27:21239801cfd3 164 txMsg.id = 9; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13.
Crazyaboutmachines 27:21239801cfd3 165 txMsg.len = 1;
Crazyaboutmachines 27:21239801cfd3 166 // txMsg.data[0] = (0b00000001 & key_switch)|((0b00000001 & to_charge_or_not_to_charge)<<1);
Crazyaboutmachines 27:21239801cfd3 167 txMsg.data[0] = 0b00000000;
Crazyaboutmachines 27:21239801cfd3 168 //------------------------------------------------
Crazyaboutmachines 27:21239801cfd3 169
ser1516 21:13ac27349025 170 if(can.write(txMsg)) {
ser1516 21:13ac27349025 171 printf("sent message\r\n");
ser1516 21:13ac27349025 172 } else {
Crazyaboutmachines 27:21239801cfd3 173 static char count = 0; //desta maneira o count é sempre zero e assim nunca chega a 3??
ser1516 21:13ac27349025 174 count++;
ser1516 21:13ac27349025 175 printf("transmission error\n\r overflow: %x\n\r", count);
ser1516 21:13ac27349025 176 if(count == 3) {
Crazyaboutmachines 23:1a29761becd2 177 count = 0;
Crazyaboutmachines 23:1a29761becd2 178 NVIC_SystemReset(); //faz reset se estiver a falhar o envio de mensagens
ser1516 21:13ac27349025 179 // attach 'CAN receive-complete' interrupt handler
ser1516 21:13ac27349025 180
ser1516 21:13ac27349025 181 }
ser1516 21:13ac27349025 182
hudakz 0:c5e5d0df6f2a 183 }
hudakz 0:c5e5d0df6f2a 184 }
hudakz 0:c5e5d0df6f2a 185 }
hudakz 0:c5e5d0df6f2a 186 }
hudakz 7:2dce8ed51091 187
hudakz 12:e91e44924194 188
hudakz 17:18d4d0ff26a6 189