ritenn/rplidar-a1

Rplidar A1 库。

dev-master 2021-05-17 16:57 UTC

This package is auto-updated.

Last update: 2024-09-18 00:13:46 UTC


README

此包包含由 SLAMTEC 制造的 RPLIDAR-A1 的驱动程序,该驱动程序是用 PHP 编写的,可以通过简单的 PHP 接口/外观访问。它不包含数据的视觉表示(稍后我会添加 js 包)。

安装

  1. 下载包并通过 composer 安装

composer require ritenn/rplidar-a1

  1. 启用包并将配置复制到您的 Laravel 安装中

php artisan vendor:publish --provider="Ritenn\RplidarA1\RplidarA1ServiceProvider" --force

基本用法/示例

您可以使用外观或接口实现此库

use Ritenn\RplidarA1\Interfaces\LidarCommandInterface;
use Ritenn\RplidarA1\Facades\LidarCommands;
use Illuminate\Support\Collection;

/**
 * @var LidarCommandInterface
 */
public $lidarCommands;

/**
 * Scan flag 
 */
public $isRunningScan = false;

/**
* @param LidarCommandInterface $lidarCommands
 */
public function __construct(LidarCommandInterface $lidarCommands)
{
    $this->lidarCommands = $lidarCommands;
}

/**
 * @return bool
 */
public function startScan() : bool
{
    $this->lidarCommand->startProcess();
    // ...or facade - LidarCommands::startProcess(); etc.
    $this->lidarCommand->openPort();
    $this->lidarCommand->sendCommand('GET_HEALTH');
    
    /**
     * getData() will always return response from device
     * In this case, simply returns health status. 
     */
    $data = $this->lidarCommand->getData();
    
    if ( ! is_null($data) && $data->get('status') === 'Good' )
    {
        $this->lidarCommand->runMotor();
        
        if ( ! $this->lidarCommand->sendCommand('SCAN') )
        {
            return false;
        }
        
        $this->isRunningScan = true;
    }
    
    return true;
}

/**
 * @return bool
 */
public function stopScan() : bool
{
    if ( $this->lidarCommand->sendCommand('STOP') )
    {
        $this->lidarCommand->stopMotor();
        $this->lidarCommand->closePort();
        $this->lidarCommand->stopProcess();
        $this->isRunningScan = false;
        
        return true;
    }
    
    return false;
}

/**
 * @return Collection|null
 */
public function getScanData() : ?Collection
{
    /**
     * If scan is running, getData() will keep updating collection with new data
     * as soon as packets are received and interpreted by background process.
     * 
     * You should implement websockets on frontend to get updated data.
     */
     if ( $this->isRunningScan )
     {
         return $this->lidarCommand->getData();
     }
     
     return collect([]);
}

命令列表 - sendCommand(string $command)

它如何工作?

一切从 PHP 服务器(指挥中心)开始。当后台进程由服务器启动时,它开始监听其他命令以及来自 rplidar 设备的数据(如果端口已经打开)。后台进程通过 USB/UART 转换器直接与 rplidar 通信,这意味着您不需要任何 C++ 驱动程序即可使其工作。来自设备的所有数据都转换为 360 度角度/距离(厘米)数组,并存储在共享内存中。这种解决方案非常灵活,因为您也可以在其他编程语言中访问它,而不仅仅是 PHP。

注意

此包使用小 c++ 程序设置/清除 DTR(数据传输就绪)信号,这是启动/停止电机所必需的。我不了解任何更好的 PHP 解决方案。

how it works

支持的扫描模式

  • 传统模式(传统模式) - 旧简单,但有点混乱的模式。最多 4000 个样本(A1 为 2000 个)
  • Express 模式(传统) - 推荐,最多 8000 个样本(A1 为 4000 个)
  • 扩展版本 - 可能在未来。
  • 密集版本 - 我的设备不支持此模式。当您调用 express(传统)时,您的设备可能会期望此模式。它具有相同的请求包,但不同的描述符。

要求

  • 此库仅在 Linux ARM x64(Raspberry Pi 4)上进行了测试,因此它在 Windows 上可能无法工作。
  • 此库与 rplidar A1 和固件版本 1.04 进行了测试。
  • PHP 7.2+,推荐 8.x。
  • 启用 exec 函数,没有它无法启动后台进程和运行电机。
  • 了解 Linux 权限。