# STEP 2 : How to working on Embedded

## STEP 2-1 : Remote SSH via VScode

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

<figure><img src="/files/vdWa5LDiQ78V5Xi4T9Oz" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/iww1WWNaDmuz7SdQwa7v" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/FnsQUvMhbnQ5MvsVpi1V" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/Uno01hcAakdGCrA5qA5J" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/qAadvLosRDAqe5O8QtQp" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/Kb5UA8YJ8fSBUBLx4pxL" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/NT6NkMIvVvPLv16hiH5L" 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="/files/MyVe8XQ1wCfL5foymvAu" 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="/files/mGHpfYbyXahAPVgDWHEV" alt=""><figcaption><p>คอมเม้นบรรทัดที่ 20 และเพิ่ม fstream ในบรรทัดที่ 21</p></figcaption></figure>

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

<figure><img src="/files/8lYODCkkZjrlPq69dofa" 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](/fpga-design/c-c++-programming-on-ultra96v2-fpga-board/application-c-c++-on-ultra96v2-part-1/step-3-installing-the-vitis-ai-runtime-packages.md)) เพราะว่าการคอมไพล์ที่ใช้คำสั่ง 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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/fpga-design/c-c++-programming-on-ultra96v2-fpga-board/application-c-c++-on-ultra96v2-part-2/step-2-how-to-working-on-embedded.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
