圓創力科技

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

查看: 8382|回復: 0

童芯派搭配第三方Python編輯器使用教學[轉載繁譯]

[複製鏈接]
magiccar 發表於 2021-7-2 15:47 | 顯示全部樓層 |閱讀模式
本教學譯自Makeblock官方教學文件,原文點[]
本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。
快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。
pip install pyserial
pip install cyberpi

% D/ {8 t& A3 }. \7 q$ j9 ^1.安裝Python環境
  • 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。
    + _5 i2 _: f/ |# X7 V+ E" I  [8 D* \

Python Windows環境不同版本下載頁面:Windows Python下載

Python Mac OS 環境不同版本下載頁面: Mac OS Python下載

4 s7 @1 o" V% _* |
  • 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
    & g. ?: r3 Y/ L3 l) T
2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)
提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。
  • 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
  • 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
    % Z$ F  L7 A& |1 [; j& F& [
  • 點擊桌面上Visual Studio Code的圖示運行程式。
  • 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。- s7 \* t( n4 s2 \

) n# O' S" z2 Q. |  G; ^
  • 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)& z3 \8 f7 A* p
2 r" ?( s# b* ]' d1 R5 O% f
  • 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
    & D1 T: E# o9 M* q% x

* Y; E5 n, I- c2 Q+ ?  `
  • 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
  • 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。
    9 V& A" q% T8 H. m0 n' T

+ E: h8 f: v3 p/ b) |3.童芯派第三方編輯器程式設計準備
0 M/ L6 @' R0 n0 l! b8 ~9 z4 {: t  q+ I
  • windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。
    , K/ t! A4 X. E. z9 e# t

( {' u( D, j/ f( \- C
  • 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。
    7 E8 h9 v1 O: x% ?* W

在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。


2 V3 G5 z' T' v3 J1 Y  ^. C
  • 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。" b, }9 F- W/ q

在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。


6 r1 ?. L- x7 O5 I' W; i
查閱cyberpi API文檔見:童芯派API文檔

# ~( |% S$ E1 }" m9 u
  • 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。
    , h, J3 T- x  D7 S+ ?7 O0 K, d' W

  J. S: O( s2 W" J+ W% L* X) H/ Y
4.注意事項
在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。

) w1 o; ~- i: _7 w2 V5.一些程式設計案例
! t' e9 c' a/ v童芯派滑鼠
需要安裝pynput模組,參照上面的步驟pip install pynput
! w- [+ I" E/ p" p
功能介紹:+ A; e9 P7 K9 j/ }: H- N( [; Y& s1 _
利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。1 u$ Z3 n7 x$ q! w) W4 c
通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。, w4 f; V* F; @5 ?9 R
結合藍牙模組可以實現無線滑鼠的功能。
2 X+ @8 o, t" o  J& @  g9 X3 R% m6 j6 U  Y  J; R/ O  a6 I: S
from pynput.mouse import Button,Controller
; a4 p. I' w" U# X: M/ W/ himport cyberpi
: L7 [7 z3 v' _4 T6 u' gimport time  G( F+ }. k. J$ k0 |. d

# F2 G$ V: |: B% X& S& E' M. ]mouse = Controller()9 m4 j7 d7 i' F/ S4 ~( ?
# U1 H5 k7 C6 |" k- A
while True:
# K  y  G( X* @- y9 B9 W3 C   if cyberpi.is_tiltback():
/ J" A. x- g- b0 K  T2 e       mouse.move(-3, 0)/ d2 r* }, S# H) X8 o: h& Q# w
       print(mouse.position)
0 W- x  F. G% {1 N   if cyberpi.is_tiltforward():
3 F9 x. s6 f. P' E       mouse.move(3, 0)
& C, y: V# i9 E1 [% U/ K0 f   if cyberpi.is_tiltleft():, v+ q3 m6 a% d0 k; W6 F
       mouse.move(0, -3)
: G4 n, |: Z' {, a0 f- t  {" S   if cyberpi.is_tiltright():3 o" g* D) q( T8 B
       mouse.move(0, 3)9 n+ D; [0 Z/ \( ?3 S- c
   if cyberpi.controller.is_press("b"):
0 l, g% O9 e. w* X) R       mouse.press(Button.left)
0 a* V8 S8 y+ I& g. n6 i       mouse.release(Button.left)
1 ]1 K4 x5 `( E7 [9 R; A! o( Q* V' ^       mouse.press(Button.left)/ w* O" _8 R+ {* ~
       mouse.release(Button.left)% G; ^' ~# `: X- W# k5 A1 d- K
   if cyberpi.controller.is_press("a"):8 }, \* a, n  o& W5 Q
       mouse.press(Button.right)
& \' l2 ?$ R% C, f- V4 Q& L       mouse.release(Button.right)
: `- {+ u/ Z% N( R3 D    time.sleep(0.01)8 i9 D: x; t! X/ B/ c( l
2 o5 u, S) V* T% U- f* _& X

3 {5 O6 `) L) |" E
語音打字器

1 k; n( v( p0 H; s/ z0 W& x功能介紹:1 x( g. J3 H( Z  q) T% C
利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。
, d. J/ N5 r4 O. o將語音辨識的結果通過pynput在文檔中打出來。; q7 W8 k9 r# k
該程式目前只能支援英文。, B5 |0 y* U7 E3 L- U
3 `1 Q6 s+ w$ Y% Q. s# i
import cyberpi% r7 o  A. q, ~+ C. E/ i7 m
from pynput.keyboard import Key,Controller
3 Q  _/ b" q$ Q4 {( eimport time' n% X& j  O( x9 O3 P) h  J

, S8 g$ Z$ z, C% ?$ F- Nkeyboard = Controller()
  s+ T) w3 d% R5 d' Vcyberpi.console.clear()
6 C# [) R7 S/ J2 b' h, U5 x8 _$ a6 \% Ecyberpi.led.on(0, 0, 0)+ f( f; k/ k/ G" q( c
cyberpi.set_recognition_url()
6 w0 _1 d7 J+ W7 j( scyberpi.cloud.setkey("請輸入雲服務授權碼")    # 通過慧程式設計帳號可以獲得
  X& T; }+ c! n+ hcyberpi.wifi.connect("WIFI名稱", "WIFI密碼")
6 h+ A! y9 I. {5 F2 f' E/ f' pwhile not cyberpi.wifi.is_connect():
# J" o. t# _) U0 [( i   pass. n- C* _! \8 M' D! |- g
cyberpi.led.on(0,0,255)" T2 p( F$ b7 q- P0 v+ c; F
cyberpi.console.println("WIFI已連接")" L! z, A7 k! Y
cyberpi.console.println("--------------")  \' h+ s! p% I
cyberpi.console.println("按下A鍵開始語音辨識")' H% A" a0 |) D0 H9 h. t
while True:4 \8 n+ N9 H/ V! @" W3 H
   if cyberpi.controller.is_press('a'):% ]2 A1 Y9 A/ d* @4 r. L6 I8 l( G8 I5 Z
       keyboard.press(Key.space)
& v3 f6 E  e5 ?% L, s       cyberpi.console.clear()
6 p8 n0 g) B' }       cyberpi.led.on(100, 0, 0)
, i9 ~$ {9 z3 z. E  m( X# \       cyberpi.console.println("開始語音辨識")
. b+ C: s' j* n2 t       cyberpi.audio.play("switch")/ u' y- B4 h8 B. K' x
       cyberpi.console.println("--------------")
5 X  R) p6 m: B       cyberpi.cloud.listen("english", 2)
& v  n/ Z+ t! R- [       cyberpi.led.on(0, 0, 0)1 N6 {! y2 E7 G4 ~, V8 g$ |
       say = cyberpi.cloud.listen_result()0 X" o" O) S5 I- E
       cyberpi.console.println(say)
2 i5 V9 C. S5 W, g7 I9 n       cyberpi.console.println("--------------")
( i& V1 L' b6 q3 K7 i) _       for i in say:0 ~, ?4 \( y8 h$ t
            if i == '':* s' g% B, q% h7 y3 z
                 keyboard.press(' ')( v9 g3 A: m# ?1 e. Y7 ]! W
            else:! ]6 R) o' s6 }7 l: f; E: s% e9 T
                keyboard.press(str(i))
% w/ Z) D1 J% J% S7 Q- J                time.sleep(0.03)9 C+ R: r; o: V- w4 b
5 f& O& J0 `/ U
matplotlib音量柱狀圖
7 ?: y. I6 U* B' g
import cyberpi; u, f# [+ \0 B, Q) G% n) O
import time
) t1 S. a  x7 _) N7 e* A* Afrom matplotlib import pyplot as plt
4 S# g8 T' I# E ; I+ R0 T7 N$ z. N, \8 q% x3 D
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
( L, }; ]! l1 \8 gplt.ion()3 z0 Y5 l7 T: h) T1 W! M

5 p; p) @# e% Z: x3 I$ s" H% [; nwhile True:& _4 \4 o$ A3 j6 `, a& k% F
   loud = cyberpi.get_loudness()
0 K* m; k* i% x. y! t( }" x   plt.clf()
0 ?/ O/ ^% }1 [% c! U+ U( e2 T! n( p0 P   plt.xlabel("數據名稱")
! ~, e, R& B0 u, B( \   plt.ylabel("音量讀值")
8 m4 E6 e5 q# e9 D   y_locator = plt.MultipleLocator(5)
' s0 ^# ?3 ]$ m; J# y   ax=plt.gca(). Z% N/ _6 L+ x7 K
   ax.yaxis.set_major_locator(y_locator)4 M7 r- }$ N+ Y" q$ ]8 c
   plt.ylim(0,100)
( n  B0 A6 F: N& S   plt.bar('音量', loud, align='center',label=f'溫度{loud}')
1 {" V/ V: Q/ o; D; \/ c2 `! K0 j   plt.title('童芯派聲音感測器讀值')( }, b7 u) U( M% I# v. O; g: t
   plt.legend()
, X$ g+ N6 m: m   plt.show()
. b6 T$ ~; L. k5 O: m    plt.pause(0.01)
! |8 h- {# |! t* g0 S; K' f# ~" e- M) k3 g5 H1 Z9 }
9 Z; U: j/ N( j
matplotlib 多圖表顯示
: _; ^9 y! M  |

/ F7 w5 b9 v$ }from matplotlib import pyplot as plt: K8 ?: B1 B, w% J: y) T# D
import time- K& u0 t/ @. ?% ?6 p
import cyberpi
' P! r/ ^* A% K. Y: W( R   X+ Q1 m# E" ]/ C  n5 \
loud_list = []/ Q. Q9 b5 q! s. k( u5 m2 D8 E
bri_list = []
" ?! [5 ]( E6 _; e! ?; H- hplt.rcParams['font.sans-serif'] =['Microsoft YaHei']   # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體1 R7 \9 U0 |; y* H" @
plt.ion()
3 g' _2 T& f/ g5 n. [! Wplt.figure(figsize=(10,10))/ }) y# v, H* z3 g
plt.figure(1)
9 c5 \8 T- ~( d+ o/ {- Bx_locator = plt.MultipleLocator(5)' }2 o; ~! Q% T: o* _" ~4 S
y_locator = plt.MultipleLocator(5)$ D/ l/ }2 v- I3 q
while True:! w2 ?! t" s. Z
   loud = cyberpi.get_loudness()  C5 S* M4 _. e% f, Z. N/ Y
   bri = cyberpi.get_bri()8 B$ q% M5 H" F( X% O0 H1 D1 }
   loud_list.append(loud)' H% S, X8 E1 ]2 d% L  k
   bri_list.append(bri)
  s  c1 e0 }) a3 g/ v' ^. N! u5 i& G- o   battery = cyberpi.get_battery()* h2 N" |) H6 U. y
   size = [battery, 100-battery]9 {& X; X+ O8 M/ {  n7 N: b- ?- Y
   status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']: S9 [. d& G: K8 K
   ax1 = plt.subplot(221)# H4 h  v  L  q9 h& u  D/ d
   plt.title('光線折線圖')
9 Z! Z$ A6 d5 }0 X   ax1.plot(bri_list)5 u6 `. O! \, f( P# H; e" h
   ax2 = plt.subplot(222)0 [2 o" H" N! T! q+ ?% W
   plt.title('聲音柱狀圖')
* s$ n. p. h/ K3 u/ k) a   ax2.xaxis.set_major_locator(x_locator)
4 [& `1 x3 _! x* P4 B   ax2.yaxis.set_major_locator(y_locator)8 ^) V% K3 k7 \6 J
   plt.ylim(0,100)$ N2 A: Z0 @- {8 U& {
   ax2.bar('sound', loud)
- w  K5 E& k( ]+ ?+ j   ax3 = plt.subplot(223)
+ t; E, l0 X6 `+ j   ax3.xaxis.set_major_locator(x_locator)
, J  j+ A5 g0 ]   ax3.yaxis.set_major_locator(y_locator)
- t& ^" I) a7 U" j5 S* _9 e2 N( o   plt.xlim(0,100)
3 v8 w. l' f, ~   plt.ylim(0,100)% u1 l6 }0 v4 _, e
   plt.title('聲音、音量散點圖')! V% G( T. O' ?2 O" u" d' k1 O0 y
   ax3.scatter(loud_list,bri_list)
/ ~. P# {8 N/ o4 p* R   ax4 = plt.subplot(224)
- U/ A: L0 |% Z   ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})1 m$ \' v4 C  Z; T+ G3 K
   plt.title('童芯派電量')
, u+ D. `* i1 ^4 g7 ?   plt.pause(0.2); k3 Z; M3 d1 R8 g2 d5 |" V
   plt.clf()
& p" i+ F/ K: N/ N- m  V9 o. h   if cyberpi.controller.is_press('a'):4 I7 A. q6 r5 @" O& E4 a
       break- y; D. L$ X) Q' Z
   if len(bri_list) > 500:3 C' l& \# V" s8 X% n; Q$ e( [: i) e
       bri_list = []
/ R- y1 W; y; Z# e# c        loud_list = []
2 r4 @( e: Q- z9 ?9 v2 e% c+ R- ?$ x3 w0 c, E+ P3 ~: ]3 O

本帖子中包含更多資源

您需要 登錄 才可以下載或查看,沒有賬號?立即註冊

x
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

QQ|Archiver|手機版|小黑屋|圓創力科技有限公司 IOP Robotic Technology Co.,Ltd Tel: 07-3924582 Fax: 07-3924001

GMT+8, 2025-11-22 16:20 , Processed in 0.026007 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表