Libstock report: PID Library available for download!

Here you can find latest news on mikroElektronika products.
Post Reply
Author
Message
User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

Libstock report: PID Library available for download!

#1 Post by anikolic » 27 Sep 2011 18:05

Libstock report: PID Library available for download!

Libstock's top contributor, Dany Rossel, has just released his new PID Library.
Many of you have asked for it, and we hope this is the right solution for you.
Library has intuitive function prototype, and comes in Package manager form with Help, Source, MCL files and examples.

We invite you to visit the Library page on Libstock and read Dany's blog to see whether this library is what you might need.
Web Department Manager

octal
Posts: 534
Joined: 09 Feb 2005 14:15
Location: France
Contact:

Re: Libstock report: PID Library available for download!

#2 Post by octal » 29 Sep 2011 11:25

Hello,
thanks to Dany for his great contribution.

I took a bit of time and ported the lib to mikroBASIC. If Dany is interrested he can add my codes to his own codes on his libstock page to centralise the lib on one and unique access point.

The codes are attached as a zip to this post

Code: Select all

module PID_Lib

' ---------------------------------------------------------- '
' ---- PID library with fixed calculation time interval ---- '
' ---------------------------------------------------------- '

' Ahmed Lazreg
' ahmed.lazreg@pocketmt.com

' Translated from original mikroPascal code from D.Rossel

' D. Rosseel
' Original: 27-09-2011
' Latest update: 27-09-2011

' History:
'  29-09-2011: Translation from Pascal Code to MikroBasic
'  27-09-2011: Original Version (in mikroPascal).


' Documentation: http://en.wikipedia.org/wiki/PID_controller/ and http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/


' interface

sub procedure Init_PID(dim Kp, Ki, Kd, MinOutput, MaxOutput as float)
' Initialises the PID engine
' Kp = the "proportional" error multiplier
' Ki = the "integrated value" error multiplier
' Kd = the "derivative" error multiplier
' MinOutput = the minimal value the output value can have (should be < 0)
' MaxOutput = the maximal value the output can have (should be > 0)

sub procedure Reset_PID
' Re-initialises the PID engine without change of settings

sub function  PID_Calculate(dim Setpoint, InputValue as float) as float
' To be called at a regular time interval (e.g. every 100 msec)
' Setpoint: the target value for "InputValue" to be reached
' InputValue: the actual value measured in the system
' Functionresult: PID function of (SetPoint - InputValue),
'   a positive value means "InputValue" is too low (< SetPoint), the process should take action to increase it
'   a negative value means "InputValue" is too high (> SetPoint), the process should take action to decrease it

implements

dim PID_Kp, PID_Ki, PID_Kd as float
dim PID_Integrated as float
dim PID_Prev_Input as float
dim PID_MinOutput, PID_MaxOutput as float
dim PID_First_Time as boolean

sub procedure Reset_PID
  PID_Integrated = 0.0
  PID_Prev_Input = 0.0
  PID_First_Time = true
end sub

sub procedure Init_PID(dim Kp, Ki, Kd, MinOutput, MaxOutput as float)
  PID_Kp         = Kp
  PID_Ki         = Ki
  PID_Kd         = Kd
  PID_MinOutput  = MinOutput
  PID_MaxOutput  = MaxOutput
  PID_Integrated = 0.0
  PID_Prev_Input = 0.0
  PID_First_Time = true
end sub

sub function PID_Calculate(dim Setpoint, InputValue as float) as float
dim TheErr, ErrValue, DiffValue as float

  TheErr = SetPoint - InputValue

  ' --- calculate proportional value ---
  ErrValue  = TheErr * PID_Kp

  ' --- Calculate integrated value ---
  PID_Integrated = PID_Integrated + (TheErr * PID_Ki)
  ' limit it to output minimum and maximum
  if PID_Integrated < PID_MinOutput then 
    PID_Integrated = PID_MinOutput
  end if
  if PID_Integrated > PID_MaxOutput then 
    PID_Integrated = PID_MaxOutput
  end if

  ' --- calculate derivative value ---
  if PID_First_Time then  ' to avoid a huge DiffValue the first time (PID_Prev_Input = 0)
    PID_First_Time = false
    PID_Prev_Input = InputValue
  end if
  DiffValue = (InputValue - PID_Prev_Input) * PID_Kd
  PID_Prev_Input = InputValue

  ' --- calculate total ---
  Result = ErrValue + PID_Integrated - DiffValue ' mind the minus sign!!!
  ' limit it to output minimum and maximum
  if Result < PID_MinOutput then 
    Result = PID_MinOutput
  end if
  if Result > PID_MaxOutput then 
    Result = PID_MaxOutput
  end if

end sub

end.
Attachments
PID_Lib.zip
(1.24 KiB) Downloaded 1065 times
http://www.pocketmt.com

kaniux
Posts: 4
Joined: 07 Oct 2011 17:15

Re: Libstock report: PID Library available for download!

#3 Post by kaniux » 15 Oct 2011 18:53

do you have in mind released this library for mikroC version??

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Libstock report: PID Library available for download!

#4 Post by Dany » 17 Oct 2011 11:08

kaniux wrote:do you have in mind released this library for mikroC version??
Octal provided also the C version now. It is available in Libstock, see http://www.libstock.com/projects/view/161/pid-library.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “Product Announcements”