Setti inn allar skrár frá Project 3 og bætti við overlay í samræmi við controlloerinn og pin out.

This commit is contained in:
2026-10-08 13:11:39 +00:00
parent 9194d71f10
commit 53091f68df
29 changed files with 1645 additions and 0 deletions
+191
View File
@@ -0,0 +1,191 @@
#include "motor_controller.h"
#include "state.h"
#include <Arduino.h>
MotorController::MotorController(
RotaryEncoderDriver& encoder,
MotorDriver& motor,
P_controller& controller,
State& initialization,
State& pre_operational,
State& operational,
State& stopped
)
: current_state(nullptr),
initialization_state(initialization),
pre_operational_state(pre_operational),
operational_state(operational),
stopped_state(stopped),
encoder(encoder),
motor(motor),
controller(&controller),
led(LED_PIN),
reference_rpm(DEFAULT_REFERENCE_RPM),
last_actual_rpm(0.0),
last_pwm(0.0)
{
initialization_state.set_context(this);
pre_operational_state.set_context(this);
operational_state.set_context(this);
stopped_state.set_context(this);
}
// void MotorController::set_states(
// State* initialization,
// State* operational,
// State* stopped
// )
// initialization_state = initialization;
// operational_state = operational;
// stopped_state = stopped;
void MotorController::begin()
{
led.init();
led.set_lo();
transition_to(initialization_state);
}
void MotorController::transition_to(State& new_state)
{
if (current_state != nullptr)
{
current_state->exit();
}
current_state = &new_state;
current_state->enter();
}
void MotorController::update()
{
if (current_state != nullptr)
{
current_state->update();
}
}
void MotorController::command(char c)
{
if (current_state != nullptr)
{
current_state->command(c);
}
}
void MotorController::to_initialization()
{
transition_to(initialization_state);
}
void MotorController::to_pre_operational()
{
transition_to(pre_operational_state);
}
void MotorController::to_operational()
{
transition_to(operational_state);
}
bool MotorController::run_speed_controller()
{
if (!encoder.new_speed_available())
{
return false;
}
double actual = encoder.rpm();
double pwm_percent =
controller->update(reference_rpm, actual);
double pwm =
pwm_percent * PWM_PER_PERCENT;
// Klippt hér svo last_pwm sé nákvæmlega það sem fer á mótorinn
if (pwm > 255.0)
{
pwm = 255.0;
}
else if (pwm < -255.0)
{
pwm = -255.0;
}
motor.set(pwm);
last_actual_rpm = actual;
last_pwm = pwm;
return true;
}
void MotorController::stop_motor()
{
motor.stop();
}
void MotorController::reset_speed_measurement()
{
encoder.reset_speed_measurement();
}
void MotorController::reset_controller()
{
controller->reset();
}
void MotorController::set_Kp(double value)
{
controller->set_Kp(value);
}
void MotorController::set_Ti(double value)
{
controller->set_Ti(value);
}
void MotorController::set_controller(P_controller& new_controller)
{
controller = &new_controller;
controller->reset();
}
void MotorController::to_stopped()
{
transition_to(stopped_state);
}
bool MotorController::fault_active()
{
return motor.fault_active();
}
void MotorController::led_on()
{
led.set_hi();
}
void MotorController::led_off()
{
led.set_lo();
}