except: print("BAD RIGHT COLOR: Port 1") healthy = False try: touch = TouchSensor(TOUCHSENSOR_PORT) except: print("BAD TOUCH: Port 4") healthy = False try: gyro = ShivaGyro(GYROSENSOR_PORT) except: print("BAD GYRO: Port 2") healthy = False sleep(0.5) console.reset_console() # Attachment motor direction CLK_WISE = 'clock_wise' # positive speed ANTI_CLK_WISE = 'anti_clck_wise' # negative speed # Create objects for the ev3 motors and sensors. Only one object will be created for each physical object and used in every program # LARGEMOTORS USED FOR WHEELS # Create object functions for basic movements for wheel pair blocks steer_pair = Shiva_MoveSteering(LARGE_MOTOR_LEFT_PORT, LARGE_MOTOR_RIGHT_PORT) steer_pair.set_polarity(LargeMotor.POLARITY_INVERSED) tank_pair = Shiva_MoveTank(LARGE_MOTOR_LEFT_PORT, LARGE_MOTOR_RIGHT_PORT) tank_pair.set_polarity(LargeMotor.POLARITY_INVERSED)
def display_menu(self, start_page=0, before_run_function=None, after_run_function=None, skip_to_next_page=True): """ Console Menu that accepts choices and corresponding functions to call. The user must press the same button twice: once to see their choice highlited, a second time to confirm and run the function. The EV3 LEDs show each state change: Green = Ready for button, Amber = Ready for second button, Red = Running Parameters: - `choices` a dictionary of tuples "button-name": ("function-name", function-to-call) NOTE: Don't call functions with parentheses, unless preceded by lambda: to defer the call - `before_run_function` when not None, call this function before each function run, passed with function-name - `after_run_function` when not None, call this function after each function run, passed with function-name """ self.current_page = start_page console = Console() leds = Leds() button = Button() leds.all_off() leds.set_color("LEFT", "GREEN") leds.set_color("RIGHT", "GREEN") menu_positions = self.get_menu_positions(console) last = None # the last choice--initialize to None self.menu_tone() self.debug("Starting Menu") while True: # display the menu of choices, but show the last choice in inverse console.reset_console() self.debug("Reset the display screen") console.set_font('Lat15-TerminusBold24x12.psf.gz', True) # store the currently selected menu page menu_page = self.menu_pages[self.current_page] # store the currently selected menu items menu_options_on_page = menu_page.items() for btn, (name, _) in menu_options_on_page: align, col, row = menu_positions[btn] console.text_at(name, col, row, inverse=(btn == last), alignment=align) self.debug("Waiting for button press...") pressed = self.wait_for_button_press(button) self.debug("Registered button press: {}".format(pressed)) # get the choice for the button pressed if pressed in menu_page: if last == pressed: # was same button pressed? console.reset_console() leds.set_color("LEFT", "RED") leds.set_color("RIGHT", "RED") # call the user's subroutine to run the function, but catch any errors try: name, run_function = menu_page[pressed] if before_run_function is not None: self.debug('Running before function') before_run_function(name) self.press_tone() type_of_run_function = type(run_function) self.debug("Type of run_function: {}".format(type_of_run_function)) if isinstance(run_function, str): self.debug("Running {}".format(run_function)) if run_function == 'next': self.debug("About to call next") self.next() elif run_function =='back': self.debug("About to call back") self.back() elif callable(run_function): run_function() except Exception as e: print("**** Exception when running") raise(e) finally: if after_run_function is not None: after_run_function(name) last = None leds.set_color("LEFT", "GREEN") leds.set_color("RIGHT", "GREEN") else: # different button pressed last = pressed leds.set_color("LEFT", "AMBER") leds.set_color("RIGHT", "AMBER")
def menu(choices, before_run_function=None, after_run_function=None): """ Console Menu that accepts choices and corresponding functions to call. The user must press the same button twice: once to see their choice highlited, a second time to confirm and run the function. The EV3 LEDs show each state change: Green = Ready for button, Amber = Ready for second button, Red = Running Parameters: - `choices` a dictionary of tuples "button-name": ("mission-name", function-to-call) Example: choices = { # "button-name": ("mission-name", function-to-call) # or "button-name": ("mission-name", lambda: call(x, y, z)) "enter": ("CAL", lambda: auto_calibrate(robot, 1.0)), "up": ("MI2", fmission2), "right": ("MI3", fmission3), "down": ("MI4", fmission4), "left": ("MI5", fmission5) } where fmission2, fmission3 are functions; note don't call them with parentheses, unless preceded by lambda: to defer the call - `before_run_function` when not None, call this function before each mission run, passed with mission-name - `after_run_function` when not None, call this function after each mission run, passed with mission-name """ console = Console() leds = Leds() button = Button() leds.all_off() leds.set_color("LEFT", "GREEN") leds.set_color("RIGHT", "GREEN") menu_positions = get_positions(console) last = None # the last choice--initialize to None while True: # display the menu of choices, but show the last choice in inverse console.reset_console() for btn, (name, _) in choices.items(): align, col, row = menu_positions[btn] console.text_at(name, col, row, inverse=(btn == last), alignment=align) pressed = wait_for_button_press(button) # get the choice for the button pressed if pressed in choices: if last == pressed: # was same button pressed? console.reset_console() leds.set_color("LEFT", "RED") leds.set_color("RIGHT", "RED") # call the user's subroutine to run the mission, but catch any errors try: name, mission_function = choices[pressed] if before_run_function is not None: before_run_function(name) mission_function() except Exception as ex: print("**** Exception when running") print(ex) finally: if after_run_function is not None: after_run_function(name) last = None leds.set_color("LEFT", "GREEN") leds.set_color("RIGHT", "GREEN") else: # different button pressed last = pressed leds.set_color("LEFT", "AMBER") leds.set_color("RIGHT", "AMBER")
def menu(choices, before_run_function=None, after_run_function=None, skip_to_next_page=True): """ Console Menu that accepts choices and corresponding functions to call. The user must press the same button twice: once to see their choice highlited, a second time to confirm and run the function. The EV3 LEDs show each state change: Green = Ready for button, Amber = Ready for second button, Red = Running Parameters: - `choices` a dictionary of tuples "button-name": ("mission-name", function-to-call) NOTE: Don't call functions with parentheses, unless preceded by lambda: to defer the call - `before_run_function` when not None, call this function before each mission run, passed with mission-name - `after_run_function` when not None, call this function after each mission run, passed with mission-name """ global proc console = Console() leds = Leds() button = Button() leds.all_off() leds.set_color("LEFT", "GREEN") leds.set_color("RIGHT", "GREEN") menu_positions = get_positions(console) last = None # the last choice--initialize to None while True: # display the menu of choices, but show the last choice in inverse console.reset_console() for btn, (name, _) in choices.items(): align, col, row = menu_positions[btn] console.text_at(name, col, row, inverse=(btn == last), alignment=align) pressed = wait_for_button_press(button) # get the choice for the button pressed if pressed in choices: if last == pressed: # was same button pressed? console.reset_console() leds.set_color("LEFT", "RED") leds.set_color("RIGHT", "RED") # call the user's subroutine to run the mission, but catch any errors try: name, mission_function = choices[pressed] if before_run_function is not None: before_run_function(name) if name in ('NEXT', 'BACK', 'OFF'): mission_function() else: # launch a sub process so it could be canceled with the enter button # store the subprocess in self to reference in the stop function proc = Process(target=mission_function) debug('Starting {}'.format(name)) proc.start() debug('Just started {} in proc {}'.format(name, proc.pid)) sleep(1) proc.terminate() # TODO: Need to figure out when to call self.proc.join except Exception as e: print("**** Exception when running") debug('Exception when running {}'.format(e)) finally: if after_run_function is not None: after_run_function(name) last = None leds.set_color("LEFT", "GREEN") leds.set_color("RIGHT", "GREEN") else: # different button pressed last = pressed leds.set_color("LEFT", "AMBER") leds.set_color("RIGHT", "AMBER")