Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Documentation please #20

Open
arnolde opened this issue Jan 7, 2022 · 0 comments
Open

More Documentation please #20

arnolde opened this issue Jan 7, 2022 · 0 comments

Comments

@arnolde
Copy link

arnolde commented Jan 7, 2022

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant