def main(r):
    try:
        ctask = asyncio.ensure_future(capture_color(r))
        turn_task = None
        start_angle = 0
        while True:
            if c.started:
                if turn_task is None:
                    turn_task = asyncio.ensure_future(r.drive.turn_speed(np.radians(30)))
                    start_angle = r.drive.odometer.val.theta
            elif turn_task is not None:
                turn_task.cancel()
                turn_task = None
            if abs(r.drive.odometer.val.theta - start_angle) > 2*np.pi:
                if turn_task is not None:
                    turn_task.cancel()
                    c.started = False
                    turn_task = None

            if ctask.done():
                yield From(ctask)

            yield
            if w.get_key() == ' ':
                break
    finally:
        print "Finally in main"
        ctask.cancel()
        yield From(ctask)
 def run(self, driver, time_out):
     self.loop = new_event_loop()
     set_event_loop(self.loop)
     ensure_future(self.control_loop(driver, time_out))
     self.loop.run_forever()
     self.loop.close()
     return (self.distance_to_goal, self.left_velocity, self.right_velocity)
Exemple #3
0
def run(item_group, stream, args):
    n_bytes = 0
    n_errors = 0
    last_error = None
    start_time = time.time()
    tasks = collections.deque()
    if args.heaps is None:
        rep = itertools.repeat(False)
    else:
        rep = itertools.chain(itertools.repeat(False, args.heaps), [True])
    for is_end in rep:
        if len(tasks) >= 2:
            try:
                n_bytes += yield From(tasks.popleft())
            except Exception as error:
                n_errors += 1
                last_error = error
        if is_end:
            task = trollius.ensure_future(stream.async_send_heap(item_group.get_end()))
        else:
            for item in item_group.values():
                item.version += 1
            task = trollius.ensure_future(stream.async_send_heap(item_group.get_heap()))
        tasks.append(task)
    while len(tasks) > 0:
        try:
            n_bytes += yield From(tasks.popleft())
        except Exception as error:
            n_errors += 1
            last_error = error
    elapsed = time.time() - start_time
    if last_error is not None:
        logging.warn('%d errors, last one: %s', n_errors, last_error)
    print('Sent {} bytes in {:.6f}s, {:.6f} Gb/s'.format(
        n_bytes, elapsed, n_bytes * 8 / elapsed / 1e9))
    def test_log_slow_callbacks(self, m_logger):
        def stop_loop_cb(loop):
            loop.stop()

        @asyncio.coroutine
        def stop_loop_coro(loop):
            yield From(None)
            loop.stop()

        asyncio.set_event_loop(self.loop)
        self.loop.set_debug(True)
        self.loop.slow_callback_duration = 0.0

        # slow callback
        self.loop.call_soon(stop_loop_cb, self.loop)
        self.loop.run_forever()
        fmt = m_logger.warning.call_args[0][0]
        args = m_logger.warning.call_args[0][1:]
        self.assertRegex(
            fmt % tuple(args), "^Executing <Handle.*stop_loop_cb.*> "
            "took .* seconds$")

        # slow task
        asyncio.ensure_future(stop_loop_coro(self.loop), loop=self.loop)
        self.loop.run_forever()
        fmt = m_logger.warning.call_args[0][0]
        args = m_logger.warning.call_args[0][1:]
        self.assertRegex(
            fmt % tuple(args), "^Executing <Task.*stop_loop_coro.*> "
            "took .* seconds$")
def run(driver, time_out):
    loop = new_event_loop()
    set_event_loop(loop)
    ensure_future(control_loop(driver, time_out, loop))
    loop.run_forever()
    loop.close()
    return (distance_to_goal, left_velocity, right_velocity)
 def run(self, driver, time_out):
     self.loop = new_event_loop()
     set_event_loop(self.loop)
     ensure_future(self.control_loop(driver, time_out))
     self.loop.run_forever()
     self.loop.close()
     return (self.distance_to_goal, self.left_velocity, self.right_velocity)
Exemple #7
0
def run(driver, time_out):
    loop = new_event_loop()
    set_event_loop(loop)
    ensure_future(control_loop(driver, time_out, loop))
    loop.run_forever()
    loop.close()
    return (distance_to_goal, left_velocity, right_velocity)
 def test_async_flush_fail(self):
     """Test async_flush in the case that the last heap sent failed.
     This is arranged by filling up the queue slots first.
     """
     for i in range(5):
         trollius.ensure_future(self.stream.async_send_heap(self.heap))
     # The above only queues up the async sends on the event loop. The rest of the
     # test needs to be run from inside the event loop
     trollius.get_event_loop().run_until_complete(self._test_async_flush())
Exemple #9
0
def run(dev=None):
    driver = Driver()
    platform_setup(driver)
    if dev is not None:
        asyncio.ensure_future(arrive_device(driver, dev))
    asyncio.ensure_future(keypress_dispatch(driver))
    driver.running = True
    #driver.loop.set_debug(True)
    driver.loop.run_forever()
    driver.loop.close()
Exemple #10
0
    def interrupt_watcher(self):
        if hasattr(self, "_wait_for_interrupt"):
            waiter = self._wait_for_interrupt
        else:
            waiter = self._watch_for_sb_update

        yield From(self.loop.run_in_executor(None, waiter))
        yield From(self.handle_interrupt())
        if self.running:
            asyncio.ensure_future(self.interrupt_watcher())
Exemple #11
0
    def interrupt_watcher(self):
        if hasattr(self, "_wait_for_interrupt"):
            waiter = self._wait_for_interrupt
        else:
            waiter = self._watch_for_sb_update

        yield From(self.loop.run_in_executor(None, waiter))
        yield From(self.handle_interrupt())
        if self.running:
            asyncio.ensure_future(self.interrupt_watcher())
Exemple #12
0
def run(dev=None):
    driver = Driver()
    platform_setup(driver)
    if dev is not None:
        asyncio.ensure_future(arrive_device(driver, dev))
    asyncio.ensure_future(keypress_dispatch(driver))
    driver.running = True
    #driver.loop.set_debug(True)
    driver.loop.run_forever()
    driver.loop.close()
Exemple #13
0
    def start(self):
        loop = asyncio.get_event_loop()

        try:
            if not loop.is_running():
                loop.run_until_complete(self.run())
            else:
                asyncio.ensure_future(self.run(), loop=loop)
        except KeyboardInterrupt:
            loop.close()
Exemple #14
0
    def run(self):
        self.running = True
        self.keypress_handlers = {
            'g': self.gui_handler,
            'h': self.help_handler,
            'q': self.quit_handler,
            'v': self.verbosity_handler,
        }

        self.loop = asyncio.get_event_loop()
        asyncio.ensure_future(self.keypress_dispatch())
        asyncio.ensure_future(self.arrive_device())
        self.loop.run_forever()
        asyncio.executor.get_default_executor().shutdown(False) 
Exemple #15
0
    def run(self):
        self.running = True
        self.keypress_handlers = {
            'g': self.gui_handler,
            'h': self.help_handler,
            'q': self.quit_handler,
            'v': self.verbosity_handler,
        }

        self.loop = asyncio.get_event_loop()
        asyncio.ensure_future(self.keypress_dispatch())
        asyncio.ensure_future(self.arrive_device())
        self.loop.run_forever()
        asyncio.executor.get_default_executor().shutdown(False)
Exemple #16
0
def arrive_device(driver, dev):
    logger.info("device arrival initiated")

    driver.device = dev

    dev.init()
    dev.nvram.acquire_lock()
    dev.reset()
    yield From(msleep(0.5))

    setup_steps = [
        _pci_setup,
        _msi_setup,
        _hc_setup,
        _grc_setup,
        _bufman_setup,
        _emac_setup,
        _rbdi_setup,
        _rx_ring_setup,
    ]

    tasks = []
    for step in setup_steps:
        tasks += [asyncio.ensure_future(step(driver))]

    try:
        asyncio.wait(tasks)
    except Exception:
        print "there was an exception!"
    logger.info("device arrival concluded")
Exemple #17
0
def arrive_device(driver, dev):
    logger.info("device arrival initiated")
    
    driver.device = dev

    dev.init()
    dev.nvram.acquire_lock()
    dev.reset()
    yield From(msleep(0.5))

    setup_steps = [
        _pci_setup,
        _msi_setup,
        _hc_setup,
        _grc_setup,
        _bufman_setup,
        _emac_setup,
        _rbdi_setup,
        _rx_ring_setup,
    ]

    tasks = []
    for step in setup_steps:
        tasks += [asyncio.ensure_future(step(driver))]

    try:
        asyncio.wait(tasks)
    except Exception:
        print "there was an exception!"
    logger.info("device arrival concluded")
    def test_async(self):
        fut = asyncio.Future()
        self.assertIs(fut._loop, self.loop)

        fut2 = trollius.ensure_future(fut)
        self.assertIs(fut2, fut)
        self.assertIs(fut._loop, self.loop)
Exemple #19
0
def run_avoiding_walls(r, coro):
    try:
        task = asyncio.ensure_future(coro)
        while not task.done():
            if r.left_short_ir.val and r.right_short_ir.val and util.close_to_wall(
                    r):
                task.cancel()
                log.debug("All sensors hit")
                if r.left_bumper.val:
                    yield From(r.drive.turn_angle(np.radians(-120)))
                else:
                    yield From(r.drive.turn_angle(np.radians(120)))
            if r.left_bumper.val or r.left_short_ir.val:
                log.debug("Left side triggered")
                task.cancel()
                yield From(avoid_wall(r, r.left_short_ir, r.left_bumper, -1))
            if r.right_bumper.val or r.right_short_ir.val:
                log.debug("Right side triggered")
                task.cancel()
                yield From(avoid_wall(r, r.right_short_ir, r.right_bumper, 1))
            yield

        try:
            task.result()
        except asyncio.CancelledError:
            pass
    finally:
        task.cancel()
    def test_default_exc_handler_coro(self):
        self.loop._process_events = mock.Mock()
        self.loop.set_debug(True)
        asyncio.set_event_loop(self.loop)

        @asyncio.coroutine
        def zero_error_coro():
            yield From(asyncio.sleep(0.01, loop=self.loop))
            1 / 0

        # Test Future.__del__
        with mock.patch('trollius.base_events.logger') as log:
            fut = asyncio.ensure_future(zero_error_coro(), loop=self.loop)
            fut.add_done_callback(lambda *args: self.loop.stop())
            self.loop.run_forever()
            fut = None  # Trigger Future.__del__ or futures._TracebackLogger
            support.gc_collect()
            if PY34:
                # Future.__del__ in Python 3.4 logs error with
                # an actual exception context
                log.error.assert_called_with(
                    test_utils.MockPattern('.*exception was never retrieved'),
                    exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY))
            else:
                # futures._TracebackLogger logs only textual traceback
                log.error.assert_called_with(test_utils.MockPattern(
                    '.*exception was never retrieved.*ZeroDiv'),
                                             exc_info=False)
Exemple #21
0
def init_rx_rings(self):
    dev = self.dev
    
    dev.rdi.mini_rcb.disable_ring = 1
    logger.debug("mini receive producer ring disabled")

    self.rx_ring_vaddr, self.rx_ring_len = _init_xx_ring(self, tg.rbd)
    self.rx_ring_paddr = self.mm.get_paddr(self.rx_ring_vaddr)
    self.rx_buffers = [0] * self.rx_ring_len

    dev.rdi.std_rcb.host_addr_hi = self.rx_ring_paddr >> 32
    dev.rdi.std_rcb.host_addr_low = self.rx_ring_paddr & 0xffffffff
    dev.rdi.std_rcb.ring_size = self.rx_ring_len
    dev.rdi.std_rcb.max_frame_len = 0x600
    dev.rdi.std_rcb.nic_addr = 0x6000
    dev.rdi.std_rcb.disable_ring = 0
    logger.info("standard receive producer ring of size %d allocated at %x",
            self.rx_ring_len, self.rx_ring_vaddr)

    dev.rdi.jumbo_rcb.disable_ring = 1
    logger.debug("jumbo receive producer ring disabled")
    dev.hpmb.box[tg.mb_rbd_standard_producer].low = 0
    self._std_rbd_pi = 0
    self._std_rbd_ci = 0
    producers = []
    for i in range(self.rx_ring_len):
        producers += [asyncio.ensure_future(produce_rxb(self, i))]
    asyncio.wait(producers)
    self._std_rbd_pi = self.rx_ring_len - 1
    dev.hpmb.box[tg.mb_rbd_standard_producer].low = self._std_rbd_pi
Exemple #22
0
def run_avoiding_walls(r, coro):
    try:
        task = asyncio.ensure_future(coro)
        while not task.done():
            if r.left_short_ir.val and r.right_short_ir.val and util.close_to_wall(r):
                task.cancel()
                log.debug("All sensors hit")
                if r.left_bumper.val:
                    yield From(r.drive.turn_angle(np.radians(-120)))
                else:
                    yield From(r.drive.turn_angle(np.radians(120)))
            if r.left_bumper.val or r.left_short_ir.val:
                log.debug("Left side triggered")
                task.cancel()
                yield From(avoid_wall(r,r.left_short_ir,r.left_bumper,-1))
            if r.right_bumper.val or r.right_short_ir.val:
                log.debug("Right side triggered")
                task.cancel()
                yield From(avoid_wall(r,r.right_short_ir,r.right_bumper,1))
            yield

        try:
            task.result()
        except asyncio.CancelledError:
            pass
    finally:
        task.cancel()
Exemple #23
0
def wall_fondle(r):
    global has_spun
    log.info("Going into wall fondling")
    while True:
        yield
        if ganked_cube >= has_spun:
            log.info("Spinning")
            has_spun = time.time()
            startAngle = r.drive.odometer.val.theta

            # Turn until find good direction or if we spin all the way around
            try:
                task = asyncio.ensure_future(r.drive.turn_speed(np.radians(30)))
                while not task.done():
                    gap_found = False
                    if abs(r.drive.odometer.val.theta - startAngle) >= 2*pi:
                        task.cancel()
                    yield
                try:
                    task.result()
                except asyncio.CancelledError:
                    pass
            finally:
                task.cancel()
        log.info("Done spinning, going straight now")
        try:
            yield From(run_avoiding_walls(r, r.drive.go_forever(0.2, 0)))
        except asyncio.TimeoutError:
            log.warn("Wall bouncing task timed out and we caught it")
            # TODO: Make smarter thing than just moving arbitrarily
            Drive.go_distance(r.drive, -1)
            # Pick a random direction to turn to
            side = 1 if int(time.time()) % 2 else -1 # this is apparently in python???
            yield From(r.drive.turn_angle(side * np.radians(45)))
Exemple #24
0
 def test_close(self):
     a, b = self.loop._socketpair()
     trans = self.loop._make_socket_transport(a, asyncio.Protocol())
     f = asyncio.ensure_future(self.loop.sock_recv(b, 100))
     trans.close()
     self.loop.run_until_complete(f)
     self.assertEqual(f.result(), b'')
     b.close()
Exemple #25
0
def main(r):
    task = asyncio.ensure_future(find_cubes(r))
    try:
        yield From(asyncio.wait_for(task, SILO_TIME))
    except asyncio.TimeoutError:
        pass

    yield From(clean_up(r))
Exemple #26
0
def main(r):
    task = asyncio.ensure_future(find_cubes(r))
    try:
        yield From(asyncio.wait_for(task, SILO_TIME))
    except asyncio.TimeoutError:
        pass

    yield From(clean_up(r))
Exemple #27
0
def start_write_loops(file_pairs, interval, random_variation, no_loop):
    tasks = []
    if not 0.1 < interval < 100.0:
        raise RuntimeError('Invalid time interval value: {}'.format(interval))
    loop = trollius.get_event_loop()
    for input_file, output_file in file_pairs:
        task = trollius.ensure_future(log_write_loop(input_file, output_file, interval, random_variation, no_loop))
        tasks.append(task)
    loop.run_until_complete(trollius.wait(tasks))
Exemple #28
0
def wall_fondle(r):
    try:
        task = asyncio.ensure_future(dumb_drive(r))
        while True:
            yield
            yield From(dumb_drive(r))
            if r.l_bumper.val:
                task.cancel()
                yield From(avoid_wall(r, r.left_short_ir, r.l_bumper, -1))
                task = asyncio.ensure_future(dumb_drive(r))
            if r.r_bumper.val:
                task.cancel()
                yield From(avoid_wall(r, r.right_short_ir, r.r_bumper, 1))
                task = asyncio.ensure_future(dumb_drive(r))
            if get_cube(r):
                task.cancel()
                yield From(pick_up_cubes(r))
    finally:
        task.cancel()
def wall_fondle(r):
    try:
        task = asyncio.ensure_future(dumb_drive(r))
        while True:
            yield
            yield From(dumb_drive(r))
            if r.l_bumper.val:
                task.cancel()
                yield From(avoid_wall(r,r.left_short_ir,r.l_bumper,-1))
                task = asyncio.ensure_future(dumb_drive(r))
            if r.r_bumper.val:
                task.cancel()
                yield From(avoid_wall(r,r.right_short_ir,r.r_bumper,1))
                task = asyncio.ensure_future(dumb_drive(r))
            if get_cube(r):
                task.cancel()
                yield From(pick_up_cubes(r))
    finally:
        task.cancel()
Exemple #30
0
def start_write_loops(file_pairs, interval, random_variation, no_loop):
    tasks = []
    if not 0.1 < interval < 100.0:
        raise RuntimeError('Invalid time interval value: {}'.format(interval))
    loop = trollius.get_event_loop()
    for input_file, output_file in file_pairs:
        task = trollius.ensure_future(
            log_write_loop(input_file, output_file, interval, random_variation,
                           no_loop))
        tasks.append(task)
    loop.run_until_complete(trollius.wait(tasks))
Exemple #31
0
def call_as_future(callable, *args, **kwargs):
    """This is a copy of thrift.util.asyncio. So, let's consider unifying them.

        call_as_future(callable, *args, **kwargs) -> trollius.Task

    Like trollius.ensure_future() but takes any callable and converts
    it to a coroutine function first.
    """
    if not asyncio.iscoroutinefunction(callable):
        callable = asyncio.coroutine(callable)

    return asyncio.ensure_future(callable(*args, **kwargs))
Exemple #32
0
def call_as_future(callable, loop, *args, **kwargs):
    """This is a copy of thrift.util.asyncio. So, let's consider unifying them.

        call_as_future(callable, *args, **kwargs) -> trollius.Task

    Like trollius.ensure_future() but takes any callable and converts
    it to a coroutine function first.
    """
    if not asyncio.iscoroutinefunction(callable):
        callable = asyncio.coroutine(callable)

    return asyncio.ensure_future(callable(*args, **kwargs), loop=loop)
Exemple #33
0
def keypress_dispatch(driver):
    key = yield From(driver.loop.run_in_executor(None, wait_for_keypress, driver))
    if key in KEYPRESS_HANDLERS:
        asyncio.ensure_future(KEYPRESS_HANDLERS[key](driver))
    else:
        asyncio.ensure_future(unknown_keypress_handler(key))
    asyncio.ensure_future(keypress_dispatch(driver))
Exemple #34
0
 def keypress_dispatch(self):
     r = yield From(self.loop.run_in_executor(None, self._wait_for_keypress))
     if r in self.keypress_handlers:
         asyncio.ensure_future(self.keypress_handlers[r]())
     else:
         asyncio.ensure_future(self.unknown_keypress_handler(r))
     asyncio.ensure_future(self.keypress_dispatch())
Exemple #35
0
    def __init__(self, config):
        # type: (config.Config) -> ()

        self.config = config

        # Initialize ros service interfaces
        self.grasping_controller = grasping_controller.MoveitPickPlaceInterface(
            arm_name=self.config.arm_move_group_name,
            gripper_name=self.config.gripper_move_group_name,
            grasp_approach_tran_frame=self.config.grasp_approach_frame,
            analyzer_planner_id=self.config.analyzer_planner_id,
            execution_planner_id=self.config.executor_planner_id,
            allowed_analyzing_time=self.config.allowed_analyzing_time,
            allowed_execution_time=self.config.allowed_execution_time)

        self.scene = moveit_commander.PlanningSceneInterface()
        self.tf_listener = tf.TransformListener()
        self.tf_broadcaster = tf.TransformBroadcaster()

        # If we need to broadcast approach_tran - start publishing
        if self.config.broadcast_approach_tran:
            asyncio.ensure_future(self._publish_approach_tran())
Exemple #36
0
def keypress_dispatch(driver):
    key = yield From(
        driver.loop.run_in_executor(None, wait_for_keypress, driver))
    if key in KEYPRESS_HANDLERS:
        asyncio.ensure_future(KEYPRESS_HANDLERS[key](driver))
    else:
        asyncio.ensure_future(unknown_keypress_handler(key))
    asyncio.ensure_future(keypress_dispatch(driver))
Exemple #37
0
 def keypress_dispatch(self):
     r = yield From(self.loop.run_in_executor(None,
                                              self._wait_for_keypress))
     if r in self.keypress_handlers:
         asyncio.ensure_future(self.keypress_handlers[r]())
     else:
         asyncio.ensure_future(self.unknown_keypress_handler(r))
     asyncio.ensure_future(self.keypress_dispatch())
Exemple #38
0
def run_picking_up_cubes(r, coro):
    try:
        task = asyncio.ensure_future(coro)
        while not task.done():
            if get_cube(r) != Colors.NONE:
                task.cancel()
                yield From(pick_up_cubes(r))
            yield
        try:
            task.result()
        except asyncio.CancelledError:
            pass
    finally:
        task.cancel()
    def __init__(self, config):
        # type: (config.Config) -> ()

        self.config = config

        # Initialize ros service interfaces
        self.grasping_controller = grasping_controller.MoveitPickPlaceInterface(
            arm_name=self.config.arm_move_group_name,
            gripper_name=self.config.gripper_move_group_name,
            grasp_approach_tran_frame=self.config.grasp_approach_frame,
            analyzer_planner_id=self.config.analyzer_planner_id,
            execution_planner_id=self.config.executor_planner_id,
            allowed_analyzing_time=self.config.allowed_analyzing_time,
            allowed_execution_time=self.config.allowed_execution_time
        )

        self.scene = moveit_commander.PlanningSceneInterface()
        self.tf_listener = tf.TransformListener()
        self.tf_broadcaster = tf.TransformBroadcaster()

        # If we need to broadcast approach_tran - start publishing
        if self.config.broadcast_approach_tran:
            asyncio.ensure_future(self._publish_approach_tran())
Exemple #40
0
def run_picking_up_cubes(r, coro):
    try:
        task = asyncio.ensure_future(coro)
        while not task.done():
            if get_cube(r) != Colors.NONE:
                task.cancel()
                yield From(pick_up_cubes(r))
            yield
        try:
            task.result()
        except asyncio.CancelledError:
            pass
    finally:
        task.cancel()
Exemple #41
0
def send_stream(item_group, stream, num_heaps):
    tasks = collections.deque()
    transferred = 0
    for i in range(num_heaps + 1):
        while len(tasks) >= 2:
            transferred += yield From(tasks.popleft())
        if i == num_heaps:
            heap = item_group.get_end()
        else:
            heap = item_group.get_heap(data='all')
        task = trollius.ensure_future(stream.async_send_heap(heap))
        tasks.append(task)
    for task in tasks:
        transferred += yield From(task)
    raise Return(transferred)
    def test_create_task(self):
        class MyTask(asyncio.Task):
            pass

        @asyncio.coroutine
        def test():
            pass

        class EventLoop(base_events.BaseEventLoop):
            def create_task(self, coro):
                return MyTask(coro, loop=loop)

        loop = EventLoop()
        self.set_event_loop(loop)

        coro = test()
        task = asyncio.ensure_future(coro, loop=loop)
        self.assertIsInstance(task, MyTask)

        # make warnings quiet
        task._log_destroy_pending = False
        coro.close()
Exemple #43
0
def clean_up(r):
    log.info('Doing round cleanup')
    r.drive.stop()

    startAngle = r.drive.odometer.val.theta

    # Turn until find good direction or if we spin all the way around
    try:
        task = asyncio.ensure_future(r.drive.turn_speed(np.radians(30)))
        while not task.done():
            gap_found = False
            if abs(r.drive.odometer.val.theta - startAngle) >= 2*pi:
                task.cancel()

            if not util.close_to_wall(r):
                task.cancel()
                gap_found = True

            yield

        try:
            task.result()
        except asyncio.CancelledError:
            pass
    finally:
        task.cancel()

    if not gap_found:
        log.debug('Rotated 2pi, no gaps')

    r.drive.stop()
    yield asyncio.sleep(0.5)
    r.silo.open()
    yield asyncio.sleep(1.5)
    if gap_found:
        log.debug('Going forward to leave stack, because there\'s space')
        Drive.go_distance(r.drive, 6)
Exemple #44
0
def clean_up(r):
    log.info('Doing round cleanup')
    r.drive.stop()

    startAngle = r.drive.odometer.val.theta

    # Turn until find good direction or if we spin all the way around
    try:
        task = asyncio.ensure_future(r.drive.turn_speed(np.radians(30)))
        while not task.done():
            gap_found = False
            if abs(r.drive.odometer.val.theta - startAngle) >= 2 * pi:
                task.cancel()

            if not util.close_to_wall(r):
                task.cancel()
                gap_found = True

            yield

        try:
            task.result()
        except asyncio.CancelledError:
            pass
    finally:
        task.cancel()

    if not gap_found:
        log.debug('Rotated 2pi, no gaps')

    r.drive.stop()
    yield asyncio.sleep(0.5)
    r.silo.open()
    yield asyncio.sleep(1.5)
    if gap_found:
        log.debug('Going forward to leave stack, because there\'s space')
        Drive.go_distance(r.drive, 6)
Exemple #45
0
def main_task():
    t = None
    while True:
        c = w.get_key()

        new_t = None

        if c == 'q':
            break
        elif c == 'm':
            new_t = drive.turn_speed(np.radians(5))
        elif c == 'w':
            new_t = drive.turn_to(0)
        elif c == 'e':
            new_t = drive.go_to([24, 0])
        elif c == 'd':
            new_t = drive.turn_to(np.pi)
        elif c == 's':
            new_t = drive.go_to([0, 0])
        elif c == ' ' and t:
            t.cancel()
            try:
                yield From(t)
            except asyncio.CancelledError:
                pass

        if new_t:
            if t:
                t.cancel()
                try:
                    yield From(t)
                except asyncio.CancelledError:
                    pass
            t = asyncio.ensure_future(new_t)

        yield From(asyncio.sleep(0.05))
Exemple #46
0
def wall_fondle(r):
    global has_spun
    log.info("Going into wall fondling")
    while True:
        yield
        if ganked_cube >= has_spun:
            log.info("Spinning")
            has_spun = time.time()
            startAngle = r.drive.odometer.val.theta

            # Turn until find good direction or if we spin all the way around
            try:
                task = asyncio.ensure_future(r.drive.turn_speed(
                    np.radians(30)))
                while not task.done():
                    gap_found = False
                    if abs(r.drive.odometer.val.theta - startAngle) >= 2 * pi:
                        task.cancel()
                    yield
                try:
                    task.result()
                except asyncio.CancelledError:
                    pass
            finally:
                task.cancel()
        log.info("Done spinning, going straight now")
        try:
            yield From(run_avoiding_walls(r, r.drive.go_forever(0.2, 0)))
        except asyncio.TimeoutError:
            log.warn("Wall bouncing task timed out and we caught it")
            # TODO: Make smarter thing than just moving arbitrarily
            Drive.go_distance(r.drive, -1)
            # Pick a random direction to turn to
            side = 1 if int(
                time.time()) % 2 else -1  # this is apparently in python???
            yield From(r.drive.turn_angle(side * np.radians(45)))
def main_task():
    t = None
    while True:
        c = w.get_key()

        new_t = None

        if c == 'q':
            break
        elif c == 'm':
            new_t = drive.turn_speed(np.radians(5))
        elif c == 'w':
            new_t = drive.turn_to(0)
        elif c == 'e':
            new_t = drive.go_to([24,0])
        elif c == 'd':
            new_t = drive.turn_to(np.pi)
        elif c == 's':
            new_t = drive.go_to([0,0])
        elif c == ' ' and t:
            t.cancel()
            try:
                yield From(t)
            except asyncio.CancelledError:
                pass

        if new_t:
            if t:
                t.cancel()
                try:
                    yield From(t)
                except asyncio.CancelledError:
                    pass
            t = asyncio.ensure_future(new_t)

        yield From(asyncio.sleep(0.05))
Exemple #48
0
 def run_control(self):
     try:
         stream_task = None
         while True:
             command = yield From(self.reader.readline())
             command = command.decode('ascii')
             logging.debug("command = %s", command)
             if not command:
                 break
             command = json.loads(command)
             if command['cmd'] == 'start':
                 if stream_task is not None:
                     logging.warning("Start received while already running: %s", command)
                     continue
                 args = argparse.Namespace(**command['args'])
                 if six.PY2:
                     args_dict = vars(args)
                     for key in args_dict:
                         if isinstance(args_dict[key], six.text_type):
                             args_dict[key] = args_dict[key].encode('ascii')
                 if args.recv_affinity is not None and len(args.recv_affinity) > 0:
                     spead2.ThreadPool.set_affinity(args.recv_affinity[0])
                     thread_pool = spead2.ThreadPool(
                         1, args.recv_affinity[1:] + args.recv_affinity[:1])
                 else:
                     thread_pool = spead2.ThreadPool()
                 thread_pool = spead2.ThreadPool()
                 memory_pool = spead2.MemoryPool(
                     args.heap_size, args.heap_size + 1024, args.mem_max_free, args.mem_initial)
                 stream = spead2.recv.trollius.Stream(thread_pool, 0, args.heaps,
                                                      args.ring_heaps)
                 stream.set_memory_allocator(memory_pool)
                 if args.memcpy_nt:
                     stream.set_memcpy(spead2.MEMCPY_NONTEMPORAL)
                 bind_hostname = '' if args.multicast is None else args.multicast
                 if 'recv_ibv' in args and args.recv_ibv is not None:
                     try:
                         stream.add_udp_ibv_reader(
                             bind_hostname, args.port,
                             args.recv_ibv,
                             args.packet, args.recv_buffer,
                             args.recv_ibv_vector, args.recv_ibv_max_poll)
                     except AttributeError:
                         logging.error('--recv-ibv passed but slave does not support ibv')
                         sys.exit(1)
                 else:
                     stream.add_udp_reader(args.port, args.packet, args.recv_buffer,
                                           bind_hostname)
                 thread_pool = None
                 memory_pool = None
                 stream_task = trollius.ensure_future(self.run_stream(stream))
                 self._write('ready\n')
             elif command['cmd'] == 'stop':
                 if stream_task is None:
                     logging.warning("Stop received when already stopped")
                     continue
                 stream.stop()
                 received_heaps = yield From(stream_task)
                 self._write(json.dumps({'received_heaps': received_heaps}) + '\n')
                 stream_task = None
                 stream = None
             elif command['cmd'] == 'exit':
                 break
             else:
                 logging.warning("Bad command: %s", command)
                 continue
         logging.debug("Connection closed")
         if stream_task is not None:
             stream.stop()
             yield From(stream_task)
     except Exception:
         traceback.print_exc()
Exemple #49
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--log', metavar='LEVEL', default='INFO', help='Log level [%(default)s]')
    subparsers = parser.add_subparsers(title='subcommands')
    master = subparsers.add_parser('master')
    master.add_argument('--quiet', action='store_true', default=False,
                        help='Print only the final result')
    master.add_argument('--packet', metavar='BYTES', type=int, default=9172,
                        help='Maximum packet size to use for UDP [%(default)s]')
    master.add_argument('--heap-size', metavar='BYTES', type=int, default=4194304,
                        help='Payload size for heap [%(default)s]')
    master.add_argument('--addr-bits', metavar='BITS', type=int, default=40,
                        help='Heap address bits [%(default)s]')
    master.add_argument('--multicast', metavar='ADDRESS', type=str,
                        help='Send via multicast group [unicast]')
    group = master.add_argument_group('sender options')
    group.add_argument('--send-affinity', type=spead2.parse_range_list,
                       help='List of CPUs to pin threads to [no affinity]')
    group.add_argument('--send-buffer', metavar='BYTES', type=int,
                       default=spead2.send.trollius.UdpStream.DEFAULT_BUFFER_SIZE,
                       help='Socket buffer size [%(default)s]')
    group.add_argument('--burst', metavar='BYTES', type=int,
                       default=spead2.send.StreamConfig.DEFAULT_BURST_SIZE,
                       help='Send burst size [%(default)s]')
    group.add_argument('--burst-rate-ratio', metavar='RATIO', type=float,
                       default=spead2.send.StreamConfig.DEFAULT_BURST_RATE_RATIO,
                       help='Hard rate limit, relative to nominal rate [%(default)s]')
    if hasattr(spead2.send, 'UdpIbvStream'):
        group.add_argument('--send-ibv', type=str, metavar='ADDRESS',
                           help='Use ibverbs with this interface address [no]')
        group.add_argument('--send-ibv-vector', type=int, default=0, metavar='N',
                           help='Completion vector, or -1 to use polling [%(default)s]')
        group.add_argument('--send-ibv-max-poll', type=int,
                           default=spead2.send.UdpIbvStream.DEFAULT_MAX_POLL,
                           help='Maximum number of times to poll in a row [%(default)s]')
    group = master.add_argument_group('receiver options')
    group.add_argument('--recv-affinity', type=spead2.parse_range_list,
                       help='List of CPUs to pin threads to [no affinity]')
    group.add_argument('--recv-buffer', metavar='BYTES', type=int,
                       default=spead2.recv.Stream.DEFAULT_UDP_BUFFER_SIZE,
                       help='Socket buffer size [%(default)s]')
    if hasattr(spead2.recv.Stream, 'add_udp_ibv_reader'):
        group.add_argument('--recv-ibv', type=str, metavar='ADDRESS',
                           help='Use ibverbs with this interface address [no]')
        group.add_argument('--recv-ibv-vector', type=int, default=0, metavar='N',
                           help='Completion vector, or -1 to use polling [%(default)s]')
        group.add_argument('--recv-ibv-max-poll', type=int,
                           default=spead2.recv.Stream.DEFAULT_UDP_IBV_MAX_POLL,
                           help='Maximum number of times to poll in a row [%(default)s]')
    group.add_argument('--heaps', type=int, default=spead2.recv.Stream.DEFAULT_MAX_HEAPS,
                       help='Maximum number of in-flight heaps [%(default)s]')
    group.add_argument('--ring-heaps', type=int, default=spead2.recv.Stream.DEFAULT_RING_HEAPS,
                       help='Ring buffer capacity in heaps [%(default)s]')
    group.add_argument('--memcpy-nt', action='store_true',
                       help='Use non-temporal memcpy [no]')
    group.add_argument('--mem-max-free', type=int, default=12,
                       help='Maximum free memory buffers [%(default)s]')
    group.add_argument('--mem-initial', type=int, default=8,
                       help='Initial free memory buffers [%(default)s]')
    master.add_argument('host')
    master.add_argument('port', type=int)
    slave = subparsers.add_parser('slave')
    slave.add_argument('port', type=int)

    args = parser.parse_args()
    logging.basicConfig(level=getattr(logging, args.log.upper()))
    if 'host' in args:
        task = run_master(args)
    else:
        task = run_slave(args)
    task = trollius.ensure_future(task)
    trollius.get_event_loop().run_until_complete(task)
    task.result()
Exemple #50
0
logger = logging.getLogger('uweclang')
sh = logging.StreamHandler()
sh.setFormatter(logging.Formatter(
    '[%(name)s] %(filename)s:%(lineno)d at %(asctime)s: %(levelname)s %(message)s',
    '%H:%M:%S'
))
logger.addHandler(sh)
logger.setLevel(logging.DEBUG)

@asyncio.coroutine
def factorial(name, number):
    f = 1
    for i in range(2, number+1):
        print("Task %s: Compute factorial(%s)..." % (name, i))
        logger.debug('test message')
        yield From(asyncio.sleep(1))
        f *= i
    print("Task %s: factorial(%s) = %s" % (name, number, f))


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    tasks = [
        asyncio.ensure_future(factorial("A", 2)),
        asyncio.ensure_future(factorial("B", 3)),
        asyncio.ensure_future(factorial("C", 4))]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    logging.shutdown()
    print('Done')
Exemple #51
0
def find_cubes(r):
    try:
        search_task = None
        while True:
            if r.break_beams.blocked:
                log.info("break beams were broken")
                yield From(run_picking_up_cubes(r, run_avoiding_walls(r, r.drive.go_distance(6))))

            yield From(pick_up_cubes(r))

            try:
                yield From(asyncio.get_event_loop().run_in_executor(None, v.update))
            except IOError:
                continue
            m.update_cubes_from(v)
            cube = v.nearest_cube()
            #print cube

            # start scanning for cubes
            if cube is None:
                if search_task is None:
                    log.info('No cubes in view - scanning')
                    search_task = asyncio.ensure_future(search_for_cubes(r))
                continue

            # we found a cube - stop scanning
            if search_task:

                # clean up the old task
                search_task.cancel()
                while not search_task.done():
                    yield
                try:
                    search_task.result()
                except asyncio.CancelledError:
                    pass

                search_task = None
                log.info('Stopped scanning')

            if abs(cube.angle_to) > np.radians(10):
                log.debug("Turning {} to {}".format(cube.angle_to, cube))
                yield From(r.drive.turn_angle(cube.angle_to))
            else:
                log.debug("Going {}in to {}".format(cube.distance, cube))
                # limit distance
                to_go = cube.pos2
                if cube.distance > 60:
                    to_go = to_go * 60 / cube.distance

                # transform to world space
                to_go = np.append(to_go, 1)
                dest = r.drive.odometer.val.robot_matrix.dot(to_go)

                try:
                    yield From(
                        run_picking_up_cubes(r, run_avoiding_walls(r, 
                            r.drive.go_to(dest[:2], throw_timeout=True)
                        ))
                    )
                except asyncio.TimeoutError:
                    log.warn("Driving task timed out and we caught it")
                    if util.close_to_wall(r):
                        # TODO: Make smarter thing than just moving arbitrarily
                        Drive.go_distance(r.drive, -1)
                        yield From(r.drive.turn_angle(np.radians(45)))

    finally:
        if search_task:
            search_task.cancel()
Exemple #52
0
	decayer = Decayer(0.25, 1.5, 8)
	while True:
		try:
			coro = yield from loop.create_connection(wsFactory, host, port)
		except OSError:
			delay = decayer.decay()
			printToReal(
				"Could not connect to ws://{}:{}, retrying in {:.1f} seconds..."
					.format(host, port, delay))
			yield from asyncio.sleep(delay)
		else:
			printToReal("Connected to ws://{}:{}".format(host, port))
			break

loop = asyncio.get_event_loop()
asyncio.ensure_future(connectToServer())

def gracefulStopCallback():
	printToReal("\n^C detected, creating 'stop' file, please wait for exit...")
	with open(os.path.join(workingDir, "stop"), "wb") as f:
		pass

def forcefulStopCallback():
	loop.stop()

loop.add_signal_handler(signal.SIGINT, gracefulStopCallback)
loop.add_signal_handler(signal.SIGTERM, forcefulStopCallback)


igsetCache = {}
def getPatternsForIgnoreSet(name):
Exemple #53
0
	decayer = Decayer(0.25, 1.5, 8)
	while True:
		try:
			yield from loop.create_connection(ws_factory, host, port)
		except OSError:
			delay = decayer.decay()
			print_to_real(
				"Could not connect to ws://{}:{}, retrying in {:.1f} seconds..."
					.format(host, port, delay))
			yield from asyncio.sleep(delay)
		else:
			print_to_real("Connected to ws://{}:{}".format(host, port))
			break

loop = asyncio.get_event_loop()
asyncio.ensure_future(connect_to_server())


def graceful_stop_callback():
	print_to_real("\n^C detected, creating 'stop' file, please wait for exit...")
	with open(os.path.join(working_dir, "stop"), "wb") as f:
		pass


def forceful_stop_callback():
	loop.stop()


try:
	loop.add_signal_handler(signal.SIGINT, graceful_stop_callback)
	loop.add_signal_handler(signal.SIGTERM, forceful_stop_callback)
Exemple #54
0
	def on_close(self, was_clean, code, reason):
		self.factory.client = None
		print_to_real(
			"Disconnected from ws:// server with (was_clean, code, reason): {!r}"
				.format((was_clean, code, reason)))
		asyncio.ensure_future(connect_to_server())
        if r.status_code != 200:
            print "{0} {1}: {2} {3} {4}".format(url, time.strftime("%X %x %Z"), r.status_code, r.elapsed, r.text)
        else:
            if r.elapsed.total_seconds() > 1.0 or random.randint(0, 100) < 10:
                print "{0} {1}: {2} {3}".format(
                    url, time.strftime("%X %x %Z"), r.status_code, r.elapsed.total_seconds()
                )
        yield From(trollius.sleep(int(delay)))


if __name__ == "__main__":
    usage = """usage: %prog [options]
        """
    parser = argparse.ArgumentParser()
    parser.add_argument("-u", "--url", dest="url", action="append", default=[], help="the url to request")
    parser.add_argument("-d", "--delay", dest="delay", default=2, help="the delay between requests")

    options = parser.parse_args()

    if not options.url or len(options.url) == 0:
        parser.error("-u is required")

    loop = trollius.get_event_loop()
    tasks = []
    for url in options.url:
        tasks.append(trollius.ensure_future(request_every(url, options.delay)))

    loop.run_until_complete(trollius.wait(tasks))
    loop.close()
Exemple #56
0
    def execute_queries_async(
        self,
        queries,
        definitions=None,
        meta_filter=None,
        exclude_modified=False):
        """Runs the given queries on the corpus asynchronously.

        Arguments:
            queries ([Query]): The queries to run.
            definitions (dict): A dictionary defining query terms.
            meta_filter (dict -> bool): A function taking file meta data and
                returning whether the file should be queried.
            exclude_modified (bool): Whether to exclude modified files from
                the query.

        Returns:
            [Result]: An iterator producing the results of the query.
        """
        log = logging.getLogger('uweclang.corpus.manager')
        results = []

        # Get filtered files from corpus.
        try:
            files = self.files(
                meta_filter=meta_filter,
                exclude_modified=exclude_modified)
        except Exception as e:
            raise CorpusException(e)

        status = {
            'completed' : 0,
            'total': 0,
        } # Dictionary needed since `nonlocal` is not in Python 2.7.

        log.debug('Executing query batch (async.)')
        # Function for searching a single file.
        def query_file(meta, tagged, index):
            results = []
            # Extract TaggedToken list from file.
            text = list(chain.from_iterable(uweclang.read_tagged_string(tagged)))
            # Execute search.
            try:
                for i, query in enumerate(queries):
                    res = query.match(text, source_id=index, definitions=definitions)
                    if res:
                        results.extend(res)
            except Exception as e:
                raise QueryExecutionError(e)

            # Update status variables.
            status['completed'] += 1
            log.debug('Completed file %d', index)
            percent = int(status['completed'] / status['total'] * 100)
            log.info('%d%% complete', percent)
            return results

        # Worker function for running a file search.
        @asyncio.coroutine
        def worker(meta, tagged, index):
            log.debug('Starting file %d', index)
            return loop.run_in_executor(None, query_file, meta, tagged, index)

        # Create asynchronous task list.
        loop = asyncio.get_event_loop()
        tasks = []
        for index, (meta, tagged) in enumerate(files):
            log.debug('Added task %d', index)
            tasks.append(asyncio.ensure_future(worker(meta, tagged, index)))

        # Run tasks.
        status['total'] = len(tasks)
        log.info('Starting %d tasks.', status['total'])
        data = loop.run_until_complete(asyncio.gather(*tuple(tasks)))

        # Shutdown event loop and logger.
        loop.close()
        logging.shutdown()

        results = (task.result() for task in tasks if task.result())
        return chain.from_iterable(results)
        print("%s set to %s" % (message.name, message.force))
        publisher.publish(message)

fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

print("Making event loop")
loop = trollius.get_event_loop()

print("Connecting")
future = trollius.futures.Future()
trollius.ensure_future(connect(future))
loop.run_until_complete(future)
publisher = future.result()

print("Processing keys: Q to exit")
loop.add_reader(fd, process_keys)
try:
    loop.run_forever()
except KeyboardInterrupt as e:
    pass

print("Cleaning up")
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)