pid.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Copyright (c) 2012, Falkor Systems, Inc.  All rights reserved.
00003 
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions are
00006 # met:
00007 
00008 # Redistributions of source code must retain the above copyright notice,
00009 # this list of conditions and the following disclaimer.  Redistributions
00010 # in binary form must reproduce the above copyright notice, this list of
00011 # conditions and the following disclaimer in the documentation and/or
00012 # other materials provided with the distribution.  THIS SOFTWARE IS
00013 # PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00014 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00015 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00016 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
00017 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00018 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00019 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00020 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00021 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00022 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00023 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024 
00025 class Pid2:
00026     def __init__( self, gain_p = 0.0, gain_i = 0.0, gain_d = 0.0,
00027                   time_constant = 0.0, limit = -1.0 ):
00028         self.gain_p = gain_p
00029         self.gain_i = gain_i
00030         self.gain_d = gain_d
00031         self.time_constant = time_constant
00032         self.limit = limit
00033 
00034         self.input = 0.0
00035         self.dinput = 0.0
00036         self.output = 0.0
00037         self.p = 0.0
00038         self.i = 0.0
00039         self.d = 0.0
00040 
00041     def update( self, new_input, x, dx, dt ):
00042         if self.limit > 0.0 and abs( new_input ) > self.limit:
00043             if new_input < 0:
00044                 new_input = - self.limit
00045             else:
00046                 new_input = self.limit
00047 
00048         if dt + self.time_constant > 0.0:
00049             self.dinput = ( new_input - self.input ) / ( dt + self.time_constant )
00050             self.input = ( dt * new_input + self.time_constant * self.input ) / ( dt + self.time_constant )
00051 
00052         self.p = self.input - x
00053         self.d = self.dinput - dx
00054         self.i = self.i + dt * self.p
00055 
00056 #        print self.p,self.d,self.i
00057 
00058         self.output = self.gain_p * self.p + self.gain_d * self.d + self.gain_i * self.i
00059         return self.output
00060 
00061     def reset( self ):
00062         self.input = self.dinput = 0.0
00063         self.p = self.i = self.d = 0.0
00064 
00065         
00066 class Pid:
00067     def __init__( self, Kp = 0.0, Ti = 0.0, Td = 0.0, outputLimit = None ):
00068         self.Kp = Kp
00069         self.Ti = Ti
00070         self.Td = Td
00071 
00072         self.prev_error = 0.0
00073         self.integral = 0.0
00074 
00075         self.outputLimit = outputLimit
00076         self.setPointMin = self.setPointMax = 0.0
00077 
00078     def get_output( self, pv, dt ):
00079         if( pv < self.setPointMax and pv > self.setPointMin ):
00080             error = 0
00081         else:
00082             errorFromMax = self.setPointMax - pv
00083             errorFromMin = self.setPointMin - pv
00084 
00085             if abs( errorFromMax ) > abs( errorFromMin ):
00086                 error = errorFromMin
00087             else:
00088                 error = errorFromMax
00089 
00090         self.integral += error * dt
00091 
00092         if dt != 0:
00093             derivative = ( error - self.prev_error ) / dt
00094         else:
00095             derivative = 0
00096 
00097         self.prev_error = error
00098 
00099         if self.Ti == 0.0:
00100             TiInv = 0
00101         else:
00102             TiInv = 1/self.Ti
00103 
00104         output = self.Kp * ( error + TiInv * self.integral + self.Td * derivative )
00105         if self.outputLimit != None:
00106             limitedOutput = min( max( output, -self.outputLimit ),
00107                                  self.outputLimit )
00108             return limitedOutput
00109         else:
00110             return output


falkor_ardrone
Author(s): Sameer Parekh
autogenerated on Tue Jan 7 2014 11:12:13