LEGO EV3 中执行 VSCode Python 代码过程分析
鏡像為 ev3dev。
通過 SSH 連接 LEGO EV3 設備,默認密碼為 maker:
$ ssh robot@ev3dev.local 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 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: robot@pts/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 中進程的運行情況,所有運行的進程如下:
robot@ev3dev:~$ 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: robot@pts/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: robot@notty 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: robot@notty 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: 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 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.pybrickrun 進程由進程號為 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。
總結
以上是生活随笔為你收集整理的LEGO EV3 中执行 VSCode Python 代码过程分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LEGO EV3 通信开发者套件
- 下一篇: EV3 直接命令 - 第一课 无为的艺术