Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import time
  3. import os
  4. import RPIO
  5. import RPIO.PWM
  6.  
  7. PWM_PIN = 22
  8. PWM_FREQUENCY = 50    # Hz
  9.  
  10. PWM_DMA_CHANNEL = 0
  11. PWM_SUBCYLCLE_TIME_US = 1000/PWM_FREQUENCY * 1000
  12. PWM_PULSE_INCREMENT_US = 10
  13.  
  14. ZERO_US = 1500
  15. MIN_US = ZERO_US - 500
  16. MAX_US = ZERO_US + 500
  17.  
  18. #---------------------------------------------------------------------------
  19. def setServoPulseDurationUS( pwmPin, durationUS, lastDurationUS ):
  20.    
  21.     # Clip durationUS
  22.     if durationUS < MIN_US:
  23.         durationUS = MIN_US
  24.     if durationUS > MAX_US:
  25.         durationUS = MAX_US
  26.    
  27.     # Ensure that durationUS is an integer multiple of the smallest possible  pulse increment
  28.     pulseIncrementUS = RPIO.PWM.get_pulse_incr_us()
  29.     numPulsesNeeded = int( durationUS/pulseIncrementUS )
  30.     durationUS = numPulsesNeeded * pulseIncrementUS
  31.    
  32.     if durationUS != lastDurationUS:
  33.        
  34.         RPIO.PWM.add_channel_pulse( PWM_DMA_CHANNEL, pwmPin, 0, numPulsesNeeded )
  35.         lastDurationUS = durationUS
  36.        
  37.     return lastDurationUS
  38.  
  39. #---------------------------------------------------------------------------
  40.  
  41. RPIO.setmode( RPIO.BCM )
  42.  
  43. RPIO.PWM.setup( pulse_incr_us=PWM_PULSE_INCREMENT_US )
  44. RPIO.PWM.init_channel( PWM_DMA_CHANNEL, PWM_SUBCYLCLE_TIME_US )
  45.  
  46. lastDurationUS = None
  47.    
  48. try:
  49.  
  50.     while True:
  51.        
  52.         # Count PWM forwards
  53.         durationUS = MIN_US
  54.         while durationUS <= MAX_US:
  55.            
  56.             lastDurationUS = setServoPulseDurationUS(
  57.                 PWM_PIN, durationUS, lastDurationUS )
  58.             durationUS += 1.0
  59.             time.sleep( 1.0/1000.0 )   # Sleep 1ms
  60.        
  61.         # Count PWM backwards
  62.         durationUS = MAX_US
  63.         while durationUS >= MIN_US:
  64.            
  65.             lastDurationUS = setServoPulseDurationUS(
  66.                 PWM_PIN,  durationUS, lastDurationUS )
  67.             durationUS -= 1.0
  68.             time.sleep( 1.0/1000.0 )   # Sleep 1ms
  69.            
  70. finally:
  71.    
  72.     RPIO.PWM.clear_channel_gpio( PWM_DMA_CHANNEL, PWM_PIN )
  73.    
  74.     RPIO.PWM.cleanup()
  75.     RPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement