Advertisement
Guest User

PI_Controller.py no Ada

a guest
Nov 22nd, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #!/usr/bin/python
  2. """
  3. PI Controller for BeagleBone Black
  4. """
  5. __author__ = "Joe Moquin"
  6. __copyright__ = "Copyright 2015, Smart Green Power Node"
  7. __credits__="""
  8. Brett Beauregard:
  9. http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/
  10. """
  11. __email__ = "sjmoquin@gmail.com"
  12. __maintainer__ = "Joe Moquin"
  13. __status__ = "Alpha"
  14. __version__ = "0.1"
  15.  
  16. from time import clock
  17. #import Adafruit_BBIO.ADC as ADC
  18. #import Adafruit_BBIO.PWM as PWM
  19. import signal
  20.  
  21. class PI_Controller:
  22. """
  23. Handles storage and calculation of PWM output from ADC input.
  24. Derivative values removed.
  25. """
  26. def __init__(self):
  27. self.ADC_input = 0.0
  28. self.errSum = 0.0
  29. self.kp = 0.0
  30. self.ki = 0.0
  31.  
  32. self.lastTime = 0
  33. self.lastErr = 0.0
  34. self.PWM_output = 0.0
  35. self.sample_time = 1000
  36. self.setpoint = 0.0
  37.  
  38. def Compute(self, ADC_input):
  39. #For reporting
  40. self.ADC_input = ADC_input
  41. #How long since we last calculated, in miliseconds
  42. now = int(1000*clock())
  43.  
  44. dt = now - self.lastTime
  45. if dt >= self.sample_time
  46. """
  47. #boost_voltage = Black_Magic * ADC_input
  48.  
  49. #Compute all the working error variables for PI
  50. error = self.setpoint - boost_voltage
  51. self.errSum = self.errSum + error
  52.  
  53.  
  54. #Compute PID Output
  55. boost_feedback_voltage = self.kp * error + self.ki * self.errSum
  56. PWM_output = Devil_Magic * boost_feedback_voltage
  57.  
  58. #For reporting
  59. self.PWM_output = PWM_output
  60. """
  61. PWM_output = 90.0
  62. self.PWM_output
  63. #Remember some variables for next time
  64. #self.lastErr = error
  65. self.lastTime = now
  66.  
  67. return PWM_output
  68. else:
  69. return self.PWM_output
  70.  
  71. def print_handler(self, signum, frame):
  72. """
  73. Prints ADC input and PWM output upon USR1 signal from environment.
  74.  
  75. In Terminal, type:
  76. ps -C python
  77.  
  78. You'll get something like
  79. debian@beaglebone:~$ ps -C python
  80. PID TTY TIME CMD
  81. 2276 pts/0 00:04:16 python
  82. in reply.
  83.  
  84. Type in the following, using the PID from the ps command:
  85. sudo kill -s USR1 2276
  86.  
  87. In the PI_Controller.py's terminal, the following will print.
  88. """
  89. print "ADC_input:\t{}".format(self.ADC_input)
  90. print "boost_PWM:\t{}".format(self.PWM_output)
  91.  
  92. def set_kp(self, kp):
  93. self.kp = kp
  94.  
  95. def set_ki(self, ki):
  96. self.ki = ki * self.sample_time/1000
  97.  
  98. def set_setpoint(self, setpoint):
  99. self.setpoint = setpoint
  100.  
  101.  
  102.  
  103.  
  104. if __name__ == "__main__":
  105. #ADC.setup()
  106. #PWM.start(channel, duty, freq=2000, polarity=0)
  107. #PWM.start("P9_14", 100, 10000, 0)
  108. pi = PI_Controller()
  109. signal.signal(signal.SIGUSR1, pi.print_handler)
  110.  
  111. while True:
  112. #Pesky ADC read gives faulty values for first few reads.
  113. #So, just read a few times, saving the last read.
  114. ADC_input = 0.5
  115. #for __ in range(5):
  116. # ADC_input = ADC.read("P9_40")
  117.  
  118. boost_PWM = pi.Compute(ADC_input)
  119. #PWM.set_frequency(boost_PWM)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement