Okay, sorry, I was messing things up. Now I'm able to tune the PID values using the pots. That's okay.
Problem is: I'm trying to use the Ziegler–Nichols method to tune the PID loop. So I try to get a steady oscillation by varying Kp. I had a hard time doing that just by looking at the Serial monitor, so I tried using Python to plot the data in real time. With Ki and Kd both equal to zero, I get something like this:
That doesn't look like a steady oscillation to me. As Kp approaches zero, the system oscillates less, but I can't see a significant difference by rising it's value. I tried varying Kd and Ki also, without sucess. I just can't see any real difference.
That's the complete code I'm using:
#include <PID_v1.h>
#define SpeedSetPoint 200
double Input, Output, Setpoint; //PID Variables
int potKp = 2;
int potKd = 3;
int potKi = 4;
float Kp = 0, Kd = 0, Ki =0;
PID myPid(&Input, &Output, &Setpoint, 0, 0, 0, DIRECT);
//RPM Counter
unsigned long start;
const byte encoderPinA = 2;//A pin -> interrupt pin 0
const byte encoderPinB = 4;//B pin -> digital pin 4
volatile long pulse;
volatile bool pinB, pinA, dir;
const byte ppr = 25, upDatesPerSec = 2;
const int fin = 1000 / upDatesPerSec;
const float konstant = 60.0 * upDatesPerSec / (ppr * 2);
int rpm=0;
int outputValue=0;
//DC Motor Pins
int pwm = 5;
int motorA1 = 10;
int motorA2 = 9;
void setup(){
Serial.begin(19200);
Input = 25;
Setpoint = SpeedSetPoint;
myPid.SetMode(AUTOMATIC);
attachInterrupt(0, readEncoder, CHANGE);
pinMode(encoderPinA,INPUT_PULLUP);
pinMode(encoderPinB,INPUT_PULLUP);
pinMode(pwm, OUTPUT);
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
//Start DC Motor
digitalWrite(pwm, HIGH);
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
}
void loop() {
Kp = analogRead(potKp)/10.0;
Ki = analogRead(potKi)/10.0;
Kd = analogRead(potKd)/10.0;
myPid.SetTunings(Kp, Ki, Kd);
if(millis() - start > fin)
{
start = millis();
rpm = pulse * konstant;
rpm = abs(rpm);
Serial.println(rpm);
pulse = 0;
}//End RPM reading
Input = rpm;
myPid.Compute();
outputValue = Output;
//outputValue = map(outputValue, 0, 400, 180, 255);
analogWrite(pwm, outputValue);
//Serial.print("RPM = ");
//Serial.println(rpm);
//Serial.print("\t");
//Serial.print("Kp = ");
//Serial.print(Kp);
//Serial.print("\t");
//Serial.print("Ki = ");
//Serial.print(Ki);
//Serial.print("\t");
//Serial.print("Kd = ");
// Serial.println(Kd);
}
void readEncoder()
{
pinA = bitRead(PIND,encoderPinA);
pinB = bitRead(PIND,encoderPinB);
dir = pinA ^ pinB; // if pinA & pinB are the same
dir ? --pulse : ++pulse; // dir is CW, else CCW
}
Any tips? I really don't know what to do.
