DonnyCraft1 / PIDArduino

A simple PID library for Arduino
MIT License
45 stars 21 forks source link

More Documentation please #20

Open arnolde opened 2 years ago

arnolde commented 2 years ago

I installed this library via PIO and am trying to get the example working, so far, after an hour or so, without success. I have a BLDC motor (from a scooter) who's sensor provides 45 pulses per revolution, controlled by a 10 bit PWM value. I have no idea what the "600" value means in the PID example or how to tune the P,I,D values. At least I would appreciate some units in the comments, indicating what the values mean and how adjusting which one will affect the operation. Right now when I try to set a target value of 60 [pulses per second], which corresponds to a PWM value of about 70, (about 7% dutycycle) the program keeps setting a PWM value of 33 (never changes) while the speed stays around 4 pulses/sec. If I play around with the PID values I get different static speeds but never is anything regulated to match the target afaik.

Here is my code (only left motor implemented so far):

#include <Arduino.h>
#include <PIDController.h>

#define pwmLeft 25
#define pwmRight 26
#define speedSensLeft 34
#define speedSensRight 35

uint16_t leftSpeed = 0;
volatile uint16_t speedCounter = 0;

void countISR();
PIDController pid;

void setup()
{
  pinMode(pwmLeft, OUTPUT);
  pinMode(pwmRight, OUTPUT);
  pinMode(speedSensLeft, INPUT);
  pinMode(speedSensRight, INPUT);
  ledcSetup(0,10000,10);
  ledcSetup(1,10000,10);
  ledcAttachPin(pwmLeft,0);
  ledcAttachPin(pwmRight,1);

  Serial.begin(115200);

  attachInterrupt(speedSensLeft, countISR, RISING);

  pid.begin();          // initialize the PID instance
  pid.setpoint(60);    // The "goal" the PID controller tries to "reach"
  pid.tune(1, 1, 1);    // Tune the PID, arguments: kP, kI, kD
  pid.limit(0, 255);    // Limit the PID output between 0 and 255, this is important to get rid of integral windup!

}

void loop()
{
  Serial.printf("Current speed is %d pulses/second. ", speedCounter);
  int output = pid.compute(speedCounter);
  Serial.printf("Setting PWM to %d.\n", output);
  speedCounter = 0;
  ledcWrite(0, output);
  delay(100);
//    ledcWrite(1, abs(rightSpeed/2));
}

void countISR()  // counts from the speed sensor
{
  speedCounter++;  // increase +1 the counter value
}