Advertisement
Guest User

A more complete listing

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