Skip to content
Snippets Groups Projects
Commit a600908c authored by mehtank's avatar mehtank
Browse files

A to swap vertical axes

parent 60aadeb0
No related merge requests found
#!/usr/bin/env python3
import pygame
import math
from websocket import create_connection
#helper function to change joystick range of [0,1] to [0,255]
def convertServo(OldValue):
OldMax = 1
OldMin = -1
NewMax = 255
NewMin = 0
OldRange = (OldMax - OldMin)
NewRange = (NewMax - NewMin)
NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
return int(NewValue)
def convertThrust(OldValue):
def rescale(oldValue, newMin=0, newMax=255):
oldMax = 1
oldMin = -1
OldMax = 1
OldMin = -1
NewMax = 255
NewMin = -255
oldRange = (oldMax - oldMin)
newRange = (newMax - newMin)
newValue = (((oldValue - oldMin) * newRange) / oldRange) + newMin
return int(newValue)
OldRange = (OldMax - OldMin)
NewRange = (NewMax - NewMin)
NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
return int(math.fabs(NewValue))
#helper function to change joystick range of [0,1] to [0,255]
def convertServo(axis):
return rescale(joystick.get_axis(axis), 0, 180 )
def convertThrust(data):
return abs(rescale(data, -255, 255))
#helper function to assign the proper hbridge values
def convertHBridge(data):
if data > 0:
return [1,0]
else:
return[0,1]
return [0,1]
def convertAxis(axis):
value = joystick.get_axis(axis)*-1
return convertHBridge(value) + [ convertThrust(value) ]
data = joystick.get_axis(axis)*-1
return convertHBridge(data) + [ convertThrust(data) ]
if __name__ == "__main__":
target = "ws://192.168.4.1:81"
#Set up connection to Blimp Board over websocket
#ws = create_connection(target)
......@@ -57,6 +42,16 @@ if __name__ == "__main__":
pygame.joystick.init()
done = False
# Joystick axes
turn = 0
forward = 1
vertical = 3
vertical_alt = 4
camera_l = 2
camera_r = 5
debounce = 0
while not done:
try:
for event in pygame.event.get():
......@@ -67,14 +62,19 @@ if __name__ == "__main__":
joystick.init()
axes = joystick.get_numaxes()
forward = 1
vertical = 3
turn = joystick.get_axis(0)
# Swap axes 3 and 4 on 'A' button push [ windowns vs linux ]
if joystick.get_button(0) and not debounce:
v = vertical_alt
vertical_alt = vertical
vertical = v
debounce = 20
if debounce: debounce -= 1
#Create a new binary code to send to blimp
cmd = [126] + convertAxis(forward) + [ convertServo(turn) ] + convertAxis(vertical) + [127]
cmd = [126] + convertAxis(forward) + \
[ convertServo(turn) ] + \
convertAxis(vertical) + \
[ (180 - convertServo(camera_l) + convertServo(camera_r)) // 2 ]
print(cmd)
#ws.send_binary(cmd)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment