Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:6547599b54a0, 2017-02-06 (annotated)
- Committer:
- magdamcn
- Date:
- Mon Feb 06 15:24:36 2017 +0000
- Revision:
- 4:6547599b54a0
- Parent:
- 3:743125ca6a46
- Child:
- 5:8e64f59100e0
wd
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| magdamcn | 1:31e63b43238f | 1 | ////////////////////////////////////////// |
| magdamcn | 1:31e63b43238f | 2 | // Copyright (c) 2017 Smartcharge Ltd // |
| magdamcn | 1:31e63b43238f | 3 | ////////////////////////////////////////// |
| magdamcn | 1:31e63b43238f | 4 | // >>CONVEX 2017rev1 build<< // |
| magdamcn | 1:31e63b43238f | 5 | // MAIN FEATURES: // |
| magdamcn | 1:31e63b43238f | 6 | // - watchdog // |
| magdamcn | 1:31e63b43238f | 7 | // - 3 sec reset button // |
| magdamcn | 1:31e63b43238f | 8 | // - 32/16 Amp Charging // |
| magdamcn | 1:31e63b43238f | 9 | // - 9V & 6V timers not applied // |
| magdamcn | 1:31e63b43238f | 10 | // in this revision, line 315&325 // |
| magdamcn | 2:156af70e6f15 | 11 | // - error when voltage out of range // |
| magdamcn | 2:156af70e6f15 | 12 | // cp_check_voltage(); // |
| magdamcn | 1:31e63b43238f | 13 | ////////////////////////////////////////// |
| magdamcn | 0:0ba5f6ec8fa5 | 14 | |
| magdamcn | 0:0ba5f6ec8fa5 | 15 | #include "mbed.h" |
| magdamcn | 0:0ba5f6ec8fa5 | 16 | #include "Watchdog.h" |
| magdamcn | 0:0ba5f6ec8fa5 | 17 | |
| magdamcn | 0:0ba5f6ec8fa5 | 18 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 19 | // * Variables for capturing analog cp and pp values * |
| magdamcn | 0:0ba5f6ec8fa5 | 20 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 21 | AnalogIn cp_value(A1); //A1 – cp analog read. |
| magdamcn | 0:0ba5f6ec8fa5 | 22 | AnalogIn pp_value(A2); //A2 - pp analog read. |
| magdamcn | 0:0ba5f6ec8fa5 | 23 | |
| magdamcn | 0:0ba5f6ec8fa5 | 24 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 25 | // * Variables and constants for new cp acquisition routine * |
| magdamcn | 0:0ba5f6ec8fa5 | 26 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 27 | #define NUMBER_OF_SAMPLES 5000 // Size of ADC sample series for cp signal (default = 5000). |
| magdamcn | 0:0ba5f6ec8fa5 | 28 | #define VOLTAGE_RENORMALISATION 4.5 // Renormalisation constant to correct cp measured voltages (default = 4.5). |
| magdamcn | 0:0ba5f6ec8fa5 | 29 | #define VOLTAGE_THRESHOLD 4.0 // Threshold value for pwm edge detection (default = 4.0). |
| magdamcn | 0:0ba5f6ec8fa5 | 30 | float cp_voltage; // Global variable to store the measured voltage of the cp signal. |
| magdamcn | 0:0ba5f6ec8fa5 | 31 | float cp_duty_cycle; // Global variable to store the measured duty cycle of the cp signal. |
| magdamcn | 0:0ba5f6ec8fa5 | 32 | float cp_frequency; // Global variable to store the the measured frequency of the cp signal. |
| magdamcn | 0:0ba5f6ec8fa5 | 33 | Timer cp_timer; // Timer used to determine the frequency of the cp signal. |
| magdamcn | 0:0ba5f6ec8fa5 | 34 | uint16_t cp_array[NUMBER_OF_SAMPLES]; // Array to store ADC sample series for cp signal. |
| magdamcn | 0:0ba5f6ec8fa5 | 35 | |
| magdamcn | 0:0ba5f6ec8fa5 | 36 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 37 | // * Constant for voltage checking routine * |
| magdamcn | 0:0ba5f6ec8fa5 | 38 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 39 | #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) |
| magdamcn | 0:0ba5f6ec8fa5 | 40 | |
| magdamcn | 0:0ba5f6ec8fa5 | 41 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 42 | // * Timers and variables for reset button * |
| magdamcn | 0:0ba5f6ec8fa5 | 43 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 44 | InterruptIn button(D8); // Interupt for button on pin D8. |
| magdamcn | 0:0ba5f6ec8fa5 | 45 | Timer button_timer; // Timer used for reset button press. |
| magdamcn | 0:0ba5f6ec8fa5 | 46 | Timeout button_timeout; // Timeout case for reset button press. |
| magdamcn | 0:0ba5f6ec8fa5 | 47 | bool reset_down = false; // Flag used to determine whether reset button is held down. |
| magdamcn | 0:0ba5f6ec8fa5 | 48 | bool reset_charger = false; // Flag used to determine whether charger is to be reset. |
| magdamcn | 0:0ba5f6ec8fa5 | 49 | #define RESET_SECONDS 2 // Define length of time in seconds reset button needs to be held down before reset registered (default 3s). |
| magdamcn | 0:0ba5f6ec8fa5 | 50 | |
| magdamcn | 0:0ba5f6ec8fa5 | 51 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 52 | // * Variables and constants to set the charging current * |
| magdamcn | 0:0ba5f6ec8fa5 | 53 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 54 | #define UPPER_CURRENT 32 // Sets the upper current value desired. |
| magdamcn | 1:31e63b43238f | 55 | #define LOWER_CURRENT 16 // Sets the lower current value desired. |
| magdamcn | 0:0ba5f6ec8fa5 | 56 | float pwm_duty_high = 1.0-((UPPER_CURRENT / 30.0) * 0.5); // Calculates the pwm duty cycle for the desired upper current. |
| magdamcn | 0:0ba5f6ec8fa5 | 57 | float pwm_duty_low = 1.0-((LOWER_CURRENT / 30.0) * 0.5); // Calculates the pwm duty cycle for the desired lower current. |
| magdamcn | 0:0ba5f6ec8fa5 | 58 | bool use_upper_current = false; |
| magdamcn | 0:0ba5f6ec8fa5 | 59 | |
| magdamcn | 0:0ba5f6ec8fa5 | 60 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 61 | // * Variables and constants to allow state changes * |
| magdamcn | 0:0ba5f6ec8fa5 | 62 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 63 | unsigned char control_pilot; |
| magdamcn | 1:31e63b43238f | 64 | #define PILOT_NOK 0 // Error state. |
| magdamcn | 1:31e63b43238f | 65 | #define PILOT_12V 1 // Standby state. |
| magdamcn | 1:31e63b43238f | 66 | #define PILOT_9V 2 // Vehicle detection state. |
| magdamcn | 1:31e63b43238f | 67 | #define PILOT_6V 3 // Charging state. |
| magdamcn | 1:31e63b43238f | 68 | #define PILOT_DIODE 4 // Charging state with ventilation (not currently implemented). |
| magdamcn | 1:31e63b43238f | 69 | #define PILOT_RESET 5 // Reset state. |
| magdamcn | 1:31e63b43238f | 70 | #define PWM_CHANGE 6 // New state added to allow change in PWM duty cycle and charging current. |
| magdamcn | 1:31e63b43238f | 71 | #define PILOT_START_AGAIN 7 // Restart charger if stuck in stateB - 9V for defined amount of time. |
| magdamcn | 0:0ba5f6ec8fa5 | 72 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 73 | // * Digital out definitions * |
| magdamcn | 0:0ba5f6ec8fa5 | 74 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 75 | PwmOut my_pwm(D5); // PWM out on pin D5. |
| magdamcn | 0:0ba5f6ec8fa5 | 76 | DigitalOut lock(D7); // Cable lock on pin D7. |
| magdamcn | 0:0ba5f6ec8fa5 | 77 | DigitalOut relay(D12); // Relay on pin D12. |
| magdamcn | 0:0ba5f6ec8fa5 | 78 | DigitalOut contactor(D13); // Contactor on pin D13. |
| magdamcn | 0:0ba5f6ec8fa5 | 79 | DigitalOut green(D9); // Green LED on pin D9. |
| magdamcn | 0:0ba5f6ec8fa5 | 80 | DigitalOut red(D10); // Red LED on pin D10. |
| magdamcn | 0:0ba5f6ec8fa5 | 81 | DigitalOut blue(D11); // Blue LED on pin D11. |
| magdamcn | 0:0ba5f6ec8fa5 | 82 | |
| magdamcn | 0:0ba5f6ec8fa5 | 83 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 84 | // * Serial connections * |
| magdamcn | 0:0ba5f6ec8fa5 | 85 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 86 | Serial pc(USBTX, USBRX); // Serial output to PC. |
| magdamcn | 0:0ba5f6ec8fa5 | 87 | int TESTCOUNTER = 0; // Variable to count number of cycles of main loop. Used to determine when to switch the pwm in this test version. |
| magdamcn | 0:0ba5f6ec8fa5 | 88 | |
| magdamcn | 1:31e63b43238f | 89 | 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. |
| magdamcn | 4:6547599b54a0 | 90 | Watchdog wd; |
| magdamcn | 4:6547599b54a0 | 91 | |
| magdamcn | 4:6547599b54a0 | 92 | |
| magdamcn | 2:156af70e6f15 | 93 | |
| magdamcn | 0:0ba5f6ec8fa5 | 94 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 95 | // * New Acquisition Routine for Capturing CP Signal Data * |
| magdamcn | 0:0ba5f6ec8fa5 | 96 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 97 | void cp_acquire() |
| magdamcn | 0:0ba5f6ec8fa5 | 98 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 99 | int i; // Variable for loop counter. |
| magdamcn | 0:0ba5f6ec8fa5 | 100 | float sample_value_current = 0; // Stores the current cp value obtained by ADC (A1). |
| magdamcn | 0:0ba5f6ec8fa5 | 101 | float sample_value_previous = 0; // Stores the previous cp value obtained by ADC (A1). |
| magdamcn | 0:0ba5f6ec8fa5 | 102 | float peak_counter; // Used to store the number of samples representing a peak of the pwm square wave. |
| magdamcn | 0:0ba5f6ec8fa5 | 103 | float trough_counter; // Used to store the number of samples representing a trough of the pwm square wave. |
| magdamcn | 0:0ba5f6ec8fa5 | 104 | float voltage_average; // Used to calculate the average peak voltage value. |
| magdamcn | 0:0ba5f6ec8fa5 | 105 | float thres_cross_rise; // Used to store the number of times the pwm wave goes from low to high. |
| magdamcn | 0:0ba5f6ec8fa5 | 106 | float thres_cross_fall; // Used to store the number of times the pwm wave goes from high to low. |
| magdamcn | 0:0ba5f6ec8fa5 | 107 | float t; // Used to determine the time over which samples were acquired. |
| magdamcn | 0:0ba5f6ec8fa5 | 108 | |
| magdamcn | 0:0ba5f6ec8fa5 | 109 | cp_timer.start(); // Starts a timer before we begin sampling the cp signal. |
| magdamcn | 0:0ba5f6ec8fa5 | 110 | for (i = 0; i < NUMBER_OF_SAMPLES; i++) // Starts a loop to take a certain number of samples as defined in NUMBER_OF_SAMPLES. |
| magdamcn | 0:0ba5f6ec8fa5 | 111 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 112 | wait_us(30); // Waits 30 us. This sets the sample rate at approximately 33 KS/second. |
| magdamcn | 0:0ba5f6ec8fa5 | 113 | 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. |
| magdamcn | 0:0ba5f6ec8fa5 | 114 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 115 | cp_timer.stop(); // Stop the timer once the acqusition has finished. |
| magdamcn | 0:0ba5f6ec8fa5 | 116 | t = cp_timer.read_us(); // Read the timer value in microseconds and store the result in t. |
| magdamcn | 0:0ba5f6ec8fa5 | 117 | t = t / 1000000.0; // Divide t by 1000000 to convert from microseconds to seconds. |
| magdamcn | 0:0ba5f6ec8fa5 | 118 | cp_timer.reset(); // Reset the timer. |
| magdamcn | 0:0ba5f6ec8fa5 | 119 | |
| magdamcn | 0:0ba5f6ec8fa5 | 120 | peak_counter = 0; // Set peak_counter to zero. |
| magdamcn | 0:0ba5f6ec8fa5 | 121 | trough_counter = 0; // Set trough_counter to zero. |
| magdamcn | 0:0ba5f6ec8fa5 | 122 | voltage_average = 0; // Set voltage_average to zero. |
| magdamcn | 0:0ba5f6ec8fa5 | 123 | thres_cross_rise = 0; // Set thres_cross_rise to zero. |
| magdamcn | 0:0ba5f6ec8fa5 | 124 | thres_cross_fall = 0; // Set thres_cross_fall to zero. |
| magdamcn | 0:0ba5f6ec8fa5 | 125 | |
| magdamcn | 0:0ba5f6ec8fa5 | 126 | // Having captured cp data, we now have to process each sample. This is done in a separate loop to maximize the ADC sampling rate. |
| magdamcn | 0:0ba5f6ec8fa5 | 127 | for (i = 0; i < NUMBER_OF_SAMPLES; i++) |
| magdamcn | 0:0ba5f6ec8fa5 | 128 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 129 | // 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) |
| magdamcn | 0:0ba5f6ec8fa5 | 130 | // 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 |
| magdamcn | 0:0ba5f6ec8fa5 | 131 | // by a factor of 4.5 (VOLTAGE_RENORMALISATION). |
| magdamcn | 0:0ba5f6ec8fa5 | 132 | sample_value_current = (cp_array[i] * 3.3 * VOLTAGE_RENORMALISATION) / 65535.0; |
| magdamcn | 0:0ba5f6ec8fa5 | 133 | |
| magdamcn | 0:0ba5f6ec8fa5 | 134 | |
| magdamcn | 0:0ba5f6ec8fa5 | 135 | 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. |
| magdamcn | 0:0ba5f6ec8fa5 | 136 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 137 | peak_counter+=1; // Add one to the peak_counter. |
| magdamcn | 0:0ba5f6ec8fa5 | 138 | voltage_average+=sample_value_current; // Add the cp_voltage to a running total (voltage_average) so we can work out the average voltage later. |
| magdamcn | 0:0ba5f6ec8fa5 | 139 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 140 | else |
| magdamcn | 0:0ba5f6ec8fa5 | 141 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 142 | 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. |
| magdamcn | 0:0ba5f6ec8fa5 | 143 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 144 | |
| magdamcn | 0:0ba5f6ec8fa5 | 145 | |
| magdamcn | 0:0ba5f6ec8fa5 | 146 | if (i > 0) // If we've already processed the first sample then ... |
| magdamcn | 0:0ba5f6ec8fa5 | 147 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 148 | 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 |
| magdamcn | 0:0ba5f6ec8fa5 | 149 | // is below the threshold. If this is the case then we've detected the rising edge of the pwm square wave. |
| magdamcn | 0:0ba5f6ec8fa5 | 150 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 151 | thres_cross_rise+=1; // We increment thres_cross_rise if this is the case. |
| magdamcn | 0:0ba5f6ec8fa5 | 152 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 153 | 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 |
| magdamcn | 0:0ba5f6ec8fa5 | 154 | // is above the threshold then we've detected the falling edge of the pwm square wave. |
| magdamcn | 0:0ba5f6ec8fa5 | 155 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 156 | thres_cross_fall+=1; // We increment thres_cross_fall is this is the case. |
| magdamcn | 0:0ba5f6ec8fa5 | 157 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 158 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 159 | |
| magdamcn | 0:0ba5f6ec8fa5 | 160 | sample_value_previous = sample_value_current; // Before we proces the next sample, we copy the current value into the previous value. |
| magdamcn | 0:0ba5f6ec8fa5 | 161 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 162 | |
| magdamcn | 0:0ba5f6ec8fa5 | 163 | |
| magdamcn | 0:0ba5f6ec8fa5 | 164 | 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 ... |
| magdamcn | 0:0ba5f6ec8fa5 | 165 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 166 | 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. |
| magdamcn | 0:0ba5f6ec8fa5 | 167 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 168 | 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 ... |
| magdamcn | 0:0ba5f6ec8fa5 | 169 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 170 | cp_voltage = voltage_average / peak_counter; // ... so determine the cp voltage by taking the running total (voltage_average) and dividing it by peak_counter. |
| magdamcn | 0:0ba5f6ec8fa5 | 171 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 172 | |
| magdamcn | 0:0ba5f6ec8fa5 | 173 | 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 ... |
| magdamcn | 0:0ba5f6ec8fa5 | 174 | cp_duty_cycle = cp_duty_cycle * 100.0; // ... but we need to convert it into a percentage. |
| magdamcn | 0:0ba5f6ec8fa5 | 175 | 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. |
| magdamcn | 0:0ba5f6ec8fa5 | 176 | |
| magdamcn | 0:0ba5f6ec8fa5 | 177 | pc.printf("CP Measured Peak/DC Voltage (V): %f \r\n", cp_voltage); |
| magdamcn | 0:0ba5f6ec8fa5 | 178 | pc.printf("CP Measured Duty Cycle (%%): %f \r\n", cp_duty_cycle); |
| magdamcn | 0:0ba5f6ec8fa5 | 179 | pc.printf("CP Measured Frequency (Hz): %f \r\n", cp_frequency); |
| magdamcn | 0:0ba5f6ec8fa5 | 180 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 181 | |
| magdamcn | 0:0ba5f6ec8fa5 | 182 | |
| magdamcn | 0:0ba5f6ec8fa5 | 183 | |
| magdamcn | 0:0ba5f6ec8fa5 | 184 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 185 | // * Routines for handling reset button press * |
| magdamcn | 0:0ba5f6ec8fa5 | 186 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 187 | void button_timed_out() |
| magdamcn | 0:0ba5f6ec8fa5 | 188 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 189 | reset_charger = true; |
| magdamcn | 0:0ba5f6ec8fa5 | 190 | pc.printf("Reset button pressed for more than 3 sec! Charger reset! \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 191 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 192 | |
| magdamcn | 0:0ba5f6ec8fa5 | 193 | void reset_pressed() |
| magdamcn | 0:0ba5f6ec8fa5 | 194 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 195 | pc.printf("Reset button pressed ... starting timer. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 196 | button_timer.stop(); |
| magdamcn | 0:0ba5f6ec8fa5 | 197 | button_timer.reset(); |
| magdamcn | 0:0ba5f6ec8fa5 | 198 | button_timer.start(); |
| magdamcn | 0:0ba5f6ec8fa5 | 199 | reset_down = true; |
| magdamcn | 0:0ba5f6ec8fa5 | 200 | button_timeout.attach(&button_timed_out, RESET_SECONDS); |
| magdamcn | 0:0ba5f6ec8fa5 | 201 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 202 | |
| magdamcn | 0:0ba5f6ec8fa5 | 203 | void reset_released() |
| magdamcn | 0:0ba5f6ec8fa5 | 204 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 205 | int elapsed_seconds; |
| magdamcn | 0:0ba5f6ec8fa5 | 206 | pc.printf("Reset button released. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 207 | elapsed_seconds = button_timer.read(); |
| magdamcn | 0:0ba5f6ec8fa5 | 208 | button_timer.stop(); |
| magdamcn | 0:0ba5f6ec8fa5 | 209 | button_timer.reset(); |
| magdamcn | 0:0ba5f6ec8fa5 | 210 | if (elapsed_seconds > RESET_SECONDS) |
| magdamcn | 0:0ba5f6ec8fa5 | 211 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 212 | reset_charger = true; |
| magdamcn | 0:0ba5f6ec8fa5 | 213 | pc.printf("Reset button was pressed for more than 3 sec! \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 214 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 215 | else |
| magdamcn | 0:0ba5f6ec8fa5 | 216 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 217 | pc.printf("Reset button released before 3 seconds were up. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 218 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 219 | pc.printf("Detach the timeout and setup for the next time.\r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 220 | pc.printf("%u \r\n", elapsed_seconds); |
| magdamcn | 0:0ba5f6ec8fa5 | 221 | button_timeout.detach(); |
| magdamcn | 0:0ba5f6ec8fa5 | 222 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 223 | |
| magdamcn | 0:0ba5f6ec8fa5 | 224 | |
| magdamcn | 0:0ba5f6ec8fa5 | 225 | |
| magdamcn | 0:0ba5f6ec8fa5 | 226 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 227 | // * Routine for Checking CP Voltages * |
| magdamcn | 0:0ba5f6ec8fa5 | 228 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 229 | bool cp_check_voltage (float v) // Function accepts a voltage value (eg. 12V, 9V, 6V) ... |
| magdamcn | 0:0ba5f6ec8fa5 | 230 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 231 | bool voltage_in_range = false; // ... and initially sets a flag to false. |
| magdamcn | 0:0ba5f6ec8fa5 | 232 | |
| magdamcn | 0:0ba5f6ec8fa5 | 233 | // If the measured cp voltage is within a range of +/- ACCEPTABLE_VOLTAGE_RANGE around the |
| magdamcn | 0:0ba5f6ec8fa5 | 234 | // value (12V, 9V, 6V) then we change the flag state to true. |
| magdamcn | 3:743125ca6a46 | 235 | if (cp_voltage < (v + ACCEPTABLE_VOLTAGE_RANGE) && cp_voltage > (v - ACCEPTABLE_VOLTAGE_RANGE)) voltage_in_range = true; |
| magdamcn | 3:743125ca6a46 | 236 | |
| magdamcn | 3:743125ca6a46 | 237 | return voltage_in_range; // The function then returns the value of the flag state. |
| magdamcn | 0:0ba5f6ec8fa5 | 238 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 239 | |
| magdamcn | 0:0ba5f6ec8fa5 | 240 | |
| magdamcn | 0:0ba5f6ec8fa5 | 241 | |
| magdamcn | 0:0ba5f6ec8fa5 | 242 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 243 | // * Main * |
| magdamcn | 0:0ba5f6ec8fa5 | 244 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 245 | int main() |
| magdamcn | 0:0ba5f6ec8fa5 | 246 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 247 | button.fall(&reset_pressed); // Attach interupt to button when pressed. |
| magdamcn | 0:0ba5f6ec8fa5 | 248 | button.rise(&reset_released); // Attach interupt to button when released. |
| magdamcn | 0:0ba5f6ec8fa5 | 249 | |
| magdamcn | 4:6547599b54a0 | 250 | if (wd.WatchdogCausedReset()) |
| magdamcn | 4:6547599b54a0 | 251 | printf("Watchdog caused reset.\r\n"); |
| magdamcn | 4:6547599b54a0 | 252 | |
| magdamcn | 4:6547599b54a0 | 253 | wd.Configure(5.0); |
| magdamcn | 4:6547599b54a0 | 254 | |
| magdamcn | 0:0ba5f6ec8fa5 | 255 | float reading_pp; // Create variable to store pp reading. |
| magdamcn | 0:0ba5f6ec8fa5 | 256 | bool cable_32A = false; // Create boolean to flag whether a 32 or 16A cable is being used. Default is 16A cable (cable_32A = false). |
| magdamcn | 0:0ba5f6ec8fa5 | 257 | bool cable_connected = false; // Create boolean to flag whether a cable is attached. |
| magdamcn | 0:0ba5f6ec8fa5 | 258 | bool pwm_state = false; // Create boolean to flag current state of pwm (whether it is on or off). |
| magdamcn | 0:0ba5f6ec8fa5 | 259 | float pwm_duty_cycle; // Create float to store the current pwm duty cycle. |
| magdamcn | 0:0ba5f6ec8fa5 | 260 | |
| magdamcn | 0:0ba5f6ec8fa5 | 261 | while(true) // Start of process loop. |
| magdamcn | 0:0ba5f6ec8fa5 | 262 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 263 | // check the cable using pp value |
| magdamcn | 0:0ba5f6ec8fa5 | 264 | reading_pp = pp_value.read(); // Read pp value and ... |
| magdamcn | 0:0ba5f6ec8fa5 | 265 | reading_pp = reading_pp * 3300; // ... multiply it by 3300 to convert to mV. |
| magdamcn | 0:0ba5f6ec8fa5 | 266 | |
| magdamcn | 0:0ba5f6ec8fa5 | 267 | if(reading_pp > 3200) // If the pp value is 3.3 V (greater than 3200 mV) then ... |
| magdamcn | 0:0ba5f6ec8fa5 | 268 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 269 | cable_connected = false; // ... the cable *isn't* connected to charger ... |
| magdamcn | 0:0ba5f6ec8fa5 | 270 | pc.printf("Cable not connected. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 271 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 272 | else |
| magdamcn | 0:0ba5f6ec8fa5 | 273 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 274 | cable_connected = true; // ... otherwise the cable *is* connected to charger. |
| magdamcn | 0:0ba5f6ec8fa5 | 275 | pc.printf("Cable connected. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 276 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 277 | |
| magdamcn | 0:0ba5f6ec8fa5 | 278 | if(reading_pp > 200 && reading_pp < 300) // If the pp reading is between 200 and 300 mV then ... |
| magdamcn | 0:0ba5f6ec8fa5 | 279 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 280 | cable_32A = false; // ... a 16A cable is being used. |
| magdamcn | 0:0ba5f6ec8fa5 | 281 | pc.printf("16A cable detected. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 282 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 283 | |
| magdamcn | 0:0ba5f6ec8fa5 | 284 | if(reading_pp > 0 && reading_pp <100) // If the pp reading if between 0 and 100 mV then ... |
| magdamcn | 0:0ba5f6ec8fa5 | 285 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 286 | cable_32A = true; // ... a 32A cable is being used. |
| magdamcn | 0:0ba5f6ec8fa5 | 287 | pc.printf("32A cable detected. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 288 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 289 | |
| magdamcn | 0:0ba5f6ec8fa5 | 290 | cp_acquire(); // Call the new acquisition routine (replaces the moving average in previous versions). |
| magdamcn | 0:0ba5f6ec8fa5 | 291 | |
| magdamcn | 0:0ba5f6ec8fa5 | 292 | if (cable_connected == false) |
| magdamcn | 0:0ba5f6ec8fa5 | 293 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 294 | if (cp_check_voltage(12) == true) control_pilot = PILOT_12V; |
| magdamcn | 0:0ba5f6ec8fa5 | 295 | |
| magdamcn | 0:0ba5f6ec8fa5 | 296 | if (cp_check_voltage(-12) == true) |
| magdamcn | 0:0ba5f6ec8fa5 | 297 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 298 | control_pilot = PILOT_12V; |
| magdamcn | 0:0ba5f6ec8fa5 | 299 | reset_charger = false; |
| magdamcn | 0:0ba5f6ec8fa5 | 300 | } |
| magdamcn | 2:156af70e6f15 | 301 | //if (cp_check_voltage(12) == false && cp_check_voltage(-12) == false)control_pilot = PILOT_NOK; // voltage not at 12 or -12, error accured |
| magdamcn | 0:0ba5f6ec8fa5 | 302 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 303 | |
| magdamcn | 0:0ba5f6ec8fa5 | 304 | if (cable_connected == true) |
| magdamcn | 0:0ba5f6ec8fa5 | 305 | { |
| magdamcn | 1:31e63b43238f | 306 | if (cp_check_voltage(12) == true) control_pilot = PILOT_12V; |
| magdamcn | 0:0ba5f6ec8fa5 | 307 | if (cp_check_voltage(9) == true) control_pilot = PILOT_9V; |
| magdamcn | 0:0ba5f6ec8fa5 | 308 | if (cp_check_voltage(6) == true) control_pilot = PILOT_6V; |
| magdamcn | 0:0ba5f6ec8fa5 | 309 | if (reset_charger == true) control_pilot = PILOT_RESET; |
| magdamcn | 1:31e63b43238f | 310 | //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 |
| magdamcn | 0:0ba5f6ec8fa5 | 311 | } |
| magdamcn | 2:156af70e6f15 | 312 | |
| magdamcn | 0:0ba5f6ec8fa5 | 313 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 314 | // * Switching PWM Cycle & TEST Counter Timer * |
| magdamcn | 0:0ba5f6ec8fa5 | 315 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 316 | // if (use_upper_current == false) pwm_duty_cycle = pwm_duty_low; |
| magdamcn | 0:0ba5f6ec8fa5 | 317 | // if (use_upper_current == true) pwm_duty_cycle = pwm_duty_high; |
| magdamcn | 0:0ba5f6ec8fa5 | 318 | // |
| magdamcn | 0:0ba5f6ec8fa5 | 319 | // 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. |
| magdamcn | 0:0ba5f6ec8fa5 | 320 | // * TESTERCOUNTER monitoring is switched of for the Smartcharge Home+ charger, PWN cycle based on a cable inserted |
| magdamcn | 0:0ba5f6ec8fa5 | 321 | // |
| magdamcn | 0:0ba5f6ec8fa5 | 322 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 323 | |
| magdamcn | 0:0ba5f6ec8fa5 | 324 | // ************************************************************ |
| magdamcn | 1:31e63b43238f | 325 | // * PWM cycle based on cable instered * |
| magdamcn | 0:0ba5f6ec8fa5 | 326 | // ************************************************************ |
| magdamcn | 0:0ba5f6ec8fa5 | 327 | if (cable_32A == false) pwm_duty_cycle = pwm_duty_low; |
| magdamcn | 0:0ba5f6ec8fa5 | 328 | if (cable_32A == true) pwm_duty_cycle = pwm_duty_high; |
| magdamcn | 1:31e63b43238f | 329 | |
| magdamcn | 1:31e63b43238f | 330 | //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. |
| magdamcn | 1:31e63b43238f | 331 | //9V monitorin & time reser disabled for this version |
| magdamcn | 0:0ba5f6ec8fa5 | 332 | |
| magdamcn | 0:0ba5f6ec8fa5 | 333 | switch(control_pilot) |
| magdamcn | 0:0ba5f6ec8fa5 | 334 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 335 | case PILOT_12V: |
| magdamcn | 0:0ba5f6ec8fa5 | 336 | contactor = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 337 | lock = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 338 | red = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 339 | green = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 340 | blue = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 341 | my_pwm = 0; |
| magdamcn | 1:31e63b43238f | 342 | my_pwm.write(0); |
| magdamcn | 0:0ba5f6ec8fa5 | 343 | pwm_state = false; |
| magdamcn | 1:31e63b43238f | 344 | pc.printf("Charger in STATE:------------------------------------------------ A. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 345 | pc.printf("PILOT_12V - Pilot at 12 V. \r\n"); |
| magdamcn | 1:31e63b43238f | 346 | TESTCOUNTER =0; |
| magdamcn | 1:31e63b43238f | 347 | pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds \r\n", TESTCOUNTER); |
| magdamcn | 1:31e63b43238f | 348 | stateB_COUNTER =0; |
| magdamcn | 1:31e63b43238f | 349 | pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds \r\n", stateB_COUNTER); |
| magdamcn | 0:0ba5f6ec8fa5 | 350 | break; |
| magdamcn | 0:0ba5f6ec8fa5 | 351 | |
| magdamcn | 0:0ba5f6ec8fa5 | 352 | case PILOT_9V: |
| magdamcn | 0:0ba5f6ec8fa5 | 353 | contactor = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 354 | //relay=0; |
| magdamcn | 0:0ba5f6ec8fa5 | 355 | lock = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 356 | red = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 357 | green = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 358 | blue = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 359 | if (pwm_state == false) |
| magdamcn | 0:0ba5f6ec8fa5 | 360 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 361 | my_pwm.period_us(1000); |
| magdamcn | 0:0ba5f6ec8fa5 | 362 | my_pwm.pulsewidth_us(1000); |
| magdamcn | 0:0ba5f6ec8fa5 | 363 | my_pwm.write(pwm_duty_cycle); |
| magdamcn | 1:31e63b43238f | 364 | pwm_state = true; |
| magdamcn | 0:0ba5f6ec8fa5 | 365 | } |
| magdamcn | 1:31e63b43238f | 366 | pc.printf("PWM duty cycle is at: ------------------------------------------- %.1f %% \r\n",100-pwm_duty_cycle*100); |
| magdamcn | 1:31e63b43238f | 367 | pc.printf("Charger in STATE:------------------------------------------------ B. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 368 | pc.printf("PILOT_9V - Pilot at 9 V. \r\n"); |
| magdamcn | 1:31e63b43238f | 369 | TESTCOUNTER =0; |
| magdamcn | 1:31e63b43238f | 370 | pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds \r\n", TESTCOUNTER); |
| magdamcn | 1:31e63b43238f | 371 | //stateB_COUNTER+=1; |
| magdamcn | 1:31e63b43238f | 372 | pc.printf("stateB_COUNTER timer: ------------------------------- %u seconds \r\n", stateB_COUNTER); |
| magdamcn | 0:0ba5f6ec8fa5 | 373 | break; |
| magdamcn | 0:0ba5f6ec8fa5 | 374 | |
| magdamcn | 0:0ba5f6ec8fa5 | 375 | case PILOT_6V: |
| magdamcn | 0:0ba5f6ec8fa5 | 376 | contactor = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 377 | relay = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 378 | lock = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 379 | red = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 380 | green = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 381 | blue = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 382 | if (pwm_state == false) |
| magdamcn | 0:0ba5f6ec8fa5 | 383 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 384 | my_pwm.period_us(1000); |
| magdamcn | 0:0ba5f6ec8fa5 | 385 | my_pwm.pulsewidth_us(1000); |
| magdamcn | 0:0ba5f6ec8fa5 | 386 | my_pwm.write(pwm_duty_cycle); |
| magdamcn | 0:0ba5f6ec8fa5 | 387 | pwm_state = true; |
| magdamcn | 0:0ba5f6ec8fa5 | 388 | } |
| magdamcn | 2:156af70e6f15 | 389 | pc.printf("PWM duty cycle is at: ------------------------------------------- %.1f %% \r\n", 100-pwm_duty_cycle*100); |
| magdamcn | 1:31e63b43238f | 390 | pc.printf("Charger in STATE:------------------------------------------------ C. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 391 | pc.printf("PILOT_6V - Pilot at 6 V. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 392 | // TESTCOUNTER+=1; |
| magdamcn | 0:0ba5f6ec8fa5 | 393 | // * TESTCOUNTER switched of |
| magdamcn | 1:31e63b43238f | 394 | pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds \r\n", TESTCOUNTER); |
| magdamcn | 1:31e63b43238f | 395 | stateB_COUNTER = 0; |
| magdamcn | 1:31e63b43238f | 396 | pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds \r\n", stateB_COUNTER); |
| magdamcn | 0:0ba5f6ec8fa5 | 397 | break; |
| magdamcn | 0:0ba5f6ec8fa5 | 398 | |
| magdamcn | 0:0ba5f6ec8fa5 | 399 | case PILOT_NOK: |
| magdamcn | 1:31e63b43238f | 400 | contactor = 0; |
| magdamcn | 1:31e63b43238f | 401 | relay = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 402 | lock = 0; |
| magdamcn | 1:31e63b43238f | 403 | my_pwm.period_ms(1); |
| magdamcn | 1:31e63b43238f | 404 | my_pwm.pulsewidth_ms(1); |
| magdamcn | 1:31e63b43238f | 405 | my_pwm.write(1); |
| magdamcn | 1:31e63b43238f | 406 | pwm_state = false; |
| magdamcn | 0:0ba5f6ec8fa5 | 407 | red = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 408 | green = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 409 | blue = 0; |
| magdamcn | 1:31e63b43238f | 410 | wait(0.5); // 500 ms |
| magdamcn | 1:31e63b43238f | 411 | red = 0; // LED is OFF |
| magdamcn | 1:31e63b43238f | 412 | wait(0.2); // 200 ms |
| magdamcn | 0:0ba5f6ec8fa5 | 413 | pc.printf("Error. \r\n"); |
| magdamcn | 1:31e63b43238f | 414 | pc.printf("PILOT_NOK:------------------------------------------------------- Pilot ERROR. \r\n"); |
| magdamcn | 1:31e63b43238f | 415 | TESTCOUNTER =0; |
| magdamcn | 1:31e63b43238f | 416 | pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds \r\n", TESTCOUNTER); |
| magdamcn | 1:31e63b43238f | 417 | stateB_COUNTER =0; |
| magdamcn | 1:31e63b43238f | 418 | pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds \r\n", stateB_COUNTER); |
| magdamcn | 0:0ba5f6ec8fa5 | 419 | break; |
| magdamcn | 0:0ba5f6ec8fa5 | 420 | |
| magdamcn | 0:0ba5f6ec8fa5 | 421 | case PILOT_RESET: |
| magdamcn | 0:0ba5f6ec8fa5 | 422 | contactor = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 423 | relay = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 424 | lock = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 425 | red = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 426 | green = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 427 | blue = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 428 | my_pwm.period_ms(1); |
| magdamcn | 0:0ba5f6ec8fa5 | 429 | my_pwm.pulsewidth_ms(1); |
| magdamcn | 0:0ba5f6ec8fa5 | 430 | my_pwm.write(1); |
| magdamcn | 0:0ba5f6ec8fa5 | 431 | pwm_state = false; |
| magdamcn | 0:0ba5f6ec8fa5 | 432 | pc.printf("RESET IMPLEMENTED. \r\n"); |
| magdamcn | 1:31e63b43238f | 433 | pc.printf("PILOT_RESET:----------------------------------------------------- Pilot at -12V. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 434 | wait(0.5); // 500 ms |
| magdamcn | 1:31e63b43238f | 435 | blue = 0; // LED is OFF |
| magdamcn | 0:0ba5f6ec8fa5 | 436 | wait(0.2); // 200 ms |
| magdamcn | 1:31e63b43238f | 437 | TESTCOUNTER =0; |
| magdamcn | 1:31e63b43238f | 438 | pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds \r\n", TESTCOUNTER); |
| magdamcn | 1:31e63b43238f | 439 | stateB_COUNTER =0; |
| magdamcn | 1:31e63b43238f | 440 | pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds \r\n", stateB_COUNTER); |
| magdamcn | 0:0ba5f6ec8fa5 | 441 | use_upper_current = false; |
| magdamcn | 0:0ba5f6ec8fa5 | 442 | break; |
| magdamcn | 0:0ba5f6ec8fa5 | 443 | |
| magdamcn | 0:0ba5f6ec8fa5 | 444 | case PWM_CHANGE: |
| magdamcn | 0:0ba5f6ec8fa5 | 445 | lock = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 446 | contactor = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 447 | red = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 448 | green = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 449 | blue = 1; |
| magdamcn | 0:0ba5f6ec8fa5 | 450 | wait(0.1); |
| magdamcn | 1:31e63b43238f | 451 | pc.printf("----------------------------------------------------------------- Charger changing PWM. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 452 | my_pwm.period_ms(1); |
| magdamcn | 0:0ba5f6ec8fa5 | 453 | my_pwm.pulsewidth_ms(1); |
| magdamcn | 0:0ba5f6ec8fa5 | 454 | my_pwm.write(1); |
| magdamcn | 0:0ba5f6ec8fa5 | 455 | wait(1); |
| magdamcn | 0:0ba5f6ec8fa5 | 456 | pc.printf("STOPPED PWM - Switching to -12 V. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 457 | my_pwm = 0; |
| magdamcn | 0:0ba5f6ec8fa5 | 458 | pc.printf("STOPPED PWM - Switching to +12 V. \r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 459 | wait(1); |
| magdamcn | 0:0ba5f6ec8fa5 | 460 | pwm_state = false; |
| magdamcn | 1:31e63b43238f | 461 | TESTCOUNTER =0; |
| magdamcn | 1:31e63b43238f | 462 | pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds \r\n", TESTCOUNTER); |
| magdamcn | 1:31e63b43238f | 463 | stateB_COUNTER =0; |
| magdamcn | 1:31e63b43238f | 464 | pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds \r\n", stateB_COUNTER); |
| magdamcn | 0:0ba5f6ec8fa5 | 465 | if(use_upper_current == false) |
| magdamcn | 0:0ba5f6ec8fa5 | 466 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 467 | use_upper_current = true; |
| magdamcn | 0:0ba5f6ec8fa5 | 468 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 469 | else |
| magdamcn | 0:0ba5f6ec8fa5 | 470 | { |
| magdamcn | 0:0ba5f6ec8fa5 | 471 | use_upper_current = false; |
| magdamcn | 0:0ba5f6ec8fa5 | 472 | } |
| magdamcn | 1:31e63b43238f | 473 | break; |
| magdamcn | 1:31e63b43238f | 474 | |
| magdamcn | 1:31e63b43238f | 475 | case PILOT_START_AGAIN: |
| magdamcn | 1:31e63b43238f | 476 | red = 1; |
| magdamcn | 1:31e63b43238f | 477 | green = 0; |
| magdamcn | 1:31e63b43238f | 478 | blue = 1; |
| magdamcn | 1:31e63b43238f | 479 | wait(0.1); |
| magdamcn | 1:31e63b43238f | 480 | pc.printf("Charger:--------------------------------------------------------- RESTARTING stat B - 9V. \r\n"); |
| magdamcn | 1:31e63b43238f | 481 | my_pwm.period_ms(1); |
| magdamcn | 1:31e63b43238f | 482 | my_pwm.pulsewidth_ms(1); |
| magdamcn | 1:31e63b43238f | 483 | my_pwm.write(1); |
| magdamcn | 1:31e63b43238f | 484 | wait(1); |
| magdamcn | 1:31e63b43238f | 485 | pc.printf("STOPPED PWM - Switching to -12 V. \r\n"); |
| magdamcn | 1:31e63b43238f | 486 | my_pwm = 0; |
| magdamcn | 1:31e63b43238f | 487 | pc.printf("STOPPED PWM - Switching to +12 V. \r\n"); |
| magdamcn | 1:31e63b43238f | 488 | wait(1); |
| magdamcn | 1:31e63b43238f | 489 | pwm_state = false; |
| magdamcn | 1:31e63b43238f | 490 | TESTCOUNTER =0; |
| magdamcn | 1:31e63b43238f | 491 | pc.printf("TESTCOUNTER timer:----------------------------------- %u seconds \r\n", TESTCOUNTER); |
| magdamcn | 1:31e63b43238f | 492 | stateB_COUNTER =0; |
| magdamcn | 1:31e63b43238f | 493 | pc.printf("stateB_COUNTER timer:-------------------------------- %u seconds \r\n", stateB_COUNTER); |
| magdamcn | 1:31e63b43238f | 494 | break; |
| magdamcn | 1:31e63b43238f | 495 | |
| magdamcn | 1:31e63b43238f | 496 | } |
| magdamcn | 1:31e63b43238f | 497 | |
| magdamcn | 0:0ba5f6ec8fa5 | 498 | pc.printf("#################\r\n"); |
| magdamcn | 0:0ba5f6ec8fa5 | 499 | //wait(1); // wait(); added to slow down the feed from nucleo for easier evaluation |
| magdamcn | 0:0ba5f6ec8fa5 | 500 | } |
| magdamcn | 0:0ba5f6ec8fa5 | 501 | } |