mardi 15 mai 2012

PID control: algorithm and implementation


Meet,



Mr Proportional
a fat guy that eat too much his one and only issue is his stomach :pp





Mr Integrator

 a punctual guy and obsessed with time he can never be satisfied with something unless he finished it all.






Mr Derivator
an athlete that loves to run, sometimes slow and sometimes fast and he can make his colleagues run faster.








Mr P Mr I and Mr D can make a great combination to make any system work at his best withe all the energy that given by Mr P all the punctuality that is given by Mr I and all the speed that it's given by Mr D.


So PID Controller is a command law that controls a system to respond to a specific order.
Our objective to make a specific system respond to our command with the minimum possible error as fast as possible and with maintaining it's stabilité

So let's look to this system:
It can be controlled by two different way.
The first method is an open loop command  it's a very simple command we just put the command manually and the system will react according to that command BUT that means we don't have any idea about what's going on in the end and even that we can observe our system working we don't have an exact idea about his performance.

The second method is a close loop command that means we give a desired goal for example a specific position the system then will calculate the error = desired goal - output and create the command accordingly so in this case the command is generated automatically. 
here we can notice that the desired goal and the output must be with the same unity that means the desired goal must much the unity of the system output.

Where's the PID?

well PID regulator is the first one which meets the error calculated each time.


As we already saw Mr P, I and D are the 3 gentlemen who constitute the PID regulator.

Mr P is simply the error that we already talked about.
Mr I is the sum of all the errors calculated
Mr D present the Dynamic of the errors whether the error value are changed suddenly or smoothly we should consider that in out regulator.

How can I much this gentlemen with my specific system?

well here we need a little magic. You should know the exact amount of food to give to Mr P Let's called Kp. The exact sense of punctuality to Mr I (Ki) and the perfect body to Mr D (Kd).

Implementation:

If you want to implement your PID controller into a microcontroller you need to follow this steps;

1° Create a Timer interrupt (which represent the sampling time)
2° predefine your PID parameters
3° in the Timer routine calculate the 3 Mrs:
   MrP = target-output
-------------------------------
  MrI=MrP+LastMrI
 LastMr+=MrI
------------------------------
MrD=MrP-LastMrD
LastMrD=MrP
------------------------------
4°calculate the correction
 correction = Kp*Mrp+Ki*MrI+ Kd*MrD
5°Saturation:
correction mustn't exceed the maximum value the actuator would take so we need to do a saturation routine

N°: Sometimes "correction" have negative values so if we are dealing with motors that means turning in the other sens if we are dealing with a temperature regulation that means we should stop or cooling our system.

dimanche 6 mai 2012

use the visual assistant

visual assistant is a wonderful tool that allows you to create complex image processing alghorithm with simple cliks

USB communication


In this tutorial I'm using a PIC18F4550 with 48MHz crystal to communicate through usb with PC.
If you are using proteus ISIS I'm happy to tell you that there's a wonderful feature which create a virtual usb driver so you don't need any electronics to test your program.

I'm going to show you how to create a project that includes the usb HID (Human interface devices) library. So after this project you will be able to create your own HID devices with your product and vendor IDs.

First start by creating a new Projects with all the settings mentioned above and copy this code which is by the way the example code given by the Help.

unsigned char readbuff[64] absolute 0x500;   // Buffers should be in USB RAM, please consult datasheet
unsigned char writebuff[64] absolute 0x540;

char cnt;
char kk;

void interrupt(){
   USB_Interrupt_Proc();                   // USB servicing is done inside the interrupt
}

void main(void){
  ADCON1 |= 0x0F;                         // Configure all ports with analog function as digital
  CMCON  |= 7;                            // Disable comparators

  HID_Enable(&readbuff,&writebuff);       // Enable HID communication

  while(1){
    while(!HID_Read())
      ;

    for(cnt=0;cnt<64;cnt++)
      writebuff[cnt]=readbuff[cnt];

    while(!HID_Write(&writebuff,64))
      ;
  }
}
The code simply send back what he received ^^

Now Tools->HID terminal

Set your VID and PID your Vendor Name and Product Name as you want.
Then select the mikroC radio and save your descriptor file with your project files (not an obligation but recommended :p ).

Go to view-->project manager
right click on the "source" folder and choose "add files to project" to add the descriptor file you already created.

before compiling one last step:
go to edit project and set it like in the picture


Finally Compile your project. It should work fine if you are not using the demo version.