W!o+ 的《小伶鼬工坊演義》︰樹莓派 3 小樹林系統之回歸

總是到了回歸『小樹林系統』的時候了。隨著時間流逝,生怕記憶會褪色,學習會生疏。故於此摘要過往篇章,以為複習也。

W!o+ 的《小伶鼬工坊演義》︰ 一窺全豹之系統設計《實驗》

……

將要如何追查

若是考以不同『裝置組合』,為什麼『超聲波測距儀』 Ultrasonic Ranger 與『繼電器』 Relay 分接『三、七』可以!但是『二、八』卻不行的呢?

import time
import grovepi

relay = 8

ultrasonic_ranger = 2

grovepi.pinMode(relay,"OUTPUT")

time.sleep(0.1)

grovepi.digitalWrite(relay,1)

time.sleep(0.5)

while True:
    try:

# 因派生二、三『 print 』語法不同之相容寫法。
        print (grovepi.ultrasonicRead(ultrasonic_ranger))

        time.sleep(0.1)

    except IOError:
        print ("Error")

。 一個簡單如此的程式!又怎麼會出錯的呢?既然是寫 W!o+ 的『小伶鼬工坊演義』,總該採納 Mrphs 是怎麼說的哩!!恍恍惚惚中,或是這麼講的吧︰ W!o+ 認為『單獨』與『共聚』物件情況,有很大的不同。縱使彼此可以排除『干擾性』,還有『同時性』和『共時性』的難題,這大部分發生於『資源性』之競奪領域。 若是因『時序性』之問題引起,它在『通訊規範』的設計上尤其重要。大自然春暖花開固然花花無礙,社會上洛陽紙貴可就買不著的了。這些終究還是得用『實驗』 來『確立』的事務。……

───

 

W!o+ 的《小伶鼬工坊演義》︰ 一窺全豹之系統設計‧探索‧甲

……

之『★★』的『論難』也。所以這裡僅先列出『修改』、『測試』之事,再做『討論』耶??!!

pi@raspberrypi ~/example python Python 2.7.9 (default, Mar  8 2015, 00:52:26)  [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import grovepi >>> addr = 0x05  # 不觸動韌體『 receiveData 』, Wire 傳回 0  >>> grovepi.read_i2c_byte(addr) 0 # 觸動韌體『 receiveData 』, SMBus 讀回 32 byte 。注意第二 Byte 亦為 0 。這或許是 GrovePi 的回傳緩衝器 buffer 始於一之故。 由於會改變 index ,恐有『越界』之虞。 >>> grovepi.read_i2c_block(addr) [11, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255] # Grove - Rotary Angle Sensor 測試 >>> potentiometer = 0 >>> sensor_value = grovepi.analogRead(potentiometer) 0 [11, 2, 190, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255] >>> sensor_value 702 # 注意。注意。這造成 cmd[5] array 指標『越界』。 >>> grovepi.read_i2c_block(addr) [11, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255] # 無須設定 Grove - Rotary Angle Sensor 為『輸入』? >>> grovepi.pinMode(potentiometer,"INPUT") 1 # Ultrasonic sensor 接於『二』測試。已將//attachInterrupt(0,readPulseDust,CHANGE); 『刪除』。 請注意多次讀取值。 >>> ultrasonic_ranger = 2 >>> grovepi.ultrasonicRead(ultrasonic_ranger) 0 [11, 1, 196, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]  452 # Relay 接於『八』測試 >>> relay = 8 # 一定要宣告 pinMode 為『輸出』 >>> grovepi.pinMode(relay,"OUTPUT") 1 # relay 開 >>> grovepi.digitalWrite(relay,1) 1 # relay 關 >>> grovepi.digitalWrite(relay,0) 1 # 與 relay 沒有交互影響。注意讀取值與之前不同 >>> grovepi.ultrasonicRead(ultrasonic_ranger) 0 [11, 0, 199, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255] 199 # 注意指標『越界』 >>> grovepi.read_i2c_block(addr) [11, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255] # 注意指標『變化』 >>> grovepi.digitalWrite(relay,1) 1 # 穩定性測試 >>> grovepi.analogRead(potentiometer) 0 [11, 2, 190, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]  702 >>>  </pre> ───     <span style="color: #666699;"><a style="color: #666699;" href="http://www.freesandal.org/?p=47815">W!o+ 的《小伶鼬工坊演義》︰ 一窺全豹之系統設計‧探索‧乙</a></span>  ……  <span style="color: #808000;">既然欲覽 GrovePi 之園林景觀,又已知其韌體</span>  <span style="color: #ff9900;">void receiveData(int byteCount)</span>  <span style="color: #808000;">有『越界』的事實。豈應不先修繕,就隨意斧鑿開墾的耶!因此在繼續探索前, 略作一點『補丁』,同時制定『除錯』啟閉『指令』 ,這也算未來增刪『感測器』時,先將寫程式之章法預演一下。 </span>  <span style="color: #808080;"><strong>【 grove_pi_v1_2_5.ino 補丁】</strong></span> <pre class="lang:default decode:true"># grove_pi_v1_2_5.ino 第三十三行。 int cmd[5]; int index=0; int flag=0; int i; byte val=0,b[21],float_array[4],dht_b[21]; unsigned char dta[21]; int length; int aRead=0; byte accFlag=0,clkFlag=0; int8_t accv[3]; byte rgb[] = { 0, 0, 0 }; int run_once; # 增加 debug int debug = 0;  # 第五百八十九行         else if(cmd[0]==flow_dis_cmd)     {                 flow_run_bk=0;         detachInterrupt(0);         cmd[0]=0;     }  # 增加啟閉指令 # 開啟     else if(cmd[0]==254)       debug=1; # 關閉     else if(cmd[0]==255)       debug=0;   }  # 第六百五十六行        cmd[index++] = Wire.read();     } # 出 while 迴圈後,檢查 index     //cmd buffer index check     if(index==5)       index=0; # 是否要 debug     if(debug==1)     {       Serial.print("CMD = ");       Serial.print(cmd[0]);       Serial.print(", ");       Serial.print("index =  ");       Serial.println(index);     }  # 第六百九十一行   if(cmd[0]==flow_read_cmd)   {     Wire.write(flow_val,3);     flow_val[0]=0;         cmd[0]=0;   } # 是否要 debug     if(debug==1)     {       Serial.print("DATA = ");       Serial.print(b[0]);       Serial.print(", ");       Serial.print(b[1]);       Serial.print(", ");       Serial.println(b[2]);     } } </pre>    <span style="color: #808080;"><strong>【 grovepi.py 補丁】</strong></span> <pre class="lang:python decode:true"># 第一百五十二行 flow_read_cmd=[12] flow_disable_cmd=[13] flow_en_cmd=[18]  # 增加啟閉 debug 指令 en_debug=[254] dis_debug=[255]  # 第五百五十八行 def flowRead():         write_i2c_block(address, flow_read_cmd + [unused, unused, unused])         time.sleep(.2)         data_back= bus.read_i2c_block_data(address, 1)[0:3]         #print data_back         if data_back[0]!=255:                 return [data_back[0],data_back[2]*256+data_back[1]]         else:                 return [-1,-1] # 增加開啟除錯 def debugEnable():         write_i2c_block(address, en_debug + [unused, unused, unused])         time.sleep(.2) # 增加關閉除錯 def debugDisable():         write_i2c_block(address, dis_debug + [unused, unused, unused])         time.sleep(.2) # </pre> ───     <span style="color: #666699;"><a style="color: #666699;" href="http://www.freesandal.org/?p=47985">W!o+ 的《小伶鼬工坊演義》︰ 一窺全豹之系統設計‧探索‧戊</a></span>  ……  <span style="color: #808000;">或許我們也應從善如流,一探春花春景的耶!!</span>  <span style="color: #808080;"><strong>【 dtoverlay=i2c-gpio 】</strong></span>  <span style="color: #808000;">使用 i2c-gpio 預設值︰ GPIO23 是 SDA , GPIO24 為 SCL ,串接回硬體 I2C : GPIO0 SDA , GPIO1 SCL 。因此樹莓派上有兩個 I2C Master 。</span>  <img class="alignnone size-full wp-image-48045" src="http://www.freesandal.org/wp-content/uploads/Bit-Banging-I2C.jpg" alt="Bit-Banging-I2C" width="1023" height="632" /> <pre class="lang:default decode:true ">pi@raspberrypi ~ ls /dev/i2c* -l
crw-rw---- 1 root i2c 89, 1  1月 29 14:52 /dev/i2c-1
crw-rw---- 1 root i2c 89, 3  1月 29 14:52 /dev/i2c-3
pi@raspberrypi ~ $ 

都可定址 GrovePi 也。

───

 

循著足跡

W!o+ 的《小伶鼬工坊演義》︰ 一窺全豹之系統設計‧探索‧庚 

,續踏征途的耶!!!