LEGO EV3 中执行 VSCode Python 代码过程分析
鏡像為 ev3dev。
通過(guò) SSH 連接 LEGO EV3 設(shè)備,默認(rèn)密碼為 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登錄之后,查看設(shè)備系統(tǒng)中運(yùn)行的所有進(jìn)程,剔除所有的內(nèi)核進(jìn)程,用戶進(jìn)程主要包括如下這些:
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 個(gè)用戶進(jìn)程。
以 ev3dev 官方提供的在 VSCode 中為 EV3 開(kāi)發(fā)的 Python 示例工程 vscode-hello-python 為例,在 VSCode 中以打開(kāi)目錄的方式打開(kāi)下載的這個(gè)工程的源碼目錄,按下 F5 鍵在 EV3 中運(yùn)行代碼,此時(shí)查看 EV3 中進(jìn)程的運(yùn)行情況,所有運(yùn)行的進(jìn)程如下:
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 個(gè)用戶進(jìn)程。此時(shí)多了如下 5 個(gè)進(jìn)程:
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過(guò)程似乎是,在 VSCode 需要將代碼灌進(jìn) EV3 設(shè)備中執(zhí)行時(shí),EV3 設(shè)備的系統(tǒng)中啟動(dòng)了 sftp 服務(wù)器 sftp-server 用于傳送代碼,代碼傳送完成后執(zhí)行 brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello.py 命令運(yùn)行我們的代碼。這個(gè) brickrun 進(jìn)程的父進(jìn)程是進(jìn)程 ID 為 868 的 sshd: robot@notty 進(jìn)程。
brickrun 并沒(méi)有直接解釋執(zhí)行我們的 Python 代碼,而是借助于進(jìn)程號(hào)為 378 的 conrun-server 和 EV3 設(shè)備系統(tǒng)中的 Python3 解釋器 python3 解釋執(zhí)行我們的代碼。
vscode-hello-python 的代碼所完成的功能主要是,在設(shè)備的屏幕上顯示 “Hello World!”,并在終端上顯示 “Hello VS Code!”。通過(guò) VSCode 的插件將代碼灌進(jìn)設(shè)備并執(zhí)行,無(wú)疑可以正確的完成這些功能。
如果我們用 SSH 登錄進(jìn)設(shè)備的系統(tǒng)中之后,直接在終端中執(zhí)行 Python 代碼,就像下面這樣:
robot@ev3dev:~$ python3 /home/robot/vscode-hello-python/hello.py Hello World! Hello VS Code!此時(shí),無(wú)法看到字符串被顯示在 LEGO EV3 設(shè)備的屏幕上,兩條字符串都被顯示在了終端中。
如果我們直接在終端中執(zhí)行 brickrun 命令,如下面這樣:
robot@ev3dev:~$ brickrun --directory=/home/robot/vscode-hello-python /home/robot/vscode-hello-python/hello.py Hello VS Code!此時(shí)代碼依然可以如最初預(yù)期的那樣運(yùn)行。如果查看 LEGO EV3 設(shè)備中進(jìn)程運(yùn)行情況的話,可以看到多了如下這兩個(gè)進(jìn)程:
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 進(jìn)程由進(jìn)程號(hào)為 722 號(hào)的我們正在運(yùn)行的 bash 進(jìn)程創(chuàng)建,Python3 解釋器進(jìn)程依然由進(jìn)程號(hào)為 378 的 conrun-server 進(jìn)程創(chuàng)建。
在 搭建 LEGO EV3 的 PyCharm Python 開(kāi)發(fā)環(huán)境 一文中,可以看到,用 Python 代碼控制馬達(dá)轉(zhuǎn)動(dòng)的代碼,可以直接在終端中用 Python 解釋器如我們預(yù)期的那樣執(zhí)行。
這就說(shuō)明,直接在終端中執(zhí)行 Python 解釋器的 Python 運(yùn)行環(huán)境,和 brickrun/conrun-server 創(chuàng)建的 Python 運(yùn)行環(huán)境是不同的,主要的不同點(diǎn)應(yīng)該在 Python 標(biāo)準(zhǔn)庫(kù)中輸入輸出函數(shù)的行為上。
總體上來(lái)看,LEGO EV3 中執(zhí)行 VSCode Python 代碼的過(guò)程,似乎主要是一個(gè)遠(yuǎn)程代碼執(zhí)行的過(guò)程。
LEGO EV3 設(shè)備中的 ev3dev 鏡像的官方網(wǎng)站為 ev3dev,ev3dev 鏡像中運(yùn)行的定制的用戶進(jìn)程的代碼基本上都是開(kāi)源的。前面提到的幾個(gè)命令的源碼地址如下:
- brickrun - brickrun
- conrun-server - console-runner
ev3dev 的 VSCode 插件同樣是開(kāi)源的,其地址為 vscode-ev3dev-browser。
總結(jié)
以上是生活随笔為你收集整理的LEGO EV3 中执行 VSCode Python 代码过程分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LEGO EV3 通信开发者套件
- 下一篇: python网络编程实例简析