class OP_Pi: #Main volume volume = 0 #State of the device 0 - mixerview 1 - trackview state = 1 last_action = "" bpm = 128 def __init__(self): #General parameters self.start_input_thread() self.sm = ScreenManager(self) self.start_client() track_letters = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I') self.tracks = [Track(letter) for letter in track_letters] self.default_loop() def default_loop(self): while True: self.check_queue() self.sm.refresh_screen() def start_input_thread(self): self.input_queue = Queue() im = InputManager() input_thread = Process(target=im.input_loop, args=(self.input_queue, ), daemon=True) input_thread.start() def start_client(self): self.max_synths = 2 self.active_synthn = 0 #OSC client self.client = udp_client.SimpleUDPClient("127.0.0.1", 57120) #Synths array self.synths = [Synth(self.client, i) for i in range(self.max_synths)] def check_queue(self): while (not self.input_queue.empty()): action = self.input_queue.get() self.last_action = str(action[0]) + " | " + str(action[1]) print("OP_Pi | Action recieved: {} {}".format( action[0], action[1])) if (action[0] == ACTION_VOLUME): self.volume = action[1] elif (action[0] == ACTION_KEYBOARD_PRESS): self.synths[self.active_synthn].osc_note_on(action[1]) elif (action[0] == ACTION_KEYBOARD_RELEASE): self.synths[self.active_synthn].osc_note_off(action[1]) elif (action[0] == ACTION_BUTTON): if (action[1] == 4): self.state = 0 elif (action[1] == 5): self.state = 1 else: print("OP_Pi ERROR unrecognized action")
def __init__(self): #General parameters self.start_input_thread() self.sm = ScreenManager(self) self.start_client() track_letters = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I') self.tracks = [Track(letter) for letter in track_letters] self.default_loop()
class MiraScreenHandler(): def __init__(self): self.sm = ScreenManager() rospy.init_node('mira_screen', anonymous=True) rospy.Subscriber("/mira_screen/input", Int8, self.callback) rospy.Subscriber('/mira_screen/exit', Int8, self.exit_callback) rospy.spin() def exit_callback(self, data): if data.data == EXIT_SIGNAL: rospy.loginfo('Thread has exited...') rospy.loginfo('Shutting down ROS...') rospy.signal_shutdown('[Mira Screen] Exit signal received') def callback(self, data): # rospy.loginfo(rospy.get_caller_id() + "Changing emotion to: %s", data.data) self.sm.change_to_emotion(data.data)
def drive(host, port, gpu_number, path, show_screen, resolution, noise_type, config_path, type_of_driver, experiment_name, city_name, game, drivers_name): print "port:", port use_planner = False screen_manager = ScreenManager() if game == "Carla": from carla_recorder import Recorder if type_of_driver == "Human": from carla_human import CarlaHuman driver = CarlaHuman( use_planner, 'drive_interfaces/carla_interface/' + city_name + '.txt', 'drive_interfaces/carla_interface/' + city_name + '.png', augment_left_right=False) else: from carla_machine import CarlaMachine driver = CarlaMachine("0",experiment_name,use_planner,'drive_interfaces/carla_interface/' + city_name + '.txt',\ 'drive_interfaces/carla_interface/' + city_name + '.png',augment_left_right=False) '''if drive_config.interface == "VirtualElektra": from carla_recorder import Recorder if drive_config.type_of_driver == "Human": from virtual_elektra_human import VirtualElektraHuman driver = VirtualElektraHuman(drive_config) else: from virtual_elektra_carla_machine import VirtualElektraMachine driver = VirtualElektraMachine("0",experiment_name,drive_config,memory_use)''' noiser = Noiser(noise_type) print host print port driver.start(host, port, config_path, resolution) first_time = True if show_screen: screen_manager.start_screen(resolution, 3, 2) folder_name = str(datetime.datetime.today( ).day) + '_' + 'Carla_' + type_of_driver + '_' + experiment_name folder_name += '_' + str(get_latest_file_number(path, folder_name)) recorder = Recorder(path + folder_name + '/', 88, 200) #Note: resolution size is 400,300. but we give input to network 200,100 by cropping it. direction = 2 old_speed = 0 #the speed we start the car with iteration = 0 try: while direction != -1: #which never happens capture_time = time.time() direction_time = time.time() rewards, image = driver.get_sensor_data() for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop recording = driver.get_recording() action, new_speed, human_intervention = driver.compute_action( old_speed, rewards, image ) #passing rewards so that finally carla speed = computed speed #depending on driver being human or machine, new_speed can be the one given by driver or the network resp. action_noisy, drifting_time, will_drift = noiser.compute_noise( action) if recording: recorder.record(image, rewards, action, action_noisy, human_intervention) if show_screen: if game == "Carla": #print len(image) screen_manager.plot_driving_interface(capture_time,np.copy(image),\ action,action_noisy,recording and (drifting_time == 0.0 or will_drift),\ drifting_time,will_drift,rewards.speed,new_speed,0,0,0,type_of_driver, driver.continous_steer, human_intervention) # else: dist_to_goal = math.sqrt( (rewards.goal[0] - rewards.position[0]) * (rewards.goal[0] - rewards.position[0]) + (rewards.goal[1] - rewards.position[1]) * (rewards.goal[1] - rewards.position[1])) image = image[:, :, ::-1] screen_manager.plot_driving_interface( capture_time,np.copy(image), action,action_noisy,\ rewards.direction,recording and (drifting_time == 0.0 or will_drift),drifting_time,will_drift\ ,rewards.speed,0,0,None,rewards.reseted,driver.get_number_completions(),dist_to_goal,0) # iteration += 1 old_speed = new_speed driver.act(action_noisy) except: traceback.print_exc() finally: #driver.write_performance_file(path,folder_name,iteration) pygame.quit() if type_of_driver == "Machine": print "Machine:", driver.machine_driving_count print "Human:", driver.human_driving_count autonomy = (float(driver.machine_driving_count) / float(driver.machine_driving_count + driver.human_driving_count)) * 100 print("Autonomy: {0:.2f}%".format(autonomy)) print "Machine checkpoint score:", driver.checkpoint_score driver.tester.plot_map()
type=str, default="ElektraData1_video", help='Dataset/video clip name') args = parser.parse_args() dataset = args.dataset first_time = True count = 0 steering_pred = [] steering_gt = [] positions_to_test = range(7, 15) #total hdf5 files path = '../Desktop/VirtualElektraData2_W1/SeqTrain/' screen = ScreenManager() image_queue = deque() speed_list = [] steer_list = [] noisy_steer_list = [] actions_queue = deque() screen.start_screen([200, 88], 1, 2) images = np.array([200, 88, 3]) actions = Control() for h_num in positions_to_test:
first_time = True count = 0 steering_pred = [] steering_gt = [] step_size = 1 # initial_positions =[20,25,48,68,79,105,108,120,130] # positions_to_test = [] # for i in initial_positions: # positions_to_test += range(i-1,i+2) positions_to_test = range(0, len( [name for name in os.listdir(path) if os.path.isfile(os.path.join(path, name))])) #positions_to_test = range(10 * 3, 33 * 3) screen = ScreenManager() image_queue = deque() actions_queue = deque() # Start a screen to show everything. The way we work is that we do IMAGES x Sensor. # But maybe a more arbitrary configuration may be useful screen.start_screen([resolution[0], resolution[1]], [1, 1], 2) ts = [] images = [np.zeros([resolution[1], resolution[0], 3])] * sensors['RGB'] labels = [np.zeros([resolution[1], resolution[0], 1])] * sensors['labels'] depths = [np.zeros([resolution[1], resolution[0], 3])] * sensors['depth'] steer_gt_order = [0] * 3
print(dialPins) print(dials) for dial in sorted(dials.items()): print(dial[0]) inputs.add_input(dialPins[counter], list(dials[dial[0]]), dial[0], 1024) counter += 1 button = ButtonManager(go_pin=18, reset_pin=23, go_ahead_light=12, stop_light=17, reset_time=0, go_sound='/home/pi/pox/client/go.wav', reset_sound='/home/pi/pox/client/reset.wav') screen = ScreenManager("Enter Phone #\nand Pull Lever") # Create new threads thread1 = InputThread(1, "Input Manager Thread", 1, inputs) #thread2 = ScreenThread(2, "Screen Manager Thread", ScreenManager()) # Start new Threads thread1.start() #thread2.start() if len(sys.argv) == 2 and sys.argv[1] == 'debug': while (1): print(thread1.get_readings()) while (1): check = button.check() # Button hasn't been pressed and hasn't been reset if check == 0:
dataset = args.dataset first_time = True count = 0 steering_pred = [] steering_gt = [] positions_to_test = list(range(0, 28)) # positions_to_test = [93,104,170,173,229,245,283,397,413,425,565,581,591] # positions_to_test = range(0,660) # positions_to_test = [617,618,619,620,622,623,624,636,637,638,639] # positions_to_test = [637,638] # positions_to_test = [55,108,109,353,410,411,426,441,442] # positions_to_test = [656,657,675,676,854,855,859,860,861,902] path = '/media/matthias/7E0CF8640CF818BB/Github/Desktop/2_SegData_1/' screen = ScreenManager() image_queue = deque() speed_list = [] actions_queue = deque() speed_list_noise = [] just_noise = [] screen.start_screen([800, 600], 1, 1) ts = [] images = [np.array([800, 600, 3]), np.array([800, 600, 3]), np.array([800, 600, 3])] # 200x88 actions = [Control(), Control(), Control()] actions_noise = [Control(), Control(), Control()] for h_num in positions_to_test:
class MainApp(App): sm = ScreenManager(transition=FadeTransition()) def build(self): return self.sm
from screen_manager import ScreenManager m = ScreenManager() m.run()
import pygame from screen_manager import ScreenManager pygame.init() resolution = (1280, 720) display = pygame.display.set_mode(resolution) clock = pygame.time.Clock() running = True manager = ScreenManager(resolution) while running: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: running = False # Force quit the game display.fill((0, 0, 0)) # Black out the screen for a new flip manager.update(display, events) if not manager.active: running = False # quit the game if ScreenManager ended. # Render the screen pygame.display.flip() clock.tick(30) # Lock at 30FPS
def setup(self): logger.info("Starting setup") signal(SIGTERM, signal_term_handler) # Python game ###################### logger.info("Setting pygame") pygame_init_done = False while not pygame_init_done: try: pygame.init() pygame_init_done = True except: logger.debug("Pygame init failed") pygame_init_done = False time.sleep(5) pygame.mouse.set_visible(False) # Hax for freezing signal(SIGALRM, alarm_handler) alarm(1) try: # Set screen size size = width, height = config.resolution self.screen = pygame.display.set_mode(size) alarm(0) except Alarm: logger.debug("Keyboard interrupt?") raise KeyboardInterrupt # Hax end logger.info("Display driver: %s" % pygame.display.get_driver()) # Player control ############### logger.info("Setting player control") self.pc = PlayerControl() logger.debug("Player control set") # Screen manager ############### logger.info("Setting screen manager") self.sm = ScreenManager(path, self.pc) logger.debug("Screen manager set") # LIRC lircrcfile = path + config.lircrcfile self.lirc_enabled = False if os.path.isfile(lircrcfile): try: self.lirc_sockid = lirc.init("pitft-playerui", lircrcfile, blocking=False) self.lirc_enabled = True except Exception as e: logger.error(e) self.lirc_enabled = False # Mouse variables self.clicktime = datetime.datetime.now() self.longpress_time = timedelta(milliseconds=300) self.click_filtertime = datetime.datetime.now() self.click_filterdelta = timedelta(milliseconds=10) self.scroll_threshold = (20, 20) self.start_pos = 0, 0 self.mouse_scroll = "" self.mousebutton_down = False self.pos = 0 # Smooth scrolling variables self.smoothscroll = False self.smoothscroll_direction = 0, 0 self.smoothscroll_directions_index = 0 self.smoothscroll_direction_samples = 10 self.smoothscroll_directions = [0 ] * self.smoothscroll_direction_samples self.smoothscroll_factor = 0.9 self.smoothscroll_timedelta = timedelta(milliseconds=1) self.smoothscroll_time = datetime.datetime.now() # Times in milliseconds self.screen_refreshtime = 16.67 self.player_refreshtime = 200 #Backlight self.screen_timer = 0.0 self.backlight = False self.update_screen_timeout(True) logger.debug("Setup done")
class PitftDaemon(Daemon): # Setup Python game and Screen manager def setup(self): logger.info("Starting setup") signal(SIGTERM, signal_term_handler) # Python game ###################### logger.info("Setting pygame") pygame_init_done = False while not pygame_init_done: try: pygame.init() pygame_init_done = True except: logger.debug("Pygame init failed") pygame_init_done = False time.sleep(5) pygame.mouse.set_visible(False) # Hax for freezing signal(SIGALRM, alarm_handler) alarm(1) try: # Set screen size size = width, height = config.resolution self.screen = pygame.display.set_mode(size) alarm(0) except Alarm: logger.debug("Keyboard interrupt?") raise KeyboardInterrupt # Hax end logger.info("Display driver: %s" % pygame.display.get_driver()) # Player control ############### logger.info("Setting player control") self.pc = PlayerControl() logger.debug("Player control set") # Screen manager ############### logger.info("Setting screen manager") self.sm = ScreenManager(path, self.pc) logger.debug("Screen manager set") # LIRC lircrcfile = path + config.lircrcfile self.lirc_enabled = False if os.path.isfile(lircrcfile): try: self.lirc_sockid = lirc.init("pitft-playerui", lircrcfile, blocking=False) self.lirc_enabled = True except Exception as e: logger.error(e) self.lirc_enabled = False # Mouse variables self.clicktime = datetime.datetime.now() self.longpress_time = timedelta(milliseconds=300) self.click_filtertime = datetime.datetime.now() self.click_filterdelta = timedelta(milliseconds=10) self.scroll_threshold = (20, 20) self.start_pos = 0, 0 self.mouse_scroll = "" self.mousebutton_down = False self.pos = 0 # Smooth scrolling variables self.smoothscroll = False self.smoothscroll_direction = 0, 0 self.smoothscroll_directions_index = 0 self.smoothscroll_direction_samples = 10 self.smoothscroll_directions = [0 ] * self.smoothscroll_direction_samples self.smoothscroll_factor = 0.9 self.smoothscroll_timedelta = timedelta(milliseconds=1) self.smoothscroll_time = datetime.datetime.now() # Times in milliseconds self.screen_refreshtime = 16.67 self.player_refreshtime = 200 #Backlight self.screen_timer = 0.0 self.backlight = False self.update_screen_timeout(True) logger.debug("Setup done") def shutdown(self): pass # Main loop def run(self): self.setup() drawtime = datetime.datetime.now() refreshtime = datetime.datetime.now() while 1: updated = False # Check mouse and LIRC events try: active = self.read_mouse() if self.lirc_enabled: active = active | self.read_lirc() except Exception as e: logger.error(e) try: # Refresh info if refreshtime < datetime.datetime.now(): refreshtime = datetime.datetime.now() + timedelta( milliseconds=self.player_refreshtime) # Refresh information from players ret, updated = self.pc.refresh() active = active | ret # Update screen if updated: self.sm.refresh() except Exception as e: logger.error(e) try: # Update screen timeout, if there was any activity if config.screen_timeout > 0: self.update_screen_timeout(active) except Exception as e: logger.error(e) try: # Draw screen if drawtime < datetime.datetime.now(): drawtime = datetime.datetime.now() + timedelta( milliseconds=self.screen_refreshtime) # Don't draw when display is off if self.backlight: self.sm.render(self.screen) pygame.display.flip() else: time.sleep(0.01) except Exception as e: logger.error(e) def read_mouse(self): direction = 0, 0 userevents = False for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: self.clicktime = datetime.datetime.now() # Filter out if instantly after previous mousebutton up event if self.clicktime > self.click_filtertime: self.pos = self.start_pos = pygame.mouse.get_pos() userevents = True if event.button == 1: if self.smoothscroll: self.scroll(self.start_pos, (0, 0), True) self.smoothscroll = False self.smoothscroll_directions_index = 0 self.smoothscroll_directions = [ 0 ] * self.smoothscroll_direction_samples self.smoothscroll_direction = 0, 0 # Instant click when backlight is off to wake if not self.backlight: self.mousebutton_down = False else: self.mousebutton_down = True # Scroll wheel elif event.button == 4: self.scroll(self.start_pos, (0, 30), True) elif event.button == 5: self.scroll(self.start_pos, (0, -30), True) elif event.type == pygame.MOUSEMOTION and self.mousebutton_down: userevents = True pos = pygame.mouse.get_pos() direction = (pos[0] - self.pos[0], pos[1] - self.pos[1]) if not self.mouse_scroll: # Start scrolling: Lock direction if abs(direction[0]) >= self.scroll_threshold[0]: self.mouse_scroll = "x" elif abs(direction[1]) >= self.scroll_threshold[1]: self.mouse_scroll = "y" # Scrolling already, update offset if self.mouse_scroll == "x": direction = direction[0], 0 elif self.mouse_scroll == "y": direction = 0, direction[1] else: direction = 0, 0 if self.mouse_scroll: self.smoothscroll = self.scroll(self.start_pos, direction) # Save directions from latest samples for smooth scrolling - Direction is always Y self.smoothscroll_directions_index = self.smoothscroll_directions_index + 1 if self.smoothscroll_directions_index > self.smoothscroll_direction_samples - 1: self.smoothscroll_directions_index = 0 self.smoothscroll_directions[ self.smoothscroll_directions_index] = direction[1] self.smoothscroll_direction = 0, sum( self.smoothscroll_directions) # Save new position self.pos = pos elif event.type == pygame.MOUSEBUTTONUP and event.button == 1: userevents = True if self.mousebutton_down: # Not a long click or scroll: click if not self.mouse_scroll: self.click(1, self.start_pos) # Scrolling: End right away or start deceleration if allowed else: if self.smoothscroll: self.scroll(self.start_pos, self.smoothscroll_direction) self.smoothscroll_time = datetime.datetime.now( ) + self.smoothscroll_timedelta else: self.scroll(self.start_pos, (0, 0), True) self.mouse_scroll = "" # Clear variables self.mousebutton_down = False # Filter next click, if it happens instantly self.click_filtertime = datetime.datetime.now( ) + self.click_filterdelta # Long press - register second click if self.mousebutton_down and not self.mouse_scroll and not self.smoothscroll: userevents = True if datetime.datetime.now() - self.clicktime > self.longpress_time: self.mousebutton_down = self.click(2, self.start_pos) # Update timers self.clicktime = datetime.datetime.now() # No activity, but smooth scrolling if self.smoothscroll and not self.mousebutton_down: if datetime.datetime.now() > self.smoothscroll_time: userevents = True self.smoothscroll_direction = 0, int( self.smoothscroll_direction[1] * self.smoothscroll_factor) # Decelerated under threshold -> Stop scrolling if abs(self.smoothscroll_direction[1] ) < self.scroll_threshold[1]: self.scroll(self.start_pos, (0, 0), True) self.mouse_scroll = "" self.smoothscroll = False self.smoothscroll_directions_index = 0 self.smoothscroll_directions = [ 0 ] * self.smoothscroll_direction_samples self.smoothscroll_direction = 0, 0 else: # Continue scrolling self.scroll(self.start_pos, self.smoothscroll_direction) self.smoothscroll_time = datetime.datetime.now( ) + self.smoothscroll_timedelta return userevents def click(self, mousebutton, clickpos): self.sm.click(mousebutton, clickpos) def scroll(self, start, direction, end=False): return self.sm.scroll(start, direction, end) def read_lirc(self): commands = lirc.nextcode() if commands: for line in commands: logger.debug("LIRC: %s" % line) try: target, command = line.split() if target == "switch": self.sm.switch_player(command) elif target == "control": self.pc.control_player(command) else: logger.debug("LIRC: Unknown target %s" % target) except Exception as e: logger.error(e) return True return False def set_backlight(self, state): logger.debug("Backlight %s" % state) subprocess.call("echo '" + str(state * 1) + "' > " + config.backlight_sysfs, shell=True) self.backlight = state def update_screen_timeout(self, active): if active: self.screen_timer = datetime.datetime.now() + timedelta( seconds=config.screen_timeout) if not self.backlight: self.set_backlight(True) elif self.screen_timer < datetime.datetime.now() and self.backlight: self.set_backlight(False)
def drive_elektra( experiment_name, drive_config, input_method, name=None, memory_use=1.0, ): driver, recorder = get_instance(drive_config, experiment_name, name, memory_use, input_method) noiser = Noiser(drive_config.noise) #print 'before starting' driver.start() first_time = True if drive_config.show_screen: screen_manager = ScreenManager() screen_manager.start_screen(drive_config.resolution, drive_config.number_screens, drive_config.scale_factor) driver.use_planner = False old_speed = 0 #the speed we start the car with direction = 2 iteration = 0 try: while direction != -1: capture_time = time.time() images = driver.get_sensor_data() for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop recording = driver.get_recording( ) #just booleans, received from joystick driver.get_reset() #just booleans, received from joystick action, new_speed, human_intervention = driver.compute_action( images, old_speed) #rewards.speed #action_noisy,drifting_time,will_drift = noiser.compute_noise(action[drive_config.middle_camera]) action_noisy, drifting_time, will_drift = noiser.compute_noise( action) if recording: print "RECORDING" recorder.record(images, action.speed, action.steer, action_noisy.steer, human_intervention) '''if drive_config.show_screen: if drive_config.interface == "Carla": for i in range(drive_config.number_screens-1): screen_manager.plot_driving_interface( capture_time,np.copy(images[i]),\ action[i],action_noisy,driver.compute_direction((rewards.player_x,rewards.player_y,22),(rewards.ori_x,rewards.ori_y,rewards.ori_z)),recording and (drifting_time == 0.0 or will_drift),\ drifting_time,will_drift,rewards.speed,0,0,i) # else: print "Not supported interface" pass''' '''if drive_config.type_of_driver == "Machine" and drive_config.show_screen and drive_config.plot_vbp: image_vbp =driver.compute_perception_activations(images[drive_config.middle_camera],rewards.speed) screen_manager.plot_image(image_vbp,1)''' iteration += 1 old_speed = new_speed driver.act(action_noisy) except: traceback.print_exc() finally: driver.end() #driver.write_performance_file(path,folder_name,iteration) pygame.quit()
def __init__(self): self.sm = ScreenManager() rospy.init_node('mira_screen', anonymous=True) rospy.Subscriber("/mira_screen/input", Int8, self.callback) rospy.Subscriber('/mira_screen/exit', Int8, self.exit_callback) rospy.spin()
import pyglet from screen_manager import ScreenManager window = pyglet.window.Window(fullscreen=True) screen_manager = ScreenManager(window) @window.event def on_draw(): window.clear() screen_manager.on_draw() pyglet.app.run()
count = 0 steering_pred = [] steering_gt = [] path = '/media/matthias/7E0CF8640CF818BB/Github/Desktop/RSS_W1_w1000_2h_WP/' step_size = 1 showSlow = False h5start = 587 h5end = 1080 # len([name for name in os.listdir(path) if os.path.isfile(os.path.join(path, name))]) # initial_positions =[20,25,48,68,79,105,108,120,130] # positions_to_test = [] # for i in initial_positions: # positions_to_test += range(i-1,i+2) positions_to_test = list(range(h5start, h5end + 1)) screen = ScreenManager() image_queue = deque() actions_queue = deque() # Start a screen to show everything. The way we work is that we do IMAGES x Sensor. # But maybe a more arbitrary configuration may be useful screen.start_screen([resolution[0] * 2, resolution[1] * 2], [sensors['RGB'], 2], 1) ts = [] images = [np.zeros([resolution[1], resolution[0], 3])] * sensors['RGB'] labels = [np.zeros([resolution[1], resolution[0], 1])] * sensors['labels'] depths = [np.zeros([resolution[1], resolution[0], 3])] * sensors['depth']