feat: modular configuration
This commit is contained in:
+4
-4
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <struct.h>
|
||||
|
||||
#define SPEED 40 // Base speed
|
||||
#define KP 0.05 // Proportional gain
|
||||
#define KI 0.0 // Integral gain
|
||||
#define KD 0.002 // Derivative gain
|
||||
// Speed, Kp, Ki, Kd
|
||||
const Config configSlow = { 40, 0.05, 0.0, 0.002 };
|
||||
const Config configMain = { 80, 0.05, 0.0, 0.002 };
|
||||
|
||||
const uint8_t SENSORS[] = {
|
||||
19, 18, 17, 16, 15, 14, 11, 10, 9 };
|
||||
+2
-2
@@ -6,7 +6,6 @@ void setup() {
|
||||
Serial.begin(115200);
|
||||
uSetup();
|
||||
qtrSetup();
|
||||
pidSetup();
|
||||
|
||||
if(SW()) {
|
||||
Serial.println(F("EMERGENCY MODE RESETTING EEPROM"));
|
||||
@@ -38,5 +37,6 @@ void loop() {
|
||||
// TODO: replace with line tracking code
|
||||
// fd(20);
|
||||
// delay(20000);
|
||||
track(30000);
|
||||
track(configSlow, 500);
|
||||
track(configMain, 30000);
|
||||
fd(0); } }
|
||||
|
||||
+8
-6
@@ -4,23 +4,25 @@
|
||||
#include <us82_motor.h>
|
||||
float input, output, setpoint, leftmotor, rightmotor;
|
||||
|
||||
QuickPID pid = QuickPID(&input, &output, &setpoint, KP, KI, KD, QuickPID::Action::reverse);
|
||||
QuickPID pid = QuickPID(&input, &output, &setpoint, 0, 0, 0, QuickPID::Action::reverse);
|
||||
|
||||
void pidSetup() {
|
||||
void pidSetup(Config config) {
|
||||
pid.SetMode(QuickPID::Control::automatic);
|
||||
const int maxOutput = 100 + SPEED;
|
||||
pid.SetTunings(config.kp, config.ki, config.kd);
|
||||
const int maxOutput = 100 + config.speed;
|
||||
pid.SetOutputLimits(-maxOutput, maxOutput);
|
||||
pid.SetSampleTimeUs(10000); }
|
||||
|
||||
void track(unsigned long time) {
|
||||
void track(Config config, unsigned long time) {
|
||||
pidSetup(config);
|
||||
const unsigned long startTime = millis();
|
||||
|
||||
while (millis() - startTime < (unsigned long)time) {
|
||||
input = (qtr.readLineBlack(sensors) - ((SENSOR_COUNT - 1) * 500.0)) / (SENSOR_COUNT - 1);
|
||||
pid.Compute();
|
||||
|
||||
leftmotor = SPEED + output;
|
||||
rightmotor = SPEED - output;
|
||||
leftmotor = config.speed + output;
|
||||
rightmotor = config.speed - output;
|
||||
|
||||
// Serial.print("Input: "); Serial.print(input); Serial.print("\tOutput: "); Serial.print(output);
|
||||
// Serial.print("\tLeft Motor: "); Serial.print(leftmotor); Serial.print("\tRight Motor: "); Serial.println(rightmotor);
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
struct Config {
|
||||
int speed;
|
||||
float kp;
|
||||
float ki;
|
||||
float kd; };
|
||||
Reference in New Issue
Block a user