doesn't charge ;(

Dependencies:   mbed

Committer:
magdamcn
Date:
Wed Nov 02 12:33:15 2016 +0000
Revision:
0:ae42d6933214
Child:
1:a9315bd73ea5
n

Who changed what in which revision?

UserRevisionLine numberNew contents of line
magdamcn 0:ae42d6933214 1
magdamcn 0:ae42d6933214 2 #define CHARGER_PRESET 32
magdamcn 0:ae42d6933214 3 //#define CHARGER_PRESET 16
magdamcn 0:ae42d6933214 4
magdamcn 0:ae42d6933214 5 #include "mbed.h"
magdamcn 0:ae42d6933214 6
magdamcn 0:ae42d6933214 7 AnalogIn cp_value(A1); //A1 – PWM sense, analog read
magdamcn 0:ae42d6933214 8 AnalogIn pp_value(A2); //A2 - PP, analog reads
magdamcn 0:ae42d6933214 9 PwmOut my_pwm(D5); //pwm pin 5
magdamcn 0:ae42d6933214 10 DigitalOut lock(D7);
magdamcn 0:ae42d6933214 11 InterruptIn button(D8);
magdamcn 0:ae42d6933214 12
magdamcn 0:ae42d6933214 13 DigitalOut relay(D12);
magdamcn 0:ae42d6933214 14 DigitalOut contactor(D13);
magdamcn 0:ae42d6933214 15
magdamcn 0:ae42d6933214 16 Timer buttonTimer; //CC for reset button
magdamcn 0:ae42d6933214 17 Timeout buttonTimeout; //CC for reset button
magdamcn 0:ae42d6933214 18
magdamcn 0:ae42d6933214 19 DigitalOut green(D9);
magdamcn 0:ae42d6933214 20 DigitalOut red(D10);
magdamcn 0:ae42d6933214 21 DigitalOut blue(D11);
magdamcn 0:ae42d6933214 22
magdamcn 0:ae42d6933214 23 unsigned char control_pilot;
magdamcn 0:ae42d6933214 24 float pwm;
magdamcn 0:ae42d6933214 25
magdamcn 0:ae42d6933214 26 #define PILOT_12V 1
magdamcn 0:ae42d6933214 27 #define PILOT_9V 2
magdamcn 0:ae42d6933214 28 #define PILOT_6V 3
magdamcn 0:ae42d6933214 29 #define PILOT_NOK 4
magdamcn 0:ae42d6933214 30 #define PILOT_RESET 5
magdamcn 0:ae42d6933214 31
magdamcn 0:ae42d6933214 32 #define NUM_SAMPLES 2000 // size of sample series
magdamcn 0:ae42d6933214 33
magdamcn 0:ae42d6933214 34 #define SAMPLE_BLOCKS 1 //CC for reset button
magdamcn 0:ae42d6933214 35 #define RESET_SECONDS 5 //CC for reset button
magdamcn 0:ae42d6933214 36
magdamcn 0:ae42d6933214 37 bool resetDown = false;
magdamcn 0:ae42d6933214 38 bool resetCharger = false;
magdamcn 0:ae42d6933214 39
magdamcn 0:ae42d6933214 40 float CP_average=0;
magdamcn 0:ae42d6933214 41 float CP_previous_average=0;
magdamcn 0:ae42d6933214 42 float PP_value;
magdamcn 0:ae42d6933214 43 float PP_reading;
magdamcn 0:ae42d6933214 44
magdamcn 0:ae42d6933214 45 unsigned char cableType;
magdamcn 0:ae42d6933214 46 unsigned char chargerType;
magdamcn 0:ae42d6933214 47
magdamcn 0:ae42d6933214 48 void timedOut() {
magdamcn 0:ae42d6933214 49 resetCharger = true;
magdamcn 0:ae42d6933214 50 printf("Button pressed for more than 3 sec ! Charger reset !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \r\n");
magdamcn 0:ae42d6933214 51 }
magdamcn 0:ae42d6933214 52
magdamcn 0:ae42d6933214 53 void resetPress() {
magdamcn 0:ae42d6933214 54 printf("Reset button pressed ... starting timer \r\n");
magdamcn 0:ae42d6933214 55 buttonTimer.stop();
magdamcn 0:ae42d6933214 56 buttonTimer.reset();
magdamcn 0:ae42d6933214 57 buttonTimer.start();
magdamcn 0:ae42d6933214 58 resetDown = true;
magdamcn 0:ae42d6933214 59 buttonTimeout.attach(&timedOut, RESET_SECONDS);
magdamcn 0:ae42d6933214 60 }
magdamcn 0:ae42d6933214 61
magdamcn 0:ae42d6933214 62 void resetRelease() {
magdamcn 0:ae42d6933214 63 printf("Reset button released \r\n");
magdamcn 0:ae42d6933214 64 int elapsedSeconds = buttonTimer.read();
magdamcn 0:ae42d6933214 65 buttonTimer.stop();
magdamcn 0:ae42d6933214 66 buttonTimer.reset();
magdamcn 0:ae42d6933214 67 if (elapsedSeconds > 3) {
magdamcn 0:ae42d6933214 68 resetCharger = true;
magdamcn 0:ae42d6933214 69 printf("Button was pressed for more than 3 sec !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \r\n");
magdamcn 0:ae42d6933214 70 }
magdamcn 0:ae42d6933214 71 else{
magdamcn 0:ae42d6933214 72 printf("If we're getting here then we've released the button before 3 seconds were up.\r\n");
magdamcn 0:ae42d6933214 73 }
magdamcn 0:ae42d6933214 74 printf("We will detach the timeout an setup for the next time.\r\n");
magdamcn 0:ae42d6933214 75 printf("%u \r\n", elapsedSeconds);
magdamcn 0:ae42d6933214 76 buttonTimeout.detach();
magdamcn 0:ae42d6933214 77 }
magdamcn 0:ae42d6933214 78
magdamcn 0:ae42d6933214 79
magdamcn 0:ae42d6933214 80 int main() {
magdamcn 0:ae42d6933214 81
magdamcn 0:ae42d6933214 82 button.fall(&resetPress);
magdamcn 0:ae42d6933214 83 button.rise(&resetRelease);
magdamcn 0:ae42d6933214 84
magdamcn 0:ae42d6933214 85 while(1){
magdamcn 0:ae42d6933214 86
magdamcn 0:ae42d6933214 87 //----------------------END Cable detection, Charger Power Check, Cable Power Check---------------------------//
magdamcn 0:ae42d6933214 88 float PP_value = pp_value.read();
magdamcn 0:ae42d6933214 89 PP_value = PP_value * 3300;
magdamcn 0:ae42d6933214 90
magdamcn 0:ae42d6933214 91 if(PP_value == 3300){
magdamcn 0:ae42d6933214 92 printf("Cable not detected \r\n");
magdamcn 0:ae42d6933214 93 printf("Charger Type %u AMP \r\n", CHARGER_PRESET);
magdamcn 0:ae42d6933214 94 chargerType = 0;
magdamcn 0:ae42d6933214 95 }//end if
magdamcn 0:ae42d6933214 96 else{
magdamcn 0:ae42d6933214 97 if(CHARGER_PRESET == 16){
magdamcn 0:ae42d6933214 98 chargerType = 16;
magdamcn 0:ae42d6933214 99 printf("Charger 16AMP ONLY \r\n");
magdamcn 0:ae42d6933214 100 pwm=0.734;
magdamcn 0:ae42d6933214 101 printf("PWM @ 26.6% \r\n");
magdamcn 0:ae42d6933214 102 }// end if
magdamcn 0:ae42d6933214 103 else if(CHARGER_PRESET == 32){
magdamcn 0:ae42d6933214 104 if((PP_value > 200) && (PP_value < 300)){
magdamcn 0:ae42d6933214 105 printf("32 AMP charger \n\r\r");
magdamcn 0:ae42d6933214 106 printf("16 AMP cable detected \n\r\r");
magdamcn 0:ae42d6933214 107 chargerType = 16;
magdamcn 0:ae42d6933214 108 pwm=0.734;
magdamcn 0:ae42d6933214 109 printf("PWM @ 26.6%\r\n");
magdamcn 0:ae42d6933214 110 }
magdamcn 0:ae42d6933214 111 if((PP_value > 0 ) && (PP_value < 100)){
magdamcn 0:ae42d6933214 112 printf("32 AMP charger \n\r\r");
magdamcn 0:ae42d6933214 113 printf("32 AMP cable detected \r\n");
magdamcn 0:ae42d6933214 114 chargerType = 32;
magdamcn 0:ae42d6933214 115 pwm=0.468;
magdamcn 0:ae42d6933214 116 printf("PWM @ 52.2%\r\n");
magdamcn 0:ae42d6933214 117
magdamcn 0:ae42d6933214 118 }
magdamcn 0:ae42d6933214 119 }
magdamcn 0:ae42d6933214 120
magdamcn 0:ae42d6933214 121 }
magdamcn 0:ae42d6933214 122 //--------------------------------------Post instertatiom signaling CP-----------------------------------------//
magdamcn 0:ae42d6933214 123
magdamcn 0:ae42d6933214 124
magdamcn 0:ae42d6933214 125
magdamcn 0:ae42d6933214 126
magdamcn 0:ae42d6933214 127 for (int i = 0; i < NUM_SAMPLES ; i++){
magdamcn 0:ae42d6933214 128 float CP_sample_value = cp_value.read_u16();
magdamcn 0:ae42d6933214 129 CP_sample_value = (float) 3300 / 65535 * (float) CP_sample_value; // convert sample to voltage
magdamcn 0:ae42d6933214 130 CP_average = ((CP_sample_value + (i * CP_previous_average)) / (i+1));
magdamcn 0:ae42d6933214 131 CP_previous_average = CP_average;
magdamcn 0:ae42d6933214 132 }
magdamcn 0:ae42d6933214 133 printf("CP Average Value = %.f \n\r", CP_average);
magdamcn 0:ae42d6933214 134 printf("PP Value = %.0f mV\n\r",PP_value);
magdamcn 0:ae42d6933214 135 printf("\n\r");
magdamcn 0:ae42d6933214 136
magdamcn 0:ae42d6933214 137
magdamcn 0:ae42d6933214 138
magdamcn 0:ae42d6933214 139 //--------------------------------------------- PILOT CHECK --------------------------------------------------//
magdamcn 0:ae42d6933214 140
magdamcn 0:ae42d6933214 141 if(chargerType == 0){
magdamcn 0:ae42d6933214 142 control_pilot = PILOT_12V;
magdamcn 0:ae42d6933214 143 resetCharger = false; //-----check this for restet------//
magdamcn 0:ae42d6933214 144 printf("12 V \n\r");
magdamcn 0:ae42d6933214 145 printf("\n\r");
magdamcn 0:ae42d6933214 146 }
magdamcn 0:ae42d6933214 147
magdamcn 0:ae42d6933214 148 if(chargerType == 16){
magdamcn 0:ae42d6933214 149 if(((CP_average > 1900)&&(2100 > CP_average))||((CP_average > 550)&&(650 > CP_average))){
magdamcn 0:ae42d6933214 150 control_pilot = PILOT_9V;
magdamcn 0:ae42d6933214 151 printf("16amp@ 9 V \n\r");
magdamcn 0:ae42d6933214 152 printf("\n\r");
magdamcn 0:ae42d6933214 153 }
magdamcn 0:ae42d6933214 154
magdamcn 0:ae42d6933214 155 if((CP_average > 400)&&(500 > CP_average)){
magdamcn 0:ae42d6933214 156 control_pilot = PILOT_6V;
magdamcn 0:ae42d6933214 157 printf("16amp@ 6 V \n\r");
magdamcn 0:ae42d6933214 158 printf("\n\r");
magdamcn 0:ae42d6933214 159 }
magdamcn 0:ae42d6933214 160
magdamcn 0:ae42d6933214 161 if((resetCharger == true)||((CP_average > 700)&&(800 > CP_average))){
magdamcn 0:ae42d6933214 162 control_pilot = PILOT_RESET;
magdamcn 0:ae42d6933214 163 printf("reset \n\r");
magdamcn 0:ae42d6933214 164 printf("\n\r");
magdamcn 0:ae42d6933214 165 }
magdamcn 0:ae42d6933214 166 }
magdamcn 0:ae42d6933214 167 if(chargerType == 32){
magdamcn 0:ae42d6933214 168 if(((CP_average > 1900)&&(2100 > CP_average))||((CP_average > 1070)&&(1170 > CP_average))){
magdamcn 0:ae42d6933214 169 control_pilot = PILOT_9V;
magdamcn 0:ae42d6933214 170 printf("32amp@ 9 V \n\r");
magdamcn 0:ae42d6933214 171 printf("\n\r");
magdamcn 0:ae42d6933214 172 }
magdamcn 0:ae42d6933214 173 if((CP_average > 730)&&(830 > CP_average)){
magdamcn 0:ae42d6933214 174 control_pilot = PILOT_6V;
magdamcn 0:ae42d6933214 175 printf("32amp@ 6 V \n\r");
magdamcn 0:ae42d6933214 176 printf("\n\r");
magdamcn 0:ae42d6933214 177 }
magdamcn 0:ae42d6933214 178 if(resetCharger == true){
magdamcn 0:ae42d6933214 179 control_pilot = PILOT_RESET;
magdamcn 0:ae42d6933214 180 printf("reset \n\r");
magdamcn 0:ae42d6933214 181 printf("\n\r");
magdamcn 0:ae42d6933214 182 }
magdamcn 0:ae42d6933214 183 }
magdamcn 0:ae42d6933214 184
magdamcn 0:ae42d6933214 185 //------------------------------------------- STATE ASSIGNED ------------------------------------------------//
magdamcn 0:ae42d6933214 186
magdamcn 0:ae42d6933214 187 switch(control_pilot) {
magdamcn 0:ae42d6933214 188 case PILOT_12V:
magdamcn 0:ae42d6933214 189 printf("Charger in STATE A\r\n");
magdamcn 0:ae42d6933214 190 printf("PILOT_12V - Pilot at 12 V \n\r");
magdamcn 0:ae42d6933214 191 my_pwm = 0;
magdamcn 0:ae42d6933214 192 contactor =0;
magdamcn 0:ae42d6933214 193 lock=0;
magdamcn 0:ae42d6933214 194 red = 0;
magdamcn 0:ae42d6933214 195 green = 0;
magdamcn 0:ae42d6933214 196 blue = 1;
magdamcn 0:ae42d6933214 197 break;
magdamcn 0:ae42d6933214 198
magdamcn 0:ae42d6933214 199 case PILOT_9V:
magdamcn 0:ae42d6933214 200 contactor =0;
magdamcn 0:ae42d6933214 201 // relay=0;
magdamcn 0:ae42d6933214 202 lock=1;
magdamcn 0:ae42d6933214 203 my_pwm.period_ms(1);
magdamcn 0:ae42d6933214 204 my_pwm.pulsewidth_ms(1);
magdamcn 0:ae42d6933214 205 //my_pwm.write(0.734);
magdamcn 0:ae42d6933214 206 my_pwm.write(pwm);
magdamcn 0:ae42d6933214 207 printf("Charger in STATE b\r\n");
magdamcn 0:ae42d6933214 208 printf("PILOT_9V - Pilot at 9 V \n\r");
magdamcn 0:ae42d6933214 209 red = 1;
magdamcn 0:ae42d6933214 210 green = 1;
magdamcn 0:ae42d6933214 211 blue = 0;
magdamcn 0:ae42d6933214 212 break;
magdamcn 0:ae42d6933214 213
magdamcn 0:ae42d6933214 214 case PILOT_6V:
magdamcn 0:ae42d6933214 215 contactor =1;
magdamcn 0:ae42d6933214 216 relay=1;
magdamcn 0:ae42d6933214 217 lock=1;
magdamcn 0:ae42d6933214 218 my_pwm.period_ms(1);
magdamcn 0:ae42d6933214 219 my_pwm.pulsewidth_ms(1);
magdamcn 0:ae42d6933214 220 my_pwm.write(pwm); // 8 amp charger setup
magdamcn 0:ae42d6933214 221 //my_pwm.write(0.734); // 16 amp charger setup
magdamcn 0:ae42d6933214 222
magdamcn 0:ae42d6933214 223 printf("Charger in STATE c\r\n");
magdamcn 0:ae42d6933214 224 printf("PILOT_6V - Pilot at 6 V \n\r");
magdamcn 0:ae42d6933214 225 red = 0;
magdamcn 0:ae42d6933214 226 green = 1;
magdamcn 0:ae42d6933214 227 blue = 0;
magdamcn 0:ae42d6933214 228 break;
magdamcn 0:ae42d6933214 229
magdamcn 0:ae42d6933214 230 case PILOT_NOK:
magdamcn 0:ae42d6933214 231 printf("Error");
magdamcn 0:ae42d6933214 232 printf("PILOT_NOK - Pilot ERROR \n\r");
magdamcn 0:ae42d6933214 233 lock=0;
magdamcn 0:ae42d6933214 234 red = 1;
magdamcn 0:ae42d6933214 235 green = 0;
magdamcn 0:ae42d6933214 236 blue = 0;
magdamcn 0:ae42d6933214 237 break;
magdamcn 0:ae42d6933214 238
magdamcn 0:ae42d6933214 239 case PILOT_RESET:
magdamcn 0:ae42d6933214 240 printf("RESET IMPLEMENTED \n\r");
magdamcn 0:ae42d6933214 241 printf("PILOT_RESET - Pilot at -12V \n\r");
magdamcn 0:ae42d6933214 242 my_pwm.period_ms(1);
magdamcn 0:ae42d6933214 243 my_pwm.pulsewidth_ms(1);
magdamcn 0:ae42d6933214 244 my_pwm.write(1);
magdamcn 0:ae42d6933214 245 contactor =0;
magdamcn 0:ae42d6933214 246 relay=0;
magdamcn 0:ae42d6933214 247 lock=0;
magdamcn 0:ae42d6933214 248 red = 1;
magdamcn 0:ae42d6933214 249 green = 0;
magdamcn 0:ae42d6933214 250 blue = 0;
magdamcn 0:ae42d6933214 251 wait(0.5); // 200 ms
magdamcn 0:ae42d6933214 252 red = 0; // LED is OFF
magdamcn 0:ae42d6933214 253 wait(0.2); // 1 sec
magdamcn 0:ae42d6933214 254 break;
magdamcn 0:ae42d6933214 255
magdamcn 0:ae42d6933214 256 }//end switch
magdamcn 0:ae42d6933214 257
magdamcn 0:ae42d6933214 258 //wait(2);
magdamcn 0:ae42d6933214 259
magdamcn 0:ae42d6933214 260 } //end while(1)
magdamcn 0:ae42d6933214 261
magdamcn 0:ae42d6933214 262 } //end main
magdamcn 0:ae42d6933214 263
magdamcn 0:ae42d6933214 264
magdamcn 0:ae42d6933214 265