From fbb9bbdbbb05ccd52691786ec4cc321a2b565428 Mon Sep 17 00:00:00 2001 From: helgie22 Date: Thu, 24 Sep 2026 11:02:37 +0000 Subject: [PATCH] added state machine behaviour with switch/case --- .vscode/c_cpp_properties.json | 8 +- .vscode/launch.json | 6 +- src/main.cpp | 191 ++++++++++++++++++++++++---------- 3 files changed, 142 insertions(+), 63 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 931738c..67c239e 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -8,8 +8,8 @@ { "name": "PlatformIO", "includePath": [ - "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include", - "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src", + "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/include", + "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/src", "C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino", "C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", @@ -22,8 +22,8 @@ "browse": { "limitSymbolsToIncludedHeaders": true, "path": [ - "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include", - "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src", + "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/include", + "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/src", "C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino", "C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", diff --git a/.vscode/launch.json b/.vscode/launch.json index 120fa44..270540e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,7 +12,7 @@ "type": "platformio-debug", "request": "launch", "name": "PIO Debug", - "executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", + "executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/.pio/build/nanoatmega328new/firmware.elf", "projectEnvName": "nanoatmega328new", "toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin", "internalConsoleOptions": "openOnSessionStart", @@ -25,7 +25,7 @@ "type": "platformio-debug", "request": "launch", "name": "PIO Debug (skip Pre-Debug)", - "executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", + "executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/.pio/build/nanoatmega328new/firmware.elf", "projectEnvName": "nanoatmega328new", "toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin", "internalConsoleOptions": "openOnSessionStart" @@ -34,7 +34,7 @@ "type": "platformio-debug", "request": "launch", "name": "PIO Debug (without uploading)", - "executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", + "executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/.pio/build/nanoatmega328new/firmware.elf", "projectEnvName": "nanoatmega328new", "toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin", "internalConsoleOptions": "openOnSessionStart", diff --git a/src/main.cpp b/src/main.cpp index 7b09aef..518cc24 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,87 +1,166 @@ #include #include #include +#include #include "rotary_encoder_driver.h" #include "motor_driver.h" #include "p_controller.h" -RotaryEncoderDriver encoder; -MotorDriver motor; +// RotaryEncoderDriver encoder; +// MotorDriver motor; -P_controller controller(12.5); +// P_controller controller(12.5); -const double REF_RPM1 = 30.0; -const double REF_RPM2 = 50.0; +// const double REF_RPM1 = 30.0; +// const double REF_RPM2 = 50.0; -const uint16_t STEP1_AT_SAMPLE = 50; -const uint16_t STEP2_AT_SAMPLE = 500; +// const uint16_t STEP1_AT_SAMPLE = 50; +// const uint16_t STEP2_AT_SAMPLE = 500; -const uint16_t STOP_AT_SAMPLE = 1000; +// const uint16_t STOP_AT_SAMPLE = 1000; + +unsigned long state_entry_time; + +unsigned long time_in_state() +{ + return millis() - state_entry_time; +} int main() { init(); Serial.begin(115200); - - cli(); - encoder.begin(); - motor.begin(); + // Serial.begin(9600); sei(); + enum State + { + Initialization, + Operational, + Pre_Operational, + Stopped + }; + + State state = State::Initialization; // Define state variable and set the initial state + + // cli(); + // encoder.begin(); + // motor.begin(); + // sei(); + // Mótor stopp og smá bið svo kerfið sé í kyrrstöðu áður en mælingin hefst - motor.stop(); - _delay_ms(2000); + // motor.stop(); + // _delay_ms(2000); - Serial.println("time_ms,ref_rpm,actual_rpm,pwm_requested,pwm_actual"); - Serial.println("0.0,0.0,0.0,0.0,0.0"); + // Serial.println("time_ms,ref_rpm,actual_rpm,pwm_requested,pwm_actual"); + // Serial.println("0.0,0.0,0.0,0.0,0.0"); - encoder.reset_speed_measurement(); + // encoder.reset_speed_measurement(); - uint16_t sample = 0; - double ref = 0.0; + // uint16_t sample = 0; + // double ref = 0.0; - while (true) { - if (encoder.new_speed_available()) { - sample++; + while (1) { + int c = 0; // for incoming serial data - float time_ms = sample * 4.0f; + if (Serial.available() > 0) + { + c = Serial.read(); + Serial.print("I received: "); + Serial.println(c, DEC); + } - // Step-inntak: viðmiðið hoppar úr 0 í REF_RPM á STEP_AT_SAMPLE - if (sample == STEP1_AT_SAMPLE) { - ref = REF_RPM1; + switch (state) + { + case State::Initialization: + if (c == 'p') + { + state_entry_time = millis(); + // State::Idle exit action + // event1 transition action + Serial.println("Initialization finished"); + // set new target state + state = State::Pre_Operational; + // target state entry action + Serial.println("Going into Pre Operation"); + } + if (c == 'o') + { + state_entry_time = millis(); + // State::Idle exit action + // event2 transition action + Serial.println("Initialization finished"); + // set new target state + state = State::Operational; + // target state entry action + Serial.println("Going straight to Operation"); + } + break; + + case State::Operational: + if (c == 'i') + { + state_entry_time = millis(); + // State::Cooling exit action + Serial.println("Operation over"); + // event3 transition action + // set new target state + state = State::Initialization; + // target state entry action + Serial.println("Initialization state"); + } + if (c == 's') + { + state_entry_time = millis(); + // State::Cooling exit action + Serial.println("Operation over"); + // event4 transition action + // set new target state + state = State::Stopped; + // target state entry action + Serial.println("Stopping"); + } + break; + + case State::Pre_Operational: + if (c == 'o') + { + state_entry_time = millis(); + // State::Heating exit action + Serial.println("Pre Operation over"); + // event3 transition action + // set new target state + state = State::Operational; + // target state entry action + Serial.println("Going to Operation"); + } + if (c == 's') + { + state_entry_time = millis(); + // State::Heating exit action + Serial.println("Pre Operation over"); + // event4 transition action + // set new target state + state = State::Stopped; + // target state entry action + Serial.println("Stopping"); + } + break; + + case State::Stopped: + if (c == 'i') + { + state_entry_time = millis(); + // State::Failure exit action + Serial.println("Stop over"); + // event3 transition action + // set new target state + state = State::Initialization; + // target state entry action + Serial.println("Initialization state"); } - if (sample == STEP2_AT_SAMPLE) { - ref = REF_RPM2; - } - - double actual = encoder.rpm(); - - double pwm_percent = controller.update(ref, actual); - double pwm_requested = pwm_percent * 2.55; - - double pwm_actual = pwm_requested; - if (pwm_actual > 255.0) pwm_actual = 255.0; - if (pwm_actual < -255.0) pwm_actual = -255.0; - - motor.set(pwm_actual); - - Serial.print(time_ms, 1); - Serial.print(","); - Serial.print(ref, 1); - Serial.print(","); - Serial.print(actual, 2); - Serial.print(","); - Serial.print(pwm_requested, 1); - Serial.print(","); - Serial.println(pwm_actual, 1); - - if (sample >= STOP_AT_SAMPLE) { - motor.stop(); - while (true) { - } } } - } return 0; }