Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Programming PID into P18f4520

Status
Not open for further replies.

Forsaken82

Newbie level 3
Newbie level 3
Joined
Nov 1, 2010
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,312
Hi all

I am doing my FYP project (Beam Ball Balancing) and I need to c program a PID controller into my micro-controller (P18f4520).

I have done some research but the program do not seem to work. Anyone here has experience programming a PID into P18F4520 can help me with it? Any help will be greatly appreciated.

Thanks alot
 

thanks bassa

I have read through and it is for Asm code. I tried implenting it to c code but it dont work.
 

Hi!
I have implemented the PID code on dsPIC in C. You can implement it very easily on any microcontroller.
For example if you are using the PID to control the speed of a motor using PWM technique then.

NewError=Desiredspeed-Actualspeed.
ErrorSum=ErrorSum+NewError.
dutycycle=Kp*NewError+Kd*(OldError-NewError)+Ki*ErrorSum
OldError=NewError;

This is the simplest form of PID that you can implement. Note the value of OldError=DesiredSpeed at the start of the system when the motor starts from rest. Once the motor starts and the PID algorithm is called in a loop after a certain interval of time (a few millisecounds in this case) the values automatically keep updating themselves.
Regards.
 
hi!
Microchip PIC micros and C - source and sample code on this link you find a heater project with pid calculation!

---------- Post added at 19:44 ---------- Previous post was at 18:55 ----------

or try this : PID controller - Wikipedia, the free encyclopedia

previous_error = 0
integral = 0
start:
error = setpoint - actual_position
integral = integral + (error*dt)
derivative = (error - previous_error)/dt
output = (Kp*error) + (Ki*integral) + (Kd*derivative)
previous_error = error
wait(dt)
goto start
 

The following is my code and the ball is oscillating along the beam and it does not return to the set-point. Any help will be greatly appreciated. Thanks a lot.

error = setpoint - PositionData2; //calculate error values
PositionData2 = 0; //reset encoder counts ready for next sample
derror = (error - preverror);
c =((KP*error) + (KD*derror) + (KI*Ierror)); //PID equations

if (c<0)
{
RotationDir=1; //ccw
}
else if (c>0)
{
RotationDir=2; //cw
}
else
{
RotationDir=0;
}

c=((c/5)*1024);

if(output+c > 1023) //prevent PWM from overrunning 1024
{
output = 1023;
}
else
{
output = output+c; //use output from PID equations to alter motor speeds
}

preverror = error; //set previous error to current error
Ierror = Ierror + error; //add current error to integral error*/
 

Well you will have to tune your PID gains (Kp, Ki, Kd) according to your system. More over be careful about the integral term as it can cause actuator saturation (i.e. maximum PWM). The integral error is usually neglected unless the difference between the desired position and the actual position becomes very small. The reason is that if you keep on adding the integral error then it will become so large that it will cause maximum PWM duty cycle and the other terms will become ineffective. One way to deal with this problem in software is

if(error<5)
{
Ierror = Ierror + error
}
else
{
Ierror=0;
}
c =((KP*error) + (KD*derror) + (KI*Ierror)); //PID equations

Remember to keep the value of Ki very small compared to Kp and Kd. e.g. if Kp=5 and Kd=1.6, then Ki=0.005;

Hopefully this will solve your problems.
 

Thanks waseem

I have taken out the integral term already. But it still does not seem to response good.
 

Hi,

how do you read the ball position? Which kind of sensor?
Which actuator you are using to tilt the beam?
A photo would be nice...

Regards

Udo
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top