Post on 14-Jan-2017
電子工程系應 用 電 子 組電 腦 遊 戲 設 計 組
教學(10)藍牙控制應用
吳錫修
Feb 18, 2017
shap
e th
e fu
ture
藍牙 (Bluetooth)是⼀種短距、低功耗無線技術標準
藍牙使用2.402-2.480GHz的ISM (Industrial Scientific Medical) 頻段,共79個頻道 (頻寬為1MHz)
2002年藍牙成為IEEE 802.15.1標準
藍牙微網 (Piconet) 由⼀個主裝置 (master) 與最多七個作業中的從屬 (slave) 裝置所組成,使用分時雙工 (TDD,Time-Division Duplexing) 方式傳輸
藍牙傳輸簡介
2
分時雙工
Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
mBot藍牙模組
3
Bluetooth
Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
手機與mBot藍牙配對
完成配對後可重新命名以便管理
準備作業
4 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
Makeblock官方App,可直接操控mBot
須將mBot恢復出廠程序
使用mBot app 1/8
5 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
1. 與mBot連線
A. 開啟藍牙裝置管理
使用mBot app 2/8
6 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
B. 點選Makeblock連線
使用mBot app 3/8
7 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
2. 無人駕駛模式,自動避障礙
使用mBot app 4/8
8
喇叭聲
切換mBot LED顏色
極速衝刺5秒Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
3. 手動操作模式
使用mBot app 5/8
9
拖曳操控mBot在其它模式下,拖曳就會自動回到手動操作模式
Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
4. 軌道巡跡模式
使用mBot app 6/8
10 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
5. 手機G-sensor操控模式
手機前後左右傾斜控制mBot移動,傾斜角度愈大,mBot速度愈快
使用mBot app 7/8
11 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
6. 搖手機動力操控模式,只能前進或後退
使用mBot app 8/8
12
切換前進或後退
Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
⼀款免費通用的藍牙傳輸app,提供遊戲搖桿、三軸重力感測、電子羅盤、賽車方向盤…等7種操作介面
必須依據Bluetooth RC送出的訊息,撰寫mBot端受控程式
使用Bluetooth RC 1/13
13 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
1. 點擊 選擇mBot藍牙裝置
使用Bluetooth RC 2/13
14
目前選定的藍牙裝置
操作模式Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
選取mBot藍牙裝置
使用Bluetooth RC 3/13
15 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
2. 使用GamePAD操作介面
按下 開始藍牙連線,變成 表示連線OK
按下左側按鈕會分別送出F、R、B、L;按下右側按鈕會分別送出D、O、X、S
使用Bluetooth RC 4/13
16 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
3. 使用G-sensor操作介面
手機保持水平時送出N,朝八方向傾斜則會分別送出F、I、R、J、B、H、L、G;狀態變化時才送出⼀次,不會連續發送
使用Bluetooth RC 5/13
17 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
4. 使用電子羅盤操作介面
後面數字表示手機目前方位,0表示朝向正北方
第⼀個數字表示從按下 開始,手機方位角度變化值
方位異動時,會送出數值字串
使用Bluetooth RC 6/13
18 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
5. 使用汽車駕駛操作介面
預設以G-sensor控制方向盤,按下 可切換為按鈕方式控制方向盤
使用Bluetooth RC 7/13
19
油門刹車
倒車檔
Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
按下右邊Lock,油門及刹車不會自動放開
按下左邊Lock,方向盤不會自動回正 (只適用按鈕操作模式)
使用Bluetooth RC 8/13
20
左轉 右轉 轉動量
Lock
Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
G-Sensor模式下,方向盤數值由S0、S10、…到S180,方向盤回正時為S90,方向盤往右轉時遞減,方向盤往左轉時遞增;狀態變化時才發送
油門輸出D0、D1、...到D9,倒檔時為R0、R1、...到R9,持續發送
按鈕分別送出C1、C2、C3、C4
使用Bluetooth RC 9/13
21 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
6. 使用文字訊息交談介面
在文字框輸入資料,按下 即可;LOG窗格會記錄送出的訊息及收到的回傳訊息
使用Bluetooth RC 10/13
22
清空LOG
Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
7. 按鈕操作介面
按 送出Cmd欄內容
按鈕分別送出A、B、C、D、E、F、G、H、i、J
使用Bluetooth RC 11/13
23 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
8. 數字鍵操作介面
按 與藍牙連線/中斷連線
按鍵分別送出0~9
使用Bluetooth RC 12/13
24 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
上方面板會分別顯示傳送與接收資料
第⼀⾏為發送資料,第⼆⾏為接收資料 (無資料則顯示Ready),收到資料時會顯示「Rx>接收字串」
使用Bluetooth RC 13/13
25 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
開啟通訊擴充模組
mBlock選單「擴充/Communication」
檢查是否收到藍牙傳輸資料,回傳值為0表示已有收到資料
讀取藍牙傳輸字串
字串比對,用來檢查收到的字串內容
送出字串
mBot藍牙控制指令
26 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
功能要求
使用Bluetooth RC G-sensor操作介面,控制mBot移動
G-sensor控制mBot練習 1/4
27 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
G-sensor狀態對應的控制碼
F
B
R
L
N
G-sensor控制mBot練習 2/4
28 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
參考程式
mBlock選單「擴充/Communication」
G-sensor控制mBot練習 3/4
29 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
G-sensor控制mBot練習 4/4
30 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
App Inventor是⼀款免費的Android App開發工具,它使用類似於Scratch語言和StarLogo TNG圖形化界面,透過拖放積木指令來設計Android App
ai2.appinventor.mit.edu
使用App Inventor設計mBot App
依據App送出的訊息,撰寫mBot端受控程式
設計客制化mBot App
31 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
畫面設計
App Inventor設計mBot App 1/5
32 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
程式碼
App畫面初始作業
App Inventor設計mBot App 2/5
33 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
點擊藍牙裝置清單作業
App Inventor設計mBot App 3/5
34 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
點擊方向按鈕作業
App Inventor設計mBot App 4/5
35 Wu, ShyiShiou Dept. of E.E.
shap
e th
e fu
ture
點擊中斷藍牙連線按鈕作業
App Inventor設計mBot App 5/5
36 Wu, ShyiShiou Dept. of E.E.