PID + micro controller help

Status
Not open for further replies.

freska

New Member
Hi every body

i need help in how to design a PID "Proportional Integral Differentiate"
by the PIC , with assembly or C++

the Idea based on... i want to Design PID to control the speed of the motor.
A PID control algorithm is stored in the microcontroller which acts on the error signal between the desired speed and the actual speed and produce a pulse width modulated signal whose 'on' time is proportional to the output of the control signal.

so, if any one have an idea about designing the PID by PIC, tell me
or knows any Links related to that

Thanks,
freska
 
I have more ideas than can fit in a single post. Why don't we start with how you plan to measure the speed of the motor. After we establish that, we can talk about how you plan to integrate and take the derivative of the error signal.
 
there was a relation between the output voltage of the motor "around 7 volt"
this relation relates the voltage and the velocity of the motor

stea / V = K / (JS +b)(LS + R)+k^2

* moment of inertia of the rotor (J) = 0.01 kg.m^2/s^2
* damping ratio of the mechanical system (b) = 0.1 Nms
* electromotive force constant (K=Ke=Kt) = 0.01 Nm/Amp
* electric resistance (R) = 1 ohm
* electric inductance (L) = 0.5 H

so, i have the velocity ??
i found that in this Link
**broken link removed**
 
Have a look at,
**broken link removed**
**broken link removed**.
Microchip also have a few app notes on PID implementation.

Mike.
 
freska said:
there was a relation between the output voltage of the motor "around 7 volt"
this relation relates the voltage and the velocity of the motor

so, i have the velocity ??

The formula you listed does not account for all of the factors that could affect the speed of your motor. Parasitic losses in the bearings, dynamic load changes, temperature changes which affect permanent magnet field and winding resistance, and a number of other factors are directly related to the performance of yoru motor. Without any direct feedback this implementation is an open loop control system which will not work as expected unless you can account for every variable that affects the motor.

What you need is some means of directly feeding motor velocity or position back to your control system. This is typically done with some form of rotary encoder with quatrature output. Instead of worrying about all the little factors that can change your motor speed, the control system will look at the current speed of the motor and compare it to the speed that is set in your program. Should the two values be different, your PID algorithm will either increase or decrease the power to the motor in order to make its speed converge with your setpoint. This is known as a closed loop control system.
 
**phalanx

yes i do that already.......i'll explain the criteria of the project
>>>
the idea of the project based on i have 2 motors.

The speed of a DC permanent magnet motor is to be regulated using feedback control. The motor speed is measured using another dc permanent motor coupled to the first motor and acts as a generator to produce a voltage proportional to the speed of the motor. This signal is feedback to a microcontroller through a data acquisition system to a microcontroller. The desired speed is set through a potentiometer and the voltage of the potentiometer is fed to the microcontroller through the data acquisition system.

the input voltage to the motor is 12 volt, when we test the motors
we found the output is 7 volt , we Design Sallen key LPF to pass the DC
component only ...

then we followed the LPF by ADC "0804" to convert the 7 volt "analog signal
to digital number to deal with the microcontroller "PIC 16F877A"

so, the output of that ADC (say X) corresponding to the current voltage that i want to know the current velocity from that voltage, thats can be done by the relation between voltage and velocity

and the setpoint came from the another ADC after a potentiometer (say Y)

then the error = Y - X

the missing part that i have no idea about it is the PID Algorithm
and How i can converting the speed error into a variable duty ratio PWM
to give the controllable average voltage @ the motor.....

i need some one have an experience in this part of PID and PWM

thanks
~freska~
 
The error is the proportional term of PID and you multiply it by a constant k1
To compute the integral term you sum the samples at fixed intervals so delta t is 1. This is the integral term which you multiply by a constant k2. You compute the derivative by taking the difference between successive samples and dividing by delta t, which again we have chosen to be 1. You multiply the derivative term by a constant k3. To get the command output you add them all together.
Code:
COMMAND = (k1 * P) + (k2 * I) + (k3 + D)

Now you start with k1 = 1 and k2 and k3 = 0
Adjust k1 until you are happy with the response.  Then try non zero values of k2 until you get some improvement.  Adjust k3 last.  In many cases you will find that k3 = 0 is a pretty good choice, especially for motors.
 
Last edited:
** hi Papabravo

Thank you, i understand what u illustrate

but, do u have any ideas for measuring the speed of the motor ??

or can i assume the relation between the motor and the speed is Linear ??

i.e speed = const * voltage
???????
 
freska said:
** hi Papabravo

Thank you, i understand what u illustrate

but, do u have any ideas for measuring the speed of the motor ??

or can i assume the relation between the motor and the speed is Linear ??

i.e speed = const * voltage
???????

I wouldn't assume that the voltage output is linear. I would check the output of the second motor on a scope to see what you are actually getting. I'm going to guess you will see some sort of varying amplitude DC voltage the average of which will relate to motor speed. Your microcontroller will be fast enough to see these changes in voltage you will will also have to apply some sort of filtering either in the physical world or in your software to account for it.

Definitely check the motor on a scope to see how it performs.
 
I think measuring average DC voltage may get you a first approximation to speed measurement. When accurate measurement is required, it is hard to beat a rotary encoder. There are other measures you might employ like hall effect sensors or tachometer wheels, but each has it's advantages and disadvantages.
 
i write this code by mikroC
it gives me no error when simulation , but when i burn it by "winpic"
and test it in the circuit the motor does not move :S :S (
i dont know why ?!!!

Papabravo, or anyone plz check this code and tell me whats wrong
am gonna to be crazy :S :S

----------------------------------------------------------------------------
------------------------------Code-----------------------------------------
//*------------------Globals--------------------------*
unsigned short j,oj;
signed short error1,error;
unsigned short refrence_input;
unsigned short measured_output;
//*------------Initialization Function----------*
void Initmain()
{
//------(o) --> output & (1)--> input ---------
ADCON1=6;//All ADC pins to digital I/O
PORTC=0xFF;//set PORTC to FF
TRISC=0;//Port C is output
PWM_Init(5000);//intialize PWM module
PORTB=0;
PORTD=0;
TRISB=255;
TRISD=255;
PORTE=0;
TRISE=0;//set PortA is output
}
//*-------------main Program------------------*
void main()
{
Initmain();
//i want here to send start signal to ADC 1 & 2
//while( )
//{
PORTE.F1=1;
PORTE.F1=0;//start signal to ADC1 is activated
PORTE.F1=1;
delay_us(100);
refrence_input=dataport1;

PORTE.F2=1;
PORTE.F2=0;//start signal to ADC1 is activated
PORTE.F2=1;
delay_us(100);
measured_output=dataport2;

error1=refrence_input-measured_output;
//error=BCD2DEC16(error1);
//*---------------PWM Code-------------------*
j=80;
oj=0;//oj will keep the old j value
PWM_Start();//start PWM
if(error1>0)
j++;
if(error1<0)
j--;
if(oj!=j)
{//if change in duty cycle requestred
PWM_change_Duty(j);//set new duty ratio
oj=j;
Delay_us(200);//slowdown a bit
}//end if
//}
}
 
Check this microchip application note. One is for 16F684 controller and other for PIC18 but can be easily ported to 16 series.
**broken link removed**
**broken link removed**
 
Last edited:
Without a schematic, the type of processor, and the motor you are using it is impossible to draw any conclusions. The information you have provided is quite simply inadequate. Do a proper job of documenting your system, or solve your own problems, and quit wasting our time.
 
u'r right , thats my mistake
but i think that u understand me and so i ask
anyway i am using Pic16f877A
i'll attach the schematic later as i have no time now

sorry again
 
I have the code for pid control with which i am to control motor speed, but now my problem is to link it with the chip pic16f877 that i am going to use; in terms of declaring ports, interrupts and the rest, can anyone help me please...

Should i provide the code for pid?
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…