STEP 2 : How to working on Embedded

ทำงานกับ Embedded board ในทางโปรแกรมยังไงให้ง่ายและมีประสิทธิภาพ

STEP 2-1 : Remote SSH via VScode

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

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

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

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

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

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

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

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

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

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

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

  1. เมื่อเราต้องการความสะดวกสะบายในการทำงานไม่ต้องโยนไฟล์ไปหลายรอบเพื่อทำการ Compile บนบอร์ดซ้ำๆ

  2. ใช้กับ Micro controller เพราะทรัพยากรบนบอร์ดจะแตกต่างกับเครื่องคอมพิวเตอร์ของเราทั้ง Hardware, Firmware และ Software

  3. เมื่อต้องใช้กับอุปกรณ์ที่ Spec ต่ำ เพื่อลดระยะเวลาในการ Compile code

  4. เมื่อเราต้องการจัดการทรัพยากรการทำงานของเครื่องเรากับเครื่องทดสอบให้ชัดเจน

  1. เปิด Terminal WSL Ubuntu ที่ท่านใช้งานบน VScode ด้วยการกด Ctrl + Shift + `และทำการติดตั้ง Gdown เพื่อใช้ในการโหลดไฟล์จาก Google drive โดยตรง

WSL terminal on VScode
$ sudo apt-get update && sudo apt-get upgrade
$ pip install gdown
  1. ทำการสร้างไฟล์ที่จะเรียกการติดตั้ง SDK และ Vitis-AI library ด้วยการสร้าง script

WSL terminal on VScode
$ cd && vim sdk_setup.sh

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

#
# 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! เพื่อการเขียนทับและเซฟไฟล์

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

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

WSL terminal on VScode
$ chmod +x sdk_setup.sh && ./sdk_setup.sh 

SDK และ VART library ที่ติดตั้งเพื่อใช้ในการ Cross compile ของนิสิตจะอยู่ที่ ~/petalinux_sdk_2021.1

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

WSL terminal on VScode
$ gdown 1VM9CjLNgLPzODbYCPWyv-lqewaOX8o6O
$ tar -zxvf pp_3d_detect.tar.gz
$ cd ppdemo1216 && code .
  1. ทำการแก้ไขไฟล์ Source code เพื่อรองรับการทำงานของ การ stream ข้อมูลหรือฟังชัน fstream โดยการคอมเม้น library iostream ในบรรทัดที่ 20 และเพิ่ม library fstream เข้าไปต่อจากที่พึ่งคอมเม้น

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

  • ทำการเปิด build.sh ตามหมายเลข 1

  • ทำการคอมเม้นบรรทัดที่ 1 ตามหมายเลข 2

  • ทำการ Copy คำสั่งที่ใช้ในการ Cross compile ด้านล่างลงไปวางตามหมายเลข 3

build.sh
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 

การทำงานของสคริปต์คอมไพล์จะเห็นว่าบรรทัดที่ 10 และ 12 จากรูป จะแตกต่างจากก build.sh ในการทดลองในพาร์ทแรก(Part 1, Step 3-3) เพราะว่าการคอมไพล์ที่ใช้คำสั่ง g++ นำหน้าจะเป็นการใช้การ Compile โดยการใช้ทรัพยากรบนเครื่องของเราซึ่งครั้งที่แล้วคือใช้ทรัพยากรบนบอร์ด แต่ถ้าเราต้องการทำการ Cross compile บนเครื่องของเราจำเป็นที่จะต้องกำหนดตัวแปร CXX เพื่ออ้างอิงถึงตัวคอมไพลเลอร์ g++ ที่เราจะทำการเรียกใช้ผ่าน SDK ในขั้นตอนถัดไป

ถ้านิสิตต้องการเพิ่ม llibrary ที่จะใช้งาน จะสังเกตได้จากบรรทัดที่ 12 จะเห็นว่าในการคอมไพล์มีสัญลักษณ์ -I เพื่อสื่อถึงการ include ไฟล์ library เหมือนเวลาเราเลือกไลบรารี่ใน Arduino มาใช้งานนั่นเอง ดังนั้นถ้าเราต้องการใช้ library อย่างอืน สามารถเขียนเติมในบรรทัดที่ 12 โดยการเขียน

-l[ชื่อไลบรารี่ที่ใช้]

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

WSL terminal on VScode
$ source ~/petalinux_sdk_2021.1/environment-setup-cortexa72-cortexa53-xilinx-linux
  1. ทำการ Cross compiled

WSL terminal on VScode
$ cd ~/ppdemo1216/
$ ./build.sh

เมื่อทำการทดลองนี้จบหาต้องการทำการ Cross compile เพื่อเช็คการทำงานของโปรแกรม จะสามารถทำซ้ำขั้นตอนที่ 8-9 ในขั้นตอนนี้ได้ ไม่ต้องทำการติดตั้งใหม่

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

WSL terminal on VScode
$ scp demo root@[IP ของบอร์ด]

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

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

Include libraries

Function

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

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

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

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 : pointpillars_kitti_12000_0_pt

  3. model 2: pointpillars_kitti_12000_1_pt

  4. .bin file is the file of Point cloud

  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

Last updated

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University