From eb402d5da549dd1faba08a4b28a174f9a380f24d Mon Sep 17 00:00:00 2001 From: isakg22 Date: Thu, 1 Oct 2026 13:38:28 +0000 Subject: [PATCH] =?UTF-8?q?Uppf=C3=A6r=C3=B0ur=20k=C3=B3=C3=B0i=20sem=20te?= =?UTF-8?q?kur=20=C3=A1=20m=C3=B3ti=20input=20fyrir=20Kp=20og=20Ti,=20?= =?UTF-8?q?=C3=A1samt=20=C3=BEv=C3=AD=20a=C3=B0=20uppf=C3=A6ra=20UML=20dia?= =?UTF-8?q?gram?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/diagrams/src/motor_state.wsd | 17 +++++-- include/pre_operational_state.h | 16 +++++++ src/main.cpp | 8 ++-- src/pre_operational_state.cpp | 77 ++++++++++++++++++++++++++++++- 4 files changed, 110 insertions(+), 8 deletions(-) diff --git a/docs/diagrams/src/motor_state.wsd b/docs/diagrams/src/motor_state.wsd index 862e45c..d864398 100644 --- a/docs/diagrams/src/motor_state.wsd +++ b/docs/diagrams/src/motor_state.wsd @@ -16,6 +16,12 @@ state Initialization Initialization : Do / Wait 2 seconds Initialization : Exit / Send Boot-up message +state PreOperational + PreOperational : Entry / Stop motor + PreOperational : Do / Blink LED at 1 Hz + PreOperational : Do / Accept Kp and Ti configuration + PreOperational : Exit / LED off + state Operational Operational : Entry / LED on Operational : Do / Run speed controller @@ -27,10 +33,15 @@ state Stopped Stopped : Exit / LED off Initialization --> Operational : initialization complete -Operational --> Initialization : reset + +PreOperational --> Operational : set operational [o] +PreOperational --> Initialization : reset [r] + +Operational --> PreOperational : set pre-operational [p] +Operational --> Initialization : reset [r] Operational --> Stopped : fault detected -Stopped --> Initialization : reset -Stopped --> Operational : set operational +Stopped --> Initialization : reset [r] +Stopped --> Operational : set operational [o] @enduml diff --git a/include/pre_operational_state.h b/include/pre_operational_state.h index 66cf018..7d968f6 100644 --- a/include/pre_operational_state.h +++ b/include/pre_operational_state.h @@ -2,6 +2,7 @@ #define PRE_OPERATIONAL_STATE_H #include "state.h" +#include "stdint.h" class PreOperationalState : public State { @@ -9,6 +10,21 @@ private: unsigned long previous_time; bool led_state; + //bool waiting_for_kp; + //bool waiting_for_ti; + + enum InputMode + { + NONE, + ENTER_KP, + ENTER_TI + }; + + InputMode input_mode; + + char input_buffer[16]; + uint8_t input_index; + public: PreOperationalState(); diff --git a/src/main.cpp b/src/main.cpp index d779391..af7f497 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,10 +81,10 @@ int main() { char c = Serial.read(); - if (c != '\n' && c != '\r') - { - motor_controller.command(c); - } + //if (c != '\n' && c != '\r') // Tók þetta út svo það + //{ // svo það væri hægt að setja + motor_controller.command(c); // inn gildi fyrir Kp og Ti + //} } diff --git a/src/pre_operational_state.cpp b/src/pre_operational_state.cpp index bb9f198..e74b385 100644 --- a/src/pre_operational_state.cpp +++ b/src/pre_operational_state.cpp @@ -2,10 +2,15 @@ #include "motor_controller.h" #include +#include PreOperationalState::PreOperationalState() : previous_time(0), - led_state(false) + led_state(false), + //waiting_for_kp(false), + //waiting_for_ti(false), + input_mode(NONE), + input_index(0) { } @@ -45,6 +50,60 @@ void PreOperationalState::update() void PreOperationalState::command(char c) { + // We are currently entering a Kp or Ti value + if (input_mode != NONE) + { + // Enter pressed + if (c == '\n' || c == '\r') + { + if (input_index > 0) + { + // End the string + input_buffer[input_index] = '\0'; + + // Convert text to a number + double value = atof(input_buffer); + + if (input_mode == ENTER_KP) + { + context->set_Kp(value); + + Serial.print("Kp set to: "); + Serial.println(value); + } + else if (input_mode == ENTER_TI) + { + context->set_Ti(value); + + Serial.print("Ti set to: "); + Serial.println(value); + } + } + + // Finished entering a value + input_index = 0; + input_mode = NONE; + + return; + } + + // Store number characters + if ((c >= '0' && c <= '9') || + c == '.' || + c == '-') + { + if (input_index < sizeof(input_buffer) - 1) + { + input_buffer[input_index] = c; + input_index++; + } + } + + return; + } + + + // Normal state commands if (c == 'o' || c == 'O') { Serial.println("Command: set operational"); @@ -58,6 +117,22 @@ void PreOperationalState::command(char c) context->to_initialization(); } + + else if (c == 'k' || c == 'K') + { + input_mode = ENTER_KP; + input_index = 0; + + Serial.println("Enter Kp and press Enter:"); + } + + else if (c == 't' || c == 'T') + { + input_mode = ENTER_TI; + input_index = 0; + + Serial.println("Enter Ti and press Enter:"); + } } void PreOperationalState::exit()