Clean & pid tuning

This commit is contained in:
Pascal Serrarens 2024-01-19 14:14:28 +01:00
parent f3021c1f58
commit b86484d59d
2 changed files with 18 additions and 14 deletions

View File

@ -20,25 +20,29 @@ void ControlledMotor::SetTargetSpeed(float speed) {
} }
void ControlledMotor::Update(float currentTimeMs) { void ControlledMotor::Update(float currentTimeMs) {
actualSpeed = encoder->GetRevolutionsPerSecond(currentTimeMs); this->actualSpeed = encoder->GetRevolutionsPerSecond(currentTimeMs);
if (this->currentTargetSpeed < 0) if (this->currentTargetSpeed < 0)
actualSpeed = -actualSpeed; this->actualSpeed = -this->actualSpeed;
float deltaTime = currentTimeMs - this->lastUpdateTime; float deltaTime = currentTimeMs - this->lastUpdateTime;
float error = currentTargetSpeed - actualSpeed; float error = this->currentTargetSpeed - this->actualSpeed;
float errorChange = (error - lastError) * deltaTime; float errorChange = (error - lastError) * deltaTime;
float delta = error * pidP + errorChange * pidD; float delta = error * pidP + errorChange * pidD;
// Serial.print(" actualSpeed "); Serial.print(" actual Speed ");
// Serial.print(actualSpeed); Serial.print(actualSpeed);
// Serial.print(" motor target speed "); Serial.print(" target Speed ");
// Serial.print(motor->currentTargetSpeed); Serial.print(this->currentTargetSpeed);
// Serial.print(" + "); Serial.print(" motor target speed ");
// Serial.print(error * pidP); Serial.print(motor->currentTargetSpeed);
// Serial.print(" + "); Serial.print(" + ");
// Serial.println(errorChange * pidD); Serial.print(error * pidP);
Serial.print(" + ");
Serial.print(errorChange * pidD);
Serial.print(" = ");
Serial.println(motor->currentTargetSpeed + delta);
motor->SetTargetSpeed(motor->currentTargetSpeed + motor->SetTargetSpeed(motor->currentTargetSpeed +
delta); // or something like that delta); // or something like that

View File

@ -19,9 +19,9 @@ public:
} }
float velocity; float velocity;
float pidP = 0.02F; float pidP = 0.1F;
float pidD = 0.001F; float pidD = 0.0F;
float pidI = 0; float pidI = 0.0F;
void Update(float currentTimeMs) override; void Update(float currentTimeMs) override;