init commit

This commit is contained in:
Drake Marino 2024-10-11 21:35:11 -05:00
parent d387b9bcdc
commit b952e9fe1e
6 changed files with 172 additions and 0 deletions

6
.gitmodules vendored Normal file
View File

@ -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

@ -0,0 +1 @@
Subproject commit 524a4268fc01e6ea397e7fc5b5d820741e9b662f

1
lib/DMXSerial Submodule

@ -0,0 +1 @@
Subproject commit 46745295694eda720361a18b21ac3d5cba6d872f

46
lib/README Normal file
View File

@ -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 <Foo.h>
#include <Bar.h>
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

15
platformio.ini Normal file
View File

@ -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

103
src/main.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <Arduino.h>
#include <DMXSerial.h>
#include <PID_v1.h>
// #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(&currentPosition, &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);
}
}