def __init__(self, args=None): super().__init__(target=self.key_listener) # queue for getting info from 1st process self.q1, self.q2, self.joy_n = args # Set up the GUI part self.joy = BotController.VXBOX_Device(self.joy_n) self.running = True self.playing = False self.hks_on = False self.rest = True # ignore action interval (set to False at end of loops) self.iai = False #holds all hotkeys self.hotkeys = [] self.hk_names = [] #holds all action strings self.strings = [] self.pending = [] # variable for making sure client process has been informed self.client_informed = False
def processIncoming(self): # unpack data from queue # may need to find a more robust way of doing this. Test for speed #begin = time.time() while self.q1.qsize(): try: info = self.q1.get(0) if info is not None: # True or False try: self.cfg = info['cfg'] except Exception as e: pass #print(e) try: self.joycfg = info['joycfg'] except Exception as e: pass try: self.playing = info['playing'] if self.playing == True: self.rest = False #print"self.playing = True") #if self.strings == []: self.strings = info['actlist'] else: self.rest = True #print"self.playing = False") self.strings = [] self.client_informed = True self.pending = [] except Exception as e: pass #print("PLAYING EXCEPTION: ", e) try: n = int(info['joy']) #print("joy number: ", n) if n != self.joy_n: self.joy = BotController.VXBOX_Device(n) except Exception as e: pass try: self.settings = info['settings'] self.fps = int(self.settings['fps']) self.start_delay_t = float( self.settings['start delay']) self.act_int_t = float(self.settings['ADI']) self.defaultdir = self.settings['default direction'] except Exception as e: #print("SETTINGS: ", e) pass try: self.custom_delays = info['delay tuners'] #print"new custom delays: ", self.custom_delays) self.fps = int(self.custom_delays['fps']) self.act_int_t = float(self.custom_delays['ADI']) print("action interval: {}; custom delays: {}".format( self.act_int_t, self.custom_delays)) except Exception as e: #print("custom delay error: ", e) pass try: self.hks_on = info['hks on'] except Exception as e: pass # print("HKS ON: ", e) try: self.facing = info['facing'] except Exception as e: #print("FACING: ", e) pass try: if info['hks on'] == True: # throw error if bus driver doesn't exist self.check_for_vbus() # turn on controller self.joy.TurnOn() for k, v in self.cfg.items(): hk = v['Hotkey'] try: string = eval(v['String']) except: string = v['String'] notation = v['Notation'] if hk not in [ "None", "'None'" ] and string not in [ "None", "'None'" ] and hk != None and string != None and hk not in self.hk_names: try: key = keyboard.add_hotkey( str(hk), self.perform_hk, args=(notation, string)) #print"appending {} to list".format(hk)) self.hk_names.append(hk) self.hotkeys.append(key) except: pass if 'play hotkey' not in self.hk_names: print("play hotkey added") v = self.settings['play hotkey'] key = keyboard.add_hotkey( str(v), self.toggle_play) self.hk_names.append('play hotkey') self.hotkeys.append(key) if 'switch sides hotkey' not in self.hk_names: v = self.settings['switch sides hotkey'] key = keyboard.add_hotkey( str(v), self.switch_sides) self.hk_names.append('switch sides hotkey') self.hotkeys.append(key) # if 'increase tuner hotkey' not in self.hk_names: # v = self.settings['increase tuner hotkey'] # #print'increase tuner appended') # key = keyboard.add_hotkey(str(v), self.increase_tuner) # self.hk_names.append('increase tuner hotkey') # self.hotkeys.append(key) # # if 'decrease tuner hotkey' not in self.hk_names: # v = self.settings['decrease tuner hotkey'] # #print'decrease tuner appended') # key = keyboard.add_hotkey(str(v), self.decrease_tuner) # self.hk_names.append('decrease tuner hotkey') # self.hotkeys.append(key) # # if 'next tuner hotkey' not in self.hk_names: # v = self.settings['next tuner hotkey'] # #print'increase adi hk appended') # key = keyboard.add_hotkey(str(v), self.next_tuner) # self.hk_names.append('next tuner hotkey') # self.hotkeys.append(key) # # if 'previous tuner hotkey' not in self.hk_names: # v = self.settings['previous tuner hotkey'] # #print'previous tuner hk appended') # key = keyboard.add_hotkey(str(v), self.prev_tuner) # self.hk_names.append('previous tuner hotkey') # self.hotkeys.append(key) for k, v in self.joycfg.items(): hk = v['Hotkey'] try: string = eval(v['String']) except: string = v['String'] notation = v['Notation'] if hk not in [ "None", "'None'" ] and string not in [ "None", "'None'" ] and hk != None and string != None and hk not in self.hk_names: try: key = keyboard.add_hotkey( str(hk), self.perform_hk, args=(notation, string)) self.hk_names.append(hk) self.hotkeys.append(key) except: pass else: #print"hks off") # turn off controller self.joy.TurnOff() # unbind hotkeys for k in self.hotkeys: try: keyboard.remove_hotkey(k) except: pass # empty containers self.strings = [] self.hotkeys = [] self.hk_names = [] self.pending = [] except Exception as e: print("HKS: ", e) #pass except Exception as e: msg = "{}: {}".format(type(e).__name__, e.args)
def useLocalController(self, devPath): self.bot = BotController() self.bot.connect(devPath) self.bot.start() self.bot.enterFullMode()
print "Usage: \ \n\tpyircbot.py [COMMAND] [FILE] [SERVER] [CHANNEL] [BOT NICK] [PASSWORD] \ \n\n\tCOMMAND: \ \n\t\t-h, --help\t\tHelp you are seeing right now \ \n\t\t-t, --testconfig\tTests [FILE] for being valid config file.\n\t\t\t\t\tIf no [FILE] specified use default (pyircbot.config) \ \n\n\tExample of usage: \ \n\n\tpython pyircbot.py irc.freenode.com:6667 \"#python\" ircbot secretpass \ \n\tConnects to irc.freenode.com at port 6667, join channel \"#python\" as ircbot and identify with password secretpass" sys.exit() # Runtime argument -t or --testconfig runs config file check if len(sys.argv) > 1 and (sys.argv[1] == "-t" or sys.argv[1] == "--testconfig"): # Second argument after -t or --testconfig should be config file path # if there's second argument parse it as config file, if not use default config file if len(sys.argv) == 3: Bot = BotController.BotController(sys.argv[2]) else: Bot = BotController.BotController() # Print config generated from config file print Bot.TestConfigurationFile() sys.exit() # There should be at least 4 and no more than 5 arguments to run the bot properly if len(sys.argv) < 4 and len(sys.argv) > 5: print "Invalid usage! Run script with flag -h or --help to see usage information." sys.exit() # Create Bot controller object Bot = BotController.BotController()
class BotNavigator: def __init__(self): self.bot = None self.lastBotCommand = None self.angularVelocity = 50 self.linearVelocity = 200 self.isBraked = False self.keyMap = { 'w':('self.bot.driveStraight', '( self.linearVelocity, )'), 's':('self.bot.driveStraight', '( -self.linearVelocity, )'), 'a':('self.bot.rotate', '( self.angularVelocity, )'), 'd':('self.bot.rotate', '( -self.angularVelocity, )'), 'q':('self.terminate', None), ' ':('self.toggleState', None) } def useRemoteController(self, uri): self.bot = xmlrpclib.ServerProxy(uri, allow_none=True) def useLocalController(self, devPath): self.bot = BotController() self.bot.connect(devPath) self.bot.start() self.bot.enterFullMode() def isBotCommand(self, cmdStr): return ( cmdStr.find('self.bot.')==0 ) def execCommand(self, funcName, argExp): cmd = eval(funcName) if argExp==None: cmd() else: args = eval(argExp) cmd(*args) if self.isBotCommand(funcName): self.isBraked = False self.lastBotCommand = (funcName, argExp) def terminate(self): self.isRunning = False def toggleState(self): if(self.lastBotCommand == None): return self.isBraked = not self.isBraked if (self.isBraked): self.bot.brake() else: self.execCommand(*(self.lastBotCommand)) def navigate(self): self.isRunning = True while(self.isRunning): ch = getch() try: cmdTuple = self.keyMap[ch] self.execCommand(*cmdTuple) except KeyError: pass self.bot.brake()