PID Regler + Getriebmotor + Encoder

Stimmt - ich hab nen Mega :wink:
Danke!

Hallo,
na ja es ging ja langsam nicht mehr mit rechten Dingen zu, da sehen manchmal zwei zusätzliche Augen etwas.
Ich hab ab vor ein paar Tagen was mit einem Webserver und fetch gemacht. Und dabei was kopiert, stundenlang den Fehler gesucht und die Welt nicht mehr verstanden. Bis ich gemerkt habe das ich in dem reinkopieren Teil eine server.print() Zeile drin hatte die da nicht reingehörte. Da ärgert man sich dann schon :angry::sob:
Gruß Heinz

Eine Fehlersuchstrategie ist es nach 1 Stunde Fehlersuche mal eine lange Pause machen. Wenn Du später / nächsten Tag wieder draufschaust dann springt Dir der Fehler sofort ins Auge.
Grüße Uwe

Vielen Dank für die Hinweis

Vielen Dank. Das System läuft besser
ich will jetzt PID-Regler Parameter KP, Ki und Kd optimieren.
code:

#include <Encoder.h>   //encoder library
#include <PID_v1.h>    // PID library

// DC24V40RPM DC motor  and MD30C R2 30A DC Motor Driver
#define DIR 10            // DIR motor Driver
#define PWM_OUTPUT 11     // PWM motor Driver

// PIR 
double Setpoint, Input, Output;
double   Kp=0.1, Ki=0.2, Kd=0.01;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
 //const int PPR = 100; //define number of pulses in one round of encoder

// Rotary encoder Type: HQK38H6-100N-G5-24
#define EN_A 2
#define EN_B 3
Encoder Enc(EN_A, EN_B);

//Time variables
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;

//Encoder variables
volatile long currentEncoder;
volatile long previousEncoder = 0;
volatile long oldPosition = 0;
volatile long newPosition;

float old_rot_speed=0;
float rot_speed;    

void setup() 
{
  Input = read_speed();
  Setpoint = 20;  
  Serial.begin(115200);
  pinMode(EN_A, INPUT_PULLUP);
  pinMode(EN_B,  INPUT_PULLUP); 

   
  //turn the PID on
   myPID.SetMode(AUTOMATIC);
   myPID.SetOutputLimits(0,255);
   myPID.SetSampleTime(1);   // 2ms from 50ms/3/10 
 
}
float read_speed(void)
{
     static float currentSpeed = 0;
    const float alpha = 0.2 ; // EWMA smoothing constant;
    const int PPR = 100; //define number of pulses in one round of encoder
    const int interval = 500; //choose interval is 1 second (1000 milliseconds)
    currentMillis = millis();

    if ((currentMillis - previousMillis) > interval){
    
        previousMillis = currentMillis;
        currentEncoder = Enc.read();
        rot_speed = ((currentEncoder - previousEncoder)*60*1000 )/PPR/interval;     //rotating speed in rpm
        
        previousEncoder = currentEncoder;
     
        currentSpeed += alpha * (rot_speed - currentSpeed); // EWMA smoothing
     }
    return currentSpeed;
    //return rot_speed;
}

void report(void){
  static uint32_t last=0;
  uint32_t now = millis();
  const uint32_t interval = 500;
  if(now - last >= interval ){
     last += interval;
 // Serial.print(now);
  Serial.print("  Setpoint = ");
  Serial.print(Setpoint); 
  Serial.print(" Input = ");
  Serial.print(Input);
  //Serial.println();
  Serial.print("  Output = ");
  Serial.print(Output);
  Serial.println();
  }
}



void loop() {
   Input = read_speed();

  if (myPID.Compute()) {
     analogWrite(PWM_OUTPUT, Output);
  }
  report();
}

Das was kommt raus

19:54:58.966 -> 500  Setpoint = 20.00 Input = 0.00  Output = 3.96
19:54:59.482 -> 1000  Setpoint = 20.00 Input = 0.00  Output = 5.91
19:54:59.997 -> 1500  Setpoint = 20.00 Input = 0.00  Output = 7.86
19:55:00.466 -> 2000  Setpoint = 20.00 Input = 0.20  Output = 9.78
19:55:00.984 -> 2500  Setpoint = 20.00 Input = 0.76  Output = 11.60
19:55:01.452 -> 3000  Setpoint = 20.00 Input = 1.81  Output = 13.27
19:55:01.968 -> 3500  Setpoint = 20.00 Input = 2.65  Output = 14.88
19:55:02.483 -> 4000  Setpoint = 20.00 Input = 3.72  Output = 16.36
19:55:02.952 -> 4500  Setpoint = 20.00 Input = 4.57  Output = 17.78
19:55:03.467 -> 5000  Setpoint = 20.00 Input = 5.46  Output = 19.12
19:55:03.983 -> 5500  Setpoint = 20.00 Input = 6.37  Output = 20.36
19:55:04.452 -> 6000  Setpoint = 20.00 Input = 7.09  Output = 21.55
19:55:04.967 -> 6500  Setpoint = 20.00 Input = 8.27  Output = 22.58
19:55:05.483 -> 7000  Setpoint = 20.00 Input = 9.02  Output = 23.57
19:55:05.951 -> 7500  Setpoint = 20.00 Input = 10.02  Output = 24.45
19:55:06.467 -> 8000  Setpoint = 20.00 Input = 10.81  Output = 25.27
19:55:06.982 -> 8500  Setpoint = 20.00 Input = 11.45  Output = 26.04
19:55:07.498 -> 9000  Setpoint = 20.00 Input = 12.16  Output = 26.74
19:55:07.966 -> 9500  Setpoint = 20.00 Input = 12.73  Output = 27.40
19:55:08.482 -> 10000  Setpoint = 20.00 Input = 13.18  Output = 28.02
19:55:08.997 -> 10500  Setpoint = 20.00 Input = 13.75  Output = 28.57
19:55:09.466 -> 11000  Setpoint = 20.00 Input = 14.20  Output = 29.10
19:55:09.981 -> 11500  Setpoint = 20.00 Input = 14.56  Output = 29.59
19:55:10.450 -> 12000  Setpoint = 20.00 Input = 15.25  Output = 29.99
19:55:10.966 -> 12500  Setpoint = 20.00 Input = 15.80  Output = 30.35
19:55:11.481 -> 13000  Setpoint = 20.00 Input = 16.44  Output = 30.64
19:55:11.949 -> 13500  Setpoint = 20.00 Input = 16.95  Output = 30.89
19:55:12.465 -> 14000  Setpoint = 20.00 Input = 17.36  Output = 31.11
19:55:12.981 -> 14500  Setpoint = 20.00 Input = 17.69  Output = 31.30
19:55:13.449 -> 15000  Setpoint = 20.00 Input = 18.15  Output = 31.44
19:55:13.965 -> 15500  Setpoint = 20.00 Input = 18.52  Output = 31.55
19:55:14.480 -> 16000  Setpoint = 20.00 Input = 18.82  Output = 31.63
19:55:14.949 -> 16500  Setpoint = 20.00 Input = 19.05  Output = 31.70
19:55:15.464 -> 17000  Setpoint = 20.00 Input = 19.04  Output = 31.80
19:55:15.980 -> 17500  Setpoint = 20.00 Input = 19.23  Output = 31.85
19:55:16.495 -> 18000  Setpoint = 20.00 Input = 19.19  Output = 31.94
19:55:16.964 -> 18500  Setpoint = 20.00 Input = 19.35  Output = 31.99
19:55:17.480 -> 19000  Setpoint = 20.00 Input = 19.28  Output = 32.06
19:55:17.948 -> 19500  Setpoint = 20.00 Input = 19.42  Output = 32.11
19:55:18.464 -> 20000  Setpoint = 20.00 Input = 19.54  Output = 32.14
19:55:18.979 -> 20500  Setpoint = 20.00 Input = 19.43  Output = 32.21
19:55:19.495 -> 21000  Setpoint = 20.00 Input = 19.54  Output = 32.24
19:55:19.963 -> 21500  Setpoint = 20.00 Input = 19.44  Output = 32.31
19:55:20.479 -> 22000  Setpoint = 20.00 Input = 19.55  Output = 32.34
19:55:20.994 -> 22500  Setpoint = 20.00 Input = 19.44  Output = 32.40
19:55:21.463 -> 23000  Setpoint = 20.00 Input = 19.55  Output = 32.44
19:55:21.979 -> 23500  Setpoint = 20.00 Input = 19.44  Output = 32.50
19:55:22.494 -> 24000  Setpoint = 20.00 Input = 19.55  Output = 32.54
19:55:22.963 -> 24500  Setpoint = 20.00 Input = 19.44  Output = 32.60
19:55:23.478 -> 25000  Setpoint = 20.00 Input = 19.55  Output = 32.63
19:55:23.947 -> 25500  Setpoint = 20.00 Input = 19.44  Output = 32.70
19:55:24.462 -> 26000  Setpoint = 20.00 Input = 19.75  Output = 32.69
19:55:24.978 -> 26500  Setpoint = 20.00 Input = 19.80  Output = 32.71
19:55:25.447 -> 27000  Setpoint = 20.00 Input = 19.84  Output = 32.72
19:55:25.962 -> 27500  Setpoint = 20.00 Input = 20.07  Output = 32.69
19:55:26.478 -> 28000  Setpoint = 20.00 Input = 20.06  Output = 32.69
19:55:26.993 -> 28500  Setpoint = 20.00 Input = 20.05  Output = 32.68
19:55:27.462 -> 29000  Setpoint = 20.00 Input = 20.04  Output = 32.68
19:55:27.977 -> 29500  Setpoint = 20.00 Input = 20.03  Output = 32.68
19:55:28.492 -> 30000  Setpoint = 20.00 Input = 20.02  Output = 32.68
19:55:28.961 -> 30500  Setpoint = 20.00 Input = 20.02  Output = 32.67
19:55:29.477 -> 31000  Setpoint = 20.00 Input = 20.02  Output = 32.67
19:55:29.992 -> 31500  Setpoint = 20.00 Input = 20.01  Output = 32.67
19:55:30.461 -> 32000  Setpoint = 20.00 Input = 20.01  Output = 32.67
19:55:30.976 -> 32500  Setpoint = 20.00 Input = 19.81  Output = 32.71
19:55:31.445 -> 33000  Setpoint = 20.00 Input = 19.85  Output = 32.72
19:55:31.960 -> 33500  Setpoint = 20.00 Input = 19.68  Output = 32.76
19:55:32.476 -> 34000  Setpoint = 20.00 Input = 19.74  Output = 32.78
19:55:32.955 -> 34500  Setpoint = 20.00 Input = 19.59  Output = 32.84
19:55:33.471 -> 35000  Setpoint = 20.00 Input = 19.67  Output = 32.86
19:55:33.986 -> 35500  Setpoint = 20.00 Input = 19.54  Output = 32.92
19:55:34.455 -> 36000  Setpoint = 20.00 Input = 19.63  Output = 32.95
19:55:34.971 -> 36500  Setpoint = 20.00 Input = 19.51  Output = 33.00
19:55:35.486 -> 37000  Setpoint = 20.00 Input = 19.60  Output = 33.03
19:55:35.955 -> 37500  Setpoint = 20.00 Input = 19.88  Output = 33.02
19:55:36.470 -> 38000  Setpoint = 20.00 Input = 19.91  Output = 33.03
19:55:36.986 -> 38500  Setpoint = 20.00 Input = 20.13  Output = 33.00
19:55:37.454 -> 39000  Setpoint = 20.00 Input = 20.10  Output = 32.99
19:55:37.970 -> 39500  Setpoint = 20.00 Input = 20.28  Output = 32.95
19:55:38.485 -> 40000  Setpoint = 20.00 Input = 20.22  Output = 32.93
19:55:38.954 -> 40500  Setpoint = 20.00 Input = 20.18  Output = 32.92
19:55:39.469 -> 41000  Setpoint = 20.00 Input = 20.14  Output = 32.91
19:55:39.985 -> 41500  Setpoint = 20.00 Input = 20.11  Output = 32.90
19:55:40.454 -> 42000  Setpoint = 20.00 Input = 20.09  Output = 32.89
19:55:40.969 -> 42500  Setpoint = 20.00 Input = 20.27  Output = 32.85
19:55:41.477 -> 43000  Setpoint = 20.00 Input = 20.02  Output = 32.87
19:55:41.992 -> 43500  Setpoint = 20.00 Input = 20.02  Output = 32.87
19:55:42.461 -> 44000  Setpoint = 20.00 Input = 20.01  Output = 32.86
19:55:42.977 -> 44500  Setpoint = 20.00 Input = 19.81  Output = 32.90
19:55:43.445 -> 45000  Setpoint = 20.00 Input = 19.85  Output = 32.91
19:55:43.961 -> 45500  Setpoint = 20.00 Input = 19.88  Output = 32.92
19:55:44.472 -> 46000  Setpoint = 20.00 Input = 19.70  Output = 32.96
19:55:44.987 -> 46500  Setpoint = 20.00 Input = 19.76  Output = 32.98
19:55:45.456 -> 47000  Setpoint = 20.00 Input = 19.61  Output = 33.03
19:55:45.964 -> 47500  Setpoint = 20.00 Input = 19.69  Output = 33.05
19:55:46.479 -> 48000  Setpoint = 20.00 Input = 19.75  Output = 33.07
19:55:46.993 -> 48500  Setpoint = 20.00 Input = 19.80  Output = 33.09
19:55:47.462 -> 49000  Setpoint = 20.00 Input = 20.04  Output = 33.06
19:55:47.977 -> 49500  Setpoint = 20.00 Input = 20.03  Output = 33.06
19:55:48.493 -> 50000  Setpoint = 20.00 Input = 20.23  Output = 33.02
19:55:48.962 -> 50500  Setpoint = 20.00 Input = 20.18  Output = 33.01
19:55:49.477 -> 51000  Setpoint = 20.00 Input = 20.34  Output = 32.96
19:55:49.946 -> 51500  Setpoint = 20.00 Input = 20.28  Output = 32.94
19:55:50.461 -> 52000  Setpoint = 20.00 Input = 20.22  Output = 32.93
19:55:50.977 -> 52500  Setpoint = 20.00 Input = 20.18  Output = 32.91
19:55:51.445 -> 53000  Setpoint = 20.00 Input = 20.14  Output = 32.90

Hallo,
na ja das ist jetzt Regelungstechnik Grundlagen

Also Kd Ki erst mal abschalten. 0
Kp kleiner machen bis es nicht nur wenig Überschwingt. Dabei bleibt dann allerdings eine bleibende Regeldifferenz. Bei kleineren Kp wird die Regeldifferenz kleiner, aber irgendwann schwingt es stark. Optimal währen ein einmaliger Überschwingt von 7% des Sollwertsprungs. Wenn du dann den Ki dazunimmst wird die Regeldifferenz langsam kleiner, der Istwert läuft langsam auf den Sollwert zu. Nun kannst du KI grosser machen , damit wird der Regler dann schneller und der Sollwert wird schneller erreicht. Wenn das Überschwinget dann stärker wird als vorher nur mit KP solltest Du KI wieder etwas zurück nehmen.
KD kannst du eigendlich vergessen , das dient dazu Lastsprünge schneller auszuregeln bei wechselnden Lasten.
Fang Mal mit KP =1 an , dann 0.5 , 0.2 usw.
Bei KI fängst du mit 0.1 an dann 0.2 wenn sich nix tut machst du mit 0.5,dann 1 usw weiter
Ich denke aber dein Kp ist schon zu klein. Aber dazu muss Mann erst in als Ki und KD ausschalten