Author SHA1 Message Date
helgie22 fbb9bbdbbb added state machine behaviour with switch/case 2026-09-24 11:02:37 +00:00
28 changed files with 659 additions and 986 deletions
+3 -8
View File
@@ -1,11 +1,6 @@
.pio
venv
__pycache__
*.py
*.csv
*.typ
*.pdf
# Vélarsértækar skrár sem PlatformIO býr til sjálft
.vscode/c_cpp_properties.json
.vscode/launch.json
compile_commands.json
scripts
*.typ
+56
View File
@@ -0,0 +1,56 @@
//
// !!! WARNING !!! AUTO-GENERATED FILE!
// PLEASE DO NOT MODIFY IT AND USE "platformio.ini":
// https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags
//
{
"configurations": [
{
"name": "PlatformIO",
"includePath": [
"C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/include",
"C:/Users/helgi/H26/ESP/Projects/ESP_Project3_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/helgi/H26/ESP/Projects/ESP_Project3_H26/include",
"C:/Users/helgi/H26/ESP/Projects/ESP_Project3_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",
""
]
},
"defines": [
"PLATFORMIO=60200",
"ARDUINO_AVR_NANO",
"F_CPU=16000000L",
"ARDUINO_ARCH_AVR",
"ARDUINO=10808",
"__AVR_ATmega328P__",
""
],
"cStandard": "gnu11",
"cppStandard": "gnu++11",
"compilerPath": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe",
"compilerArgs": [
"-mmcu=atmega328p",
""
]
}
],
"version": 4
}
+44
View File
@@ -0,0 +1,44 @@
// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY
//
// PlatformIO Debugging Solution
//
// Documentation: https://docs.platformio.org/en/latest/plus/debugging.html
// Configuration: https://docs.platformio.org/en/latest/projectconf/sections/env/options/debug/index.html
{
"version": "0.2.0",
"configurations": [
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug",
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/.pio/build/nanoatmega328new/firmware.elf",
"projectEnvName": "nanoatmega328new",
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
"internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": {
"type": "PlatformIO",
"task": "Pre-Debug"
}
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (skip Pre-Debug)",
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/.pio/build/nanoatmega328new/firmware.elf",
"projectEnvName": "nanoatmega328new",
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (without uploading)",
"executable": "C:/Users/helgi/H26/ESP/Projects/ESP_Project3_H26/.pio/build/nanoatmega328new/firmware.elf",
"projectEnvName": "nanoatmega328new",
"toolchainBinDir": "C:/Users/helgi/.platformio/packages/toolchain-atmelavr/bin",
"internalConsoleOptions": "openOnSessionStart",
"loadMode": "manual"
}
]
}
+200
View File
@@ -0,0 +1,200 @@
[
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/CDC.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/CDC.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/CDC.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/CDC.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial0.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial0.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial0.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial0.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial1.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial1.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial1.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial1.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial2.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial2.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial2.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial2.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial3.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial3.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/HardwareSerial3.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/HardwareSerial3.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/IPAddress.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/IPAddress.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/IPAddress.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/IPAddress.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/PluggableUSB.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/PluggableUSB.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/PluggableUSB.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/PluggableUSB.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/Print.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/Print.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/Print.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/Print.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/Stream.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/Stream.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/Stream.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/Stream.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/Tone.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/Tone.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/Tone.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/Tone.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/USBCore.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/USBCore.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/USBCore.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/USBCore.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc -o .pio/build/nanoatmega328new/FrameworkArduino/WInterrupts.c.o -c -std=gnu11 -fno-fat-lto-objects -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/WInterrupts.c",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/WInterrupts.c",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/WInterrupts.c.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/WMath.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/WMath.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/WMath.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/WMath.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/WString.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/WString.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/WString.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/WString.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/abi.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/abi.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/abi.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/abi.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc -o .pio/build/nanoatmega328new/FrameworkArduino/hooks.c.o -c -std=gnu11 -fno-fat-lto-objects -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/hooks.c",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/hooks.c",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/hooks.c.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/main.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/main.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/main.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/main.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/FrameworkArduino/new.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/new.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/new.cpp",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/new.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc -o .pio/build/nanoatmega328new/FrameworkArduino/wiring.c.o -c -std=gnu11 -fno-fat-lto-objects -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring.c",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring.c",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/wiring.c.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc -o .pio/build/nanoatmega328new/FrameworkArduino/wiring_analog.c.o -c -std=gnu11 -fno-fat-lto-objects -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_analog.c",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_analog.c",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/wiring_analog.c.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc -o .pio/build/nanoatmega328new/FrameworkArduino/wiring_digital.c.o -c -std=gnu11 -fno-fat-lto-objects -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_digital.c",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_digital.c",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/wiring_digital.c.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc -mmcu=atmega328p -x assembler-with-cpp -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs -c -o .pio/build/nanoatmega328new/FrameworkArduino/wiring_pulse.S.o /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_pulse.S",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_pulse.S",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/wiring_pulse.S.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc -o .pio/build/nanoatmega328new/FrameworkArduino/wiring_pulse.c.o -c -std=gnu11 -fno-fat-lto-objects -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_pulse.c",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_pulse.c",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/wiring_pulse.c.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-gcc -o .pio/build/nanoatmega328new/FrameworkArduino/wiring_shift.c.o -c -std=gnu11 -fno-fat-lto-objects -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs /Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_shift.c",
"directory": "/Users/dadi/embedded/proj1",
"file": "/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino/wiring_shift.c",
"output": ".pio/build/nanoatmega328new/FrameworkArduino/wiring_shift.c.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/src/digital_in.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -Iinclude -Isrc -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs src/digital_in.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "src/digital_in.cpp",
"output": ".pio/build/nanoatmega328new/src/digital_in.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/src/digital_out.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -Iinclude -Isrc -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs src/digital_out.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "src/digital_out.cpp",
"output": ".pio/build/nanoatmega328new/src/digital_out.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/src/main.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -Iinclude -Isrc -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs src/main.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "src/main.cpp",
"output": ".pio/build/nanoatmega328new/src/main.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/src/part1_first_working_main.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -Iinclude -Isrc -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs src/part1_first_working_main.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "src/part1_first_working_main.cpp",
"output": ".pio/build/nanoatmega328new/src/part1_first_working_main.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/src/part1part2.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -Iinclude -Isrc -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs src/part1part2.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "src/part1part2.cpp",
"output": ".pio/build/nanoatmega328new/src/part1part2.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/src/part3part1.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -Iinclude -Isrc -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs src/part3part1.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "src/part3part1.cpp",
"output": ".pio/build/nanoatmega328new/src/part3part1.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/src/rotary_encoder_driver.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -Iinclude -Isrc -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs src/rotary_encoder_driver.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "src/rotary_encoder_driver.cpp",
"output": ".pio/build/nanoatmega328new/src/rotary_encoder_driver.cpp.o"
},
{
"command": "/Users/dadi/.platformio/packages/toolchain-atmelavr/bin/avr-g++ -o .pio/build/nanoatmega328new/src/timer_usec.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -mmcu=atmega328p -Os -Wall -ffunction-sections -fdata-sections -flto -DPLATFORMIO=60119 -DARDUINO_AVR_NANO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -Iinclude -Isrc -I/Users/dadi/.platformio/packages/framework-arduino-avr/cores/arduino -I/Users/dadi/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs src/timer_usec.cpp",
"directory": "/Users/dadi/embedded/proj1",
"file": "src/timer_usec.cpp",
"output": ".pio/build/nanoatmega328new/src/timer_usec.cpp.o"
}
]
-40
View File
@@ -1,40 +0,0 @@
@startuml
' skinparam linetype ortho
' skinparam nodesep 100
skinparam ranksep 80
skinparam state {
BackgroundColor LightBlue
}
[*] --> Initialization
state Initialization
Initialization : Entry / Stop motor
Initialization : Do / Initialize internal parameters
Initialization : Do / Reset speed measurement
Initialization : Do / Wait 2 seconds
Initialization : Exit / Send Boot-up message
state Operational
Operational : Entry / LED on
Operational : Do / Run speed controller
Operational : Exit / Stop motor and LED off
state Stopped
Stopped : Entry / Stop motor
Stopped : Do / Blink LED at 2 Hz
Stopped : Exit / LED off
Initialization --> Operational : initialization\n complete
Operational --> Initialization : reset
Operational --> Stopped : fault detected
Stopped --> Initialization : reset
Stopped --> Operational : set operational
@enduml
-18
View File
@@ -1,18 +0,0 @@
#ifndef CONTROLLER_H
#define CONTROLLER_H
class Controller
{
public:
virtual ~Controller()
{
}
virtual double update(double ref, double actual) = 0;
virtual void reset()
{
}
};
#endif
-24
View File
@@ -1,24 +0,0 @@
#ifndef INITIALIZATION_STATE_H
#define INITIALIZATION_STATE_H
#include "state.h"
class InitializationState : public State
{
private:
static const unsigned long WAIT_MS = 2000UL;
unsigned long enter_time_ms = 0;
public:
void enter() override;
void update() override;
void command(char c) override;
void exit() override;
};
#endif
-94
View File
@@ -1,94 +0,0 @@
#ifndef MOTOR_CONTROLLER_H
#define MOTOR_CONTROLLER_H
#include "rotary_encoder_driver.h"
#include "motor_driver.h"
//#include "p_controller.h"
//#include "pi_controller.h"
#include "controller.h"
#include "digital_out.h"
class State;
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
State* current_state;
State& initialization_state;
State& operational_state;
State& stopped_state;
RotaryEncoderDriver& encoder;
MotorDriver& motor;
//P_controller& controller;
//PI_controller& controller;
Controller& controller;
Digital_out led;
double reference_rpm;
public:
MotorController(
RotaryEncoderDriver& encoder,
MotorDriver& motor,
//P_controller& controller,
Controller& controller,
State& initialization,
State& operational,
State& stopped
);
// void set_states(
// State* initialization,
// State* operational,
// State* stopped
// );
void begin();
void update();
void command(char c);
void transition_to(State& new_state);
void to_initialization();
void to_operational();
void to_stopped();
bool fault_active();
void run_speed_controller();
void stop_motor();
void reset_speed_measurement();
void reset_controller();
void led_on();
void led_off();
};
#endif
-4
View File
@@ -9,14 +9,10 @@ public:
void begin();
void forward();
void reverse();
void stop();
void set(double pwm);
bool fault_active(); // Fault detection for part 2
};
#endif
-19
View File
@@ -1,19 +0,0 @@
#ifndef OPERATIONAL_STATE_H
#define OPERATIONAL_STATE_H
#include "state.h"
class OperationalState : public State
{
public:
void enter() override;
void update() override;
void command(char c) override;
void exit() override;
};
#endif
+2 -7
View File
@@ -1,9 +1,7 @@
#ifndef P_CONTROLLER_H
#define P_CONTROLLER_H
#include "controller.h"
class P_controller : public Controller
class P_controller
{
private:
double Kp;
@@ -11,10 +9,7 @@ private:
public:
P_controller(double Kp);
double update(double ref, double actual) override;
double update(double ref, double actual);
};
#endif
-27
View File
@@ -1,27 +0,0 @@
#ifndef PI_CONTROLLER_H
#define PI_CONTROLLER_H
#include "controller.h"
class PI_controller : public Controller
{
private:
double Kp;
double Ti;
double T;
double integral;
public:
PI_controller(
double Kp,
double Ti,
double T
);
double update(double ref, double actual) override;
void reset() override;
};
#endif
+11 -17
View File
@@ -4,45 +4,39 @@
#include <stdint.h>
#include "digital_in.h"
#include "digital_out.h"
#include "timer_usec.h"
class RotaryEncoderDriver {
public:
static constexpr uint16_t SAMPLE_PERIOD_US = 4000; // Ts = 4 ms
static constexpr float COUNTS_PER_REV = 1050.0f;
static const uint8_t HIST_LEN = 4; // 8 x 5 ms = 40 ms gluggi fyrir rpm-utreikning
private:
static const uint8_t HIST_LEN = 4; // 4 x Ts = 16 ms gluggi fyrir rpm-utreikning
volatile int32_t count;
volatile int16_t delta;
volatile uint8_t cycle;
volatile int16_t count;
volatile int16_t prev_count;
volatile bool speed_ready;
volatile int32_t history[HIST_LEN];
volatile int16_t history[HIST_LEN];
volatile uint8_t hist_idx;
Digital_out led;
Digital_in eA;
Digital_in eB;
Timer_usec timer;
public:
volatile int16_t delta;
RotaryEncoderDriver();
void begin();
void update();
int32_t position();
int position();
void update_delta();
float count_per_sec();
float rpm();
bool new_speed_available();
void reset_speed_measurement();
};
-32
View File
@@ -1,32 +0,0 @@
#ifndef STATE_H
#define STATE_H
class MotorController;
class State
{
protected:
MotorController* context = nullptr;
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
-25
View File
@@ -1,25 +0,0 @@
#ifndef STOPPED_STATE_H
#define STOPPED_STATE_H
#include "state.h"
class StoppedState : public State
{
private:
unsigned long previous_time; // Used for blinking LED
bool led_state; // Ditto
public:
StoppedState();
void enter() override;
void update() override;
void command(char c) override;
void exit() override;
};
#endif
+1 -3
View File
@@ -13,6 +13,4 @@ platform = atmelavr
board = nanoatmega328new
framework = arduino
monitor_speed = 115200
test_framework = unity
test_build_src = yes
monitor_speed = 115200
-91
View File
@@ -1,91 +0,0 @@
#!/usr/bin/env python3
"""
Teiknar step response-plott ur CSV-gognum sem Arduino-verkefnid prentar ut
(dalkar: time_ms, ref_rpm, actual_rpm, pwm_requested, pwm_actual).
pwm_requested = Kp * error * 2.55, oklippt (getur farid langt yfir +-255)
pwm_actual = sama gildi en klippt vid +-255, þ.e. þad sem raunverulega
var sent a motor.set() / vélbúnaðinn
Notkun:
python plot_step_response.py [inntak.csv] [uttak.png] [titill]
Sjalfgefid: data/step_response.csv -> data/step_response.png
"""
import sys
import csv
import matplotlib.pyplot as plt
def load_csv(path):
cols = {"time_ms": [], "ref_rpm": [], "actual_rpm": [],
"pwm_requested": [], "pwm_actual": []}
with open(path, newline="") as f:
reader = csv.DictReader(f)
for row in reader:
try:
for key in cols:
cols[key].append(float(row[key]))
except (ValueError, KeyError):
# sleppir linum sem eru ekki gild data (t.d. rusl fyrir/eftir)
for key in cols:
if len(cols[key]) > len(cols["time_ms"]):
cols[key].pop()
continue
return cols
def plot(cols, out_path, title_suffix=""):
time_s = [t / 1000.0 for t in cols["time_ms"]]
ref_rpm = cols["ref_rpm"]
actual_rpm = cols["actual_rpm"]
pwm_requested = cols["pwm_requested"]
pwm_actual = cols["pwm_actual"]
fig, ax1 = plt.subplots(figsize=(10, 5))
ax1.plot(time_s, ref_rpm, label="Reference (ref_rpm)", color="#666666",
linestyle="--", linewidth=1.5)
ax1.plot(time_s, actual_rpm, label="Measured (actual_rpm)", color="#1f77b4",
linewidth=1.8)
ax1.set_xlabel("Time [s]")
ax1.set_ylabel("Angular velocity [RPM]")
ax1.grid(True, alpha=0.3)
ax2 = ax1.twinx()
ax2.plot(time_s, pwm_requested, label="PWM requested (unclamped)",
color="#d62728", linewidth=1.0, alpha=0.5, linestyle=":")
ax2.plot(time_s, pwm_actual, label="PWM actual (clamped)",
color="#d62728", linewidth=1.4)
ax2.axhline(255, color="#d62728", linewidth=0.6, alpha=0.3)
ax2.axhline(-255, color="#d62728", linewidth=0.6, alpha=0.3)
ax2.set_ylabel("PWM [-255, 255]")
# Merkjum stokkpunktinn (fyrsta stadin thar sem ref hoppar fra 0)
step_idx = next((i for i, r in enumerate(ref_rpm) if r > 0), None)
if step_idx is not None:
ax1.axvline(time_s[step_idx], color="black", linewidth=0.8,
linestyle=":")
ax1.text(time_s[step_idx], max(actual_rpm) * 1.02, " step",
fontsize=9, va="bottom")
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2, loc="lower right",
fontsize=8)
title = "Double step response: RPM and PWM over time"
if title_suffix:
title += f" ({title_suffix})"
fig.suptitle(title)
fig.tight_layout()
fig.savefig(out_path, dpi=150)
print(f"Vistad: {out_path}")
if __name__ == "__main__":
in_path = sys.argv[1] if len(sys.argv) > 1 else "data/twostep.csv"
out_path = sys.argv[2] if len(sys.argv) > 2 else "data/twostep.png"
title_suffix = sys.argv[3] if len(sys.argv) > 3 else ""
data = load_csv(in_path)
plot(data, out_path, title_suffix)
-43
View File
@@ -1,43 +0,0 @@
#include "initialization_state.h"
#include "motor_controller.h"
#include <Arduino.h>
void InitializationState::enter()
{
Serial.println();
Serial.println("State: Initialization");
context->led_off();
context->stop_motor();
context->reset_speed_measurement();
context->reset_controller();
enter_time_ms = millis();
}
void InitializationState::update()
{
// Non-blocking bið svo aðallykkjan haldi áfram að keyra
if (millis() - enter_time_ms >= WAIT_MS)
{
context->to_operational();
}
}
void InitializationState::command(char c)
{
}
void InitializationState::exit()
{
Serial.println("Boot-up");
}
+151 -80
View File
@@ -1,95 +1,166 @@
#include <avr/io.h>
#include <util/delay.h>
#include <Arduino.h>
#include <avr/interrupt.h>
#include "rotary_encoder_driver.h"
#include "motor_driver.h"
//#include "p_controller.h"
#include "pi_controller.h"
#include "p_controller.h"
#include "motor_controller.h"
// RotaryEncoderDriver encoder;
// MotorDriver motor;
#include "initialization_state.h"
#include "operational_state.h"
#include "stopped_state.h"
// P_controller controller(12.5);
// const double REF_RPM1 = 30.0;
// const double REF_RPM2 = 50.0;
RotaryEncoderDriver encoder;
// const uint16_t STEP1_AT_SAMPLE = 50;
// const uint16_t STEP2_AT_SAMPLE = 500;
MotorDriver motor;
// const uint16_t STOP_AT_SAMPLE = 1000;
//const double KP = 12.5;
unsigned long state_entry_time;
//P_controller controller(KP);
PI_controller controller(
12.5, // Kp
1.0, // Ti
0.004 // T = 4 ms
);
InitializationState initialization_state;
OperationalState operational_state;
StoppedState stopped_state;
// Stöðurnar verða að vera skilgreindar á undan motor_controller
MotorController motor_controller(
encoder,
motor,
controller,
initialization_state,
operational_state,
stopped_state
);
int main()
unsigned long time_in_state()
{
init();
Serial.begin(115200);
cli();
encoder.begin();
motor.begin();
sei();
// motor_controller.set_states(
// &initialization_state,
// &operational_state,
// &stopped_state
// );
motor_controller.begin();
while (true)
{
if (Serial.available() > 0)
{
char c = Serial.read();
if (c != '\n' && c != '\r')
{
motor_controller.command(c);
}
}
motor_controller.update();
}
return 0;
return millis() - state_entry_time;
}
// int main(){
// return 0;
// }
int main() {
init();
Serial.begin(115200);
// Serial.begin(9600);
sei();
enum State
{
Initialization,
Operational,
Pre_Operational,
Stopped
};
State state = State::Initialization; // Define state variable and set the initial state
// 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 (1) {
int c = 0; // for incoming serial data
if (Serial.available() > 0)
{
c = Serial.read();
Serial.print("I received: ");
Serial.println(c, DEC);
}
switch (state)
{
case State::Initialization:
if (c == 'p')
{
state_entry_time = millis();
// State::Idle exit action
// event1 transition action
Serial.println("Initialization finished");
// set new target state
state = State::Pre_Operational;
// target state entry action
Serial.println("Going into Pre Operation");
}
if (c == 'o')
{
state_entry_time = millis();
// State::Idle exit action
// event2 transition action
Serial.println("Initialization finished");
// set new target state
state = State::Operational;
// target state entry action
Serial.println("Going straight to Operation");
}
break;
case State::Operational:
if (c == 'i')
{
state_entry_time = millis();
// State::Cooling exit action
Serial.println("Operation over");
// event3 transition action
// set new target state
state = State::Initialization;
// target state entry action
Serial.println("Initialization state");
}
if (c == 's')
{
state_entry_time = millis();
// State::Cooling exit action
Serial.println("Operation over");
// event4 transition action
// set new target state
state = State::Stopped;
// target state entry action
Serial.println("Stopping");
}
break;
case State::Pre_Operational:
if (c == 'o')
{
state_entry_time = millis();
// State::Heating exit action
Serial.println("Pre Operation over");
// event3 transition action
// set new target state
state = State::Operational;
// target state entry action
Serial.println("Going to Operation");
}
if (c == 's')
{
state_entry_time = millis();
// State::Heating exit action
Serial.println("Pre Operation over");
// event4 transition action
// set new target state
state = State::Stopped;
// target state entry action
Serial.println("Stopping");
}
break;
case State::Stopped:
if (c == 'i')
{
state_entry_time = millis();
// State::Failure exit action
Serial.println("Stop over");
// event3 transition action
// set new target state
state = State::Initialization;
// target state entry action
Serial.println("Initialization state");
}
}
}
return 0;
}
-152
View File
@@ -1,152 +0,0 @@
#include "motor_controller.h"
#include "state.h"
#include <Arduino.h>
MotorController::MotorController(
RotaryEncoderDriver& encoder,
MotorDriver& motor,
//P_controller& controller,
//PI_controller& controller;
Controller& controller,
State& initialization,
State& operational,
State& stopped
)
: current_state(nullptr),
initialization_state(initialization),
operational_state(operational),
stopped_state(stopped),
encoder(encoder),
motor(motor),
controller(controller),
led(LED_PIN),
reference_rpm(DEFAULT_REFERENCE_RPM)
{
initialization_state.set_context(this);
operational_state.set_context(this);
stopped_state.set_context(this);
}
// void MotorController::set_states(
// State* initialization,
// State* operational,
// State* stopped
// )
// initialization_state = initialization;
// operational_state = operational;
// stopped_state = stopped;
void MotorController::begin()
{
led.init();
led.set_lo();
transition_to(initialization_state);
}
void MotorController::transition_to(State& new_state)
{
if (current_state != nullptr)
{
current_state->exit();
}
current_state = &new_state;
current_state->set_context(this);
current_state->enter();
}
void MotorController::update()
{
if (current_state != nullptr)
{
current_state->update();
}
}
void MotorController::command(char c)
{
if (current_state != nullptr)
{
current_state->command(c);
}
}
void MotorController::to_initialization()
{
transition_to(initialization_state);
}
void MotorController::to_operational()
{
transition_to(operational_state);
}
void MotorController::run_speed_controller()
{
if (encoder.new_speed_available())
{
double actual = encoder.rpm();
double pwm_percent =
controller.update(reference_rpm, actual);
double pwm =
pwm_percent * PWM_PER_PERCENT;
motor.set(pwm);
}
}
void MotorController::stop_motor()
{
motor.stop();
}
void MotorController::reset_speed_measurement()
{
encoder.reset_speed_measurement();
}
void MotorController::to_stopped()
{
transition_to(stopped_state);
}
bool MotorController::fault_active()
{
return motor.fault_active();
}
void MotorController::reset_controller()
{
controller.reset();
}
void MotorController::led_on()
{
led.set_hi();
}
void MotorController::led_off()
{
led.set_lo();
}
+42 -62
View File
@@ -3,37 +3,6 @@
#include <avr/io.h>
// Í Fast PWM gefur OCR = 0 samt einn stuttan púls í hverri lotu.
// Til að pinni sé alveg LOW er hann aftengdur frá timernum (COM-bitar)
// og keyrður LOW gegnum PORT-registerið.
static void ain1_off() // D3 = PD3 = OC2B
{
TCCR2A &= ~(1 << COM2B1);
PORTD &= ~(1 << PORTD3);
OCR2B = 0;
}
static void ain2_off() // D11 = PB3 = OC2A
{
TCCR2A &= ~(1 << COM2A1);
PORTB &= ~(1 << PORTB3);
OCR2A = 0;
}
static void ain1_pwm(uint8_t duty)
{
OCR2B = duty;
TCCR2A |= (1 << COM2B1);
}
static void ain2_pwm(uint8_t duty)
{
OCR2A = duty;
TCCR2A |= (1 << COM2A1);
}
MotorDriver::MotorDriver()
{
}
@@ -43,16 +12,10 @@ void MotorDriver::begin()
{
// Arduino D3 = PD3 = AIN1
// Arduino D11 = PB3 = AIN2
// Arduino D4 = PD4 = FLT
DDRD |= (1 << DDD3);
DDRB |= (1 << DDB3);
DDRD &= ~(1 << DDD4);
PORTD |= (1 << PORTD4); // Enable pull-up resistor on D4 for part 2
// Timer2 Fast PWM
TCCR2A = 0;
TCCR2B = 0;
@@ -61,32 +24,57 @@ void MotorDriver::begin()
TCCR2A |= (1 << WGM20);
TCCR2A |= (1 << WGM21);
// Non-inverting PWM on OC2A and OC2B
TCCR2A |= (1 << COM2A1);
TCCR2A |= (1 << COM2B1);
// Timer2 prescaler = 32
TCCR2B |= (1 << CS21);
TCCR2B |= (1 << CS20);
// PWM-úttök eru tengd í set() eftir þörfum
stop();
OCR2A = 0;
OCR2B = 0;
//stop();
}
void MotorDriver::forward()
{
set(255.0);
// AIN1 / D3 = HIGH
// AIN2 / D11 = LOW
//PORTD |= (1 << PORTD3);
//PORTB &= ~(1 << PORTB3);
OCR2A = 0;
OCR2B = 255;
}
void MotorDriver::reverse()
{
set(-255.0);
// AIN1 / D3 = LOW
// AIN2 / D11 = HIGH
//PORTD &= ~(1 << PORTD3);
//PORTB |= (1 << PORTB3);
OCR2B = 0;
OCR2A = 255;
}
void MotorDriver::stop()
{
// Both LOW
ain1_off();
ain2_off();
//PORTD &= ~(1 << PORTD3);
//PORTB &= ~(1 << PORTB3);
OCR2A = 0;
OCR2B = 0;
}
void MotorDriver::set(double pwm)
@@ -103,28 +91,20 @@ void MotorDriver::set(double pwm)
}
uint8_t duty = (uint8_t)(pwm > 0 ? pwm : -pwm);
if (duty == 0)
{
stop();
}
else if (pwm > 0)
if (pwm > 0)
{
// Forward
ain2_off();
ain1_pwm(duty);
OCR2A = 0;
OCR2B = (uint8_t)pwm;
}
else if (pwm < 0)
{
// Reverse
OCR2B = 0;
OCR2A = (uint8_t)(-pwm);
}
else
{
// Reverse
ain1_off();
ain2_pwm(duty);
stop();
}
}
bool MotorDriver::fault_active()
{
return !(PIND & (1 << PIND4));
}
}
-54
View File
@@ -1,54 +0,0 @@
#include "operational_state.h"
#include "motor_controller.h"
#include <Arduino.h>
void OperationalState::enter()
{
Serial.println("State: Operational");
context->led_on();
}
// Part 1
//void OperationalState::update()
//{
// context->run_speed_controller();
//}
// Part 2
void OperationalState::update()
{
if (context->fault_active())
{
Serial.println("Fault detected");
context->to_stopped();
return;
}
context->run_speed_controller();
}
void OperationalState::command(char c)
{
if (c == 'r' || c == 'R')
{
Serial.println("Command: reset");
context->to_initialization();
}
}
void OperationalState::exit()
{
context->stop_motor();
context->led_off();
}
-57
View File
@@ -1,57 +0,0 @@
#include "pi_controller.h"
PI_controller::PI_controller(
double Kp,
double Ti,
double T
)
: Kp(Kp),
Ti(Ti),
T(T),
integral(0.0)
{
}
double PI_controller::update(double ref, double actual)
{
return 0.0;
}
// double PI_controller::update(double ref, double actual)
// {
// double error = ref - actual;
// double new_integral =
// integral + error * T;
// double output =
// Kp * (error + new_integral / Ti);
// // Positive saturation
// if (output > 100.0)
// {
// return 100.0;
// }
// // Negative saturation
// if (output < -100.0)
// {
// return -100.0;
// }
// // Only save the integral if the controller
// // is not saturated
// integral = new_integral;
// return output;
// }
void PI_controller::reset()
{
integral = 0.0;
}
+35 -31
View File
@@ -1,71 +1,73 @@
#include "rotary_encoder_driver.h"
#include <avr/interrupt.h>
static RotaryEncoderDriver* driver = nullptr;
RotaryEncoderDriver* driver = nullptr;
RotaryEncoderDriver::RotaryEncoderDriver()
: count(0)
, delta(0)
: cycle(0)
, count(0)
, prev_count(0)
, speed_ready(false)
, hist_idx(0)
, led(5)
, eA(2)
, eB(5)
{
for (uint8_t i = 0; i < HIST_LEN; i++)
{
for (uint8_t i = 0; i < HIST_LEN; i++) {
history[i] = 0;
}
}
void RotaryEncoderDriver::begin()
{
void RotaryEncoderDriver::begin() {
driver = this;
led.init();
eA.init();
eB.init();
EICRA |= (1<<ISC01) | (1<<ISC00);
EIMSK |= (1<<INT0);
timer.init(SAMPLE_PERIOD_US);
timer.init(4000);
}
void RotaryEncoderDriver::update()
{
void RotaryEncoderDriver::update() {
count += eB.is_hi() ? 1 : -1;
led.set_hi();
}
int32_t RotaryEncoderDriver::position()
{
int RotaryEncoderDriver::position() {
uint8_t oldSREG = SREG;
cli();
int32_t local_count = count;
int16_t local_count = count;
SREG = oldSREG;
led.set_lo();
return local_count;
}
}
void RotaryEncoderDriver::update_delta()
{
int32_t oldest = history[hist_idx];
void RotaryEncoderDriver::update_delta() {
int16_t oldest = history[hist_idx];
history[hist_idx] = count;
hist_idx = (hist_idx + 1) % HIST_LEN;
delta = (int16_t)(count - oldest); // breyting yfir HIST_LEN * Ts (16 ms) glugga
delta = count - oldest; // breyting yfir HIST_LEN * Ts (40 ms) glugga
prev_count = count;
speed_ready = true;
led.set_lo();
}
float RotaryEncoderDriver::count_per_sec()
{
float RotaryEncoderDriver::count_per_sec() {
uint8_t oldSREG = SREG;
@@ -75,12 +77,14 @@ float RotaryEncoderDriver::count_per_sec()
SREG = oldSREG;
return local_delta / (HIST_LEN * SAMPLE_PERIOD_US * 1e-6f);
return local_delta / (HIST_LEN * 0.004f);
}
float RotaryEncoderDriver::rpm()
{
const float COUNTS_PER_REV = 1050.0f;
return count_per_sec() * 60.0f / COUNTS_PER_REV;
}
@@ -109,24 +113,24 @@ void RotaryEncoderDriver::reset_speed_measurement()
cli();
cycle = 0;
prev_count = count;
delta = 0;
speed_ready = false;
for (uint8_t i = 0; i < HIST_LEN; i++)
{
for (uint8_t i = 0; i < HIST_LEN; i++) {
history[i] = count;
}
hist_idx = 0;
SREG = oldSREG;
}
ISR(INT0_vect)
{
ISR(INT0_vect) {
driver->update();
}
ISR(TIMER1_COMPA_vect)
{
driver->update_delta();
ISR(TIMER1_COMPA_vect) {
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();
}
*/
-70
View File
@@ -1,70 +0,0 @@
#include "stopped_state.h"
#include "motor_controller.h"
#include <Arduino.h>
StoppedState::StoppedState()
: previous_time(0),
led_state(false)
{
}
void StoppedState::enter()
{
Serial.println("State: Stopped");
context->stop_motor();
led_state = false;
digitalWrite(LED_BUILTIN, LOW);
previous_time = millis();
}
// Controlling the blinking of the LED @ 2Hz
void StoppedState::update()
{
unsigned long current_time = millis();
if (current_time - previous_time >= 250)
{
previous_time = current_time;
led_state = !led_state;
digitalWrite(
LED_BUILTIN,
led_state ? HIGH : LOW
);
}
}
// Commands for Stopped [ r = reset and o for operational]
void StoppedState::command(char c)
{
if (c == 'r' || c == 'R')
{
Serial.println("Command: reset");
context->to_initialization();
}
else if (c == 'o' || c == 'O')
{
Serial.println("Command: set operational");
context->to_operational();
}
}
void StoppedState::exit()
{
digitalWrite(LED_BUILTIN, LOW);
}
+53
View File
@@ -0,0 +1,53 @@
/* Óvirkt núna, "alvöru" main() er í main.cpp. Þessi skrá var notuð í undirbúning/prófun
#include <Arduino.h>
#include "p_controller.h"
P_controller controller(2.0);
int main()
{
init();
Serial.begin(115200);
double result = controller.update(100.0, 70.0);
Serial.print("PWM = ");
Serial.println(result);
while (true)
{
}
return 0;
}
#include <Arduino.h>
#include <util/delay.h>
#include "motor_driver.h"
MotorDriver motor;
int main()
{
init();
motor.begin();
while (true)
{
motor.set(60);
_delay_ms(3000);
motor.set(120);
_delay_ms(3000);
motor.set(240);
_delay_ms(3000);
}
}
*/
-28
View File
@@ -1,28 +0,0 @@
#include <unity.h>
//#include "p_controller.h"
#include "pi_controller.h"
static const double KP = 2.0;
static const double KI = 1.0;
static const double T = 0.1;
static const double TOL = 1e-4; // Tolerance
void test_positive_error()
{
PI_controller controller(KP, KI, T);
double output = controller.update(10.0, 8.0);
TEST_ASSERT_DOUBLE_WITHIN(TOL, 4.4, output);
}
int main()
{
UNITY_BEGIN();
RUN_TEST(test_positive_error);
return UNITY_END();
}