Parcourir la source

Fehlerhandling hinzugefügt:
* Der Batterie-Type hat jetzt einen status typ, der Errors beinhaltet
* i2c und regelungsfehler werden automatisch gesetzt
* durch Integration in Mess-Typedef wird es beim i2c read automatisch mit ausgegeben

An dem Feature fehlt noch:
* Fehler für fehlende ADC/DAC Adressen hinzufügen
* Fehlerstati zum ausmachen von pwm und dac verwenden
* Error clear message via I2C muss noch implementiert werden

Heinrich Blatt il y a 8 mois
Parent
commit
ef8cfa53ca

+ 11 - 9
src/battery_data/battery.c

@@ -27,6 +27,12 @@ static void batteryslots_init() {
 
     for(uint8_t i=0; i< NUM_SLOTS; i++){
 
+        battery_slots[i].measurement.state = SLOT_STATE_OK;
+        // convinience trick:
+        // with that we can set *battery_slots[i].state = SLOT_STATE_* or SLOT_ERR_*
+        // like e.g. *battery_slots[i].state = SLOT_ERR_OVERTEMPERATURE
+        battery_slots[i].state = &battery_slots[i].measurement.state;
+
         battery_slots[i].measurement.voltage = 0;
         battery_slots[i].measurement.current = 0;
         battery_slots[i].measurement.temperature = 0;
@@ -70,9 +76,8 @@ static void batteryslots_adjust_current(uint8_t slot) {
                 set_dac(slot, --battery_slots[slot].dac_value);
             }
             else {
-                // error case to be implemented:
                 // we want to give more current, but we can't ?!
-                ;
+                *battery_slots[slot].state = SLOT_WARN_LOWER_DAC_NOT_POSSIBLE;
             }
         } else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD < battery_slots[slot].measurement.current) {
             // we are outside of the tolerance band
@@ -82,9 +87,8 @@ static void batteryslots_adjust_current(uint8_t slot) {
                 set_dac(slot, ++battery_slots[slot].dac_value);
             }
             else {
-                // error case to be implemented:
                 // we want to give more current, but we can't ?!
-                ;
+                *battery_slots[slot].state = SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE;
             }
         }
         // no else statement here: we are ok, since we are in the tolerance measure
@@ -104,14 +108,13 @@ static void batteryslots_adjust_current(uint8_t slot) {
             // @todo debugging & validation: ensure that this directive works
             printf("timer count: %d\n", DL_Timer_getTimerCount(battery_slots[0].timer));
 
-            if (battery_slots[slot].pwm_value+1 >= DL_Timer_getTimerCount(battery_slots[0].timer)) {
+            if (battery_slots[slot].pwm_value+1 <= DL_Timer_getTimerCount(battery_slots[0].timer)) {
                 // pwm is inverse to the DAC since dragging more current means more negative
                 set_pwm(slot, ++battery_slots[slot].pwm_value);
             }
             else {
-                // error case to be implemented:
                 // we want to give more current, but we can't ?!
-                ;
+                *battery_slots[slot].state = SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE;
             }
         } else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD < battery_slots[slot].measurement.current) {
             // we are outside of the tolerance band
@@ -121,9 +124,8 @@ static void batteryslots_adjust_current(uint8_t slot) {
                 set_dac(slot, --battery_slots[slot].pwm_value);
             }
             else {
-                // error case to be implemented:
                 // we want to give more current, but we can't ?!
-                ;
+                *battery_slots[slot].state = SLOT_WARN_LOWER_PWM_NOT_POSSIBLE;
             }
         }
     } else {

+ 25 - 7
src/battery_data/battery.h

@@ -16,22 +16,39 @@
 #define BATTERY_CURRENT_THRESHOLD (5)
 
 // for i2c communication to know battery health
-#define BOOST_SOV_THRESHOLD_MV (4900)
-#define BOOST_HOV_THRESHOLD_MV (5400)
+#define SOV_THRESHOLD_MV (6000)
+#define HOV_THRESHOLD_MV (8000)
+
 #define TEMPERATURE_MAX_C (60)
 
 //Battery Discharge Safety Check
 typedef enum{
-    STATE_OK= 0x00,
-    STATE_SOV= 0x01,
-    STATE_HOV= 0x02,
-    STATE_OVERTEMPERATURE= 0x03,
-} DischargeSafetyCondition;
+    SLOT_STATE_OK= 0x00,
+    SLOT_STATE_SOV= 0x01,
+    SLOT_STATE_HOV= 0x02,
+
+    SLOT_WARN_OVERTEMPERATURE = 0x03,
+    SLOT_ERR_OVERTEMPERATURE= 0x04,
+
+    // Control error states
+    SLOT_WARN_LOWER_DAC_NOT_POSSIBLE=0x10,
+    SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE=0x11,
+    SLOT_WARN_DAC_INVALID_VALUE=0x12,
+    SLOT_WARN_LOWER_PWM_NOT_POSSIBLE=0x13,
+    SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE=0x14,
+
+    // I2C Slave Error states
+    SLOT_ERR_NO_ADC = 0x20,
+    SLOT_ERR_NO_DAC = 0x21,
+    SLOT_ERR_CONFIGBYTE = 0x22,
+    SLOT_ERR_DAC_WRITE_FAILED=0x23,
+} SlotState;
 
 typedef struct{
     uint16_t voltage;
     int16_t current;
     uint16_t temperature;
+    SlotState state;
 } BatteryMeasurement;
 
 typedef struct {
@@ -41,6 +58,7 @@ typedef struct {
     uint16_t dac_value;
     uint16_t pwm_value;
     GPTIMER_Regs *timer;
+    SlotState *state;
 } BatterySlot;
 
 //global battery array declaration: extending visiblity of the variable to multiple source files: variable declaration

+ 4 - 0
src/peripherals/adc/adc_hal.c

@@ -53,6 +53,7 @@ D15.
 #include "src/peripherals/adc/adc_interface.h"
 #include "ti_msp_dl_config.h"
 #include <stdio.h>
+#include "src/battery_data/battery.h"
 
 /*
 * Creating Configuartion Register as mentioned in the datasheet: https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/22226a.pdf
@@ -116,7 +117,10 @@ static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
          DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
     ;
   if(controllerTxPackage.packet[0] == 0xFF){
+    // this clause can only happen if the internal memory management is messed up?!
+    // the config function should take care that this is never the case
     printf("[ADC] Unable to send config bytes\n");
+    *battery_slots[slot_id].state = SLOT_ERR_CONFIGBYTE;
     return false;
   } 
   // Prepare TX Buffer

+ 4 - 2
src/peripherals/dac/dac.c

@@ -2,6 +2,7 @@
 #include "src/interfaces/i2c_controller_interface.h"
 #include "ti/driverlib/dl_i2c.h"
 #include "ti_msp_dl_config.h"
+#include "src/battery_data/battery.h"
 #include <stdint.h>
 #include <stdio.h>
 
@@ -31,6 +32,7 @@ void DAC_UpdateOutput() {
 bool DAC_SingleWrite(uint8_t slot, uint16_t channel_value) {
   if(channel_value > MAX_DAC_VALUE){
     printf("DAC Error: channel_value out of range. Must be between 0 and %d\n", MAX_DAC_VALUE);
+    *battery_slots[slot].state = SLOT_WARN_DAC_INVALID_VALUE;
     return false;
   }
   controllerTxPackage.len = 3;
@@ -48,11 +50,11 @@ bool DAC_SingleWrite(uint8_t slot, uint16_t channel_value) {
   // Write data to DAC
   if (!i2c_hal.write(DAC_TARGET_ADDRESS)) {
         printf("I2C DAC Write Error: Failed to write to DAC.\n");
+        *battery_slots[slot].state = SLOT_ERR_DAC_WRITE_FAILED;
         return false;
     }
 
   DAC_UpdateOutput();
 
   return true;
-
-}
+}