Code to run on the charger board (used to charge the car from the mains).

Dependencies:   mbed CUER_CAN

Revision:
3:a7626dffb64a
Parent:
2:da91931184de
Child:
4:f6459580c312
--- a/charger.cpp	Sun Jul 23 21:57:34 2017 +0000
+++ b/charger.cpp	Thu Jul 27 21:00:52 2017 +0000
@@ -17,7 +17,7 @@
 void init();
 void get_CAN_data();
 
-void calculate_current(float voltage_error, float temp_margin, float *current, float *voltage);
+void calculate_current(float voltage_error, float temp_margin, float &current);
 void check_timeouts();
 void update_LEDS();
 bool idAccepted(int id);
@@ -53,14 +53,26 @@
         get_CAN_data();
 
         check_timeouts();
-        
         update_LEDS();
         
         //TODO: add logic here to calculate current and then decide if charging should go ahead
-
+        
+        if (min_cell_voltage > RISING_BALANCE_THRESHOLD || charge_finished) { 
+            charge_finished = true;
+            printf("Charge Finished\r\n");
+            car_can.write(generate_charging_finished_msg());
+            charger_control = 0; //set charger control bit to stop charging
+        } 
+        else {
+            calculate_current(voltage_error, temp_margin, desired_current);
+            desired_voltage = MAX_VOLTAGE;
+            charge_finished = false;
+            charger_control = 1; //set charger control bit to start charging
+        }
         
         //send CAN data
         charger_can.write(generate_charger_control_msg(desired_voltage, desired_current, charger_control)); //control message to charger
+        wait(0.1);
         car_can.write(generate_charger_info_msg(charger_voltage, charger_current, charger_status)); //charger info message for rest of car
         
         //send charge_finished value to control bit.
@@ -77,18 +89,19 @@
     }
 }
 
-void calculate_current(float voltage_error, float temp_margin, float *current, float *voltage){
+void calculate_current(float voltage_error, float temp_margin, float &current){
         
-        float Idot, I;
-        static bool balancing = false;
-         I = *current;
-         if (I < 0.8 && voltage_error < 0.1 || balancing) {
-                balancing = true;
-                printf("balancing\r\n");
-                Idot = voltage_error*KI_BALANCE;
-            } else {
-                    Idot = voltage_error*KI_CHARGE;
-                }
+    float Idot, I;
+    static bool balancing = false;
+    I = current;
+    if (I < 800 && voltage_error < 100 || balancing) {
+            balancing = true;
+            printf("balancing\r\n");
+            Idot = voltage_error*KI_BALANCE;
+        } 
+    else {
+        Idot = voltage_error*KI_CHARGE;
+    }
     I += Idot*TIME_STEP/1000.0;
 
     if(I > MAX_CURRENT) {
@@ -97,7 +110,7 @@
     if(I < 0) {
         I = 0;
     }
-
+    /*
     //Reduce current if temperature is too high
     if (temp_margin > TEMP_RAMP_START) {
         I = 1 - ((temp_margin - TEMP_RAMP_START) / (TEMP_RAMP_FINISH - TEMP_RAMP_START));
@@ -105,9 +118,8 @@
     if (temp_margin > TEMP_RAMP_FINISH) {
         I *= 0;
     }
-
-    *current = I;
-    *voltage = MAX_VOLTAGE;
+    */
+    current = I;
 }
 
 void init()
@@ -208,9 +220,6 @@
 }   
 
 
-
-
-
 void check_timeouts() //Check if it's been too long since any of the other devices in the car have communicated
 {
     
@@ -249,15 +258,7 @@
     }
         
         
-    if (min_cell_voltage > RISING_BALANCE_THRESHOLD) { 
-        charge_finished = true;
-        printf("Charge Finished\r\n");
-        //set charger control bit to finished charging
-    } 
-    else {
-        calculate_current(voltage_error, temp_margin, &desired_current, &desired_voltage);
-        charge_finished = false;
-    }
+
 }
 
 void get_CAN_data() {
@@ -269,7 +270,6 @@
     for (int i = 0; i<CAN_BUFFER_SIZE; ++i) 
     {
         car_safe_to_write[i] = false;
-        car_can_data[i].importCANData(car_buffer[i]);
         car_received_CAN_IDs[i] = car_buffer[i].id;
         car_msgArray[i] = car_buffer[i];
         car_safe_to_write[i] = true;
@@ -305,7 +305,6 @@
     for (int i = 0; i<CAN_BUFFER_SIZE; ++i) 
     {
         charger_safe_to_write[i] = false;
-        charger_can_data[i].importCANData(charger_buffer[i]);
         charger_received_CAN_IDs[i] = charger_buffer[i].id;
         charger_msgArray[i] = charger_buffer[i];
         charger_safe_to_write[i] = true;