# STEP 2 : How to working on Embedded

## STEP 2-1 : Remote SSH via VScode

1. ทำการติดตั้ง Extension remote ssh ด้วย VScode&#x20;

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FnFXjAegAHxGo2hnvy7rh%2FkI0xg1n9.png?alt=media&#x26;token=6187a2a0-5016-448e-a45d-25997c61f0e3" alt=""><figcaption></figcaption></figure>

2. ทำการกด **F1** และเลือกไปที่การเชื่อมต่อ **Remote-SSH: Connect to host...** จากในรูป

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FqrYCso9CWRJnktUSX5Pu%2FKLnE1Zz-.png?alt=media&#x26;token=01dbf21e-48b0-469d-a830-57dd34b67d71" alt=""><figcaption></figcaption></figure>

3. ใส่ชื่อของบอร์ดที่จะทำการเชื่อมต่อด้วยซึ่งจะเป็น root@\[IP board] ที่จะเชื่อมต่อไปบอร์ด Ultra96v2

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F3iS1nJtjJQbuGaVe9KG6%2FHgWItPhA.png?alt=media&#x26;token=7e4c18fa-5d74-4e8e-a39d-d5f71554abe6" alt=""><figcaption></figcaption></figure>

4. ใส่ password ที่เชื่อมต่อคือ "root"

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F5b3ypEzhnKZhpLX03zFw%2FVLFU9-Zd.png?alt=media&#x26;token=a1ef7fb4-3cf1-4183-9b7e-6672de51a7b1" alt=""><figcaption></figcaption></figure>

รอจนเชื่อมต่อเรียบร้อยจะขึ้นสถานะการเชื่อมต่อ SSH สมบูรณ์ที่มุมซ้ายล่าง

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F601cQMtzfb9K8Sk4oB5Z%2FIkwgRtP5.png?alt=media&#x26;token=5ea3a36d-131b-43c6-bfc4-2f3b9d22c4d6" alt=""><figcaption></figcaption></figure>

5. ทำการทดลองเปิดไฟล์ **File > Open Folder** แล้วเข้าไปใน directory ที่ใช้รัน code ครั้งที่แล้ว ppdemo1216

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FrFfBH3GuOluONvfFplSI%2FvXxmvCwk.png?alt=media&#x26;token=d0fef917-d667-4257-b094-a13ab750612f" alt=""><figcaption></figcaption></figure>

6. ลองทำการแก้ไข code ดู

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FZCaMguUMZ2NZXQbx3RZP%2FWlus7fQ3.png?alt=media&#x26;token=1af86309-ee5c-48d4-84f3-fe975768ebe9" alt=""><figcaption></figcaption></figure>

## STEP 2-2 : Cross compiled for using on your Embedded board

ในขั้นตอนนี้เราจะสอนวิธีการทำการ Cross compiled เพื่อทำการ compiled code บนเครื่องคอมพิวเตอร์ของเราไปยังตัวบอร์ดที่จะใช้งาน&#x20;

การ Cross compiled คือการ Compile ที่สามารถนำไฟล์ Executable ไปรันบนเครื่องเป้าหมายได้นั่นเอง ในที่นี้เราคือบอร์ด Ultra96v2 ที่เราทำการทดสอบกันไปในขั้นตอนก่อนๆ โดยการทำงานของ Cross compile จะใช้ทรัพยากรของบอร์ดบนเครื่องของเรา โดยจะใช้ทั้ง Library, SDK, Firmware เพื่อนำมาใช้ในการ compile code&#x20;

### เมื่อไหรเราต้องใช้เครื่องมือ Cross compile ละ

1. เมื่อเราต้องการความสะดวกสะบายในการทำงานไม่ต้องโยนไฟล์ไปหลายรอบเพื่อทำการ Compile บนบอร์ดซ้ำๆ&#x20;
2. ใช้กับ Micro controller เพราะทรัพยากรบนบอร์ดจะแตกต่างกับเครื่องคอมพิวเตอร์ของเราทั้ง Hardware, Firmware และ Software&#x20;
3. เมื่อต้องใช้กับอุปกรณ์ที่ Spec ต่ำ เพื่อลดระยะเวลาในการ Compile code&#x20;
4. เมื่อเราต้องการจัดการทรัพยากรการทำงานของเครื่องเรากับเครื่องทดสอบให้ชัดเจน

<figure><img src="https://media.geeksforgeeks.org/wp-content/uploads/20220915123006/Crosscompiler-660x330.png" alt=""><figcaption></figcaption></figure>

1. &#x20;เปิด Terminal WSL Ubuntu ที่ท่านใช้งานบน VScode ด้วยการกด <mark style="background-color:orange;">**Ctrl + Shift + \`**</mark>และทำการติดตั้ง Gdown เพื่อใช้ในการโหลดไฟล์จาก Google drive โดยตรง

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FQFkSOdVPPPXl3tthIi8q%2Fimage.png?alt=media&#x26;token=01d734b7-0831-4d92-aa4c-da8a74b472d2" alt=""><figcaption></figcaption></figure>

{% code title="WSL terminal on VScode" %}

```bash
$ sudo apt-get update && sudo apt-get upgrade
$ pip install gdown
```

{% endcode %}

2. ทำการสร้างไฟล์ที่จะเรียกการติดตั้ง SDK และ Vitis-AI library ด้วยการสร้าง script

{% code title="WSL terminal on VScode" %}

```bash
$ cd && vim sdk_setup.sh
```

{% endcode %}

และทำการ Copy ไฟล์ script และนำไปวางใน vim ด้วยการกด Crtl + Shift + V เพื่อวางบนไฟล์ Script

```bash
#
# Copyright 2021 AIC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#!/bin/bash

# Download SDK and Vitis-AI runtime
gdown 1ob2rTI2TwfrTZum_6xczw1rdgLZgrJXF
wget https://www.xilinx.com/bin/public/openDownload?filename=vitis_ai_2021.1-r1.4.0.tar.gz -O vitis_ai_2021.1-r1.4.0.tar.gz
# Change mod for executable file SDK
chmod +x sdk.sh
# Set install path default
echo "The Cross Compiler will be installed in ~/petalinux_sdk_2021.1 by default"
install_path=~/petalinux_sdk_2021.1
# Create install path
if [ -d $install_path ]
then
echo ""
else
mkdir -p $install_path
fi
# Show install path set up SDK
echo $install_path|./sdk.sh

tar -xzvf vitis_ai_2021.1-r1.4.0.tar.gz -C $install_path/sysroots/cortexa72-cortexa53-xilinx-linux/

echo "Complete Cross Compiler installation"
echo ""
echo "Please run the following command to enable Cross Compiler"
echo "    source $install_path/environment-setup-cortexa72-cortexa53-xilinx-linux"
echo "If you run the above command failed, run the following commands to enable Cross Compiler"
echo "    unset LD_LIBRARY_PATH"
echo "    source $install_path/environment-setup-cortexa72-cortexa53-xilinx-linux"
echo ""

```

เมื่อต้องการ save ไฟล์ script ให้นิสิตทำการออกและ save ด้วยการกด Esc และพิมว่า :wq! เพื่อการเขียนทับและเซฟไฟล์

3. ตรวจสอบว่าไฟล์ Script สามารถมีข้อความที่ Copy ไปจากข้อสองสมบูรณ์หรือไม่ด้วยคำสั่ง&#x20;

{% code title="" %}

```bash
$ cat sdk_setup.sh
```

{% endcode %}

4. ทำการรัน script ที่สร้างเพื่อดาวน์โหลด SDK และติดตั้ง VART library ลงบน SDK

{% code title="WSL terminal on VScode" %}

```bash
$ chmod +x sdk_setup.sh && ./sdk_setup.sh 
```

{% endcode %}

{% hint style="success" %}
SDK และ VART library ที่ติดตั้งเพื่อใช้ในการ Cross compile ของนิสิตจะอยู่ที่ \~/petalinux\_sdk\_2021.1
{% endhint %}

5. ทำการดาวน์โหลดไฟล์ ppdemo และทำการแตกไฟล์ พร้อมทั้งเปิด VScode

{% code title="WSL terminal on VScode" %}

```bash
$ gdown 1VM9CjLNgLPzODbYCPWyv-lqewaOX8o6O
$ tar -zxvf pp_3d_detect.tar.gz
$ cd ppdemo1216 && code .
```

{% endcode %}

6. ทำการแก้ไขไฟล์ Source code เพื่อรองรับการทำงานของ การ stream ข้อมูลหรือฟังชัน fstream โดยการคอมเม้น library iostream ในบรรทัดที่ 20 และเพิ่ม library fstream เข้าไปต่อจากที่พึ่งคอมเม้น

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FC7McEnqkA91jiySixpLM%2Fimage.png?alt=media&#x26;token=6a6640f2-8f64-4a63-9159-4e34ed98b5b5" alt=""><figcaption><p>คอมเม้นบรรทัดที่ 20 และเพิ่ม fstream ในบรรทัดที่ 21</p></figcaption></figure>

7\. แก้ไขไฟล์ที่ต้องใช้ในการคอมไพล์ดังนี้

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F9f4NvDigsKDKzx0Kz84r%2Fimage.png?alt=media&#x26;token=b7333fd4-add4-44bf-9a99-64f262238a79" alt=""><figcaption></figcaption></figure>

* ทำการเปิด build.sh ตามหมายเลข 1&#x20;
* ทำการคอมเม้นบรรทัดที่ 1 ตามหมายเลข 2&#x20;
* ทำการ Copy คำสั่งที่ใช้ในการ Cross compile ด้านล่างลงไปวางตามหมายเลข 3

{% code title="build.sh" %}

```bash
result=0 && pkg-config --list-all | grep opencv4 && result=1
if [ $result -eq 1 ]; then
OPENCV_FLAGS=$(pkg-config --cflags --libs-only-L opencv4)
else
OPENCV_FLAGS=$(pkg-config --cflags --libs-only-L opencv)
fi
CXX=${CXX:-g++}

$CXX -std=c++17 -O2 -I. ${OPENCV_FLAGS} -o demo demo.cpp -lopencv_core -lopencv_video -lopencv_videoio -lopencv_imgproc -lopencv_imgcodecs -lopencv_highgui -lvitis_ai_library-pointpillars  -lvart-util -pthread -lglog 

```

{% endcode %}

การทำงานของสคริปต์คอมไพล์จะเห็นว่าบรรทัดที่ 10 และ 12 จากรูป จะแตกต่างจากก build.sh ในการทดลองในพาร์ทแรก([Part 1, Step 3-3](https://docs.aic-eec.com/fpga-design/c-c++-programming-on-ultra96v2-fpga-board/application-c-c++-on-ultra96v2-part-1/step-3-installing-the-vitis-ai-runtime-packages)) เพราะว่าการคอมไพล์ที่ใช้คำสั่ง g++ นำหน้าจะเป็นการใช้การ Compile โดยการใช้ทรัพยากรบนเครื่องของเราซึ่งครั้งที่แล้วคือใช้ทรัพยากรบนบอร์ด แต่ถ้าเราต้องการทำการ Cross compile บนเครื่องของเราจำเป็นที่จะต้องกำหนดตัวแปร CXX เพื่ออ้างอิงถึงตัวคอมไพลเลอร์ g++ ที่เราจะทำการเรียกใช้ผ่าน SDK ในขั้นตอนถัดไป

{% hint style="warning" %}
ถ้านิสิตต้องการเพิ่ม llibrary ที่จะใช้งาน จะสังเกตได้จากบรรทัดที่ 12 จะเห็นว่าในการคอมไพล์มีสัญลักษณ์ -I เพื่อสื่อถึงการ include ไฟล์ library เหมือนเวลาเราเลือกไลบรารี่ใน Arduino มาใช้งานนั่นเอง ดังนั้นถ้าเราต้องการใช้ library อย่างอืน สามารถเขียนเติมในบรรทัดที่ 12 โดยการเขียน&#x20;

-l\[ชื่อไลบรารี่ที่ใช้]
{% endhint %}

8. ทำการ source SDK ของบอร์ด Ultra96v2 เพื่อใช้ทรัพยากรของบอร์ด

{% code title="WSL terminal on VScode" %}

```bash
$ source ~/petalinux_sdk_2021.1/environment-setup-cortexa72-cortexa53-xilinx-linux
```

{% endcode %}

9. ทำการ Cross compiled&#x20;

{% code title="WSL terminal on VScode" %}

```bash
$ cd ~/ppdemo1216/
$ ./build.sh
```

{% endcode %}

{% hint style="success" %}
เมื่อทำการทดลองนี้จบหาต้องการทำการ Cross compile เพื่อเช็คการทำงานของโปรแกรม จะสามารถทำซ้ำขั้นตอนที่ 8-9 ในขั้นตอนนี้ได้ ไม่ต้องทำการติดตั้งใหม่&#x20;
{% endhint %}

10. โยนไฟล์ไปทดสอบบนบอร์ด Ultra96v2 ผ่านการ secure copy

{% code title="WSL terminal on VScode" %}

```bash
$ scp demo root@[IP ของบอร์ด]
```

{% endcode %}

## *ทำความเข้าใจการทำงานของ Code*

<https://github.com/Xilinx/Vitis-AI/blob/master/demo/Vitis-AI-Library/samples/pointpillars/test_bin_pointpillars.cpp><br>

| <h3>                    $$Keyword$$</h3> | <h3>                                    $$Reference$$</h3>                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| using namespace                          | <ul><li><a href="https://www.geeksforgeeks.org/namespace-in-c/"><https://www.geeksforgeeks.org/namespace-in-c/></a></li><li><a href="https://www.youtube.com/watch?v=etQX4Mme2f4&#x26;t=599s"><https://www.youtube.com/watch?v=etQX4Mme2f4&#x26;t=599s></a></li></ul>                                                                                                                                                                                                                                         |
| Extern                                   | <ul><li><a href="https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/"> Theextern keyword extends the function’s visibility to the whole program, the function can be used</a><a href="https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/"> </a><a href="https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/">(called)</a><a href="https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/"> anywhere in any of the files of the whole program</a> </li></ul> |
| Inline                                   | <ul><li><a href="https://www.geeksforgeeks.org/inline-functions-cpp/"><https://www.geeksforgeeks.org/inline-functions-cpp/></a></li></ul>                                                                                                                                                                                                                                                                                                                                                                     |
| Template                                 | <ul><li><a href="https://www.geeksforgeeks.org/templates-cpp/?ref=gcse"><https://www.geeksforgeeks.org/templates-cpp/?ref=gcse></a></li></ul>                                                                                                                                                                                                                                                                                                                                                                 |
| Ifstream                                 | <ul><li><a href="https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm"><https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm></a></li></ul>                                                                                                                                                                                                                                                                                                                                               |
| Struct stat                              | <ul><li><a href="https://stackoverflow.com/questions/24854572/struct-stat-c-not-compiling-in-different-computer"><https://stackoverflow.com/questions/24854572/struct-stat-c-not-compiling-in-different-computer></a></li><li><a href="https://pubs.opengroup.org/onlinepubs/007904875/basedefs/sys/stat.h.html"><https://pubs.opengroup.org/onlinepubs/007904875/basedefs/sys/stat.h.html></a></li></ul>                                                                                                     |
| Auto                                     | <ul><li><a href="https://www.geeksforgeeks.org/type-inference-in-c-auto-and-decltype/?ref=gcse"><https://www.geeksforgeeks.org/type-inference-in-c-auto-and-decltype/?ref=gcse></a></li></ul>                                                                                                                                                                                                                                                                                                                 |

### Include libraries

<figure><img src="https://paper-attachments.dropboxusercontent.com/s_F0A42A2E610F7B53E18C30AE5CBF3CC539CFC3FC195557D548631A4DA2623026_1643564210236_Screenshot+from+2022-01-31+00-36-22.png" alt=""><figcaption></figcaption></figure>

### Function&#x20;

Read file function `void myreadfile(T* dest, int size1, std::string filename); // Read file`

<figure><img src="https://paper-attachments.dropboxusercontent.com/s_F3F65A486320FAAD40D7CFF47C095B201121A57F9533013089797275F4CFF941_1643709857994_Year3_Course-Page-4.drawio.png" alt=""><figcaption></figcaption></figure>

Get file length function `int getfloatfilelen(const std::string& file);`

<figure><img src="https://paper-attachments.dropboxusercontent.com/s_F3F65A486320FAAD40D7CFF47C095B201121A57F9533013089797275F4CFF941_1643709940782_Screenshot+from+2022-02-01+17-05-31.png" alt=""><figcaption></figcaption></figure>

Display data function `void get_display_data(DISPLAY_PARAM&);`\
Create Matrix for store dataWe can using cv::mat as a class for create the matrix for store the data of point cloud and image `cv::mat`<br>

### Main code (Flowchart)

In this demo, we need to insert 5 arguments:

1. executed file (get this file after compile the build.sh)
2. [model 1 :](https://www.dropbox.com/scl/fi/nym75g4ue1sdl5il5e1cd/Untitled.paper?dl=0\&rlkey=o4l1fw8fn4lg452trsvb1aw55#:uid=637034167026346266047010\&h2=model-1) pointpillars\_kitti\_12000\_0\_pt
3. [model 2](https://www.dropbox.com/scl/fi/nym75g4ue1sdl5il5e1cd/Untitled.paper?dl=0\&rlkey=o4l1fw8fn4lg452trsvb1aw55#:uid=048984937656559534284601\&h2=model-2): pointpillars\_kitti\_12000\_1\_pt
4. .bin file is the file of Point cloud&#x20;
5. .png file is the file of image.

sample: `./test_bin_pointpillars  pointpillars_kitti_12000_0_pt pointpillars_kitti_12000_1_pt sample_pointpillars.bin  sample_pointpillars.png`

<figure><img src="https://paper-attachments.dropboxusercontent.com/s_F3F65A486320FAAD40D7CFF47C095B201121A57F9533013089797275F4CFF941_1643709814407_Year3_Course-Page-3.drawio.png" alt=""><figcaption></figcaption></figure>
