Example #1
0
async def quit(agent: Agent) -> None:
    while agent.working():
        _, mess = await agent.fetch_from_observer()
        if mess == "session end":
            agent.finish()
            break
    return None
Example #2
0
async def stimulate(agent: Agent, ino: Arduino, led: int, reward: int,
                    intervals: List[float], blackout: float,
                    max_trial: int) -> None:
    global trial
    _ = await agent.recv_from_observer()
    print(f"{agent.addr} started")
    events.append((perf_counter(), 1))
    try:
        for interval in intervals:
            ino.digital_write(led, LIGHT_HIGH)
            events.append((perf_counter(), led))
            await agent.sleep(interval)
            agent.send_to(CAMAERA_ADDR, "is mouse in the box?")
            print(f"reward is set in {agent.addr}")
            while True:
                _, mess = await agent.recv()
                if mess:
                    break
            ino.digital_write(led, LIGHT_LOW)
            ino.digital_write(reward, HIGH)
            await agent.sleep(0.1)
            ino.digital_write(reward, LOW)
            events.append((perf_counter(), reward))
            trial += 1
            if trial >= max_trial:
                break
            await agent.sleep(blackout)
        events.append((perf_counter(), 0))
        if agent.working():
            agent.send_to(OBSERVER, "session terminated")
    except NotWorkingError:
        pass
    print(f"{agent.addr} stopped")
    ino.digital_write(led, LIGHT_LOW)
    return None
Example #3
0
async def quit(agent: Agent) -> None:
    await agent.sleep(7.5)
    while agent.working():
        print(f"{agent.addr} await message from observer")
        _, mess = await agent.recv_from_observer()
        print(f"{agent.addr} recv {mess} from observer")
        if mess == "session end" or mess == "session terminated":
            agent.finish()
            break
    print(f"{agent.addr} quited")
    return None
Example #4
0
async def sort(agent: Agent) -> None:
    while agent.working():
        try:
            await agent.sort_mail()
        except NotWorkingError:
            pass
    return None
Example #5
0
async def light(agent: Agent, ino: Arduino) -> None:
    await agent.fetch_from_observer()
    while agent.working():
        await agent.send_to("recorder", "is bright")
        _, mess = await agent.fetch_from_others()
        if mess:
            ino.digital_write(13, HIGH)
            continue
        ino.digital_write(13, LOW)
    return None
Example #6
0
async def check_message(agent: Agent) -> None:
    while agent.working():
        try:
            mail = await agent.recv(timeout=2)
            if mail is None:
                continue
            sender, mess = mail
            print(f"{mess} from {sender}")
        except NotWorkingError:
            pass
    return None
Example #7
0
async def record(agent: Agent, cap: cv.VideoCapture) -> None:
    await agent.fetch_from_observer()
    widht = cap.get(cv.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)
    num_pixel = widht * height
    while agent.working():
        ret, frame = cap.read()
        if not ret:
            continue
        gframe = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
        _sum = np.sum(gframe) / num_pixel
        if _sum >= 50:
            await agent.send_to("lighter", True)
        else:
            await agent.send_to("lighter", False)
        cv.imshow("frame", frame)
        if cv.waitKey(1) & 0xFF == ord("q"):
            break

    await agent.send_to(OBSERVER, "session end")
    cv.destroyAllWindows()
    return None
Example #8
0
    from amas.connection import Register
    from pino.ino import Comport

    com = Comport() \
        .set_inofile("~/Github/GrudgeDuck/pino/example/proto.ino") \
        .set_baudrate(115200) \
        .set_port("/dev/ttyACM0") \
        .deploy() \
        .connect()

    ino = Arduino(com)

    cap = cv.VideoCapture(0)

    lighter = Agent("lighter") \
        .assign_task(light, ino=ino) \
        .assign_task(sort) \
        .assign_task(quit)
    recorder = Agent("recorder") \
        .assign_task(record, cap=cap) \
        .assign_task(sort) \
        .assign_task(quit)
    observer = Observer() \
        .assign_task(sort) \
        .assign_task(kill)

    rgist = Register([recorder, observer, lighter])
    env = Environment([recorder, observer, lighter])

    try:
        env.parallelize()
        env.join()
Example #9
0
async def send_after(agent: Agent, t: int, mess: str) -> None:
    while agent.working():
        await asyncio.sleep(t)
        await agent.send_to("bar", mess)
    return None
Example #10
0
async def quit(agent: Agent) -> None:
    while agent.working():
        _, mess = await agent.recv_from_observer()
        agent.finish()
    return None
Example #11
0

async def quit(agent: Agent) -> None:
    while agent.working():
        _, mess = await agent.recv_from_observer()
        agent.finish()
    return None


async def kill(agent: Observer) -> None:
    await asyncio.sleep(5)
    await agent.send_all("q")


if __name__ == '__main__':
    a1 = Agent("foo")
    a2 = Agent("bar")
    a3 = Observer()

    rgist = Register([a1, a2, a3])

    a1.assign_task(send_after, t=1, mess="hello").assign_task(quit)
    a2.assign_task(check_message).assign_task(quit)
    a3.assign_task(kill)

    a1.apply_args(t=1, mess="hello")

    t = perf_counter()
    env = Environment([a1, a2, a3])
    env.run()
    print(perf_counter() - t)
Example #12
0
    blackn = SESSION_DURATION // BLACK_INTERVAL
    black_intervals = init_table(BLACK_INTERVAL, blackn)
    whiten = SESSION_DURATION // WHITE_INTERVAL
    white_intervals = init_table(BLACK_INTERVAL, whiten)

    now = datetime.datetime.now().strftime("%m%d%y")
    basename = "-".join([SUBJECT, CONDITION, now])
    fpath = join("data", basename)
    # fourcc = cv.VideoWriter_fourcc(*"mp4v")
    # videoname = fpath + ".MP4"
    # output = cv.VideoWriter(videoname, fourcc, 30.0, (1280, 480))

    lstim = Agent(BLACK_STIM_ADDR) \
        .assign_task(stimulate, ino=ino, led=BLACK_LED,
                     reward=BLACK_REWARD, intervals=black_intervals,
                     blackout=BLACKOUT, max_trial=NUM_TRIAL) \
        .assign_task(quit)

    rstim = Agent(WHITE_STIM_ADDR) \
        .assign_task(stimulate, ino=ino, led=WHITE_LED,
                     reward=WHITE_REWARD, intervals=white_intervals,
                     blackout=BLACKOUT, max_trial=NUM_TRIAL) \
        .assign_task(quit)

    lcap = cv.VideoCapture(LCAM)
    rcap = cv.VideoCapture(RCAM)

    recorder = Recorder(CAMAERA_ADDR) \
        .assign_task(record, lcap=lcap, rcap=rcap) \
        .assign_task(asked) \