doesn't charge ;(

Dependencies:   mbed

Committer:
magdamcn
Date:
Wed Nov 02 16:51:12 2016 +0000
Revision:
1:a9315bd73ea5
Parent:
0:ae42d6933214
Child:
2:92d505d15a7b
32 amp to 16 amp after cable recognition

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 1:a9315bd73ea5 108 pwm=0.734; //////////////////////////////////////////////////////////////////
magdamcn 1:a9315bd73ea5 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 //--------------------------------------Post instertatiom signaling CP-----------------------------------------//
magdamcn 0:ae42d6933214 122
magdamcn 0:ae42d6933214 123
magdamcn 0:ae42d6933214 124
magdamcn 0:ae42d6933214 125
magdamcn 0:ae42d6933214 126 for (int i = 0; i < NUM_SAMPLES ; i++){
magdamcn 0:ae42d6933214 127 float CP_sample_value = cp_value.read_u16();
magdamcn 0:ae42d6933214 128 CP_sample_value = (float) 3300 / 65535 * (float) CP_sample_value; // convert sample to voltage
magdamcn 0:ae42d6933214 129 CP_average = ((CP_sample_value + (i * CP_previous_average)) / (i+1));
magdamcn 0:ae42d6933214 130 CP_previous_average = CP_average;
magdamcn 0:ae42d6933214 131 }
magdamcn 0:ae42d6933214 132 printf("CP Average Value = %.f \n\r", CP_average);
magdamcn 0:ae42d6933214 133 printf("PP Value = %.0f mV\n\r",PP_value);
magdamcn 0:ae42d6933214 134 printf("\n\r");
magdamcn 0:ae42d6933214 135
magdamcn 0:ae42d6933214 136
magdamcn 0:ae42d6933214 137
magdamcn 0:ae42d6933214 138 //--------------------------------------------- PILOT CHECK --------------------------------------------------//
magdamcn 0:ae42d6933214 139
magdamcn 0:ae42d6933214 140 if(chargerType == 0){
magdamcn 0:ae42d6933214 141 control_pilot = PILOT_12V;
magdamcn 0:ae42d6933214 142 resetCharger = false; //-----check this for restet------//
magdamcn 0:ae42d6933214 143 printf("12 V \n\r");
magdamcn 0:ae42d6933214 144 printf("\n\r");
magdamcn 0:ae42d6933214 145 }
magdamcn 0:ae42d6933214 146
magdamcn 0:ae42d6933214 147 if(chargerType == 16){
magdamcn 0:ae42d6933214 148 if(((CP_average > 1900)&&(2100 > CP_average))||((CP_average > 550)&&(650 > CP_average))){
magdamcn 0:ae42d6933214 149 control_pilot = PILOT_9V;
magdamcn 0:ae42d6933214 150 printf("16amp@ 9 V \n\r");
magdamcn 0:ae42d6933214 151 printf("\n\r");
magdamcn 0:ae42d6933214 152 }
magdamcn 0:ae42d6933214 153
magdamcn 0:ae42d6933214 154 if((CP_average > 400)&&(500 > CP_average)){
magdamcn 0:ae42d6933214 155 control_pilot = PILOT_6V;
magdamcn 0:ae42d6933214 156 printf("16amp@ 6 V \n\r");
magdamcn 0:ae42d6933214 157 printf("\n\r");
magdamcn 0:ae42d6933214 158 }
magdamcn 0:ae42d6933214 159
magdamcn 0:ae42d6933214 160 if((resetCharger == true)||((CP_average > 700)&&(800 > CP_average))){
magdamcn 0:ae42d6933214 161 control_pilot = PILOT_RESET;
magdamcn 0:ae42d6933214 162 printf("reset \n\r");
magdamcn 0:ae42d6933214 163 printf("\n\r");
magdamcn 0:ae42d6933214 164 }
magdamcn 0:ae42d6933214 165 }
magdamcn 0:ae42d6933214 166 if(chargerType == 32){
magdamcn 0:ae42d6933214 167 if(((CP_average > 1900)&&(2100 > CP_average))||((CP_average > 1070)&&(1170 > CP_average))){
magdamcn 0:ae42d6933214 168 control_pilot = PILOT_9V;
magdamcn 0:ae42d6933214 169 printf("32amp@ 9 V \n\r");
magdamcn 0:ae42d6933214 170 printf("\n\r");
magdamcn 0:ae42d6933214 171 }
magdamcn 0:ae42d6933214 172 if((CP_average > 730)&&(830 > CP_average)){
magdamcn 0:ae42d6933214 173 control_pilot = PILOT_6V;
magdamcn 0:ae42d6933214 174 printf("32amp@ 6 V \n\r");
magdamcn 0:ae42d6933214 175 printf("\n\r");
magdamcn 0:ae42d6933214 176 }
magdamcn 0:ae42d6933214 177 if(resetCharger == true){
magdamcn 0:ae42d6933214 178 control_pilot = PILOT_RESET;
magdamcn 0:ae42d6933214 179 printf("reset \n\r");
magdamcn 0:ae42d6933214 180 printf("\n\r");
magdamcn 0:ae42d6933214 181 }
magdamcn 0:ae42d6933214 182 }
magdamcn 0:ae42d6933214 183
magdamcn 0:ae42d6933214 184 //------------------------------------------- STATE ASSIGNED ------------------------------------------------//
magdamcn 0:ae42d6933214 185
magdamcn 0:ae42d6933214 186 switch(control_pilot) {
magdamcn 0:ae42d6933214 187 case PILOT_12V:
magdamcn 0:ae42d6933214 188 printf("Charger in STATE A\r\n");
magdamcn 0:ae42d6933214 189 printf("PILOT_12V - Pilot at 12 V \n\r");
magdamcn 0:ae42d6933214 190 my_pwm = 0;
magdamcn 0:ae42d6933214 191 contactor =0;
magdamcn 0:ae42d6933214 192 lock=0;
magdamcn 0:ae42d6933214 193 red = 0;
magdamcn 0:ae42d6933214 194 green = 0;
magdamcn 0:ae42d6933214 195 blue = 1;
magdamcn 0:ae42d6933214 196 break;
magdamcn 0:ae42d6933214 197
magdamcn 0:ae42d6933214 198 case PILOT_9V:
magdamcn 0:ae42d6933214 199 contactor =0;
magdamcn 0:ae42d6933214 200 // relay=0;
magdamcn 0:ae42d6933214 201 lock=1;
magdamcn 0:ae42d6933214 202 my_pwm.period_ms(1);
magdamcn 0:ae42d6933214 203 my_pwm.pulsewidth_ms(1);
magdamcn 0:ae42d6933214 204 //my_pwm.write(0.734);
magdamcn 0:ae42d6933214 205 my_pwm.write(pwm);
magdamcn 0:ae42d6933214 206 printf("Charger in STATE b\r\n");
magdamcn 0:ae42d6933214 207 printf("PILOT_9V - Pilot at 9 V \n\r");
magdamcn 0:ae42d6933214 208 red = 1;
magdamcn 0:ae42d6933214 209 green = 1;
magdamcn 0:ae42d6933214 210 blue = 0;
magdamcn 0:ae42d6933214 211 break;
magdamcn 0:ae42d6933214 212
magdamcn 0:ae42d6933214 213 case PILOT_6V:
magdamcn 0:ae42d6933214 214 contactor =1;
magdamcn 0:ae42d6933214 215 relay=1;
magdamcn 0:ae42d6933214 216 lock=1;
magdamcn 0:ae42d6933214 217 my_pwm.period_ms(1);
magdamcn 0:ae42d6933214 218 my_pwm.pulsewidth_ms(1);
magdamcn 0:ae42d6933214 219 my_pwm.write(pwm); // 8 amp charger setup
magdamcn 0:ae42d6933214 220 //my_pwm.write(0.734); // 16 amp charger setup
magdamcn 0:ae42d6933214 221
magdamcn 0:ae42d6933214 222 printf("Charger in STATE c\r\n");
magdamcn 0:ae42d6933214 223 printf("PILOT_6V - Pilot at 6 V \n\r");
magdamcn 0:ae42d6933214 224 red = 0;
magdamcn 0:ae42d6933214 225 green = 1;
magdamcn 0:ae42d6933214 226 blue = 0;
magdamcn 0:ae42d6933214 227 break;
magdamcn 0:ae42d6933214 228
magdamcn 0:ae42d6933214 229 case PILOT_NOK:
magdamcn 0:ae42d6933214 230 printf("Error");
magdamcn 0:ae42d6933214 231 printf("PILOT_NOK - Pilot ERROR \n\r");
magdamcn 0:ae42d6933214 232 lock=0;
magdamcn 0:ae42d6933214 233 red = 1;
magdamcn 0:ae42d6933214 234 green = 0;
magdamcn 0:ae42d6933214 235 blue = 0;
magdamcn 0:ae42d6933214 236 break;
magdamcn 0:ae42d6933214 237
magdamcn 0:ae42d6933214 238 case PILOT_RESET:
magdamcn 0:ae42d6933214 239 printf("RESET IMPLEMENTED \n\r");
magdamcn 0:ae42d6933214 240 printf("PILOT_RESET - Pilot at -12V \n\r");
magdamcn 0:ae42d6933214 241 my_pwm.period_ms(1);
magdamcn 0:ae42d6933214 242 my_pwm.pulsewidth_ms(1);
magdamcn 0:ae42d6933214 243 my_pwm.write(1);
magdamcn 0:ae42d6933214 244 contactor =0;
magdamcn 0:ae42d6933214 245 relay=0;
magdamcn 0:ae42d6933214 246 lock=0;
magdamcn 0:ae42d6933214 247 red = 1;
magdamcn 0:ae42d6933214 248 green = 0;
magdamcn 0:ae42d6933214 249 blue = 0;
magdamcn 0:ae42d6933214 250 wait(0.5); // 200 ms
magdamcn 0:ae42d6933214 251 red = 0; // LED is OFF
magdamcn 0:ae42d6933214 252 wait(0.2); // 1 sec
magdamcn 0:ae42d6933214 253 break;
magdamcn 0:ae42d6933214 254
magdamcn 0:ae42d6933214 255 }//end switch
magdamcn 0:ae42d6933214 256
magdamcn 0:ae42d6933214 257 //wait(2);
magdamcn 0:ae42d6933214 258
magdamcn 0:ae42d6933214 259 } //end while(1)
magdamcn 0:ae42d6933214 260
magdamcn 0:ae42d6933214 261 } //end main
magdamcn 0:ae42d6933214 262
magdamcn 0:ae42d6933214 263
magdamcn 0:ae42d6933214 264