Explorar o código

Logic added for PWM on the controller end and to read the ADc

namrota ghosh hai 6 meses
pai
achega
3885eebb47
Modificáronse 1 ficheiros con 47 adicións e 20 borrados
  1. 47 20
      src/adc_peripheral/adc_singleConversion.c

+ 47 - 20
src/adc_peripheral/adc_singleConversion.c

@@ -1,48 +1,75 @@
 #include "ti_msp_dl_config.h"
 #include "src/battery_data/battery.h"
 #include <stdio.h>
+#include "src/config.h"
 
 /* ADC variables:
  * bare_voltage gets the ADC value from the memory register
  * checkADC boolean flag 
  */
 
-volatile uint16_t bare_voltage;
-bool checkADC = false;
-
-void processADC(){
-    uint16_t gADCvoltage= 0;
-    int16_t gADCcurrent= 0;
+volatile bool gCheckADC;
+volatile uint16_t gAdcResult;
+static int16_t pwm_value= 0;
 
+void processPWMController(){
+    
+    gAdcResult= false;
+    static bool slot_sov_hov_state= false;
+    uint16_t adc_voltage= 0;
+    int16_t hi_power= 0;
 
     //Start the ADC Sampling:
     DL_ADC12_startConversion(ADC_Controller_INST);
 
     /* Sleep until the ADC sampling and conversion is finished, then reset the boolean */
-    while(checkADC == false){
+    while(gCheckADC == false){
         __WFI();
     }
 
-    bare_voltage = DL_ADC12_getMemResult(ADC_Controller_INST, DL_ADC12_MEM_IDX_0);
+    gAdcResult = DL_ADC12_getMemResult(ADC_Controller_INST, DL_ADC12_MEM_IDX_0);
 
-    // if any invalid value then ignore
-    if (bare_voltage == 0xffff) {
-        // the voltage reading is invalid -> ignore this cycle.
-        return;
+    if (gAdcResult == 0xffff) {
+            // the voltage reading is invalid -> ignore this cycle.
+            return;
     }
 
-    gADCvoltage = bare_voltage*(56+100)/56; // Because of the voltage divider
-    gADCcurrent= bare_voltage*10/1000;
+    //Voltage and Current Conversion:
+    uint16_t adc_val= ((gAdcResult*3300)/4096);
+    adc_voltage= adc_val*(56+100)/56;
+    uint16_t shunt_current = 10*gAdcResult/1000;
+    hi_power= shunt_current * adc_voltage;
+
+
+    printf("ADC Read after conversion: %d\n", adc_val);
+    printf("ADC Voltage Conversion:%d\n", adc_voltage);
+    printf("ADC Current conversion: %d\n", shunt_current);
 
-    printf("ADC Voltage Read on Controller Board: %d\n", gADCvoltage);
-    printf("ADC Current Received:%d\n", gADCcurrent);
     
     //If the controller exceeds HOV_THRESHOLD then
-    if(gADCvoltage >= (HOV_THRESHOLD_MV - CONTROLLER_HYSTERESIS)){
-        printf("Controller HOV state\n");
+    if(adc_voltage >= SOV_THRESHOLD_MV && adc_voltage <= HOV_THRESHOLD_MV){
+        printf("Controller entered SOV state\n");
         DL_GPIO_togglePins(GPIO_ControllerBoard_PORT, GPIO_ControllerBoard_LED_Error_PIN);
+        if(pwm_value< (PWM_MAX_VALUE - PWM_INCREMENT_VALUE)){
+            pwm_value+= PWM_INCREMENT_VALUE; 
+            printf("[Power Burning PWM] SOV/HOV state: Increased power burn PWM to %d\n", pwm_value);
+            DL_TimerG_setCaptureCompareValue(PWM_0_INST, pwm_value, DL_TIMER_CC_0_INDEX); 
+        }
+    }else if(adc_voltage >= HOV_THRESHOLD_MV){
+        //Clear all the relays of charging and discharging in the target board:
+        DL_GPIO_clearPins(GPIO_Battery_Charging_PIN_PB7_PORT, GPIO_Battery_Charging_PIN_PB7_PIN);
+        DL_GPIO_clearPins(GPIO_Battery_Discharging_PIN_PB6_PORT, GPIO_Battery_Discharging_PIN_PB6_PIN);
+        //Set PWM
+        DL_TimerG_setCaptureCompareValue(PWM_0_INST, PWM_MAX_VALUE, DL_TIMER_CC_0_INDEX); 
+        printf("[Power Burning PWM] initial state: %d\n", pwm_value);      
+    }else{
+        if(pwm_value>= (INITIAL_PWM_VALUE + PWM_DECREMENT_VALUE)){
+            pwm_value-= PWM_DECREMENT_VALUE;
+            printf("[Power Burning PWM] OK state: Decreased power burn PWM to %d\n", pwm_value);
+            DL_TimerG_setCaptureCompareValue(PWM_0_INST, pwm_value, DL_TIMER_CC_0_INDEX);
+        }
     }
-    checkADC = false;
+    gCheckADC = false;
     DL_ADC12_enableConversions(ADC_Controller_INST);
 }
 
@@ -51,7 +78,7 @@ void ADC_Controller_INST_IRQHandler(void)
 {
     switch (DL_ADC12_getPendingInterrupt(ADC_Controller_INST)) {
         case DL_ADC12_IIDX_MEM0_RESULT_LOADED:
-            checkADC = true;
+            gCheckADC = true;
             break;
         default:
             break;