Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. from time import sleep
  2. import RPi.GPIO as gpio
  3. gpio.setmode(gpio.BOARD)
  4. gpio.setup(10,gpio.OUT)
  5. pwm = gpio.PWM(10,80)
  6. pwm.start(0)
  7.  
  8. def SetAngle(angle):
  9. duty = angle / 18 + 2
  10. gpio.output(10, True)
  11. pwm.ChangeDutyCycle(duty)
  12. sleep(1)
  13. gpio.output(10, False)
  14. # pwm.ChangeDutyCycle(0)
  15. try:
  16. while True:
  17. angle = float(input('ENTER ANGLE PLEASE: '))
  18. SetAngle(angle)
  19.  
  20. except:
  21. SetAngle(0)
  22. pwm.stop()
  23. gpio.cleanup()
  24.  
  25. import pigpio
  26. import math
  27.  
  28. GPIO_SERVO_PIN = 10
  29.  
  30. MIN_ANG=-180.0 #degrees
  31. MAX_ANG=180.0 #degrees
  32.  
  33. MIN_PW=1000 # microseconds
  34. MAX_PW=2000 # microseconds
  35.  
  36. ANG_RANGE=MAX_ANG-MIN_ANG
  37. PW_RANGE=MAX_PW-MIN_PW
  38. PWAR=float(PW_RANGE)/ANG_RANGE
  39.  
  40. RAD2DEG=180.0/math.pi
  41.  
  42. def angleToPulseWidth(angle):
  43. """
  44. angle is mapped to valid pulse widths for servo
  45. which are determined by experiment.
  46. """
  47. assert MIN_ANG <= angle <= MAX_ANG
  48. return MIN_PW + ((angle - MIN_ANG) * PWAR)
  49.  
  50. pi = pigpio.pi()
  51. if not pi.connected:
  52. exit()
  53. while True:
  54. angle = float(input('Please enter a angle: '))
  55. pw = angleToPulseWidth(angle)
  56. pi.set_servo_pulsewidth(GPIO_SERVO_PIN, pw)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement