GOOD EVENING YOUI HAVE A PROBLEM WITH THE PID CONTROLLER I CAN NOT FIND THE RIGHT Kp Ki Kd WHETHER TO GO TO SETPOINT I'VE argument. COULD SOMEONE TO HELP ME?
[#include <digitalWriteFast.h>
// Quadrature encoders
// Left encoder
#define c_LeftEncoderInterruptA 0
#define c_LeftEncoderPinA 2
#define c_LeftEncoderInterruptB 1
#define c_LeftEncoderPinB 3
#define LeftEncoderIsReversed
volatile bool _LeftEncoderASet;
volatile bool _LeftEncoderBSet;
volatile long _LeftEncoderTicks = 0;
//Variables for PID
double Setpoint=10800.0;
unsigned long lastTime;
double errSum, lastErr;
double Input, Output;
double kp=0.1;
double ki=0;
double kd=0;
int SpeedPin=5;
int motorPin1=6;
int motorPin2=7;
//Send command to DC motor
void loop(){
Input=_LeftEncoderTicks;
Compute();
if(Output > 0) {
CC();
}
else if(Output<=0){
CCW();
}
Serial.print(_LeftEncoderTicks);
Serial.print("\n");
delay(20);
}
//Motor Control Function
void CC(){
digitalWriteFast(motorPin1,LOW);
digitalWriteFast(motorPin2,HIGH);
analogWrite(SpeedPin, abs(Output));
}
void CCW(){
digitalWriteFast(motorPin1,HIGH);
digitalWriteFast(motorPin2,LOW);
analogWrite(SpeedPin, abs(Output));
}
//Compute controller output by PID algorithm
void Compute()
{
/*How long since we last calculated*/
unsigned long now = millis();
double timeChange = (double)(now - lastTime);
Input=_LeftEncoderTicks;
/*Compute all the working error variables*/
double error = Setpoint - Input;
errSum += (error * timeChange);
double dErr = (error - lastErr) / timeChange;
/*Compute PID Output*/
Output = (kp * error + ki * errSum + kd * dErr)+80;
if(Output>255){Output = 255;}
else if(Output <-255){Output = -255;}
else{}
/*Remember some variables for next time*/
lastErr = error;
lastTime = now;
}
void setup()
{
Serial.begin(115200);
// Quadrature encoders
// Left encoder
pinMode(c_LeftEncoderPinA, INPUT); // sets pin A as input
pinMode(c_LeftEncoderPinB, INPUT); // sets pin B as input
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA); // read the input pin
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB); // read the input pin
attachInterrupt(c_LeftEncoderInterruptA, HandleLeftMotorInterruptA, CHANGE);
attachInterrupt(c_LeftEncoderInterruptB, HandleLeftMotorInterruptB, CHANGE);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(SpeedPin, OUTPUT);
}
// Interrupt service routines for the left motor’s quadrature encoder
void HandleLeftMotorInterruptA()
{
// Test transition
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA); // read the input pin
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB); // read the input pin
if(_LeftEncoderASet != _LeftEncoderBSet) {
_LeftEncoderTicks=_LeftEncoderTicks + 1;
}
else {
_LeftEncoderTicks=_LeftEncoderTicks - 1;
}
}
// Interrupt service routines for the left motor’s quadrature encoder
void HandleLeftMotorInterruptB()
{
// Test transition
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA); // read the input pin
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB); // read the input pin
// and adjust counter + if A leads B
if ( _LeftEncoderASet == _LeftEncoderBSet) {
_LeftEncoderTicks=_LeftEncoderTicks + 1;
}
else {
_LeftEncoderTicks=_LeftEncoderTicks - 1;
}
}]