Nýtt branch og kóðar/files fyrir part 1

This commit is contained in:
2026-09-24 11:20:26 +00:00
parent 46c761dd25
commit 00c251b26d
11 changed files with 173 additions and 92 deletions
View File
View File
View File
+10 -3
View File
@@ -4,11 +4,11 @@
#include <stdint.h> #include <stdint.h>
#include "digital_in.h" #include "digital_in.h"
#include "digital_out.h" //#include "digital_out.h"
#include "timer_usec.h" #include "timer_usec.h"
class RotaryEncoderDriver { class RotaryEncoderDriver {
static const uint8_t HIST_LEN = 4; // 8 x 5 ms = 40 ms gluggi fyrir rpm-utreikning static const uint8_t HIST_LEN = 4; // 4 x 4 ms = 16 ms gluggi fyrir rpm-utreikning
volatile uint8_t cycle; volatile uint8_t cycle;
volatile int16_t count; volatile int16_t count;
@@ -18,7 +18,7 @@ class RotaryEncoderDriver {
volatile int16_t history[HIST_LEN]; volatile int16_t history[HIST_LEN];
volatile uint8_t hist_idx; volatile uint8_t hist_idx;
Digital_out led; //Digital_out led;
Digital_in eA; Digital_in eA;
Digital_in eB; Digital_in eB;
@@ -29,14 +29,21 @@ public:
volatile int16_t delta; volatile int16_t delta;
RotaryEncoderDriver(); RotaryEncoderDriver();
void begin(); void begin();
void update(); void update();
int position(); int position();
void update_delta(); void update_delta();
float count_per_sec(); float count_per_sec();
float rpm(); float rpm();
bool new_speed_available(); bool new_speed_available();
void reset_speed_measurement(); void reset_speed_measurement();
}; };
+32
View File
@@ -0,0 +1,32 @@
#ifndef STATE_H
#define STATE_H
class MotorController;
class State
{
protected:
MotorController* context;
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
View File
+13 -72
View File
@@ -1,87 +1,28 @@
#include <avr/io.h> int main()
#include <util/delay.h> {
#include <Arduino.h>
#include "rotary_encoder_driver.h"
#include "motor_driver.h"
#include "p_controller.h"
RotaryEncoderDriver encoder;
MotorDriver motor;
P_controller controller(12.5);
const double REF_RPM1 = 30.0;
const double REF_RPM2 = 50.0;
const uint16_t STEP1_AT_SAMPLE = 50;
const uint16_t STEP2_AT_SAMPLE = 500;
const uint16_t STOP_AT_SAMPLE = 1000;
int main() {
init(); init();
Serial.begin(115200); Serial.begin(115200);
cli(); cli();
encoder.begin(); encoder.begin();
motor.begin(); motor.begin();
sei(); sei();
// Mótor stopp og smá bið svo kerfið sé í kyrrstöðu áður en mælingin hefst system.begin();
motor.stop();
_delay_ms(2000);
Serial.println("time_ms,ref_rpm,actual_rpm,pwm_requested,pwm_actual"); while (true)
Serial.println("0.0,0.0,0.0,0.0,0.0"); {
if (Serial.available())
encoder.reset_speed_measurement(); {
char c = Serial.read();
uint16_t sample = 0; system.command(c);
double ref = 0.0;
while (true) {
if (encoder.new_speed_available()) {
sample++;
float time_ms = sample * 4.0f;
// Step-inntak: viðmiðið hoppar úr 0 í REF_RPM á STEP_AT_SAMPLE
if (sample == STEP1_AT_SAMPLE) {
ref = REF_RPM1;
} }
if (sample == STEP2_AT_SAMPLE) { system.update();
ref = REF_RPM2;
} }
double actual = encoder.rpm();
double pwm_percent = controller.update(ref, actual);
double pwm_requested = pwm_percent * 2.55;
double pwm_actual = pwm_requested;
if (pwm_actual > 255.0) pwm_actual = 255.0;
if (pwm_actual < -255.0) pwm_actual = -255.0;
motor.set(pwm_actual);
Serial.print(time_ms, 1);
Serial.print(",");
Serial.print(ref, 1);
Serial.print(",");
Serial.print(actual, 2);
Serial.print(",");
Serial.print(pwm_requested, 1);
Serial.print(",");
Serial.println(pwm_actual, 1);
if (sample >= STOP_AT_SAMPLE) {
motor.stop();
while (true) {
}
}
}
}
return 0; return 0;
} }
+88
View File
@@ -0,0 +1,88 @@
/* #include <avr/io.h>
#include <util/delay.h>
#include <Arduino.h>
#include "rotary_encoder_driver.h"
#include "motor_driver.h"
#include "p_controller.h"
RotaryEncoderDriver encoder;
MotorDriver motor;
P_controller controller(12.5);
const double REF_RPM1 = 30.0;
const double REF_RPM2 = 50.0;
const uint16_t STEP1_AT_SAMPLE = 50;
const uint16_t STEP2_AT_SAMPLE = 500;
const uint16_t STOP_AT_SAMPLE = 1000;
int main() {
init();
Serial.begin(115200);
cli();
encoder.begin();
motor.begin();
sei();
// Mótor stopp og smá bið svo kerfið sé í kyrrstöðu áður en mælingin hefst
motor.stop();
_delay_ms(2000);
Serial.println("time_ms,ref_rpm,actual_rpm,pwm_requested,pwm_actual");
Serial.println("0.0,0.0,0.0,0.0,0.0");
encoder.reset_speed_measurement();
uint16_t sample = 0;
double ref = 0.0;
while (true) {
if (encoder.new_speed_available()) {
sample++;
float time_ms = sample * 4.0f;
// Step-inntak: viðmiðið hoppar úr 0 í REF_RPM á STEP_AT_SAMPLE
if (sample == STEP1_AT_SAMPLE) {
ref = REF_RPM1;
}
if (sample == STEP2_AT_SAMPLE) {
ref = REF_RPM2;
}
double actual = encoder.rpm();
double pwm_percent = controller.update(ref, actual);
double pwm_requested = pwm_percent * 2.55;
double pwm_actual = pwm_requested;
if (pwm_actual > 255.0) pwm_actual = 255.0;
if (pwm_actual < -255.0) pwm_actual = -255.0;
motor.set(pwm_actual);
Serial.print(time_ms, 1);
Serial.print(",");
Serial.print(ref, 1);
Serial.print(",");
Serial.print(actual, 2);
Serial.print(",");
Serial.print(pwm_requested, 1);
Serial.print(",");
Serial.println(pwm_actual, 1);
if (sample >= STOP_AT_SAMPLE) {
motor.stop();
while (true) {
}
}
}
}
return 0;
}
*/
View File
View File
+27 -14
View File
@@ -12,17 +12,20 @@ RotaryEncoderDriver::RotaryEncoderDriver()
, led(5) , led(5)
, eA(2) , eA(2)
, eB(5) , eB(5)
, delta(0)
{ {
for (uint8_t i = 0; i < HIST_LEN; i++) { for (uint8_t i = 0; i < HIST_LEN; i++)
{
history[i] = 0; history[i] = 0;
} }
} }
void RotaryEncoderDriver::begin() { void RotaryEncoderDriver::begin()
{
driver = this; driver = this;
led.init(); //led.init();
eA.init(); eA.init();
eB.init(); eB.init();
@@ -32,12 +35,14 @@ void RotaryEncoderDriver::begin() {
timer.init(4000); timer.init(4000);
} }
void RotaryEncoderDriver::update() { void RotaryEncoderDriver::update()
{
count += eB.is_hi() ? 1 : -1; count += eB.is_hi() ? 1 : -1;
led.set_hi(); //led.set_hi();
} }
int RotaryEncoderDriver::position() { int RotaryEncoderDriver::position()
{
uint8_t oldSREG = SREG; uint8_t oldSREG = SREG;
@@ -47,27 +52,31 @@ int RotaryEncoderDriver::position() {
SREG = oldSREG; SREG = oldSREG;
led.set_lo(); //led.set_lo();
return local_count; return local_count;
} }
void RotaryEncoderDriver::update_delta() { void RotaryEncoderDriver::update_delta()
{
int16_t oldest = history[hist_idx]; int16_t oldest = history[hist_idx];
history[hist_idx] = count; history[hist_idx] = count;
hist_idx = (hist_idx + 1) % HIST_LEN; hist_idx = (hist_idx + 1) % HIST_LEN;
delta = count - oldest; // breyting yfir HIST_LEN * Ts (40 ms) glugga delta = count - oldest; // breyting yfir HIST_LEN * Ts (16 ms) glugga
prev_count = count; prev_count = count;
speed_ready = true; speed_ready = true;
led.set_lo(); //led.set_lo();
} }
float RotaryEncoderDriver::count_per_sec() { float RotaryEncoderDriver::count_per_sec()
{
uint8_t oldSREG = SREG; uint8_t oldSREG = SREG;
@@ -118,18 +127,22 @@ void RotaryEncoderDriver::reset_speed_measurement()
delta = 0; delta = 0;
speed_ready = false; speed_ready = false;
for (uint8_t i = 0; i < HIST_LEN; i++) { for (uint8_t i = 0; i < HIST_LEN; i++)
{
history[i] = count; history[i] = count;
} }
hist_idx = 0; hist_idx = 0;
SREG = oldSREG; SREG = oldSREG;
} }
ISR(INT0_vect) { ISR(INT0_vect)
{
driver->update(); driver->update();
} }
ISR(TIMER1_COMPA_vect) { ISR(TIMER1_COMPA_vect)
{
driver->update_delta(); driver->update_delta();
} }