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:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
+4
-4
@@ -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
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,10 +2,15 @@
|
||||
#include "motor_controller.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user