Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:e57b023e41f3, 2020-04-30 (annotated)
- Committer:
- takuma1
- Date:
- Thu Apr 30 08:05:06 2020 +0000
- Revision:
- 4:e57b023e41f3
- Parent:
- 3:a1368cd4b0a9
- Child:
- 5:f07de56debf3
LTC6811_CAN
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
APS_Lab | 0:f06ed53310a3 | 1 | |
APS_Lab | 0:f06ed53310a3 | 2 | #include "mbed.h" |
APS_Lab | 0:f06ed53310a3 | 3 | #include "bms.h" |
APS_Lab | 0:f06ed53310a3 | 4 | #include "LTC681x.h" |
APS_Lab | 0:f06ed53310a3 | 5 | #include "LTC6811.h" |
takuma1 | 4:e57b023e41f3 | 6 | #include "INA226.hpp" |
takuma1 | 4:e57b023e41f3 | 7 | //#include "MSCFileSystem.h" |
APS_Lab | 0:f06ed53310a3 | 8 | |
APS_Lab | 0:f06ed53310a3 | 9 | #define UI_BUFFER_SIZE 64 |
APS_Lab | 0:f06ed53310a3 | 10 | #define SERIAL_TERMINATOR '\n' |
APS_Lab | 0:f06ed53310a3 | 11 | |
APS_Lab | 0:f06ed53310a3 | 12 | #define ENABLED 1 |
APS_Lab | 0:f06ed53310a3 | 13 | #define DISABLED 0 |
APS_Lab | 0:f06ed53310a3 | 14 | |
APS_Lab | 0:f06ed53310a3 | 15 | #define DATALOG_ENABLED 1 |
APS_Lab | 0:f06ed53310a3 | 16 | #define DATALOG_DISABLED 0 |
takuma1 | 2:f6f76dde7e1d | 17 | |
takuma1 | 4:e57b023e41f3 | 18 | |
takuma1 | 4:e57b023e41f3 | 19 | |
APS_Lab | 0:f06ed53310a3 | 20 | DigitalOut led1(LED1); |
APS_Lab | 0:f06ed53310a3 | 21 | Serial pc(USBTX, USBRX); |
takuma1 | 4:e57b023e41f3 | 22 | CAN canSlave(p30,p29); |
takuma1 | 4:e57b023e41f3 | 23 | I2C i2c(p28,p27); |
takuma1 | 4:e57b023e41f3 | 24 | DigitalOut led2(LED2); |
takuma1 | 4:e57b023e41f3 | 25 | DigitalOut BAT_MIN_safty(p21); |
takuma1 | 4:e57b023e41f3 | 26 | DigitalOut BAT_MAX_safty(p22); |
takuma1 | 4:e57b023e41f3 | 27 | //MSCFileSystem msc("usb"); // Mount flash drive under the name "msc" |
takuma1 | 4:e57b023e41f3 | 28 | //Timer timer; //書き込み時間を計測するタイマ |
APS_Lab | 0:f06ed53310a3 | 29 | |
takuma1 | 4:e57b023e41f3 | 30 | // CAN関係 |
takuma1 | 4:e57b023e41f3 | 31 | short int forSend = 0; //データ送信時に一時的に使用する変数 |
takuma1 | 4:e57b023e41f3 | 32 | bool CANsendOK = 0; //CAN送信完了時,セットする(mainでのprintfのため) |
takuma1 | 4:e57b023e41f3 | 33 | CANMessage msgSlave1; //CAN送信用 |
takuma1 | 4:e57b023e41f3 | 34 | short int canSlaveID = 0x20; //canSlaveのIDをに設定 |
takuma1 | 4:e57b023e41f3 | 35 | void Handler_canSend(); |
takuma1 | 4:e57b023e41f3 | 36 | bool flag_can = 1; |
takuma1 | 4:e57b023e41f3 | 37 | |
takuma1 | 4:e57b023e41f3 | 38 | //測定関係の関数(変更の際触る必要がない) |
takuma1 | 4:e57b023e41f3 | 39 | void run_command(uint32_t cmd); //測定コマンド送信関数 |
APS_Lab | 0:f06ed53310a3 | 40 | void measurement_loop(uint8_t datalog_en); |
takuma1 | 4:e57b023e41f3 | 41 | void print_cells(uint8_t datalog_en); //セル電圧の測定表示関数 |
APS_Lab | 0:f06ed53310a3 | 42 | void print_open(); |
APS_Lab | 0:f06ed53310a3 | 43 | void print_aux(uint8_t datalog_en); |
APS_Lab | 0:f06ed53310a3 | 44 | void print_stat(); |
APS_Lab | 0:f06ed53310a3 | 45 | void print_config(); |
APS_Lab | 0:f06ed53310a3 | 46 | void print_rxconfig(); |
APS_Lab | 0:f06ed53310a3 | 47 | void print_pec(void); |
APS_Lab | 0:f06ed53310a3 | 48 | void serial_print_hex(uint8_t data); |
APS_Lab | 0:f06ed53310a3 | 49 | void check_error(int error); |
takuma1 | 2:f6f76dde7e1d | 50 | |
takuma1 | 4:e57b023e41f3 | 51 | //測定関係関数(使ってる) |
takuma1 | 4:e57b023e41f3 | 52 | void wakeup();//ICの起動関数、消費電力削減関数 |
takuma1 | 4:e57b023e41f3 | 53 | void cell_read();//セルの電圧の読み込みコマンド |
takuma1 | 4:e57b023e41f3 | 54 | void spi_error();//SPIエラー処理関数 |
takuma1 | 4:e57b023e41f3 | 55 | void ic_check();//ICが測定可能状態か診断して次のコマンドに持ち込むための関数 |
takuma1 | 4:e57b023e41f3 | 56 | void spi_check();//SPIエラーの確認関数 |
takuma1 | 4:e57b023e41f3 | 57 | void print_CAN(uint8_t datalog_en);//セルの電圧の表示コマンドと過充電過放電検出 |
takuma1 | 4:e57b023e41f3 | 58 | void BAT_safty(); |
takuma1 | 4:e57b023e41f3 | 59 | void print_math(); |
takuma1 | 4:e57b023e41f3 | 60 | void can_sent1(); |
takuma1 | 4:e57b023e41f3 | 61 | void can_sent2(); |
takuma1 | 4:e57b023e41f3 | 62 | void can_sent3(); |
takuma1 | 4:e57b023e41f3 | 63 | void can_sent4(); |
takuma1 | 4:e57b023e41f3 | 64 | //void can_set(); |
takuma1 | 4:e57b023e41f3 | 65 | void can_wait(); |
takuma1 | 4:e57b023e41f3 | 66 | void ic_set(); |
APS_Lab | 0:f06ed53310a3 | 67 | |
takuma1 | 4:e57b023e41f3 | 68 | |
APS_Lab | 0:f06ed53310a3 | 69 | |
takuma1 | 4:e57b023e41f3 | 70 | |
takuma1 | 4:e57b023e41f3 | 71 | const uint8_t TOTAL_IC = 3; //IC数 |
APS_Lab | 0:f06ed53310a3 | 72 | char ui_buffer[UI_BUFFER_SIZE]; |
APS_Lab | 0:f06ed53310a3 | 73 | |
APS_Lab | 0:f06ed53310a3 | 74 | |
APS_Lab | 0:f06ed53310a3 | 75 | |
takuma1 | 4:e57b023e41f3 | 76 | const uint8_t ADC_CONVERSION_MODE = MD_7KHZ_3KHZ; |
takuma1 | 4:e57b023e41f3 | 77 | const uint8_t ADC_DCP = DCP_DISABLED; |
takuma1 | 4:e57b023e41f3 | 78 | const uint8_t CELL_CH_TO_CONVERT = CELL_CH_ALL; |
takuma1 | 4:e57b023e41f3 | 79 | const uint8_t AUX_CH_TO_CONVERT = AUX_CH_ALL; |
takuma1 | 4:e57b023e41f3 | 80 | const uint8_t STAT_CH_TO_CONVERT = STAT_CH_ALL; |
APS_Lab | 0:f06ed53310a3 | 81 | |
takuma1 | 4:e57b023e41f3 | 82 | const uint16_t MEASUREMENT_LOOP_TIME = 10;//milliseconds(mS) |
takuma1 | 2:f6f76dde7e1d | 83 | const uint16_t OV_THRESHOLD = 41000; // |
takuma1 | 2:f6f76dde7e1d | 84 | const uint16_t UV_THRESHOLD = 30000; // |
APS_Lab | 0:f06ed53310a3 | 85 | |
takuma1 | 4:e57b023e41f3 | 86 | |
takuma1 | 4:e57b023e41f3 | 87 | const uint8_t WRITE_CONFIG = DISABLED; |
takuma1 | 4:e57b023e41f3 | 88 | const uint8_t READ_CONFIG = DISABLED; |
takuma1 | 4:e57b023e41f3 | 89 | const uint8_t MEASURE_CELL = ENABLED; |
takuma1 | 4:e57b023e41f3 | 90 | const uint8_t MEASURE_AUX = DISABLED; |
takuma1 | 4:e57b023e41f3 | 91 | const uint8_t MEASURE_STAT = DISABLED; |
takuma1 | 4:e57b023e41f3 | 92 | const uint8_t PRINT_PEC = DISABLED; |
takuma1 | 3:a1368cd4b0a9 | 93 | short n =0; |
APS_Lab | 0:f06ed53310a3 | 94 | |
APS_Lab | 0:f06ed53310a3 | 95 | uint8_t read_data(); |
APS_Lab | 0:f06ed53310a3 | 96 | |
APS_Lab | 0:f06ed53310a3 | 97 | float read_float(); |
takuma1 | 4:e57b023e41f3 | 98 | INA226 VCmonitor(i2c); // INA226 |
APS_Lab | 0:f06ed53310a3 | 99 | |
APS_Lab | 0:f06ed53310a3 | 100 | int32_t read_int(); |
takuma1 | 4:e57b023e41f3 | 101 | float BAT_MIN ; |
takuma1 | 4:e57b023e41f3 | 102 | float BAT_MAX ; |
takuma1 | 4:e57b023e41f3 | 103 | float BAT_AVG ; |
takuma1 | 4:e57b023e41f3 | 104 | double BAT_C; |
takuma1 | 4:e57b023e41f3 | 105 | float BAT_SUM; |
takuma1 | 3:a1368cd4b0a9 | 106 | unsigned short BAT_CELL[23]; |
takuma1 | 3:a1368cd4b0a9 | 107 | |
APS_Lab | 0:f06ed53310a3 | 108 | char *read_string(); |
takuma1 | 4:e57b023e41f3 | 109 | /* |
takuma1 | 4:e57b023e41f3 | 110 | int Replace1; |
takuma1 | 4:e57b023e41f3 | 111 | int Replace2; |
takuma1 | 4:e57b023e41f3 | 112 | int Replace3; |
takuma1 | 4:e57b023e41f3 | 113 | int Replace4; |
takuma1 | 4:e57b023e41f3 | 114 | */ |
takuma1 | 3:a1368cd4b0a9 | 115 | |
APS_Lab | 0:f06ed53310a3 | 116 | int8_t read_char(); |
APS_Lab | 0:f06ed53310a3 | 117 | |
APS_Lab | 0:f06ed53310a3 | 118 | cell_asic bms_ic[TOTAL_IC]; |
APS_Lab | 0:f06ed53310a3 | 119 | |
takuma1 | 4:e57b023e41f3 | 120 | |
takuma1 | 4:e57b023e41f3 | 121 | void Handler_canSend() { |
takuma1 | 4:e57b023e41f3 | 122 | |
takuma1 | 4:e57b023e41f3 | 123 | int Replace1 = 0; |
takuma1 | 4:e57b023e41f3 | 124 | int Replace2 = 0; |
takuma1 | 4:e57b023e41f3 | 125 | int Replace3 = 0; |
takuma1 | 4:e57b023e41f3 | 126 | short int Replace4 = 0; |
takuma1 | 4:e57b023e41f3 | 127 | |
takuma1 | 4:e57b023e41f3 | 128 | short n =0; |
takuma1 | 4:e57b023e41f3 | 129 | int BAT_SUM = 0; // 0にする |
takuma1 | 4:e57b023e41f3 | 130 | int BAT_MIN = 55876; |
takuma1 | 4:e57b023e41f3 | 131 | double C; |
takuma1 | 4:e57b023e41f3 | 132 | //unsigned short BTA_MAX = 0; |
takuma1 | 4:e57b023e41f3 | 133 | |
takuma1 | 4:e57b023e41f3 | 134 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
takuma1 | 4:e57b023e41f3 | 135 | for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) { |
takuma1 | 4:e57b023e41f3 | 136 | |
takuma1 | 4:e57b023e41f3 | 137 | //pc.printf("C%d:", i+1); |
takuma1 | 4:e57b023e41f3 | 138 | pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); |
takuma1 | 4:e57b023e41f3 | 139 | BAT_SUM = bms_ic[current_ic].cells.c_codes[i] + BAT_SUM; |
takuma1 | 4:e57b023e41f3 | 140 | |
takuma1 | 4:e57b023e41f3 | 141 | if(bms_ic[current_ic].cells.c_codes[i] > 10000){ |
takuma1 | 4:e57b023e41f3 | 142 | if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){ |
takuma1 | 4:e57b023e41f3 | 143 | BAT_MIN = bms_ic[current_ic].cells.c_codes[i]; |
takuma1 | 4:e57b023e41f3 | 144 | } |
takuma1 | 4:e57b023e41f3 | 145 | } |
takuma1 | 4:e57b023e41f3 | 146 | |
takuma1 | 4:e57b023e41f3 | 147 | if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){ |
takuma1 | 4:e57b023e41f3 | 148 | BAT_MAX = bms_ic[current_ic].cells.c_codes[i]; |
takuma1 | 4:e57b023e41f3 | 149 | } |
takuma1 | 4:e57b023e41f3 | 150 | } |
takuma1 | 4:e57b023e41f3 | 151 | |
takuma1 | 4:e57b023e41f3 | 152 | if(VCmonitor.getCurrent(&C) == 0){ // INA226 |
takuma1 | 4:e57b023e41f3 | 153 | BAT_C = C; |
takuma1 | 4:e57b023e41f3 | 154 | printf("C,%f\n",C); |
takuma1 | 4:e57b023e41f3 | 155 | } |
takuma1 | 4:e57b023e41f3 | 156 | |
takuma1 | 4:e57b023e41f3 | 157 | } |
takuma1 | 4:e57b023e41f3 | 158 | // BMA_SUM |
takuma1 | 4:e57b023e41f3 | 159 | Replace1 = (int)(BAT_SUM / 100); |
takuma1 | 4:e57b023e41f3 | 160 | msgSlave1.data[0] = Replace1 / 100; |
takuma1 | 4:e57b023e41f3 | 161 | msgSlave1.data[1] = Replace1 % 100; |
takuma1 | 4:e57b023e41f3 | 162 | //BAT_MIN |
takuma1 | 4:e57b023e41f3 | 163 | Replace2 = (int)(BAT_MIN / 10); |
takuma1 | 4:e57b023e41f3 | 164 | msgSlave1.data[2] = Replace2 / 100; |
takuma1 | 4:e57b023e41f3 | 165 | msgSlave1.data[3] = Replace2 % 100; |
takuma1 | 4:e57b023e41f3 | 166 | //BAT_MAX |
takuma1 | 4:e57b023e41f3 | 167 | Replace3 = (int)(BAT_MAX /10); |
takuma1 | 4:e57b023e41f3 | 168 | msgSlave1.data[4] = Replace3 / 100; |
takuma1 | 4:e57b023e41f3 | 169 | msgSlave1.data[5] = Replace3 % 100; |
takuma1 | 4:e57b023e41f3 | 170 | //BAT_C |
takuma1 | 4:e57b023e41f3 | 171 | |
takuma1 | 4:e57b023e41f3 | 172 | //short BAT_C = -233; |
takuma1 | 4:e57b023e41f3 | 173 | if(BAT_C < 1000){ |
takuma1 | 4:e57b023e41f3 | 174 | BAT_C = BAT_C + 9002; |
takuma1 | 4:e57b023e41f3 | 175 | } |
takuma1 | 4:e57b023e41f3 | 176 | |
takuma1 | 4:e57b023e41f3 | 177 | if(BAT_C < 0){ //正電流か負電流を判定する |
takuma1 | 4:e57b023e41f3 | 178 | BAT_C = BAT_C + 9001; //負だと正の電流に変更する。さらにモニタ側でマイナスに戻すための定数を9001とする |
takuma1 | 4:e57b023e41f3 | 179 | //最大放電電流は50000mA 充電電流は-40000mAであるため9001とした。 |
takuma1 | 4:e57b023e41f3 | 180 | } |
takuma1 | 4:e57b023e41f3 | 181 | |
takuma1 | 4:e57b023e41f3 | 182 | Replace4 = (short int)(BAT_C / 1); |
takuma1 | 4:e57b023e41f3 | 183 | msgSlave1.data[6] = Replace4 / 100; |
takuma1 | 4:e57b023e41f3 | 184 | msgSlave1.data[7] = Replace4 % 100; |
takuma1 | 4:e57b023e41f3 | 185 | pc.printf("BAT_C %d\n",Replace4 ); |
takuma1 | 4:e57b023e41f3 | 186 | printf("Data in msgSlave1.data[6] : %d\n\r", msgSlave1.data[6]); |
takuma1 | 4:e57b023e41f3 | 187 | |
takuma1 | 4:e57b023e41f3 | 188 | |
takuma1 | 4:e57b023e41f3 | 189 | if(canSlave.write(msgSlave1)){ //格納したデータを送信する |
takuma1 | 4:e57b023e41f3 | 190 | led1 = !led1; |
takuma1 | 4:e57b023e41f3 | 191 | CANsendOK = 1; |
takuma1 | 4:e57b023e41f3 | 192 | } |
takuma1 | 4:e57b023e41f3 | 193 | ic_check(); |
takuma1 | 4:e57b023e41f3 | 194 | } |
takuma1 | 4:e57b023e41f3 | 195 | |
takuma1 | 4:e57b023e41f3 | 196 | void Handler_canRecieve(){ //canMasterから送信要求が来たとき,データ送信するための関数 |
takuma1 | 4:e57b023e41f3 | 197 | if( flag_can ){ |
takuma1 | 4:e57b023e41f3 | 198 | if( canSlave.read( msgSlave1 ) ){ //msgに送られたデータが入る |
takuma1 | 4:e57b023e41f3 | 199 | led2 = !led2; |
takuma1 | 4:e57b023e41f3 | 200 | if( msgSlave1.id == canSlaveID ){ //IDがcanSlaveIDであれば処理する |
takuma1 | 4:e57b023e41f3 | 201 | Handler_canSend(); |
takuma1 | 4:e57b023e41f3 | 202 | } |
takuma1 | 4:e57b023e41f3 | 203 | } |
takuma1 | 4:e57b023e41f3 | 204 | } |
takuma1 | 4:e57b023e41f3 | 205 | } |
APS_Lab | 0:f06ed53310a3 | 206 | int main(void) |
APS_Lab | 0:f06ed53310a3 | 207 | { |
APS_Lab | 0:f06ed53310a3 | 208 | uint32_t user_command; |
takuma1 | 4:e57b023e41f3 | 209 | //short int BAT_C = 2322; |
APS_Lab | 0:f06ed53310a3 | 210 | pc.baud(115200); |
takuma1 | 3:a1368cd4b0a9 | 211 | |
takuma1 | 4:e57b023e41f3 | 212 | printf("main()\n\r"); |
takuma1 | 4:e57b023e41f3 | 213 | canSlave.attach(&Handler_canRecieve, CAN::RxIrq); //CAN受信割り込みの設定 |
takuma1 | 4:e57b023e41f3 | 214 | msgSlave1.id = canSlaveID; //CAN送信側(slave)のIDを決定 |
takuma1 | 4:e57b023e41f3 | 215 | msgSlave1.len = 8; //CAN送信側で送るデータのバイト数 |
takuma1 | 3:a1368cd4b0a9 | 216 | |
takuma1 | 4:e57b023e41f3 | 217 | while(1) { |
takuma1 | 4:e57b023e41f3 | 218 | BAT_MIN = 0; |
takuma1 | 4:e57b023e41f3 | 219 | BAT_MAX = 0; // 0にする |
takuma1 | 4:e57b023e41f3 | 220 | /* |
takuma1 | 4:e57b023e41f3 | 221 | if ( fp == NULL ) |
takuma1 | 4:e57b023e41f3 | 222 | { |
takuma1 | 4:e57b023e41f3 | 223 | pc.printf("USB fileopen!\r\n"); |
takuma1 | 4:e57b023e41f3 | 224 | exit(1); |
takuma1 | 4:e57b023e41f3 | 225 | } |
takuma1 | 4:e57b023e41f3 | 226 | FILE *fp = fopen( "/usb/test.csv", "w"); //ファイルを開く "W"は新規作成して書き込みっていう命令? |
takuma1 | 4:e57b023e41f3 | 227 | */ |
takuma1 | 4:e57b023e41f3 | 228 | ic_check(); |
takuma1 | 4:e57b023e41f3 | 229 | //timer.start(); //書き込み時間測定開始 |
takuma1 | 4:e57b023e41f3 | 230 | //timer.stop(); //書き込み時間測定終了 |
takuma1 | 4:e57b023e41f3 | 231 | //fclose(fp); //ファイルを閉じる |
takuma1 | 4:e57b023e41f3 | 232 | if( CANsendOK ) { |
takuma1 | 4:e57b023e41f3 | 233 | CANsendOK = 0; |
takuma1 | 4:e57b023e41f3 | 234 | printf("Data in msgSlave1.data[0] : %d\n\r", msgSlave1.data[0]); //CANで送信したデータをそのまま表示 |
takuma1 | 4:e57b023e41f3 | 235 | printf("Data in msgSlave1.data[1] : %d\n\r", msgSlave1.data[1]); //上に同じ |
takuma1 | 4:e57b023e41f3 | 236 | printf("Data in msgSlave1.data[2] : %d\n\r", msgSlave1.data[2]); //上に同じ |
takuma1 | 4:e57b023e41f3 | 237 | printf("Data in msgSlave1.data[3] : %d\n\r", msgSlave1.data[3]); |
takuma1 | 4:e57b023e41f3 | 238 | printf("Data in msgSlave1.data[4] : %d\n\r", msgSlave1.data[4]); |
takuma1 | 4:e57b023e41f3 | 239 | printf("Data in msgSlave1.data[5] : %d\n\r", msgSlave1.data[5]); |
takuma1 | 4:e57b023e41f3 | 240 | printf("Data in msgSlave1.data[6] : %d\n\r", msgSlave1.data[6]); |
takuma1 | 4:e57b023e41f3 | 241 | printf("Data in msgSlave1.data[7] : %d\n\r", msgSlave1.data[7]); //上に同じ |
takuma1 | 4:e57b023e41f3 | 242 | printf("\n\r"); |
takuma1 | 4:e57b023e41f3 | 243 | } |
takuma1 | 4:e57b023e41f3 | 244 | BAT_safty(); |
takuma1 | 3:a1368cd4b0a9 | 245 | } |
takuma1 | 3:a1368cd4b0a9 | 246 | } |
takuma1 | 4:e57b023e41f3 | 247 | |
takuma1 | 4:e57b023e41f3 | 248 | /* |
takuma1 | 4:e57b023e41f3 | 249 | void ic_set(){ |
takuma1 | 4:e57b023e41f3 | 250 | |
takuma1 | 4:e57b023e41f3 | 251 | // __disable_irq(); |
takuma1 | 4:e57b023e41f3 | 252 | // flag_can = 0; |
takuma1 | 4:e57b023e41f3 | 253 | ic_check(); |
takuma1 | 4:e57b023e41f3 | 254 | spi_check(); |
takuma1 | 4:e57b023e41f3 | 255 | spi_error(); |
takuma1 | 4:e57b023e41f3 | 256 | wakeup(); |
takuma1 | 4:e57b023e41f3 | 257 | cell_read(); |
takuma1 | 4:e57b023e41f3 | 258 | } |
takuma1 | 4:e57b023e41f3 | 259 | */ |
takuma1 | 3:a1368cd4b0a9 | 260 | void ic_check(){ //1 |
takuma1 | 3:a1368cd4b0a9 | 261 | |
takuma1 | 4:e57b023e41f3 | 262 | //__disable_irq(); |
takuma1 | 4:e57b023e41f3 | 263 | |
APS_Lab | 0:f06ed53310a3 | 264 | spi_enable(); |
APS_Lab | 0:f06ed53310a3 | 265 | LTC681x_init_cfg(TOTAL_IC, bms_ic); |
APS_Lab | 0:f06ed53310a3 | 266 | LTC6811_reset_crc_count(TOTAL_IC,bms_ic); |
APS_Lab | 0:f06ed53310a3 | 267 | LTC6811_init_reg_limits(TOTAL_IC,bms_ic); |
takuma1 | 2:f6f76dde7e1d | 268 | wakeup_sleep(TOTAL_IC); |
takuma1 | 2:f6f76dde7e1d | 269 | LTC6811_wrcfg(TOTAL_IC,bms_ic); |
takuma1 | 2:f6f76dde7e1d | 270 | print_config(); |
takuma1 | 4:e57b023e41f3 | 271 | spi_check(); |
takuma1 | 4:e57b023e41f3 | 272 | } |
takuma1 | 4:e57b023e41f3 | 273 | |
takuma1 | 3:a1368cd4b0a9 | 274 | |
takuma1 | 3:a1368cd4b0a9 | 275 | void spi_check(){ //2 |
takuma1 | 4:e57b023e41f3 | 276 | |
takuma1 | 4:e57b023e41f3 | 277 | //__disable_irq(); |
takuma1 | 3:a1368cd4b0a9 | 278 | int countup; |
takuma1 | 3:a1368cd4b0a9 | 279 | int8_t error = 0; |
takuma1 | 3:a1368cd4b0a9 | 280 | |
takuma1 | 2:f6f76dde7e1d | 281 | wakeup_sleep(TOTAL_IC); |
takuma1 | 2:f6f76dde7e1d | 282 | error = LTC6811_rdcfg(TOTAL_IC,bms_ic); |
takuma1 | 2:f6f76dde7e1d | 283 | check_error(error); |
takuma1 | 4:e57b023e41f3 | 284 | print_rxconfig(); |
takuma1 | 3:a1368cd4b0a9 | 285 | spi_error(); |
takuma1 | 3:a1368cd4b0a9 | 286 | } |
takuma1 | 3:a1368cd4b0a9 | 287 | |
takuma1 | 4:e57b023e41f3 | 288 | void wakeup(){ //3 |
takuma1 | 3:a1368cd4b0a9 | 289 | |
takuma1 | 4:e57b023e41f3 | 290 | //__disable_irq(); |
takuma1 | 3:a1368cd4b0a9 | 291 | int countup; |
takuma1 | 3:a1368cd4b0a9 | 292 | int8_t error = 0; |
takuma1 | 3:a1368cd4b0a9 | 293 | uint32_t conv_time = 0; |
takuma1 | 2:f6f76dde7e1d | 294 | |
takuma1 | 3:a1368cd4b0a9 | 295 | |
takuma1 | 3:a1368cd4b0a9 | 296 | wakeup_sleep(TOTAL_IC); |
takuma1 | 2:f6f76dde7e1d | 297 | LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT); |
takuma1 | 2:f6f76dde7e1d | 298 | conv_time = LTC6811_pollAdc(); |
takuma1 | 3:a1368cd4b0a9 | 299 | cell_read(); |
takuma1 | 4:e57b023e41f3 | 300 | } |
takuma1 | 2:f6f76dde7e1d | 301 | |
takuma1 | 2:f6f76dde7e1d | 302 | |
takuma1 | 2:f6f76dde7e1d | 303 | void spi_error(){ |
takuma1 | 4:e57b023e41f3 | 304 | |
takuma1 | 4:e57b023e41f3 | 305 | // __disable_irq(); |
takuma1 | 2:f6f76dde7e1d | 306 | |
takuma1 | 2:f6f76dde7e1d | 307 | int8_t error = 0; |
takuma1 | 2:f6f76dde7e1d | 308 | int countup; |
takuma1 | 3:a1368cd4b0a9 | 309 | /* |
takuma1 | 2:f6f76dde7e1d | 310 | if(error = -1 ){ |
takuma1 | 3:a1368cd4b0a9 | 311 | for(countup = 0; countup <= 2; countup++){ |
takuma1 | 3:a1368cd4b0a9 | 312 | spi_check(); |
takuma1 | 3:a1368cd4b0a9 | 313 | if(error =1){ |
takuma1 | 2:f6f76dde7e1d | 314 | break; |
takuma1 | 3:a1368cd4b0a9 | 315 | } |
takuma1 | 3:a1368cd4b0a9 | 316 | } |
takuma1 | 2:f6f76dde7e1d | 317 | } |
takuma1 | 3:a1368cd4b0a9 | 318 | if(error = 1){ |
takuma1 | 3:a1368cd4b0a9 | 319 | wakeup(); |
takuma1 | 2:f6f76dde7e1d | 320 | } |
takuma1 | 3:a1368cd4b0a9 | 321 | */ |
takuma1 | 3:a1368cd4b0a9 | 322 | wakeup(); |
takuma1 | 2:f6f76dde7e1d | 323 | } |
takuma1 | 3:a1368cd4b0a9 | 324 | |
takuma1 | 3:a1368cd4b0a9 | 325 | void cell_read(){ //4 電圧読み取り |
APS_Lab | 0:f06ed53310a3 | 326 | |
takuma1 | 4:e57b023e41f3 | 327 | //__disable_irq(); |
APS_Lab | 0:f06ed53310a3 | 328 | int8_t error = 0; |
APS_Lab | 0:f06ed53310a3 | 329 | uint32_t conv_time = 0; |
APS_Lab | 0:f06ed53310a3 | 330 | int8_t readIC=0; |
takuma1 | 2:f6f76dde7e1d | 331 | |
takuma1 | 3:a1368cd4b0a9 | 332 | wakeup_sleep(TOTAL_IC); |
takuma1 | 3:a1368cd4b0a9 | 333 | error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); |
APS_Lab | 0:f06ed53310a3 | 334 | check_error(error); |
takuma1 | 3:a1368cd4b0a9 | 335 | //print_cells(DATALOG_DISABLED); |
takuma1 | 4:e57b023e41f3 | 336 | print_CAN(DATALOG_DISABLED); |
takuma1 | 3:a1368cd4b0a9 | 337 | } |
APS_Lab | 0:f06ed53310a3 | 338 | |
takuma1 | 3:a1368cd4b0a9 | 339 | void print_CAN(uint8_t datalog_en){ |
takuma1 | 4:e57b023e41f3 | 340 | /* |
takuma1 | 4:e57b023e41f3 | 341 | __disable_irq(); |
takuma1 | 3:a1368cd4b0a9 | 342 | short n =0; |
takuma1 | 4:e57b023e41f3 | 343 | int BAT_SUM = 0; // 0にする |
takuma1 | 4:e57b023e41f3 | 344 | int BAT_MIN = 55876; |
takuma1 | 4:e57b023e41f3 | 345 | double C; |
takuma1 | 3:a1368cd4b0a9 | 346 | //unsigned short BTA_MAX = 0; |
takuma1 | 4:e57b023e41f3 | 347 | |
takuma1 | 4:e57b023e41f3 | 348 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
takuma1 | 4:e57b023e41f3 | 349 | for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) { |
takuma1 | 4:e57b023e41f3 | 350 | |
takuma1 | 4:e57b023e41f3 | 351 | //pc.printf("C%d:", i+1); |
takuma1 | 4:e57b023e41f3 | 352 | //pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); |
takuma1 | 4:e57b023e41f3 | 353 | BAT_SUM = bms_ic[current_ic].cells.c_codes[i] + BAT_SUM; |
takuma1 | 4:e57b023e41f3 | 354 | if(bms_ic[current_ic].cells.c_codes[i] > 10000){ |
takuma1 | 4:e57b023e41f3 | 355 | if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){ |
takuma1 | 4:e57b023e41f3 | 356 | BAT_MIN = bms_ic[current_ic].cells.c_codes[i]; |
takuma1 | 4:e57b023e41f3 | 357 | } |
takuma1 | 4:e57b023e41f3 | 358 | } |
takuma1 | 4:e57b023e41f3 | 359 | |
takuma1 | 4:e57b023e41f3 | 360 | if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){ |
takuma1 | 4:e57b023e41f3 | 361 | BAT_MAX = bms_ic[current_ic].cells.c_codes[i]; |
takuma1 | 4:e57b023e41f3 | 362 | } |
takuma1 | 4:e57b023e41f3 | 363 | |
takuma1 | 4:e57b023e41f3 | 364 | if(VCmonitor.getCurrent(&C) == 0){ // INA226 |
takuma1 | 4:e57b023e41f3 | 365 | BAT_C = C; |
takuma1 | 4:e57b023e41f3 | 366 | //printf("C,%f\n",C); |
takuma1 | 4:e57b023e41f3 | 367 | } |
takuma1 | 4:e57b023e41f3 | 368 | } |
takuma1 | 4:e57b023e41f3 | 369 | } |
takuma1 | 4:e57b023e41f3 | 370 | /* |
takuma1 | 3:a1368cd4b0a9 | 371 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
takuma1 | 3:a1368cd4b0a9 | 372 | |
takuma1 | 3:a1368cd4b0a9 | 373 | //if (datalog_en == 0) { |
takuma1 | 4:e57b023e41f3 | 374 | //pc.printf("IC%d, ", current_ic+1); |
takuma1 | 3:a1368cd4b0a9 | 375 | //for(n = 0; n <= 12; n++){ |
takuma1 | 3:a1368cd4b0a9 | 376 | for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) { |
takuma1 | 3:a1368cd4b0a9 | 377 | |
takuma1 | 4:e57b023e41f3 | 378 | //BAT_CELL= bms_ic[current_ic].cells.c_codes[i]; |
takuma1 | 4:e57b023e41f3 | 379 | |
takuma1 | 4:e57b023e41f3 | 380 | //fprintf(fp,"C%d:", i+1); |
takuma1 | 4:e57b023e41f3 | 381 | //fprintf(fp,"%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); |
takuma1 | 3:a1368cd4b0a9 | 382 | BAT_SUM = bms_ic[current_ic].cells.c_codes[i] + BAT_SUM; |
takuma1 | 3:a1368cd4b0a9 | 383 | |
takuma1 | 3:a1368cd4b0a9 | 384 | if(bms_ic[current_ic].cells.c_codes[i] > 10000){ |
takuma1 | 3:a1368cd4b0a9 | 385 | if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){ |
takuma1 | 3:a1368cd4b0a9 | 386 | BAT_MIN = bms_ic[current_ic].cells.c_codes[i]; |
takuma1 | 3:a1368cd4b0a9 | 387 | } |
takuma1 | 3:a1368cd4b0a9 | 388 | } |
takuma1 | 3:a1368cd4b0a9 | 389 | //unsigned short BTA_MAX = 0; |
takuma1 | 3:a1368cd4b0a9 | 390 | if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){ |
takuma1 | 3:a1368cd4b0a9 | 391 | BAT_MAX = bms_ic[current_ic].cells.c_codes[i]; |
takuma1 | 4:e57b023e41f3 | 392 | } |
takuma1 | 4:e57b023e41f3 | 393 | if(VCmonitor.getCurrent(&C) == 0){ // INA226 |
takuma1 | 4:e57b023e41f3 | 394 | BAT_C = C; |
takuma1 | 4:e57b023e41f3 | 395 | } |
takuma1 | 4:e57b023e41f3 | 396 | |
takuma1 | 4:e57b023e41f3 | 397 | } |
takuma1 | 4:e57b023e41f3 | 398 | } |
takuma1 | 4:e57b023e41f3 | 399 | |
takuma1 | 4:e57b023e41f3 | 400 | fprintf(fp,"SUM"); |
takuma1 | 4:e57b023e41f3 | 401 | fprintf(fp,"%0.4f, ",BAT_SUM*0.0001); |
takuma1 | 4:e57b023e41f3 | 402 | BAT_AVG = BAT_SUM / 20; |
takuma1 | 4:e57b023e41f3 | 403 | fprintf(fp,"AVG"); |
takuma1 | 4:e57b023e41f3 | 404 | fprintf(fp,"%.4f, ",BAT_AVG*0.0001); |
takuma1 | 4:e57b023e41f3 | 405 | fprintf(fp,"MIN"); |
takuma1 | 4:e57b023e41f3 | 406 | fprintf(fp,"%.4f, ",BAT_MIN*0.0001); |
takuma1 | 4:e57b023e41f3 | 407 | fprintf(fp,"MAX"); |
takuma1 | 4:e57b023e41f3 | 408 | fprintf(fp,"%.4f, ",BAT_MAX*0.0001); |
takuma1 | 4:e57b023e41f3 | 409 | fprintf(fp,"BAT_Current,%f\n",BAT_Current); |
takuma1 | 4:e57b023e41f3 | 410 | */ |
APS_Lab | 0:f06ed53310a3 | 411 | } |
takuma1 | 4:e57b023e41f3 | 412 | |
takuma1 | 4:e57b023e41f3 | 413 | void BAT_safty(){ |
takuma1 | 4:e57b023e41f3 | 414 | if( BAT_MIN < 25000 ){ |
takuma1 | 4:e57b023e41f3 | 415 | BAT_MIN_safty = 1 ; |
takuma1 | 4:e57b023e41f3 | 416 | wait(1); |
takuma1 | 4:e57b023e41f3 | 417 | BAT_MIN_safty = 0 ; |
takuma1 | 4:e57b023e41f3 | 418 | } |
takuma1 | 4:e57b023e41f3 | 419 | if( BAT_MAX > 42000){ |
takuma1 | 4:e57b023e41f3 | 420 | BAT_MAX_safty = 1; |
takuma1 | 4:e57b023e41f3 | 421 | wait(1); |
takuma1 | 4:e57b023e41f3 | 422 | BAT_MAX_safty = 0; |
takuma1 | 4:e57b023e41f3 | 423 | } |
takuma1 | 4:e57b023e41f3 | 424 | if(BAT_C > 60000){ |
takuma1 | 4:e57b023e41f3 | 425 | BAT_MIN_safty = 1 ; |
takuma1 | 4:e57b023e41f3 | 426 | wait(1); |
takuma1 | 4:e57b023e41f3 | 427 | BAT_MIN_safty = 0 ; |
takuma1 | 4:e57b023e41f3 | 428 | } |
takuma1 | 4:e57b023e41f3 | 429 | } |
takuma1 | 3:a1368cd4b0a9 | 430 | |
takuma1 | 4:e57b023e41f3 | 431 | |
APS_Lab | 0:f06ed53310a3 | 432 | void measurement_loop(uint8_t datalog_en) |
APS_Lab | 0:f06ed53310a3 | 433 | { |
APS_Lab | 0:f06ed53310a3 | 434 | int8_t error = 0; |
APS_Lab | 0:f06ed53310a3 | 435 | if (WRITE_CONFIG == ENABLED) { |
APS_Lab | 0:f06ed53310a3 | 436 | wakeup_sleep(TOTAL_IC); |
APS_Lab | 0:f06ed53310a3 | 437 | LTC6811_wrcfg(TOTAL_IC,bms_ic); |
APS_Lab | 0:f06ed53310a3 | 438 | print_config(); |
APS_Lab | 0:f06ed53310a3 | 439 | } |
APS_Lab | 0:f06ed53310a3 | 440 | |
APS_Lab | 0:f06ed53310a3 | 441 | if (READ_CONFIG == ENABLED) { |
APS_Lab | 0:f06ed53310a3 | 442 | wakeup_sleep(TOTAL_IC); |
APS_Lab | 0:f06ed53310a3 | 443 | error = LTC6811_rdcfg(TOTAL_IC,bms_ic); |
APS_Lab | 0:f06ed53310a3 | 444 | check_error(error); |
APS_Lab | 0:f06ed53310a3 | 445 | print_rxconfig(); |
APS_Lab | 0:f06ed53310a3 | 446 | } |
APS_Lab | 0:f06ed53310a3 | 447 | |
APS_Lab | 0:f06ed53310a3 | 448 | if (MEASURE_CELL == ENABLED) { |
APS_Lab | 0:f06ed53310a3 | 449 | wakeup_idle(TOTAL_IC); |
APS_Lab | 0:f06ed53310a3 | 450 | LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT); |
APS_Lab | 0:f06ed53310a3 | 451 | LTC6811_pollAdc(); |
APS_Lab | 0:f06ed53310a3 | 452 | wakeup_idle(TOTAL_IC); |
APS_Lab | 0:f06ed53310a3 | 453 | error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); |
APS_Lab | 0:f06ed53310a3 | 454 | check_error(error); |
APS_Lab | 0:f06ed53310a3 | 455 | print_cells(datalog_en); |
APS_Lab | 0:f06ed53310a3 | 456 | |
APS_Lab | 0:f06ed53310a3 | 457 | } |
APS_Lab | 0:f06ed53310a3 | 458 | |
APS_Lab | 0:f06ed53310a3 | 459 | if (MEASURE_AUX == ENABLED) { |
APS_Lab | 0:f06ed53310a3 | 460 | wakeup_idle(TOTAL_IC); |
APS_Lab | 0:f06ed53310a3 | 461 | LTC6811_adax(ADC_CONVERSION_MODE , AUX_CH_ALL); |
APS_Lab | 0:f06ed53310a3 | 462 | LTC6811_pollAdc(); |
APS_Lab | 0:f06ed53310a3 | 463 | wakeup_idle(TOTAL_IC); |
APS_Lab | 0:f06ed53310a3 | 464 | error = LTC6811_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers |
APS_Lab | 0:f06ed53310a3 | 465 | check_error(error); |
APS_Lab | 0:f06ed53310a3 | 466 | print_aux(datalog_en); |
APS_Lab | 0:f06ed53310a3 | 467 | } |
APS_Lab | 0:f06ed53310a3 | 468 | |
APS_Lab | 0:f06ed53310a3 | 469 | if (MEASURE_STAT == ENABLED) { |
APS_Lab | 0:f06ed53310a3 | 470 | wakeup_idle(TOTAL_IC); |
APS_Lab | 0:f06ed53310a3 | 471 | LTC6811_adstat(ADC_CONVERSION_MODE, STAT_CH_ALL); |
APS_Lab | 0:f06ed53310a3 | 472 | LTC6811_pollAdc(); |
APS_Lab | 0:f06ed53310a3 | 473 | wakeup_idle(TOTAL_IC); |
APS_Lab | 0:f06ed53310a3 | 474 | error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers |
APS_Lab | 0:f06ed53310a3 | 475 | check_error(error); |
APS_Lab | 0:f06ed53310a3 | 476 | print_stat(); |
APS_Lab | 0:f06ed53310a3 | 477 | } |
APS_Lab | 0:f06ed53310a3 | 478 | |
APS_Lab | 0:f06ed53310a3 | 479 | if (PRINT_PEC == ENABLED) { |
APS_Lab | 0:f06ed53310a3 | 480 | print_pec(); |
APS_Lab | 0:f06ed53310a3 | 481 | } |
APS_Lab | 0:f06ed53310a3 | 482 | |
APS_Lab | 0:f06ed53310a3 | 483 | } |
APS_Lab | 0:f06ed53310a3 | 484 | |
takuma1 | 4:e57b023e41f3 | 485 | |
takuma1 | 3:a1368cd4b0a9 | 486 | |
APS_Lab | 0:f06ed53310a3 | 487 | void print_cells(uint8_t datalog_en) |
APS_Lab | 0:f06ed53310a3 | 488 | { |
APS_Lab | 0:f06ed53310a3 | 489 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
APS_Lab | 0:f06ed53310a3 | 490 | |
APS_Lab | 0:f06ed53310a3 | 491 | if (datalog_en == 0) { |
APS_Lab | 0:f06ed53310a3 | 492 | pc.printf("IC%d, ", current_ic+1); |
takuma1 | 3:a1368cd4b0a9 | 493 | for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) { |
APS_Lab | 0:f06ed53310a3 | 494 | pc.printf("C%d:", i+1); |
APS_Lab | 0:f06ed53310a3 | 495 | pc.printf("%.4f, ", bms_ic[current_ic].cells.c_codes[i]*0.0001); |
takuma1 | 3:a1368cd4b0a9 | 496 | } |
takuma1 | 3:a1368cd4b0a9 | 497 | |
APS_Lab | 0:f06ed53310a3 | 498 | pc.printf("\n"); |
takuma1 | 3:a1368cd4b0a9 | 499 | } |
takuma1 | 3:a1368cd4b0a9 | 500 | |
takuma1 | 3:a1368cd4b0a9 | 501 | else { |
APS_Lab | 0:f06ed53310a3 | 502 | pc.printf("Cells, "); |
APS_Lab | 0:f06ed53310a3 | 503 | for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) { |
takuma1 | 3:a1368cd4b0a9 | 504 | pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); |
takuma1 | 3:a1368cd4b0a9 | 505 | |
APS_Lab | 0:f06ed53310a3 | 506 | } |
APS_Lab | 0:f06ed53310a3 | 507 | } |
takuma1 | 3:a1368cd4b0a9 | 508 | |
APS_Lab | 0:f06ed53310a3 | 509 | } |
APS_Lab | 0:f06ed53310a3 | 510 | pc.printf("\n"); |
APS_Lab | 0:f06ed53310a3 | 511 | } |
APS_Lab | 0:f06ed53310a3 | 512 | |
APS_Lab | 0:f06ed53310a3 | 513 | void print_open() |
APS_Lab | 0:f06ed53310a3 | 514 | { |
APS_Lab | 0:f06ed53310a3 | 515 | for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) { |
APS_Lab | 0:f06ed53310a3 | 516 | if (bms_ic[current_ic].system_open_wire == 0) { |
APS_Lab | 0:f06ed53310a3 | 517 | pc.printf("No Opens Detected on IC%d\n", current_ic+1); |
APS_Lab | 0:f06ed53310a3 | 518 | } else { |
APS_Lab | 0:f06ed53310a3 | 519 | for (int cell=0; cell<bms_ic[0].ic_reg.cell_channels+1; cell++) { |
APS_Lab | 0:f06ed53310a3 | 520 | if ((bms_ic[current_ic].system_open_wire &(1<<cell))>0) { |
APS_Lab | 0:f06ed53310a3 | 521 | pc.printf("There is an open wire on IC%d Channel: %d\n", current_ic + 1, cell); |
APS_Lab | 0:f06ed53310a3 | 522 | } |
APS_Lab | 0:f06ed53310a3 | 523 | } |
APS_Lab | 0:f06ed53310a3 | 524 | } |
APS_Lab | 0:f06ed53310a3 | 525 | } |
APS_Lab | 0:f06ed53310a3 | 526 | } |
APS_Lab | 0:f06ed53310a3 | 527 | |
takuma1 | 4:e57b023e41f3 | 528 | |
APS_Lab | 0:f06ed53310a3 | 529 | void print_aux(uint8_t datalog_en) |
APS_Lab | 0:f06ed53310a3 | 530 | { |
APS_Lab | 0:f06ed53310a3 | 531 | |
APS_Lab | 0:f06ed53310a3 | 532 | for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) { |
APS_Lab | 0:f06ed53310a3 | 533 | if (datalog_en == 0) { |
APS_Lab | 0:f06ed53310a3 | 534 | pc.printf(" IC%d", current_ic+1); |
APS_Lab | 0:f06ed53310a3 | 535 | for (int i=0; i < 5; i++) { |
APS_Lab | 0:f06ed53310a3 | 536 | pc.printf(" GPIO-%d:%.4f,", i+1, bms_ic[current_ic].aux.a_codes[i]*0.0001); |
APS_Lab | 0:f06ed53310a3 | 537 | } |
APS_Lab | 0:f06ed53310a3 | 538 | pc.printf("Vref2:%.4f\n", bms_ic[current_ic].aux.a_codes[5]*0.0001); |
APS_Lab | 0:f06ed53310a3 | 539 | } else { |
APS_Lab | 0:f06ed53310a3 | 540 | pc.printf("AUX, "); |
APS_Lab | 0:f06ed53310a3 | 541 | for (int i=0; i < 6; i++) { |
APS_Lab | 0:f06ed53310a3 | 542 | pc.printf("%.4f,", bms_ic[current_ic].aux.a_codes[i]*0.0001); |
APS_Lab | 0:f06ed53310a3 | 543 | } |
APS_Lab | 0:f06ed53310a3 | 544 | } |
APS_Lab | 0:f06ed53310a3 | 545 | } |
APS_Lab | 0:f06ed53310a3 | 546 | pc.printf("\n"); |
APS_Lab | 0:f06ed53310a3 | 547 | } |
APS_Lab | 0:f06ed53310a3 | 548 | |
takuma1 | 4:e57b023e41f3 | 549 | |
APS_Lab | 0:f06ed53310a3 | 550 | void print_stat() |
APS_Lab | 0:f06ed53310a3 | 551 | { |
APS_Lab | 0:f06ed53310a3 | 552 | |
APS_Lab | 0:f06ed53310a3 | 553 | for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) { |
APS_Lab | 0:f06ed53310a3 | 554 | pc.printf("IC%d", current_ic+1); |
APS_Lab | 0:f06ed53310a3 | 555 | pc.printf(" SOC:%.4f,", bms_ic[current_ic].stat.stat_codes[0]*0.0001*20); |
APS_Lab | 0:f06ed53310a3 | 556 | pc.printf(" Itemp:%.4f,", bms_ic[current_ic].stat.stat_codes[1]*0.0001); |
APS_Lab | 0:f06ed53310a3 | 557 | pc.printf(" VregA:%.4f,", bms_ic[current_ic].stat.stat_codes[2]*0.0001); |
APS_Lab | 0:f06ed53310a3 | 558 | pc.printf(" VregD:%.4f\n", bms_ic[current_ic].stat.stat_codes[3]*0.0001); |
APS_Lab | 0:f06ed53310a3 | 559 | } |
APS_Lab | 0:f06ed53310a3 | 560 | |
APS_Lab | 0:f06ed53310a3 | 561 | pc.printf("\n"); |
APS_Lab | 0:f06ed53310a3 | 562 | } |
APS_Lab | 0:f06ed53310a3 | 563 | |
takuma1 | 4:e57b023e41f3 | 564 | |
APS_Lab | 0:f06ed53310a3 | 565 | void print_config() |
APS_Lab | 0:f06ed53310a3 | 566 | { |
APS_Lab | 0:f06ed53310a3 | 567 | int cfg_pec; |
APS_Lab | 0:f06ed53310a3 | 568 | |
takuma1 | 3:a1368cd4b0a9 | 569 | //pc.printf("Written Configuration: \n"); |
APS_Lab | 0:f06ed53310a3 | 570 | for (int current_ic = 0; current_ic<TOTAL_IC; current_ic++) { |
takuma1 | 3:a1368cd4b0a9 | 571 | //pc.printf(" IC "); |
takuma1 | 3:a1368cd4b0a9 | 572 | //pc.printf("%d", current_ic+1); |
takuma1 | 3:a1368cd4b0a9 | 573 | // pc.printf(": "); |
takuma1 | 3:a1368cd4b0a9 | 574 | //pc.printf("0x"); |
APS_Lab | 0:f06ed53310a3 | 575 | serial_print_hex(bms_ic[current_ic].config.tx_data[0]); |
takuma1 | 3:a1368cd4b0a9 | 576 | // pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 577 | serial_print_hex(bms_ic[current_ic].config.tx_data[1]); |
takuma1 | 3:a1368cd4b0a9 | 578 | //pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 579 | serial_print_hex(bms_ic[current_ic].config.tx_data[2]); |
takuma1 | 3:a1368cd4b0a9 | 580 | //pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 581 | serial_print_hex(bms_ic[current_ic].config.tx_data[3]); |
takuma1 | 3:a1368cd4b0a9 | 582 | // pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 583 | serial_print_hex(bms_ic[current_ic].config.tx_data[4]); |
takuma1 | 3:a1368cd4b0a9 | 584 | // pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 585 | serial_print_hex(bms_ic[current_ic].config.tx_data[5]); |
takuma1 | 3:a1368cd4b0a9 | 586 | //pc.printf(", Calculated PEC: 0x"); |
APS_Lab | 0:f06ed53310a3 | 587 | cfg_pec = pec15_calc(6,&bms_ic[current_ic].config.tx_data[0]); |
APS_Lab | 0:f06ed53310a3 | 588 | serial_print_hex((uint8_t)(cfg_pec>>8)); |
takuma1 | 3:a1368cd4b0a9 | 589 | //pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 590 | serial_print_hex((uint8_t)(cfg_pec)); |
takuma1 | 3:a1368cd4b0a9 | 591 | // pc.printf("\n"); |
APS_Lab | 0:f06ed53310a3 | 592 | } |
takuma1 | 3:a1368cd4b0a9 | 593 | // pc.printf("\n"); |
APS_Lab | 0:f06ed53310a3 | 594 | } |
APS_Lab | 0:f06ed53310a3 | 595 | |
takuma1 | 4:e57b023e41f3 | 596 | |
APS_Lab | 0:f06ed53310a3 | 597 | void print_rxconfig() |
APS_Lab | 0:f06ed53310a3 | 598 | { |
takuma1 | 3:a1368cd4b0a9 | 599 | //pc.printf("Received Configuration "); |
APS_Lab | 0:f06ed53310a3 | 600 | for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) { |
takuma1 | 3:a1368cd4b0a9 | 601 | // pc.printf(" IC "); |
takuma1 | 3:a1368cd4b0a9 | 602 | // pc.printf("%d", current_ic+1); |
takuma1 | 3:a1368cd4b0a9 | 603 | //pc.printf(": 0x"); |
APS_Lab | 0:f06ed53310a3 | 604 | serial_print_hex(bms_ic[current_ic].config.rx_data[0]); |
takuma1 | 3:a1368cd4b0a9 | 605 | //pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 606 | serial_print_hex(bms_ic[current_ic].config.rx_data[1]); |
takuma1 | 3:a1368cd4b0a9 | 607 | //pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 608 | serial_print_hex(bms_ic[current_ic].config.rx_data[2]); |
takuma1 | 3:a1368cd4b0a9 | 609 | //pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 610 | serial_print_hex(bms_ic[current_ic].config.rx_data[3]); |
takuma1 | 3:a1368cd4b0a9 | 611 | //pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 612 | serial_print_hex(bms_ic[current_ic].config.rx_data[4]); |
takuma1 | 3:a1368cd4b0a9 | 613 | // pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 614 | serial_print_hex(bms_ic[current_ic].config.rx_data[5]); |
takuma1 | 3:a1368cd4b0a9 | 615 | //pc.printf(", Received PEC: 0x"); |
APS_Lab | 0:f06ed53310a3 | 616 | serial_print_hex(bms_ic[current_ic].config.rx_data[6]); |
takuma1 | 3:a1368cd4b0a9 | 617 | //pc.printf(", 0x"); |
APS_Lab | 0:f06ed53310a3 | 618 | serial_print_hex(bms_ic[current_ic].config.rx_data[7]); |
takuma1 | 3:a1368cd4b0a9 | 619 | // pc.printf("\n"); |
APS_Lab | 0:f06ed53310a3 | 620 | } |
APS_Lab | 0:f06ed53310a3 | 621 | pc.printf("\n"); |
APS_Lab | 0:f06ed53310a3 | 622 | } |
APS_Lab | 0:f06ed53310a3 | 623 | |
APS_Lab | 0:f06ed53310a3 | 624 | void print_pec() |
APS_Lab | 0:f06ed53310a3 | 625 | { |
APS_Lab | 0:f06ed53310a3 | 626 | for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) { |
APS_Lab | 0:f06ed53310a3 | 627 | pc.printf("\n%d", bms_ic[current_ic].crc_count.pec_count); |
APS_Lab | 0:f06ed53310a3 | 628 | pc.printf(" : PEC Errors Detected on IC"); |
APS_Lab | 0:f06ed53310a3 | 629 | pc.printf("%d\n", current_ic+1); |
APS_Lab | 0:f06ed53310a3 | 630 | } |
APS_Lab | 0:f06ed53310a3 | 631 | } |
APS_Lab | 0:f06ed53310a3 | 632 | |
APS_Lab | 0:f06ed53310a3 | 633 | |
APS_Lab | 0:f06ed53310a3 | 634 | void serial_print_hex(uint8_t data) |
APS_Lab | 0:f06ed53310a3 | 635 | { |
takuma1 | 3:a1368cd4b0a9 | 636 | /* |
APS_Lab | 0:f06ed53310a3 | 637 | if (data < 16) { |
takuma1 | 3:a1368cd4b0a9 | 638 | //pc.printf("0x0%X", data); |
APS_Lab | 0:f06ed53310a3 | 639 | } else |
takuma1 | 3:a1368cd4b0a9 | 640 | pc.printf("0x%X", data); |
takuma1 | 3:a1368cd4b0a9 | 641 | */ |
APS_Lab | 0:f06ed53310a3 | 642 | } |
APS_Lab | 0:f06ed53310a3 | 643 | |
APS_Lab | 0:f06ed53310a3 | 644 | //Function to check error flag and print PEC error message |
APS_Lab | 0:f06ed53310a3 | 645 | void check_error(int error) |
APS_Lab | 0:f06ed53310a3 | 646 | { |
APS_Lab | 0:f06ed53310a3 | 647 | if (error == -1) { |
APS_Lab | 0:f06ed53310a3 | 648 | pc.printf("A PEC error was detected in the received data"); |
APS_Lab | 0:f06ed53310a3 | 649 | } |
APS_Lab | 0:f06ed53310a3 | 650 | } |
APS_Lab | 0:f06ed53310a3 | 651 | |
APS_Lab | 0:f06ed53310a3 | 652 | |
takuma1 | 4:e57b023e41f3 | 653 | |
APS_Lab | 0:f06ed53310a3 | 654 | char hex_digits[16]= { |
APS_Lab | 0:f06ed53310a3 | 655 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
APS_Lab | 0:f06ed53310a3 | 656 | }; |
APS_Lab | 0:f06ed53310a3 | 657 | |
takuma1 | 4:e57b023e41f3 | 658 | |
APS_Lab | 0:f06ed53310a3 | 659 | |
APS_Lab | 0:f06ed53310a3 | 660 | char hex_to_byte_buffer[5]= { |
APS_Lab | 0:f06ed53310a3 | 661 | '0', 'x', '0', '0', '\0' |
APS_Lab | 0:f06ed53310a3 | 662 | }; // buffer for ASCII hex to byte conversion |
APS_Lab | 0:f06ed53310a3 | 663 | char byte_to_hex_buffer[3]= { |
APS_Lab | 0:f06ed53310a3 | 664 | '\0','\0','\0' |
APS_Lab | 0:f06ed53310a3 | 665 | }; |
APS_Lab | 0:f06ed53310a3 | 666 | |
takuma1 | 2:f6f76dde7e1d | 667 | // シリアル インターフェイスから ui_バッファへのデータの読み取り |
APS_Lab | 0:f06ed53310a3 | 668 | uint8_t read_data() |
APS_Lab | 0:f06ed53310a3 | 669 | { |
takuma1 | 2:f6f76dde7e1d | 670 | uint8_t index = 0; // 内の現在の場所を保持するインデックス |
takuma1 | 2:f6f76dde7e1d | 671 | int c; // 着信キーストロークの格納に使用される単一の文字 |
APS_Lab | 0:f06ed53310a3 | 672 | //pc.printf("check 1\n"); |
APS_Lab | 0:f06ed53310a3 | 673 | while (index < UI_BUFFER_SIZE-1) { |
APS_Lab | 0:f06ed53310a3 | 674 | //pc.printf("check 2\n"); |
takuma1 | 4:e57b023e41f3 | 675 | c = pc.getc(); |
APS_Lab | 0:f06ed53310a3 | 676 | //return c; |
APS_Lab | 0:f06ed53310a3 | 677 | //pc.printf("check 3\n"); |
APS_Lab | 0:f06ed53310a3 | 678 | |
takuma1 | 4:e57b023e41f3 | 679 | if (((char) c == '\r') || ((char) c == '\n')) break; |
takuma1 | 4:e57b023e41f3 | 680 | if ( ((char) c == '\x7F') || ((char) c == '\x08') ) { |
APS_Lab | 0:f06ed53310a3 | 681 | if (index > 0) index--; |
APS_Lab | 0:f06ed53310a3 | 682 | } else if (c >= 0) { |
takuma1 | 4:e57b023e41f3 | 683 | ui_buffer[index++]=(char) c; |
APS_Lab | 0:f06ed53310a3 | 684 | } |
APS_Lab | 0:f06ed53310a3 | 685 | //pc.printf("check 4\n"); |
APS_Lab | 0:f06ed53310a3 | 686 | |
APS_Lab | 0:f06ed53310a3 | 687 | } |
takuma1 | 4:e57b023e41f3 | 688 | ui_buffer[index]='\0'; |
APS_Lab | 0:f06ed53310a3 | 689 | |
takuma1 | 4:e57b023e41f3 | 690 | if ((char) c == '\r') { |
APS_Lab | 0:f06ed53310a3 | 691 | wait_ms(1); |
APS_Lab | 0:f06ed53310a3 | 692 | //pc.printf("check 5\n"); |
APS_Lab | 0:f06ed53310a3 | 693 | |
APS_Lab | 0:f06ed53310a3 | 694 | if (pc.readable()==1) { |
APS_Lab | 0:f06ed53310a3 | 695 | //pc.printf("check 6\n"); |
takuma1 | 4:e57b023e41f3 | 696 | pc.getc(); |
APS_Lab | 0:f06ed53310a3 | 697 | } |
takuma1 | 4:e57b023e41f3 | 698 | |
APS_Lab | 0:f06ed53310a3 | 699 | |
APS_Lab | 0:f06ed53310a3 | 700 | } |
takuma1 | 4:e57b023e41f3 | 701 | |
APS_Lab | 0:f06ed53310a3 | 702 | |
takuma1 | 4:e57b023e41f3 | 703 | return index; |
APS_Lab | 0:f06ed53310a3 | 704 | } |
APS_Lab | 0:f06ed53310a3 | 705 | |
takuma1 | 4:e57b023e41f3 | 706 | |
APS_Lab | 0:f06ed53310a3 | 707 | float read_float() |
APS_Lab | 0:f06ed53310a3 | 708 | { |
APS_Lab | 0:f06ed53310a3 | 709 | float data; |
APS_Lab | 0:f06ed53310a3 | 710 | read_data(); |
APS_Lab | 0:f06ed53310a3 | 711 | data = atof(ui_buffer); |
APS_Lab | 0:f06ed53310a3 | 712 | return(data); |
APS_Lab | 0:f06ed53310a3 | 713 | } |
APS_Lab | 0:f06ed53310a3 | 714 | |
takuma1 | 4:e57b023e41f3 | 715 | |
APS_Lab | 0:f06ed53310a3 | 716 | int32_t read_int() |
APS_Lab | 0:f06ed53310a3 | 717 | { |
APS_Lab | 0:f06ed53310a3 | 718 | int32_t data; |
APS_Lab | 0:f06ed53310a3 | 719 | read_data(); |
APS_Lab | 0:f06ed53310a3 | 720 | if (ui_buffer[0] == 'm') |
APS_Lab | 0:f06ed53310a3 | 721 | return('m'); |
APS_Lab | 0:f06ed53310a3 | 722 | if ((ui_buffer[0] == 'B') || (ui_buffer[0] == 'b')) { |
APS_Lab | 0:f06ed53310a3 | 723 | data = strtol(ui_buffer+1, NULL, 2); |
APS_Lab | 0:f06ed53310a3 | 724 | } else |
APS_Lab | 0:f06ed53310a3 | 725 | data = strtol(ui_buffer, NULL, 0); |
APS_Lab | 0:f06ed53310a3 | 726 | return(data); |
APS_Lab | 0:f06ed53310a3 | 727 | } |
APS_Lab | 0:f06ed53310a3 | 728 | |
APS_Lab | 0:f06ed53310a3 | 729 | char *read_string() |
APS_Lab | 0:f06ed53310a3 | 730 | { |
APS_Lab | 0:f06ed53310a3 | 731 | read_data(); |
APS_Lab | 0:f06ed53310a3 | 732 | return(ui_buffer); |
APS_Lab | 0:f06ed53310a3 | 733 | } |
APS_Lab | 0:f06ed53310a3 | 734 | |
takuma1 | 4:e57b023e41f3 | 735 | |
APS_Lab | 0:f06ed53310a3 | 736 | int8_t read_char() |
APS_Lab | 0:f06ed53310a3 | 737 | { |
APS_Lab | 0:f06ed53310a3 | 738 | read_data(); |
APS_Lab | 0:f06ed53310a3 | 739 | return(ui_buffer[0]); |
APS_Lab | 0:f06ed53310a3 | 740 | } |