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
登入之後,檢視裝置系統中執行的所有程序,剔除所有的核心程序,使用者程序主要包括如下這些:
robot@ev3dev:~$ ps -alx FUIDPIDPPID PRINIVSZRSS WCHANSTAT TTYTIME COMMAND 401020 0279605632-Ss?0:10 /sbin/init 40 148120 0396207484-Ss?0:05 /lib/systemd/systemd-journald 40 193120 0136602692-Ss?0:03 /lib/systemd/systemd-udevd 4107 358120 0 63602964-Ss?0:01 avahi-daemon: running [ev3dev.local] 4106 359120 0 64763268-Ss?0:02 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation 1107 360 35820 0 63601444-S?0:00 avahi-daemon: chroot helper 40 362120 0 73404224-Ss?0:00 /lib/systemd/systemd-logind 40 365120 0171844600-Ssl?0:04 /usr/sbin/brickd 4 1000 378120 0337564200poll_s Ssl+ tty50:00 /usr/bin/conrun-server 10 393120 0 1768 544-Ss?0:00 /usr/sbin/ldattach29/dev/tty_ev3-ports:in1 40 427120 0109884588-Ss?0:03 /usr/sbin/connmand -n 40 429120 0 73443300-Ss?0:00 /usr/lib/bluetooth/bluetoothd 4103 510120 0 81924444-Ss?0:00 /lib/systemd/systemd-resolved 40 512120 0 99003612-Ss?0:00 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant 40 514120 05135611772 -Ssl+ tty10:34 /usr/sbin/brickman 40 518120 0102685188-Ss?0:00 /usr/sbin/sshd -D 5105 535120 0 89723856-Ssl?0:01 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 105:108 40 702 51820 0115245616-Ss?0:00 sshd: robot [priv] 4 1000 709120 0 96445564SyS_ep Ss?0:00 /lib/systemd/systemd --user 5 1000 712 70920 0295642540-S?0:00 (sd-pam) 5 1000 719 70220 0115243680-R?0:00 sshd: robot@pts/0 0 1000 722 71920 0 52643008waitSspts/00:00 -bash 0 1000 842 72220 0 72002296-R+pts/00:00 ps -alx
總共有 23 個使用者程序。
以 ev3dev 官方提供的在 VSCode 中為 EV3 開發 Python 程式碼的示例工程vscode-hello-python 為例,在 VSCode 中以開啟目錄的方式開啟下載的這個工程的原始碼目錄,按下 F5 鍵在 EV3 中執行程式碼,此時檢視 EV3 中程序的執行情況,所有執行的程序如下:
robot@ev3dev:~$ ps -alx FUIDPIDPPID PRINIVSZRSS WCHANSTAT TTYTIME COMMAND 401020 0279605192-Ss?0:10 /sbin/init 40 148120 0396206604-Ss?0:05 /lib/systemd/systemd-journald 40 193120 0136602676-Ss?0:03 /lib/systemd/systemd-udevd 4107 358120 0 63602772-Ss?0:01 avahi-daemon: running [ev3dev.local] 4106 359120 0 64763028-Ss?0:02 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation 1107 360 35820 0 63601332-S?0:00 avahi-daemon: chroot helper 40 362120 0 73403672-Ss?0:00 /lib/systemd/systemd-logind 40 365120 0172124444-Ssl?0:05 /usr/sbin/brickd 4 1000 378120 0337563896poll_s Ssltty50:00 /usr/bin/conrun-server 10 393120 0 1768 492-Ss?0:00 /usr/sbin/ldattach29/dev/tty_ev3-ports:in1 40 427120 0109883592-Ss?0:03 /usr/sbin/connmand -n 40 429120 0 73443084-Ss?0:00 /usr/lib/bluetooth/bluetoothd 4103 510120 0 81923752-Ss?0:00 /lib/systemd/systemd-resolved 40 512120 0 99002712-Ss?0:00 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant 40 514120 0513568764-Ssl+ tty10:38 /usr/sbin/brickman 40 518120 0102685016-Ss?0:00 /usr/sbin/sshd -D 5105 535120 0 89723256-Ssl?0:01 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 105:108 40 702 51820 0115245616-Ss?0:00 sshd: robot [priv] 4 1000 709120 0 96445144SyS_ep Ss?0:00 /lib/systemd/systemd --user 5 1000 712 70920 0295642412-S?0:00 (sd-pam) 5 1000 719 70220 0115243692-S?0:04 sshd: robot@pts/0 0 1000 722 71920 0 52643008waitSspts/00:00 -bash 40 857 51820 0115245568-Ss?0:01 sshd: robot [priv] 5 1000 868 85720 0115244800-S?0:00 sshd: robot@notty 0 1000 870 86820 0 22961448-Ss?0:00 /usr/lib/openssh/sftp-server 0 1000 872 86820 0341484724poll_s Ssl?0:00 brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello. 0 1000 884 37820 0 91006572poll_s S+tty50:02 python3 /home/robot/vscode-hello-python/hello.py 0 1000 907 72220 0 72002300-R+pts/00:00 ps -alx
這次總共有 28 個使用者程序。此時多瞭如下 5 個程序:
40857518200115245568 -Ss?0:01 sshd: robot [priv] 5 1000868857200115244800 -S?0:00 sshd: robot@notty 0 100087086820022961448 -Ss?0:00 /usr/lib/openssh/sftp-server 0 1000872868200341484724 poll_s Ssl?0:00 brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello. 0 100088437820091006572 poll_s S+tty50: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: robot@notty
程序。
brickrun
並沒有直接解釋執行我們的 Python 程式碼,而是藉助於程序號為 378 的conrun-server
和 EV3 裝置系統中的 Python3 直譯器python3
解釋執行我們的程式碼。
vscode-hello-python 的程式碼所完成的功能主要是,在裝置的螢幕上顯示 “Hello World!”,並在終端上顯示 “Hello VS Code!”。通過 VSCode 的外掛將程式碼灌進裝置並執行,無疑可以正確的完成這些功能。
如果我們用 SSH 登入進裝置的系統中之後,直接在終端中執行 Python 程式碼,就像下面這樣:
robot@ev3dev:~$ python3 /home/robot/vscode-hello-python/hello.py Hello World! Hello VS Code!
此時,無法看到字串被顯示在 LEGO EV3 裝置的螢幕上,兩條字串都被顯示在了終端中。
如果我們直接在終端中執行brickrun
命令,如下面這樣:
robot@ev3dev:~$brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello.py Hello VS Code!
此時程式碼依然可以如最初預期的那樣執行。如果檢視 LEGO EV3 裝置中程序執行情況的話,可以看到多瞭如下這兩個程序:
0 10001046722200341484616 poll_s Sl+pts/00:00 brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello.py 0 10001058378200106966736 poll_s S+tty50: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 映象中執行的定製的使用者程序的程式碼基本上都是開源的。前面提到的幾個命令的原始碼地址如下:
-
brickrun
- brickrun -
conrun-server
- console-runner
ev3dev 的 VSCode 外掛同樣是開源的,其地址為 vscode-ev3dev-browser 。