![](/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@24:c9c7dcdcbbc5, 2016-10-23 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Sun Oct 23 13:10:00 2016 +0000
- Revision:
- 24:c9c7dcdcbbc5
- Parent:
- 23:1a29761becd2
- Child:
- 25:76c6f417eb48
cell ordering fail
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 | 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 | int counter = 0; |
hudakz | 17:18d4d0ff26a6 | 58 | volatile bool msgAvailable = false; |
ser1516 | 21:13ac27349025 | 59 | volatile bool to_send = false; |
hudakz | 0:c5e5d0df6f2a | 60 | |
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 | 24:c9c7dcdcbbc5 | 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 | */ |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 96 | volatile float cells[36]; |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 97 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 98 | void cvprint(){ |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 99 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 100 | int i=0; |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 101 | for(i=0; i<=35; i=i+1) |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 102 | printf("data: %f, cell : %d\n", cells[i],i); |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 103 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 104 | } |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 105 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 106 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 107 | //Ticker flipper; |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 108 | Ticker cvprinter; |
ser1516 | 21:13ac27349025 | 109 | |
ser1516 | 21:13ac27349025 | 110 | typedef union can_union { |
ser1516 | 21:13ac27349025 | 111 | int i[2]; |
ser1516 | 21:13ac27349025 | 112 | char bytes[8]; |
ser1516 | 21:13ac27349025 | 113 | float f[2]; |
ser1516 | 21:13ac27349025 | 114 | } data; |
ser1516 | 21:13ac27349025 | 115 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 116 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 117 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 118 | |
ser1516 | 21:13ac27349025 | 119 | int main() |
ser1516 | 21:13ac27349025 | 120 | |
ser1516 | 21:13ac27349025 | 121 | { |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 122 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 123 | int n=0; |
hudakz | 16:a86f339d1c25 | 124 | can.frequency(1000000); // set bit rate to 1Mbps |
hudakz | 16:a86f339d1c25 | 125 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 126 | // flipper.attach(&flip, 30); // turn on or off |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 127 | cvprinter.attach(&cvprint, 30); |
ser1516 | 21:13ac27349025 | 128 | led=key_switch; |
hudakz | 10:66da8731bdb6 | 129 | timer.start(); // start timer |
ser1516 | 21:13ac27349025 | 130 | |
ser1516 | 21:13ac27349025 | 131 | printf("started\r\n"); |
ser1516 | 21:13ac27349025 | 132 | while(true) { |
ser1516 | 21:13ac27349025 | 133 | |
ser1516 | 21:13ac27349025 | 134 | if(msgAvailable) { |
ser1516 | 21:13ac27349025 | 135 | static int counter = 0; |
ser1516 | 21:13ac27349025 | 136 | data data; |
ser1516 | 21:13ac27349025 | 137 | int len = can.read(rxMsg); |
ser1516 | 21:13ac27349025 | 138 | data.bytes[0] = rxMsg.data[0]; |
ser1516 | 21:13ac27349025 | 139 | data.bytes[1] = rxMsg.data[1]; |
ser1516 | 21:13ac27349025 | 140 | data.bytes[2] = rxMsg.data[2]; |
ser1516 | 21:13ac27349025 | 141 | data.bytes[3] = rxMsg.data[3]; |
ser1516 | 21:13ac27349025 | 142 | msgAvailable = false; // reset flag for next use |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 143 | // printf(" Id: %d, data: %f, counter : %d\n", rxMsg.id, data.f[0],rxMsg.data[4]); |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 144 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 145 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 146 | n = (rxMsg.id-11)*12+rxMsg.data[4]; |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 147 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 148 | cells[n]=data.f[0]; |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 149 | //------------------------------ |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 150 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 151 | /* |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 152 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 153 | if(rxMsg.id==11){ |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 154 | cells[rxMsg.data[4]]=data.f[0]; |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 155 | } |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 156 | if(rxMsg.id==12){ |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 157 | cells[rxMsg.data[4]+12]=data.f[0]; |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 158 | } |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 159 | if(rxMsg.id==13){ |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 160 | cells[rxMsg.data[4]+24]=data.f[0]; |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 161 | } |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 162 | */ |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 163 | |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 164 | //---------------------------- |
hudakz | 0:c5e5d0df6f2a | 165 | |
ser1516 | 21:13ac27349025 | 166 | /* |
ser1516 | 21:13ac27349025 | 167 | printf("\r\nreceived message ID: \t%d\n\r", rxMsg.id); |
ser1516 | 21:13ac27349025 | 168 | for(int i=0; i<len; i++) { |
ser1516 | 21:13ac27349025 | 169 | printf("\t%x",rxMsg.data[i]); |
ser1516 | 21:13ac27349025 | 170 | }*/ |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 171 | //printf("\r\n"); |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 172 | //counter++; |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 173 | /*if(rxMsg.data[4] == 1) { //counter == 12 |
ser1516 | 21:13ac27349025 | 174 | |
ser1516 | 21:13ac27349025 | 175 | counter = 0; |
ser1516 | 21:13ac27349025 | 176 | printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n"); |
ser1516 | 21:13ac27349025 | 177 | } |
Crazyaboutmachines | 24:c9c7dcdcbbc5 | 178 | */ |
ser1516 | 21:13ac27349025 | 179 | // Filtering performed by software: |
hudakz | 0:c5e5d0df6f2a | 180 | } |
ser1516 | 21:13ac27349025 | 181 | if(to_send) { |
ser1516 | 21:13ac27349025 | 182 | to_send = 0; |
ser1516 | 21:13ac27349025 | 183 | txMsg.clear(); |
Crazyaboutmachines | 23:1a29761becd2 | 184 | txMsg.id = 9; |
ser1516 | 21:13ac27349025 | 185 | txMsg << key_switch; |
ser1516 | 21:13ac27349025 | 186 | if(can.write(txMsg)) { |
ser1516 | 21:13ac27349025 | 187 | printf("sent message\r\n"); |
ser1516 | 21:13ac27349025 | 188 | } else { |
ser1516 | 21:13ac27349025 | 189 | static char count = 0; |
ser1516 | 21:13ac27349025 | 190 | count++; |
ser1516 | 21:13ac27349025 | 191 | printf("transmission error\n\r overflow: %x\n\r", count); |
ser1516 | 21:13ac27349025 | 192 | if(count == 3) { |
Crazyaboutmachines | 23:1a29761becd2 | 193 | count = 0; |
Crazyaboutmachines | 23:1a29761becd2 | 194 | NVIC_SystemReset(); //faz reset se estiver a falhar o envio de mensagens |
ser1516 | 21:13ac27349025 | 195 | // attach 'CAN receive-complete' interrupt handler |
ser1516 | 21:13ac27349025 | 196 | |
ser1516 | 21:13ac27349025 | 197 | } |
ser1516 | 21:13ac27349025 | 198 | |
hudakz | 0:c5e5d0df6f2a | 199 | } |
hudakz | 0:c5e5d0df6f2a | 200 | } |
hudakz | 0:c5e5d0df6f2a | 201 | } |
hudakz | 0:c5e5d0df6f2a | 202 | } |
hudakz | 7:2dce8ed51091 | 203 | |
hudakz | 12:e91e44924194 | 204 | |
hudakz | 17:18d4d0ff26a6 | 205 |