October 2017 - 32 & 16 A charger on nucleo 103RB

Dependencies:   Watchdog mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //////////////////////////////////////////
00002 //  Copyright (c) 2017 Smartcharge Ltd  //
00003 //////////////////////////////////////////
00004 //       >>CONVEX 2017rev1 build<<      //
00005 //  MAIN FEATURES:                      //
00006 //  - watchdog                          //
00007 //  - 3 sec reset button                //
00008 //  - 32/16 Amp Charging                //
00009 //  - 9V & 6V timers not applied        //
00010 //    in this revision, line 315&325    //
00011 //  - error when voltage out of range   //
00012 //    cp_check_voltage();               //
00013 //  - Relay switched to 6V supply to    //
00014 //    reduce it's temperature           //
00015 //////////////////////////////////////////
00016 
00017 #include "mbed.h"
00018 #include "Watchdog.h"
00019 
00020 // ************************************************************
00021 // *     Variables for capturing analog cp and pp values      *       
00022 // ************************************************************
00023 AnalogIn cp_value(A1);                                          //A1 – cp analog read.
00024 AnalogIn pp_value(A2);                                          //A2 - pp analog read.
00025 
00026 // ************************************************************
00027 // *  Variables and constants for new cp acquisition routine  *
00028 // ************************************************************
00029 #define NUMBER_OF_SAMPLES 5000                                  // Size of ADC sample series for cp signal (default = 5000).
00030 #define VOLTAGE_RENORMALISATION 4.5                             // Renormalisation constant to correct cp measured voltages (default = 4.5).
00031 #define VOLTAGE_THRESHOLD 4.0                                   // Threshold value for pwm edge detection (default = 4.0).
00032 float cp_voltage;                                               // Global variable to store the measured voltage of the cp signal.
00033 float cp_duty_cycle;                                            // Global variable to store the measured duty cycle of the cp signal.
00034 float cp_frequency;                                             // Global variable to store the the measured frequency of the cp signal.
00035 Timer cp_timer;                                                 // Timer used to determine the frequency of the cp signal.
00036 uint16_t cp_array[NUMBER_OF_SAMPLES];                           // Array to store ADC sample series for cp signal.
00037 
00038 // ************************************************************
00039 // *          Constant for voltage checking routine           *
00040 // ************************************************************
00041 #define ACCEPTABLE_VOLTAGE_RANGE 0.5                            // Sets the acceptable range of measured cp voltages (default 0.5, i.e. +/-0.5 V around value of 12, 9, 6V) 
00042 
00043 // ************************************************************
00044 // *          Timers and variables for reset button           *                 
00045 // ************************************************************
00046 InterruptIn button(D8);                                         // Interupt for button on pin D8.
00047 Timer button_timer;                                             // Timer used for reset button press.
00048 Timeout button_timeout;                                         // Timeout case for reset button press.
00049 bool reset_down = false;                                        // Flag used to determine whether reset button is held down.
00050 bool reset_charger = false;                                     // Flag used to determine whether charger is to be reset.
00051 #define RESET_SECONDS 2                                         // Define length of time in seconds reset button needs to be held down before reset registered (default 3s).
00052 
00053 
00054 // ************************************************************
00055 // *   Variables and constants to set the charging current    *
00056 // ************************************************************
00057 #define UPPER_CURRENT 32                                        // Sets the upper current value desired. 
00058 #define LOWER_CURRENT 16                                        // Sets the lower current value desired.
00059 float pwm_duty_high = 1.0-((UPPER_CURRENT / 30.0) * 0.5);       // Calculates the pwm duty cycle for the desired upper current.
00060 float pwm_duty_low = 1.0-((LOWER_CURRENT / 30.0) * 0.5);        // Calculates the pwm duty cycle for the desired lower current.
00061 bool use_upper_current = false;
00062 
00063 // ************************************************************
00064 // *      Variables and constants to allow state changes      *        
00065 // ************************************************************
00066 unsigned char control_pilot;
00067 #define PILOT_NOK     0                                         // Error state.               
00068 #define PILOT_12V     1                                         // Standby state.
00069 #define PILOT_9V      2                                         // Vehicle detection state.
00070 #define PILOT_6V      3                                         // Charging state.
00071 #define PILOT_DIODE   4                                         // Charging state with ventilation (not currently implemented).
00072 #define PILOT_RESET   5                                         // Reset state.
00073 #define PWM_CHANGE    6                                         // New state added to allow change in PWM duty cycle and charging current.
00074 #define PILOT_START_AGAIN 7                                         // Restart charger if stuck in stateB - 9V for defined amount of time.
00075 // ************************************************************
00076 // *                 Digital out definitions                  *                               
00077 // ************************************************************
00078 PwmOut my_pwm(D5);                                              // PWM out on pin D5.
00079 DigitalOut lock(D7);                                            // Cable lock on pin D7.
00080 //DigitalOut relay(D12);                                          // Relay on pin D12.
00081 DigitalOut contactor(D13);                                      // Contactor on pin D13.
00082 DigitalOut green(D9);                                           // Green LED on pin D9.                
00083 DigitalOut red(D10);                                            // Red LED on pin D10.
00084 DigitalOut blue(D11);                                           // Blue LED on pin D11.                
00085 
00086 // ************************************************************
00087 // *                    Serial connections                    *                                    
00088 // ************************************************************
00089 Serial pc(USBTX, USBRX);                                        // Serial output to PC.
00090 int TESTCOUNTER = 0;                                            // Variable to count number of cycles of main loop. Used to determine when to switch the pwm in this test version.
00091 
00092 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.
00093 Watchdog wd;
00094 
00095 // ************************************************************
00096 // *          Variables for relay swicthing                   *                 
00097 // ************************************************************
00098 Ticker relay_contactor;
00099 bool relay_active = false;                                      // Flag used to determine whether the relay is switched on or of.
00100 bool trigger = false;
00101 
00102 
00103 // ************************************************************
00104 // *   New Acquisition Routine for Capturing CP Signal Data   *                                    
00105 // ************************************************************
00106 void cp_acquire()
00107 {            
00108     int i;                                                      // Variable for loop counter.
00109     float sample_value_current = 0;                             // Stores the current cp value obtained by ADC (A1).
00110     float sample_value_previous = 0;                            // Stores the previous cp value obtained by ADC (A1).
00111     float peak_counter;                                         // Used to store the number of samples representing a peak of the pwm square wave.
00112     float trough_counter;                                       // Used to store the number of samples representing a trough of the pwm square wave.
00113     float voltage_average;                                      // Used to calculate the average peak voltage value.
00114     float thres_cross_rise;                                     // Used to store the number of times the pwm wave goes from low to high.
00115     float thres_cross_fall;                                     // Used to store the number of times the pwm wave goes from high to low. 
00116     float t;                                                    // Used to determine the time over which samples were acquired.
00117     
00118     cp_timer.start();                                           // Starts a timer before we begin sampling the cp signal.
00119     for (i = 0; i < NUMBER_OF_SAMPLES; i++)                     // Starts a loop to take a certain number of samples as defined in NUMBER_OF_SAMPLES.
00120     {
00121         wait_us(30);                                            // Waits 30 us. This sets the sample rate at approximately 33 KS/second.
00122         cp_array[i] = cp_value.read_u16();                      // Reads the ADC (A1) and stores the measured cp voltage as a 16 bit integer in cp_array.
00123     }
00124     cp_timer.stop();                                            // Stop the timer once the acqusition has finished.
00125     t = cp_timer.read_us();                                     // Read the timer value in microseconds and store the result in t. 
00126     t = t / 1000000.0;                                          // Divide t by 1000000 to convert from microseconds to seconds.
00127     cp_timer.reset();                                           // Reset the timer.
00128     
00129     peak_counter = 0;                                           // Set peak_counter to zero.  
00130     trough_counter = 0;                                         // Set trough_counter to zero.
00131     voltage_average = 0;                                        // Set voltage_average to zero. 
00132     thres_cross_rise = 0;                                       // Set thres_cross_rise to zero.
00133     thres_cross_fall = 0;                                       // Set thres_cross_fall to zero.
00134     
00135     // Having captured cp data, we now have to process each sample. This is done in a separate loop to maximize the ADC sampling rate.        
00136     for (i = 0; i < NUMBER_OF_SAMPLES; i++) 
00137     {
00138         // The cp data was stored in cp_array as a 16 bit integer. To convert this into a voltage we divide by 65535 (16 bits is 0 - 65535)
00139         // and multiply by 3.3 V. Because of the resistors and diode on the shield, we need to renormalise the values and scale them up
00140         // by a factor of 4.5 (VOLTAGE_RENORMALISATION).
00141         sample_value_current = (cp_array[i] * 3.3 * VOLTAGE_RENORMALISATION) / 65535.0;
00142         
00143                
00144         if (sample_value_current > VOLTAGE_THRESHOLD)           // We examine the cp voltage. If it is above the threshold then we assume it is at the peak of the pwm square wave.
00145         {
00146             peak_counter+=1;                                    // Add one to the peak_counter.
00147             voltage_average+=sample_value_current;              // Add the cp_voltage to a running total (voltage_average) so we can work out the average voltage later.
00148         }
00149         else
00150         {
00151             trough_counter+=1;                                  // If the cp voltage is less than the threshold then we assume it is at the trough of the pwm square wave and increment trough_counter.
00152         }
00153         
00154                 
00155         if (i > 0)                                              // If we've already processed the first sample then ...                                                  
00156         {
00157             if (sample_value_current > VOLTAGE_THRESHOLD && sample_value_previous < VOLTAGE_THRESHOLD)          // ... we check if the cp voltage we're looking at is above the threshold and if the previous cp voltage
00158                                                                                                                 // is below the threshold. If this is the case then we've detected the rising edge of the pwm square wave.
00159             {
00160                 thres_cross_rise+=1;                                                                            // We increment thres_cross_rise if this is the case.
00161             }
00162             if (sample_value_current < VOLTAGE_THRESHOLD && sample_value_previous > VOLTAGE_THRESHOLD)          // Alternatively, if the cp voltage we're looking at is below the theshold and the previous cp voltage 
00163                                                                                                                 // is above the threshold then we've detected the falling edge of the pwm square wave.
00164             {
00165                 thres_cross_fall+=1;                                                                            // We increment thres_cross_fall is this is the case.
00166             }
00167         }                          
00168         
00169         sample_value_previous = sample_value_current;                                                           // Before we proces the next sample, we copy the current value into the previous value.
00170     } 
00171     
00172     
00173     if(peak_counter == 0)                                       // If, having processed each sample, the peak_counter is still zero, then every cp voltage we acquired was less than the threshold ...
00174     {
00175         cp_voltage = -12.0;                                     // ... which implies that the cp is not at 6, 9, or 12V. In the current implementation, that means the cp is actually at -12 V.   
00176     }
00177     else                                                        // On the other hand, if the peak_counter is greater than 0, then some (pwm is on) or all (DC, pwm is off) of the values were greater than the threshold ...
00178     {
00179         cp_voltage = voltage_average / peak_counter;            // ... so determine the cp voltage by taking the running total (voltage_average) and dividing it by peak_counter.               
00180     }        
00181     
00182     cp_duty_cycle = peak_counter / NUMBER_OF_SAMPLES;           // The duty cycle is the number of peak samples of the pwm square waves divided by the total number of samples ... 
00183     cp_duty_cycle = cp_duty_cycle * 100.0;                      // ... but we need to convert it into a percentage.
00184     cp_frequency = ((thres_cross_rise + thres_cross_fall) / 2.0) / t;           // The frequency of the cp signal is the total number of crossings divided by 2, divided by the time. 
00185     
00186     pc.printf("CP Measured Peak/DC Voltage (V): %f \r\n", cp_voltage);  
00187     pc.printf("CP Measured Duty Cycle (%%): %f \r\n", cp_duty_cycle);
00188     pc.printf("CP Measured Frequency (Hz): %f \r\n", cp_frequency);
00189 }
00190 
00191 
00192 // ************************************************************
00193 // *             Routine for Checking CP Voltages             *                                                                        
00194 // ************************************************************
00195 bool cp_check_voltage (float v)                                 // Function accepts a voltage value (eg. 12V, 9V, 6V) ...
00196 {
00197     bool voltage_in_range = false;                              // ... and initially sets a flag to false.
00198     
00199     // If the measured cp voltage is within a range of +/- ACCEPTABLE_VOLTAGE_RANGE around the 
00200     // value (12V, 9V, 6V) then we change the flag state to true.
00201     if (cp_voltage < (v + ACCEPTABLE_VOLTAGE_RANGE) && cp_voltage > (v - ACCEPTABLE_VOLTAGE_RANGE)) voltage_in_range = true;
00202                             
00203     return voltage_in_range;                                     // The function then returns the value of the flag state.
00204 }
00205 
00206 // ************************************************************
00207 // *         Routines for handling reset button press         *                                                
00208 // ************************************************************
00209 void button_timed_out() 
00210 {
00211     reset_charger = true;
00212     pc.printf("Reset button pressed for more than 3 sec! Charger reset! \r\n");
00213 }
00214  
00215 void reset_pressed() 
00216 {
00217     pc.printf("Reset button pressed ... starting timer. \r\n");
00218     button_timer.stop();
00219     button_timer.reset();
00220     button_timer.start();
00221     reset_down = true;   
00222     button_timeout.attach(&button_timed_out, RESET_SECONDS);   
00223 }
00224     
00225 void reset_released() 
00226 {
00227     int elapsed_seconds;    
00228     pc.printf("Reset button released. \r\n");           
00229     elapsed_seconds = button_timer.read();
00230     button_timer.stop();
00231     button_timer.reset();
00232     if (elapsed_seconds > RESET_SECONDS) 
00233     {
00234         reset_charger = true;
00235         pc.printf("Reset button was pressed for more than 3 sec! \r\n");
00236     }
00237     else
00238     {
00239         pc.printf("Reset button released before 3 seconds were up. \r\n");
00240     }
00241     pc.printf("Detach the timeout and setup for the next time.\r\n");
00242     pc.printf("%u \r\n", elapsed_seconds);
00243     button_timeout.detach();
00244 }
00245 
00246 // ************************************************************
00247 // *  Routines for resering the relay / temperature control   *                                                
00248 // ************************************************************
00249 
00250 void flip (void)
00251 {
00252     if(relay_active == true)
00253     {
00254         contactor = !contactor;
00255     }
00256     else
00257     {
00258         if(trigger == true)
00259         {
00260             contactor = 1;
00261         }
00262         else
00263         {
00264             contactor = 0;
00265         }
00266     }
00267 }
00268 
00269 
00270 // ************************************************************
00271 // *                           Main                           *                                                                                                    
00272 // ************************************************************
00273 int main() 
00274 {        
00275     button.fall(&reset_pressed);                                // Attach interupt to button when pressed.
00276     button.rise(&reset_released);                               // Attach interupt to button when released.
00277     relay_contactor.attach(&flip, 0.01);                        // Attach switch to relay every milisecond.
00278     
00279     
00280     if (wd.WatchdogCausedReset())
00281        printf("Watchdog caused reset.\r\n");    
00282 
00283     wd.Configure(5.0);
00284     
00285     float reading_pp;                                           // Create variable to store pp reading.
00286     bool cable_32A = false;                                     // Create boolean to flag whether a 32 or 16A cable is being used. Default is 16A cable (cable_32A = false).
00287     bool cable_connected = false;                               // Create boolean to flag whether a cable is attached.
00288     bool pwm_state = false;                                     // Create boolean to flag current state of pwm (whether it is on or off). 
00289     float pwm_duty_cycle;                                       // Create float to store the current pwm duty cycle.
00290     
00291     while(true)                                                 // Start of process loop. 
00292     {
00293         
00294         wd.Service();                                           // Service the Watchdog so it does not cause a system reset.
00295         
00296         // check the cable using pp value
00297         reading_pp = pp_value.read();                           // Read pp value and ...
00298         reading_pp = reading_pp * 3300;                         // ... multiply it by 3300 to convert to mV.
00299         
00300         if(reading_pp > 3200)                                   // If the pp value is 3.3 V (greater than 3200 mV) then ... 
00301         {
00302             cable_connected = false;                            // ... the cable *isn't* connected to charger ...
00303             pc.printf("Cable not connected. \r\n");
00304         }
00305         else
00306         {
00307             cable_connected = true;                             // ... otherwise the cable *is* connected to charger. 
00308             pc.printf("Cable connected. \r\n");
00309         }
00310         
00311         if(reading_pp > 200 && reading_pp < 300)                // If the pp reading is between 200 and 300 mV then ...
00312         {
00313             cable_32A = false;                                  // ... a 16A cable is being used.
00314             pc.printf("16A cable detected. \r\n");
00315         }
00316         
00317         if(reading_pp > 0 && reading_pp <100)                   // If the pp reading if between 0 and 100 mV then ...
00318         {
00319             cable_32A = true;                                   // ... a 32A cable is being used.
00320             pc.printf("32A cable detected. \r\n");  
00321         }
00322         
00323         cp_acquire();                                           // Call the new acquisition routine (replaces the moving average in previous versions).        
00324 
00325         if (cable_connected == false)
00326         {
00327             if (cp_check_voltage(12) == true) control_pilot = PILOT_12V;     
00328             
00329             if (cp_check_voltage(-12) == true)
00330             {
00331                 control_pilot = PILOT_12V;
00332                 reset_charger = false;
00333             }
00334             //if (cp_check_voltage(12) == false && cp_check_voltage(-12) == false)control_pilot = PILOT_NOK;  // voltage not at 12 or -12, error accured
00335         }
00336         
00337         if (cable_connected == true)
00338         {
00339             if (cp_check_voltage(12) == true) control_pilot = PILOT_12V;
00340             if (cp_check_voltage(9) == true) control_pilot = PILOT_9V;
00341             if (cp_check_voltage(6) == true) control_pilot = PILOT_6V;
00342             if (reset_charger == true) control_pilot = PILOT_RESET;
00343             //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
00344         }
00345        
00346 // ************************************************************
00347 // *        Switching PWM Cycle & TEST Counter Timer          *                                                                                                    
00348 // ************************************************************        
00349 //      if (use_upper_current == false) pwm_duty_cycle = pwm_duty_low;
00350 //      if (use_upper_current == true) pwm_duty_cycle = pwm_duty_high;
00351 //        
00352 //      if (TESTCOUNTER > 1800) control_pilot = PWM_CHANGE; // Each cycle takes approximately 1 second, so 1800 seconds is a change of pwm every 30 mins or so.              
00353 //      * TESTERCOUNTER monitoring is switched of for the Smartcharge Home+ charger, PWN cycle based on a cable inserted      
00354 //  
00355 // ************************************************************ 
00356  
00357 // ************************************************************
00358 // *            PWM cycle based on cable instered             *                                                                                                    
00359 // ************************************************************  
00360         if (cable_32A == false)
00361         {
00362             pwm_duty_cycle = pwm_duty_low;
00363             pc.printf("------------------------------------------------Charger mode 16A. \r\n");
00364         }
00365         if(cable_32A == true)
00366         {
00367             pwm_duty_cycle = pwm_duty_high;
00368             pc.printf("------------------------------------------------Charger mode 32A. \r\n");
00369         }
00370         
00371         //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.
00372         //9V monitorin & time reser disabled for this version 
00373  
00374         switch(control_pilot) 
00375         {
00376             case PILOT_12V:
00377                 relay_active = false;
00378                 trigger = false;
00379                 //contactor = 0;
00380                 lock = 0;
00381                 red = 0;
00382                 green = 0;
00383                 blue = 1;
00384                 my_pwm = 0;
00385                 my_pwm.write(0);
00386                 pwm_state = false;
00387                 pc.printf("Charger in STATE:------------------------------------------------ A. \r\n");
00388                 //pc.printf("PILOT_12V - Pilot at 12 V. \r\n");
00389                 TESTCOUNTER =0;
00390                 //pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
00391                 stateB_COUNTER =0;
00392                 //pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
00393             break;
00394   
00395             case PILOT_9V:
00396                 relay_active = false;
00397                 trigger = false;
00398                 //contactor = 0;
00399                 //relay=0;
00400                 lock = 1;
00401                 red = 1;
00402                 green = 1;
00403                 blue = 0;
00404                 if (pwm_state == false)
00405                 {
00406                     my_pwm.period_us(1000);
00407                     my_pwm.pulsewidth_us(1000);
00408                     my_pwm.write(pwm_duty_cycle);
00409                     pwm_state = true;
00410                 }
00411                 pc.printf("PWM duty cycle is at: ------------------------------------------- %.1f  %% \r\n",100-pwm_duty_cycle*100);
00412                 pc.printf("Charger in STATE:------------------------------------------------ B. \r\n");
00413                 //pc.printf("PILOT_9V - Pilot at 9 V. \r\n");
00414                 TESTCOUNTER =0;
00415                 //pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
00416                 //stateB_COUNTER+=1;
00417                 //pc.printf("stateB_COUNTER timer: ------------------------------- %u seconds  \r\n", stateB_COUNTER);
00418             break;
00419                 
00420             case PILOT_6V:
00421                 if (relay_active == false)
00422                 {
00423                     trigger = true;
00424                 }
00425                 //contactor = 1;
00426                 //relay = 1;
00427                 lock = 1;
00428                 red = 0;
00429                 green = 1;
00430                 blue = 0;
00431                 if (pwm_state == false)
00432                 {
00433                     my_pwm.period_us(1000);
00434                     my_pwm.pulsewidth_us(1000);
00435                     my_pwm.write(pwm_duty_cycle);
00436                     pwm_state = true;
00437                 }
00438                 pc.printf("PWM duty cycle is at: ------------------------------------------- %.1f  %% \r\n", 100-pwm_duty_cycle*100);
00439                 pc.printf("Charger in STATE:------------------------------------------------ C. \r\n");
00440                 //pc.printf("PILOT_6V - Pilot at 6 V. \r\n");
00441 //              TESTCOUNTER+=1;
00442 //              * TESTCOUNTER switched of
00443                 //pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
00444                 stateB_COUNTER = 0;
00445                 //pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
00446                 relay_active = true;
00447             break;
00448                    
00449             case PILOT_NOK:
00450                 relay_active = false;
00451                 trigger = false;
00452                 //contactor = 0;
00453                 //relay = 0;
00454                 lock = 0;
00455                 my_pwm.period_ms(1);
00456                 my_pwm.pulsewidth_ms(1);
00457                 my_pwm.write(1);
00458                 pwm_state = false;
00459                 red = 1;
00460                 green = 0;
00461                 blue = 0;
00462                 wait(0.5); // 500 ms
00463                 red = 0; // LED is OFF
00464                 wait(0.2); // 200 ms
00465                 pc.printf("Error. \r\n");
00466                 pc.printf("PILOT_NOK:------------------------------------------------------- Pilot ERROR. \r\n");
00467                 TESTCOUNTER =0;
00468                 //pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
00469                 stateB_COUNTER =0;
00470                 //pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
00471             break;
00472             
00473             case PILOT_RESET:
00474                 relay_active = false;
00475                 trigger = false;
00476                 //contactor = 0;
00477                 //relay = 0;
00478                 lock = 0;
00479                 red = 0;
00480                 green = 0;
00481                 blue = 1;
00482                 my_pwm.period_ms(1);
00483                 my_pwm.pulsewidth_ms(1);
00484                 my_pwm.write(1);
00485                 pwm_state = false;
00486                 pc.printf("RESET IMPLEMENTED. \r\n");
00487                 pc.printf("PILOT_RESET:----------------------------------------------------- Pilot at -12V. \r\n");
00488                 wait(0.5); // 500 ms
00489                 blue = 0; // LED is OFF
00490                 wait(0.2); // 200 ms
00491                 TESTCOUNTER =0;
00492                 //pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
00493                 stateB_COUNTER =0;
00494                 //pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
00495                 use_upper_current = false;
00496             break;
00497             
00498             case PWM_CHANGE:
00499                 lock = 1;
00500                 contactor = 0;
00501                 red = 1;
00502                 green = 1;
00503                 blue = 1;
00504                 wait(0.1);
00505                 pc.printf("----------------------------------------------------------------- Charger changing PWM. \r\n");
00506                 my_pwm.period_ms(1);
00507                 my_pwm.pulsewidth_ms(1);
00508                 my_pwm.write(1);
00509                 wait(1);
00510                 pc.printf("STOPPED PWM - Switching to -12 V. \r\n");
00511                 my_pwm = 0;
00512                 pc.printf("STOPPED PWM - Switching to +12 V. \r\n");
00513                 wait(1);
00514                 pwm_state = false;
00515                 TESTCOUNTER =0;
00516                 //pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
00517                 stateB_COUNTER =0;
00518                 //pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
00519                 if(use_upper_current == false) 
00520                 {
00521                    use_upper_current = true;
00522                 }
00523                 else
00524                 {
00525                     use_upper_current = false;
00526                 }
00527             break;
00528           
00529             case PILOT_START_AGAIN:
00530                 red = 1;
00531                 green = 0;
00532                 blue = 1;
00533                 wait(0.1);
00534                 pc.printf("Charger:--------------------------------------------------------- RESTARTING stat B - 9V. \r\n");
00535                 my_pwm.period_ms(1);
00536                 my_pwm.pulsewidth_ms(1);
00537                 my_pwm.write(1);
00538                 wait(1);
00539                 pc.printf("STOPPED PWM - Switching to -12 V. \r\n");
00540                 my_pwm = 0;
00541                 pc.printf("STOPPED PWM - Switching to +12 V. \r\n");
00542                 wait(1);
00543                 pwm_state = false;
00544                 TESTCOUNTER =0;
00545                 //pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds  \r\n", TESTCOUNTER);
00546                 stateB_COUNTER =0;
00547                 //pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds  \r\n", stateB_COUNTER);
00548             break;
00549         
00550         }
00551                 
00552         pc.printf("#################\r\n");
00553         //wait(1);      // wait(); added to slow down the feed from nucleo for easier evaluation
00554     }
00555 }