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.
Diff: main.cpp
- Revision:
- 4:e57b023e41f3
- Parent:
- 3:a1368cd4b0a9
- Child:
- 5:f07de56debf3
--- a/main.cpp Sat Nov 30 07:24:22 2019 +0000 +++ b/main.cpp Thu Apr 30 08:05:06 2020 +0000 @@ -3,6 +3,8 @@ #include "bms.h" #include "LTC681x.h" #include "LTC6811.h" +#include "INA226.hpp" +//#include "MSCFileSystem.h" #define UI_BUFFER_SIZE 64 #define SERIAL_TERMINATOR '\n' @@ -13,16 +15,30 @@ #define DATALOG_ENABLED 1 #define DATALOG_DISABLED 0 -Ticker ticker; + + DigitalOut led1(LED1); Serial pc(USBTX, USBRX); -CAN BMS_CAN(p9,p10); - +CAN canSlave(p30,p29); +I2C i2c(p28,p27); +DigitalOut led2(LED2); +DigitalOut BAT_MIN_safty(p21); +DigitalOut BAT_MAX_safty(p22); +//MSCFileSystem msc("usb"); // Mount flash drive under the name "msc" +//Timer timer; //書き込み時間を計測するタイマ -void run_command(uint32_t cmd); +// CAN関係 +short int forSend = 0; //データ送信時に一時的に使用する変数 +bool CANsendOK = 0; //CAN送信完了時,セットする(mainでのprintfのため) +CANMessage msgSlave1; //CAN送信用 +short int canSlaveID = 0x20; //canSlaveのIDをに設定 +void Handler_canSend(); +bool flag_can = 1; + +//測定関係の関数(変更の際触る必要がない) +void run_command(uint32_t cmd); //測定コマンド送信関数 void measurement_loop(uint8_t datalog_en); -//void print_menu(); -void print_cells(uint8_t datalog_en); +void print_cells(uint8_t datalog_en); //セル電圧の測定表示関数 void print_open(); void print_aux(uint8_t datalog_en); void print_stat(); @@ -31,117 +47,220 @@ void print_pec(void); void serial_print_hex(uint8_t data); void check_error(int error); -void wakeup(); -void cell_read(); -void spi_error(); -void ic_check(); -void spi_check(); -void print_CAN(uint8_t datalog_en); -void print_math(); -//char get_char(); -//void read_config_data(uint8_t cfg_data[][6], uint8_t nIC); +//測定関係関数(使ってる) +void wakeup();//ICの起動関数、消費電力削減関数 +void cell_read();//セルの電圧の読み込みコマンド +void spi_error();//SPIエラー処理関数 +void ic_check();//ICが測定可能状態か診断して次のコマンドに持ち込むための関数 +void spi_check();//SPIエラーの確認関数 +void print_CAN(uint8_t datalog_en);//セルの電圧の表示コマンドと過充電過放電検出 +void BAT_safty(); +void print_math(); +void can_sent1(); +void can_sent2(); +void can_sent3(); +void can_sent4(); +//void can_set(); +void can_wait(); +void ic_set(); -/********************************************************** - Setup Variables - The following variables can be modified to - configure the software. + -***********************************************************/ -const uint8_t TOTAL_IC = 2;//!<number of ICs in the daisy chain + +const uint8_t TOTAL_IC = 3; //IC数 char ui_buffer[UI_BUFFER_SIZE]; -//ADC Command Configurations -//const uint8_t ADC_OPT = ADC_OPT_DISABLED; // See LTC6811_daisy.h for Options -const uint8_t ADC_CONVERSION_MODE = MD_7KHZ_3KHZ;//MD_7KHZ_3KHZ; //MD_26HZ_2KHZ;//MD_7KHZ_3KHZ; // See LTC6811_daisy.h for Options -const uint8_t ADC_DCP = DCP_DISABLED; // See LTC6811_daisy.h for Options -const uint8_t CELL_CH_TO_CONVERT = CELL_CH_ALL; // See LTC6811_daisy.h for Options -const uint8_t AUX_CH_TO_CONVERT = AUX_CH_ALL; // See LTC6811_daisy.h for Options -const uint8_t STAT_CH_TO_CONVERT = STAT_CH_ALL; // See LTC6811_daisy.h for Options -const uint16_t MEASUREMENT_LOOP_TIME = 500;//milliseconds(mS) +const uint8_t ADC_CONVERSION_MODE = MD_7KHZ_3KHZ; +const uint8_t ADC_DCP = DCP_DISABLED; +const uint8_t CELL_CH_TO_CONVERT = CELL_CH_ALL; +const uint8_t AUX_CH_TO_CONVERT = AUX_CH_ALL; +const uint8_t STAT_CH_TO_CONVERT = STAT_CH_ALL; -//Under Voltage and Over Voltage Thresholds +const uint16_t MEASUREMENT_LOOP_TIME = 10;//milliseconds(mS) const uint16_t OV_THRESHOLD = 41000; // const uint16_t UV_THRESHOLD = 30000; // -//Loop Measurement Setup These Variables are ENABLED or DISABLED Remember ALL CAPS -const uint8_t WRITE_CONFIG = DISABLED; // This is ENABLED or DISABLED -const uint8_t READ_CONFIG = DISABLED; // This is ENABLED or DISABLED -const uint8_t MEASURE_CELL = ENABLED; // This is ENABLED or DISABLED -const uint8_t MEASURE_AUX = DISABLED; // This is ENABLED or DISABLED -const uint8_t MEASURE_STAT = DISABLED; //This is ENABLED or DISABLED -const uint8_t PRINT_PEC = DISABLED; //This is ENABLED or DISABLED + +const uint8_t WRITE_CONFIG = DISABLED; +const uint8_t READ_CONFIG = DISABLED; +const uint8_t MEASURE_CELL = ENABLED; +const uint8_t MEASURE_AUX = DISABLED; +const uint8_t MEASURE_STAT = DISABLED; +const uint8_t PRINT_PEC = DISABLED; short n =0; -// Read data from the serial interface into the ui_buffer buffer uint8_t read_data(); -// Read a float value from the serial interface float read_float(); +INA226 VCmonitor(i2c); // INA226 int32_t read_int(); -int kaisuu; -unsigned short BAT_MIN, BAT_MAX, BAT_AVG; -unsigned long BAT_SUM; +float BAT_MIN ; +float BAT_MAX ; +float BAT_AVG ; +double BAT_C; +float BAT_SUM; unsigned short BAT_CELL[23]; char *read_string(); - +/* +int Replace1; +int Replace2; +int Replace3; +int Replace4; +*/ int8_t read_char(); -/************************************ - END SETUP -*************************************/ - -/****************************************************** - *** Global Battery Variables received from 681x commands - These variables store the results from the LTC6811 - register reads and the array lengths must be based - on the number of ICs on the stack - ******************************************************/ - cell_asic bms_ic[TOTAL_IC]; + +void Handler_canSend() { + + int Replace1 = 0; + int Replace2 = 0; + int Replace3 = 0; + short int Replace4 = 0; + + short n =0; + int BAT_SUM = 0; // 0にする + int BAT_MIN = 55876; + double C; + //unsigned short BTA_MAX = 0; + + for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { + for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) { + + //pc.printf("C%d:", i+1); + pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); + BAT_SUM = bms_ic[current_ic].cells.c_codes[i] + BAT_SUM; + + if(bms_ic[current_ic].cells.c_codes[i] > 10000){ + if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){ + BAT_MIN = bms_ic[current_ic].cells.c_codes[i]; + } + } + + if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){ + BAT_MAX = bms_ic[current_ic].cells.c_codes[i]; + } + } + + if(VCmonitor.getCurrent(&C) == 0){ // INA226 + BAT_C = C; + printf("C,%f\n",C); + } + + } +// BMA_SUM + Replace1 = (int)(BAT_SUM / 100); + msgSlave1.data[0] = Replace1 / 100; + msgSlave1.data[1] = Replace1 % 100; +//BAT_MIN + Replace2 = (int)(BAT_MIN / 10); + msgSlave1.data[2] = Replace2 / 100; + msgSlave1.data[3] = Replace2 % 100; +//BAT_MAX + Replace3 = (int)(BAT_MAX /10); + msgSlave1.data[4] = Replace3 / 100; + msgSlave1.data[5] = Replace3 % 100; +//BAT_C + + //short BAT_C = -233; + if(BAT_C < 1000){ + BAT_C = BAT_C + 9002; + } + + if(BAT_C < 0){ //正電流か負電流を判定する + BAT_C = BAT_C + 9001; //負だと正の電流に変更する。さらにモニタ側でマイナスに戻すための定数を9001とする + //最大放電電流は50000mA 充電電流は-40000mAであるため9001とした。 + } + + Replace4 = (short int)(BAT_C / 1); + msgSlave1.data[6] = Replace4 / 100; + msgSlave1.data[7] = Replace4 % 100; + pc.printf("BAT_C %d\n",Replace4 ); + printf("Data in msgSlave1.data[6] : %d\n\r", msgSlave1.data[6]); + + + if(canSlave.write(msgSlave1)){ //格納したデータを送信する + led1 = !led1; + CANsendOK = 1; + } + ic_check(); +} + +void Handler_canRecieve(){ //canMasterから送信要求が来たとき,データ送信するための関数 + if( flag_can ){ + if( canSlave.read( msgSlave1 ) ){ //msgに送られたデータが入る + led2 = !led2; + if( msgSlave1.id == canSlaveID ){ //IDがcanSlaveIDであれば処理する + Handler_canSend(); + } + } + } +} int main(void) { uint32_t user_command; - + //short int BAT_C = 2322; pc.baud(115200); - //spi_enable(); - //LTC681x_init_cfg(TOTAL_IC, bms_ic); - //LTC6811_reset_crc_count(TOTAL_IC,bms_ic); - //LTC6811_init_reg_limits(TOTAL_IC,bms_ic); - //ic_check(); - //wakeup(); + printf("main()\n\r"); + canSlave.attach(&Handler_canRecieve, CAN::RxIrq); //CAN受信割り込みの設定 + msgSlave1.id = canSlaveID; //CAN送信側(slave)のIDを決定 + msgSlave1.len = 8; //CAN送信側で送るデータのバイト数 - //print_menu(); - - - while(1) { - - - unsigned short BAT_MIN = 0; - unsigned short BAT_MAX = 0; - //spi_error(); - //pc.printf("check 00\n"); - //while(!pc.readable()) { - //wait(0.3); - // pc.printf("check 001\n"); - //} // Check for user input - //pc.printf("check 01\n"); - //user_command = read_int(); - //run_command(user_command); - - ic_check(); + while(1) { + BAT_MIN = 0; + BAT_MAX = 0; // 0にする + /* + if ( fp == NULL ) + { + pc.printf("USB fileopen!\r\n"); + exit(1); + } + FILE *fp = fopen( "/usb/test.csv", "w"); //ファイルを開く "W"は新規作成して書き込みっていう命令? + */ + ic_check(); + //timer.start(); //書き込み時間測定開始 + //timer.stop(); //書き込み時間測定終了 + //fclose(fp); //ファイルを閉じる + if( CANsendOK ) { + CANsendOK = 0; + printf("Data in msgSlave1.data[0] : %d\n\r", msgSlave1.data[0]); //CANで送信したデータをそのまま表示 + printf("Data in msgSlave1.data[1] : %d\n\r", msgSlave1.data[1]); //上に同じ + printf("Data in msgSlave1.data[2] : %d\n\r", msgSlave1.data[2]); //上に同じ + printf("Data in msgSlave1.data[3] : %d\n\r", msgSlave1.data[3]); + printf("Data in msgSlave1.data[4] : %d\n\r", msgSlave1.data[4]); + printf("Data in msgSlave1.data[5] : %d\n\r", msgSlave1.data[5]); + printf("Data in msgSlave1.data[6] : %d\n\r", msgSlave1.data[6]); + printf("Data in msgSlave1.data[7] : %d\n\r", msgSlave1.data[7]); //上に同じ + printf("\n\r"); + } + BAT_safty(); } } + +/* +void ic_set(){ + + // __disable_irq(); +// flag_can = 0; + ic_check(); + spi_check(); + spi_error(); + wakeup(); + cell_read(); + } +*/ void ic_check(){ //1 - pc.baud(115200); +//__disable_irq(); + spi_enable(); LTC681x_init_cfg(TOTAL_IC, bms_ic); LTC6811_reset_crc_count(TOTAL_IC,bms_ic); @@ -149,24 +268,26 @@ wakeup_sleep(TOTAL_IC); LTC6811_wrcfg(TOTAL_IC,bms_ic); print_config(); - spi_check(); - - } + spi_check(); +} + void spi_check(){ //2 + +//__disable_irq(); int countup; int8_t error = 0; wakeup_sleep(TOTAL_IC); error = LTC6811_rdcfg(TOTAL_IC,bms_ic); check_error(error); - print_rxconfig(); + print_rxconfig(); spi_error(); - } +void wakeup(){ //3 -void wakeup(){ //3 + //__disable_irq(); int countup; int8_t error = 0; uint32_t conv_time = 0; @@ -175,14 +296,13 @@ wakeup_sleep(TOTAL_IC); LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT); conv_time = LTC6811_pollAdc(); - // pc.printf("cell conversion completed in:"); - //pc.printf("%.1f",((float)conv_time/1000)); - //pc.printf("mS\n"); cell_read(); - } + } void spi_error(){ + + // __disable_irq(); int8_t error = 0; int countup; @@ -199,14 +319,12 @@ wakeup(); } */ - wakeup(); - - } void cell_read(){ //4 電圧読み取り + //__disable_irq(); int8_t error = 0; uint32_t conv_time = 0; int8_t readIC=0; @@ -215,33 +333,52 @@ error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); check_error(error); //print_cells(DATALOG_DISABLED); - print_CAN(DATALOG_DISABLED); - - ic_check(); + print_CAN(DATALOG_DISABLED); } - - - void print_CAN(uint8_t datalog_en){ - + /* +__disable_irq(); short n =0; - long BAT_SUM = 0; - unsigned short BAT_MIN = 55000; + int BAT_SUM = 0; // 0にする + int BAT_MIN = 55876; + double C; //unsigned short BTA_MAX = 0; - + + for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { + for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) { + + //pc.printf("C%d:", i+1); + //pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); + BAT_SUM = bms_ic[current_ic].cells.c_codes[i] + BAT_SUM; + if(bms_ic[current_ic].cells.c_codes[i] > 10000){ + if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){ + BAT_MIN = bms_ic[current_ic].cells.c_codes[i]; + } + } + + if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){ + BAT_MAX = bms_ic[current_ic].cells.c_codes[i]; + } + + if(VCmonitor.getCurrent(&C) == 0){ // INA226 + BAT_C = C; + //printf("C,%f\n",C); + } + } + } + /* for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { //if (datalog_en == 0) { - pc.printf("IC%d, ", current_ic+1); + //pc.printf("IC%d, ", current_ic+1); //for(n = 0; n <= 12; n++){ for (int i=0; i < bms_ic[0].ic_reg.cell_channels; i++) { - BAT_CELL[n] = bms_ic[current_ic].cells.c_codes[i]; - - - pc.printf("C%d:", i+1); - pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); + //BAT_CELL= bms_ic[current_ic].cells.c_codes[i]; + + //fprintf(fp,"C%d:", i+1); + //fprintf(fp,"%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); BAT_SUM = bms_ic[current_ic].cells.c_codes[i] + BAT_SUM; if(bms_ic[current_ic].cells.c_codes[i] > 10000){ @@ -252,56 +389,46 @@ //unsigned short BTA_MAX = 0; if( bms_ic[current_ic].cells.c_codes[i] > BAT_MAX){ BAT_MAX = bms_ic[current_ic].cells.c_codes[i]; - } - - - - - - // BAT_SUM = BAT_CELL[n] + BAT_SUM; - // if(bms_ic[current_ic].cells.c_codes[i] < BAT_MIN){ - // BAT_MIN = BAT_CELL[n]; - // } + } + if(VCmonitor.getCurrent(&C) == 0){ // INA226 + BAT_C = C; + } + + } + } + + fprintf(fp,"SUM"); + fprintf(fp,"%0.4f, ",BAT_SUM*0.0001); + BAT_AVG = BAT_SUM / 20; + fprintf(fp,"AVG"); + fprintf(fp,"%.4f, ",BAT_AVG*0.0001); + fprintf(fp,"MIN"); + fprintf(fp,"%.4f, ",BAT_MIN*0.0001); + fprintf(fp,"MAX"); + fprintf(fp,"%.4f, ",BAT_MAX*0.0001); + fprintf(fp,"BAT_Current,%f\n",BAT_Current); + */ } - - - - - - //BAT_CELL[n] = bms_ic[current_ic].cells.c_codes[i]*0.0001; - // BAT_CELL[n] = BAT_CELL[n]*0.0001; - // pc.printf("C%d:", i+1); - // pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]); - // pc.printf("%d, ",BAT_CELL[n]); - //} - // } - - // else { - // pc.printf("Cells, "); - // for (int i=0; i<bms_ic[0].ic_reg.cell_channels; i++) { - // pc.printf("%.4f, ",bms_ic[current_ic].cells.c_codes[i]*0.0001); + +void BAT_safty(){ + if( BAT_MIN < 25000 ){ + BAT_MIN_safty = 1 ; + wait(1); + BAT_MIN_safty = 0 ; + } + if( BAT_MAX > 42000){ + BAT_MAX_safty = 1; + wait(1); + BAT_MAX_safty = 0; + } + if(BAT_C > 60000){ + BAT_MIN_safty = 1 ; + wait(1); + BAT_MIN_safty = 0 ; + } + } - //} - // } - } - - //print_math(); - pc.printf("\n"); - pc.printf("SUM"); - pc.printf("%f, ",BAT_SUM*0.0001); - BAT_AVG = BAT_SUM / 20; - pc.printf("AVG"); - pc.printf("%.4f, ",BAT_AVG*0.0001); - pc.printf("MIN"); - pc.printf("%.4f, ",BAT_MIN*0.0001); - pc.printf("MAX"); - pc.printf("%.4f, ",BAT_MAX*0.0001); - pc.printf("\n"); - pc.printf("\n"); - - } - - + void measurement_loop(uint8_t datalog_en) { int8_t error = 0; @@ -355,25 +482,7 @@ } -/* -void print_menu() -{ - pc.printf("Please enter LTC6811 Command\n"); - pc.printf("Write Configuration: 1 | Reset PEC Counter: 11\n"); - pc.printf("Read Configuration: 2 | Run ADC Self Test: 12\n"); - pc.printf("Start Cell Voltage Conversion: 3 | Set Discharge: 13\n"); - pc.printf("Read Cell Voltages: 4 | Clear Discharge: 14\n"); - pc.printf("Start Aux Voltage Conversion: 5 | Clear Registers: 15\n"); - pc.printf("Read Aux Voltages: 6 | Run Mux Self Test: 16\n"); - pc.printf("Start Stat Voltage Conversion: 7 | Run ADC overlap Test: 17\n"); - pc.printf("Read Stat Voltages: 8 | Run Digital Redundancy Test: 18\n"); - pc.printf("loop Measurements: 9 | Run Open Wire Test: 19\n"); - pc.printf("Read PEC Errors: 10 | Loop measurements with datalog output: 20\n"); - pc.printf("\n"); - pc.printf("Please enter command:\n"); - pc.printf("\n"); -} -*/ + void print_cells(uint8_t datalog_en) { @@ -401,9 +510,6 @@ pc.printf("\n"); } -/*!**************************************************************************** - \brief Prints Open wire test results to the serial port - *****************************************************************************/ void print_open() { for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++) { @@ -419,9 +525,7 @@ } } -/*!**************************************************************************** - \brief Prints GPIO voltage codes and Vref2 voltage code onto the serial port - *****************************************************************************/ + void print_aux(uint8_t datalog_en) { @@ -442,9 +546,7 @@ pc.printf("\n"); } -/*!**************************************************************************** - \brief Prints Status voltage codes and Vref2 voltage code onto the serial port - *****************************************************************************/ + void print_stat() { @@ -459,10 +561,7 @@ pc.printf("\n"); } -/*!****************************************************************************** - \brief Prints the configuration data that is going to be written to the LTC6811 - to the serial port. - ********************************************************************************/ + void print_config() { int cfg_pec; @@ -494,10 +593,7 @@ // pc.printf("\n"); } -/*!***************************************************************** - \brief Prints the configuration data that was read back from the - LTC6811 to the serial port. - *******************************************************************/ + void print_rxconfig() { //pc.printf("Received Configuration "); @@ -554,12 +650,12 @@ } -// hex conversion constants + char hex_digits[16]= { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; -// global variables + char hex_to_byte_buffer[5]= { '0', 'x', '0', '0', '\0' @@ -576,38 +672,38 @@ //pc.printf("check 1\n"); while (index < UI_BUFFER_SIZE-1) { //pc.printf("check 2\n"); - c = pc.getc(); //read one character + c = pc.getc(); //return c; //pc.printf("check 3\n"); - if (((char) c == '\r') || ((char) c == '\n')) break; // キャリッジ リターンまたはラインフィードの場合は、データを停止して返します。 - if ( ((char) c == '\x7F') || ((char) c == '\x08') ) { // remove previous character (decrement index) if Backspace/Delete key pressed index--; + if (((char) c == '\r') || ((char) c == '\n')) break; + if ( ((char) c == '\x7F') || ((char) c == '\x08') ) { if (index > 0) index--; } else if (c >= 0) { - ui_buffer[index++]=(char) c; // put character into ui_buffer + ui_buffer[index++]=(char) c; } //pc.printf("check 4\n"); } - ui_buffer[index]='\0'; // terminate string with NULL + ui_buffer[index]='\0'; - if ((char) c == '\r') { // if the "last" character was a carriage return, also clear linefeed if it is next character + if ((char) c == '\r') { wait_ms(1); //pc.printf("check 5\n"); if (pc.readable()==1) { //pc.printf("check 6\n"); - pc.getc(); // if linefeed appears, read it and throw it away + pc.getc(); } - //pc.printf("check 7\n"); + } - //pc.printf("check 8\n"); + - return index; // return number of characters, not including null terminator + return index; } -// Read a float value from the serial interface + float read_float() { float data; @@ -616,13 +712,7 @@ return(data); } -// Read an integer from the serial interface. -// The routine can recognize Hex, Decimal, Octal, or Binary -// Example: -// Hex: 0x11 (0x prefix) -// Decimal: 17 -// Octal: 021 (leading zero prefix) -// Binary: B10001 (leading B prefix) + int32_t read_int() { int32_t data; @@ -636,14 +726,13 @@ return(data); } -// Read a string from the serial interface. Returns a pointer to the ui_buffer. char *read_string() { read_data(); return(ui_buffer); } -// Read a character from the serial interface + int8_t read_char() { read_data();