def _load_joysticks( self, *args ): #Clear existing state graphics for i in xrange( self.num_joysticks ): self.states[i].clear() #Clear current list of joysticks self.joysticks = [] self.states = [] self.active_joysticks = [False] * 4 self.active_players = [False] * 4 #Reinitialize pygame.joystick to update joystick information js.quit() js.init() self.num_joysticks = js.get_count() #Create Joystick instance for each physical joystick for i in xrange( self.num_joysticks ): self.joysticks.append( js.Joystick( i ) ) self.joysticks[i].init() #Draw state information for i in xrange( self.num_joysticks ): self.states.append( ControllerState( i, self.graphics ) ) self.states[i].update(self.active_joysticks[i], self.active_players[i] )
def refresh(self): """ Finds all connected gamepads. Must be called at the start of the run loop if there has been a change. Will uninitialize and then initialize the joystick module """ if self.joystick: self.joystick.quit() self.joystick = None joystick.quit() joystick.init() joyDictList = self.get_all_gamepads() # Must be fetched before initializing the ID, since otherwise we delete that ID afterwards, apparently the objects here are global! self.initialize(clamp(self.joystick_id, 0, maxVal((joystick.get_count() - 1), 0))) self.refreshedGamepad.emit(joyDictList) self.statusChanged.emit(self.joystick.get_init() if self.joystick else False)
def destroy(self): eng.detach_obs(self.on_frame) joystick.quit() pygame.quit() self.joysticks = None JoystickMgrBase.destroy(self)
if not accel: print "No accelerator device found. Exiting..." exit(1) def rotate(orientation): print "Rotate " + orientation os.system('rotate %s &' % orientation) return orientation orientation = rotate("inverted") while True: time.sleep(0.5) for ev in event.get(): if ev.type == QUIT: joystick.quit() pygame.quit() exit(0) if ev.type == JOYAXISMOTION: axis_x = accel.get_axis(0) axis_y = accel.get_axis(1) axis_z = accel.get_axis(2) if (axis_x > 0.25) and (orientation != "left"): orientation=rotate("left") elif (axis_x < -0.2) and (orientation != "right"): orientation=rotate("right") elif (axis_x < 0.3) and (axis_x > -0.2) and (axis_y < -0.1) and (orientation != "normal"): orientation=rotate("normal") elif (axis_x < 0.3) and (axis_x > -0.2) and (axis_y > 0.3) and (orientation != "inverted"): orientation=rotate("inverted")
if loggingEnabled: # csvwriter.writerow(['time'] + ['deltaTimeMs']+['angle'] + ['position'] + ['angleErr'] + ['positionErr'] + ['angleCmd'] + ['positionCmd'] + ['motorCmd']) csvwriter.writerow([ elapsedTime, deltaTime * 1000, angle, position, ANGLE_TARGET, angleErr, positionTargetNow, positionErr, angleCmd, positionCmd, motorCmd, actualMotorCmd ]) # Print output printCount += 1 if printCount >= (PRINT_PERIOD_MS / CONTROL_PERIOD_MS): printCount = 0 print( "\r angle {:+4d} angleErr {:+6.1f} position {:+6d} positionErr {:+6.1f} angleCmd {:+6d} positionCmd {:+6d} motorCmd {:+6d} dt {:.3f}ms stick {:.3f} \r" .format(int(angle), angleErr, int(position), positionErr, int(round(angleCmd)), int(round(positionCmd)), actualMotorCmd, deltaTime * 1000, stickPos), end='') # if we pause like below, state info piles up in serial input buffer # instead loop at max possible rate to get latest state info # time.sleep(CONTROL_PERIOD_MS*.001) # not quite correct since there will be time for execution below # when x hit during loop or other loop exit p.set_motor(0) # turn off motor p.close() joystick.quit() if loggingEnabled: csvfile.close()