圓創力科技

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

查看: 8535|回復: 0

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

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

6 r% X, O, G# F1.安裝Python環境
  • 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。
    7 O0 Z: n& I  P$ ]0 ~1 S

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

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


$ }7 Q0 F. X2 s" O, r' b* E" C
  • 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
    , T% T( P+ |% c3 n* d- s5 _
2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)
提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。
  • 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
  • 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)" @( s# h9 m& T
  • 點擊桌面上Visual Studio Code的圖示運行程式。
  • 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。. i6 U; v  e* `3 r" x  V/ b( s% e) r

0 G0 t" \* S& ~5 ^
  • 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)
    $ t8 h) g  O. _3 Y" i; `$ e
% ^! ~6 H( N; r0 X/ F, s* k0 \
  • 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。5 h; u7 t5 H: j! c/ ^- b
2 Z& F- m/ ~" d. H1 q; A
  • 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
  • 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。
    : R) h, e3 ^& B* j! M) Y! D$ i

) P" W' F" P1 {6 K3 T6 |& z3.童芯派第三方編輯器程式設計準備
) [. z2 J* k" L$ s4 ?
  • windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。. e& Y2 S1 l/ s, |
) ?( ]" W9 b. B! t  ]
  • 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。
      z# H+ U, j- B2 ~- T

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

- z( \' S4 T$ i9 x( _' Q8 f
  • 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。
    + @+ G7 T) o- i0 w% |

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


! f& E7 t. U% [6 I1 J( }
查閱cyberpi API文檔見:童芯派API文檔
# d& V8 y4 p8 R  [$ Z
  • 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。
    ! N6 P# U+ t( j0 b
, V& y6 X" R2 l& {
4.注意事項
在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。

8 Y- X) z7 Y8 c# D8 g5.一些程式設計案例
3 D0 e) c' c2 M# x5 m* I7 `% w童芯派滑鼠
需要安裝pynput模組,參照上面的步驟pip install pynput

$ J2 `, p& |+ g+ p5 Z功能介紹:
9 z7 e( R0 b! U4 A' E0 y利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。) e% I* l( o- O: m2 L6 _! C
通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。, Q' v# L7 o# n" o4 L: x0 l! o
結合藍牙模組可以實現無線滑鼠的功能。' K, J( R% W5 {7 T4 x, Y9 i

# J% |% }: h* t3 `3 d( T2 O. Z, Rfrom pynput.mouse import Button,Controller
: i  Q; h- @+ e. p5 @% o4 C' Q. x% Qimport cyberpi9 y/ W. O+ |5 p1 k! r, F
import time
6 m3 c1 L, y- p' I3 c( l* n% a 5 h, w  \6 `& a/ n. K& a2 U+ b) }
mouse = Controller()
$ g2 f' q* Q6 x- u
& G4 [. ?$ |+ \while True:
8 E- i( `' W9 X4 ^' ]   if cyberpi.is_tiltback():7 v. D. x: E/ ]$ V7 |
       mouse.move(-3, 0)  b2 ~. d& g+ b. x+ _. U
       print(mouse.position)5 b/ b1 n6 m- X6 r, n
   if cyberpi.is_tiltforward():
" R; {$ ~* d! N" s" X5 c. T1 @8 F9 R       mouse.move(3, 0)2 j' s; j1 o4 |4 S
   if cyberpi.is_tiltleft():+ o6 U! M3 t% n3 u, Z
       mouse.move(0, -3): Q. c$ D9 X  C: P" Y
   if cyberpi.is_tiltright():% G2 J1 O. G6 m
       mouse.move(0, 3); U4 K% o% K, r9 ~; ~
   if cyberpi.controller.is_press("b"):
( H: H7 u4 x1 _1 H+ \       mouse.press(Button.left)7 m/ h3 A! b5 N
       mouse.release(Button.left)
3 i" K: `. h. {7 ]/ s4 K7 {       mouse.press(Button.left)
( e/ p$ q6 f6 p* @3 k       mouse.release(Button.left)% \/ h7 j! e5 [
   if cyberpi.controller.is_press("a"):3 O# T4 q( I. e. D6 f2 z8 X& |
       mouse.press(Button.right)
5 ~7 ~4 u# y6 ~/ F9 q" b0 s       mouse.release(Button.right)
# x. \8 g! |& X    time.sleep(0.01); p  {6 C6 ]8 @3 |6 [" D

+ _5 A) m, L+ k6 K9 N4 h- }
& v4 W$ |9 B8 G
語音打字器

1 }6 P! b6 e: E6 g' [! g2 V功能介紹:6 D5 w# k- H& C! j: E% L
利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。
$ Z8 o7 g& ^# ?. r+ Z  {* C將語音辨識的結果通過pynput在文檔中打出來。
9 S# I) ]/ `1 c+ }( w% u該程式目前只能支援英文。
) I  @. ^. r# F' z
/ C" n' j+ u2 @" ^3 qimport cyberpi
8 |0 h9 ?/ m( A+ }: Q# cfrom pynput.keyboard import Key,Controller
* e2 f0 T8 k' \, ^4 `' W9 G+ r6 u& jimport time7 k$ D+ }4 e/ x) j5 A

: G8 g. h3 R; Y2 Nkeyboard = Controller()
, i; q4 V; n$ |5 b8 Z* K4 scyberpi.console.clear()
$ u% l1 D" M8 l" P( }cyberpi.led.on(0, 0, 0); G! f+ ?$ N  w
cyberpi.set_recognition_url()5 k1 B% S" T/ T6 k0 H7 C! z
cyberpi.cloud.setkey("請輸入雲服務授權碼")    # 通過慧程式設計帳號可以獲得3 N; @! L5 L0 N
cyberpi.wifi.connect("WIFI名稱", "WIFI密碼")
" o# l0 O( G7 d, Y# D5 l+ \while not cyberpi.wifi.is_connect():, ~$ ^; K! n; v7 z5 ^9 }' M5 c* r
   pass" q4 P( s  ?6 C/ }6 e1 E: L
cyberpi.led.on(0,0,255)6 W: ^4 c; I' u
cyberpi.console.println("WIFI已連接")
9 g2 X; ~& i$ B# m) v$ ]cyberpi.console.println("--------------"); o5 p0 ~; D9 \2 g8 Y: c% `
cyberpi.console.println("按下A鍵開始語音辨識")5 ?: E0 H$ x& u8 t" u9 K
while True:
- j. |3 `5 F2 c6 {1 n   if cyberpi.controller.is_press('a'):
  Y2 G5 ^9 L( \3 b5 h       keyboard.press(Key.space)( l, v1 H3 k% |2 M
       cyberpi.console.clear()
( ^; d+ y1 f7 O       cyberpi.led.on(100, 0, 0)# Q3 W* O5 m$ M/ q* P- r/ X
       cyberpi.console.println("開始語音辨識")
7 ]4 @/ i/ ]' `* G       cyberpi.audio.play("switch")
+ U( [/ {  n- A0 J+ b3 U6 A+ k       cyberpi.console.println("--------------")+ Y! H$ V5 a# t  K* c
       cyberpi.cloud.listen("english", 2). x& q) a4 N/ g( i
       cyberpi.led.on(0, 0, 0)& D( R  N- k9 {& A
       say = cyberpi.cloud.listen_result(), }1 f" o5 x; v/ K
       cyberpi.console.println(say)) K( S8 |0 Z! Y% b* {
       cyberpi.console.println("--------------")9 Z/ P+ p# m- o6 j1 O$ @
       for i in say:
2 ?" V0 R: J; [3 {5 D1 V            if i == '':0 Y* A# M8 f- a% `
                 keyboard.press(' ')1 U. b, T, B# k' r1 |* z. ~- T+ _- D6 ]
            else:
" ^( `# [- j7 F5 f0 }* I, C( J                keyboard.press(str(i))
2 k# R4 o* S' x6 ^, }                time.sleep(0.03)
3 ~7 A3 S: A# r
; W, ~) J. k6 i6 o# o4 @
matplotlib音量柱狀圖
* a% S6 S# a4 ]2 p9 j+ h
import cyberpi; P, N; D4 J! O3 V9 K
import time3 V/ C7 A. t9 w7 P
from matplotlib import pyplot as plt
! k7 S4 }- t0 @5 |4 |. k 8 q5 D; v! ?" w6 z" _: k
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']1 \9 R( u: i" M6 B
plt.ion()
+ D7 z1 h/ X# j6 P5 r* u
# Y, Y8 F  n' dwhile True:
, U' j" j4 _% U9 [5 r   loud = cyberpi.get_loudness()
, M! ~0 [  m* U; M9 }' C   plt.clf()% @  F5 i% f) r5 h  @
   plt.xlabel("數據名稱")
; _" \- A, k& h: p& ~% M! S% O   plt.ylabel("音量讀值")0 w( s* k/ \. P: _) K
   y_locator = plt.MultipleLocator(5)4 w4 T8 m2 j) H; i
   ax=plt.gca()
, J( Z$ S% J2 P. Z6 [) W   ax.yaxis.set_major_locator(y_locator): M  C* |  W' s; ?( Y
   plt.ylim(0,100)
' g& `0 @& T% T  [( }! P: v5 m0 }   plt.bar('音量', loud, align='center',label=f'溫度{loud}')
: K: E% o$ b) c  l/ l   plt.title('童芯派聲音感測器讀值')
1 `$ L$ @2 i6 p% P+ L' R7 Z& j   plt.legend()7 q! `- b# H; V: Z( [  @
   plt.show()
5 c( T) A" C3 A9 C/ `# n, h: n    plt.pause(0.01)
, d' f1 f7 P8 _/ a0 w% E/ f, n% i( O* f5 t4 y+ N- j$ T. [& A1 }
  c, I1 H, F* ?. c# I
matplotlib 多圖表顯示
, n5 \. X4 i" E1 N9 x6 _8 v
  E% t; B0 t/ i
from matplotlib import pyplot as plt6 A% U: m4 h% x) ^) E4 L7 ]: |! z
import time& p7 j5 I7 s& n# m! W( j" Y! q
import cyberpi
3 ~8 V# j$ R6 A+ j' M , H. A& s% R( Z
loud_list = []! X! X3 K8 u/ @  f5 ^" ?
bri_list = []4 m! T: R9 t# S6 f- o
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']   # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體
# W/ c8 c* P/ o- I$ dplt.ion()/ Q- Z; E5 [; v1 n8 t; c  l8 O
plt.figure(figsize=(10,10))
/ c4 [- M7 _2 j% W9 n' m& D; x9 nplt.figure(1)( _/ M" z% F7 F' Q3 c) G# l* t
x_locator = plt.MultipleLocator(5)
7 z% E& x, x, m3 Q0 Dy_locator = plt.MultipleLocator(5)
- L% i: c  T: ]# g8 _4 S2 Kwhile True:/ p9 H9 M* b) N: S. b
   loud = cyberpi.get_loudness()
, W  H/ ~$ e! B% ~/ z   bri = cyberpi.get_bri()
9 [  I, Z# O& x8 ]9 ?$ S6 m9 z   loud_list.append(loud)0 ]6 ]! k! }4 I( H& u
   bri_list.append(bri)2 J+ U6 h/ g8 q
   battery = cyberpi.get_battery()
. h- {3 U2 Y5 }   size = [battery, 100-battery]
$ `% l; I& O/ J& r   status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']4 Z1 w9 \- C( z
   ax1 = plt.subplot(221)5 _# K" y# K2 E  s" K
   plt.title('光線折線圖')
6 H" [; _' p/ `) m: A& F# E   ax1.plot(bri_list)
8 j6 \+ O; N1 D/ T0 x& N9 |   ax2 = plt.subplot(222)
' N" `, V) H! Z4 W  C   plt.title('聲音柱狀圖')
* ~. E& N+ t! r- u) ~   ax2.xaxis.set_major_locator(x_locator)
; b$ x: n6 E7 T   ax2.yaxis.set_major_locator(y_locator)/ k+ O5 l  @7 Q, N: ]$ f1 g% Q
   plt.ylim(0,100)
. M# q; J. Q6 o3 ~2 Y/ M" E4 _   ax2.bar('sound', loud)) ~) o# E& {) h; a
   ax3 = plt.subplot(223)8 j- x2 [( p! g1 H* E$ b
   ax3.xaxis.set_major_locator(x_locator)/ \1 D0 Q* G2 J: A6 W
   ax3.yaxis.set_major_locator(y_locator)2 j5 s: {# _, s6 g* }8 I, Z6 {
   plt.xlim(0,100)
3 c7 V) P$ S0 q6 T; q  ^: Y   plt.ylim(0,100)
3 \. a, F. P  p, `; y7 K   plt.title('聲音、音量散點圖')
) r7 ]# _6 Q4 h$ p# U$ Q! \   ax3.scatter(loud_list,bri_list)
# ^" B7 o" \$ Y3 c   ax4 = plt.subplot(224)
$ z5 v5 o/ n0 e) W4 v" D4 _   ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})
4 b, x: m: R4 }( q: N* t   plt.title('童芯派電量')5 V/ ^  r$ n* s7 o: i$ @
   plt.pause(0.2)( O0 y7 h" a/ |0 C6 n7 O. o
   plt.clf()  A' h' o. O% b: i* i, C. P/ S- |9 M
   if cyberpi.controller.is_press('a'):) l( I$ S, b: }( A4 }( r
       break/ O, s  c4 y. H: f& X. d, N4 ]5 R% Q
   if len(bri_list) > 500:/ P  n% Q. F' _1 z& d$ h
       bri_list = []
# {/ p( d+ ]( L        loud_list = []8 z* r4 \" f. a
, ^+ G0 \6 p' y

本帖子中包含更多資源

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

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

本版積分規則

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

GMT+8, 2025-12-4 21:24 , Processed in 0.021547 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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