Exemplo n.º 1
0
 def load(self):
     '''
     Loads up the settings from gado.conf and instantiates objects for:
         
         The Scanner
         The Robot
         The Webcam
     '''
     
     self.load_settings()
     
     self.scanner = Scanner(**self.s)
     self.robot = Robot(**self.s)
     self.camera = Webcam(**self.s)
Exemplo n.º 2
0
 def _sanity_checks(self):
     '''
     Runs a series of checks on the connected peripherals and other components
     of the Gado system
     
     If any of these tests fail, then the Gui thread is notified as to what didn't work so
     the user can take appropriate action
     
     If all the tests pass, then the system is ready to proceed with the scanning process
     '''
     
     self.logger.debug('in sanity checks')
     
     #If the scanning process has started, and a user attempts to start it again
     #Tell the gui to display a message stating that
     if self.started:
         add_to_queue(self.q_out, messages.DISPLAY_ERROR,
             'The scanning process has already started')
         return False
     
     _a = 'a'
     self.started = id(_a)
     
     #User did not select an artifact set from the dropdown
     if not self.selected_set:
         self.logger.error('failed sanity check on selected_set')
         add_to_queue(self.q_out, messages.DISPLAY_ERROR,
             'Please select an artifact set from the dropdown.')
         self.started = False
         return False
     
     #Robot is not currently connected
     if not self.robot.connected():
         self.logger.error('failed sanity check on robot.connected()')
         
         #Try and connect
         self.connect()
         
         #If that failed, let the Gui know so it can tell the user
         if not self.robot.connected():
             self.logger.error("COMPLETELY FAILED ON robot.connected()")
             add_to_queue(self.q_out, messages.DISPLAY_ERROR,
                 'Unable to connect to the robot. Try pressing the reset button and then unplugging it and replugging it.')
             self.started = False
             return False
     
     #Otherwise, let the user know that the connection was successfull
     add_to_queue(self.q_out, messages.SET_STATUS_TEXT, 'Connected to the Gado')
     
     #Reset the robot to its initial location
     self.robot.reset()
     
     #If the scanner is not currently connected
     if not self.scanner.connected():
         self.logger.error('failed sanity check on scanner.connected(), retrying')
         
         #Delete the scanner object (if any)
         try: del self.scanner
         except:
             self.logger.exception("Error while connecting to scanner")
             pass
         
         #Reinstantiate the object with the user settings
         self.scanner = Scanner(**import_settings())
         
         #If the scanner is still not connected to the system
         if not self.scanner.connected():
             self.logger.error('failed sanity check on scanner.connected()')
             
             #Let the Gui thread know that the scanner could not be found
             self.started = False
             add_to_queue(self.q_out, messages.DISPLAY_ERROR,
                 'Unable to connect to the scanner. Try unplugging it and replugging it. You may need to restart this application or run the setup wizard again.')
             return False
     
     #Let the Gui thread know that the scanner was found so it can alert the user
     add_to_queue(self.q_out, messages.SET_STATUS_TEXT, 'Connected to the scanner')
     
     #Webcam is not currently connected
     if not self.camera.connected():
         
         #Attempt to connect to it
         self.camera.connect()
         
         #If that fails
         if not self.camera.connected():
             self.logger.error('failed sanity check on camera.connected()')
             
             #Let the Gui thread know that it is not connected so it can alert the user
             add_to_queue(self.q_out, messages.DISPLAY_ERROR,
                 'Unable to connect to the webcam. Try unplugging it and replugging it. You may need to restart this application or run the setup wizard again.')
             self.started = False
             return False
         
     #Let the Gui thread know that the webcam is connected
     add_to_queue(self.q_out, messages.SET_STATUS_TEXT, 'Connected to the webcam')
     
     #Move the robot's actuator up to its starting position
     self.robot._moveActuator(self.robot.actuator_up_value)
     
     #Allow some time for the movement to happen
     time.sleep(2)
     
     #Something strange happened, let the Gui thread know
     if self.started != id(_a):
         add_to_queue(self.q_out, messages.DISPLAY_ERROR, 'An unknown error has occurred. Try restarting this application')
         self.started = False
         return False
     
     #Sanity checks all passed, let the Gui thread know that the system is ready to proceed
     add_to_queue(self.q_out, messages.SET_STATUS_TEXT, 'Ready to begin scanning')
     
     return True
Exemplo n.º 3
0
    def mainloop(self):
        '''
        Repeating loop that constantly checks to see if the in-queue (the Gui) has any new
        messages for the system
        
        A Series of if/elses is then used to determine the appropriate action to take for
        each message received
        '''
    
        #Set database interface, the system-to-gui queue and the robot as local vars
        dbi = self.dbi
        q = self.q_out
        robot = self.robot
        
        #add_to_queue(q, messages.READY)
        
        #Loop until told to stop
        while True:
            expecting_return = False
            try:
                #Grab a message from the gui-to-system queue (if on exists)
                msg = fetch_from_queue(self.q_in)
                
                #Add an new artifact set to the local database
                if msg[0] == messages.ADD_ARTIFACT_SET_LIST:
                    i = dbi.add_artifact_set(**msg[1])
                
                #Grab the full lits of artifact sets and pass it back
                #using the system-to-gui queue
                elif msg[0] == messages.ARTIFACT_SET_LIST:
                    expecting_return = messages.ARTIFACT_SET_LIST
                    i = dbi.artifact_set_list()
                    add_to_queue(q, messages.ARTIFACT_SET_LIST, i)
                
                #Delete a specified artifact set
                elif msg[0] == messages.DELETE_ARTIFACT_SET_LIST:
                    dbi.delete_artifact_set(msg[1])
                
                #Grab the user's settings and either update the settings for
                #the robot, scanner and webcam; or reinstantiate those objects
                #with the new setttings
                elif msg[0] == messages.RELOAD_SETTINGS:
                    settings = import_settings()
                    self.s = settings
                    
                    self.robot.updateSettings(**settings)
                    
                    del self.camera
                    self.camera = Webcam(**settings)
                    
                    del self.scanner
                    self.scanner = Scanner(**settings)
                    
                    self._load_settings(**settings)
                
                #Restart the robot
                elif msg[0] == messages.RESET:
                    self.robot.reset()
                
                #Attempt to connect to the robot (via serial port)
                #Pass back to Gui whether or not it was sucessfull
                elif msg[0] == messages.ROBOT_CONNECT:
                    expecting_return = messages.ROBOT_CONNECT
                    success = self.connect()
                    self.logger.info('ROBOT_CONNECT: %s' % success)
                    add_to_queue(q, messages.ROBOT_CONNECT, robot.connected())
                
                #Delete the current scanner object (if it exists)
                #and instantiate a new one
                #
                #Pass back to Gui whether or not this was sucessfull
                elif msg[0] == messages.SCANNER_CONNECT:
                    expecting_return = messages.SCANNER_CONNECT
                    try: del self.scanner
                    except:
                        self.logger.exception("Exception while connecting to scanner")
                        pass
                    self.scanner = Scanner(**import_settings())
                    add_to_queue(q, messages.SCANNER_CONNECT, self.scanner.connected())
                
                elif msg[0] == messages.SCANNER_CONNECTED:
                    add_to_queue(q, messages.SCANNER_CONNECTED, self.scanner.connected())
                    
                #Scan an inmage using the scanner
                elif msg[0] == messages.SCANNER_PICTURE:
                    expecting_return = messages.SCANNER_PICTURE
                    self.scanner.scanImage(self.s['scanned_image'])
                    add_to_queue(q, messages.SCANNER_PICTURE, self.s['scanned_image'])

                #Set the passed in artifact set as the currently selected
                elif msg[0] == messages.SET_SELECTED_ARTIFACT_SET:
                    self.selected_set = msg[1]

                #Starts the robot's overall procedure for moving/scanning artifacts
                elif msg[0] == messages.START:
                    self.start()
                
                #Grabs a list of available webcams and returns them to the Gui thread
                elif msg[0] == messages.WEBCAM_LISTING:
                    self.logger.debug('WEBCAM_LISTING')
                    expecting_return = messages.WEBCAM_LISTING
                    
                    opts = self.camera.options()
                    self.logger.debug('WEBCAM_LISTING - %s' % opts)
                    add_to_queue(q, messages.WEBCAM_LISTING, opts)
                
                #Attempt to connect to the selected webcam
                elif msg[0] == messages.WEBCAM_CONNECT:
                    self.logger.debug('WEBCAM_CONNECT switch made it')
                    expecting_return = messages.WEBCAM_CONNECT
                    
                    #If the webcam object exists
                    if self.camera:
                        self.logger.info('Camera already exists')
                        #And it is connected
                        if self.camera.connected():
                            self.logger.info('Already connected to the webcam')
                            
                            #Relay that information back to the Gui thread
                            add_to_queue(q, messages.WEBCAM_CONNECT, self.camera.connected())
                            return
                        #Otherwise, disconnect
                        else: self.camera.disconnect()
                        
                    #Reinstantiate the webcam object with the user settings
                    self.camera = Webcam(**import_settings())
                    
                    #Connect to webcam and let the Gui thread know if it was sucessfull
                    self.logger.info('self.camera.connected() %s' % self.camera.connected())
                    add_to_queue(q, messages.WEBCAM_CONNECT, self.camera.connected())
                
                elif msg[0] == messages.WEBCAM_CONNECTED:
                    add_to_queue(q, messages.WEBCAM_CONNECTED, self.camera.connected())
                    
                #Take a picture using the currently connected webcam
                elif msg[0] == messages.WEBCAM_PICTURE:
                    expecting_return = messages.WEBCAM_PICTURE
                    
                    #Save the picture as the temp image name
                    self.camera.savePicture(self.s['webcam_image'])
                    
                    #Pass that name back to the Gui thread
                    add_to_queue(q, messages.WEBCAM_PICTURE, self.s['webcam_image'])
                    
                #Grab a list of artifact sets, with weights so we can figure out the most
                #recently used
                elif msg[0] == messages.WEIGHTED_ARTIFACT_SET_LIST:
                    expecting_return = messages.WEIGHTED_ARTIFACT_SET_LIST
                    li = self.dbi.weighted_artifact_set_list()
                    add_to_queue(q, messages.WEIGHTED_ARTIFACT_SET_LIST, arguments=li)
                
                #Kill the program!!!
                elif msg[0] == messages.MAIN_ABANDON_SHIP:
                    add_to_queue(q, messages.GUI_LISTENER_DIE)
                    add_to_queue(self.q_in, messages.MAIN_ABANDON_SHIP)
                    return
                
                elif msg[0] == messages.STOP or msg[0] == messages.LAST_ARTIFACT or msg[0] == messages.RESET:
                    # These commands are only relevant if the robot is already running.
                    pass
                
                #Pass the robot object to the Gui thread
                elif msg[0] == messages.GIVE_ME_A_ROBOT:
                    add_to_queue(q, messages.GIVE_ME_A_ROBOT, self.robot)
                else:
                    # add it back to the in queue, was somebody else waiting for that message?
                    add_to_queue(self.q_in, msg[0], (msg[1] if len(msg) > 1 else None))
            
            #Something went horribly wrong, if the Gui thread is expecting a response,
            #Tell it things went poorly
            except:
                self.logger.exception("EXCEPTION GENERATED")
                #raise
                if expecting_return:
                    add_to_queue(q, expecting_return)