17th Nov 2016

Dependencies:   mbed

Fork of TEST10_8A_Nucleo_Charger_vApril_copy by SmartCharge

Revision:
4:4fae92446559
Parent:
3:0069f2cad504
--- a/main.cpp	Wed Aug 10 13:25:04 2016 +0000
+++ b/main.cpp	Tue Nov 22 14:44:32 2016 +0000
@@ -1,11 +1,9 @@
-/**
+/*
  * Copyright (c) 2016 M2C Ltd
- * TEST 10.8A  (18% PWM) for first Convex build / Mick's dad house
- 
- 2000 samples 
- 
- 3 second reset
- *
+ * TEST  32to16 with cp error sense/ Hyundai Corrected 16112016
+ * 5000 samples
+ * 3 second reset
+ * if / else if / else - post insertation read
  */
 
 #include "mbed.h"
@@ -26,23 +24,26 @@
 DigitalOut red(D10);
 DigitalOut blue(D11);
 
-unsigned char control_pilot;
+
 
 #define STATE_A 1           // Vehicle not connected
 #define STATE_B 2           // Vehicle connected / not ready to accept energy
 #define STATE_C 3           // Vehicle connected / ready to accept energy / ventilation not required
 #define STATE_D 4           // Vehicle connected / ready to accept energy / ventilation required
 
-#define PILOT_12V   1
-#define PILOT_9V    2
-#define PILOT_6V    3
-#define PILOT_DIODE 4 
-#define PILOT_NOK   0
-#define PILOT_RESET 5
+#define PILOT_12V      1
+#define PILOT_9V_16    2
+#define PILOT_6V_16    3 
+#define PILOT_9V_32    4
+#define PILOT_6V_32    5
+#define PILOT_RESET    6
+#define PILOT_NOK      7 
 
 float current_average=0;
 float previous_average=0;
-#define NUM_SAMPLES 2000 // size of sample series
+unsigned char control_pilot;
+
+#define NUM_SAMPLES 5000 // size of sample series
 
 #define SAMPLE_BLOCKS 1 //CC for reset button
 #define RESET_SECONDS 3 //CC for reset button
@@ -50,21 +51,24 @@
 bool resetDown = false;
 bool resetCharger = false;
 
-void timedOut() {
+void timedOut()
+{
     resetCharger = true;
     printf("Button pressed for more than 3 sec ! Charger reset !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \r\n");
 }
- 
-void resetPress() {
+
+void resetPress()
+{
     printf("Reset button pressed ... starting timer \r\n");
     buttonTimer.stop();
     buttonTimer.reset();
     buttonTimer.start();
-    resetDown = true;   
-    buttonTimeout.attach(&timedOut, RESET_SECONDS);   
+    resetDown = true;
+    buttonTimeout.attach(&timedOut, RESET_SECONDS);
 }
-    
-void resetRelease() {
+
+void resetRelease()
+{
     printf("Reset button released \r\n");
     int elapsedSeconds = buttonTimer.read();
     buttonTimer.stop();
@@ -72,158 +76,212 @@
     if (elapsedSeconds > 3) {
         resetCharger = true;
         printf("Button was pressed for more than 3 sec !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \r\n");
-    }
-    else{
-    printf("If we're getting here then we've released the button before 3 seconds were up.\r\n");
+    } else {
+        printf("If we're getting here then we've released the button before 3 seconds were up.\r\n");
     }
     printf("We will detach the timeout an setup for the next time.\r\n");
     printf("%u \r\n", elapsedSeconds);
     buttonTimeout.detach();
 }
- 
-int main() {
-        
+
+int main()
+{
+
     button.fall(&resetPress);
     button.rise(&resetRelease);
 
     while(1) {
-               
-    float readingA2 = pp_value.read();
 
-    readingA2 = readingA2 * 3300;
+        float readingA2 = pp_value.read();
+        readingA2 = readingA2 * 3300;
 
- for (int i = 0; i < NUM_SAMPLES ; i++)
-        {
+        for (int i = 0; i < NUM_SAMPLES ; i++) {
             float sample_value = cp_value.read_u16();
             sample_value = (float) 3300 / 65535 * (float) sample_value;  // convert sample to voltage
             current_average = ((sample_value + (i * previous_average)) / (i+1));
-            previous_average = current_average; 
+            previous_average = current_average;
         }
-printf("current_average %.f \n\r", current_average);
-printf("PP Pilot Value = %.0f mV\n\r",readingA2);
+        printf("PP Pilot Value = %.0f mV\n\r",readingA2);
+        printf("CP avrg. Value %.f \n\r", current_average);
         
-  
-  
-//*********************   TEST CONVEX BUILD***********************//
+
+
+
+//*********************   TEST CONVEX BUILD Hyundai 32/16 ***********************//
 if(readingA2>3200) 
 {
+    printf("cable out \n\r");
     
-    if ((current_average > 2600) &&  (current_average < 2750) || (current_average > 370) && (current_average < 470)) // first reading ˜2702 & ˜755 after 
-    {        
+    if ((current_average > 2650) &&  (current_average < 2750)){        
             control_pilot = PILOT_12V;    // Pilot at 12V 
     }
-    if (current_average < 100) // first reading ˜2702 & ˜755 after 
-    {        
+    if (current_average < 100){ // first reading ˜2702 & ˜755 after         
             control_pilot = PILOT_12V;    // Pilot at 12V 
             resetCharger = false;
-    }
+    } 
+}
 
-        
-}
-if(readingA2<3200) // tester 32A cable
+if(readingA2<3200)
 {
-       
-        if((current_average > 1900) &&  (current_average <2100) || (current_average > 400) &&  (current_average <470)) // first reading ˜2012 & ˜573 after
-        {      
-            control_pilot = PILOT_9V;    // Pilot at 9V       
-        }
-        if((current_average> 270) && (current_average<350)) // first reading ˜1372 & ˜405 after
-        {       
-            control_pilot = PILOT_6V;    // Pilot at 6V       
-        }
-        if(resetCharger == true)
-        {control_pilot = PILOT_RESET;}
-        
-}//end if(readingA2!=0) 
-  
-        
-/*
-if(readingA2>3200) 
-{
-    
-    if ((current_average > 2650) &&  (current_average < 2750) || (current_average > 700) && (current_average < 800)) // first reading ˜2702 & ˜755 after 
-    {        
+       //16amp cable in
+        if((readingA2>150)&&(readingA2<300)){ 
+            
+            if((resetCharger == true)||(current_average < 100)){
+                control_pilot = PILOT_RESET;
+                printf("reset @ 16\n\r");
+            }
+            else if ((current_average > 2650) &&  (current_average < 2750))
+            {         
             control_pilot = PILOT_12V;    // Pilot at 12V 
-    }
-    if (current_average < 100) // first reading ˜2702 & ˜755 after 
-    {        
+            printf("16A cable in -no car \r\n");
+            printf("12 @ 32\n\r");
+            }
+            else if((current_average > 1900) &&  (current_average <2100) || (current_average > 550) &&  (current_average <650))
+            {    
+                control_pilot = PILOT_9V_16;    // Pilot at 9V   
+                printf("9 @ 16 \r\n");    
+            }
+            else if((current_average> 400) && (current_average<500)) {      
+                control_pilot = PILOT_6V_16;    // Pilot at 6V
+                printf("6 @ 16 \r\n");        
+            }
+            /*else if (current_average < 100){ // first reading ˜2702 & ˜755 after        
+                control_pilot = PILOT_RESET;
+                printf("car gone so @ 16");
+            }
+            */ 
+            else {
+                control_pilot = PILOT_NOK;
+                printf("NOT OK @ 16 \r\n");
+            }
+            
+        }
+        
+        //32amp cable in
+        if((readingA2> 0)&&(readingA2<100))
+        {
+            if((resetCharger == true)||(current_average < 100))
+            {
+                control_pilot = PILOT_RESET;
+                printf("reset @ 32\n\r");
+            }
+            else if ((current_average > 2650) &&  (current_average < 2750)) // first reading ˜2702 & ˜755 after 
+            {         
             control_pilot = PILOT_12V;    // Pilot at 12V 
-            resetCharger = false;
-    }
-
-        
+            printf("32A cable in -no car \r\n");
+            printf("12 @ 32\n\r");
+            }
+            else if((current_average > 1900) &&  (current_average <2100) || (current_average > 1070) &&  (current_average <1170)) // first reading ˜2012 & ˜573 after
+            {    
+                control_pilot = PILOT_9V_32;    // Pilot at 9V   
+                printf("9 @ 32 \r\n");     
+            }
+            else if((current_average> 730) && (current_average<870)) 
+            {       
+                control_pilot = PILOT_6V_32;    // Pilot at 6V       
+                printf("6 @ 32 \r\n"); 
+            }
+            /*else if (current_average < 100){ // first reading ˜2702 & ˜755 after        
+                control_pilot = PILOT_RESET;
+                printf("car gone so @ 32");
+            }
+            */
+            else {
+                control_pilot = PILOT_NOK;
+                printf("NOT OK @ 32 \r\n"); 
+            }
+        }
 }
-if(readingA2<3200) // tester 32A cable
-{
-       
-        if((current_average > 1900) &&  (current_average <2100) || (current_average > 500) &&  (current_average <600)) // first reading ˜2012 & ˜573 after
-        {      
-            control_pilot = PILOT_9V;    // Pilot at 9V       
-        }
-        if((current_average> 350) && (current_average<450)) // first reading ˜1372 & ˜405 after
-        {       
-            control_pilot = PILOT_6V;    // Pilot at 6V       
-        }
-        if(resetCharger == true)
-        {control_pilot = PILOT_RESET;}
         
-}//end if(readingA2!=0) 
-*/
-
-
+        
         switch(control_pilot) {
         case PILOT_12V:
             printf("Charger in STATE A\r\n");
-            printf("PILOT_12V - Pilot at 12 V \n\r");
-            my_pwm = 0;
+            //printf("PILOT_12V - Pilot at 12 V \n\r");
             contactor =0;
+            my_pwm.write(0);
             lock=0;
             red = 0;
             green = 0;
             blue = 1;
             break;
   
-        case PILOT_9V:
+        case PILOT_9V_16:
             contactor =0;
             //relay=0;
             lock=1;
             my_pwm.period_ms(1);
             my_pwm.pulsewidth_ms(1);
-            my_pwm.write(0.820);                    ///////PWM CHANGED from my_pwm.write(0.734);
+            my_pwm.write(0.734);         
             printf("Charger in STATE b\r\n");
-            printf("PILOT_9V - Pilot at 9 V \n\r");
+            //printf("PILOT_9V 16- Pilot at 9 V \n\r");
             red = 1;
             green = 1;
             blue = 0;
             break;
                 
-        case PILOT_6V:
+        case PILOT_6V_16:
             contactor =1;
             relay=1;
             lock=1;
             my_pwm.period_ms(1);
             my_pwm.pulsewidth_ms(1);
-            my_pwm.write(0.820);                    ///////PWM CHANGED from my_pwm.write(0.734);
-
+            my_pwm.write(0.734);                    
             printf("Charger in STATE c\r\n");
-            printf("PILOT_6V - Pilot at 6 V \n\r");
+            //printf("PILOT_6V 16- Pilot at 6 V \n\r");
             red = 0;
             green = 1;
             blue = 0;
             break;
-                   
-        case PILOT_NOK:
-            printf("Error");
-            printf("PILOT_NOK - Pilot ERROR \n\r");
-            lock=0;
+            
+        case PILOT_9V_32:
+            contactor =0;
+            //relay=0;
+            lock=1;
+            my_pwm.period_ms(1);
+            my_pwm.pulsewidth_ms(1);
+            my_pwm.write(0.468);         
+            printf("Charger in STATE b\r\n");
+            //printf("PILOT_9V 32- Pilot at 9 V \n\r");
             red = 1;
-            green = 0;
+            green = 1;
+            blue = 0;
+            break;
+                
+        case PILOT_6V_32:
+            contactor =1;
+            relay=1;
+            lock=1;
+            my_pwm.period_ms(1);
+            my_pwm.pulsewidth_ms(1);
+            my_pwm.write(0.468);                    
+            printf("Charger in STATE c\r\n");
+            //printf("PILOT_6V 32- Pilot at 6 V \n\r");
+            red = 0;
+            green = 1;
             blue = 0;
             break;
             
         case PILOT_RESET:
             printf("RESET IMPLEMENTED \n\r");
-            printf("PILOT_RESET - Pilot at -12V \n\r");
+            //printf("PILOT_RESET - Pilot at -12V \n\r");
+            my_pwm.period_ms(1);
+            my_pwm.pulsewidth_ms(1);
+            my_pwm.write(1);
+            contactor =0;
+            relay=0;
+            lock=0;
+            red = 0;
+            green = 0;
+            blue = 1;
+            wait(0.5); // 200 ms
+            blue = 0; // LED is OFF
+            wait(0.2); // 1 sec
+            break;
+            
+        case PILOT_NOK:
+            printf("Error");
+            printf("PILOT_NOT OK - Pilot readding incorect \n\r");
             my_pwm.period_ms(1);
             my_pwm.pulsewidth_ms(1);
             my_pwm.write(1);
@@ -233,13 +291,12 @@
             red = 1;
             green = 0;
             blue = 0;
-            wait(0.5); // 200 ms
-            red = 0; // LED is OFF
-            wait(0.2); // 1 sec
             break;
-            
         }//end switch
-     
-printf("#################\n\r");          
-}//end while (1)
+
+        printf("############## 32/16 with error 16112016 ###\n\r");
+        //wait(2);
+    }//end while (1)
+    
+    
 }//end main()
\ No newline at end of file