Compare commits

4 Commits
Author SHA1 Message Date
isakg22 8c5f98c8df commented out unused encoder 2026-09-17 12:26:19 +00:00
isakg22 9885f03678 yep 2026-09-17 12:21:00 +00:00
isakg22 c355c232e6 Fokkaði upp p2 2026-09-17 12:20:06 +00:00
isakg22 a54dbfbe2e nýtt branch og kóði 2026-09-16 13:48:46 +00:00
11 changed files with 285 additions and 224 deletions
+19 -19
View File
@@ -8,29 +8,29 @@
{ {
"name": "PlatformIO", "name": "PlatformIO",
"includePath": [ "includePath": [
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include", "C:/Users/isakt/Embedded/ESP_Project2_H26/include",
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src", "C:/Users/isakt/Embedded/ESP_Project2_H26/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/cores/arduino",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/HID/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/HID/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SPI/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/Wire/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
"" ""
], ],
"browse": { "browse": {
"limitSymbolsToIncludedHeaders": true, "limitSymbolsToIncludedHeaders": true,
"path": [ "path": [
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include", "C:/Users/isakt/Embedded/ESP_Project2_H26/include",
"C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src", "C:/Users/isakt/Embedded/ESP_Project2_H26/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/cores/arduino",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/HID/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/HID/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SPI/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
"C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/Wire/src", "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
"" ""
] ]
}, },
@@ -45,7 +45,7 @@
], ],
"cStandard": "gnu11", "cStandard": "gnu11",
"cppStandard": "gnu++11", "cppStandard": "gnu++11",
"compilerPath": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe", "compilerPath": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe",
"compilerArgs": [ "compilerArgs": [
"-mmcu=atmega328p", "-mmcu=atmega328p",
"" ""
+6 -6
View File
@@ -12,9 +12,9 @@
"type": "platformio-debug", "type": "platformio-debug",
"request": "launch", "request": "launch",
"name": "PIO Debug", "name": "PIO Debug",
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", "executable": "C:/Users/isakt/Embedded/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
"projectEnvName": "nanoatmega328new", "projectEnvName": "nanoatmega328new",
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin", "toolchainBinDir": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin",
"internalConsoleOptions": "openOnSessionStart", "internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": { "preLaunchTask": {
"type": "PlatformIO", "type": "PlatformIO",
@@ -25,18 +25,18 @@
"type": "platformio-debug", "type": "platformio-debug",
"request": "launch", "request": "launch",
"name": "PIO Debug (skip Pre-Debug)", "name": "PIO Debug (skip Pre-Debug)",
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", "executable": "C:/Users/isakt/Embedded/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
"projectEnvName": "nanoatmega328new", "projectEnvName": "nanoatmega328new",
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin", "toolchainBinDir": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin",
"internalConsoleOptions": "openOnSessionStart" "internalConsoleOptions": "openOnSessionStart"
}, },
{ {
"type": "platformio-debug", "type": "platformio-debug",
"request": "launch", "request": "launch",
"name": "PIO Debug (without uploading)", "name": "PIO Debug (without uploading)",
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", "executable": "C:/Users/isakt/Embedded/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf",
"projectEnvName": "nanoatmega328new", "projectEnvName": "nanoatmega328new",
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin", "toolchainBinDir": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin",
"internalConsoleOptions": "openOnSessionStart", "internalConsoleOptions": "openOnSessionStart",
"loadMode": "manual" "loadMode": "manual"
} }
+16
View File
@@ -0,0 +1,16 @@
#ifndef MOTOR_DRIVER_H
#define MOTOR_DRIVER_H
class MotorDriver
{
public:
MotorDriver();
void begin();
void forward();
void reverse();
void stop();
};
#endif
+6
View File
@@ -1,6 +1,8 @@
#ifndef ROTARY_ENCODER_DRIVER_H #ifndef ROTARY_ENCODER_DRIVER_H
#define ROTARY_ENCODER_DRIVER_H #define ROTARY_ENCODER_DRIVER_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"
@@ -9,6 +11,7 @@ class RotaryEncoderDriver {
volatile uint8_t cycle; volatile uint8_t cycle;
volatile int16_t count; volatile int16_t count;
volatile int16_t prev_count; volatile int16_t prev_count;
volatile bool speed_ready;
Digital_out led; Digital_out led;
@@ -28,5 +31,8 @@ public:
float count_per_sec(); float count_per_sec();
float rpm(); float rpm();
bool new_speed_available();
void reset_speed_measurement();
}; };
#endif #endif
+59 -6
View File
@@ -3,23 +3,76 @@
#include <Arduino.h> #include <Arduino.h>
#include "rotary_encoder_driver.h" #include "rotary_encoder_driver.h"
#include "motor_driver.h"
RotaryEncoderDriver encoder; RotaryEncoderDriver encoder;
MotorDriver motor;
int main() { int main() {
init(); init();
Serial.begin(9600); Serial.begin(115200);
cli(); cli();
encoder.begin(); encoder.begin();
motor.begin();
sei(); sei();
// Make sure motor is stopped before starting test
motor.stop();
_delay_ms(2000);
Serial.println("time_ms,rpm");
Serial.println("0.0,0.0");
// Synchronize the RPM measurement window with the step input
encoder.reset_speed_measurement();
// Apply unit-step input
motor.forward();
uint16_t sample = 0;
while (true) { while (true) {
_delay_ms(50);
// Serial.print("Counts per second: "); if (encoder.new_speed_available())
// Serial.println(encoder.count_per_sec()); {
Serial.print("RPM: "); sample++;
Serial.println(encoder.rpm());
float time_ms = sample * 5.0f;
float speed = encoder.rpm();
Serial.print(time_ms, 1);
Serial.print(",");
Serial.println(speed, 2);
// Stop after about 8 seconds {2000 (<- input in sample >= xxxx) * 4 = 8 sec}
if (sample >= 400)
{
motor.stop();
while (true)
{
}
}
}
//Serial.print("POSITION: ");
//Serial.print("RPM: ");
//Serial.println(encoder.position());
//Serial.println(encoder.rpm());
//_delay_ms(50);
} }
return 0; return 0;
} }
+49
View File
@@ -0,0 +1,49 @@
#include "motor_driver.h"
#include <avr/io.h>
MotorDriver::MotorDriver()
{
}
void MotorDriver::begin()
{
// Arduino D3 = PD3 = AIN1
// Arduino D11 = PB3 = AIN2
DDRD |= (1 << DDD3);
DDRB |= (1 << DDB3);
stop();
}
void MotorDriver::forward()
{
// AIN1 / D3 = HIGH
// AIN2 / D11 = LOW
PORTD |= (1 << PORTD3);
PORTB &= ~(1 << PORTB3);
}
void MotorDriver::reverse()
{
// AIN1 / D3 = LOW
// AIN2 / D11 = HIGH
PORTD &= ~(1 << PORTD3);
PORTB |= (1 << PORTB3);
}
void MotorDriver::stop()
{
// Both LOW
PORTD &= ~(1 << PORTD3);
PORTB &= ~(1 << PORTB3);
}
-50
View File
@@ -1,50 +0,0 @@
/* #include <avr/io.h>
#include <util/delay.h>
#include <Arduino.h>
#include "digital_in.h"
#include "digital_out.h"
int main() {
uint8_t cycle{0};
int8_t counter{0};
double ratio;
bool last_lo = false;
Digital_in encoderA{4};
Digital_in encoderB{5};
Digital_out led{5};
encoderA.init();
encoderB.init();
led.init();
Serial.begin(9600);
while (true) {
_delay_us(100);
led.set_lo();
bool curr_hi = encoderA.is_hi();
if (last_lo && curr_hi) {
counter += encoderB.is_lo() ? -1 : 1;
led.set_hi();
}
++cycle;
if (cycle == 255) {
ratio = counter / (0.0255 * 700);
counter = 0;
cycle = 0;
Serial.println(ratio);
}
last_lo = !curr_hi;
}
return 0;
} */
-66
View File
@@ -1,66 +0,0 @@
/* #include <avr/io.h>
#include <util/delay.h>
#include <Arduino.h>
#include "digital_in.h"
#include "digital_out.h"
class RotaryEncoderDriver {
int count{0};
bool last_lo{false};
Digital_out led{5};
Digital_in eA;
Digital_in eB;
public:
RotaryEncoderDriver(unsigned short pin_a = 4, unsigned short pin_b = 5) : eA(pin_a), eB(pin_b) {
led.init();
eA.init();
eB.init();
last_lo = eA.is_lo();
}
int position() {
bool curr_hi = eA.is_hi();
if (last_lo && curr_hi) {
count += eB.is_hi() ? 1 : -1;
led.set_hi();
} else {
led.set_lo();
}
last_lo = !curr_hi;
return count;
}
};
int main() {
uint8_t cycle{0};
RotaryEncoderDriver encoder;
Serial.begin(9600);
int prev_pos = encoder.position();
while (true) {
_delay_us(100);
int curr_pos = encoder.position();
if (cycle++ == 255) {
int delta = curr_pos - prev_pos;
float rpm = delta * 60.0f / (700.0f * 0.0256f);
Serial.print("Count: ");
Serial.print(delta);
Serial.print("\tRPM: ");
Serial.println(rpm);
prev_pos = curr_pos;
}
}
return 0;
}
*/
-69
View File
@@ -1,69 +0,0 @@
/* #include <avr/io.h>
#include <util/delay.h>
#include <Arduino.h>
#include "digital_in.h"
#include "digital_out.h"
class RotaryEncoderDriver {
volatile int count{0};
Digital_out led{5};
Digital_in eA;
Digital_in eB;
public:
RotaryEncoderDriver(unsigned short pin_a = 4, unsigned short pin_b = 5) : eA(pin_a), eB(pin_b) {
led.init();
eA.init();
eB.init();
EICRA |= (1<<ISC01) | (1<<ISC00);
EIMSK |= (1<<INT0);
}
void update() {
count += eB.is_hi() ? 1 : -1;
led.set_hi();
}
int position() {
led.set_lo();
return count;
}
};
RotaryEncoderDriver encoder{2,5};
int main() {
uint8_t cycle{0};
Serial.begin(9600);
int prev_pos = encoder.position();
sei();
while (true) {
_delay_us(100);
int curr_pos = encoder.position();
if (cycle++ == 255) {
int delta = curr_pos - prev_pos;
float rpm = delta * 60.0f / (700.0f * 0.0256f);
Serial.print("Count: ");
Serial.print(delta);
Serial.print("\tRPM: ");
Serial.println(rpm);
prev_pos = curr_pos;
}
}
return 0;
}
ISR(INT0_vect) {
encoder.update();
}
*/
+68 -7
View File
@@ -7,10 +7,12 @@ RotaryEncoderDriver::RotaryEncoderDriver()
: cycle(0) : cycle(0)
, count(0) , count(0)
, prev_count(0) , prev_count(0)
, speed_ready(false)
, led(5) , led(5)
, eA(2) , eA(2)
, eB(5) {} , eB(5) {}
void RotaryEncoderDriver::begin() { void RotaryEncoderDriver::begin() {
driver = this; driver = this;
@@ -21,7 +23,7 @@ void RotaryEncoderDriver::begin() {
EICRA |= (1<<ISC01) | (1<<ISC00); EICRA |= (1<<ISC01) | (1<<ISC00);
EIMSK |= (1<<INT0); EIMSK |= (1<<INT0);
timer.init(100); timer.init(5000);
} }
void RotaryEncoderDriver::update() { void RotaryEncoderDriver::update() {
@@ -30,26 +32,84 @@ void RotaryEncoderDriver::update() {
} }
int RotaryEncoderDriver::position() { int RotaryEncoderDriver::position() {
uint8_t oldSREG = SREG;
cli();
int16_t local_count = count;
SREG = oldSREG;
led.set_lo(); led.set_lo();
return count;
return local_count;
} }
void RotaryEncoderDriver::update_delta() { void RotaryEncoderDriver::update_delta() {
if (++cycle == 0) {
delta = count - prev_count; delta = count - prev_count;
prev_count = count; prev_count = count;
}
speed_ready = true;
led.set_lo(); led.set_lo();
} }
float RotaryEncoderDriver::count_per_sec() { float RotaryEncoderDriver::count_per_sec() {
return delta / (256.0f * 0.0001f);
uint8_t oldSREG = SREG;
cli();
int16_t local_delta = delta;
SREG = oldSREG;
return local_delta / 0.005f;
} }
float RotaryEncoderDriver::rpm() {
return (count_per_sec() * 60) / 1050.0; 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) { ISR(INT0_vect) {
driver->update(); driver->update();
@@ -58,3 +118,4 @@ ISR(TIMER1_COMPA_vect) {
driver->update_delta(); driver->update_delta();
} }
+61
View File
@@ -0,0 +1,61 @@
/* #include "rotary_encoder_driver.h"
#include <avr/interrupt.h>
RotaryEncoderDriver* driver = nullptr;
RotaryEncoderDriver::RotaryEncoderDriver()
: cycle(0)
, count(0)
, prev_count(0)
, led(5)
, eA(2)
, eB(5) {}
void RotaryEncoderDriver::begin() {
driver = this;
led.init();
eA.init();
eB.init();
EICRA |= (1<<ISC01) | (1<<ISC00);
EIMSK |= (1<<INT0);
timer.init(100);
}
void RotaryEncoderDriver::update() {
count += eB.is_hi() ? 1 : -1;
led.set_hi();
}
int RotaryEncoderDriver::position() {
led.set_lo();
return count;
}
void RotaryEncoderDriver::update_delta() {
if (++cycle == 0) {
delta = count - prev_count;
prev_count = count;
}
led.set_lo();
}
float RotaryEncoderDriver::count_per_sec() {
return delta / (256.0f * 0.0001f);
}
float RotaryEncoderDriver::rpm() {
return (count_per_sec() * 60) / 1050.0;
}
ISR(INT0_vect) {
driver->update();
}
ISR(TIMER1_COMPA_vect) {
driver->update_delta();
}
*/