Is there any code for a PID for the PSOC 5LP

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
philipnielsen90
Level 1
Level 1
10 sign-ins 5 sign-ins First question asked
 
0 Likes
1 Solution
Rolf_Nooteboom
Level 5
Level 5
10 sign-ins 5 solutions authored First solution authored

Best is to write the PID routine yourself. The PID itself isn't quite complex, however it's most often the implementation which lacks and makes it not perform well.

For this you need to separate the 3 process terms:

P: the proportional part consisting of the error (setpoint - measured value): output = error * factor (P setting)

I: the integral part consisting of the accumulated value of the error (accumulated error = accumulated error + error): output = accumulated error * factor (I setting)

😧 the differential part consisting of the change in error (delta error = error - previous error): output = delta error * factor (D setting).

What I often do is convert the 3 process outputs to a range of -100% to +100%. In other words the P, I and D output can individually control the output from fully counteract to fully contribute. The output values will be added together (resulting in a range of -300% to +300%) and will be limited to -100% to + 100%. Next this PID output value has to be converted to the usable range of what you want to control. Let's say you have a PWM which ranges from 0% to 100%, then the output would be: 50% + (PID output / 2).

The loop would simply look like:
PIDerror = Psetpoint - Pmeasured
PIDerrorAccumulated +=PIDerror
PIDerrorDelta = PIDerror - PIDerrorPrevious, PIDerrorPrevious = PIDerror
Pout = PIDerror * Psetting (make sure the Pout is in a range of -100 to +100 and limited)
Iout = PIDerrorAccumulated * Isetting (again make sure Iout is in the range of -100 to + 100)
Dout = PIDerrorDelta * Dsetting
PIDout = Pout + Iout + Dout (and limit to -100 to + 100)
PWMout = 50 + (PIDout / 2)

A good practice is also to stop adding the PIDerror to PIDerrorAccumulated when Iout comes near -100 or +100 (in order to avoid wind-up).

I strongly suggest to write the loop yourself and get a grip of the process values. It's not that hard and it's great fun too. The tuning can be rather hard but as soon as you manage to log the process values into graphs, the best setting can be determined (even with trial and error).

Regards,
Rolf


View solution in original post

0 Likes
3 Replies
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

The capabilities of the analog PSoC part were demonstrated by odissey1 in the project KIT-059_PID_ex_small-000.cywrk_.Archive01.zip
it's here:    Annotation library for CY8CKIT-059 Prototyping Kit (PSoC Creator 3.3 )
The demo project shows all-analog PID control of the levitationg globe, implemented in CY8CKIT-059 PSoC 5LP

another project for PSoC4:
PID controller project for the PSoC 4 CY8CKIT-049 42xx.
Generates PWM commands to control 3 motors.

Searching the forum will reveal a few more threads
Evgeniy

 

0 Likes
Rolf_Nooteboom
Level 5
Level 5
10 sign-ins 5 solutions authored First solution authored

Best is to write the PID routine yourself. The PID itself isn't quite complex, however it's most often the implementation which lacks and makes it not perform well.

For this you need to separate the 3 process terms:

P: the proportional part consisting of the error (setpoint - measured value): output = error * factor (P setting)

I: the integral part consisting of the accumulated value of the error (accumulated error = accumulated error + error): output = accumulated error * factor (I setting)

😧 the differential part consisting of the change in error (delta error = error - previous error): output = delta error * factor (D setting).

What I often do is convert the 3 process outputs to a range of -100% to +100%. In other words the P, I and D output can individually control the output from fully counteract to fully contribute. The output values will be added together (resulting in a range of -300% to +300%) and will be limited to -100% to + 100%. Next this PID output value has to be converted to the usable range of what you want to control. Let's say you have a PWM which ranges from 0% to 100%, then the output would be: 50% + (PID output / 2).

The loop would simply look like:
PIDerror = Psetpoint - Pmeasured
PIDerrorAccumulated +=PIDerror
PIDerrorDelta = PIDerror - PIDerrorPrevious, PIDerrorPrevious = PIDerror
Pout = PIDerror * Psetting (make sure the Pout is in a range of -100 to +100 and limited)
Iout = PIDerrorAccumulated * Isetting (again make sure Iout is in the range of -100 to + 100)
Dout = PIDerrorDelta * Dsetting
PIDout = Pout + Iout + Dout (and limit to -100 to + 100)
PWMout = 50 + (PIDout / 2)

A good practice is also to stop adding the PIDerror to PIDerrorAccumulated when Iout comes near -100 or +100 (in order to avoid wind-up).

I strongly suggest to write the loop yourself and get a grip of the process values. It's not that hard and it's great fun too. The tuning can be rather hard but as soon as you manage to log the process values into graphs, the best setting can be determined (even with trial and error).

Regards,
Rolf


0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

This  may be useful: PID without PHD 

0 Likes