Пример #1
0
 public static extern int LidarGetDeviceInfo(
     ref rplidar_response_device_info_t info,
     uint timeout = 2000);
Пример #2
0
        /// <summary>
        /// Starts this instance.
        /// </summary>
        /// <param name="scanMode">The scan mode.</param>
        /// <returns><c>true</c> if successfully, <c>false</c> otherwise.</returns>
        public bool LidarStart(EScanMode scanMode = EScanMode.Boost)
        {
            try
            {
                if (IsRunning ||
                    IsDisposed)
                {
                    return(false);
                }

                _ScanNumber = 0;

                // Load driver
                if (!NativeModuleManager.InitializeNativeModule(NativeModuleNames.NativeRpLidar))
                {
                    Console.WriteLine("Can not load lidar native driver library.");
                    return(false);
                }

                // Initialize driver
                if (RpLidarInterface.LidarInitializeDriver() != 0)
                {
                    Console.WriteLine("Can not initialize lidar driver.");
                }

                // Connect device
                if (RpLidarInterface.LidarConnect(_SerialPort, _SerialPortSpeed) != 0)
                {
                    Console.WriteLine($"Can not connect to serial port \"{_SerialPort}:{_SerialPortSpeed}\".");
                }

                // Get device info
                _DeviceInfo = new rplidar_response_device_info_t
                {
                    serialNum = new byte[16]
                };
                if (RpLidarInterface.LidarGetDeviceInfo(ref _DeviceInfo) != 0)
                {
                    Console.WriteLine("Can not get device info from device.");
                }

                // Get device health info
                _DeviceHealthInfo = new rplidar_response_device_health_t();
                if (RpLidarInterface.LidarGetHealth(ref _DeviceHealthInfo) != 0)
                {
                    Console.WriteLine($"Can not get device health info from device.");
                }

                // Reset device on error state
                if (_DeviceHealthInfo.Status == (byte)EDeviceStatus.RPLIDAR_STATUS_ERROR)
                {
                    Console.WriteLine($@"Reset device due to error device health status: {_DeviceHealthInfo.Status}");
                    RpLidarInterface.LidarReset();
                }

                // Start spinning motor
                if (RpLidarInterface.LidarStartMotor() != 0)
                {
                    Console.WriteLine($"Can not start device motor.");
                }

                // Start scanning
                _RpLidarScanMode = new RplidarScanMode
                {
                    scan_mode = new char[64]
                };
                if (RpLidarInterface.LidarStartScan(false, (ushort)scanMode, ref _RpLidarScanMode) != 0)
                {
                    Console.WriteLine($"Can not start scan mode.");
                }

                // Initialize task
                _TokenSource = new CancellationTokenSource();
                _Token       = _TokenSource.Token;

                _Task = Task.Factory.StartNew(() => LidarTaskDoWork(_Token), _Token);

                if (RpLidarInterface.LidarIsConnected() == 1)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            finally
            {
                if (RpLidarInterface.LidarIsConnected() == 1)
                {
                    OnDeviceConnected?.Invoke(this, EventArgs.Empty);
                }
            }
        }