![](/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@23:1a29761becd2, 2016-10-22 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Sat Oct 22 19:06:09 2016 +0000
- Revision:
- 23:1a29761becd2
- Parent:
- 21:13ac27349025
- Child:
- 24:c9c7dcdcbbc5
todos os BMS a comunicar com o ECU mas ainda desorganizadamente
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 | |
ser1516 | 21:13ac27349025 | 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 | |
ser1516 | 21:13ac27349025 | 95 | Ticker flipper; |
ser1516 | 21:13ac27349025 | 96 | |
ser1516 | 21:13ac27349025 | 97 | typedef union can_union { |
ser1516 | 21:13ac27349025 | 98 | int i[2]; |
ser1516 | 21:13ac27349025 | 99 | char bytes[8]; |
ser1516 | 21:13ac27349025 | 100 | float f[2]; |
ser1516 | 21:13ac27349025 | 101 | } data; |
ser1516 | 21:13ac27349025 | 102 | |
ser1516 | 21:13ac27349025 | 103 | int main() |
ser1516 | 21:13ac27349025 | 104 | |
ser1516 | 21:13ac27349025 | 105 | { |
hudakz | 16:a86f339d1c25 | 106 | can.frequency(1000000); // set bit rate to 1Mbps |
hudakz | 16:a86f339d1c25 | 107 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
ser1516 | 21:13ac27349025 | 108 | flipper.attach(&flip, 30); // turn on or off |
ser1516 | 21:13ac27349025 | 109 | led=key_switch; |
hudakz | 10:66da8731bdb6 | 110 | timer.start(); // start timer |
ser1516 | 21:13ac27349025 | 111 | |
ser1516 | 21:13ac27349025 | 112 | printf("started\r\n"); |
ser1516 | 21:13ac27349025 | 113 | while(true) { |
ser1516 | 21:13ac27349025 | 114 | |
ser1516 | 21:13ac27349025 | 115 | if(msgAvailable) { |
ser1516 | 21:13ac27349025 | 116 | static int counter = 0; |
ser1516 | 21:13ac27349025 | 117 | data data; |
ser1516 | 21:13ac27349025 | 118 | int len = can.read(rxMsg); |
ser1516 | 21:13ac27349025 | 119 | data.bytes[0] = rxMsg.data[0]; |
ser1516 | 21:13ac27349025 | 120 | data.bytes[1] = rxMsg.data[1]; |
ser1516 | 21:13ac27349025 | 121 | data.bytes[2] = rxMsg.data[2]; |
ser1516 | 21:13ac27349025 | 122 | data.bytes[3] = rxMsg.data[3]; |
ser1516 | 21:13ac27349025 | 123 | msgAvailable = false; // reset flag for next use |
ser1516 | 21:13ac27349025 | 124 | printf(" Id: %d, data: %f, counter : %d\n", rxMsg.id, data.f[0],rxMsg.data[4]); |
hudakz | 0:c5e5d0df6f2a | 125 | |
ser1516 | 21:13ac27349025 | 126 | /* |
ser1516 | 21:13ac27349025 | 127 | printf("\r\nreceived message ID: \t%d\n\r", rxMsg.id); |
ser1516 | 21:13ac27349025 | 128 | for(int i=0; i<len; i++) { |
ser1516 | 21:13ac27349025 | 129 | printf("\t%x",rxMsg.data[i]); |
ser1516 | 21:13ac27349025 | 130 | }*/ |
ser1516 | 21:13ac27349025 | 131 | printf("\r\n"); |
ser1516 | 21:13ac27349025 | 132 | counter++; |
Crazyaboutmachines | 23:1a29761becd2 | 133 | if(rxMsg.data[4] == 1) { //counter == 12 |
ser1516 | 21:13ac27349025 | 134 | |
ser1516 | 21:13ac27349025 | 135 | counter = 0; |
ser1516 | 21:13ac27349025 | 136 | printf("\r\n""""""""""""""""""""""""""""""""""""""""""""""""\r\n"); |
ser1516 | 21:13ac27349025 | 137 | } |
ser1516 | 21:13ac27349025 | 138 | // Filtering performed by software: |
hudakz | 0:c5e5d0df6f2a | 139 | } |
ser1516 | 21:13ac27349025 | 140 | if(to_send) { |
ser1516 | 21:13ac27349025 | 141 | to_send = 0; |
ser1516 | 21:13ac27349025 | 142 | txMsg.clear(); |
Crazyaboutmachines | 23:1a29761becd2 | 143 | txMsg.id = 9; |
ser1516 | 21:13ac27349025 | 144 | txMsg << key_switch; |
ser1516 | 21:13ac27349025 | 145 | if(can.write(txMsg)) { |
ser1516 | 21:13ac27349025 | 146 | printf("sent message\r\n"); |
ser1516 | 21:13ac27349025 | 147 | } else { |
ser1516 | 21:13ac27349025 | 148 | static char count = 0; |
ser1516 | 21:13ac27349025 | 149 | count++; |
ser1516 | 21:13ac27349025 | 150 | printf("transmission error\n\r overflow: %x\n\r", count); |
ser1516 | 21:13ac27349025 | 151 | if(count == 3) { |
Crazyaboutmachines | 23:1a29761becd2 | 152 | count = 0; |
Crazyaboutmachines | 23:1a29761becd2 | 153 | NVIC_SystemReset(); //faz reset se estiver a falhar o envio de mensagens |
ser1516 | 21:13ac27349025 | 154 | // attach 'CAN receive-complete' interrupt handler |
ser1516 | 21:13ac27349025 | 155 | |
ser1516 | 21:13ac27349025 | 156 | } |
ser1516 | 21:13ac27349025 | 157 | |
hudakz | 0:c5e5d0df6f2a | 158 | } |
hudakz | 0:c5e5d0df6f2a | 159 | } |
hudakz | 0:c5e5d0df6f2a | 160 | } |
hudakz | 0:c5e5d0df6f2a | 161 | } |
hudakz | 7:2dce8ed51091 | 162 | |
hudakz | 12:e91e44924194 | 163 | |
hudakz | 17:18d4d0ff26a6 | 164 |