project 2 pasted in to repo

This commit is contained in:
2026-09-24 10:15:22 +00:00
parent fcc0f82dc6
commit 46c761dd25
24 changed files with 1038 additions and 0 deletions
+37
View File
@@ -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
+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
+18
View File
@@ -0,0 +1,18 @@
#ifndef MOTOR_DRIVER_H
#define MOTOR_DRIVER_H
class MotorDriver
{
public:
MotorDriver();
void begin();
void forward();
void reverse();
void stop();
void set(double pwm);
};
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef P_CONTROLLER_H
#define P_CONTROLLER_H
class P_controller
{
private:
double Kp;
public:
P_controller(double Kp);
double update(double ref, double actual);
};
#endif
+43
View File
@@ -0,0 +1,43 @@
#ifndef ROTARY_ENCODER_DRIVER_H
#define ROTARY_ENCODER_DRIVER_H
#include <stdint.h>
#include "digital_in.h"
#include "digital_out.h"
#include "timer_usec.h"
class RotaryEncoderDriver {
static const uint8_t HIST_LEN = 4; // 8 x 5 ms = 40 ms gluggi fyrir rpm-utreikning
volatile uint8_t cycle;
volatile int16_t count;
volatile int16_t prev_count;
volatile bool speed_ready;
volatile int16_t history[HIST_LEN];
volatile uint8_t hist_idx;
Digital_out led;
Digital_in eA;
Digital_in eB;
Timer_usec timer;
public:
volatile int16_t delta;
RotaryEncoderDriver();
void begin();
void update();
int position();
void update_delta();
float count_per_sec();
float rpm();
bool new_speed_available();
void reset_speed_measurement();
};
#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