LP Long Distance IR Vision Robot

Dependencies:   max77650_charger_sample BufferedSerial SX1276GenericLib Adafruit-MotorShield NEO-6m-GPS MAX17055_EZconfig Adafruit_GFX USBDeviceHT Adafruit-PWM-Servo-Driver

Revision:
41:1381a49e019e
Parent:
40:6f8744c366c2
Child:
42:911770814147
diff -r 6f8744c366c2 -r 1381a49e019e main.cpp
--- a/main.cpp	Wed Aug 01 03:52:55 2018 +0000
+++ b/main.cpp	Wed Aug 01 08:32:21 2018 +0000
@@ -16,6 +16,7 @@
 #include "main.h"
 #include "global_buffers.h"
 //#define DEBUG_GRID_EYE
+//#define DEBUG_MAX17055
 #define RASPBERRY_PI
 DigitalOut myled(LED);
 Serial pc(USBTX, USBRX);
@@ -416,11 +417,37 @@
      * MAX17055 Data Buffers
      **************************************************************************/
     #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
-        uint8_t curr_MAX17055_from_slave[size_of_MAX17055];
+        union max17055_u
+        {   
+            uint8_t curr_MAX17055_from_slave[size_of_MAX17055];
+            struct battery
+            {
+                uint8_t soc_FG;         // in Battery Percent
+                uint8_t avg_soc_FG;     // in Battery Percent
+                float tte_FG;           // in seconds
+                float ttf_FG;           // in seconds
+                int vcell_FG;              // in 78.125uV per bit
+                int curr_FG;               // in uAmps
+                int avg_curr_FG;           // in uAmps                
+            } battery;
+        } max17055_u;
         uint8_t prev_MAX17055_from_slave[size_of_MAX17055];
         
     #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
-        uint8_t curr_MAX17055_to_master[size_of_MAX17055];
+        union max17055_u
+        {   
+            uint8_t curr_MAX17055_to_master[size_of_MAX17055];
+            struct battery
+            {
+                uint8_t soc_FG;         // in Battery Percent
+                uint8_t avg_soc_FG;     // in Battery Percent
+                float tte_FG;           // in seconds
+                float ttf_FG;           // in seconds
+                int vcell_FG;              // in 78.125uV per bit
+                int curr_FG;               // in uAmps
+                int avg_curr_FG;           // in uAmps                
+            } battery;
+        } max17055_u;
     #endif
     
     /***************************************************************************
@@ -429,8 +456,10 @@
     #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
         uint8_t curr_MAX77650_from_slave[size_of_MAX77650];
         uint8_t prev_MAX77650_from_slave[size_of_MAX77650];
+        bool chrg_status = false;; //True = ON False = OFF    
     #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
         uint8_t curr_MAX77650_to_master[size_of_MAX77650];
+        bool chrg_status = false; //True = ON False = OFF
     #endif
 
     
@@ -481,7 +510,6 @@
         simo_pmic.initCharger();
         simoIRQ.fall( &charger_Detect );
         simo_pmic.enCharger();
-        bool chrg_status; //True = ON False = OFF
     #endif
     /***************************************************************************
      * Finish Setting up the MAX17650 on the MAX32620FTHR: Only on the Slave Device
@@ -490,18 +518,14 @@
         // No MAX17055 on the MAX32630FTHR
     #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
     //rsense 5mOhm for MAX32620FTHR
-        design_param.designcap = 0x0069;  //Design Battery Capacity mAh
+        design_param.designcap = 2200;  //Design Battery Capacity mAh
         design_param.ichgterm = 0x0016;  // Charge Termination Current for the Battery
         design_param.vempty = 0x9B00;  // Battery Empty Voltage
         design_param.vcharge = 4200;  // Battery Charge Voltage can be obtained from MAX77650 configuration
         design_param.rsense = 5; //5mOhms
     
-        int soc_FG, tte_FG, ttf_FG, curr_FG,volt_FG, status, alsValue, proxValue;
-        int oper_f1, volt_mod_FG;
-        int alsCAL;       //Variables
-        uint8_t prox_data;
-        uint8_t proxCAL;         //Prox Sensor
-        int sensor_Case_value;
+        int oper_f1, volt_mod_FG, status;
+        
         //serial.printf("Test Init and Temp Functions \r\n");
         //status = fuelGauge.forcedExitHyberMode();
         status = fuelGauge.init(design_param);
@@ -517,7 +541,7 @@
         displayOled.setTextWrap(false);
     
         displayOled.clearDisplay();
-        displayOled.printf("%ux%u OLED Display\r\n", displayOled.width(), displayOled.height());
+        displayOled.printf("%ux%u Please Check Connection to Robot!!\r\n", displayOled.width(), displayOled.height());
         //displayOled.drawBitmap(0,0,maximLarge,128,32,1);
         displayOled.display();
         displayOled.display();
@@ -615,7 +639,6 @@
         #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
             // Aquire raw data about 8x8 frame from the grid eye sensor in this function call 
             gridEye.getRaw8x8FrameData(curr_raw_frame_to_master);
-//            wait_ms( 10 );
             #if defined(RASPBERRY_PI)
                 if( memcmp(prev_raw_frame_to_master, curr_raw_frame_to_master, sizeof(curr_raw_frame_to_master)) != 0 )
                 {
@@ -655,7 +678,6 @@
                     frame_idx = frame_idx +1;
                 }
             #endif
-//            wait_ms( 10 );
             memcpy(prev_raw_frame_to_master, curr_raw_frame_to_master, sizeof(curr_raw_frame_to_master));
         #endif      
         
@@ -663,7 +685,7 @@
          * GPS Sensor 
          **************************************************************************/
         #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
-            //None yet
+            // No gps sensor is applied
         #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
             // The testing hasnt been done yet for the GPS Module. Will do this when more parts come in. 
             /*
@@ -675,6 +697,118 @@
         #endif
         
         /***************************************************************************
+         * MAX17055 Fuel Gauge Sensor 
+         **************************************************************************/
+        #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
+            // Process the soc to see if the oled display needs to be updated or not.
+            #if defined(DEBUG_MAX17055)
+                pc.printf("soc_FG :%d\n"      ,max17055_u.battery.soc_FG);
+                pc.printf("avg_soc_FG :%d\n"  ,max17055_u.battery.avg_soc_FG);
+                pc.printf("tte_FG :%d\n"      ,max17055_u.battery.tte_FG);
+                pc.printf("ttf_FG :%d\n"      ,max17055_u.battery.ttf_FG);
+                pc.printf("vcell_FG :%d\n"    ,max17055_u.battery.vcell_FG);
+                pc.printf("curr_FG :%d\n"     ,max17055_u.battery.curr_FG);
+                pc.printf("avg_curr_FG :%d\n" ,max17055_u.battery.avg_curr_FG);
+                
+                for (int idx = 0; idx < size_of_MAX17055; idx++)
+                {
+                    pc.printf("Byte #%d is: %d\n", idx, max17055_u.curr_MAX17055_from_slave[idx]);
+                }
+            #endif
+            if( memcmp(prev_MAX17055_from_slave, curr_MAX17055_from_slave, sizeof(curr_MAX17055_from_slave)) != 0 )
+            {
+                // Stop the scrolling to allow for a static image to be displayed on OLED
+                displayOled.stopscroll();
+                
+                displayOled.clearDisplay();
+
+                if ( chrg_status == false){
+                    if (max17055_u.battery.soc_FG >= 89)
+                        displayOled.drawBitmap(96,0,battFull,32,16,1);
+                    else if (max17055_u.battery.soc_FG >= 65 && max17055_u.battery.soc_FG < 89)
+                        displayOled.drawBitmap(96,0,batt75,32,16,1);
+                    else if (max17055_u.battery.soc_FG >= 45 && max17055_u.battery.soc_FG < 65)
+                        displayOled.drawBitmap(96,0,batt50,32,16,1);
+                    else if (max17055_u.battery.soc_FG >= 20 && max17055_u.battery.soc_FG < 45)
+                        displayOled.drawBitmap(96,0,batt25,32,16,1);
+                    else 
+                        displayOled.drawBitmap(96,0,battEmpty,32,16,1);
+                }
+
+                if (chrg_status == true){
+                    displayOled.drawBitmap(96,0,battChrging,32,16,1);
+                }
+
+                
+                //serial.printf("Time to Empty= %dsec \r\n",tte_FG);
+                int tte_minutes = (max17055_u.battery.tte_FG/60);
+                int ttf_minutes = (max17055_u.battery.tte_FG/60);
+                //serial.printf("Time to Empty= %dhours \r\n",tte_FG); 
+                //serial.printf("Temp Function Status= %X \r\n",status);
+                //wait(1);
+                //serial.printf("SOC = %d %% \r\n",soc_FG);
+                //serial.printf("Temp Function Status= %X \r\n",status);
+                //serial.printf("Current = %d uamp \r\n",curr_FG);
+//                pc.printf("Voltage = %d uvolts \r\n",max17055_u.battery.vcell_FG);
+                int oper_f1 = max17055_u.battery.vcell_FG /1000000;
+                int volt_mod_FG = (max17055_u.battery.vcell_FG % 1000000)/1000;
+
+//                pc.printf("Voltage = %d V \r\n",max17055_u.battery.vcell_FG);
+//                pc.printf("Voltage = %d V \r\n",oper_f1);
+
+
+
+                displayOled.setTextCursor(0,0);
+                displayOled.setTextColor(1);
+                displayOled.setTextSize(1);
+                //displayOled.setTextWrap(true);
+                displayOled.printf("MAX17055");
+                displayOled.setTextCursor(0,8);
+                displayOled.printf("SOC = %d%% \r\n",max17055_u.battery.avg_soc_FG);
+            
+                displayOled.setTextCursor(0,16);
+                if (chrg_status == true){    
+                    displayOled.printf("TTF = %dmins \r\n",tte_minutes);
+                }else 
+                    displayOled.printf("TTE = %dmins \r\n",tte_minutes);
+                displayOled.setTextCursor(0,24);
+                displayOled.printf("I=%duA \r\n",max17055_u.battery.avg_curr_FG);
+                displayOled.setTextCursor(65,24);
+                if (volt_mod_FG > 99){
+                    displayOled.printf("V=%d.%dV \r\n",oper_f1,volt_mod_FG);
+                }else if (volt_mod_FG < 100){
+                    displayOled.printf("V=%d.0%dV \r\n",oper_f1,volt_mod_FG);   
+                }
+                
+                displayOled.display();
+                displayOled.display();            
+            }        
+        #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot    
+            max17055_u.battery.soc_FG      = fuelGauge.get_SOC();
+            max17055_u.battery.avg_soc_FG  = fuelGauge.get_atAvSOC(); 
+            max17055_u.battery.tte_FG      = fuelGauge.get_TTE();
+            max17055_u.battery.ttf_FG      = fuelGauge.get_TTF();
+            max17055_u.battery.vcell_FG    = fuelGauge.get_Vcell();
+            max17055_u.battery.curr_FG     = fuelGauge.get_Current(design_param);
+            max17055_u.battery.avg_curr_FG = fuelGauge.get_AvgCurrent(design_param);
+            
+            #if defined(DEBUG_MAX17055)
+                pc.printf("soc_FG :%d\n"      ,max17055_u.battery.soc_FG);
+                pc.printf("avg_soc_FG :%d\n"  ,max17055_u.battery.avg_soc_FG);
+                pc.printf("tte_FG :%d\n"      ,max17055_u.battery.tte_FG);
+                pc.printf("ttf_FG :%d\n"      ,max17055_u.battery.ttf_FG);
+                pc.printf("vcell_FG :%d\n"    ,max17055_u.battery.vcell_FG);
+                pc.printf("curr_FG :%d\n"     ,max17055_u.battery.curr_FG);
+                pc.printf("avg_curr_FG :%d\n" ,max17055_u.battery.avg_curr_FG);
+                
+                for (int idx = 0; idx < size_of_MAX17055; idx++)
+                {
+                    pc.printf("Byte #%d is: %d\n", idx, max17055_u.curr_MAX17055_to_master[idx]);
+                }
+            #endif
+        #endif
+        
+        /***************************************************************************
          * Fill Payload Buffer With Data From Sensors for LoRa Transmition
          **************************************************************************/
         #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
@@ -684,7 +818,7 @@
             memcpy(&BufferTx[tx_idx_signature], PongMsg,                  size_signature);
             memcpy(&BufferTx[tx_idx_grid_eye],  curr_raw_frame_to_master, size_of_grid_eye);
             memcpy(&BufferTx[tx_idx_gps],       curr_gps_to_master,       size_of_gps);
-            memcpy(&BufferTx[tx_idx_MAX17055],  curr_MAX17055_to_master,  size_of_MAX17055);
+            memcpy(&BufferTx[tx_idx_MAX17055],  max17055_u.curr_MAX17055_to_master,  size_of_MAX17055);
             memcpy(&BufferTx[tx_idx_MAX77650],  curr_MAX77650_to_master,  size_of_MAX77650);
         #endif
         
@@ -694,7 +828,6 @@
         /***************************************************************************
          * Lora Communications
          **************************************************************************/
-//        wait_ms( 10 );  
         SX1276PingPong();
         
         /***************************************************************************
@@ -705,7 +838,7 @@
             memcpy(ID_of_slave,               &BufferRx[rx_idx_signature], size_signature);
             memcpy(curr_raw_frame_from_slave, &BufferRx[rx_idx_grid_eye],  size_of_grid_eye);
             memcpy(curr_gps_from_slave,       &BufferRx[rx_idx_gps],       size_of_gps);
-            memcpy(curr_MAX17055_from_slave,  &BufferRx[rx_idx_MAX17055],  size_of_MAX17055);
+            memcpy(max17055_u.curr_MAX17055_from_slave,  &BufferRx[rx_idx_MAX17055],  size_of_MAX17055);
             memcpy(curr_MAX77650_from_slave,  &BufferRx[rx_idx_MAX77650],  size_of_MAX77650);
         #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
             memcpy(ID_of_master,              &BufferRx[rx_idx_signature], size_signature);