Uppfærður kóði sem tekur á móti input fyrir Kp og Ti, ásamt því að uppfæra UML diagram

This commit is contained in:
2026-10-01 13:38:28 +00:00
parent a5c2da8bae
commit eb402d5da5
4 changed files with 110 additions and 8 deletions
+14 -3
View File
@@ -16,6 +16,12 @@ state Initialization
Initialization : Do / Wait 2 seconds Initialization : Do / Wait 2 seconds
Initialization : Exit / Send Boot-up message 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 state Operational
Operational : Entry / LED on Operational : Entry / LED on
Operational : Do / Run speed controller Operational : Do / Run speed controller
@@ -27,10 +33,15 @@ state Stopped
Stopped : Exit / LED off Stopped : Exit / LED off
Initialization --> Operational : initialization complete 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 Operational --> Stopped : fault detected
Stopped --> Initialization : reset Stopped --> Initialization : reset [r]
Stopped --> Operational : set operational Stopped --> Operational : set operational [o]
@enduml @enduml
+16
View File
@@ -2,6 +2,7 @@
#define PRE_OPERATIONAL_STATE_H #define PRE_OPERATIONAL_STATE_H
#include "state.h" #include "state.h"
#include "stdint.h"
class PreOperationalState : public State class PreOperationalState : public State
{ {
@@ -9,6 +10,21 @@ private:
unsigned long previous_time; unsigned long previous_time;
bool led_state; 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: public:
PreOperationalState(); PreOperationalState();
+4 -4
View File
@@ -81,10 +81,10 @@ int main()
{ {
char c = Serial.read(); char c = Serial.read();
if (c != '\n' && c != '\r') //if (c != '\n' && c != '\r') // Tók þetta út svo það
{ //{ // svo það væri hægt að setja
motor_controller.command(c); motor_controller.command(c); // inn gildi fyrir Kp og Ti
} //}
} }
+76 -1
View File
@@ -2,10 +2,15 @@
#include "motor_controller.h" #include "motor_controller.h"
#include <Arduino.h> #include <Arduino.h>
#include <stdlib.h>
PreOperationalState::PreOperationalState() PreOperationalState::PreOperationalState()
: previous_time(0), : 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) 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') if (c == 'o' || c == 'O')
{ {
Serial.println("Command: set operational"); Serial.println("Command: set operational");
@@ -58,6 +117,22 @@ void PreOperationalState::command(char c)
context->to_initialization(); 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() void PreOperationalState::exit()