Skip to content

Instantly share code, notes, and snippets.

@Gui13
Created January 24, 2018 07:20
Show Gist options
  • Save Gui13/72b834bb4ef106ecaabdd8beee18b556 to your computer and use it in GitHub Desktop.
Save Gui13/72b834bb4ef106ecaabdd8beee18b556 to your computer and use it in GitHub Desktop.
Xbox control of pan tilt hat
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