def __init__(self, cfg, center_x: int, center_y: int): self.wheel_center_x = center_x self.wheel_center_y = center_y + apply_scaling(22.5) self.wheel_distance = apply_scaling(cfg['wheel_settings']['spacing']) self.sprites = None self.movable_sprites = None
def __init__(self, img_cfg, center_x: int, center_y: int): super(ArmLarge, self).__init__(img_cfg['arm_large'], apply_scaling(0.50)) self.center_x = center_x self.center_y = center_y self.sweep_length = apply_scaling(229) / 4 self.rotate_x = center_x self.rotate_y = center_y - self.sweep_length self.rotate(20)
def __init__(self, address: str, img_cfg, robot: Robot, delta_x: int, delta_y: int): super(UltrasonicSensor, self).__init__(address, robot, delta_x, delta_y) self.init_texture(img_cfg['ultrasonic_sensor_top'], 0.20) self.sensor_half_height = apply_scaling(22.5) self.scaling_multiplier = get_config().get_scale() self.eye_offset = apply_scaling(18)
def __init__(self, cfg, color_code: int, depth: int, edge_spacing: float): super(BorderObstacle, self).__init__(color_code) self.screen_width = apply_scaling( cfg['screen_settings']['screen_width']) self.screen_height = apply_scaling( cfg['screen_settings']['screen_height']) self.depth = depth self.edge_spacing = edge_spacing self.top_points = None self.right_points = None self.bottom_points = None self.left_points = None self._calc_points()
def _draw_text_small_sim(self): """ Draw all the text fields. """ center_cs = 'CS: ' + str(self.center_cs_data) left_ts = 'TS right: ' + str(self.right_ts_data) right_ts = 'TS left: ' + str(self.left_ts_data) top_us = 'US: ' + str( int(round(self.front_us_data / self.scaling_multiplier))) + 'mm' message = self.robot_state.next_sound_job() sound = message if message else '-' arcade.draw_text(center_cs, self.text_x, self.screen_height - apply_scaling(80), arcade.color.BLACK_LEATHER_JACKET, 10) arcade.draw_text(left_ts, self.text_x, self.screen_height - apply_scaling(100), arcade.color.BLACK_LEATHER_JACKET, 10) arcade.draw_text(right_ts, self.text_x, self.screen_height - apply_scaling(120), arcade.color.BLACK_LEATHER_JACKET, 10) arcade.draw_text(top_us, self.text_x, self.screen_height - apply_scaling(140), arcade.color.BLACK_LEATHER_JACKET, 10) arcade.draw_text('Sound:', self.text_x, self.screen_height - apply_scaling(160), arcade.color.BLACK_LEATHER_JACKET, 10) arcade.draw_text(sound, self.text_x, self.screen_height - apply_scaling(180), arcade.color.BLACK_LEATHER_JACKET, 10, anchor_y='top') if self.msg_counter != 0: self.msg_counter -= 1 arcade.draw_text(self.falling_msg, self.msg_x, self.screen_height - apply_scaling(100), arcade.color.BLACK_LEATHER_JACKET, 14, anchor_x="center") arcade.draw_text(self.restart_msg, self.msg_x, self.screen_height - apply_scaling(130), arcade.color.BLACK_LEATHER_JACKET, 14, anchor_x="center")
def __init__(self, cfg, center_x: int, center_y: int, orientation: int): super(RobotSmall, self).__init__(cfg, center_x, center_y) img_cfg = cfg['image_paths'] alloc_cfg = cfg['alloc_settings'] address_motor_left = alloc_cfg['motor']['left'] address_motor_right = alloc_cfg['motor']['right'] address_cs_center = alloc_cfg['color_sensor']['center'] address_ts_left = alloc_cfg['touch_sensor']['left'] address_ts_right = alloc_cfg['touch_sensor']['right'] address_us_front = alloc_cfg['ultrasonic_sensor']['front'] self.wheel_distance = apply_scaling(cfg['wheel_settings']['spacing']) self.body = Body(img_cfg, self, 0, apply_scaling(-22.5)) self.left_wheel = Wheel(address_motor_left, img_cfg, self, (self.wheel_distance / -2), 0.01) self.right_wheel = Wheel(address_motor_right, img_cfg, self, (self.wheel_distance / 2), 0.01) self.center_color_sensor = ColorSensor(address_cs_center, img_cfg, self, 0, apply_scaling(81)) self.left_touch_sensor = TouchSensor(address_ts_left, img_cfg, self, apply_scaling(-75), apply_scaling(102), 'left') self.right_touch_sensor = TouchSensor(address_ts_right, img_cfg, self, apply_scaling(75), apply_scaling(102), 'right') self.front_ultrasonic_sensor = UltrasonicSensor(address_us_front, img_cfg, self, 0, apply_scaling(-91.5)) self.left_led = Led(img_cfg, self, apply_scaling(-20), apply_scaling(-55)) self.right_led = Led(img_cfg, self, apply_scaling(20), apply_scaling(-55)) self.movable_sprites = [self.left_wheel, self.right_wheel, self.body, self.center_color_sensor, self.left_touch_sensor, self.right_touch_sensor, self.front_ultrasonic_sensor, self.left_led, self.right_led] self.sprites = self.movable_sprites.copy() if orientation != 0: self._rotate(math.radians(orientation))
def __init__(self, brick_name: str, robot_state: RobotState): cfg = get_config().get_data() large_sim_type = get_config().is_large_sim_type() self.brick_name = brick_name + ':' if brick_name else '' self.pixel_coasting_sub = apply_scaling( cfg['motor_settings']['pixel_coasting_subtraction']) self.degree_coasting_sub = cfg['motor_settings'][ 'degree_coasting_subtraction'] self.frames_per_second = cfg['exec_settings']['frames_per_second'] self.address_us_front = cfg['alloc_settings']['ultrasonic_sensor'][ 'front'] self.address_us_rear = cfg['alloc_settings']['ultrasonic_sensor'][ 'rear'] if large_sim_type else '' self.robot_state = robot_state self.command_processor = MotorCommandProcessor() self.led_cache = {k: None for k in LEDS.values()}
def test_create_jobs_coast_left(self): coasting_sub = apply_scaling(get_config().get_data()['motor_settings']['pixel_coasting_subtraction']) robot_state = RobotState() message_processor = MessageProcessor('left_brick', robot_state) message_processor.process_rotate_command(RotateCommand('ev3-ports:outA', 80, 200, 'coast')) for i in range(75): c, l, r = robot_state.next_motor_jobs() self.assertIsNone(c) self.assertAlmostEqual(l, 0.833, 3) self.assertIsNone(r) ppf = 0.833 - coasting_sub for i in range(2): c, l, r = robot_state.next_motor_jobs() self.assertIsNone(c) self.assertAlmostEqual(l, ppf, 3) self.assertIsNone(r) ppf = max(ppf - coasting_sub, 0) self.assertEqual((None, None, None), robot_state.next_motor_jobs())
def __init__(self, cfg): lake_cfg = cfg['obstacle_settings']['lake_settings'] border_width = apply_scaling(lake_cfg['border_width']) inner_radius = apply_scaling(lake_cfg['inner_radius']) radius = inner_radius + (border_width / 2) edge_spacing = apply_scaling(cfg['screen_settings']['edge_spacing']) border_depth = apply_scaling( cfg['obstacle_settings']['border_settings']['border_depth']) x = apply_scaling(lake_cfg['lake_red_x']) + edge_spacing + border_depth y = apply_scaling(lake_cfg['lake_red_y']) + edge_spacing + border_depth color = eval(lake_cfg['lake_red_color']) super(RedLake, self).__init__(x, y, radius, inner_radius, color, border_width)
def __init__(self, img_cfg, robot: Robot, delta_x: int, delta_y: int): super(Led, self).__init__('', robot, delta_x, delta_y) amber_texture = arcade.load_texture(img_cfg['led_amber'], scale=apply_scaling(0.33)) black_texture = arcade.load_texture(img_cfg['led_black'], scale=apply_scaling(0.33)) green_texture = arcade.load_texture(img_cfg['led_green'], scale=apply_scaling(0.33)) red_texture = arcade.load_texture(img_cfg['led_red'], scale=apply_scaling(0.33)) orange_texture = arcade.load_texture(img_cfg['led_orange'], scale=apply_scaling(0.33)) yellow_texture = arcade.load_texture(img_cfg['led_yellow'], scale=apply_scaling(0.33)) self.textures.append(amber_texture) self.textures.append(black_texture) self.textures.append(green_texture) self.textures.append(red_texture) self.textures.append(orange_texture) self.textures.append(yellow_texture) self.old_texture_index = 1 self.set_texture(1)
def main(): """ Spawns the user thread and creates and starts the simulation. """ parser = argparse.ArgumentParser() parser.add_argument("-V", "--version", action='store_true', help="Show version info") parser.add_argument("-s", "--window_scaling", default=0.65, help="Scaling of the screen, default is 0.65", required=False, type=validate_scale) parser.add_argument( "-t", "--simulation_type", choices=['small', 'large'], default='small', help="Type of the simulation (small or large). Default is small", required=False, type=str) parser.add_argument( "-x", "--robot_position_x", default=200, help="Starting position x-coordinate of the robot, default is 200", required=False, type=validate_xy) parser.add_argument( "-y", "--robot_position_y", default=300, help="Starting position y-coordinate of the robot, default is 300", required=False, type=validate_xy) parser.add_argument("-o", "--robot_orientation", default=0, help="Starting orientation the robot, default is 0", required=False, type=int) parser.add_argument( "-c", "--connection_order_first", choices=['left', 'right'], default='left', help= "Side of the first brick to connect to the simulator, default is 'left'", required=False, type=str) parser.add_argument( "-2", "--show-on-second-monitor", action='store_true', help= "Show simulator window on second monitor instead, default is first monitor" ) parser.add_argument("-f", "--fullscreen", action='store_true', help="Show simulator fullscreen") parser.add_argument("-m", "--maximized", action='store_true', help="Show simulator maximized") args = vars(parser.parse_args()) if args['version'] == True: from ev3dev2 import version as apiversion from ev3dev2simulator import version as simversion print("version ev3dev2 : " + apiversion.__version__) print("version ev3dev2simulator : " + simversion.__version__) sys.exit(0) config = get_config() s = args['window_scaling'] config.write_scale(s) t = args['simulation_type'] config.write_sim_type(t) x = apply_scaling(args['robot_position_x']) y = apply_scaling(args['robot_position_y']) o = args['robot_orientation'] c = args['connection_order_first'] use_second_screen_to_show_simulator = args['show_on_second_monitor'] show_fullscreen = args['fullscreen'] show_maximized = args['maximized'] robot_state = RobotState() robot_pos = (x, y, o) sim = Simulator(robot_state, robot_pos, show_fullscreen, show_maximized, use_second_screen_to_show_simulator) #sim.setup() server_thread = ServerSocketDouble( robot_state, c) if t == 'large' else ServerSocketSingle(robot_state) server_thread.setDaemon(True) server_thread.start() arcade.run()
def _draw_text_large_sim(self): """ Draw all the text fields. """ center_cs = 'CS ctr: ' + str(self.center_cs_data) left_cs = 'CS left: ' + str(self.left_cs_data) right_cs = 'CS right: ' + str(self.right_cs_data) left_ts = 'TS right: ' + str(self.right_ts_data) right_ts = 'TS left: ' + str(self.left_ts_data) top_us = 'US top: ' + str( int(round(self.front_us_data / self.scaling_multiplier))) + 'mm' bottom_us = 'US bot: ' + str(int(round( self.rear_us_data))) + 'mm' message = self.robot_state.next_sound_job() sound = message if message else '-' arcade.draw_text(center_cs, self.text_x, self.screen_height - apply_scaling(70), arcade.color.WHITE, 10) arcade.draw_text(left_cs, self.text_x, self.screen_height - apply_scaling(90), arcade.color.WHITE, 10) arcade.draw_text(right_cs, self.text_x, self.screen_height - apply_scaling(110), arcade.color.WHITE, 10) arcade.draw_text(left_ts, self.text_x, self.screen_height - apply_scaling(130), arcade.color.WHITE, 10) arcade.draw_text(right_ts, self.text_x, self.screen_height - apply_scaling(150), arcade.color.WHITE, 10) arcade.draw_text(top_us, self.text_x, self.screen_height - apply_scaling(170), arcade.color.WHITE, 10) arcade.draw_text(bottom_us, self.text_x, self.screen_height - apply_scaling(190), arcade.color.WHITE, 10) arcade.draw_text('Sound:', self.text_x, self.screen_height - apply_scaling(210), arcade.color.WHITE, 10) arcade.draw_text(sound, self.text_x, self.screen_height - apply_scaling(230), arcade.color.WHITE, 10, anchor_y='top') arcade.draw_text('Robot Arm', apply_scaling(1450), self.screen_height - apply_scaling(50), arcade.color.WHITE, 14, anchor_x="center") if self.msg_counter != 0: self.msg_counter -= 1 arcade.draw_text(self.falling_msg, self.msg_x, self.screen_height - apply_scaling(100), arcade.color.WHITE, 14, anchor_x="center") arcade.draw_text(self.restart_msg, self.msg_x, self.screen_height - apply_scaling(130), arcade.color.WHITE, 14, anchor_x="center")
def init_texture(self, src, scale): texture = arcade.load_texture(src, scale=apply_scaling(scale)) self.textures.append(texture) self.set_texture(0)
def __init__(self, robot_state: RobotState, robot_pos: Tuple[int, int, int], show_fullscreen: bool, show_maximized: bool, use_second_screen_to_show_simulator: bool): self.check_for_unique_instance() self.robot_state = robot_state self.set_screen_to_display_simulator_at_startup( use_second_screen_to_show_simulator) self.scaling_multiplier = get_config().get_scale() self.large_sim_type = get_config().is_large_sim_type() self.cfg = get_config().get_data() self.screen_total_width = int( apply_scaling(self.cfg['screen_settings']['screen_total_width'])) self.screen_width = int( apply_scaling(self.cfg['screen_settings']['screen_width'])) self.screen_height = int( apply_scaling(self.cfg['screen_settings']['screen_height'])) from ev3dev2.version import __version__ as apiversion from ev3dev2simulator.version import __version__ as simversion screen_title = self.cfg['screen_settings'][ 'screen_title'] + " version: " + simversion + " ev3dev2 api: " + apiversion self.frames_per_second = self.cfg['exec_settings']['frames_per_second'] self.falling_msg = self.cfg['screen_settings']['falling_message'] self.restart_msg = self.cfg['screen_settings']['restart_message'] super(Simulator, self).__init__(self.screen_total_width, self.screen_height, screen_title, update_rate=1 / 30, resizable=True) icon1 = pyglet.image.load(r'assets/images/body.png') self.set_icon(icon1) arcade.set_background_color( eval(self.cfg['screen_settings']['background_color'])) self.robot_elements = None self.obstacle_elements = None self.robot = None self.robot_pos = robot_pos self.red_lake = None self.green_lake = None self.blue_lake = None self.rock1 = None self.rock2 = None self.bottle1 = None self.border = None self.edge = None self.ground = None self.space = None self.center_cs_data = 0 self.left_cs_data = 0 self.right_cs_data = 0 self.left_ts_data = False self.right_ts_data = False self.front_us_data = -1 self.rear_us_data = -1 self.text_x = self.screen_width - apply_scaling(220) self.msg_x = self.screen_width / 2 self.msg_counter = 0 self.setup() if show_fullscreen == True: self.toggleFullScreenOnCurrentScreen() if show_maximized == True: self.maximize() self.check_for_activation()
def setup(self): """ Set up all the necessary shapes and sprites which are used in the simulation. These elements are added to lists to make buffered rendering possible to improve performance. """ self.robot_elements = arcade.SpriteList() self.obstacle_elements = arcade.ShapeElementList() if self.large_sim_type: self.robot = RobotLarge(self.cfg, self.robot_pos[0], self.robot_pos[1], self.robot_pos[2]) else: self.robot = RobotSmall(self.cfg, self.robot_pos[0], self.robot_pos[1], self.robot_pos[2]) for s in self.robot.get_sprites(): self.robot_elements.append(s) for s in self.robot.get_sensors(): self.robot_state.load_sensor(s) self.blue_lake = BlueLake(self.cfg) self.green_lake = GreenLake(self.cfg) self.red_lake = RedLake(self.cfg) self.obstacle_elements.append(self.blue_lake.shape) self.obstacle_elements.append(self.green_lake.shape) self.obstacle_elements.append(self.red_lake.shape) self.border = Border( self.cfg, eval(self.cfg['obstacle_settings']['border_settings'] ['border_color'])) self.edge = Edge(self.cfg) for s in self.border.shapes: self.obstacle_elements.append(s) self.space = Space() if self.large_sim_type: self.rock1 = Rock(apply_scaling(825), apply_scaling(1050), apply_scaling(150), apply_scaling(60), arcade.color.DARK_GRAY, 10) self.rock2 = Rock(apply_scaling(975), apply_scaling(375), apply_scaling(300), apply_scaling(90), arcade.color.DARK_GRAY, 130) self.ground = arcade.create_rectangle(apply_scaling(1460), apply_scaling(950), apply_scaling(300), apply_scaling(10), arcade.color.BLACK) self.obstacle_elements.append(self.rock1.shape) self.obstacle_elements.append(self.rock2.shape) self.obstacle_elements.append(self.ground) touch_obstacles = [self.rock1, self.rock2] falling_obstacles = [ self.blue_lake.hole, self.green_lake.hole, self.red_lake.hole, self.edge ] self.space.add(self.rock1.poly) self.space.add(self.rock2.poly) else: self.bottle1 = Bottle(apply_scaling(1000), apply_scaling(300), apply_scaling(40), arcade.color.DARK_OLIVE_GREEN) self.obstacle_elements.append(self.bottle1.shape) touch_obstacles = [self.bottle1] falling_obstacles = [self.edge] self.space.add(self.bottle1.poly) color_obstacles = [ self.blue_lake, self.green_lake, self.red_lake, self.border ] self.robot.set_color_obstacles(color_obstacles) self.robot.set_touch_obstacles(touch_obstacles) self.robot.set_falling_obstacles(falling_obstacles)
def __init__(self, cfg, center_x: int, center_y: int, orientation: int): super(RobotLarge, self).__init__(cfg, center_x, center_y) img_cfg = cfg['image_paths'] alloc_cfg = cfg['alloc_settings'] address_motor_left = alloc_cfg['motor']['left'] address_motor_right = alloc_cfg['motor']['right'] address_cs_center = alloc_cfg['color_sensor']['center'] address_cs_left = alloc_cfg['color_sensor']['left'] address_cs_right = alloc_cfg['color_sensor']['right'] address_ts_left = alloc_cfg['touch_sensor']['left'] address_ts_right = alloc_cfg['touch_sensor']['right'] address_ts_rear = alloc_cfg['touch_sensor']['rear'] address_us_front = alloc_cfg['ultrasonic_sensor']['front'] address_us_rear = alloc_cfg['ultrasonic_sensor']['rear'] self.wheel_distance = apply_scaling(cfg['wheel_settings']['spacing']) self.left_body = Body(img_cfg, self, apply_scaling(39), apply_scaling(-22.5)) self.right_body = Body(img_cfg, self, apply_scaling(-39), apply_scaling(-22.5)) self.arm = Arm(img_cfg, self, apply_scaling(15), apply_scaling(102)) self.arm_large = ArmLarge(img_cfg, apply_scaling(1450), apply_scaling(1100)) self.left_wheel = Wheel(address_motor_left, img_cfg, self, (self.wheel_distance / -2), 0.01) self.right_wheel = Wheel(address_motor_right, img_cfg, self, (self.wheel_distance / 2), 0.01) self.center_color_sensor = ColorSensor(address_cs_center, img_cfg, self, 0, apply_scaling(84)) self.left_color_sensor = ColorSensor(address_cs_left, img_cfg, self, apply_scaling(-69), apply_scaling(95)) self.right_color_sensor = ColorSensor(address_cs_right, img_cfg, self, apply_scaling(69), apply_scaling(95)) self.left_touch_sensor = TouchSensor(address_ts_left, img_cfg, self, apply_scaling(-65), apply_scaling(102), 'left') self.right_touch_sensor = TouchSensor(address_ts_right, img_cfg, self, apply_scaling(65), apply_scaling(102), 'right') self.rear_touch_sensor = TouchSensor(address_ts_rear, img_cfg, self, 0, apply_scaling(-165), 'rear') self.front_ultrasonic_sensor = UltrasonicSensor( address_us_front, img_cfg, self, apply_scaling(-22), apply_scaling(56)) self.rear_ultrasonic_sensor = UltrasonicSensorBottom( address_us_rear, img_cfg, self, 0, apply_scaling(-145)) self.left_brick_left_led = Led(img_cfg, self, apply_scaling(-20), apply_scaling(-55)) self.left_brick_right_led = Led(img_cfg, self, apply_scaling(-60), apply_scaling(-55)) self.right_brick_left_led = Led(img_cfg, self, apply_scaling(20), apply_scaling(-55)) self.right_brick_right_led = Led(img_cfg, self, apply_scaling(60), apply_scaling(-55)) self.movable_sprites = [ self.left_wheel, self.right_wheel, self.left_body, self.right_body, self.center_color_sensor, self.left_color_sensor, self.right_color_sensor, self.left_touch_sensor, self.right_touch_sensor, self.rear_touch_sensor, self.rear_ultrasonic_sensor, self.front_ultrasonic_sensor, self.left_brick_left_led, self.left_brick_right_led, self.right_brick_left_led, self.right_brick_right_led, self.arm ] self.sprites = self.movable_sprites.copy() self.sprites.append(self.arm_large) if orientation != 0: self._rotate(math.radians(orientation))
def __init__(self, cfg): depth = apply_scaling(cfg['screen_settings']['edge_spacing']) edge_spacing = 0 super(Edge, self).__init__(cfg, 1, depth, edge_spacing)