這幾天收拾的時候發(fā)現(xiàn)了Phidget,這還是上一個案子客戶給的猿妈。型號是1014_2吹菱,記得是當時拿來做壓力測試用的。
過太久都忘了怎么用了彭则,所以再拾遺一下鳍刷。
因為重裝Ubuntu 16.04的系統(tǒng),所以就從頭裝一遍phidget在Linux平臺上的運行環(huán)境俯抖。
可參考Phidget官網(wǎng)输瓜,這里只是大體說一下。
Linux環(huán)境的搭建
安裝libusb的開發(fā)庫
sudo apt-get install libusb-1.0-0-dev
安裝Phidget庫
點擊下載路徑下載Phidget庫芬萍。
解壓后尤揣,依次執(zhí)行:
./configure
make
sudo make install
檢查Phidget庫的安裝
Software:
下載官方提供的示例
解壓后編譯HelloWorld.c
gcc HelloWorld.c -o HelloWorld -lphidget21
運行HelloWorld
jasper@jasper:~/phidget/phidget21-c-examples-2.1.8.20151217$ sudo ./HelloWorld
Opening...
Phidget Simple Playground (plug and unplug devices)
Press Enter to end anytime...
Hello Device Phidget InterfaceKit 0/0/4, Serial Number: 381507
Goodbye Device Phidget InterfaceKit 0/0/4, Serial Number: 381507
Closing...
正常情況下,伴隨著phidget設備的插入/拔出柬祠,能對應的看到Hello/Goodbye的信息芹缔。
Hardware:
使用kernal log查看插入/拔出信息:
插入:
jasper@jasper:~$ dmesg | tail
.......
[17239.182460] usb 2-1.4: new low-speed USB device number 13 using ehci-pci
.......
拔出:
jasper@jasper:~$ dmesg | tail
......
[17262.852520] usb 2-1.4: USB disconnect, device number 13
......
正常情況下插入時用到ehci-pci的device number會在拔出時disconnect掉。
Phidget的python套件安裝
下載并解壓phidget的python套件
jasper@jasper:~/phidget/PhidgetsPython$ sudo python setup.py install
可以下載官方示例驗證瓶盛。
至此最欠,Phidget在Linux平臺上的Python環(huán)境就安裝好了示罗。
Phidget的使用步驟
Python API可參考官方API文檔
Step 4:設備的初始化和打開
self.device = InterfaceKit()
self.device.openPhidget()
Step 4:等待phidget設備的接入
self.device.waitForAttach(10000)
Step 4:對設備的操作
self.device.setOutputState(output,1)
Step 4:設備的關閉
self.device.closePhidget()
實際操作
手頭材料
phidget板: 型號是1014_2(實際上就是4個relay),能做的無非就是當作開關控制線路。
小米電風扇: 小米官網(wǎng)可購芝硬,19塊好像蚜点。
USB線: 延長線更好,能省去不少麻煩拌阴。
Micro USB線: 10年以后的Phidget 1014_2采用了Micro USB連接绍绘。
實操
因為只是為了控制電風扇的開關,所以USB的四條線我們只用電源正級和接地就好迟赃,因為用不到數(shù)據(jù)部分陪拘,所以D+/D-就不用管了。
將USB小的那頭剪去,并扯出紅線和黑線。將紅線剪下一半腰埂。然后按照下圖連接Phidget和風扇就好畔乙。這里在連風扇的USB時很麻煩,有延長線的話就簡單多了,但是我這里就一條,舍不得浪費….接風扇的時候連USB長的那兩條即可,中間那兩個是數(shù)據(jù)傳輸用的喇辽。
連好后的總體效果如圖:
控制代碼如下:
import time
from Phidgets.PhidgetException import *
from Phidgets.Devices.InterfaceKit import *
class TestPhidget(object):
? ? def __init__(self):
? ? ? ? pass
? ? def __enter__(self):
? ? ? ? try:
? ? ? ? ? ? self.device = InterfaceKit()
? ? ? ? ? ? self.device.openPhidget()
? ? ? ? ? ? self.device.waitForAttach(10000)
? ? ? ? ? ? returnself
? ? ? ? exceptPhidgetException, e:
? ? ? ? ? ? exit(1)
? ? def relayOn(self, output):
? ? ? ? self.device.setOutputState(output,1)
? ? ? ? time.sleep(1)
? ? def relayOff(self, output):
? ? ? ? self.device.setOutputState(output,0)
? ? ? ? time.sleep(1)
? ? def __exit__(self, e_t, e_v, t_b):
? ? ? ? try:
? ? ? ? ? ? self.device.closePhidget()
? ? ? ? exceptPhidgetException, e:
? ? ? ? ? ? exit(1)
? ? def test():
? ? ? ? import optparse
? ? ? ? parser = optparse.OptionParser()
? ? ? ? parser.add_option("-n",
? ? ? ? ? ? dest ="counts",
? ? ? ? ? ? action ="store",
? ? ? ? ? ? help ="Number of test counts"
? ? ? ? )
? ? ? ? (options, args) = parser.parse_args()
? ? ? ? counts = int(options.counts)
? ? ? ? with TestPhidget() as testphidget:
? ? ? ? ? ? time.sleep(1)
? ? ? ? ? ? foriinrange(0, counts):
? ? ? ? ? ? testphidget.relayOff(3)
? ? ? ? ? ? time.sleep(7)
? ? ? ? ? ? testphidget.relayOn(3)
? ? ? ? ? ? time.sleep(5)
if __name__ == '__main__':
? ? test()
這些統(tǒng)統(tǒng)可以在官方API文檔里找得到。
效果就是控制風扇的開關雨席。僅此而已(好無聊@@)
結語
想了解更多Phidget的信息可以查看官方網(wǎng)址菩咨。
想要了解1014_2原理的可以參考這里,只要你學過高中物理,我覺得理解應該沒有問題陡厘。
更多請參考4coding