Dept. of Electronics Engineering
教學(10)
藍牙控制應用 吳錫修
July 10, 2017
shapethefuture
 藍牙 (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.
shapethefuture
mBot藍牙模組
3
Bluetooth
Wu, ShyiShiou Dept. of E.E.
shapethefuture
 開啟手機藍牙傳輸與mBot藍牙配對
 完成配對後可重新命名以便管理
準備作業
4 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 Makeblock App,可藍牙連線操控mBot
使用Makeblock App 1/16
5 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 將mBot恢復出廠程序
 開啟Makeblock App,將手機靠近mBot
使用Makeblock App 2/16
6 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 連線會後自動進入程式選單
 點擊控制器
使用Makeblock App 3/16
7 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 使用Makeblock App控制器程式
 遙控模式
使用Makeblock App 4/16
8 Wu, ShyiShiou Dept. of E.E.
喇叭聲LED開關
全速前進3秒 左右搖擺3秒
原地旋轉3秒
搖桿控制移動
shapethefuture
 你畫我跑模式
 用手在作業區畫出移動路徑,按下開始按鈕,mBot即依路徑行進
使用Makeblock App 5/16
9 Wu, ShyiShiou Dept. of E.E.
清除路徑
開始行進
shapethefuture
 電子琴模式
 自由彈奏鍵盤或撥放預設音樂 (聖誕歌、小星星、生日快樂、二隻老虎)
使用Makeblock App 6/16
10 Wu, ShyiShiou Dept. of E.E.
撥放音樂
自由彈奏
shapethefuture
 點擊mBot
使用Makeblock App 7/16
11 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 使用Makeblock App mBot程式
 在運行模式下,直接使用儀表板操控mBot
使用Makeblock App 8/16
12 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 在編輯模式下,可重新配置操控元件及修改程式;操控元件分四大類:
 運動作業元件
使用Makeblock App 9/16
13 Wu, ShyiShiou Dept. of E.E.
shapethefuture
使用Makeblock App 10/16
14 Wu, ShyiShiou Dept. of E.E.
 顯示作業元件
shapethefuture
使用Makeblock App 11/16
15 Wu, ShyiShiou Dept. of E.E.
 感知作業元件
shapethefuture
使用Makeblock App 12/16
16 Wu, ShyiShiou Dept. of E.E.
 使用者自訂作業元件
 前三類元件已內建預設程式碼,而自定義元件則無預設程式碼
shapethefuture
使用Makeblock App 13/16
17 Wu, ShyiShiou Dept. of E.E.
 修改程式碼
 點擊元件開啟編輯選單,點選編程即可進入程式編輯作業
shapethefuture
 程式編輯作業
使用Makeblock App 14/16
18 Wu, ShyiShiou Dept. of E.E.
積木指令分類
儀表板元件清單
縮小/放大指令區
指令區
shapethefuture
使用Makeblock App 15/16
19 Wu, ShyiShiou Dept. of E.E.
 加入積木指令
 開啟指令分類,點擊想要加入的積木指令
shapethefuture
使用Makeblock App 16/16
20 Wu, ShyiShiou Dept. of E.E.
 移除積木指令
 拖曳積木指令到垃圾桶
shapethefuture
 ⼀款免費通用的藍牙傳輸app,提供
遊戲搖桿、三軸重力感測、電子羅盤、
賽車方向盤…等7種操作介面
 必須依據Bluetooth RC送出的訊息,
撰寫mBot端受控程式
使用Bluetooth RC 1/13
21 Wu, ShyiShiou Dept. of E.E.
shapethefuture
1. 點擊 選擇mBot藍牙裝置
使用Bluetooth RC 2/13
22
目前選定的藍牙裝置
操作模式
Wu, ShyiShiou Dept. of E.E.
shapethefuture
 選取mBot藍牙裝置
使用Bluetooth RC 3/13
23 Wu, ShyiShiou Dept. of E.E.
shapethefuture
2. 使用GamePAD操作介面
 按下 開始藍牙連線,變成 表示連線OK
 按下左側按鈕會分別送出F、R、B、L;按下右側按鈕會分別送出D、O、
X、S
使用Bluetooth RC 4/13
24 Wu, ShyiShiou Dept. of E.E.
shapethefuture
3. 使用G-sensor操作介面
 手機保持水平時送出N,朝八方向傾斜則會分別送出F、I、R、J、B、H、
L、G;狀態變化時才送出⼀次,不會連續發送
使用Bluetooth RC 5/13
25 Wu, ShyiShiou Dept. of E.E.
shapethefuture
4. 使用電子羅盤操作介面
 後面數字表示手機目前方位,0表示朝
向正北方
 第⼀個數字表示從按下 開始,
手機方位角度變化值
 方位異動時,會送出數值字串
使用Bluetooth RC 6/13
26 Wu, ShyiShiou Dept. of E.E.
shapethefuture
5. 使用汽車駕駛操作介面
 預設以G-sensor控制方向盤,按下 可切換為按鈕方式控制方向盤
使用Bluetooth RC 7/13
27
油門刹車
倒車檔
Wu, ShyiShiou Dept. of E.E.
shapethefuture
 按下右邊Lock,油門及刹車不會自動放開
 按下左邊Lock,方向盤不會自動回正 (只適用按鈕操作模式)
使用Bluetooth RC 8/13
28
左轉 右轉 轉動量
Lock
Wu, ShyiShiou Dept. of E.E.
shapethefuture
 G-Sensor模式下,方向盤數值由S0、S10、…到S180,方向盤回正時為
S90,方向盤往右轉時遞減,方向盤往左轉時遞增;狀態變化時才發送
 油門輸出D0、D1、...到D9,倒檔時為R0、R1、...到R9,持續發送
 按鈕分別送出C1、C2、C3、C4
使用Bluetooth RC 9/13
29 Wu, ShyiShiou Dept. of E.E.
shapethefuture
6. 使用文字訊息交談介面
 在文字框輸入資料,按下 即可;LOG窗格會記錄送出的訊息及收
到的回傳訊息
使用Bluetooth RC 10/13
30
清空LOG
Wu, ShyiShiou Dept. of E.E.
shapethefuture
7. 按鈕操作介面
 按 送出Cmd欄內容
 按鈕分別送出A、B、C、D、E、F、G、H、i、J
使用Bluetooth RC 11/13
31 Wu, ShyiShiou Dept. of E.E.
shapethefuture
8. 數字鍵操作介面
 按 與藍牙連線/中斷連線
 按鍵分別送出0~9
使用Bluetooth RC 12/13
32 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 上方面板會分別顯示傳送與接收資料
 第⼀行為發送資料,第二行為接收資
料 (無資料則顯示Ready),收到資料
時會顯示「Rx>接收字串」
使用Bluetooth RC 13/13
33 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 開啟通訊擴充模組
 mBlock選單「擴充/Communication」
 檢查是否收到藍牙傳輸資料,回傳值為0表示已有收到資料
 讀取藍牙傳輸字串
 字串比對,用來檢查收到的字串內容
 送出字串
mBot藍牙控制指令
34 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 功能要求
 使用Bluetooth RC G-sensor操作介面,控制mBot移動
G-sensor控制mBot練習 1/4
35 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 G-sensor狀態對應的控制碼
  F
  B
  R
  L
 N
G-sensor控制mBot練習 2/4
36 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 參考程式
 mBlock選單「擴充/Communication」
G-sensor控制mBot練習 3/4
37 Wu, ShyiShiou Dept. of E.E.
shapethefuture
G-sensor控制mBot練習 4/4
38 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 App Inventor是⼀款免費的Android App開發工具,它使用類似於
Scratch語言和StarLogo TNG圖形化界面,透過拖放積木指令來設
計Android App
 ai2.appinventor.mit.edu
 使用App Inventor設計mBot App
 依據App送出的訊息,撰寫mBot端受控程式
設計客制化mBot App
39 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 畫面設計
App Inventor設計mBot App 1/5
40 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 程式碼
 App畫面初始作業
App Inventor設計mBot App 2/5
41 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 點擊藍牙裝置清單作業
App Inventor設計mBot App 3/5
42 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 點擊方向按鈕作業
App Inventor設計mBot App 4/5
43 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 點擊中斷藍牙連線按鈕作業
App Inventor設計mBot App 5/5
44 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 Makeblock提供⼀個App Inventor擴充模組,大大的簡化mBot App
開發作業
 mBot端不需要撰寫程式,只要將mBot恢復出廠程序即可
 參考資料
 http://learn.makeblock.com/en/use-mbot-with-app-inventor-v-1-9/
App Inventor的mBot擴充模組 1/7
45 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 點擊Import extension,輸入URL
http://appinventor.makeblock.com/MBot_v1.9.aix
App Inventor的mBot擴充模組 2/7
46 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 設定mBot擴充模組名稱,使用預設值即可
App Inventor的mBot擴充模組 3/7
47 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 完成mBot擴充模組安裝
App Inventor的mBot擴充模組 4/7
48 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 設計mBot App UI
App Inventor的mBot擴充模組 5/7
49 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 藍牙連線/中斷連線作業
App Inventor的mBot擴充模組 6/7
50 Wu, ShyiShiou Dept. of E.E.
shapethefuture
 點擊方向按鈕作業
App Inventor的mBot擴充模組 7/7
51 Wu, ShyiShiou Dept. of E.E.

mBot 教學10 藍牙控制應用