single current only

Dependencies:   Watchdog mbed

Revision:
1:31e63b43238f
Parent:
0:0ba5f6ec8fa5
Child:
2:156af70e6f15
--- a/main.cpp	Tue Jan 24 13:32:57 2017 +0000
+++ b/main.cpp	Thu Jan 26 12:08:23 2017 +0000
@@ -1,5 +1,14 @@
-// Copyright (c) 2017 Smartcharge Ltd
-
+//////////////////////////////////////////
+//  Copyright (c) 2017 Smartcharge Ltd  //
+//////////////////////////////////////////
+//       >>CONVEX 2017rev1 build<<      //
+//  MAIN FEATURES:                      //
+//  - watchdog                          //
+//  - 3 sec reset button                //
+//  - 32/16 Amp Charging                //
+//  - 9V & 6V timers not applied        //
+//    in this revision, line 315&325    //
+//////////////////////////////////////////
 
 #include "mbed.h"
 #include "Watchdog.h"
@@ -41,7 +50,7 @@
 // *   Variables and constants to set the charging current    *
 // ************************************************************
 #define UPPER_CURRENT 32                                        // Sets the upper current value desired. 
-#define LOWER_CURRENT 16                                         // Sets the lower current value desired.
+#define LOWER_CURRENT 16                                        // Sets the lower current value desired.
 float pwm_duty_high = 1.0-((UPPER_CURRENT / 30.0) * 0.5);       // Calculates the pwm duty cycle for the desired upper current.
 float pwm_duty_low = 1.0-((LOWER_CURRENT / 30.0) * 0.5);        // Calculates the pwm duty cycle for the desired lower current.
 bool use_upper_current = false;
@@ -50,14 +59,14 @@
 // *      Variables and constants to allow state changes      *        
 // ************************************************************
 unsigned char control_pilot;
-#define PILOT_NOK   0                                           // Error state.               
-#define PILOT_12V   1                                           // Standby state.
-#define PILOT_9V    2                                           // Vehicle detection state.
-#define PILOT_6V    3                                           // Charging state.
-#define PILOT_DIODE 4                                           // Charging state with ventilation (not currently implemented).
-#define PILOT_RESET 5                                           // Reset state.
-#define PWM_CHANGE  6                                           // New state added to allow change in PWM duty cycle and charging current.
-
+#define PILOT_NOK     0                                         // Error state.               
+#define PILOT_12V     1                                         // Standby state.
+#define PILOT_9V      2                                         // Vehicle detection state.
+#define PILOT_6V      3                                         // Charging state.
+#define PILOT_DIODE   4                                         // Charging state with ventilation (not currently implemented).
+#define PILOT_RESET   5                                         // Reset state.
+#define PWM_CHANGE    6                                         // New state added to allow change in PWM duty cycle and charging current.
+#define PILOT_START_AGAIN 7                                         // Restart charger if stuck in stateB - 9V for defined amount of time.
 // ************************************************************
 // *                 Digital out definitions                  *                               
 // ************************************************************
@@ -75,7 +84,7 @@
 Serial pc(USBTX, USBRX);                                        // Serial output to PC.
 int TESTCOUNTER = 0;                                            // Variable to count number of cycles of main loop. Used to determine when to switch the pwm in this test version.
 
-
+int stateB_COUNTER = 0;                                         // Variable to count number of cycles of main loop. Used to reset charger from state B - 9V to state A and re-initiate charging.
 // ************************************************************
 // *   New Acquisition Routine for Capturing CP Signal Data   *                                    
 // ************************************************************
@@ -278,13 +287,16 @@
                 control_pilot = PILOT_12V;
                 reset_charger = false;
             }
+            if (cp_check_voltage(12) == false && cp_check_voltage(-12) == false)control_pilot = PILOT_NOK;  // voltage not at 12 or -12, error accured
         }
         
         if (cable_connected == true)
         {
+            if (cp_check_voltage(12) == true) control_pilot = PILOT_12V;
             if (cp_check_voltage(9) == true) control_pilot = PILOT_9V;
             if (cp_check_voltage(6) == true) control_pilot = PILOT_6V;
             if (reset_charger == true) control_pilot = PILOT_RESET;
+            //if (cp_check_voltage(9) == false && cp_check_voltage(6) == false && cp_check_voltage(12) == false && reset_charger == false )control_pilot = PILOT_NOK;   // voltage not at expected values, error accured
         }
 // ************************************************************
 // *        Switching PWM Cycle & TEST Counter Timer          *                                                                                                    
@@ -298,12 +310,13 @@
 // ************************************************************ 
  
 // ************************************************************
-// *            PWM cycle basen on cable instered             *                                                                                                    
+// *            PWM cycle based on cable instered             *                                                                                                    
 // ************************************************************  
         if (cable_32A == false) pwm_duty_cycle = pwm_duty_low;
         if (cable_32A == true) pwm_duty_cycle = pwm_duty_high;
- 
- 
+
+        //if (stateB_COUNTER > 3600) control_pilot = PILOT_START_AGAIN;   // Each cycle takes approximately 1 second, so 3600 seconds is a change of pwm every hour or so.
+        //9V monitorin & time reser disabled for this version 
  
         switch(control_pilot) 
         {
@@ -314,10 +327,14 @@
                 green = 0;
                 blue = 1;
                 my_pwm = 0;
+                my_pwm.write(0);
                 pwm_state = false;
-                pc.printf("Charger in STATE A. \r\n");
+                pc.printf("Charger in STATE:------------------------------------------------ A. \r\n");
                 pc.printf("PILOT_12V - Pilot at 12 V. \r\n");
-                TESTCOUNTER = 0;
+                TESTCOUNTER =0;
+                pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
+                stateB_COUNTER =0;
+                pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
             break;
   
             case PILOT_9V:
@@ -332,12 +349,15 @@
                     my_pwm.period_us(1000);
                     my_pwm.pulsewidth_us(1000);
                     my_pwm.write(pwm_duty_cycle);
-                pwm_state = true;
+                    pwm_state = true;
                 }
-                pc.printf("PWM duty cycle is at: %.1f  %% \r\n", 100-pwm_duty_cycle*100);
-                pc.printf("Charger in STATE B. \r\n");
+                pc.printf("PWM duty cycle is at: ------------------------------------------- %.1f  %% \r\n",100-pwm_duty_cycle*100);
+                pc.printf("Charger in STATE:------------------------------------------------ B. \r\n");
                 pc.printf("PILOT_9V - Pilot at 9 V. \r\n");
-                TESTCOUNTER = 0;
+                TESTCOUNTER =0;
+                pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
+                //stateB_COUNTER+=1;
+                pc.printf("stateB_COUNTER timer: ------------------------------- %u seconds  \r\n", stateB_COUNTER);
             break;
                 
             case PILOT_6V:
@@ -355,21 +375,35 @@
                     pwm_state = true;
                 }
                 pc.printf("PWM duty cycle is at: %.1f  %% \r\n", 100-pwm_duty_cycle*100);
-                pc.printf("Charger in STATE C. \r\n");
+                pc.printf("Charger in STATE:------------------------------------------------ C. \r\n");
                 pc.printf("PILOT_6V - Pilot at 6 V. \r\n");
 //              TESTCOUNTER+=1;
 //              * TESTCOUNTER switched of
-                pc.printf("TESTCOUNTER timer: %u seconds  \r\n", TESTCOUNTER);
+                pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
+                stateB_COUNTER = 0;
+                pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
             break;
                    
             case PILOT_NOK:
+                contactor = 0;
+                relay = 0;
                 lock = 0;
+                my_pwm.period_ms(1);
+                my_pwm.pulsewidth_ms(1);
+                my_pwm.write(1);
+                pwm_state = false;
                 red = 1;
                 green = 0;
                 blue = 0;
+                wait(0.5); // 500 ms
+                red = 0; // LED is OFF
+                wait(0.2); // 200 ms
                 pc.printf("Error. \r\n");
-                pc.printf("PILOT_NOK - Pilot ERROR. \r\n");
-                TESTCOUNTER = 0;
+                pc.printf("PILOT_NOK:------------------------------------------------------- Pilot ERROR. \r\n");
+                TESTCOUNTER =0;
+                pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
+                stateB_COUNTER =0;
+                pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
             break;
             
             case PILOT_RESET:
@@ -384,11 +418,14 @@
                 my_pwm.write(1);
                 pwm_state = false;
                 pc.printf("RESET IMPLEMENTED. \r\n");
-                pc.printf("PILOT_RESET - Pilot at -12V. \r\n");
+                pc.printf("PILOT_RESET:----------------------------------------------------- Pilot at -12V. \r\n");
                 wait(0.5); // 500 ms
-                red = 0; // LED is OFF
+                blue = 0; // LED is OFF
                 wait(0.2); // 200 ms
-                TESTCOUNTER = 0;
+                TESTCOUNTER =0;
+                pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
+                stateB_COUNTER =0;
+                pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
                 use_upper_current = false;
             break;
             
@@ -399,7 +436,7 @@
                 green = 1;
                 blue = 1;
                 wait(0.1);
-                pc.printf("Charger changing PWM. \r\n");
+                pc.printf("----------------------------------------------------------------- Charger changing PWM. \r\n");
                 my_pwm.period_ms(1);
                 my_pwm.pulsewidth_ms(1);
                 my_pwm.write(1);
@@ -409,7 +446,10 @@
                 pc.printf("STOPPED PWM - Switching to +12 V. \r\n");
                 wait(1);
                 pwm_state = false;
-                TESTCOUNTER = 0;
+                TESTCOUNTER =0;
+                pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
+                stateB_COUNTER =0;
+                pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
                 if(use_upper_current == false) 
                 {
                    use_upper_current = true;
@@ -418,8 +458,31 @@
                 {
                     use_upper_current = false;
                 }
-            break;  
-        }        
+            break;
+          
+            case PILOT_START_AGAIN:
+                red = 1;
+                green = 0;
+                blue = 1;
+                wait(0.1);
+                pc.printf("Charger:--------------------------------------------------------- RESTARTING stat B - 9V. \r\n");
+                my_pwm.period_ms(1);
+                my_pwm.pulsewidth_ms(1);
+                my_pwm.write(1);
+                wait(1);
+                pc.printf("STOPPED PWM - Switching to -12 V. \r\n");
+                my_pwm = 0;
+                pc.printf("STOPPED PWM - Switching to +12 V. \r\n");
+                wait(1);
+                pwm_state = false;
+                TESTCOUNTER =0;
+                pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
+                stateB_COUNTER =0;
+                pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
+            break;
+        
+        }
+                
         pc.printf("#################\r\n");
         //wait(1);      // wait(); added to slow down the feed from nucleo for easier evaluation
     }