Motor Drive available which needs input of 0-5V analog voltage.
Using Arduino Due the output may be 0.55 - 2.75.
There is a circuit of differential OPAMP , to raise the analog voltage to 0-5V
Motor drive needs seperate Motor brake and direction signals from controller.
BLDC motor & encoder available.
BLDC Motor : Speed 200 RPM, 3Nm
Encoder : 20,000 pulses./ rotation (Make:IVO, Model;G0355, partNo.72, datasheet enclosed )
Now I made code by duplicating the PID library code instead of using library to use the same code in any platform and also to learn things being a new to this area.
Attaching the code.
Now I want read the Potentiometer Value(setpoint/potPos) and encoderPos(feedbackPos) to create error signal.
I have not understood to relate the PotPos and encoder counts.
How to program the encoderPos (counts as change, rising or falling) and relate with Potentiometer Value
to generate Error signal.
The computed PID output should be Analog signal.
Application is servo position control of elevation.
int encoderA = 2;
int encoderB = 3;
int MotBrake = 6;
int MotDirection = 4;
double kp = 0, ki = 0, kd = 0;
double PotPos = 0, feedbackPos = 0, output = 0;
volatile long encoderPos = 0;
unsigned long lastTime;
double Iterm, lastfeedbackPos;
int sampleTime = 1000;
double outMin, outMax;
bool inAuto = false;
#define MANUAL 0
#define AUTOMATIC 1
#define DIRECT 0
#define REVERSE 1
int controllerDirection = DIRECT;
void setup()
{
pinMode(encoderA, INPUT_PULLUP);
pinMode(encoderB, INPUT_PULLUP);
pinMode(DAC0, OUTPUT);
pinMode(A0, INPUT);
analogWriteResolution(12);
analogReadResolution(12);
attachInterrupt(0, encoder, FALLING);
SetMode(AUTOMATIC);
SetOutputLimits(0, 4095);
setSampleTime(1);
Serial.begin(115200);
}
void loop()
{
PotPos=analogRead(A0);
Serial.println(PotPos);
feedbackPos=encoderPos;
Serial.println(encoderPos);
compute();
analogWrite(DAC0,output);
Serial.println(output);
if (output==0)
digitalWrite(MotBrake, LOW);
else
digitalWrite(MotBrake,HIGH);
}
void compute()
{
if(!inAuto)
return;
unsigned long now=millis();
int timeChange =(now-lastTime);
if(timeChange>+sampleTime)
{
double error=(PotPos-feedbackPos);
Iterm+=(ki*error);
if (Iterm>outMax)
Iterm=outMax;
else if(Iterm<outMin)
Iterm=outMin;
double dfeedbackPos=(feedbackPos-lastfeedbackPos);
output=kp*error+Iterm-kd*dfeedbackPos;
if(output>outMax)
output=outMax;
else if(output<outMin)
output=outMin;
lastfeedbackPos=feedbackPos;
lastTime=now;
}
}
void encoder()
{
}
void SetMode(int Mode)
{
bool newAuto = (Mode == AUTOMATIC);
if (newAuto && !inAuto)
{
initialize();
}
inAuto = newAuto;
}
void initialize()
{
lastfeedbackPos=feedbackPos;
Iterm=output;
if(Iterm>outMax)
Iterm=outMax;
else if(Iterm<outMin)
Iterm=outMin;
}
void SetOutputLimits(double Min, double Max)
{
if(Min>Max)
return;
outMin=Min;
outMax=Max;
if(output>outMax)
output=outMax;
else if (output<outMin)
output=outMin;
if(Iterm>outMax)
Iterm=outMax;
else if (Iterm<outMin)
Iterm=outMin;
}
void setSampleTime(int NewSampleTime)
{
if(NewSampleTime>0)
{
double ratio = (double)NewSampleTime/(double)sampleTime;
ki*=ratio;
kd/=ratio;
sampleTime=(unsigned long)NewSampleTime;
}
}
void setTuning(double Kp,double Ki, double Kd)
{
if (kp<0 || ki<0 ||kd<0)
return;
double SampleTimeinSec = ((double)sampleTime)/1000;
kp=Kp;
ki=Ki*SampleTimeinSec;
kd=Kd/SampleTimeinSec;
if(controllerDirection==REVERSE)
{kp=(0-kp);
ki=(0-ki);
kd=(0-kd);
}
}
TomGeorge:
Hi,
Have you got the hardware parts and assembled?
Have you got code that makes sure you are able to JUST read the encoder?
Have you got code that makes sure you are able to JUST control the BLDC motor?
What is your projects application?
Thanks.. Tom... 
TomGeorge:
Hi,
Have you got the hardware parts and assembled?
Have you got code that makes sure you are able to JUST read the encoder?
Have you got code that makes sure you are able to JUST control the BLDC motor?
What is your projects application?
Thanks.. Tom... 
TomGeorge:
Hi,
Have you got the hardware parts and assembled?
Have you got code that makes sure you are able to JUST read the encoder?
Have you got code that makes sure you are able to JUST control the BLDC motor?
What is your projects application?
Thanks.. Tom... 
incremental encoder.pdf (131 KB)