Created
April 13, 2018 19:47
-
-
Save omiq/13b38bc8e5a31fcfde8c09201a478ece to your computer and use it in GitHub Desktop.
Controlling pan-tilt with game pad on Raspberry Pi with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from evdev import InputDevice | |
import pigpio | |
pi = pigpio.pi() | |
tilt = 17 | |
pan = 27 | |
x = 1250 | |
y = 1250 | |
# want to display ALL info? | |
debug = False | |
if debug: | |
for device in evdev.list_devices(): | |
print(evdev.InputDevice(device)) | |
input("Press Enter to continue ...") | |
# create object to read input - specify YOUR device | |
controller = InputDevice('/dev/input/event0') | |
# what are the controller details? | |
if debug: | |
print(controller) | |
# read controller info in an infinite loop | |
for event in controller.read_loop(): | |
# here I am looking for the start button | |
# so I can quit | |
if event.type == 1: | |
exit() | |
# set as true above if you want to look up | |
# different button values | |
if debug: | |
print("\n\nEvent Type:{}".format(event.type)) | |
print("Event Code:{}".format(event.code)) | |
print("Event Value:{}".format(event.value)) | |
# look for pad moves | |
if event.type == 3: | |
# up and down is 17 | |
if event.code == 17: | |
if event.value == 1: | |
print("\n\n\nPad Up\n") | |
if y > 600: y -= 100 | |
if event.value == -1: | |
print("\n\n\nPad Down\n") | |
if y < 2400:y += 100 | |
pi.set_servo_pulsewidth(tilt, y) | |
# left and right is 16 | |
if event.code == 16: | |
if event.value == -1: | |
print("\n\n\nPad Left\n") | |
if x > 600: x -= 100 | |
if event.value == 1: | |
print("\n\n\nPad Right\n") | |
if x < 2400: x += 100 | |
pi.set_servo_pulsewidth(pan, x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment