Files
guadaloop_lev_control/AdditiveControlCode/AdditiveControlCode.ino

95 lines
2.6 KiB
Arduino
Raw Normal View History

2025-11-16 20:05:18 -06:00
#include <Arduino.h>
#include "IndSensorMap.hpp"
#include "Controller.hpp"
#include "ADC.hpp"
2025-11-22 14:44:25 -06:00
#include "FastPWM.hpp"
2025-11-16 20:05:18 -06:00
// K, Ki, Kd Constants
2025-11-22 13:29:00 -06:00
Constants repelling = {1000, 0, 10000};
Constants attracting = {1000, 0, 10000};
2025-11-16 20:05:18 -06:00
2025-11-22 13:29:00 -06:00
Constants RollLeftUp = {500, 0, 10000};
Constants RollLeftDown = {500, 0, 10000};
2025-11-16 20:05:18 -06:00
2025-11-22 13:29:00 -06:00
Constants RollFrontUp = {500, 0, 10000};
Constants RollFrontDown = {500, 0, 10000};
2025-11-16 20:05:18 -06:00
// Reference values for average dist,
2025-11-22 13:29:00 -06:00
float avgRef = 12.0; // TBD: what is our equilibrium height with this testrig?
2025-11-16 20:05:18 -06:00
float LRDiffRef = 0.0; // TBD: what is our left-right balance equilibrium? Positive -> left is above right
2025-11-22 13:29:00 -06:00
float FBDiffRef = 2; // TBD: what is front-back balance equilibrium? Positive -> front above back.
2025-11-16 20:05:18 -06:00
2025-11-22 13:29:00 -06:00
float slewRateLimit = 10000.0; // max PWM change per control cycle (determined by 1 second / sampling rate)
2025-11-16 20:05:18 -06:00
// this was implemented by Claude and we can see if it helps.
// Set it at or above 255 to make it have no effect.
// Might be useful for things like jitter or lag.
#define sampling_rate 1000 // Hz
// EMA filter alpha value (all sensors use same alpha)
#define alphaVal 0.3f
2025-11-16 20:05:18 -06:00
// ABOVE THIS LINE IS TUNING VALUES ONLY, BELOW IS ACTUAL CODE.
unsigned long tprior;
unsigned int tDiffMicros;
FullConsts fullConsts = {
{repelling, attracting},
{RollLeftDown, RollLeftUp},
{RollFrontDown, RollFrontUp}
};
FullController controller(indL, indR, indF, indB, fullConsts, avgRef, LRDiffRef, FBDiffRef, slewRateLimit);
const int dt_micros = 1e6/sampling_rate;
#define LEV_ON
int ON = 0;
void setup() {
2025-11-22 13:29:00 -06:00
Serial.begin(115200);
setupADC();
2025-11-22 14:44:25 -06:00
setupFastPWM();
indL.alpha = alphaVal;
indR.alpha = alphaVal;
indF.alpha = alphaVal;
indB.alpha = alphaVal;
2025-11-16 20:05:18 -06:00
tprior = micros();
pinMode(dirFL, OUTPUT);
pinMode(pwmFL, OUTPUT);
pinMode(dirBL, OUTPUT);
pinMode(pwmBL, OUTPUT);
pinMode(dirFR, OUTPUT);
pinMode(pwmFR, OUTPUT);
pinMode(dirBR, OUTPUT);
pinMode(pwmBR, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
// this might need to be changed if we have trouble getting serial to read.
char c = Serial.read();
while(Serial.available()) Serial.read(); // flush remaining
2025-11-16 20:05:18 -06:00
controller.outputOn = (c != '0');
2025-11-16 20:05:18 -06:00
}
tDiffMicros = micros() - tprior;
if (tDiffMicros >= dt_micros){
2025-11-22 13:29:00 -06:00
controller.update();
2025-11-16 20:05:18 -06:00
controller.report();
controller.sendOutputs();
// this and the previous line can be switched if you want the PWMs to display 0 when controller off.
tprior = micros(); // maybe we have to move this line to before the update commands?
// since the floating point arithmetic may take a while...
}
//Serial.println(telapsed);
}