184 lines
4.2 KiB
C++
184 lines
4.2 KiB
C++
#include "pre_operational_state.h"
|
|
#include "motor_controller.h"
|
|
|
|
// #include "p_controller.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <zephyr/kernel.h>
|
|
#include <zephyr/sys/printk.h>
|
|
|
|
// PreOperationalState::PreOperationalState(P_controller &p_controller, P_controller &pi_controller)
|
|
PreOperationalState::PreOperationalState()
|
|
// : p_controller(p_controller),
|
|
// pi_controller(pi_controller),
|
|
// pi_selected(true),
|
|
: previous_time(0),
|
|
led_state(false),
|
|
input_mode(NONE),
|
|
input_index(0)
|
|
{
|
|
}
|
|
|
|
void PreOperationalState::enter()
|
|
{
|
|
printk("State: Pre-operational\n");
|
|
|
|
// The selection survives a reset, so report it (used by capture_serial.py).
|
|
printk("Controller: %s\n", (context->get_controller_type() == ControllerType::PI) ? "PI" : "P");
|
|
// Prófaði þetta, veit ekki hvort þetta skili bool
|
|
|
|
context->stop_motor();
|
|
|
|
context->led_off();
|
|
|
|
led_state = false;
|
|
|
|
previous_time = k_uptime_get();
|
|
}
|
|
|
|
void PreOperationalState::update()
|
|
{
|
|
const int64_t current_time = k_uptime_get();
|
|
|
|
if (current_time - previous_time >= 500)
|
|
{
|
|
previous_time = current_time;
|
|
|
|
led_state = !led_state;
|
|
|
|
if (led_state)
|
|
{
|
|
context->led_on();
|
|
}
|
|
else
|
|
{
|
|
context->led_off();
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
const double value = atof(input_buffer);
|
|
|
|
if (input_mode == ENTER_KP)
|
|
{
|
|
context->set_Kp(value);
|
|
|
|
printk("Kp set to: %.4f\n", value);
|
|
}
|
|
else if (input_mode == ENTER_TI)
|
|
{
|
|
if (context->set_Ti(value))
|
|
{
|
|
printk("Ti set to: %.4f\n", value);
|
|
}
|
|
else
|
|
{
|
|
printk("Ti rejected, must be > 0");
|
|
}
|
|
}
|
|
else if (input_mode == ENTER_RPM)
|
|
{
|
|
context->set_target_rpm(value);
|
|
|
|
printk("Target RPM set to: %.0f\n", 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;
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
// Normal state commands
|
|
if (c == 'o' || c == 'O')
|
|
{
|
|
printk("Command: set operational\n");
|
|
|
|
context->to_operational();
|
|
}
|
|
|
|
else if (c == 'r' || c == 'R')
|
|
{
|
|
printk("Command: reset\n");
|
|
|
|
context->to_initialization();
|
|
}
|
|
|
|
else if (c == 'k' || c == 'K')
|
|
{
|
|
input_mode = ENTER_KP;
|
|
input_index = 0;
|
|
|
|
printk("Enter Kp and press Enter:\n");
|
|
}
|
|
|
|
else if (c == 't' || c == 'T')
|
|
{
|
|
input_mode = ENTER_TI;
|
|
input_index = 0;
|
|
|
|
printk("Enter Ti and press Enter:\n");
|
|
}
|
|
|
|
else if (c == 'v' || c == 'V')
|
|
{
|
|
input_mode = ENTER_RPM;
|
|
input_index = 0;
|
|
|
|
printk("Enter RPM and press Enter:\n");
|
|
}
|
|
|
|
// Select controller algorithm, the controller is passed to the motor controller
|
|
else if (c == 'c' || c == 'C')
|
|
{
|
|
if (context->get_controller_type() == ControllerType::P)
|
|
{
|
|
context->select_controller(ControllerType::PI);
|
|
printk("Controller: PI, Kp=%.4f, Ti=%.4f\n",
|
|
context->get_Kp(), context->get_Ti());
|
|
}
|
|
else
|
|
{
|
|
context->select_controller(ControllerType::P);
|
|
printk("Controller: P, Kp=%.4f\n",
|
|
context->get_Kp());
|
|
}
|
|
}
|
|
}
|
|
|
|
void PreOperationalState::exit()
|
|
{
|
|
context->led_off();
|
|
}
|