mkner / basic-pid

A Python PID controller that is easy to use, works and does the job. The PID controller implements timestep integration that is designed to be used in discrete-time regulators.
https://pypi.org/project/basic-pid/
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Start the pid with initial value ? #1

Open ewok2 opened 1 month ago

ewok2 commented 1 month ago

Hello I use the pid to filter a temperature and wich a the first iteration to start with initial value. Is it possible to set the PID so that the first iteration is not zero but the initial value? => doesit have any sens? Maybe by setting

       self._pid_out 
       self._pid_out_prev 

to a intila value?

mkner commented 1 month ago

it depends on which initial values you want to set: you can set the gains to initial values before beginning time-step iterations, or if you want to set an initial value for the control signal you are sending to some device or process plant then you can use BasicPid in iterative mode (instead of integrative mode) and handle the time-step integrations manually in a loop see some examples in RTD for BasicPid for more @ https://basic-pid.readthedocs.io/en/latest/examples.html

ewok2 commented 1 month ago

I just had to change the pid.py file to achieve my goal :-) I just add the folowing function :

     def setInit(self,initial):
         # set initial value
         # keeps gains intact
         self._P = 0
         self._I = 0
         self._D = 0
         self._e_prev = 0 # previous error (t-1)
         self._e_prev_prev = 0 # prev-previous error (t-2)
         self._pid_out = initial
         self._pid_out_prev = initial

just befor the reset function, and it works

I just have to call "pid.setInit(initalvalue)" befor the loop

mkner commented 1 month ago

yep, you got it! just jump start the 1st iteration with initial PID evaluation (output) values. Did you then just run PID in integrate mode? I designed & coded BasicPID to be clean & logical so can be relatively easy to modify & customize. I was going to put in an interface function for setting initial values but left it for the first stable release. I will put it in for the next. thanks for asking about this. any feedback is welcome.

ewok2 commented 1 month ago

The only feedback is : wonderful job The code is clear and it was easy to add a new input point. Wonderful if you add this functionality in next release. I will pug on it when avalaible

Many Thanks