[FabNotes23] 作業(yè)11:輸入設(shè)備 | Week11 - Input Devices

給微控制器電路增加一個(gè)傳感器,測(cè)量感興趣的值

這周開始,芯片 Attiny45 將會(huì)頻繁出場(chǎng):

電路設(shè)計(jì)

嘗試用 phototransistor (光電晶體管)作為輸入模塊,去控制 LED 。

在 Eagle 先畫 schematic:

然后生成 board。可以先用 autorouter 自動(dòng)布線,然后再根據(jù)需要手動(dòng)調(diào)整。

在完成設(shè)計(jì)之前,需要用 ERC 命令檢查 schematic 的錯(cuò)誤,用 DRC 檢查 board 的錯(cuò)誤。根據(jù)機(jī)器刀頭的尺寸,設(shè)置線路間隔為 16mil 。

全部仔細(xì)檢查后,再分別把 top 圖層和 dimension 圖層輸出為兩個(gè) png 文件,要注意勾選單色輸出:

Download the eagle sch & brd & png files.

電路板制作

依然使用 Roland SMR-20 刻電路板,用 1/64 刀頭刻線路,1/32 切割外框。在 fabmodules.org 里面,設(shè)置輸入的 png 圖像 dpi 為 1500 ,然后計(jì)算雕刻路徑:

將元件們焊好:

程序

這周開始用 Arduino 寫程序。在上傳之前,需要選好板子的類型以及芯片。 Attiny45 使用 8 MHz 內(nèi)頻。

代碼如下,也可以到這里下載


  // set pin numbers:
const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  4;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

在這個(gè)簡(jiǎn)單的電路程序中,光電晶體管起到開關(guān)的作用,根據(jù)光亮控制電路中 LED 的明滅:

試驗(yàn)1:用 Python 和串口通信

制作一塊帶有光電晶體管的板子:

下載 hello.light.45.cmakefile。在 terminal cd 進(jìn)入文件夾,輸入命令 make -f hello.light.45.make

得到回應(yīng):

avr-gcc -mmcu=attiny45 -Wall -Os -DF_CPU=8000000 -I./ -o hello.button.45.out hello.button.45.c
avr-objcopy -O ihex hello.button.45.out hello.button.45.c.hex;\
    avr-size --mcu=attiny45 --format=avr hello.button.45.out
AVR Memory Usage
----------------
Device: attiny45

Program:     364 bytes (8.9% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)

然后輸入命令: sudo make -f hello.light.45.make program-usbtiny

得到回應(yīng):

avr-objcopy -O ihex hello.light.45.out hello.light.45.c.hex;\
    avr-size --mcu=attiny45 --format=avr hello.light.45.out
AVR Memory Usage
----------------
Device: attiny45

Program:     502 bytes (12.3% Full)
(.text + .data + .bootloader)

Data:          1 bytes (0.4% Full)
(.data + .bss + .noinit)


avrdude -p t45 -P usb -c usbtiny -U flash:w:hello.light.45.c.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9206
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "hello.light.45.c.hex"
avrdude: input file hello.light.45.c.hex auto detected as Intel Hex
avrdude: writing flash (502 bytes):

Writing | ################################################## | 100% 0.74s

avrdude: 502 bytes of flash written
avrdude: verifying flash memory against hello.light.45.c.hex:
avrdude: load data flash data from input file hello.light.45.c.hex:
avrdude: input file hello.light.45.c.hex auto detected as Intel Hex
avrdude: input file hello.light.45.c.hex contains 502 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.94s

avrdude: verifying ...
avrdude: 502 bytes of flash verified

Python 程序可以在這里下載.

用 USB to TTL 連接好板子并運(yùn)行程序:

python hello.light.45.py /dev/ttyUSB0

這時(shí)出現(xiàn)錯(cuò)誤提示:

Traceback (most recent call last):
  File "hello.light.45.py", line 62, in <module>
    ser = serial.Serial(port,9600)
  File "/Library/Python/2.7/site-packages/serial/serialutil.py", line 180, in __init__
    self.open()
  File "/Library/Python/2.7/site-packages/serial/serialposix.py", line 294, in open
    raise SerialException(msg.errno, "could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'

Google 一下這個(gè)問題 Failed to open port /dev/ttyUSB0 - ROS Answers: Open Source Q&A Forum

問題可能是因?yàn)槲疫€沒有裝 FTDI (用于 USB 和串口通信)驅(qū)動(dòng)。

使用命令 ls /dev/tty* 可以列出當(dāng)前可用的串口。確實(shí)沒有 /dev/tty.usbserial-A400gwhT 串口。所以我嘗試安裝 FTDI 驅(qū)動(dòng) - D2XX Direct Drivers 以及 Virtual COM Port Drivers。但是依然不成功。

接著,Google 到這一篇 How to Install FTDI Drivers - learn.sparkfun.com 照著再裝了一遍驅(qū)動(dòng)。重啟電腦后,插入 FTDI 2 USB 設(shè)備, /dev/tty.usbserial-A400gwhT 總算出現(xiàn)在串口列表中。

接著,嘗試用新的串口運(yùn)行程序:

python hello.light.45.py /dev/tty.usbserial-A400gwhT 9600

繼續(xù)看到錯(cuò)誤提示:

command line: hello.light.45.py serial_port

到 python 程序中仔細(xì)看了看,將 len(sys.argv) 從 2 改為 3:

if (len(sys.argv) != 3):
   print "command line: hello.light.45.py serial_port"
   sys.exit()
port = sys.argv[1]

問題解決:

測(cè)試視頻

實(shí)驗(yàn)2:開關(guān)

做了一個(gè)帶開關(guān)的板子:

下載 hello.button.45.cmakefile。 在 terminal cd 進(jìn)入文件夾,運(yùn)行命令: make -f hello.button.45.makesudo make -f hello.button.45.make program-usbtiny。都正常。

然后用 TTL 2 USB 連接板子:

運(yùn)行命令

python term.py /dev/tty.usbserial-A400gwhT 9600

每按一次按鈕,屏幕打出一個(gè)“du”:

測(cè)試視頻

實(shí)驗(yàn)3:溫度傳感器

做了一個(gè)帶有溫度傳感器的板子:

下載 hello.temp.45.cmakefile。輸入命令 make -f hello.button.45.makesudo make -f hello.button.45.make program-usbtiny

然后運(yùn)行程序 python hello.temp.45.py /dev/tty.usbserial-A400gwhT 9600 。傳感器開始實(shí)時(shí)監(jiān)測(cè)溫度變化。

課程資源

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,247評(píng)論 6 543
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,520評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,362評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,805評(píng)論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,541評(píng)論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,896評(píng)論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,887評(píng)論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,062評(píng)論 0 290
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,608評(píng)論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,356評(píng)論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,555評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,077評(píng)論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,769評(píng)論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,175評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,489評(píng)論 1 295
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,289評(píng)論 3 400
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,516評(píng)論 2 379

推薦閱讀更多精彩內(nèi)容