Nýjir kóðar og uppfærðir gamlir kóðar fyrir arduino yfir í zephyr

This commit is contained in:
2026-10-08 15:18:32 +00:00
parent 53091f68df
commit c2d8780478
24 changed files with 922 additions and 338 deletions
+12 -4
View File
@@ -1,14 +1,22 @@
#ifndef DIGITAL_IN_H
#define DIGITAL_IN_H
#include <stdint.h>
#include <zephyr/drivers/gpio.h>
struct Digital_in {
uint8_t pin_mask;
enum : uint8_t {
ENCODER_C1 = 0,
ENCODER_C2 = 1
};
const struct gpio_dt_spec *gpio;
explicit Digital_in(uint8_t pin_id);
Digital_in(uint8_t pin);
void init();
bool is_hi();
bool is_lo();
};
#endif
#endif
+14 -2
View File
@@ -1,15 +1,27 @@
#ifndef DIGITAL_OUT_H
#define DIGITAL_OUT_H
#include<stdint.h>
#include <zephyr/drivers/gpio.h>
struct Digital_out {
uint8_t pin_mask;
// uint8_t pin_mask;
Digital_out(uint8_t pin);
enum : uint8_t {
STATUS_LED = 0
};
explicit Digital_out(uint8_t pin_id);
void init();
void toggle();
void set_hi();
void set_lo();
private:
const struct gpio_dt_spec *gpio;
};
#endif
+9 -2
View File
@@ -1,15 +1,22 @@
#ifndef INITIALIZATION_STATE_H
#define INITIALIZATION_STATE_H
#include <stdint.h>
#include "state.h"
class InitializationState : public State
{
private:
static const unsigned long WAIT_MS = 2000;
// static const unsigned long WAIT_MS = 2000;
// unsigned long enter_time_ms = 0;
static constexpr int64_t WAIT_MS = 2000;
int64_t enter_time_ms = 0;
unsigned long enter_time_ms = 0;
public:
void enter() override;
+16 -13
View File
@@ -1,6 +1,8 @@
#ifndef MOTOR_CONTROLLER_H
#define MOTOR_CONTROLLER_H
#include <stdint.h>
#include "rotary_encoder_driver.h"
#include "motor_driver.h"
#include "p_controller.h"
@@ -14,7 +16,8 @@ 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
//static const uint8_t LED_PIN = 5; // PB5 = D13 = LED_BUILTIN
static constexpr uint8_t LED_PIN = Digital_out::STATUS_LED;
State* current_state;
@@ -23,9 +26,9 @@ private:
State& operational_state;
State& stopped_state;
RotaryEncoderDriver& encoder;
MotorDriver& motor;
P_controller* controller;
RotaryEncoderDriver &encoder;
MotorDriver &motor;
P_controller *controller;
Digital_out led;
@@ -38,13 +41,13 @@ private:
public:
MotorController(
RotaryEncoderDriver& encoder,
MotorDriver& motor,
P_controller& controller,
State& initialization,
State& pre_operational,
State& operational,
State& stopped
RotaryEncoderDriver &encoder,
MotorDriver &motor,
P_controller &controller,
State &initialization,
State &pre_operational,
State &operational,
State &stopped
);
@@ -54,7 +57,7 @@ public:
void command(char c);
void transition_to(State& new_state);
void transition_to(State &new_state);
void to_initialization();
@@ -85,7 +88,7 @@ public:
void set_Ti(double value);
void set_controller(P_controller& new_controller);
void set_controller(P_controller &new_controller);
void led_on();
+3
View File
@@ -17,6 +17,9 @@ public:
void set(double pwm);
bool fault_active(); // Fault detection for part 2
private:
bool initialized; // Flag that ignores motor commands if setup fails
};
#endif
+4 -3
View File
@@ -9,11 +9,12 @@ class P_controller;
class PreOperationalState : public State
{
private:
P_controller& p_controller;
P_controller& pi_controller;
P_controller &p_controller;
P_controller &pi_controller;
bool pi_selected;
unsigned long previous_time;
//unsigned long previous_time;
int64_t previous_time;
bool led_state;
//bool waiting_for_kp;
+40 -13
View File
@@ -2,8 +2,9 @@
#define ROTARY_ENCODER_DRIVER_H
#include <stdint.h>
#include <zephyr/drivers/gpio.h>
#include "digital_in.h"
//#include "digital_in.h"
#include "timer_usec.h"
class RotaryEncoderDriver {
@@ -11,6 +12,19 @@ public:
static constexpr uint16_t SAMPLE_PERIOD_US = 4000; // Ts = 4 ms
static constexpr float COUNTS_PER_REV = 1050.0f;
RotaryEncoderDriver();
// Returns 0 on success, or a negative Zephyr error code.
int begin();
void update();
int32_t position();
void update_delta();
float count_per_sec();
float rpm();
bool new_speed_available();
void reset_speed_measurement();
private:
static const uint8_t HIST_LEN = 4; // 4 x Ts = 16 ms gluggi fyrir rpm-utreikning
@@ -21,29 +35,42 @@ private:
volatile int32_t history[HIST_LEN];
volatile uint8_t hist_idx;
Digital_in eA;
Digital_in eB;
//Digital_in eA;
//Digital_in eB;
struct gpio_callback encoder_c1_callback;
Timer_usec timer;
public:
RotaryEncoderDriver();
static void gpio_edge_handler(
const structdevice *dev,
struct gpio_callback *callback,
uint32_t pins);
void begin();
static void timer_expired(void *context);
void update();
// public:
// RotaryEncoderDriver();
int32_t position();
// void begin();
void update_delta();
// void update();
float count_per_sec();
// int32_t position();
float rpm();
// void update_delta();
// float count_per_sec();
// float rpm();
// bool new_speed_available();
// void reset_speed_measurement();
bool new_speed_available();
void reset_speed_measurement();
};
#endif
+4 -1
View File
@@ -1,12 +1,15 @@
#ifndef STOPPED_STATE_H
#define STOPPED_STATE_H
#include <stdint.h>
#include "state.h"
class StoppedState : public State
{
private:
unsigned long previous_time; // Used for blinking LED
//unsigned long previous_time; // Used for blinking LED
int64_t previous_time;
bool led_state; // Ditto
public:
+24 -2
View File
@@ -1,9 +1,31 @@
#ifndef TIMER_USEC_H
#define TIMER_USEC_H
#include <avr/io.h>
struct Timer_usec {
//#include <avr/io.h>
#include <stdint.h>
#include <zephyr/kernel.h>
// struct Timer_usec {
// Timer_usec();
// void init(uint16_t period_us);
//};
class Timer_usec {
public:
using callback_t = void (*)(void *context);
Timer_usec();
void set_callback(callback_t callback, void *context);
void init(uint16_t period_us);
private:
struct k_timer timer;
callback_t callback;
void *callback_context;
static void expiry_handler(struct k_timer *timer);
};
#endif