"channel": 1, "side": 0, "direction": 1 }, { "channel": 2, "side": 1, "direction": -1 }, { "channel": 3, "side": 1, "direction": 1 }, ] dt = drivetrain.DriveTrain(m, dt_motors) spinner_servo = {"channel": 4, "direction": 1} spinner = spinner.Spinner(m, spinner_servo) tilter_servo = {"channel": 5, "direction": -1} tilter = tilter.Tilter(m, tilter_servo) releaser_servos = {"test1": {"channel": 6}, "test2": {"channel": 7}} releaser = releaser.Releaser(m, releaser_servos) j = xbox.Joystick() # Wrapping the robot loop in a try/finally structure makes sure that the robot stops # moving if your code errors out or the robot loop completes. try:
import camera import linetracker import time import torch import numpy as np from PIL import Image from models import FeatureExtractor, PolicyNetwork from agent import Agent timestep = 1 # Seconds if __name__ == "__main__": print("Initializing objects") dt = drivetrain.DriveTrain() cam = camera.Camera() lt = linetracker.LineTracker() input_shape = (256, 256, 3) # Should be (h, w, c) num_actions = 2 fe_filters = 4 kernel_size = 3 action_range = [[0, 100], [-60, 60]] agent = Agent(input_shape, num_actions, fe_filters, kernel_size, action_range) def robot_train(dt, agent, cam, lt, max_episodes, max_steps): # dt = drivetrain, cam = camera, lt = line tracker episode_rewards = []
def run(self): """ Main Running loop controling bot mode and menu state """ # Tell user how to connect wiimote self.lcd.clear() self.lcd.message('Press 1+2 \n') self.lcd.message('On Wiimote') # Initiate the drivetrain self.drive = drivetrain.DriveTrain(pwm_i2c=0x40) self.wiimote = None try: self.wiimote = Wiimote() except WiimoteException: logging.error("Could not connect to wiimote. please try again") if not self.wiimote: # Tell user how to connect wiimote self.lcd.clear() self.lcd.message('Wiimote \n') self.lcd.message('Not Found' + '\n') # Constantly check wiimote for button presses loop_count = 0 while self.wiimote: buttons_state = self.wiimote.get_buttons() nunchuk_buttons_state = self.wiimote.get_nunchuk_buttons() joystick_state = self.wiimote.get_joystick_state() # logging.info("joystick_state: {0}".format(joystick_state)) # logging.info("button state {0}".format(buttons_state)) # Always show current menu item # logging.info("Menu: " + self.menu[self.menu_state]) if loop_count >= self.lcd_loop_skip: # Reset loop count if over loop_count = 0 self.lcd.clear() if self.shutting_down: # How current menu item on LCD self.lcd.message('Shutting Down Pi' + '\n') else: # How current menu item on LCD self.lcd.message(self.menu[self.menu_state] + '\n') # If challenge is running, show it on line 2 if self.challenge: self.lcd.message('[' + self.challenge_name + ']') # Increment Loop Count loop_count = loop_count + 1 # Test if B button is pressed if joystick_state is None or (buttons_state & cwiid.BTN_B) or ( nunchuk_buttons_state & cwiid.NUNCHUK_BTN_Z): # No nunchuk joystick detected or B or Z button # pressed, must go into neutral for safety logging.info("Neutral") self.set_neutral(self.drive, self.wiimote) else: # Enable motors self.set_drive(self.drive, self.wiimote) if ((buttons_state & cwiid.BTN_A) or (buttons_state & cwiid.BTN_UP) or (buttons_state & cwiid.BTN_DOWN)): # Looking for state change only if not self.menu_button_pressed and (buttons_state & cwiid.BTN_A): # User wants to select a menu item self.menu_item_selected() elif not self.menu_button_pressed and (buttons_state & cwiid.BTN_UP): # Decrement menu index self.menu_state = self.menu_state - 1 if self.menu_state < 0: # Loop back to end of list self.menu_state = len(self.menu) - 1 logging.info("Menu item: {0}".format( self.menu[self.menu_state])) elif not self.menu_button_pressed and (buttons_state & cwiid.BTN_DOWN): # Increment menu index self.menu_state = self.menu_state + 1 if self.menu_state >= len(self.menu): # Loop back to start of list self.menu_state = 0 logging.info("Menu item: {0}".format( self.menu[self.menu_state])) # Only change button state AFTER we have used it self.menu_button_pressed = True else: # No menu buttons pressed self.menu_button_pressed = False time.sleep(0.05)