class JoystickUpdate(threading.Thread): ''' classdocs ''' def setDevice(self, devicePath): self.devicePath = devicePath def getJoystick(self): return self.joy def run(self): self.joy = Joystick() self.joy.init(self.devicePath) while(1): #time.sleep(1) print "Updating Joystick" self.joy.update()
class RemoteControl: deadzone = 4000 def __init__(self): self.udp_client = UDPClient() self.joystick = Joystick() self.auto = False @classmethod def adapt(cls, value): if value > cls.deadzone: return (value - cls.deadzone) * 100.0 / (32768 - cls.deadzone) elif value < -cls.deadzone: return (value + cls.deadzone) * 100.0 / (32768 - cls.deadzone) else: return 0.0 def update(self): self.joystick.update() left_drive = self.adapt(self.joystick.axis.get(1, 0)) right_drive = self.adapt(self.joystick.axis.get(4, 0)) self.auto = (self.auto or self.joystick.button.get(0, False)) and not self.joystick.button.get(1, False) self.udp_client.write("%.2f,%.2f,%d" % (left_drive, right_drive, self.auto))
prev_dir = -1 prev_speed = -1 while True: #speed = int(input("speed:")) #print("speed:", speed) #dir_input = int(input("direction:")) #direction = 0 #if dir_input == 1: # direction = 1 #elif dir_input == 0: # direction = 0 #print("dir:",direction) joystick.update() speed = 0 direction = int(0) if (joystick.button[0] == True): data = int(sensor_ser.readline().decode()) print("auto mode!") print("data", data) if data < 10: speed = 0 elif data >= 10: speed = 255 elif (joystick.button[1] == False): speed = int(joystick.axis[1])
analogs = [x for x in g.actions if g.actions[x][1] == Joystick.ANALOG] buttons = [x for x in g.actions if g.actions[x][1] == Joystick.BUTTON] print("Press ENTER to begin test") raw_input() for i in range(3, 0, -1): print(i) time.sleep(1) print("Testing analog actions:") for a in analogs: print(a) for i in range(0, 256, 4): g.update({a: i}) time.sleep(0.02) for b in buttons: print(b) g.update({b: 1}) time.sleep(0.5) for b in buttons: for c in buttons: if c != b: print(b + " + " + c) g.update({b: 1, c: 1}) time.sleep(0.5) print("Reset")
class Input: def __init__(self, device: Device) -> None: self.device = device self.type = device.type self.binds: Dict[str, Callable] = dict() if device.type == "keyboard": self.keyboard = Keyboard() for action, how in device.actions.items(): if type(how) is str: self.keyboard.bind(Input.str_to_key(how), action) elif type(how) is list and "repeat" in how: self.keyboard.bind(Input.str_to_key(how[0]), action, True) else: raise Exception("invalid binding: {}".format(how)) elif device.type == "joystick": self.joystick = Joystick(device.joystick) for action, how in device.actions.items(): invalid = True if type(how) is int: self.joystick.bind_button(how, action) invalid = False elif type(how) is list: if ( len(how) >= 3 and type(how[0]) is str and how[0] == "axis" ): repeat = "repeat" in how axis = int(how[1]) direction = 0 if how[2] in ("+", "-"): if how[2] == "-": direction = -1 elif how[2] == "+": direction = 1 invalid = False self.joystick.bind_axis( axis, direction, action, repeat ) if invalid: raise Exception("invalid binding: {}".format(how)) def bind(self, binds: Dict[str, Callable]) -> None: self.binds = binds def update(self) -> List[str]: if self.type == "dummy": return [] actions: List[str] = list() if self.type == "keyboard": actions = self.keyboard.update() elif self.type == "joystick": actions = self.joystick.update() for action in actions: if action in self.binds: self.binds[action]() return actions @staticmethod def str_to_key(key: str) -> int: keys = { "esc": pg.K_ESCAPE, "enter": pg.K_RETURN, "up": pg.K_UP, "down": pg.K_DOWN, "right": pg.K_RIGHT, "left": pg.K_LEFT, "x": pg.K_x, "z": pg.K_z, "lshift": pg.K_LSHIFT, "space": pg.K_SPACE, "c": pg.K_c, } if key in keys: return keys[key] else: raise Exception("invalid key: {}".format(key))