当前位置:   article > 正文

ROS2使用usb_cam_ros2 usb_cam

ros2 usb_cam

1、安装usb_cam

sudo apt-get install ros-humble-usb-cam

2、启动usb_cam

ros2 launch usb_cam camera.launch.py 

但是这里会出现错误

Caught exception in launch (see debug for traceback): Caught exception when trying to load file of format [py]: No module named 'pydantic'

3、下载pydantic库

pip install pydantic  -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

4、下载完成后继续启动

ros2 launch usb_cam camera.launch.py 

Pydantic 版本要求 @root_validator 使用时必须指定 skip_on_failure=True,因为默认的 pre=False 设定可能会导致验证失败。

5、解决

找到/opt//ros/humble/share/usb_cam/launch下的 camera_config.py,并使用vim打开,并修改里面的配置文件

  • 移除 @root_validator 避免 Pydantic 2.5 版本中 @root_validator 使用上的限制和不兼容问题。

原来的代码

  1. from pathlib import Path
  2. from typing import List, Optional
  3. from ament_index_python.packages import get_package_share_directory
  4. from pydantic import BaseModel, root_validator, validator
  5. USB_CAM_DIR = get_package_share_directory('usb_cam')
  6. class CameraConfig(BaseModel):
  7. name: str = 'camera1'
  8. param_path: Path = Path(USB_CAM_DIR, 'config', 'params_1.yaml')
  9. remappings: Optional[List]
  10. namespace: Optional[str]
  11. @validator('param_path')
  12. def validate_param_path(cls, value):
  13. if value and not value.exists():
  14. raise FileNotFoundError(f'Could not find parameter file: {value}')
  15. return value
  16. @root_validator
  17. def validate_root(cls, values):
  18. name = values.get('name')
  19. remappings = values.get('remappings')
  20. if name and not remappings:
  21. # Automatically set remappings if name is set
  22. remappings = [
  23. ('image_raw', f'{name}/image_raw'),
  24. ('image_raw/compressed', f'{name}/image_compressed'),
  25. ('image_raw/compressedDepth', f'{name}/compressedDepth'),
  26. ('image_raw/theora', f'{name}/image_raw/theora'),
  27. ('camera_info', f'{name}/camera_info'),
  28. ]
  29. values['remappings'] = remappings
  30. return values

修改后的

  1. # Copyright 2023 usb_cam Authors
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are met:
  5. #
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. #
  9. # * Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. #
  13. # * Neither the name of the usb_cam Authors nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. from pathlib import Path
  29. from typing import List, Optional
  30. from ament_index_python.packages import get_package_share_directory
  31. #from pydantic import BaseModel, root_validator, validator
  32. from pydantic import BaseModel, model_validator, validator
  33. USB_CAM_DIR = get_package_share_directory('usb_cam')
  34. class CameraConfig(BaseModel):
  35. name: str = 'camera1'
  36. param_path: Path = Path(USB_CAM_DIR, 'config', 'params_1.yaml')
  37. remappings: Optional[List[str]] = []
  38. namespace: Optional[str] = None
  39. @validator('param_path')
  40. def validate_param_path(cls, value):
  41. if value and not value.exists():
  42. raise FileNotFoundError(f'Could not find parameter file: {value}')
  43. return value
  44. # @root_validator
  45. # def validate_root(cls, values):
  46. # name = values.get('name')
  47. # remappings = values.get('remappings')
  48. # if name and not remappings:
  49. # # Automatically set remappings if name is set
  50. # remappings = [
  51. # ('image_raw', f'{name}/image_raw'),
  52. # ('image_raw/compressed', f'{name}/image_compressed'),
  53. # ('image_raw/compressedDepth', f'{name}/compressedDepth'),
  54. # ('image_raw/theora', f'{name}/image_raw/theora'),
  55. # ('camera_info', f'{name}/camera_info'),
  56. # ]
  57. # values['remappings'] = remappings
  58. # return value
  59. @validator('name')
  60. def validate_name(cls, value):
  61. if not value:
  62. raise ValueError("Name is required")
  63. return value
  64. def __init__(self, **data):
  65. super().__init__(**data)
  66. if self.name:
  67. # Automatically set remappings if name is set
  68. self.remappings.extend([
  69. ('image_raw', f'{self.name}/image_raw'),
  70. ('image_raw/compressed', f'{self.name}/image_compressed'),
  71. ('image_raw/compressedDepth', f'{self.name}/compressedDepth'),
  72. ('image_raw/theora', f'{self.name}/image_raw/theora'),
  73. ('camera_info', f'{self.name}/camera_info'),
  74. ])

5、继续启动

OK,成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/263091
推荐阅读
相关标签
  

闽ICP备14008679号