From 96800a05d860cc702aed96b38ae82f5bd284c5b9 Mon Sep 17 00:00:00 2001 From: helgie22 Date: Sat, 19 Sep 2026 19:29:07 +0000 Subject: [PATCH] Part 4 --- .gitignore | 5 ++ .vscode/c_cpp_properties.json | 38 +++++++-------- .vscode/launch.json | 12 ++--- include/rotary_encoder_driver.h | 5 ++ src/main.cpp | 86 ++++++++++++++++++--------------- src/rotary_encoder_driver.cpp | 31 +++++++++--- src/temporary_main.cpp | 18 +++---- 7 files changed, 111 insertions(+), 84 deletions(-) diff --git a/.gitignore b/.gitignore index 03f4a3c..a17f20c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ .pio +venv +*.py +*.csv +scripts +*.typ \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index b161267..931738c 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -8,29 +8,29 @@ { "name": "PlatformIO", "includePath": [ - "C:/Users/isakt/Embedded/ESP_Project2_H26/include", - "C:/Users/isakt/Embedded/ESP_Project2_H26/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/cores/arduino", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/HID/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SPI/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/Wire/src", + "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include", + "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/HID/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SPI/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/Wire/src", "" ], "browse": { "limitSymbolsToIncludedHeaders": true, "path": [ - "C:/Users/isakt/Embedded/ESP_Project2_H26/include", - "C:/Users/isakt/Embedded/ESP_Project2_H26/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/cores/arduino", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/HID/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SPI/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src", - "C:/Users/isakt/.platformio/packages/framework-arduino-avr/libraries/Wire/src", + "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/include", + "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/cores/arduino", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/HID/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SPI/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src", + "C:/Users/helgi/.platformio/packages/framework-arduino-avr/libraries/Wire/src", "" ] }, @@ -45,7 +45,7 @@ ], "cStandard": "gnu11", "cppStandard": "gnu++11", - "compilerPath": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe", + "compilerPath": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe", "compilerArgs": [ "-mmcu=atmega328p", "" diff --git a/.vscode/launch.json b/.vscode/launch.json index 809d280..120fa44 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,9 +12,9 @@ "type": "platformio-debug", "request": "launch", "name": "PIO Debug", - "executable": "C:/Users/isakt/Embedded/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", + "executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", "projectEnvName": "nanoatmega328new", - "toolchainBinDir": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin", + "toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin", "internalConsoleOptions": "openOnSessionStart", "preLaunchTask": { "type": "PlatformIO", @@ -25,18 +25,18 @@ "type": "platformio-debug", "request": "launch", "name": "PIO Debug (skip Pre-Debug)", - "executable": "C:/Users/isakt/Embedded/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", + "executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", "projectEnvName": "nanoatmega328new", - "toolchainBinDir": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin", + "toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin", "internalConsoleOptions": "openOnSessionStart" }, { "type": "platformio-debug", "request": "launch", "name": "PIO Debug (without uploading)", - "executable": "C:/Users/isakt/Embedded/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", + "executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project2_H26/.pio/build/nanoatmega328new/firmware.elf", "projectEnvName": "nanoatmega328new", - "toolchainBinDir": "C:/Users/isakt/.platformio/packages/toolchain-atmelavr/bin", + "toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin", "internalConsoleOptions": "openOnSessionStart", "loadMode": "manual" } diff --git a/include/rotary_encoder_driver.h b/include/rotary_encoder_driver.h index 980607f..2b51f95 100644 --- a/include/rotary_encoder_driver.h +++ b/include/rotary_encoder_driver.h @@ -8,11 +8,16 @@ #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; diff --git a/src/main.cpp b/src/main.cpp index d70fe98..7b09aef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,24 @@ -/* #include +#include #include #include #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); @@ -15,65 +26,62 @@ int main() { cli(); encoder.begin(); motor.begin(); - - - - sei(); - - - // Make sure motor is stopped before starting test + // 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,rpm"); - Serial.println("0.0,0.0"); + Serial.println("time_ms,ref_rpm,actual_rpm,pwm_requested,pwm_actual"); + Serial.println("0.0,0.0,0.0,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; + double ref = 0.0; while (true) { - - if (encoder.new_speed_available()) - { + if (encoder.new_speed_available()) { sample++; - float time_ms = sample * 5.0f; - float speed = encoder.rpm(); + 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.println(speed, 2); + Serial.print(ref, 1); + Serial.print(","); + Serial.print(actual, 2); + Serial.print(","); + Serial.print(pwm_requested, 1); + Serial.print(","); + Serial.println(pwm_actual, 1); - - // Stop after about 8 seconds {2000 (<- input in sample >= xxxx) * 4 = 8 sec} - if (sample >= 400) - { + if (sample >= STOP_AT_SAMPLE) { motor.stop(); - - while (true) - { - + while (true) { } } } - - //Serial.print("POSITION: "); - //Serial.print("RPM: "); - - - //Serial.println(encoder.position()); - //Serial.println(encoder.rpm()); - - //_delay_ms(50); } return 0; } - */ \ No newline at end of file diff --git a/src/rotary_encoder_driver.cpp b/src/rotary_encoder_driver.cpp index e71c99c..b247747 100644 --- a/src/rotary_encoder_driver.cpp +++ b/src/rotary_encoder_driver.cpp @@ -8,9 +8,15 @@ RotaryEncoderDriver::RotaryEncoderDriver() , count(0) , prev_count(0) , speed_ready(false) + , hist_idx(0) , led(5) , eA(2) - , eB(5) {} + , eB(5) +{ + for (uint8_t i = 0; i < HIST_LEN; i++) { + history[i] = 0; + } +} void RotaryEncoderDriver::begin() { @@ -23,7 +29,7 @@ void RotaryEncoderDriver::begin() { EICRA |= (1< +/* Óvirkt núna, "alvöru" main() er í main.cpp. Þessi skrá var notuð í undirbúning/prófun + +#include #include "p_controller.h" @@ -20,7 +22,7 @@ int main() } return 0; -} */ +} #include @@ -36,9 +38,6 @@ int main() motor.begin(); - //_delay_ms(3000); - //motor.set(200); - while (true) { motor.set(60); @@ -49,11 +48,6 @@ int main() motor.set(240); _delay_ms(3000); - - //motor.stop(); - //_delay_ms(2000); - } - - -} \ No newline at end of file +} +*/