KIT Solar Car Project / Mbed 2 deprecated BMS_v1

Dependencies:   mbed INA226

Revision:
2:f6f76dde7e1d
Parent:
0:f06ed53310a3
Child:
3:a1368cd4b0a9
--- a/main.cpp	Fri Feb 09 04:43:04 2018 +0000
+++ b/main.cpp	Sat Oct 26 05:24:17 2019 +0000
@@ -12,12 +12,16 @@
 
 #define DATALOG_ENABLED 1
 #define DATALOG_DISABLED 0
+
+Ticker ticker;
 DigitalOut led1(LED1);
 Serial pc(USBTX, USBRX);
+CAN BMS_CAN(p9,p10);
+
 
 void run_command(uint32_t cmd);
 void measurement_loop(uint8_t datalog_en);
-void print_menu();
+//void print_menu();
 void print_cells(uint8_t datalog_en);
 void print_open();
 void print_aux(uint8_t datalog_en);
@@ -27,6 +31,10 @@
 void print_pec(void);
 void serial_print_hex(uint8_t data);
 void check_error(int error);
+void wakeup();
+void cell_read();
+void spi_error();
+
 //char get_char();
 //void read_config_data(uint8_t cfg_data[][6], uint8_t nIC);
 
@@ -51,8 +59,8 @@
 const uint16_t MEASUREMENT_LOOP_TIME = 500;//milliseconds(mS)
 
 //Under Voltage and Over Voltage Thresholds
-const uint16_t OV_THRESHOLD = 41000; // Over voltage threshold ADC Code. LSB = 0.0001
-const uint16_t UV_THRESHOLD = 30000; // Under voltage threshold ADC Code. LSB = 0.0001
+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
@@ -77,7 +85,7 @@
 // Octal:   O21 (leading letter O prefix)
 // Binary:  B10001 (leading letter B prefix)
 int32_t read_int();
-
+int kaisuu;
 // Read a string from the serial interface.  Returns a pointer to the ui_buffer.
 char *read_string();
 
@@ -109,21 +117,73 @@
     LTC681x_init_cfg(TOTAL_IC, bms_ic);
     LTC6811_reset_crc_count(TOTAL_IC,bms_ic);
     LTC6811_init_reg_limits(TOTAL_IC,bms_ic);
-    print_menu();
+    wakeup();
+    //print_menu();
+   
 
     while(1) {
+      
+        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();      // Read the user commandi
-        pc.printf("command -> %d \n", user_command);
+        user_command = read_int();     
         run_command(user_command);
+        
     }
 }
 
+void wakeup(){
+    int countup;
+    int8_t error = 0;
+    uint32_t conv_time = 0;
+    
+    
+    wakeup_sleep(TOTAL_IC);
+            LTC6811_wrcfg(TOTAL_IC,bms_ic);
+            print_config();
+                 
+                 wait(0.05);
+                 
+     wakeup_sleep(TOTAL_IC);
+            error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
+            check_error(error);
+            print_rxconfig(); 
+                 wait(0.05);
+                 
+     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"); 
+            
+            
+    }
+    
+    
+void spi_error(){
+            
+            int8_t error = 0;
+            int countup;
+            
+            if(error = -1 ){
+            for(countup = 0; countup <= 100; countup++){
+             wakeup();
+             if(error = 1){
+                 break;
+                           }
+                                                        }                                       
+                          }
+             if(error = 1){
+             cell_read();
+                          }
+             
+                 }
+    
 /*!*****************************************
   \brief executes the user command
 *******************************************/
@@ -135,38 +195,14 @@
 //    uint32_t user_command;
     int8_t readIC=0;
     char input = 0;
+    
+    cell_read();
+   
+    
     switch (cmd) {
 
-        case 1: // Write Configuration Register
-            wakeup_sleep(TOTAL_IC);
-            LTC6811_wrcfg(TOTAL_IC,bms_ic);
-            print_config();
-            break;
 
-        case 2: // Read Configuration Register
-            wakeup_sleep(TOTAL_IC);
-            error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
-            check_error(error);
-            print_rxconfig();
-            break;
-
-        case 3: // Start Cell ADC Measurement
-            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");
-            break;
-
-        case 4: // Read Cell Voltage Registers
-            wakeup_sleep(TOTAL_IC);
-            error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); // Set to read back all cell voltage registers
-            check_error(error);
-            print_cells(DATALOG_DISABLED);
-            break;
-
-        case 5: // Start GPIO ADC Measurement
+        case 3: // Start GPIO ADC Measurement
             wakeup_sleep(TOTAL_IC);
             LTC6811_adax(ADC_CONVERSION_MODE , AUX_CH_TO_CONVERT);
             LTC6811_pollAdc();
@@ -174,14 +210,14 @@
             pc.printf("\n");
             break;
 
-        case 6: // Read AUX Voltage Registers
+        case 4: // Read AUX Voltage Registers
             wakeup_sleep(TOTAL_IC);
             error = LTC6811_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
             check_error(error);
             print_aux(DATALOG_DISABLED);
             break;
 
-        case 7: // Start Status ADC Measurement
+        case 5: // Start Status ADC Measurement
             wakeup_sleep(TOTAL_IC);
             LTC6811_adstat(ADC_CONVERSION_MODE, STAT_CH_TO_CONVERT);
             LTC6811_pollAdc();
@@ -189,14 +225,14 @@
             pc.printf("\n");
             break;
 
-        case 8: // Read Status registers
+        case 6: // Read Status registers
             wakeup_sleep(TOTAL_IC);
             error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
             check_error(error);
             print_stat();
             break;
 
-        case 9: // Loop Measurements
+        case 7: // Loop Measurements
             pc.printf("transmit 'm' to quit\n");
             wakeup_sleep(TOTAL_IC);
             LTC6811_wrcfg(TOTAL_IC,bms_ic);
@@ -212,16 +248,16 @@
             //print_menu();
             break;
 
-        case 10: // Run open wire self test
+        case 8: // Run open wire self test
             print_pec();
 
             break;
 
-        case 11: // Read in raw configuration data
+        case 9: // Read in raw configuration data
             LTC6811_reset_crc_count(TOTAL_IC,bms_ic);
             break;
 
-        case 12:  // Run the ADC/Memory Self Test
+        case 10:  // Run the ADC/Memory Self Test
             wakeup_sleep(TOTAL_IC);
             error = LTC6811_run_cell_adc_st(CELL,ADC_CONVERSION_MODE,bms_ic);
             pc.printf("%d", error);
@@ -236,10 +272,10 @@
             error = LTC6811_run_cell_adc_st(STAT,ADC_CONVERSION_MODE, bms_ic);
             pc.printf("%d",error);
             pc.printf(" : errors detected in Digital Filter and STAT Memory\n");
-            print_menu();
+            //print_menu();
             break;
 
-        case 13: // Enable a discharge transistor
+        case 11: // Enable a discharge transistor
             pc.printf("Please enter the Spin number\n");
             readIC = (int8_t)read_int();
             LTC6811_set_discharge(readIC,TOTAL_IC,bms_ic);
@@ -248,14 +284,14 @@
             print_config();
             break;
 
-        case 14: // Clear all discharge transistors
+        case 12: // Clear all discharge transistors
             clear_discharge(TOTAL_IC,bms_ic);
             wakeup_sleep(TOTAL_IC);
             LTC6811_wrcfg(TOTAL_IC,bms_ic);
             print_config();
             break;
 
-        case 15: // Clear all ADC measurement registers
+        case 13: // Clear all ADC measurement registers
             wakeup_sleep(TOTAL_IC);
             LTC6811_clrcell();
             LTC6811_clraux();
@@ -263,7 +299,7 @@
             pc.printf("All Registers Cleared\n");
             break;
 
-        case 16: // Run the Mux Decoder Self Test
+        case 14: // Run the Mux Decoder Self Test
             wakeup_sleep(TOTAL_IC);
             LTC6811_diagn();
             wait_ms(5);
@@ -280,14 +316,14 @@
 
             break;
 
-        case 17: // Run ADC Overlap self test
+        case 15: // Run ADC Overlap self test
             wakeup_sleep(TOTAL_IC);
             error = (int8_t)LTC6811_run_adc_overlap(TOTAL_IC,bms_ic);
             if (error==0) pc.printf("Overlap Test: PASS\n");
             else pc.printf("Overlap Test: FAIL\n");
             break;
 
-        case 18: // Run ADC Redundancy self test
+        case 16: // Run ADC Redundancy self test
             wakeup_sleep(TOTAL_IC);
             error = LTC6811_run_adc_redundancy_st(ADC_CONVERSION_MODE,AUX,TOTAL_IC, bms_ic);
             pc.printf("%d",error);
@@ -299,12 +335,12 @@
             pc.printf(" : errors detected in STAT Measurement\n");
             break;
 
-        case 19:
+        case 17:
             LTC6811_run_openwire(TOTAL_IC, bms_ic);
             print_open();
             break;
 
-        case 20: //Datalog print option Loop Measurements
+        case 18: //Datalog print option Loop Measurements
             pc.printf("transmit 'm' to quit\n");
             wakeup_sleep(TOTAL_IC);
             LTC6811_wrcfg(TOTAL_IC,bms_ic);
@@ -317,18 +353,31 @@
 
                 wait_ms(MEASUREMENT_LOOP_TIME);
             }
-            print_menu();
+            //print_menu();
             break;
 
         case 'm': //prints menu
-            print_menu();
+            //print_menu();
             break;
 
         default:
             pc.printf("Incorrect Option\n");
             break;
+            }
     }
-}
+void cell_read(){
+    
+    int8_t error = 0;
+    uint32_t conv_time = 0;
+
+    int8_t readIC=0;
+         wakeup_sleep(TOTAL_IC);
+            error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); // Set to read back all cell voltage registers
+            check_error(error);
+            print_cells(DATALOG_DISABLED);
+        
+        }
+
 
 void measurement_loop(uint8_t datalog_en)
 {
@@ -387,6 +436,7 @@
 /*!*********************************
   \brief Prints the main menu
 ***********************************/
+/*
 void print_menu()
 {
     pc.printf("Please enter LTC6811 Command\n");
@@ -404,7 +454,7 @@
     pc.printf("Please enter command:\n");
     pc.printf("\n");
 }
-
+*/
 /*!************************************************************
   \brief Prints cell voltage codes to the serial port
  *************************************************************/
@@ -595,11 +645,11 @@
     '\0','\0','\0'
 };
 
-// Read data from the serial interface into the ui_buffer
+// シリアル インターフェイスから ui_バッファへのデータの読み取り
 uint8_t read_data()
 {
-    uint8_t index = 0; //index to hold current location in ui_buffer
-    int c; // single character used to store incoming keystrokes
+    uint8_t index = 0; // 内の現在の場所を保持するインデックス
+    int c; // 着信キーストロークの格納に使用される単一の文字
     //pc.printf("check 1\n");
     while (index < UI_BUFFER_SIZE-1) {
         //pc.printf("check 2\n");
@@ -607,7 +657,7 @@
         //return c;
         //pc.printf("check 3\n");
         
-        if (((char) c == '\r') || ((char) c == '\n')) break; // if carriage return or linefeed, stop and return data
+        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 (index > 0) index--;
         } else if (c >= 0) {