From 0d03fee529f37563192b229e5bf53abd9f0dba94 Mon Sep 17 00:00:00 2001
From: Kilian Bracher <k.bracher@fasttube.de>
Date: Sat, 12 Apr 2025 19:50:48 +0200
Subject: [PATCH] new state machine description

---
 AMS_Master_Code/Core/Src/main.c             |  5 +----
 AMS_Master_Code/Core/Src/ts_state_machine.c | 12 ++++++------
 2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/AMS_Master_Code/Core/Src/main.c b/AMS_Master_Code/Core/Src/main.c
index 4f3256f..f8f2d0a 100644
--- a/AMS_Master_Code/Core/Src/main.c
+++ b/AMS_Master_Code/Core/Src/main.c
@@ -73,9 +73,6 @@ int pos_air_closed;
 int precharge_closed;
 int pre_and_air_open; // used to be:int precharge_opened = 0; now we read if PC is open from the pin -> high if open
 
-#warning just a fix to make the code compile
-int precharge_opened = 0;
-
 /* USER CODE END PV */
 
 /* Private function prototypes -----------------------------------------------*/
@@ -222,7 +219,7 @@ int main(void)
     }
     if (count % 4 == 0) {
       print_battery_info();
-      debug_log(LOG_LEVEL_INFO, "  Time since last update: %lu ms", HAL_GetTick() - lastTimestamp);
+      print_master_status();
     }
     shunt_check();
     ts_sm_update();
diff --git a/AMS_Master_Code/Core/Src/ts_state_machine.c b/AMS_Master_Code/Core/Src/ts_state_machine.c
index bcdfe09..6f27684 100644
--- a/AMS_Master_Code/Core/Src/ts_state_machine.c
+++ b/AMS_Master_Code/Core/Src/ts_state_machine.c
@@ -53,7 +53,7 @@ void ts_sm_update() {
     break;
   }
 
-    if (ts_state.current_state != old_state) {
+  if (ts_state.current_state != old_state) {
     debug_log(LOG_LEVEL_DEBUG,
               "Transitioned from %s to %s", TSStateToString(old_state),
               TSStateToString(ts_state.current_state));
@@ -93,7 +93,7 @@ TSState ts_sm_update_active() {
     precharge_opened_timestamp = HAL_GetTick();
   } else if (precharge_opened_timestamp != 0 &&
              HAL_GetTick() - precharge_opened_timestamp > 100) {
-    precharge_opened = 1;
+    //precharge_opened = 1;
   }
 
   return TS_ACTIVE;
@@ -113,7 +113,7 @@ TSState ts_sm_update_precharge() {
                PRECHARGE_95_DURATION) {
       precharge_95_reached_timestamp = 0;
       precharge_opened_timestamp = 0;
-      precharge_opened = 0;
+      //precharge_opened = 0;
       return TS_ACTIVE;
     }
   }
@@ -188,7 +188,7 @@ void ts_sm_set_relay_positions(TSState state) {
   case TS_CHARGING:
     ts_sm_set_relay_position(RELAY_NEG, 1);
     ts_sm_set_relay_position(RELAY_POS, 1);
-    ts_sm_set_relay_position(RELAY_PRECHARGE, !precharge_opened);
+    ts_sm_set_relay_position(RELAY_PRECHARGE, 0);
     // TODO: Open precharge relay after a while
     break;
   case TS_PRECHARGE:
@@ -203,7 +203,7 @@ void ts_sm_set_relay_positions(TSState state) {
 void ts_sm_set_relay_position(Relay relay, int closed) {
   static int neg_closed = 0;
   static int pos_closed = 0;
-  static int precharge_closed = 0;
+  static int pre_closed = 0;
 
   GPIO_PinState state = closed ? GPIO_PIN_SET : GPIO_PIN_RESET;
   switch (relay) {
@@ -216,7 +216,7 @@ void ts_sm_set_relay_position(Relay relay, int closed) {
     HAL_GPIO_WritePin(POS_AIR_CTRL_GPIO_Port, POS_AIR_CTRL_Pin, state);
     break;
   case RELAY_PRECHARGE:
-    ts_sm_check_close_wait(&precharge_closed, closed);
+    ts_sm_check_close_wait(&pre_closed, closed);
     HAL_GPIO_WritePin(PRECHARGE_CTRL_GPIO_Port, PRECHARGE_CTRL_Pin, state);
     break;
   }