def __init__(self):

        # motion control settings
        self.x_set = 0.5
        self.y_set = 0.5
        self.r_set = 0.5

        self.zoom_step = 20.
        self.focus_step = 20.
        self.no_powerzoom = False

        self.x_current = 0.0
        self.y_current = 0.0
        self.r_current = 0.0
        self.d_current = 0.0
        self.rp_current = 0.0
        self.pixels_per_mm_current = 0.0
        self.zoom_current = 0.0
        self.focus_current = 0.0

        # led settings
        self.IsetCh1 = 0.0
        self.IsetCh2 = 0.0
        self.IsetCh3 = 0.0
        self.IsetCh4 = 0.0

        # camera settings
        self.test_binning = 3
        self.binning_factor = 1
        self.gain_factor = 1.

        self.camera_device = None

        self.camera_locked = 0
        self.continuously_acquiring = 0
        self.frame_rate = 0

        # MWorks communication, if available
        self.mw_conduit = None

        # UI
        self.canvas_update_timer = None
        self.ui_queue = queue.Queue(5)

        #self.im_queue = queue.Queue(5) # NEW
        #logging.info("ui_queue and im_queue created")

        self.target_ui_update_interval = 1 / 30.
        self.last_ui_get_time = time.time()
        self.last_ui_put_time = time.time()

        self.enable_save_to_disk = global_settings.get('enable_save_to_disk',
                                                       True)
        logging.info("Save to disk?: %d" % self.enable_save_to_disk)
        self.image_save_dir = global_settings.get('data_dir',
                                                  '~/.camera_capture/data')

        self.use_simulated = global_settings.get('use_simulated', False)
        self.use_file_for_cam = global_settings.get('use_file_for_camera',
                                                    False)
    def __init__(self):

        # motion control settings
        self.x_set = 0.5
        self.y_set = 0.5
        self.r_set = 0.5

        self.zoom_step = 20.
        self.focus_step = 20.
        self.no_powerzoom = False

        self.x_current = 0.0
        self.y_current = 0.0
        self.r_current = 0.0
        self.d_current = 0.0
        self.rp_current = 0.0
        self.pixels_per_mm_current = 0.0
        self.zoom_current = 0.0
        self.focus_current = 0.0

        # led settings
        self.IsetCh1 = 0.0
        self.IsetCh2 = 0.0
        self.IsetCh3 = 0.0
        self.IsetCh4 = 0.0

        # camera settings
        self.test_binning = 3
        self.binning_factor = 1
        self.gain_factor = 1.

        self.camera_device = None

        self.camera_locked = 0
        self.continuously_acquiring = 0
        self.frame_rate = 0


        # MWorks communication, if available
        self.mw_conduit = None


        # UI
        self.canvas_update_timer = None
        self.ui_queue = queue.Queue(5)
        
        #self.im_queue = queue.Queue(5) # NEW
        #logging.info("ui_queue and im_queue created")

        self.target_ui_update_interval = 1 / 30.
        self.last_ui_get_time = time.time()
        self.last_ui_put_time = time.time()

        self.enable_save_to_disk = global_settings.get('enable_save_to_disk', True)
        logging.info("Save to disk?: %d" % self.enable_save_to_disk)
        self.image_save_dir = global_settings.get('data_dir', '~/.camera_capture/data')

        self.use_simulated = global_settings.get('use_simulated', False)
        self.use_file_for_cam = global_settings.get('use_file_for_camera',
                                                    False)
    def load_calibration_parameters(self, filename):
        logging.info("Loading: %s" % filename)
        d = None
        with open(filename, 'r') as f:
            d = pkl.load(f)

        if d is None:
            logging.warning('Error loading calibration file %s' % filename)
            return False

        candidate_pixels_per_mm = d['pixels_per_mm']

        # check to see if the pixels_per_mm matches up
        if global_settings.get('check_pixels_per_mm_when_loading', False):
            logging.warning('CHECKING pixels_per_mm as sanity check')
            self.execute_calibration_step(lambda x: self.calibrator.center_horizontal(), True)
            tolerance = global_settings.get('pixels_per_mm_tolerance', 0.01)
            deviation = (abs(self.calibrator.pixels_per_mm - candidate_pixels_per_mm) / self.calibrator.pixels_per_mm)
            if  deviation > tolerance:
                logging.warning('Calibration is not consistent with apparent pixels/mm')
                logging.warning('(loaded=%f, measured=%f' % (candidate_pixels_per_mm, self.calibrator.pixels_per_mm))
                logging.warning('deviation=%f, tolerance=%f' % (deviation, tolerance))
                return False
            else:
                logging.warning('Calibration is consistent with apparent pixels/mm')
                logging.warning('(loaded=%f, measured=%f' % (candidate_pixels_per_mm, self.calibrator.pixels_per_mm))
                logging.warning('deviation=%f, tolerance=%f' % (deviation, tolerance))

        logging.info('Calibration parameters: %s' % d)
        return self.calibrator.load_parameters(d)
Exemple #4
0
    def load_calibration_parameters(self, filename):
        logging.info("Loading: %s" % filename)
        d = None
        with open(filename, 'r') as f:
            d = pkl.load(f)

        if d is None:
            logging.warning('Error loading calibration file %s' % filename)
            return False

        candidate_pixels_per_mm = d['pixels_per_mm']

        # check to see if the pixels_per_mm matches up
        if global_settings.get('check_pixels_per_mm_when_loading', False):
            logging.warning('CHECKING pixels_per_mm as sanity check')
            self.execute_calibration_step(lambda x: self.calibrator.center_horizontal(), True)
            tolerance = global_settings.get('pixels_per_mm_tolerance', 0.01)
            deviation = (abs(self.calibrator.pixels_per_mm - candidate_pixels_per_mm) / self.calibrator.pixels_per_mm)
            if  deviation > tolerance:
                logging.warning('Calibration is not consistent with apparent pixels/mm')
                logging.warning('(loaded=%f, measured=%f' % (candidate_pixels_per_mm, self.calibrator.pixels_per_mm))
                logging.warning('deviation=%f, tolerance=%f' % (deviation, tolerance))
                return False
            else:
                logging.warning('Calibration is consistent with apparent pixels/mm')
                logging.warning('(loaded=%f, measured=%f' % (candidate_pixels_per_mm, self.calibrator.pixels_per_mm))
                logging.warning('deviation=%f, tolerance=%f' % (deviation, tolerance))

        logging.info('Calibration parameters: %s' % d)
        return self.calibrator.load_parameters(d)
from simple_camera_capture.image_processing import ImageAnalyzer

try:
    import queue
except ImportError:
    import Queue as queue

# load settings
loaded_config = load_config_file('~/.simple_camera_capture/config.ini')
global_settings.update(loaded_config)

# boost.python wrapper for a MWorks interprocess conduit, if you're into
# that sort of thing
mw_enabled = False

if global_settings.get('enable_mw_conduit', True):

    try:
        sys.path.append('/Library/Application Support/MWorks/Scripting/Python')
        import mworks.conduit as mw_conduit
        GAZE_INFO = 100
        TRACKER_INFO = 101
        mw_enabled = True
    except Exception, e:
        logging.warning('Unable to load MW conduit: %s' % e)



# A catch-all class for controlling the eyetracking hardware

class CaptureController(object):
    def __init__(self):

        self.test_binning = 3

        self.x_set = 0.5
        self.y_set = 0.5
        self.r_set = 0.5

        self.zoom_step = 20.
        self.focus_step = 20.

        self.x_current = 0.0
        self.y_current = 0.0
        self.r_current = 0.0
        self.d_current = 0.0
        self.rp_current = 0.0
        self.pixels_per_mm_current = 0.0
        self.zoom_current = 0.0
        self.focus_current = 0.0

        self.d_cntr_set = 0.0
        self.r_cntr_set = 0.0

        self.r_2align_pup_cr = None
        self.pupil_cr_diff = None

        self.IsetCh1 = 0.0
        self.IsetCh2 = 0.0
        self.IsetCh3 = 0.0
        self.IsetCh4 = 0.0

        self.pupil_position_x = 0.0
        self.pupil_position_y = 0.0
        self.pupil_radius = 0.0
        self.cr_position_x = 0.0
        self.cr_position_y = 0.0

        self.pupil_only = False

        self.gaze_azimuth = 0.0
        self.gaze_elevation = 0.0
        self.calibration_status = 0

        self.sobel_avg = 0.0

        self.simulated_eye_x = 0.0
        self.simulated_eye_y = 0.0
        self.simulated_pupil_radius = 0.2

        self.binning_factor = 1
        self.gain_factor = 1.

        # Added by DZ to deal with rigs without power zoom and focus
        self.no_powerzoom = False

        self.pupil_only = False

        self.feature_finder = None

        self.camera_device = None
        self.calibrator = None
        self.mw_conduit = None

        self.canvas_update_timer = None

        self.ui_queue = Queue(2)

        self.camera_locked = 0
        self.continuously_acquiring = 0

        self.calibrating = False

        self.enable_save_to_disk = global_settings.get('enable_save_to_disk', False)
        logging.info("Save to disk?: %s" % self.enable_save_to_disk)
        self.image_save_dir = global_settings.get('data_dir', None)

        self.use_simulated = global_settings.get('use_simulated', False)
        self.use_file_for_cam = global_settings.get('use_file_for_camera',
                                                    False)

        # -------------------------------------------------------------
        # Stages
        # -------------------------------------------------------------

        logging.info('Initializing Motion Control Subsystem (Stages)...')
        if self.use_simulated:
            esp300 = SimulatedStageController()
        else:
            esp300 = ESP300StageController('169.254.0.9', 8001)

            try:
                esp300.connect()
            except Exception as E:
                logging.warning(str(E))
                logging.warning('Attempting to restart serial bridge (this can take ' \
                    + 'several tens of seconds)...')
                try:
                    kick_in_the_pants = httplib.HTTPConnection('169.254.0.9',
                            80, timeout=5)
                    kick_in_the_pants.request('GET', '/goforms/resetUnit?')
                    time.sleep(5)
                    esp300.connect()
                    del kick_in_the_pants
                except Exception:
                    self.simple_alert('Could not connect to serial bridge',
                                      "Attempts to 'kick' the serial bridge "
                                      + 'have failed.  '
                                      + 'Reverting to simulated mode.')
                    esp300 = SimulatedStageController()
                    self.use_simulated = True

        self.stages = EyeTrackerStageController(esp300)

        logging.info('Initializing Motion Control Subsystem (Focus and Zoom)')
        if self.no_powerzoom:
            esp300_2 = SimulatedStageController()
        else:
            if self.use_simulated:
                esp300_2 = SimulatedStageController()
            else:
                esp300_2 = ESP300StageController('169.254.0.9', 8002)
                esp300_2.connect()

        self.zoom_and_focus = FocusAndZoomController(esp300_2)

        self.x_current = self.stages.current_position(self.stages.x_axis)
        self.y_current = self.stages.current_position(self.stages.y_axis)
        self.r_current = self.stages.current_position(self.stages.r_axis)

        self.sobel_avg = 0

        self.r_cntr_set = 0
        self.d_cntr_set = 0

        self.zoom_step = 20.
        self.focus_step = 20.

        self.IsetCh1 = 0
        self.IsetCh2 = 0
        self.IsetCh3 = 0
        self.IsetCh4 = 0

        # Manual aligment of pupil and CR
        self.r_2align_pup_cr = 0

        # -------------------------------------------------------------
        # LED Controller
        # -------------------------------------------------------------
        logging.info('Initializing LED Control Subsystem')

        if self.use_simulated:
            self.leds = SimulatedLEDController(4)
        else:
            self.leds = MightexLEDController('169.254.0.9', 8006)
            self.leds.connect()

        # -------------------------------------------------------------
        # Camera and Image Processing
        # -------------------------------------------------------------

        logging.info('Initializing Image Processing')

        self.features = None
        self.frame_rates = []

        # set up real featutre finders (these won't be used if we use a
        # fake camera instead)

        nworkers = int(global_settings.get('nworkers', 0))
        logging.info("starting with N workers %s" % nworkers)

        self.radial_ff = None
        self.starburst_ff = None

        if nworkers != 0:

            self.feature_finder = PipelinedFeatureFinder(nworkers)
            workers = self.feature_finder.workers

            self.rffs = []
            self.sbffs = []
            for worker in workers:

                fr_ff = worker.FastRadialFeatureFinder()  # in worker process
                sb_ff = worker.StarBurstEyeFeatureFinder()  # in worker process

                #self.radial_ff = fr_ff
                #self.starburst_ff = sb_ff
                self.rffs.append(fr_ff)
                self.sbffs.append(sb_ff)
                # self.radial_symmetry_feature_finder_adaptor.addFeatureFinder(fr_ff)
                # self.starburst_feature_finder_adaptor.addFeatureFinder(sb_ff)

                comp_ff = worker.FrugalCompositeEyeFeatureFinder(fr_ff, sb_ff)
                # self.composite_feature_finder_adaptor.addFeatureFinder(comp_ff)

                worker.set_main_feature_finder(comp_ff)  # create in worker process

            self.radial_ff = ParamExpose(self.rffs, rff_params)
            self.starburst_ff = ParamExpose(self.sbffs, sbff_params)
            self.feature_finder.start()  # start the worker loops
        else:
            sb_ff = SubpixelStarburstEyeFeatureFinder()
            fr_ff = FastRadialFeatureFinder()

            comp_ff = FrugalCompositeEyeFeatureFinder(fr_ff, sb_ff)

            self.radial_ff = fr_ff
            self.starburst_ff = sb_ff

            self.feature_finder = comp_ff

        if self.enable_save_to_disk and self.image_save_dir is not None:
            logging.info('Enabling save to disk...')
            self.feature_finder = ImageSaveDummyFeatureFinder(self.feature_finder, self.image_save_dir)
        else:
            logging.warning('Data will not be saved to disk.')

        if True:
        # try:
            if not self.use_file_for_cam and not self.use_simulated:
                logging.info('Connecting to Camera...')
                self.camera_device = ProsilicaCameraDevice(self.feature_finder)

                self.binning = 4
                self.gain = 1
        # except Exception, e:
        #             print "Unexpected error:", e.message
        #             self.use_file_for_cam = 1

        if self.use_file_for_cam:
            fn = global_settings.get('camera_file', '/Users/davidcox/Desktop/albino2/Snapshot2.bmp')
            self.camera_device = FakeCameraDevice(self.feature_finder, fn)
            self.camera_device.acquire_image()

        if self.use_simulated and self.camera_device == None:
            logging.info('Failing over to Simulated Camera')

            # use a POV-Ray simulated camera + a simpler feature finder
            # that works with it
            self.camera_device = POVRaySimulatedCameraDevice(
                self.feature_finder,
                self.stages,
                self.leds,
                -370.0,
                quiet=1,
                image_width=160,
                image_height=120,
                )
            self.camera_device.move_eye(array([10.0, -10.0, 0.0]))
            self.camera_device.acquire_image()

        logging.info('Acquiring initial image')

        self.start_continuous_acquisition()

        self.ui_interval = 1. / 15
        self.start_time = time.time()
        self.last_time = self.start_time

        self.conduit_fps = 0.

        # calibrator
        logging.info('Creating Calibrator Object')
        if self.use_simulated:
            r_dir = -1
            d_guess = -380
        else:
            r_dir = 1
            # d_guess = 280
            d_guess = 380
            # d_guess = 300 # use this with the rat and set d_halfrange=30

        self.calibrator = StahlLikeCalibrator(
            self.camera_device,
            self.stages,
            self.zoom_and_focus,
            self.leds,
            d_halfrange=30,
            ui_queue=self.ui_queue,
            r_stage_direction=r_dir,
            d_guess=d_guess,
            )
            
            
            

        if mw_enabled:
            logging.info('Instantiating mw conduit')
            self.mw_conduit = mw_conduit.IPCServerConduit('cobra1')
            logging.debug('conduit = %s' % self.mw_conduit)
        else:
            self.mw_conduit = None

        if self.mw_conduit != None:
            logging.debug('Initializing conduit...')
            initialized = self.mw_conduit.initialize()
            logging.debug('conduit.initialize() = %s' % initialized)
            if not initialized:
                logging.error('Failed to initialize conduit')

            logging.info('Sending dummy data (-1000,-1000,-1000)')
            self.mw_conduit.send_data(GAZE_INFO, (-1000, -1000, -1000))
            logging.info('Finished testing conduit')
        else:
            logging.warning('No conduit')
class CaptureController(object):
    def __init__(self):

        # motion control settings
        self.x_set = 0.5
        self.y_set = 0.5
        self.r_set = 0.5

        self.zoom_step = 20.
        self.focus_step = 20.
        self.no_powerzoom = False

        self.x_current = 0.0
        self.y_current = 0.0
        self.r_current = 0.0
        self.d_current = 0.0
        self.rp_current = 0.0
        self.pixels_per_mm_current = 0.0
        self.zoom_current = 0.0
        self.focus_current = 0.0

        # led settings
        self.IsetCh1 = 0.0
        self.IsetCh2 = 0.0
        self.IsetCh3 = 0.0
        self.IsetCh4 = 0.0

        # camera settings
        self.test_binning = 3
        self.binning_factor = 1
        self.gain_factor = 1.

        self.camera_device = None

        self.camera_locked = 0
        self.continuously_acquiring = 0
        self.frame_rate = 0

        # MWorks communication, if available
        self.mw_conduit = None

        # UI
        self.canvas_update_timer = None
        self.ui_queue = queue.Queue(5)

        #self.im_queue = queue.Queue(5) # NEW
        #logging.info("ui_queue and im_queue created")

        self.target_ui_update_interval = 1 / 30.
        self.last_ui_get_time = time.time()
        self.last_ui_put_time = time.time()

        self.enable_save_to_disk = global_settings.get('enable_save_to_disk',
                                                       True)
        logging.info("Save to disk?: %d" % self.enable_save_to_disk)
        self.image_save_dir = global_settings.get('data_dir',
                                                  '~/.camera_capture/data')

        self.use_simulated = global_settings.get('use_simulated', False)
        self.use_file_for_cam = global_settings.get('use_file_for_camera',
                                                    False)

    def initialize(self):

        # -------------------------------------------------------------
        # Image "dumper" (put images to file system)
        # -------------------------------------------------------------

        if self.enable_save_to_disk:
            self.image_dumper = ImageDumper(self.image_save_dir)
            logging.info('Creating ImageDumper object at: %s',
                         self.image_save_dir)
        else:
            self.image_dumper = None

        # -------------------------------------------------------------
        # LED Controller
        # -------------------------------------------------------------
        logging.info('Initializing LED Control Subsystem')

        if not self.use_simulated:
            try:
                self.leds = MightexLEDController('169.254.0.9', 8006)
                self.leds.connect()
            except:
                self.leds = None

        if self.use_simulated or self.leds is None:
            self.leds = SimulatedLEDController(4)

        # -------------------------------------------------------------
        # Camera
        # -------------------------------------------------------------

        logging.info('Initializing Camera')

        try:
            if not self.use_file_for_cam:
                logging.info('Connecting to Camera...')
                self.camera_device = ProsilicaCameraDevice()

                self.binningX = 4
                self.binningY = 4
                self.gain = 1
                currbinx = self.binningX
                currbiny = self.binningY
                self.roi_width = 656 / currbinx  #164
                self.roi_height = 456 / currbiny  #122

        except Exception, e:
            logging.warning("Error connecting to camera: %s" % e.message)
            self.camera_device = None

        # if that didn't work, build a fake device
        if self.use_file_for_cam or self.camera_device is None:
            fake_file = global_settings.get('fake_cam_file', None)
            if fake_file is None:
                logging.error('No valid fake camera file specified')

            self.camera_device = FakeCameraDevice(fake_file)
            self.camera_device.acquire_image()

        logging.info('Starting continuous camera acquisition')

        self.start_continuous_acquisition()

        self.ui_interval = 1. / 10000.
        self.start_time = time.time()
        self.last_time = self.start_time
        self.conduit_fps = 0.

        # -------------------------------------------------------------
        # Set up the MW communications
        # -------------------------------------------------------------

        if mw_enabled:
            logging.info('Instantiating mw conduit')
            self.mw_conduit = mw_conduit.IPCServerConduit('cobra1')
            # print 'conduit = %s' % self.mw_conduit
        else:
            self.mw_conduit = None

        if self.mw_conduit != None:
            logging.info('Initializing conduit...')
            initialized = self.mw_conduit.initialize()
            # print initialized
            if not initialized:
                logging.warning('Failed to initialize conduit')

            logging.info('Sending dummy data (-1000,-1000,-1000)')
            self.mw_conduit.send_data(GAZE_INFO, (-1000, -1000, -1000))
            logging.info('Finished testing conduit')
        else:
            logging.warning('No conduit')
from simple_camera_capture.image_processing import ImageAnalyzer

try:
    import queue
except ImportError:
    import Queue as queue

# load settings
loaded_config = load_config_file('~/.simple_camera_capture/config.ini')
global_settings.update(loaded_config)

# boost.python wrapper for a MWorks interprocess conduit, if you're into
# that sort of thing
mw_enabled = False

if global_settings.get('enable_mw_conduit', True):

    try:
        sys.path.append('/Library/Application Support/MWorks/Scripting/Python')
        import mworks.conduit as mw_conduit
        GAZE_INFO = 100
        TRACKER_INFO = 101
        mw_enabled = True
    except Exception, e:
        logging.warning('Unable to load MW conduit: %s' % e)

# A catch-all class for controlling the eyetracking hardware


class CaptureController(object):
    def __init__(self):
Exemple #9
0
    def __init__(self):

        self.test_binning = 3

        self.x_set = 0.5
        self.y_set = 0.5
        self.r_set = 0.5

        self.zoom_step = 20.
        self.focus_step = 20.

        self.x_current = 0.0
        self.y_current = 0.0
        self.r_current = 0.0
        self.d_current = 0.0
        self.rp_current = 0.0
        self.pixels_per_mm_current = 0.0
        self.zoom_current = 0.0
        self.focus_current = 0.0

        self.d_cntr_set = 0.0
        self.r_cntr_set = 0.0

        self.r_2align_pup_cr = None
        self.pupil_cr_diff = None

        self.IsetCh1 = 0.0
        self.IsetCh2 = 0.0
        self.IsetCh3 = 0.0
        self.IsetCh4 = 0.0

        self.pupil_position_x = 0.0
        self.pupil_position_y = 0.0
        self.pupil_radius = 0.0
        self.cr_position_x = 0.0
        self.cr_position_y = 0.0

        self.pupil_only = False

        self.gaze_azimuth = 0.0
        self.gaze_elevation = 0.0
        self.calibration_status = 0

        self.sobel_avg = 0.0

        self.simulated_eye_x = 0.0
        self.simulated_eye_y = 0.0
        self.simulated_pupil_radius = 0.2

        self.binning_factor = 1
        self.gain_factor = 1.

        # Added by DZ to deal with rigs without power zoom and focus
        self.no_powerzoom = False

        self.pupil_only = False

        self.feature_finder = None

        self.camera_device = None
        self.calibrator = None
        self.mw_conduit = None

        self.canvas_update_timer = None

        self.ui_queue = Queue(2)

        self.camera_locked = 0
        self.continuously_acquiring = 0

        self.calibrating = False

        self.enable_save_to_disk = global_settings.get('enable_save_to_disk', False)
        logging.info("Save to disk?: %s" % self.enable_save_to_disk)
        self.image_save_dir = global_settings.get('data_dir', None)

        self.use_simulated = global_settings.get('use_simulated', False)
        self.use_file_for_cam = global_settings.get('use_file_for_camera',
                                                    False)

        # -------------------------------------------------------------
        # Stages
        # -------------------------------------------------------------

        logging.info('Initializing Motion Control Subsystem (Stages)...')
        if self.use_simulated:
            esp300 = SimulatedStageController()
        else:
            esp300 = ESP300StageController('169.254.0.9', 8001)

            try:
                esp300.connect()
            except Exception as E:
                logging.warning(str(E))
                logging.warning('Attempting to restart serial bridge (this can take ' \
                    + 'several tens of seconds)...')
                try:
                    kick_in_the_pants = httplib.HTTPConnection('169.254.0.9',
                            80, timeout=5)
                    kick_in_the_pants.request('GET', '/goforms/resetUnit?')
                    time.sleep(5)
                    esp300.connect()
                    del kick_in_the_pants
                except Exception:
                    self.simple_alert('Could not connect to serial bridge',
                                      "Attempts to 'kick' the serial bridge "
                                      + 'have failed.  '
                                      + 'Reverting to simulated mode.')
                    esp300 = SimulatedStageController()
                    self.use_simulated = True

        self.stages = EyeTrackerStageController(esp300)

        logging.info('Initializing Motion Control Subsystem (Focus and Zoom)')
        if self.no_powerzoom:
            esp300_2 = SimulatedStageController()
        else:
            if self.use_simulated:
                esp300_2 = SimulatedStageController()
            else:
                esp300_2 = ESP300StageController('169.254.0.9', 8002)
                esp300_2.connect()

        self.zoom_and_focus = FocusAndZoomController(esp300_2)

        self.x_current = self.stages.current_position(self.stages.x_axis)
        self.y_current = self.stages.current_position(self.stages.y_axis)
        self.r_current = self.stages.current_position(self.stages.r_axis)

        self.sobel_avg = 0

        self.r_cntr_set = 0
        self.d_cntr_set = 0

        self.zoom_step = 20.
        self.focus_step = 20.

        self.IsetCh1 = 0
        self.IsetCh2 = 0
        self.IsetCh3 = 0
        self.IsetCh4 = 0

        # Manual aligment of pupil and CR
        self.r_2align_pup_cr = 0

        # -------------------------------------------------------------
        # LED Controller
        # -------------------------------------------------------------
        logging.info('Initializing LED Control Subsystem')

        if self.use_simulated:
            self.leds = SimulatedLEDController(4)
        else:
            self.leds = MightexLEDController('169.254.0.9', 8006)
            self.leds.connect()

        # -------------------------------------------------------------
        # Camera and Image Processing
        # -------------------------------------------------------------

        logging.info('Initializing Image Processing')

        self.features = None
        self.frame_rates = []

        # set up real featutre finders (these won't be used if we use a
        # fake camera instead)

        nworkers = int(global_settings.get('nworkers', 0))
        logging.info("starting with N workers %s" % nworkers)

        self.radial_ff = None
        self.starburst_ff = None

        if nworkers != 0:

            self.feature_finder = PipelinedFeatureFinder(nworkers)
            workers = self.feature_finder.workers

            self.rffs = []
            self.sbffs = []
            for worker in workers:

                fr_ff = worker.FastRadialFeatureFinder()  # in worker process
                sb_ff = worker.StarBurstEyeFeatureFinder()  # in worker process

                #self.radial_ff = fr_ff
                #self.starburst_ff = sb_ff
                self.rffs.append(fr_ff)
                self.sbffs.append(sb_ff)
                # self.radial_symmetry_feature_finder_adaptor.addFeatureFinder(fr_ff)
                # self.starburst_feature_finder_adaptor.addFeatureFinder(sb_ff)

                comp_ff = worker.FrugalCompositeEyeFeatureFinder(fr_ff, sb_ff)
                # self.composite_feature_finder_adaptor.addFeatureFinder(comp_ff)

                worker.set_main_feature_finder(comp_ff)  # create in worker process

            self.radial_ff = ParamExpose(self.rffs, rff_params)
            self.starburst_ff = ParamExpose(self.sbffs, sbff_params)
            self.feature_finder.start()  # start the worker loops
        else:
            sb_ff = SubpixelStarburstEyeFeatureFinder()
            fr_ff = FastRadialFeatureFinder()

            comp_ff = FrugalCompositeEyeFeatureFinder(fr_ff, sb_ff)

            self.radial_ff = fr_ff
            self.starburst_ff = sb_ff

            self.feature_finder = comp_ff

        if self.enable_save_to_disk and self.image_save_dir is not None:
            logging.info('Enabling save to disk...')
            self.feature_finder = ImageSaveDummyFeatureFinder(self.feature_finder, self.image_save_dir)
        else:
            logging.warning('Data will not be saved to disk.')

        if True:
        # try:
            if not self.use_file_for_cam and not self.use_simulated:
                logging.info('Connecting to Camera...')
                self.camera_device = ProsilicaCameraDevice(self.feature_finder)

                self.binning = 4
                self.gain = 1
        # except Exception, e:
        #             print "Unexpected error:", e.message
        #             self.use_file_for_cam = 1

        if self.use_file_for_cam:
            fn = global_settings.get('camera_file', '/Users/davidcox/Desktop/albino2/Snapshot2.bmp')
            self.camera_device = FakeCameraDevice(self.feature_finder, fn)
            self.camera_device.acquire_image()

        if self.use_simulated and self.camera_device == None:
            logging.info('Failing over to Simulated Camera')

            # use a POV-Ray simulated camera + a simpler feature finder
            # that works with it
            self.camera_device = POVRaySimulatedCameraDevice(
                self.feature_finder,
                self.stages,
                self.leds,
                -370.0,
                quiet=1,
                image_width=160,
                image_height=120,
                )
            self.camera_device.move_eye(array([10.0, -10.0, 0.0]))
            self.camera_device.acquire_image()

        logging.info('Acquiring initial image')

        self.start_continuous_acquisition()

        self.ui_interval = 1. / 15
        self.start_time = time.time()
        self.last_time = self.start_time

        self.conduit_fps = 0.

        # calibrator
        logging.info('Creating Calibrator Object')
        if self.use_simulated:
            r_dir = -1
            d_guess = -380
        else:
            r_dir = 1
            # d_guess = 280
            d_guess = 380
            # d_guess = 300 # use this with the rat and set d_halfrange=30

        self.calibrator = StahlLikeCalibrator(
            self.camera_device,
            self.stages,
            self.zoom_and_focus,
            self.leds,
            d_halfrange=30,
            ui_queue=self.ui_queue,
            r_stage_direction=r_dir,
            d_guess=d_guess,
            )

        if mw_enabled:
            logging.info('Instantiating mw conduit')
            self.mw_conduit = mw_conduit.IPCServerConduit('cobra1')
            logging.debug('conduit = %s' % self.mw_conduit)
        else:
            self.mw_conduit = None

        if self.mw_conduit != None:
            logging.debug('Initializing conduit...')
            initialized = self.mw_conduit.initialize()
            logging.debug('conduit.initialize() = %s' % initialized)
            if not initialized:
                logging.error('Failed to initialize conduit')

            logging.info('Sending dummy data (-1000,-1000,-1000)')
            self.mw_conduit.send_data(GAZE_INFO, (-1000, -1000, -1000))
            logging.info('Finished testing conduit')
        else:
            logging.warning('No conduit')