本篇文章將介紹綜合性的應用,透過智慧型手機上的MediaTek Cloud Sandbox (聯發科的免費雲端資料平台,簡稱MCS)app,經由WiFi傳送訊息到我們的控制器Intel Edison,並控制我們的機械手臂MeArm
事前準備:
- Intel Edison 要先準備好linux的系統,本範例使用的是ubilinux,並且完成最基本的GPIO設定
- 本範例會需要用到python requests函式庫來跟MCS溝通,所以也請先安裝
- 在MCS上新增一個Prototype及test device,記下device id , device key
- MCS的Prototype加入本範例需要用的gamepad元件,並將ID命名為gamepad,如圖:
硬體部分:準備好MeArm機械手臂,連接好我們的控制器Intel Edison,如圖:
程式部分:
import mraa
import time
import requests
device_id = "輸入device_id"
device_key = "輸入device_key"
data_channel = "gamepad"
url = "http://api.mediatek.com/mcs/v2/devices/" + device_id
url += "/datachannels/" + data_channel + "/datapoints.csv"
def game_pad():
r = requests.get(url, headers = {"deviceKey" : device_key})
data = r.content.split(',')[2:]
print data
#time.sleep(0.01)
return (data[0][0], data[0][-1])
pin = [3, 5, 6, 9]
servo = []
servo_degree = [90, 90, 90, 180]
for p in pin:
servo.append(mraa.Pwm(p))
for s in servo:
s.period_us(20000) #50Hz
s.enable(False)
max_duty = 2300.0 #us
min_duty = 500.0
def servo_rotate(servo_number, degree): #servo_number (0, 1, 2, 3) = (3, 5, 6, 9)
duty_cycle = ((max_duty - min_duty) * degree / 180.0 + min_duty) / 20000.0
servo[servo_number].enable(True)
servo[servo_number].write(duty_cycle)
print "duty_cycle = ", duty_cycle, "\n"
dt = 0.01
for s in servo:
s.enable(False)
unit_degree = 30
while True:
command = game_pad()
if command[1] == "1":
if command[0] == "l":
print "left"
if servo_degree[0] > unit_degree:
servo_degree[0] -= unit_degree
servo_rotate(0, servo_degree[0])
elif command[0] == "r":
print "right"
if servo_degree[0] < 180 - unit_degree:
servo_degree[0] += unit_degree
servo_rotate(0, servo_degree[0])
elif command[0] == "d":
print "down"
if servo_degree[2] < 180 - unit_degree:
servo_degree[2] += unit_degree servo_rotate(2, servo_degree[2])
elif command[0] == "u":
print "up"
if servo_degree[2] > unit_degree:
servo_degree[2] -= unit_degree
servo_rotate(2, servo_degree[2])
elif command[0] == "A":
print "A"
servo_rotate(3, 0)
elif command[0] == "B":
print "B"
servo_rotate(3, 180)
最後就是影片展示啦!
Post Views: 290