Adapting an algorithm PID to an other motor? Possible or not?

Hi everyone!

I have this TT engine with encoder (6V 160RPM 120: 1) and I use the algorithm put on their site

https://www.dfrobot.com/wiki/index.php/Micro_DC_Motor_with_Encoder-SJ01_SKU:_FIT0450#Encoder_Sample_Code_1.

//The sample code for driving one way motor encoder
#include <PID_v1.h>
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 3;//B pin -> the digital pin 3
int E_left =5; //The enabling of L298PDC motor driver board connection to the digital interface port 5
int M_left =4; //The enabling of L298PDC motor driver board connection to the digital interface port 4
byte encoder0PinALast;
double duration,abs_duration;//the number of the pulses
boolean Direction;//the rotation direction 
boolean result;

double val_output;//Power supplied to the motor PWM value.
double Setpoint;
double Kp=0.6, Ki=5, Kd=0;  
PID myPID(&abs_duration, &val_output, &Setpoint, Kp, Ki, Kd, DIRECT); 
 
void setup()
{  
  Serial.begin(9600);//Initialize the serial port
   pinMode(M_left, OUTPUT);   //L298P Control port settings DC motor driver board for the output mode
   pinMode(E_left, OUTPUT); 
   Setpoint =80;  //Set the output value of the PID
   myPID.SetMode(AUTOMATIC);//PID is set to automatic mode
   myPID.SetSampleTime(100);//Set PID sampling frequency is 100ms
  EncoderInit();//Initialize the module
}
 
void loop()
{   
      advance();//Motor Forward
      abs_duration=abs(duration);
      result=myPID.Compute();//PID conversion is complete and returns 1
      if(result)
      {
        Serial.print("Pluse: ");
        Serial.println(duration); 
        duration = 0; //Count clear, wait for the next count
      }
        

}
 
void EncoderInit()
{
  Direction = true;//default -> Forward  
  pinMode(encoder0pinB,INPUT);  
  attachInterrupt(0, wheelSpeed, CHANGE);
}
 
void wheelSpeed()
{
  int Lstate = digitalRead(encoder0pinA);
  if((encoder0PinALast == LOW) && Lstate==HIGH)
  {
    int val = digitalRead(encoder0pinB);
    if(val == LOW && Direction)
    {
      Direction = false; //Reverse
    }
    else if(val == HIGH && !Direction)
    {
      Direction = true;  //Forward
    }
  }
  encoder0PinALast = Lstate;
 
  if(!Direction)  duration++;
  else  duration--;

}
void advance()//Motor Forward
{
     digitalWrite(M_left,LOW);
     analogWrite(E_left,val_output);
}
void back()//Motor reverse
{
     digitalWrite(M_left,HIGH);
     analogWrite(E_left,val_output);
}

void Stop()//Motor stops
{
     digitalWrite(E_left, LOW); 
}

This engine makes noise at slow speed and I need it to run at max 5 revs per second.

I think so buy this engine

https://media.digikey.com/pdf/Data%20Sheets/DFRobot%20PDFs/FIT0481_Web.pdf

But I would like to know if there is a PID algorithm that DFR did, because it is rather difficult to set a PID algorithm either.

Would it be “easy” to adapt or set the engine algorithm FIT0450 for the engine FIT0481 ???

Thanks you

I think the first thing I would do is to hook up the new motor and encoder and see what happens.

The important difference (or similarity) between the motors is probably the number of encoder pulses per revolution because that is what the speed control is based on.

Adapting the program to deal with a different number of pulses should not be difficult, but expect some trial and error,

...R

I have red the library PID Arduino and I don't think that we can set the number of pulse per rotation.

In my new motor I will have:
" The average output number of pulses can reach up to 7230 pulses per revolution=420" as say by constructor
whereas my actual motor is 1920 within one round.

Can the use of the "automatic" setup in the algorithm will suffisant to manage the control of my new motor?

Or should I will change anything in the PID library?

As far as I can see the speed control in your program is based on the number of pulses counted in an interval (the variable duration - which is not a ideal name :slight_smile: ).

If you use an encoder that gives a different number of pulses for the same speed you will just need to make a correction before making the value available to the PID function.

…R