---
# System prepended metadata

title: 車載物聯網

---

# 車載物聯網

**<...>表示該處位置須按照當下狀況決定**
**ex. <containerId> 表該處要填入欲操作的container id**

## docker 

- build image
```
docker build -t <image-tag> .
```
- run image with camera
```
 docker run -it --device=/dev/video0:/dev/video0 <image-tag>
```

- 利用`-v`參數，使得實體主機的資料夾路徑 Mapping 到 Container 的資料夾路徑
```
docker run -it -v ~/ROS2Docker:/ROS2Docker --device=/dev/video0:/dev/video0 ros2:latest bash
```

- 啟動已存在的container
```
docker start <containerId>
```

- 進入已啟動的container
```
docker exec -it <containerId> bash
```

- `-d`參數可以使container背景執行
- port mapping
```
docker run -d -p 8080:80 ytmp3api:latest
```
![image](https://hackmd.io/_uploads/BJrpbQ_DA.png)

    
- **利用docker run創建新的container的附帶條件(device、volume)已經被記錄下來**
- **利用docker start啟動已經存在的container時不用再次指定**

## ROS
- create package
```
ros2 pkg create --build-type ament_python --node-name <nodeName> <packageName>
```
for example:
```
ros2 pkg create --build-type ament_python --node-name hello_world beginner_tutorials_py
```

- build package
    - 檢查dependency是否安裝
    ```
    cd <ros2_workspace>
    rosdep install -i --from-path src --rosdistro humble -y
    ```
    - build package
    ```
    colcon build --symlink-install --packages-select <packageName>
    ```
    - **不要忘了source setup讓ROS2知道package的位置**
    ```
    source <ros2_workspace>/install/setup.bash
    ```

- Run Node
```
ros2 run <packageName> <nodeName>
```
- 現成的webcam node
    - 安裝
    ```
    sudo apt install ros-humble-usb-cam
    ``` 
    - 啟動
    ```
    ros2 run usb_cam usb_cam_node_exe
    ``` 


## python虛擬環境
喜報，ros2跟anaconda不合

依據官方文件，請愛用virtualenv
https://docs.ros.org/en/humble/How-To-Guides/Using-Python-Packages.html#installing-via-a-virtual-environment
    
下面是virtualenv的官方文件
 https://virtualenv.pypa.io/en/latest/index.html
    
## matplotlob opencv boom
這兩玩意在linux中使用的時候opencv須安裝不帶GUI的版本`opencv-python-headless`，因為帶GUI的opencv使用的QT跟matplotlib使用的QT會起衝突，然後就爆炸了，屌你老母

    
## reference
- https://github.com/twtrubiks/docker-tutorial
- https://koding.work/how-to-use-docker-to-run-ros2-app/
- https://jiangchengcheng.notion.site/ROS_Robot-Operating-System-4d6b130b78544415bafd68bb1a78f3ad
- https://hackmd.io/@evshary/ROS2Note
- https://ithelp.ithome.com.tw/users/20135014/ironman/6784
- https://hackmd.io/@jerrychu/S1QvFG98h
- https://answers.ros.org/question/371083/how-to-use-python-virtual-environments-with-ros2/
- https://dotblogs.com.tw/grayyin/2019/06/25/142934
- https://stackoverflow.com/questions/64043973/matplotlib-interactive-mode-wont-work-no-matter-what-i-do
- https://github.com/sayakpaul/Adventures-in-TensorFlow-Lite
- https://github.com/opencv/opencv-python/issues/386
- https://hackmd.io/@9kDD5OucTsigU4yUanEPQg/HJsVB5Ce0