Created
January 24, 2018 07:20
-
-
Save Gui13/72b834bb4ef106ecaabdd8beee18b556 to your computer and use it in GitHub Desktop.
Xbox control of pan tilt hat
This file contains 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
import xbox | |
import pantilthat | |
import time | |
PAN_MAX = 90 | |
PAN_MIN = -90 | |
TILT_MAX = 90 | |
TILT_MIN = -90 | |
DELTA_TIME = 0.05 | |
DEG_PER_SECOND = 180 | |
JOY_DELTA = 0.1 | |
pan = 0 | |
tilt = 0 | |
if __name__ == '__main__': | |
joy = xbox.Joystick() | |
pantilthat.pan(pan) | |
pantilthat.tilt(tilt) | |
delta = DEG_PER_SECOND * DELTA_TIME | |
while not joy.Back(): | |
if joy.A(): | |
print("Reseting camera") | |
pantilthat.pan(0) | |
pantilthat.tilt(0) | |
else: | |
# Servo | |
x, y = joy.leftStick() | |
if x > 0 and (pan - delta > -90): | |
pan -= delta | |
elif x < 0 and pan + delta < 90: | |
pan += delta | |
if y > 0 and tilt - delta > -90: | |
tilt -= delta | |
elif y < 0 and tilt + delta < 90: | |
tilt += delta | |
pantilthat.pan(int(x * -90. /1.5)) | |
pantilthat.tilt(int(y * -90./1.5)) | |
# print("delta {} x={} y={} pan={} tilt={}" | |
# .format(delta, x, y, pan, tilt)) | |
# pantilthat.pan(int(pan)) | |
# pantilthat.tilt(int(tilt)) | |
rtrig = joy.rightTrigger() | |
ltrig = joy.leftTrigger() | |
if rtrig > JOY_DELTA: | |
print("RTrigger {}".format(rtrig)) | |
pantilthat.tilt(int(rtrig * 90.)) | |
if ltrig > JOY_DELTA: | |
print("RTrigger {}".format(ltrig)) | |
pantilthat.tilt(int(ltrig * -90.)) | |
time.sleep(0.05) | |
print("Back pressed, exit") | |
joy.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment