File management and Completed Part 1

This commit is contained in:
Thorsteinngretar
2026-10-03 02:41:04 +00:00
parent 0b52148272
commit 6f3c388cef
37 changed files with 1458 additions and 0 deletions
@@ -0,0 +1,37 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the convention is to give header files names that end with `.h'.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
@@ -0,0 +1,26 @@
#ifndef CONTEXT_H
#define CONTEXT_H
#include <state.h>
class Context
{
public:
Context(State& initialState);
void transition_to(State& newState);
void do_work();
void reset();
void set_operational();
void set_pre_operational();
void fault();
const char* current_state_name() const;
private:
State* state;
};
#endif
@@ -0,0 +1,17 @@
#ifndef CONTROL_TIMER_H
#define CONTROL_TIMER_H
#include <stdint.h>
class Control_timer
{
public:
void init();
// Returns true once for every 4 ms control tick.
bool take_tick();
uint32_t tick_count() const;
};
#endif
@@ -0,0 +1,19 @@
#ifndef CONTROLLER_H
#define CONTROLLER_H
class Controller
{
public:
virtual ~Controller() = default;
virtual double update(
double reference,
double actual
) = 0;
virtual void reset() = 0;
virtual const char* name() const = 0;
};
#endif
@@ -0,0 +1,22 @@
#ifndef DIGITAL_IN_H
#define DIGITAL_IN_H
#include <stdint.h>
class Digital_in
{
public:
// pin is the PORTB bit number:
// PB0 = D8, PB1 = D9, ..., PB5 = D13
Digital_in(uint8_t pin);
void init();
bool is_hi() const;
bool is_lo() const;
private:
uint8_t pinMask;
};
#endif
@@ -0,0 +1,23 @@
#ifndef DIGITAL_OUT_H
#define DIGITAL_OUT_H
#include <stdint.h>
class Digital_out
{
public:
// pin is the PORTB bit number:
// PB0 = D8, ..., PB5 = D13
Digital_out(uint8_t pin);
void init();
void set_hi();
void set_lo();
void toggle();
private:
uint8_t pinMask;
};
#endif
@@ -0,0 +1,28 @@
#ifndef ENCODER_H
#define ENCODER_H
#include <stdint.h>
#include <digital_in.h>
class Encoder
{
public:
Encoder(uint8_t c1Pin, uint8_t c2Pin);
void init();
void update();
int32_t position() const;
private:
Digital_in c1;
Digital_in c2;
uint8_t c1Pin;
bool previousC1;
volatile int32_t count;
};
#endif
@@ -0,0 +1,35 @@
#ifndef INITIALIZATION_H
#define INITIALIZATION_H
#include <state.h>
#include <speed_controller.h>
#include <digital_out.h>
class Initialization : public State
{
public:
Initialization(
Speed_controller& speedController,
Digital_out& statusLED
);
const char* name() const override;
void on_entry() override;
void on_do() override;
void on_exit() override;
void set_operational() override;
void set_operational_state(
State& operationalState
);
private:
Speed_controller& speedController;
Digital_out& statusLED;
State* operationalState;
};
#endif
@@ -0,0 +1,26 @@
#ifndef MOTOR_DRIVER_H
#define MOTOR_DRIVER_H
class Motor_driver
{
public:
void init();
// Range: -255 ... +255
void set_command(double command);
void coast();
void brake();
double command() const;
private:
double currentCommand;
void enable_pwm_outputs();
unsigned char magnitude(double value) const;
};
#endif
@@ -0,0 +1,39 @@
#ifndef OPERATIONAL_H
#define OPERATIONAL_H
#include <stdint.h>
#include <state.h>
#include <speed_controller.h>
#include <digital_out.h>
class Operational : public State
{
public:
Operational(
Speed_controller& speedController,
Digital_out& statusLED
);
const char* name() const override;
void on_entry() override;
void on_do() override;
void on_exit() override;
void reset() override;
void set_initialization_state(
State& initializationState
);
private:
Speed_controller& speedController;
Digital_out& statusLED;
State* initializationState;
uint8_t logCounter;
};
#endif
@@ -0,0 +1,29 @@
#ifndef P_CONTROLLER_H
#define P_CONTROLLER_H
#include <controller.h>
class P_controller : public Controller
{
public:
P_controller(double kp);
double update(
double reference,
double actual
) override;
void reset() override;
const char* name() const override;
void set_kp(double kp);
double get_kp() const;
private:
double kp;
double limit(double value) const;
};
#endif
@@ -0,0 +1,47 @@
#ifndef SPEED_CONTROLLER_H
#define SPEED_CONTROLLER_H
#include <encoder.h>
#include <speed_estimator.h>
#include <motor_driver.h>
#include <controller.h>
class Speed_controller
{
public:
Speed_controller(
Encoder& encoder,
Speed_estimator& estimator,
Motor_driver& motor,
Controller& controller
);
void start();
void stop();
void brake();
void update();
void set_reference(double rpm);
double reference() const;
void set_controller(Controller& controller);
double actual_rpm() const;
double control_output() const;
private:
Encoder& encoder;
Speed_estimator& estimator;
Motor_driver& motor;
Controller* controller;
bool running;
double referenceRPM;
double actualRPM;
double controlOutput;
};
#endif
@@ -0,0 +1,37 @@
#ifndef SPEED_ESTIMATOR_H
#define SPEED_ESTIMATOR_H
#include <stdint.h>
class Speed_estimator
{
public:
Speed_estimator(
double countsPerRevolution,
double sampleTimeSeconds
);
void reset(int32_t initialPosition);
double update(int32_t currentPosition);
double rpm() const;
private:
static const uint8_t WINDOW_SIZE = 10;
double countsPerRevolution;
double sampleTime;
int32_t previousPosition;
int32_t countHistory[WINDOW_SIZE];
int32_t rollingCounts;
uint8_t historyIndex;
uint8_t samplesFilled;
double currentRPM;
};
#endif
@@ -0,0 +1,43 @@
#ifndef STATE_H
#define STATE_H
class Context;
class State
{
protected:
Context* context_ = nullptr;
public:
virtual ~State() = default;
void set_context(Context* context)
{
context_ = context;
}
virtual const char* name() const = 0;
virtual void on_entry() = 0;
virtual void on_do() = 0;
virtual void on_exit() = 0;
// Events
virtual void reset()
{
}
virtual void set_operational()
{
}
virtual void set_pre_operational()
{
}
virtual void fault()
{
}
};
#endif