Пример #1
0
def schema_run(schema_conn):
# Running a schema which AA runs on thought a session
    writeFile(PROGRAM_LOG_PATH, "%s, ***** Staring of the program.\n\n"%get_time_stamp())
    chk_resource_usage(PROGRAM_LOG_PATH) # check the resource (cpu, memory) usage
    last_chk_resource_time = time()

    flag_session_changed_to_off = False
    flag_session_changed_to_on = False
    while True:
        if time()-last_chk_resource_time > 60 * 30: # every a half an hour
            chk_resource_usage(PROGRAM_LOG_PATH) # check the resource (cpu, memory) usage
            last_chk_resource_time = time()

        # If the session time is over, archive all the data
        # If the session time is resumed, update the log-file and csv-result-file.
        if chk_session_time(SESSION_START_HOUR, SESSION_END_HOUR) == False:
            if flag_session_changed_to_off == False: # 1st run after finishing the session-time
                if AAS.feeding_snd_play_time != -1: # if it's middle of a trial
                    AAS.terminate_trial() # terminate it.

                if flag_session_changed_to_on == True:
                # The session was on before
                # If this is True here, this means it's the very 1st time after staring the program.
                # Therefore, there will be nothing to archive.
                    print '*** The session is over.'
                    uvcInst = uvc.VideoConverter()
                    uvcInst.run()
                    #ustInst = ust.WaveFileRecognizer(OUTPUT_PATH)
                    #ustInst.run()
                    umdInst = umd.M_drawer(OUTPUT_PATH)
                    umdInst.run()
                    folder_name = get_time_stamp()[:10]
                    archive_folder_path = os.path.join('archive', folder_name)
                    umf.run(folder_name, archive_folder_path)
                    print '-------------------------------------------------------------'
                    print 'Archiving process is finished at %s.'%get_time_stamp()
                    print '-------------------------------------------------------------'

                flag_session_changed_to_off = True
                flag_session_changed_to_on = False

            if schema_conn.poll(): # check whether there's any message arrive through the pipe
                msg = schema_conn.recv() # receive the message
                if msg == 'q': break
            sleep(1)
            continue
        else:
            if flag_session_changed_to_on == False: # 1st run after starting the session-time
                print '-------------------------------------------------------------'
                print 'Session starts at %s.'%get_time_stamp()
                print '-------------------------------------------------------------'
                flag_session_changed_to_off = False
                flag_session_changed_to_on = True
                AAS.session_initialization() # Initialize the log-file and result-file

        msg = ""
        if AAO.main_msgB_conn.poll(): # any message from message board
            msg = AAO.main_msgB_conn.recv() # receive
        AAS.run(msg)

        ### msg checking for exiting the program
        if schema_conn.poll(): # check whether there's any message arrive through the pipe
            msg = schema_conn.recv() # receive the message
            if msg == 'q': break
Пример #2
0
def msgB_run(msgB_conn):
# 'msgB' is for sending/recieving messages from/to Arduino-chip and Video module
# specifically for 'scheduled_feeding' part of AAS.
# * Message from Arduino chip can be lost or incomplete.
# * Messaging with VM is only for organization purpose.

    flag_chk_ARD_msg = False # checking message from Arduino-chip ?
    flag_chk_vm_msg = False # checking message from the video module ?
    vm_msg_chk_start = None # time when the VM message check started
    msg_from_Arduino = ""
    msg_from_vm = ""
    while True:
        if chk_session_time(SESSION_START_HOUR, SESSION_END_HOUR) == False:
            if msgB_conn.poll(): # check whether there's any message arrive through the pipe
                msg = msgB_conn.recv() # receive the message
                if msg == 'q': break
            sleep(1)
            continue

        ### Chekcing Arduino-chip message
        if flag_chk_ARD_msg == True:
            msg = ARD_M.receive('button', 0.1) # Try to receive any 'button'-msg for 0.1 second.
            if msg == None: msg_from_Arduino = ""
            else: msg_from_Arduino = str(msg).replace("[","").replace("]","").replace("'","").replace(" ","")

        ### Checking Video-module message (VM -> AAO)
        if flag_chk_vm_msg == True:
            for i in range(NUMBER_OF_CAMERAS):
                if AAO.main_vi_conn[i].poll():
                    msg_from_vm = AAO.main_vi_conn[i].recv() # receive the message

        if flag_chk_ARD_msg == True and msg_from_Arduino != "":
            msgB_conn.send("ARDUINO:" + msg_from_Arduino)
            msg_from_Arduino = ""

        ### As soon as a message arrived from VM or TRIAL_LENGTH is up, finish checking.
        if flag_chk_vm_msg == True:
            if msg_from_vm != "":
                msgB_conn.send("V:" + msg_from_vm)
                flag_chk_vm_msg = False
                msg_from_vm = ""
            elif time() - vm_msg_chk_start > TRIAL_LENGTH:
                flag_chk_vm_msg = False

        ### msg checking for the msgB. (AAO or AAS -> msgB)
        if msgB_conn.poll(): # check whether there's any message arrive through the pipe
            msg = msgB_conn.recv() # receive the message

            if msg.startswith('chk_ARD_msg:'):
                msg = msg.split(":")[1]
                if msg == 'start':
                    flag_chk_ARD_msg = True

                    ### initialize message. (get rid of possible remained message)
                    msg_from_Arduino = ''
                    msg = ARD_M.receive('button', 0.1) # Try to receive any 'button'-msg for 0.1 second.
                    msg = ''
                    
                    ARD_M.send('chk_b_start') # Start checking the button-press in Arduino-chip
                elif msg == 'end':
                    flag_chk_ARD_msg = False
                    ARD_M.send('chk_b_end') # Stop checking the button-press in Arduino-chip
            elif msg.startswith('chk_vm_msg:'):
                msg = msg.split(":")[1]
                if msg == 'start':
                    ### initialize the 'msg_from_vm'
                    for i in range(NUMBER_OF_CAMERAS):
                        if AAO.main_vi_conn[i].poll():
                            msg_from_vm = AAO.main_vi_conn[i].recv() # receive the message
                    msg_from_vm = ''

                    flag_chk_vm_msg = True
                    for i in range(NUMBER_OF_CAMERAS): AAO.main_vi_conn[i].send('check_motion_at_feeder')
                    vm_msg_chk_start = time()
            elif msg == 'q': break

        sleep(0.05)