USB hub 多usb接口重映射:udev 规则

USB hub 多usb接口重映射:udev 规则

你在开发的过程中是否会出现 USB端口号会发生变化。 例如: 机器人底盘与雷达都是 ttyUSB* .其序号与接入的先后有关。 对于写好的launch每次修改对应接口是不是

很麻法,或者每次得先插入一个再插另外一个。才能保证ttyUSB0 ttyUSB1. 但是如果你重启系统,说不定还是有问题出现。。

        ==》 不如将端口重映射到新的固定的名字,并且设置其权限为可读。 这样不管你的先后插入,符合对应id的硬件端口自动的映射到新定义的名字。  


一. 单一的USB映射说明

  参见:http://blog.youkuaiyun.com/zyh821351004/article/details/41577105

lsusb 命令查看所有usb端口信息。 

[cpp]  view plain  copy
 
print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ lsusb   
  2. Bus 002 Device 002: ID 0bda:0411 Realtek Semiconductor Corp.   
  3. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub  
  4. Bus 001 Device 004: ID 04f2:b48c Chicony Electronics Co., Ltd   
  5. Bus 001 Device 009: ID 046d:c31c Logitech, Inc. Keyboard K120  
  6. Bus 001 Device 010: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light  
  7. Bus 001 Device 005: ID 0bda:5411 Realtek Semiconductor Corp.   
  8. Bus 001 Device 008: ID 24ae:1100    
  9. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ lsusb   
  2. Bus 002 Device 002: ID 0bda:0411 Realtek Semiconductor Corp.   
  3. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub  
  4. Bus 001 Device 004: ID 04f2:b48c Chicony Electronics Co., Ltd   
  5. Bus 001 Device 009: ID 046d:c31c Logitech, Inc. Keyboard K120  
  6. Bus 001 Device 010: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light  
  7. Bus 001 Device 005: ID 0bda:5411 Realtek Semiconductor Corp.   
  8. Bus 001 Device 008: ID 24ae:1100    
  9. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
udev的规则说明,可以参考博客说明:http://blog.youkuaiyun.com/cokewei/article/details/8281239

简单说明:

$kernel, %k:设备的内核设备名称,例如:sdacdrom

ID  10c4:ea60     表示usb设备的ID(这个ID由芯片制造商设置,可以唯一表示该设备)
    10c4    usb_device_descriptor.idVendor
    ea60    usb_device_descriptor.idProduct
 

依据这些信息,就可以简单的写udev规则了:rplidar.rules

sudo gedit /etc/udev/rules.d/rplidar.rules

[cpp]  view plain  copy
 
print ? 在CODE上查看代码片 派生到我的代码片
  1. # set the udev rule , make the device_port be fixed by rplidar  
  2. #  
  3. KERNEL=="ttyUSB*", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", MODE:="0777", SYMLINK+="rplidar"  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. # set the udev rule , make the device_port be fixed by rplidar  
  2. #  
  3. KERNEL=="ttyUSB*", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", MODE:="0777", SYMLINK+="rplidar"  


创建生效后重新插拔USB。

用下面的命令就可以查看对应的映射结果了。

[cpp]  view plain  copy
 
print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ ls -l /dev |grep ttyUSB  
  2. lrwxrwxrwx  1 root   root             7 Apr 14 13:18 rplidar -> ttyUSB0  
  3. crwxrwxrwx  1 root   uucp      188,   0 Apr 14 13:18 ttyUSB0  
  4. yhzhao@yhzhao:~$   
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ ls -l /dev |grep ttyUSB  
  2. lrwxrwxrwx  1 root   root             7 Apr 14 13:18 rplidar -> ttyUSB0  
  3. crwxrwxrwx  1 root   uucp      188,   0 Apr 14 13:18 ttyUSB0  
  4. yhzhao@yhzhao:~$   


二. USB hub的多端口映射:

同一个hub连接设备base和laser, 将其重映射换名称到  slamtec_base_  和  slamtec_laser_

[cpp]  view plain  copy
 
print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ lsusb   
  2. Bus 002 Device 002: ID 0bda:0411 Realtek Semiconductor Corp.   
  3. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub  
  4. Bus 001 Device 004: ID 04f2:b48c Chicony Electronics Co., Ltd   
  5. Bus 001 Device 009: ID 046d:c31c Logitech, Inc. Keyboard K120  
  6. Bus 001 Device 011: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light  
  7. Bus 001 Device 012: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light  
  8. Bus 001 Device 005: ID 0bda:5411 Realtek Semiconductor Corp.   
  9. Bus 001 Device 008: ID 24ae:1100    
  10. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ lsusb   
  2. Bus 002 Device 002: ID 0bda:0411 Realtek Semiconductor Corp.   
  3. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub  
  4. Bus 001 Device 004: ID 04f2:b48c Chicony Electronics Co., Ltd   
  5. Bus 001 Device 009: ID 046d:c31c Logitech, Inc. Keyboard K120  
  6. Bus 001 Device 011: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light  
  7. Bus 001 Device 012: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light  
  8. Bus 001 Device 005: ID 0bda:5411 Realtek Semiconductor Corp.   
  9. Bus 001 Device 008: ID 24ae:1100    
  10. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  


可以看到hub的信息,同时hub上连接了两个串口转USB的设备,并且两者硬件使用的是一样的, 一种是依据设备的device配合一起分别定义udev规则,同上。

Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

现提供固定hub的每个端口的映射。

 udevadm info --attribute-walk --path=/sys/bus/usb-serial/devices/ttyUSB0

说明参见: http://askubuntu.com/questions/49910/how-to-distinguish-between-identical-usb-to-serial-adapters


可以找到hub端口的不同:

[cpp]  view plain  copy
 
print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ udevadm info --attribute-walk --path=/sys/bus/usb-serial/devices/ttyUSB1  
  2.   
  3. Udevadm info starts with the device specified by the devpath and then  
  4. walks up the chain of parent devices. It prints for every device  
  5. found, all possible attributes in the udev rules key format.  
  6. A rule to match, can be composed by the attributes of the device  
  7. and the attributes from one single parent device.  
  8.   
  9.   looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.2/1-2.2:1.0/ttyUSB1':  
  10.     KERNEL=="ttyUSB1"  
  11.     SUBSYSTEM=="usb-serial"  
  12.     DRIVER=="cp210x"  
  13.     ATTR{port_number}=="0"  
  14.   
  15.   looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.2/1-2.2:1.0':  
  16.     KERNELS=="1-2.2:1.0"  
  17.     SUBSYSTEMS=="usb"  
  18.     DRIVERS=="cp210x"  
  19.     ATTRS{bInterfaceClass}=="ff"  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ udevadm info --attribute-walk --path=/sys/bus/usb-serial/devices/ttyUSB1  
  2.   
  3. Udevadm info starts with the device specified by the devpath and then  
  4. walks up the chain of parent devices. It prints for every device  
  5. found, all possible attributes in the udev rules key format.  
  6. A rule to match, can be composed by the attributes of the device  
  7. and the attributes from one single parent device.  
  8.   
  9.   looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.2/1-2.2:1.0/ttyUSB1':  
  10.     KERNEL=="ttyUSB1"  
  11.     SUBSYSTEM=="usb-serial"  
  12.     DRIVER=="cp210x"  
  13.     ATTR{port_number}=="0"  
  14.   
  15.   looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.2/1-2.2:1.0':  
  16.     KERNELS=="1-2.2:1.0"  
  17.     SUBSYSTEMS=="usb"  
  18.     DRIVERS=="cp210x"  
  19.     ATTRS{bInterfaceClass}=="ff"  


[cpp]  view plain  copy
 
print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ udevadm info --attribute-walk --path=/sys/bus/usb-serial/devices/ttyUSB0  
  2.   
  3. Udevadm info starts with the device specified by the devpath and then  
  4. walks up the chain of parent devices. It prints for every device  
  5. found, all possible attributes in the udev rules key format.  
  6. A rule to match, can be composed by the attributes of the device  
  7. and the attributes from one single parent device.  
  8.   
  9.   looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.1/1-2.1:1.0/ttyUSB0':  
  10.     KERNEL=="ttyUSB0"  
  11.     SUBSYSTEM=="usb-serial"  
  12.     DRIVER=="cp210x"  
  13.     ATTR{port_number}=="0"  
  14.   
  15.   looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.1/1-2.1:1.0':  
  16.     KERNELS=="1-2.1:1.0"  
  17.     SUBSYSTEMS=="usb"  
  18.     DRIVERS=="cp210x"  
  19.     ATTRS{bInterfaceClass}=="ff"  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ udevadm info --attribute-walk --path=/sys/bus/usb-serial/devices/ttyUSB0  
  2.   
  3. Udevadm info starts with the device specified by the devpath and then  
  4. walks up the chain of parent devices. It prints for every device  
  5. found, all possible attributes in the udev rules key format.  
  6. A rule to match, can be composed by the attributes of the device  
  7. and the attributes from one single parent device.  
  8.   
  9.   looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.1/1-2.1:1.0/ttyUSB0':  
  10.     KERNEL=="ttyUSB0"  
  11.     SUBSYSTEM=="usb-serial"  
  12.     DRIVER=="cp210x"  
  13.     ATTR{port_number}=="0"  
  14.   
  15.   looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.1/1-2.1:1.0':  
  16.     KERNELS=="1-2.1:1.0"  
  17.     SUBSYSTEMS=="usb"  
  18.     DRIVERS=="cp210x"  
  19.     ATTRS{bInterfaceClass}=="ff"  



#参考   http://askubuntu.com/questions/49910/how-to-distinguish-between-identical-usb-to-serial-adapters

#lsusb  

#udevadm info --attribute-walk --path=/sys/bus/usb-serial/devices/ttyUSB0

[html]  view plain  copy
 
print ? 在CODE上查看代码片 派生到我的代码片
  1.     SUBSYSTEMS=="usb"  
  2.     DRIVERS=="cp210x"  
  3.     ATTRS{bInterfaceClass}=="ff"  
  4.     ATTRS{bInterfaceSubClass}=="00"  
  5.     ATTRS{bInterfaceProtocol}=="00"  
  6.     ATTRS{bNumEndpoints}=="02"  
  7.     ATTRS{supports_autosuspend}=="1"  
  8.     ATTRS{bAlternateSetting}==" 0"  
  9.     ATTRS{bInterfaceNumber}=="00"  
  10.     ATTRS{interface}=="CP2102 USB to UART Bridge Controller"  
  11. KERNEL=="ttyUSB*", KERNELS=="1-2.1", MODE:="0777",SYMLINK+="slamtec_base_"  
  12. KERNEL=="ttyUSB*", KERNELS=="1-2.2", MODE:="0777",SYMLINK+="slamtec_laser_"  
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1.     SUBSYSTEMS=="usb"  
  2.     DRIVERS=="cp210x"  
  3.     ATTRS{bInterfaceClass}=="ff"  
  4.     ATTRS{bInterfaceSubClass}=="00"  
  5.     ATTRS{bInterfaceProtocol}=="00"  
  6.     ATTRS{bNumEndpoints}=="02"  
  7.     ATTRS{supports_autosuspend}=="1"  
  8.     ATTRS{bAlternateSetting}==" 0"  
  9.     ATTRS{bInterfaceNumber}=="00"  
  10.     ATTRS{interface}=="CP2102 USB to UART Bridge Controller"  
  11. KERNEL=="ttyUSB*", KERNELS=="1-2.1", MODE:="0777",SYMLINK+="slamtec_base_"  
  12. KERNEL=="ttyUSB*", KERNELS=="1-2.2", MODE:="0777",SYMLINK+="slamtec_laser_"  

重新拔插。可以看到:

ls -l  /dev   

[cpp]  view plain  copy
 
print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ ls -l /dev|grep ttyUSB  
  2. lrwxrwxrwx  1 root   root             7 Apr 14 13:45 rplidar -> ttyUSB1  
  3. lrwxrwxrwx  1 root   root             7 Apr 14 13:45 slamtec_base_ -> ttyUSB0  
  4. lrwxrwxrwx  1 root   root             7 Apr 14 13:45 slamtec_laser_ -> ttyUSB1  
  5. crwxrwxrwx  1 root   uucp      188,   0 Apr 14 13:45 ttyUSB0  
  6. crwxrwxrwx  1 root   dialout   188,   1 Apr 14 13:45 ttyUSB1  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. yhzhao@yhzhao:~$ ls -l /dev|grep ttyUSB  
  2. lrwxrwxrwx  1 root   root             7 Apr 14 13:45 rplidar -> ttyUSB1  
  3. lrwxrwxrwx  1 root   root             7 Apr 14 13:45 slamtec_base_ -> ttyUSB0  
  4. lrwxrwxrwx  1 root   root             7 Apr 14 13:45 slamtec_laser_ -> ttyUSB1  
  5. crwxrwxrwx  1 root   uucp      188,   0 Apr 14 13:45 ttyUSB0  
  6. crwxrwxrwx  1 root   dialout   188,   1 Apr 14 13:45 ttyUSB1  





参考:http://blog.youkuaiyun.com/zyh821351004/article/details/41577105

<think>好的,用户想在树莓派上集成YOLOv5和激光雷达进行目标检测或距离测量,需要安装配置教程。我需要先理清楚各个组件的安装步骤和可能的依赖关系。 首先,树莓派的基础系统准备。用户可能已经安装了ROS,但需要确认版本。树莓派常用的ROS版本是Melodic或Noetic,根据操作系统而定。比如,Raspberry Pi OS Bullseye可能对应ROS Noetic。安装ROS后,需要配置激光雷达的驱动,比如使用roslaunch命令启动激光雷达节点,像用户提到的引用[1]中的例子:roslaunch simple_follower laser_follower.launch [^1]。这部分需要确保激光雷达型号对应的ROS包正确安装。 然后是YOLOv5的安装。树莓派的ARM架构可能不支持直接用PyTorch,需要查看官方是否提供ARM兼容的版本。用户可能需要从源码编译PyTorch或者使用预编译的wheel。此外,YOLOv5的依赖库如torchvision、numpy等也要安装,可能遇到依赖冲突,需要处理。 接下来是NVIDIA驱动的安装,但树莓派通常没有NVIDIA GPU,所以用户提到的引用[2]可能不适用,除非用户使用了外置的NVIDIA设备比如Jetson系列。这里可能存在误解,需要澄清。如果是树莓派本身,不需要安装NVIDIA驱动,所以这部分可能可以跳过,避免系统损坏的风险[^2]。 硬件连接方面,需要确保激光雷达正确连接到树莓派的USB或GPIO接口,并检查权限问题,可能需要配置udev规则。软件集成部分,需要将YOLOv5的目标检测结果与激光雷达的点云数据融合,可能需要用到ROS的话题订阅和发布,以及坐标变换(TF)来处理数据同步和空间对齐。 性能优化是关键,因为树莓派的计算资源有限。可以考虑使用轻量级模型,如YOLOv5s,或者进行模型量化、剪枝。另外,使用OpenCV的DNN模块或者TensorRT加速推理可能有效,但需要根据树莓派的具体配置调整。 测试环节,需要分别验证YOLOv5的目标检测和激光雷达的测距功能,再尝试整合两者。例如,检测到目标后,获取其中心坐标,并在激光雷达数据中找到对应位置的距离值。 可能遇到的问题包括:环境配置的依赖冲突、硬件兼容性问题、性能瓶颈等。需要建议用户逐步排查,查看日志,利用ROS的调试工具如rviz和rqt。 最后,生成的相关问题需要围绕安装步骤的细节、性能优化方法、硬件兼容性以及应用场景展开,帮助用户进一步解决可能遇到的挑战。</think>### 树莓派集成YOLOv5与激光雷达的安装配置教程 #### 1. **系统与环境准备** - **树莓派系统**: 建议使用 **Raspberry Pi OS (64-bit Bullseye)**,确保系统更新: ```bash sudo apt update && sudo apt upgrade -y ``` - **ROS安装**: 若需激光雷达与ROS通信,推荐安装 **ROS Noetic**(对应Debian Bullseye)。安装步骤参考官方文档,完成后配置工作空间。 - **Python环境**: 使用虚拟环境管理依赖(如`venv`或`conda`): ```bash python3 -m venv yolov5_env source yolov5_env/bin/activate ``` #### 2. **激光雷达配置** - **驱动安装**: 根据激光雷达型号(如RPLIDAR或YDLIDAR)安装对应ROS包。例如: ```bash sudo apt install ros-noetic-rplidar-ros ``` - **启动雷达节点**: ```bash roslaunch rplidar_ros rplidar.launch # 示例命令,实际需匹配型号 ``` - **验证数据**: 使用`rostopic echo /scan`查看激光雷达话题数据。 #### 3. **YOLOv5部署** - **依赖安装**: ```bash pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/arm # ARM兼容版本 pip install ultralytics # 包含YOLOv5 ``` - **模型下载**: ```bash git clone https://github.com/ultralytics/yolov5 cd yolov5 pip install -r requirements.txt ``` - **测试推理**: ```python import torch model = torch.hub.load('ultralytics/yolov5', 'yolov5s') results = model('https://ultralytics.com/images/zidane.jpg') results.print() ``` #### 4. **硬件集成与数据同步** - **通信框架**: 使用ROS的`cv_bridge`将摄像头图像转为ROS话题,与激光雷达的`/scan`话题时间同步。 - **坐标对齐**: 通过TF树将摄像头与激光雷达的坐标系关联,例如: ```xml <node pkg="tf" type="static_transform_publisher" name="lidar_to_cam" args="0 0 0 0 0 0 camera_link lidar_link 100"/> ``` - **距离计算**: 检测目标后,提取其像素中心坐标,映射到激光雷达极坐标系中获取距离值。 #### 5. **性能优化** - **模型轻量化**: 使用YOLOv5s或转换为ONNX/TensorRT格式。 - **线程处理**: 使用ROS线程订阅器或异步推理(如`rospy.ServiceProxy`)。 - **电源管理**: 为树莓派配备5V/3A电源,避免因功率不足导致硬件不稳定。 #### 6. **验证与调试** - **独立测试**: - 运行YOLOv5检测图像:`python detect.py --source 0`(摄像头输入) - 检查激光雷达数据:`rostopic echo /scan > scan_data.log` - **集成测试**: ```python # 示例ROS节点代码(伪代码) def callback(image_msg, lidar_msg): img = cv_bridge.imgmsg_to_cv2(image_msg) results = model(img) target_center = calculate_center(results) distance = lidar_msg.ranges[angle_to_index(target_center)] rospy.loginfo(f"目标距离: {distance}m") ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值