optimized by Claude for faster processing + lower memory usage

This commit is contained in:
Aditya Pulipaka
2025-11-16 20:14:11 -06:00
parent 676e4d78a8
commit 9c04f6264e
4 changed files with 15 additions and 16 deletions

View File

@@ -58,10 +58,10 @@ void FullController::sendOutputs() {
} }
void FullController::avgControl() { void FullController::avgControl() {
float avg = (Left.mmVal + Right.mmVal + Front.mmVal + Back.mmVal) / 4.0; float avg = (Left.mmVal + Right.mmVal + Front.mmVal + Back.mmVal) * 0.25f;
float eCurr = AvgRef - avg; // assuming up is positive and down is negative float eCurr = AvgRef - avg;
avgError.eDiff = (tDiff == 0) ? 0:(eCurr - avgError.e) / tDiff; // rise over run avgError.eDiff = (tDiff == 0.0) ? 0:(eCurr - avgError.e) / tDiff; // rise over run
avgError.eInt += eCurr * tDiff; avgError.eInt += eCurr * tDiff;
avgError.eInt = constrain(avgError.eInt, -MAX_INTEGRAL_TERM, MAX_INTEGRAL_TERM); avgError.eInt = constrain(avgError.eInt, -MAX_INTEGRAL_TERM, MAX_INTEGRAL_TERM);
avgError.e = eCurr; avgError.e = eCurr;
@@ -74,7 +74,7 @@ void FullController::LRControl() {
float eCurr = diff - LRDiffRef; // how different is that from the reference? positive -> Left repels, Right attracts. float eCurr = diff - LRDiffRef; // how different is that from the reference? positive -> Left repels, Right attracts.
K_MAP rConsts = {LConsts.attracting, LConsts.repelling}; // apply attracting to repelling and vice versa. K_MAP rConsts = {LConsts.attracting, LConsts.repelling}; // apply attracting to repelling and vice versa.
LRDiffErr.eDiff = (tDiff == 0) ? 0:(eCurr - LRDiffErr.e) / tDiff; LRDiffErr.eDiff = (tDiff == 0.0) ? 0:(eCurr - LRDiffErr.e) / tDiff;
LRDiffErr.eInt += eCurr * tDiff; LRDiffErr.eInt += eCurr * tDiff;
LRDiffErr.eInt = constrain(LRDiffErr.eInt, -MAX_INTEGRAL_TERM, MAX_INTEGRAL_TERM); LRDiffErr.eInt = constrain(LRDiffErr.eInt, -MAX_INTEGRAL_TERM, MAX_INTEGRAL_TERM);
LRDiffErr.e = eCurr; LRDiffErr.e = eCurr;
@@ -88,7 +88,7 @@ void FullController::FBControl() {
float eCurr = diff - FBDiffRef; // how different is that from ref? pos.->Front must repel, Back must attract float eCurr = diff - FBDiffRef; // how different is that from ref? pos.->Front must repel, Back must attract
K_MAP bConsts = {FConsts.attracting, FConsts.repelling}; K_MAP bConsts = {FConsts.attracting, FConsts.repelling};
FBDiffErr.eDiff = (tDiff == 0) ? 0:(eCurr - FBDiffErr.e) / tDiff; FBDiffErr.eDiff = (tDiff == 0.0) ? 0:(eCurr - FBDiffErr.e) / tDiff;
FBDiffErr.eInt += eCurr * tDiff; FBDiffErr.eInt += eCurr * tDiff;
FBDiffErr.eInt = constrain(FBDiffErr.eInt, -MAX_INTEGRAL_TERM, MAX_INTEGRAL_TERM); FBDiffErr.eInt = constrain(FBDiffErr.eInt, -MAX_INTEGRAL_TERM, MAX_INTEGRAL_TERM);
FBDiffErr.e = eCurr; FBDiffErr.e = eCurr;

View File

@@ -100,6 +100,6 @@ class FullController {
int16_t FRPrev; int16_t FRPrev;
int16_t BRPrev; int16_t BRPrev;
unsigned int tDiff; float tDiff;
}; };
#endif // CONTROLLER_HPP #endif // CONTROLLER_HPP

View File

@@ -5,11 +5,11 @@
// Inductive Sensor Mapping Struct // Inductive Sensor Mapping Struct
typedef struct IndSensorMap { typedef struct IndSensorMap {
double A; float A;
double K; float K;
double B; float B;
double C; float C;
double v; float v;
} IndSensorMap; } IndSensorMap;
class IndSensor { class IndSensor {

View File

@@ -60,12 +60,11 @@ void setup() {
void loop() { void loop() {
if (Serial.available() > 0) { if (Serial.available() > 0) {
String inputString = Serial.readStringUntil('\n'); // Read the full input // this might need to be changed if we have trouble getting serial to read.
inputString.trim(); // Remove leading/trailing whitespace, including \n and \r char c = Serial.read();
while(Serial.available()) Serial.read(); // flush remaining
// determine whether control is on or off based on serial input. controller.outputOn = (c != '0');
if (inputString == "0") controller.outputOn=0;
else controller.outputOn=1;
} }
tDiffMicros = micros() - tprior; tDiffMicros = micros() - tprior;