LEGO EV3 中執行 VSCode Python 程式碼過程分析
映象為 ev3dev。
通過 SSH 連線 LEGO EV3 裝置,預設密碼為 maker
:
$ ssh [email protected] Password: Linux ev3dev 4.14.61-ev3dev-2.2.2-ev3 #1 PREEMPT Mon Aug 6 14:22:31 CDT 2018 armv5tejl _____ _ _____ _|___ / __| | _____ __ / _ \ \ / / |_ \ / _` |/ _ \ \ / / | __/\ V / ___) | (_| | __/\ V / \___| \_/ |____/ \__,_|\___| \_/ Debian stretch on LEGO MINDSTORMS EV3! Last login: Wed Oct 24 07:42:33 2018 from 10.42.0.1
登入之後,檢視裝置系統中執行的所有程序,剔除所有的核心程序,使用者程序主要包括如下這些:
[email protected]:~$ ps -alx
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 0 1 0 20 0 27960 5632 - Ss ? 0:10 /sbin/init
4 0 148 1 20 0 39620 7484 - Ss ? 0:05 /lib/systemd/systemd-journald
4 0 193 1 20 0 13660 2692 - Ss ? 0:03 /lib/systemd/systemd-udevd
4 107 358 1 20 0 6360 2964 - Ss ? 0:01 avahi-daemon: running [ev3dev.local]
4 106 359 1 20 0 6476 3268 - Ss ? 0:02 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
1 107 360 358 20 0 6360 1444 - S ? 0:00 avahi-daemon: chroot helper
4 0 362 1 20 0 7340 4224 - Ss ? 0:00 /lib/systemd/systemd-logind
4 0 365 1 20 0 17184 4600 - Ssl ? 0:04 /usr/sbin/brickd
4 1000 378 1 20 0 33756 4200 poll_s Ssl+ tty5 0:00 /usr/bin/conrun-server
1 0 393 1 20 0 1768 544 - Ss ? 0:00 /usr/sbin/ldattach 29 /dev/tty_ev3-ports:in1
4 0 427 1 20 0 10988 4588 - Ss ? 0:03 /usr/sbin/connmand -n
4 0 429 1 20 0 7344 3300 - Ss ? 0:00 /usr/lib/bluetooth/bluetoothd
4 103 510 1 20 0 8192 4444 - Ss ? 0:00 /lib/systemd/systemd-resolved
4 0 512 1 20 0 9900 3612 - Ss ? 0:00 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
4 0 514 1 20 0 51356 11772 - Ssl+ tty1 0:34 /usr/sbin/brickman
4 0 518 1 20 0 10268 5188 - Ss ? 0:00 /usr/sbin/sshd -D
5 105 535 1 20 0 8972 3856 - Ssl ? 0:01 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 105:108
4 0 702 518 20 0 11524 5616 - Ss ? 0:00 sshd: robot [priv]
4 1000 709 1 20 0 9644 5564 SyS_ep Ss ? 0:00 /lib/systemd/systemd --user
5 1000 712 709 20 0 29564 2540 - S ? 0:00 (sd-pam)
5 1000 719 702 20 0 11524 3680 - R ? 0:00 sshd: [email protected]/0
0 1000 722 719 20 0 5264 3008 wait Ss pts/0 0:00 -bash
0 1000 842 722 20 0 7200 2296 - R+ pts/0 0:00 ps -alx
總共有 23 個使用者程序。
以 ev3dev 官方提供的在 VSCode 中為 EV3 開發的 Python 示例工程 vscode-hello-python 為例,在 VSCode 中以開啟目錄的方式開啟下載的這個工程的原始碼目錄,按下 F5 鍵在 EV3 中執行程式碼,此時檢視 EV3 中程序的執行情況,所有執行的程序如下:
[email protected]:~$ ps -alx
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 0 1 0 20 0 27960 5192 - Ss ? 0:10 /sbin/init
4 0 148 1 20 0 39620 6604 - Ss ? 0:05 /lib/systemd/systemd-journald
4 0 193 1 20 0 13660 2676 - Ss ? 0:03 /lib/systemd/systemd-udevd
4 107 358 1 20 0 6360 2772 - Ss ? 0:01 avahi-daemon: running [ev3dev.local]
4 106 359 1 20 0 6476 3028 - Ss ? 0:02 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
1 107 360 358 20 0 6360 1332 - S ? 0:00 avahi-daemon: chroot helper
4 0 362 1 20 0 7340 3672 - Ss ? 0:00 /lib/systemd/systemd-logind
4 0 365 1 20 0 17212 4444 - Ssl ? 0:05 /usr/sbin/brickd
4 1000 378 1 20 0 33756 3896 poll_s Ssl tty5 0:00 /usr/bin/conrun-server
1 0 393 1 20 0 1768 492 - Ss ? 0:00 /usr/sbin/ldattach 29 /dev/tty_ev3-ports:in1
4 0 427 1 20 0 10988 3592 - Ss ? 0:03 /usr/sbin/connmand -n
4 0 429 1 20 0 7344 3084 - Ss ? 0:00 /usr/lib/bluetooth/bluetoothd
4 103 510 1 20 0 8192 3752 - Ss ? 0:00 /lib/systemd/systemd-resolved
4 0 512 1 20 0 9900 2712 - Ss ? 0:00 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
4 0 514 1 20 0 51356 8764 - Ssl+ tty1 0:38 /usr/sbin/brickman
4 0 518 1 20 0 10268 5016 - Ss ? 0:00 /usr/sbin/sshd -D
5 105 535 1 20 0 8972 3256 - Ssl ? 0:01 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 105:108
4 0 702 518 20 0 11524 5616 - Ss ? 0:00 sshd: robot [priv]
4 1000 709 1 20 0 9644 5144 SyS_ep Ss ? 0:00 /lib/systemd/systemd --user
5 1000 712 709 20 0 29564 2412 - S ? 0:00 (sd-pam)
5 1000 719 702 20 0 11524 3692 - S ? 0:04 sshd: [email protected]/0
0 1000 722 719 20 0 5264 3008 wait Ss pts/0 0:00 -bash
4 0 857 518 20 0 11524 5568 - Ss ? 0:01 sshd: robot [priv]
5 1000 868 857 20 0 11524 4800 - S ? 0:00 sshd: [email protected]
0 1000 870 868 20 0 2296 1448 - Ss ? 0:00 /usr/lib/openssh/sftp-server
0 1000 872 868 20 0 34148 4724 poll_s Ssl ? 0:00 brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello.
0 1000 884 378 20 0 9100 6572 poll_s S+ tty5 0:02 python3 /home/robot/vscode-hello-python/hello.py
0 1000 907 722 20 0 7200 2300 - R+ pts/0 0:00 ps -alx
這次總共有 28 個使用者程序。此時多瞭如下 5 個程序:
4 0 857 518 20 0 11524 5568 - Ss ? 0:01 sshd: robot [priv]
5 1000 868 857 20 0 11524 4800 - S ? 0:00 sshd: [email protected]
0 1000 870 868 20 0 2296 1448 - Ss ? 0:00 /usr/lib/openssh/sftp-server
0 1000 872 868 20 0 34148 4724 poll_s Ssl ? 0:00 brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello.
0 1000 884 378 20 0 9100 6572 poll_s S+ tty5 0:02 python3 /home/robot/vscode-hello-python/hello.py
過程似乎是,在 VSCode 需要將程式碼灌進 EV3 裝置中執行時,EV3 裝置的系統中啟動了 sftp 伺服器 sftp-server
用於傳送程式碼,程式碼傳送完成後執行 brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello.py
命令執行我們的程式碼。這個 brickrun
程序的父程序是程序 ID 為 868 的 sshd: [email protected]
程序。
brickrun
並沒有直接解釋執行我們的 Python 程式碼,而是藉助於程序號為 378 的 conrun-server
和 EV3 裝置系統中的 Python3 直譯器 python3
解釋執行我們的程式碼。
vscode-hello-python 的程式碼所完成的功能主要是,在裝置的螢幕上顯示 “Hello World!”,並在終端上顯示 “Hello VS Code!”。通過 VSCode 的外掛將程式碼灌進裝置並執行,無疑可以正確的完成這些功能。
如果我們用 SSH 登入進裝置的系統中之後,直接在終端中執行 Python 程式碼,就像下面這樣:
[email protected]:~$ python3 /home/robot/vscode-hello-python/hello.py
Hello World!
Hello VS Code!
此時,無法看到字串被顯示在 LEGO EV3 裝置的螢幕上,兩條字串都被顯示在了終端中。
如果我們直接在終端中執行 brickrun
命令,如下面這樣:
[email protected]:~$ brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello.py
Hello VS Code!
此時程式碼依然可以如最初預期的那樣執行。如果檢視 LEGO EV3 裝置中程序執行情況的話,可以看到多瞭如下這兩個程序:
0 1000 1046 722 20 0 34148 4616 poll_s Sl+ pts/0 0:00 brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello.py
0 1000 1058 378 20 0 10696 6736 poll_s S+ tty5 0:02 python3 /home/robot/vscode-hello-python/hello.py
brickrun
程序由程序號為 722 號的我們正在執行的 bash
程序建立,Python3 直譯器程序依然由程序號為 378 的 conrun-server
程序建立。
在 搭建 LEGO EV3 的 PyCharm Python 開發環境 一文中,可以看到,用 Python 程式碼控制馬達轉動的程式碼,可以直接在終端中用 Python 直譯器如我們預期的那樣執行。
這就說明,直接在終端中執行 Python 直譯器的 Python 執行環境,和 brickrun
/conrun-server
建立的 Python 執行環境是不同的,主要的不同點應該在 Python 標準庫中輸入輸出函式的行為上。
總體上來看,LEGO EV3 中執行 VSCode Python 程式碼的過程,似乎主要是一個遠端程式碼執行的過程。
LEGO EV3 裝置中的 ev3dev 映象的官方網站為 ev3dev,ev3dev 映象中執行的定製的使用者程序的程式碼基本上都是開源的。前面提到的幾個命令的原始碼地址如下: