all bms comunicating with nucleo board and balancing

Dependencies:   CANnucleo mbed

Fork of Can_sniffer_BMS_GER by Joao Vieira

Committer:
Crazyaboutmachines
Date:
Sun Oct 23 19:25:01 2016 +0000
Revision:
25:76c6f417eb48
Parent:
24:c9c7dcdcbbc5
Child:
26:3ac15dfbb66b
a apresentar a tens?o de todas as c?lulas organizadamente

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