Advertisement
UnaClocker

Stepper Motor Controller

Nov 4th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Import required libraries
  4. import time
  5. import pygame
  6. import RPi.GPIO as GPIO
  7.  
  8. # Use BCM GPIO references
  9. # instead of physical pin numbers
  10. GPIO.setmode(GPIO.BCM)
  11. GPIO.setwarnings(False)
  12. # Define some settings
  13. StepCounter = 0
  14. WaitTime = .01
  15. steppingOne = False
  16. steppingTwo = False
  17. positionOne = 0
  18. positionTwo = 0
  19. targetOne = 0
  20. targetTwo = 0
  21.  
  22. stepPins = []
  23. stepPattern = []
  24. stepPattern = range(0, 8)
  25. stepPattern[0] = [1,0,0,0] # Starting step
  26. stepPattern[1] = [1,1,0,0]
  27. stepPattern[2] = [0,1,0,0] # Second step
  28. stepPattern[3] = [0,1,1,0]
  29. stepPattern[4] = [0,0,1,0] # Third step
  30. stepPattern[5] = [0,0,1,1]
  31. stepPattern[6] = [0,0,0,1] # Fourth step
  32. stepPattern[7] = [1,0,0,1]
  33.  
  34.  
  35. def moveFlap(zone, openIt): # zone is 1 or 2, openIt is true or false
  36.     global stepPins
  37.     if (zone==1):
  38.         if (openIt):
  39.             stepPins = [17,4,22,21]
  40.         else:
  41.             stepPins = [21,22,4,17]
  42.     else:
  43.         if (openIt):
  44.             stepPins = [23,18,25,24]
  45.         else:
  46.             stepPins = [24,25,18,23]
  47.  
  48. def initGPIOpins():
  49.     # Set all pins as output
  50.     global stepPins
  51.     stepPins = [4,17,18,21,22,23,24,25]
  52.     for pin in stepPins:
  53.         GPIO.setup(pin,GPIO.OUT)
  54.         GPIO.output(pin, False)
  55.  
  56. def stepNow():
  57.     for pin in range(0, 4):
  58.         xpin = StepPins[pin]
  59.         for stepCounter in range(0, 8):
  60.             if StepPattern[stepCounter][pin]!=0:
  61.             # print " Step %i Enable %i" %(StepCounter,xpin)
  62.                 GPIO.output(xpin, True)
  63.             else:
  64.                 GPIO.output(xpin, False)
  65.  
  66. def moveAsRequested():
  67.     global positionOne, targetOne, positionTwo, targetTwo
  68.     if (positionOne != targetOne):
  69.         steppingOne = True
  70.         moveFlap(1, positionOne>targetOne)
  71.         stepNow()
  72.         if positionOne>targetOne:
  73.             positionOne -= 4
  74.         else:
  75.             positionOne += 4   
  76.     elif (positionTwo != targetTwo):
  77.         steppingTwo = True
  78.         moveFlap(2, positionTwo>targetTwo)
  79.         stepNow()
  80.         if positionTwo>targetTwo:
  81.             positionTwo -= 4
  82.         else:
  83.             positionTwo += 4
  84.    
  85.  
  86. def main():
  87.     pygame.init()
  88.     initGPIOpins()
  89.     screen = pygame.display.set_mode((640,480))
  90.     pygame.display.set_caption("Heater Controller")
  91.     clock = pygame.time.Clock()
  92.     # Start main loop
  93.     while 1==1:
  94.         clock.tick(50)
  95.         moveAsRequested()
  96.     # Handle events
  97.         for event in pygame.event.get():
  98.             if event.type == pygame.QUIT:
  99.                 return
  100.             elif event.type == pygame.KEYDOWN:
  101.                 if event.key == pygame.K_q:
  102.                     pygame.quit()
  103.                     return
  104.                 elif event.key == pygame.K_g:
  105.                     targetOne=100
  106.                    
  107.         screen.fill((0,0,0))
  108.         pygame.display.flip()
  109.  
  110.   # If we reach the end of the sequence
  111.   # start again
  112.  
  113.   # Wait before moving on
  114. if __name__ == "__main__":
  115.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement