Fokkaði upp p2

This commit is contained in:
2026-09-17 12:20:06 +00:00
parent a54dbfbe2e
commit c355c232e6
9 changed files with 259 additions and 199 deletions
+69 -8
View File
@@ -7,10 +7,12 @@ RotaryEncoderDriver::RotaryEncoderDriver()
: cycle(0)
, count(0)
, prev_count(0)
, speed_ready(false)
, led(5)
, eA(2)
, eB(5) {}
void RotaryEncoderDriver::begin() {
driver = this;
@@ -21,7 +23,7 @@ void RotaryEncoderDriver::begin() {
EICRA |= (1<<ISC01) | (1<<ISC00);
EIMSK |= (1<<INT0);
timer.init(100);
timer.init(5000);
}
void RotaryEncoderDriver::update() {
@@ -30,27 +32,85 @@ void RotaryEncoderDriver::update() {
}
int RotaryEncoderDriver::position() {
uint8_t oldSREG = SREG;
cli();
int16_t local_count = count;
SREG = oldSREG;
led.set_lo();
return count;
return local_count;
}
void RotaryEncoderDriver::update_delta() {
if (++cycle == 0) {
delta = count - prev_count;
prev_count = count;
}
speed_ready = true;
led.set_lo();
}
float RotaryEncoderDriver::count_per_sec() {
return delta / (256.0f * 0.0001f);
}
float RotaryEncoderDriver::rpm() {
return (count_per_sec() * 60) / 1050.0;
uint8_t oldSREG = SREG;
cli();
int16_t local_delta = delta;
SREG = oldSREG;
return local_delta / 0.005f;
}
float RotaryEncoderDriver::rpm()
{
const float COUNTS_PER_REV = 1050.0f;
return count_per_sec() * 60.0f / COUNTS_PER_REV;
}
bool RotaryEncoderDriver::new_speed_available()
{
uint8_t oldSREG = SREG;
cli();
bool ready = speed_ready;
if (ready)
{
speed_ready = false;
}
SREG = oldSREG;
return ready;
}
void RotaryEncoderDriver::reset_speed_measurement()
{
uint8_t oldSREG = SREG;
cli();
cycle = 0;
prev_count = count;
delta = 0;
speed_ready = false;
SREG = oldSREG;
}
ISR(INT0_vect) {
driver->update();
}
@@ -58,3 +118,4 @@ ISR(TIMER1_COMPA_vect) {
driver->update_delta();
}