2019年7月18日 星期四

Jetson Nano Uart C code

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

static int set_interface_attribs (int fd, int speed, int parity)
{
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    if (tcgetattr (fd, &tty) != 0)
    {
            printf ("error %d from tcgetattr\n", errno);
            return -1;
    }

    cfsetospeed (&tty, speed);
    cfsetispeed (&tty, speed);
    cfmakeraw(&tty); /* RAW mode */

    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
    // disable IGNBRK for mismatched speed tests; otherwise receive break
    // as \000 chars
    tty.c_iflag &= ~IGNBRK;         // disable break processing
    tty.c_lflag = 0;                // no signaling chars, no echo,
                                    // no canonical processing
    tty.c_oflag = 0;                // no remapping, no delays
    tty.c_cc[VMIN]  = 0;            // read doesn't block
    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

    tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                    // enable reading
    tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
    tty.c_cflag |= parity;
    tty.c_cflag &= ~CSTOPB;
    tty.c_cflag &= ~CRTSCTS;

    if (tcsetattr (fd, TCSANOW, &tty) != 0)
    {
            printf ("error %d from tcsetattr\n", errno);
            return -1;
    }
    return 0;
}

static void set_blocking (int fd, int should_block)
{
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    if (tcgetattr (fd, &tty) != 0)
    {
            printf ("error %d from tggetattr\n", errno);
            return;
    }

    tty.c_cc[VMIN]  = should_block ? 1 : 0;
    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    if (tcsetattr (fd, TCSANOW, &tty) != 0)
            printf ("error %d setting term attributes\n", errno);
}

int main(int argc, char**argv)
{
    const char *ttyName = "/dev/ttyTHS1";

    int ttyFd = open (ttyName, O_RDWR | O_NOCTTY | O_SYNC);
    if (ttyFd) {
        set_interface_attribs (ttyFd, B9600, 0);  // set speed to 115,200 bps, 8n1 (no parity)
        set_blocking (ttyFd, 0);                // set no blocking
    } else
        printf ("error %d opening %s: %s\n", errno, ttyName, strerror (errno));

  char data[5] = "1234"

  write(fd, data, 4);

  return 0;
}

2019年7月12日 星期五

Jetson Nano run program on startup

I want to run my own program automatically on startup. For example, the name of my program is dragon-eye

1. Create new file /etc/systemd/system/dragon-eye.service with content below

[Unit]
Description=Dragon Eye
After=nvargus-daemon.service

[Service]
ExecStart=/usr/local/bin/dragon-eye
Type=oneshot

[Install]
WantedBy=multi-user.target

2.Enable service on startup

sudo systemctl enable dragon-eye

3.Disable service

sudo systemctl disable dragon-eye

4.Run-time service control

sudo systemctl start dragon-eye

sudo systemctl stop dragon-eye

sudo systemctl status dragon-eye

5.Reference


2019年6月3日 星期一

Jetson Nano GPIO support

Jetson Nano GPIO 位在 J41 connector

https://www.jetsonhacks.com/nvidia-jetson-nano-j41-header-pinout/

使用GPIO首先要處理權限的問題, 不然只有 root 能操作GPIO.
依據以下命令新增一個 group 叫 gpio. 把想要操作GPIO的user加入這個group.
然後在每次export GPIO後更改sys file system相關檔案的權限

sudo groupadd -f -r gpio
sudo usermod -a -G gpio $USER
sudo cp /opt/nvidia/jetson-gpio/etc/99-gpio.rules /etc/udev/rules.d/

修改 /etc/udev/rules.d/99-gpio.rules

SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", \
    PROGRAM="/bin/sh -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"

將chmod 660 改為 chmod 666

sudo udevadm control --reload-rules && sudo udevadm trigger

將想要使用的GPIO export命令放在 /etc/profile.d/export-gpio.sh
這樣每次系統重啟就會自動把GPIO export好. 例如GPIO18

echo 18 > /sys/class/gpio/export

echo out > /sys/class/gpio/gpio18/direction

echo 1 > /sys/class/gpio/gpio18/value

最後要sudo reboot now

以c/c++來控制GPIO.

https://github.com/gigijoe/jetsonTX2GPIO

Input / Output 電路

https://www.jetsonhacks.com/2015/12/29/gpio-interfacing-nvidia-jetson-tx1/


2019年5月31日 星期五

Jetson Naon Camera Support

Jetson Nano 支援的 IMX219 Cmaera 連接到主板上CSI-2 Connector. 另外也支援USB UVC 類型的 camera


IMX219 Camera 可直接購買 Raspberry Pi camera module V2 直上, 該模組有800萬畫素, 解析度支援到 3280x2464

v4l2-ctl -d /dev/video0 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Index       : 0
Type        : Video Capture
Pixel Format: 'RG10'
Name        : 10-bit Bayer RGRG/GBGB
Size: Discrete 3280x2464
Interval: Discrete 0.048s (21.000 fps)
Size: Discrete 3280x1848
Interval: Discrete 0.036s (28.000 fps)
Size: Discrete 1920x1080
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.017s (60.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.017s (60.000 fps)


gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM),width=1280, height=720, framerate=30/1, format=NV12' ! nvvidconv ! xvimagesink -e

USB Camera基本上只要有UVC支援都能用,選擇非常多

測試驅動

gst-launch-1.0 v4l2src device=/dev/video0 ! image/jpeg,width=1280,height=720,framerate=30/1 ! jpegparse ! jpegdec ! xvimagesink sync=false

IMX219 Camera 顏色校正

https://devtalk.nvidia.com/default/topic/1051913/how-to-close-gstreamer-pipeline-in-python/?offset=14

sudo cp camera_overrides.isp /var/nvidia/nvcam/settings
sudo chmod 664 /var/nvidia/nvcam/settings/camera_overrides.isp
sudo chown root:root /var/nvidia/nvcam/settings/camera_overrides.isp

在coding過程中,若Camera使用中程式不正常中止或Camera buffer overflow需要重啟Camera Daemon才能再次正常工作

sudo systemctl restart nvargus-daemon

2019年5月23日 星期四

Jetson Nano USB OTG

Jetson Nano 在RJ-45 port旁邊有個micro USB插座, 找一條USB線連接PC就能夠從PC上經由USB Ethernet登入到Jetson Nano, 非常之方便

Jetson Nano的IP address固定是192.168.55.1

從PC SSH登入
$ ssh <username>@192.168.55.1


另外,以此方式執行遠端Jetson Nano上的程式,若有輸出圖形視窗會得到錯誤訊息並跳出程式

gigijoe@gigijoe-desktop:~/object-tracking/build$ ./object-tracking-cuda 0
Wait for camera stable ... Ok

(object-tracking-cuda:9198): Gtk-WARNING **: 18:09:00.626: cannot open display:

修改PC端 /etc/ssh/sshd_config
-#    X11Forwarding no
+    X11Forwarding yes

再重新登入執行程式,圖形視窗會出現在PC端,很神奇吧





2019年5月21日 星期二

Jetson Nano initial setup

入手Nvidia Jetson Nano先做個簡單的初始設定紀錄



官網的簡介
https://devblogs.nvidia.com/jetson-nano-ai-computing/

1.依據官網上的步驟把系統映像檔寫到MMC card (32GB up)
https://developer.nvidia.com/embedded/learn/get-started-jetson-nano-devkit

Reference
https://www.pyimagesearch.com/2016/07/11/compiling-opencv-with-cuda-support/

2.短路J48使用5V / 4A的power adapter供電

3.上電開機後經過一連串設定及 user name / password 進入Ubuntu Desktop

4.系統上有4GB DDR memory在之後compile OpenCV會不夠用,先開個4GB swap

$ sudo fallocate -l 4.0G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile

$ sudo vi /etc/fstab

/swapfile none swap defaults 0 0
5.安裝必要開發套件

$ sudo apt-get update
$ sudo apt-get install cmake git
$ sudo apt-get install python-pip
$ sudo apt-get install python3-pip python3-pil

$ sudo apt-get install gstreamer1.0-tools gstreamer1.0-alsa \ 
gstreamer1.0-plugins-base gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly \
gstreamer1.0-libav

$ sudo apt-get install libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-good1.0-dev \
libgstreamer-plugins-bad1.0-dev \
libgstrtspserver-1.0-dev

$ sudo apt-get install libgtk2.0-dev

$ sudo apt-get install cmake

6.分別下載 opencv 及 opencv_contrib
https://github.com/opencv/opencv/archive/4.4.0.zip
https://github.com/opencv/opencv_contrib/archive/4.4.0.zip
解壓縮後放在同一個目錄下

cd opencv-4.4.0
mkdir build
cd build

cmake -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.4.0/modules -DWITH_CUDA=ON -DCUDA_FAST_MATH=1 -DBUILD_EXAMPLES=OFF -DWITH_GSTREAMER=ON -DWITH_V4L=ON -DWITH_LIBV4L=OFF -D BUILD_opencv_python2=ON -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_opencv_python3=ON -DPYTHON3_INCLUDE_DIR2=/usr/include/python3.6m -DPYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_EXAMPLES=OFF -D CMAKE_INSTALL_PREFIX=/usr/local -DENABLE_CXX11=ON -D CMAKE_C_COMPILER=/usr/bin/gcc-7 ..

$ make -j $(($(nproc) + 1))
等待約三個小時...

$ sudo make install

7.安裝Jetson Stats, 這樣就能即時monitor CPU / GPU / Memory 狀態

$ sudo -H pip3 install jetson-stats

$jtop