Example #1
0
        // Constructors
        public EVI_D70()
        {
            // Initialize internal variables
            port = new SerialPort();
            hardware_connected = false;
            thread_control = new CancellationTokenSource();
            initilization_error = false;
            serial_channel_open = new ManualResetEventSlim(true);
            socket_available = new ManualResetEventSlim(true);
            command_buffer = new ObservableCollection<command>();
            emergency_stop_turnstyle = new ManualResetEventSlim(false);
            _pan = new angular_position();
            _tilt = new angular_position();
            _zoom = new zoom_position();
            pan_tilt_status = DRIVE_STATUS.FULL_STOP;
            zoom_status = DRIVE_STATUS.FULL_STOP;
            last_inquiry_was_pan_tilt = false;
            num_cmds_since_inquiry = 0;
            command_buffer.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(command_buffer_changed_event_handler);
            command_buffer_populated = new ManualResetEventSlim(false);
            failed_command = null;
            last_successful_pan_tilt_cmd = null;
            last_successful_zoom_cmd = null;
            socket_one_cmd = null;
            socket_two_cmd = null;

            // Trace listener
            trace_msgs = new internal_trace_listener();
            Trace.Listeners.Add(trace_msgs);
            position_data_error += new CameraHardwareErrorEventHandler(print_trace_log);
            serial_port_error += new CameraHardwareErrorEventHandler(print_trace_log);
            command_error += new CameraHardwareErrorEventHandler(print_trace_log);

            // Threads
            inquiry_after_stop_thread = new Thread(new ThreadStart(inquiry_after_stop));
            pan_tilt_inquiry_after_stop = false;
            zoom_inquiry_after_stop = false;
            after_stop = new ManualResetEventSlim(false);
            pan_tilt_inquiry_complete = new ManualResetEventSlim(false);
            zoom_inquiry_complete = new ManualResetEventSlim(false);
            dispatch_thread = new Thread(new ThreadStart(dispatch));
            receive_thread = new Thread(new ThreadStart(receive));

            // PID
            pid_thread = new Thread(new ThreadStart(pid));
            pid_turnstyle = new ManualResetEventSlim(false);
            pid_data_turnstyle = new ManualResetEventSlim(true);
            pid_pan_tilt_speed = (new pan_tilt_jog_command(1)).pan_tilt_speed;
            pid_zoom_speed = (new zoom_jog_command(1)).zoom_speed;
            _pan.position_changed += new EventHandler<EventArgs>(pid_data_changed);
            _tilt.position_changed += new EventHandler<EventArgs>(pid_data_changed);
            _zoom.position_changed += new EventHandler<EventArgs>(pid_data_changed);
            _pid_target_pan_position.position_changed += new EventHandler<EventArgs>(pid_data_changed);
            _pid_target_tilt_position.position_changed += new EventHandler<EventArgs>(pid_data_changed);
            _pid_target_zoom_position.position_changed += new EventHandler<EventArgs>(pid_data_changed);
        }
Example #2
0
        public bool connect(String port_name)
        {
            // Setup serial port values
            port.BaudRate = 38400;
            port.DataBits = 8;
            port.NewLine = new string((char)0xFF, 1);  // This is the visca terminator, we don't use "readline" but its here for clarity
            port.Parity = Parity.None;
            port.PortName = port_name;
            port.StopBits = StopBits.One;
            port.ReadTimeout = 2000;

            try { port.Open(); }  // Open serial connection
            catch { return hardware_connected; }  // No connection, nothing else to do

            // Get camera number
            connect_command connect_cmd = new connect_command();
            port.Write(connect_cmd.raw_serial_data, 0, connect_cmd.raw_serial_data.Length);
            List<int> response;
            int response_type = get_response(out response);
            while (response_type == VISCA_CODE.RESPONSE_ACK)  // Skip acknowledge messages
                response_type = get_response(out response);
            if (response_type != VISCA_CODE.RESPONSE_ADDRESS)  // Got camera number
                return hardware_connected;  // Note: if an error or timeout occurs, connected is false (as expected)
            camera_num = response[2] - 1;

            // Get the current pan/tilt/zoom position
            // Note:  This is hard coded here rather than using the dispatch thread because at this point we don't know if we are connected yet
            command inquiry = new pan_tilt_inquiry_command(camera_num);
            port.Write(inquiry.raw_serial_data, 0, inquiry.raw_serial_data.Length);
            dispatched_cmd = inquiry;
            response_type = get_response(out response);  // Should only be command complete
            CameraHardwareErrorEventHandler backup = position_data_error;  // Backing up user event handlers
            position_data_error = new CameraHardwareErrorEventHandler(initilization_error_handler);
            receive_response_completed(response);  // Will put the response into "pan" and "tilt"
            inquiry = new zoom_inquiry_command(camera_num);
            port.Write(inquiry.raw_serial_data, 0, inquiry.raw_serial_data.Length);
            dispatched_cmd = inquiry;
            response_type = get_response(out response);  // Should only be command complete
            receive_response_completed(response);  // Will put the response into "zoom"
            position_data_error = backup;  // Restore user event handlers

            // If we can't get the current positions, then we aren't connected to the camera
            if (!initilization_error)
            {
                hardware_connected = true;

                // Start the background threads
                receive_thread.Start();
                dispatch_thread.Start();
                inquiry_after_stop_thread.Start();
                pid_thread.Start();
            }

            return hardware_connected;
        }