Exemple #1
0
        public async Task <CameraImage> AcquireImageAsync(AcquireParams acquireParams,
                                                          CancellationToken ct, IProgress <CameraProgressEventArgs> progress = null)
        {
            var takeParams = new TakeParams(
                acquireParams.ExposureType,
                acquireParams.ExposureTime,
                acquireParams.AnalogGain,
                acquireParams.MinGain,
                acquireParams.MaxGain
                );
            var takeProgress = new Progress <TakeProgressEventArgs>();

            takeProgress.ProgressChanged += (s, e) =>
            {
                progress.Report(
                    new CameraProgressEventArgs(e.Percentage, e.Description)
                    );
            };

            return(new CameraImage()
            {
                Image = await _device.TakeImage(takeParams, ct, takeProgress),
                ImageWidth = ImageWidth, ImageHeight = ImageHeight
            });
        }
        private async Task CorrectGainAsync(TakeParams acquireParams)
        {
            //lower gain limit
            var P1 = (int)acquireParams.MinGain;

            //high gain limit
            var P2 = acquireParams.MaxGain;

            //from props.txt
            var gain = (P1 == 226) ? (2048 - 2048 * Math.Exp(-acquireParams.AnalogGain * Math.Log(10) / 20)) : (acquireParams.AnalogGain * 10 + P2);

            //set gain, 131108 register from props.txt
            var rez = await _cameraHelper.SetConfigRegisterAsync(131108, (int)gain);

            gain = await _cameraHelper.GetBlocksSizeAsync(131108);
        }
        public async Task <ushort[, ]> TakeImage(
            TakeParams acquireParams, CancellationToken ct, IProgress <TakeProgressEventArgs> progress = null)
        {
            if (!IsAttached)
            {
                throw new Exception("The camera has been disconnected");
            }

            try
            {
                bool success = false;
                do
                {
                    try
                    {
                        //Pixel format
                        var r1 = await _cameraHelper.SetConfigRegisterAsync(0x30024, 5);

                        success = true;
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                        throw;
                    }
                } while (!success);

                //set gain
                await CorrectGainAsync(acquireParams);

                //Get SBRM register
                var sbrm = await _cameraHelper.GetRegisterValueAsync(0x001D8);

                //Read SIRM register
                var sirm = await _cameraHelper.GetRegisterValueAsync(sbrm + 0x0020);

                //Get leader size.
                var leaderSize = await _cameraHelper.GetBlocksSizeAsync(sirm + 0x10);

                //Get trailer size
                var trailerSize = await _cameraHelper.GetBlocksSizeAsync(sirm + 0x14);

                //image width and height, registers from props.txt
                var width = await _cameraHelper.GetBlocksSizeAsync(0x30204);

                var height = await _cameraHelper.GetBlocksSizeAsync(0x30224);

                //trigger_mode_off (on = 1), registers from props.txt
                var rez = await _cameraHelper.SetConfigRegisterAsync(0x40104, 0);

                //unknown register from WireShark dump
                rez = await _cameraHelper.SetConfigRegisterAsync(0x40204, 0);

                //acquisition mode, 0-single, 2-continue
                rez = await _cameraHelper.SetConfigRegisterAsync(0x40004, 0);

                //disable exposure
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0004, 0);

                //single payload size
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x001C, width *height);

                //count of payload transmitions
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0020, 4);

                //max final payload transfer 1 size (value from WireShark dump)
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0024, 206848);

                //max final payload transfer 1 size (value from WireShark dump)
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0028, 0);

                //leader and trailer sizes
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0018, leaderSize);

                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x002c, trailerSize);

                //exposure mode,registers from props.txt
                //EnumEntry_ExposureMode_Timed = 1
                //EnumEntry_ExposureMode_TriggerWidth = 2
                rez = await _cameraHelper.SetConfigRegisterAsync(263172, 1);

                //Exposure time, registers from props.txt
                rez = await _cameraHelper.SetConfigRegisterAsync(263268, (int)(acquireParams.ExposureTime * 1000000));

                await SetLaserState(0, acquireParams.ExposureType);

                //enable exposure
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0004, 1);

                //start exposure
                rez = await _cameraHelper.SetConfigRegisterAsync(0x40024, 0x01);

                byte[] data   = new byte[0];
                var    leader = await _cameraHelper.GetImageData();

                while (data.Length < width * height * 2)
                {
                    bool sucess = false;
                    do
                    {
                        try
                        {
                            data    = ArrayHelper.ConcatArrays(data, await _cameraHelper.GetImageData());
                            success = true;
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(e.Message);
                            throw;
                        }
                    } while (!success);
                }
                var trailer = await _cameraHelper.GetImageData();

                //stop exposure
                rez = await _cameraHelper.SetConfigRegisterAsync(0x40044, 0x01);

                await SetLaserState(0, false);

                ImageHeight = height; ImageWidth = width;
                //return ArrayHelper.UnpackImage(data, width, height);

                var imageU = new ushort[data.Length / 2];
                // Copy single image.
                Buffer.BlockCopy(data, 0, imageU, 0, data.Length);
                // Average and scale.
                for (var i = 0; i < imageU.Length; ++i)
                {
                    if (imageU[i] * 16 <= ushort.MaxValue)
                    {
                        imageU[i] = (ushort)(imageU[i] * 16);
                    }
                    else
                    {
                        imageU[i] = ushort.MaxValue;
                    }
                }
                // Prepare visual informaition.
                //FindMinMax(imageU);
                //InsertHistogram(imageU, width, height);
                //InsertSection(imageU, width, height, width * 3 / 8);
                // Convert to two dimentional array.
                var result = new ushort[height, width];
                var offset = 0;
                for (var i = 0; i < height; ++i)
                {
                    for (var j = 0; j < width; ++j)
                    {
                        result[i, j] = imageU[offset++];
                    }
                }
                return(result);
            }
            catch (ArgumentException)
            {
                OnDisconnect();
                return(null);
            }
        }
        public async Task <ushort[, ]> TakeImage(
            TakeParams acquireParams, CancellationToken ct, IProgress <TakeProgressEventArgs> progress = null)
        {
            if (!IsAttached)
            {
                throw new Exception("The camera has been disconnected");
            }

            try
            {
                //Pixel format
                var rez = await _cameraHelper.SetConfigRegisterAsync(0x30024, 5);

                //set gain
                await CorrectGainAsync(acquireParams);

                //Get SBRM register
                var sbrm = await _cameraHelper.GetRegisterValueAsync(0x001D8);

                //Read SIRM register
                var sirm = await _cameraHelper.GetRegisterValueAsync(sbrm + 0x0020);

                //Get leader size.
                var leaderSize = await _cameraHelper.GetBlocksSizeAsync(sirm + 0x10);

                //Get trailer size
                var trailerSize = await _cameraHelper.GetBlocksSizeAsync(sirm + 0x14);

                //image width and height, registers from props.txt
                var width = await _cameraHelper.GetBlocksSizeAsync(0x30204);

                var height = await _cameraHelper.GetBlocksSizeAsync(0x30224);

                //trigger_mode_off (on = 1), registers from props.txt
                rez = await _cameraHelper.SetConfigRegisterAsync(0x40104, 0);

                //unknown register from WireShark dump
                rez = await _cameraHelper.SetConfigRegisterAsync(0x40204, 0);

                //acquisition mode, 0-single, 2-continue
                rez = await _cameraHelper.SetConfigRegisterAsync(0x40004, 0);

                //disable exposure
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0004, 0);

                //single payload size
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x001C, width *height);

                //count of payload transmitions
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0020, 4);

                //max final payload transfer 1 size (value from WireShark dump)
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0024, 206848);

                //max final payload transfer 1 size (value from WireShark dump)
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0028, 0);

                //leader and trailer sizes
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0018, leaderSize);

                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x002c, trailerSize);

                //exposure mode,registers from props.txt
                //EnumEntry_ExposureMode_Timed = 1
                //EnumEntry_ExposureMode_TriggerWidth = 2
                rez = await _cameraHelper.SetConfigRegisterAsync(263172, 1);

                //Exposure time, registers from props.txt
                rez = await _cameraHelper.SetConfigRegisterAsync(263268, (int)acquireParams.ExposureTime);

                //enable exposure
                rez = await _cameraHelper.SetConfigRegisterAsync(sirm + 0x0004, 1);

                //start exposure
                rez = await _cameraHelper.SetConfigRegisterAsync(0x40024, 0x01);

                byte[] data   = new byte[0];
                var    leader = await _cameraHelper.GetImageData();

                while (data.Length < width * height * 2)
                {
                    data = ArrayHelper.ConcatArrays(data, await _cameraHelper.GetImageData());
                }
                var trailer = await _cameraHelper.GetImageData();

                //stop exposure
                rez = await _cameraHelper.SetConfigRegisterAsync(0x40044, 0x01);

                ImageHeight = height; ImageWidth = width;
                return(ArrayHelper.UnpackImage(data, width, height));
            }
            catch (ArgumentException)
            {
                OnDisconnect();
                return(null);
            }
        }