From b952e9fe1e0295a04cd07b4901eb5fe32829dd40 Mon Sep 17 00:00:00 2001 From: drake Date: Fri, 11 Oct 2024 21:35:11 -0500 Subject: [PATCH] init commit --- .gitmodules | 6 +++ lib/Arduino-PID-Library | 1 + lib/DMXSerial | 1 + lib/README | 46 ++++++++++++++++++ platformio.ini | 15 ++++++ src/main.cpp | 103 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 172 insertions(+) create mode 100644 .gitmodules create mode 160000 lib/Arduino-PID-Library create mode 160000 lib/DMXSerial create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b9c49b4 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "lib/Arduino-PID-Library"] + path = lib/Arduino-PID-Library + url = https://github.com/br3ttb/Arduino-PID-Library +[submodule "lib/DMXSerial"] + path = lib/DMXSerial + url = https://github.com/mathertel/DMXSerial diff --git a/lib/Arduino-PID-Library b/lib/Arduino-PID-Library new file mode 160000 index 0000000..524a426 --- /dev/null +++ b/lib/Arduino-PID-Library @@ -0,0 +1 @@ +Subproject commit 524a4268fc01e6ea397e7fc5b5d820741e9b662f diff --git a/lib/DMXSerial b/lib/DMXSerial new file mode 160000 index 0000000..4674529 --- /dev/null +++ b/lib/DMXSerial @@ -0,0 +1 @@ +Subproject commit 46745295694eda720361a18b21ac3d5cba6d872f diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..2593a33 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..73cac0b --- /dev/null +++ b/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:uno] +platform = atmelavr +board = uno +framework = arduino +lib_deps = DMXSerial diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..557edab --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,103 @@ +#include +#include +#include + + +// #define SENSOR_PIN A0 +#define RPWM_PIN 10 +#define LPWM_PIN 11 +#define L_EN_PIN 12 +#define R_EN_PIN 13 +#define ENCODER_A 2 +#define ENCODER_B 3 + +const int DMX_CHANNEL = 1; + +double pidGain; +double currentPosition = 0; +double targetPosition = 50; +int positionDif = 0; +double pidPwm = 0; + + +PID pid(¤tPosition, &pidPwm, &targetPosition, 2, 5, 1, DIRECT); + +void updateEncoder() { + if (digitalRead(ENCODER_B) == LOW) { + currentPosition++; // CCW + } else { + currentPosition--; // CW + } +} + +void setup() { + DMXSerial.init(DMXReceiver); + DMXSerial.write(DMX_CHANNEL, 0); + + pinMode(RPWM_PIN, OUTPUT); + pinMode(LPWM_PIN, OUTPUT); + pinMode(L_EN_PIN, OUTPUT); + pinMode(L_EN_PIN, OUTPUT); + pinMode(ENCODER_A, INPUT_PULLUP); + pinMode(ENCODER_B, INPUT_PULLUP); + + attachInterrupt(digitalPinToInterrupt(ENCODER_A), updateEncoder, RISING); + + pid.SetMode(AUTOMATIC); + // pid.SetSampleTime(50); + pid.SetOutputLimits(-255, 255); + + + pinMode(5, OUTPUT); + pinMode(6, OUTPUT); +} + + +void loop() { + // Calculate how long no data bucket was received + if (DMXSerial.noDataSince() < 5000) { + + // read recent DMX values and set pwm levels + // pidGain = 0.0001 + (0.002 * (DMXSerial.read(DMX_CHANNEL + 1) / 255)); + // float kP = 10 * (DMXSerial.read(DMX_CHANNEL + 2) / 255); + // float kI = 10 * (DMXSerial.read(DMX_CHANNEL + 3) / 255); + // float kD = 10 * (DMXSerial.read(DMX_CHANNEL + 4) / 255); + + // pid.SetTunings(kP, kI, kD); + + targetPosition = (DMXSerial.read(DMX_CHANNEL) - 100) * 6; // 0-200. 100 is center, <100 is CW, >100 is CCW. 0 & 200 are 360 degrees + + // PID + pid.Compute(); + + // positionDif = targetPosition - currentPosition; + // pidPwm += (positionDif - pidPwm) * pidGain; + // if (pidPwm > 255) pidPwm = 255; + // if (pidPwm < -255) pidPwm = -255; + + if (abs(pidPwm > 150)) { + digitalWrite(R_EN_PIN, 1); + digitalWrite(L_EN_PIN, 1); + if (pidPwm > 0) { + analogWrite(RPWM_PIN, 0); + analogWrite(LPWM_PIN, pidPwm); + + analogWrite(5, 0); + analogWrite(6, pidPwm); + } else { + analogWrite(LPWM_PIN, 0); + analogWrite(RPWM_PIN, -pidPwm); + + analogWrite(6, 0); + analogWrite(5, -pidPwm); + } + } else { + digitalWrite(R_EN_PIN, 0); + digitalWrite(L_EN_PIN, 0); + } + + } else { + digitalWrite(R_EN_PIN, 0); + digitalWrite(L_EN_PIN, 0); + } +} \ No newline at end of file