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
+14
View File
@@ -0,0 +1,14 @@
#ifndef DIGITAL_IN_H
#define DIGITAL_IN_H
#include <stdint.h>
struct Digital_in {
uint8_t pin_mask;
Digital_in(uint8_t pin);
void init();
bool is_hi();
bool is_lo();
};
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef DIGITAL_OUT_H
#define DIGITAL_OUT_H
#include<stdint.h>
struct Digital_out {
uint8_t pin_mask;
Digital_out(uint8_t pin);
void init();
void toggle();
void set_hi();
void set_lo();
};
#endif
+24
View File
@@ -0,0 +1,24 @@
#ifndef INITIALIZATION_STATE_H
#define INITIALIZATION_STATE_H
#include "state.h"
class InitializationState : public State
{
private:
static const unsigned long WAIT_MS = 2000;
unsigned long enter_time_ms = 0;
public:
void enter() override;
void update() override;
void command(char c) override;
void exit() override;
};
#endif
+95
View File
@@ -0,0 +1,95 @@
#ifndef MOTOR_CONTROLLER_H
#define MOTOR_CONTROLLER_H
#include "rotary_encoder_driver.h"
#include "motor_driver.h"
#include "p_controller.h"
#include "digital_out.h"
class State;
class MotorController
{
private:
static constexpr double DEFAULT_REFERENCE_RPM = 40.0;
static constexpr double PWM_PER_PERCENT = 255.0 / 100.0;
static const uint8_t LED_PIN = 5; // PB5 = D13 = LED_BUILTIN
State* current_state;
State& initialization_state;
State& pre_operational_state;
State& operational_state;
State& stopped_state;
RotaryEncoderDriver& encoder;
MotorDriver& motor;
P_controller* controller;
Digital_out led;
double reference_rpm;
// Síðasta sýni, notað fyrir CSV-útprentun
double last_actual_rpm;
double last_pwm;
public:
MotorController(
RotaryEncoderDriver& encoder,
MotorDriver& motor,
P_controller& controller,
State& initialization,
State& pre_operational,
State& operational,
State& stopped
);
void begin();
void update();
void command(char c);
void transition_to(State& new_state);
void to_initialization();
void to_pre_operational();
void to_operational();
void to_stopped();
bool fault_active();
// Skilar true þegar nýtt sýni var keyrt í gegnum stýringuna
bool run_speed_controller();
double get_reference_rpm() const { return reference_rpm; }
double get_actual_rpm() const { return last_actual_rpm; }
double get_pwm() const { return last_pwm; }
void stop_motor();
void reset_speed_measurement();
void reset_controller();
void set_Kp(double value);
void set_Ti(double value);
void set_controller(P_controller& new_controller);
void led_on();
void led_off();
};
#endif
+22
View File
@@ -0,0 +1,22 @@
#ifndef MOTOR_DRIVER_H
#define MOTOR_DRIVER_H
class MotorDriver
{
public:
MotorDriver();
void begin();
void forward();
void reverse();
void stop();
void set(double pwm);
bool fault_active(); // Fault detection for part 2
};
#endif
+29
View File
@@ -0,0 +1,29 @@
#ifndef OPERATIONAL_STATE_H
#define OPERATIONAL_STATE_H
#include "state.h"
#include <stdint.h>
// Sett á 1 til að prenta CSV (time_ms,ref_rpm,actual_rpm,pwm) í Operational,
// t.d. fyrir step og load response. Einnig hægt með build_flags = -D LOG_CSV=1
#ifndef LOG_CSV
#define LOG_CSV 0
#endif
class OperationalState : public State
{
private:
uint32_t sample_count = 0;
public:
void enter() override;
void update() override;
void command(char c) override;
void exit() override;
};
#endif
+26
View File
@@ -0,0 +1,26 @@
#ifndef P_CONTROLLER_H
#define P_CONTROLLER_H
class P_controller
{
protected:
double Kp;
public:
P_controller(double Kp);
virtual ~P_controller() {}
virtual double update(double ref, double actual);
virtual void reset() {}
virtual void set_Kp(double value)
{
Kp = value;
}
virtual void set_Ti(double value)
{
// P controller does not use Ti
}
};
#endif
+23
View File
@@ -0,0 +1,23 @@
#ifndef PI_CONTROLLER_H
#define PI_CONTROLLER_H
#include "p_controller.h"
class PI_controller : public P_controller
{
private:
static constexpr double U_MAX = 100.0; // %
static constexpr double U_MIN = -100.0; // %
double Ti;
double T;
double integral;
public:
PI_controller(double Kp, double Ti, double T);
double update(double ref, double actual) override;
void reset() override;
void set_Ti(double value) override;
};
#endif
+43
View File
@@ -0,0 +1,43 @@
#ifndef PRE_OPERATIONAL_STATE_H
#define PRE_OPERATIONAL_STATE_H
#include "state.h"
#include "stdint.h"
class P_controller;
class PreOperationalState : public State
{
private:
P_controller& p_controller;
P_controller& pi_controller;
bool pi_selected;
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(P_controller& p_controller, P_controller& pi_controller);
void enter() override;
void update() override;
void command(char c) override;
void exit() override;
};
#endif
+49
View File
@@ -0,0 +1,49 @@
#ifndef ROTARY_ENCODER_DRIVER_H
#define ROTARY_ENCODER_DRIVER_H
#include <stdint.h>
#include "digital_in.h"
#include "timer_usec.h"
class RotaryEncoderDriver {
public:
static constexpr uint16_t SAMPLE_PERIOD_US = 4000; // Ts = 4 ms
static constexpr float COUNTS_PER_REV = 1050.0f;
private:
static const uint8_t HIST_LEN = 4; // 4 x Ts = 16 ms gluggi fyrir rpm-utreikning
volatile int32_t count;
volatile int16_t delta;
volatile bool speed_ready;
volatile int32_t history[HIST_LEN];
volatile uint8_t hist_idx;
Digital_in eA;
Digital_in eB;
Timer_usec timer;
public:
RotaryEncoderDriver();
void begin();
void update();
int32_t position();
void update_delta();
float count_per_sec();
float rpm();
bool new_speed_available();
void reset_speed_measurement();
};
#endif
+32
View File
@@ -0,0 +1,32 @@
#ifndef STATE_H
#define STATE_H
class MotorController;
class State
{
protected:
MotorController* context = nullptr;
public:
virtual ~State()
{
}
void set_context(MotorController* context)
{
this->context = context;
}
virtual void enter() = 0; // LED ON
virtual void update() = 0; // run motor controller
virtual void command(char c) = 0; // Listen for reset
virtual void exit() = 0; // Stop motor
};
#endif
+25
View File
@@ -0,0 +1,25 @@
#ifndef STOPPED_STATE_H
#define STOPPED_STATE_H
#include "state.h"
class StoppedState : public State
{
private:
unsigned long previous_time; // Used for blinking LED
bool led_state; // Ditto
public:
StoppedState();
void enter() override;
void update() override;
void command(char c) override;
void exit() override;
};
#endif
+9
View File
@@ -0,0 +1,9 @@
#ifndef TIMER_USEC_H
#define TIMER_USEC_H
#include <avr/io.h>
struct Timer_usec {
Timer_usec();
void init(uint16_t period_us);
};
#endif