Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- import RPi.GPIO as gpio
- gpio.setmode(gpio.BOARD)
- gpio.setup(10,gpio.OUT)
- pwm = gpio.PWM(10,80)
- pwm.start(0)
- def SetAngle(angle):
- duty = angle / 18 + 2
- gpio.output(10, True)
- pwm.ChangeDutyCycle(duty)
- sleep(1)
- gpio.output(10, False)
- # pwm.ChangeDutyCycle(0)
- try:
- while True:
- angle = float(input('ENTER ANGLE PLEASE: '))
- SetAngle(angle)
- except:
- SetAngle(0)
- pwm.stop()
- gpio.cleanup()
- import pigpio
- import math
- GPIO_SERVO_PIN = 10
- MIN_ANG=-180.0 #degrees
- MAX_ANG=180.0 #degrees
- MIN_PW=1000 # microseconds
- MAX_PW=2000 # microseconds
- ANG_RANGE=MAX_ANG-MIN_ANG
- PW_RANGE=MAX_PW-MIN_PW
- PWAR=float(PW_RANGE)/ANG_RANGE
- RAD2DEG=180.0/math.pi
- def angleToPulseWidth(angle):
- """
- angle is mapped to valid pulse widths for servo
- which are determined by experiment.
- """
- assert MIN_ANG <= angle <= MAX_ANG
- return MIN_PW + ((angle - MIN_ANG) * PWAR)
- pi = pigpio.pi()
- if not pi.connected:
- exit()
- while True:
- angle = float(input('Please enter a angle: '))
- pw = angleToPulseWidth(angle)
- pi.set_servo_pulsewidth(GPIO_SERVO_PIN, pw)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement