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

壬一

壬二

壬三

《説文解字》:壬,位北方也。陰極陽生 ,故《易》曰:“龍戰于野。”戰者,接也 。象人褢妊之形。承亥壬以子,生之敘也 。與巫同意。壬承辛,象人脛。脛,任體也。凡壬之屬皆从壬。

本義:善於使用巧具,勝任事務。

論語》‧泰伯第八

曾子曰:士不可以不弘毅任重道遠,仁以為己任,不亦重乎!死而後已,不亦遠乎!

曾子曰:以能問於不能,以多問於寡;有若無,實若虛,犯而不校;昔者吾友【顏淵】,嘗從事於斯矣。

……

無論人們喜歡與否,我們已經活在了『大數據』的『礦坑』之中,我們本身就是『資訊礦石』。舉例來說,許多便利商店裝設著資訊收集、分析、儲存……的 『攝像鏡頭』,用著『影像辨識』的科技追蹤『消費者』之『視線』,分析『消費者』的性別、年齡、喜好 ……等等數據,可用於廣告、促銷、商品管理……種種用途,企圖達到『別家』或『門可羅雀』,此處卻『人滿為患』的成效。那麼這是『合理』的嗎?應該『同 意』的嗎??想像未來當你進入一個『賣場』,你的『手機』就已經告訴你今天有哪些你『喜歡』的『食品』正在『特價』,挑動著你的『感性欲望』;或許由於其 它非食品業 APP 軟體廠商的大力『宣傳』,你的『手機』上也有那種分析『食品』對『身體健康』影響的 APP,它正提醒你的『理性』再吃就『超標』了……這難道不是…資訊轟炸……更像是……擺脫不掉,自己找來的麻煩!!

也許那還是自己可以選擇之『好』的一面。因為不是所有人利用『大數據』來『淘金』,都具有『專業訓練』以及『道德良心』。因此常有『□□ 研究』的『數據』『證實』吃『○○食品』能降低『☆☆指數』如何?如之何的『廣告』??一 旦發生了『事故』,大概全成了那是『個案』以及『例外』的了。就像『風起雲湧』,天未必會下雨,『艷陽高照』,未必天就不下雨。比方說『徵候』是講時間 『先行』的『現象』,與伴隨該『現象』之後『而來』的『現象』,『現象』之間可能會有『統計相關性』,但不一定具有什麼『因果關係』。或許可以讀一讀 《知未知‧既未濟》所言︰

………

─── 摘自《《派生》 Python 作坊【壬】任重道遠

 

身在雲端大數據的今天,資訊駁雜巨量,或許需要掌握的工具不只是『搜尋谷歌』而已,更須具有『慎思明辨』的能力吧!

為了了解超音波感測器的運作原理

grove_pi_v1_2_5.ino

# line 125
 //Ultrasonic Read
else if(cmd[0]==7)
{
   pin=cmd[1];
   pinMode(pin, OUTPUT);
   digitalWrite(pin, LOW);
   delayMicroseconds(2);
   digitalWrite(pin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pin,LOW);
   pinMode(pin,INPUT);
   dur = pulseIn(pin,HIGH);
   RangeCm = dur/29/2;
   b[1]=RangeCm/256;
   b[2]=RangeCm%256;
   //Serial.println(b[1]);
   //Serial.println(b[2]);
}

 

探究問題所指

API of grovepi.read_i2c_block is unsafe and chashes grovepi.ultrasonicRead #141

read_i2c_block normally returns a list but in case of an IOError it returns -1 (int). This is extremely difficult to handle as one would had to test the return value of read_i2c_block.

One would have to use something like this:

number = read_i2c_block(address)
if type(number) is list:
     do something_usefull()
else:
  handle_error()

which is the same thing as catching the exception.
ultrasonicRead and all the other function I saw in grovepi.py use the function in unsafe manor:

def ultrasonicRead(pin):
        write_i2c_block(address, uRead_cmd + [pin, unused, unused])
        time.sleep(.2)
        read_i2c_byte(address)
        number = read_i2c_block(address)
        return (number[1] * 256 + number[2])

Which leads to an TypeError and the program to crash as one is not expecting an TypeError.

This also happens in the wild:
http://www.dexterindustries.com/forum/?topic=ultrasonic-ranger-1-0-broken/

It would be much clearer to just raise the IOError as this is what is happening as to convert it in an TypeError.

I have not checked all the other i2c routines maybe there are more problems like this.

Edit: Typo

───

 

作者一路追跡 □○ ,經過 Grove WiKi 的田野,

Grove – Ultrasonic Ranger

中文 This Grove – Ultrasonic sensor is a non-contact distance measurement module which works at 42KHz, suitable for projects that require middle distance measurement.

350px-Ultrasonic_Ranger

Specification

Parameter Value/Range
Operating voltage 3.3~5V
Operating current 15mA
Ultrasonic frequency 42kHz
Measuring range 3-400cm
Resolution 1cm
Output PWM

───

 

尋至 Cooper Maa 的家園,

Cooper Maa
一家烤肉萬家香,開放分享才會快樂

2012年9月17日 星期一

HC-SR04 超音波感測器介紹

2012-09-16_23h53_28

超音波感測器(老外好像把它叫作 PING))) sensor)是由超音波發射器、接收器和控制電路所組成。當它被觸發的時候,會發射一連串 40 kHz 的聲波並且從離它最近的物體接收回音。超音波是人類耳朵無法聽見的聲音,因為它的頻率很高。

如下圖所示,超音波測量距離的方法,是測量聲音在感測器與物體之間往返經過的時間:

image記 得國中理化有教過,聲音在空氣中的傳播速度大約是每秒 340 公尺,傳播速度會受溫度影響,溫度愈高,傳播速度愈快。假設以 340 公尺計算,1000000 / 340 * 100 = 29.4 microseconds,四捨五入後,可知聲音傳播 1 公分所需的時間為 29 microseconds (百萬分之一秒)。

由於超音波從發射到返迴是兩段距離,因此在計算時必須將結果除以 2 才是正確的物體距離。所以我們可以利用底下的公式算出物體距離(距離單位為公分,其中 timing 是測量得到的音波傳播時間):

timing / 29 / 2

超音波感測器主要應用在機器人或自走車避障、物體測距等。

───

 

終抵超音波感測器的故鄉︰

HC-SR04-Timing

───

 

在先進的光照之下︰

Three Ways To Read A PWM Signal With Arduino

PWM (Pulse-Width Modulation) is a modulation technique that controls the width of the pulse based on modulator signal information. PWM can be used to encode information for transmission or to control of the power supplied to electrical devices such as motors.

Generating a PWM signal with an Arduino is quite easy. There is significantly less documentation on how best to read a PWM signal. I needed to read the receiver signals for a remote controlled Quadcopter and after doing some research, I discovered three methods of reading a PWM signal with an Arduino.

The Gist

PWM works by varying the width of the on signal (read Duty Cycle) within a fixed signal frequency or period of time. So what we are really looking for is the length of time the signal remains high for each cycle. There are several ways to do this. The easiest is using the pulseIn function as shown below.

1. The pulseIn() Function

The pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds.

The pulseIn Function
byte PWM_PIN = 3;
 
int pwm_value;
 
void setup() {
  pinMode(PWM_PIN, INPUT);
  Serial.begin(115200);
}
 
void loop() {
  pwm_value = pulseIn(PWM_PIN, HIGH);
  Serial.println(pwm_value);
}

2. External Interrupts

The pulseIn function works well and is really simple. However, the downside is that the processor cannot be used while it is waiting for the pin to go low. This is not a very efficient use of our CPU cycles. We can improve this by using an event-driven interrupt system to handle the measurement of the PWM signal. Arduino provides the attachInterrupt function to do just this.

───

 

明白 pulseIn() 可能引發的麻煩。發現答案或將透過

GreyGnome/PinChangeInt
—- README ——————————————————————–
PinChangeInt README. Find instructions and more information at
https://github.com/GreyGnome/PinChangeInt/wiki
NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
This library is deprecated as of April 3, 2015. This means that I will be
providing bug fixes for some time, but users are encouraged to migrate to the EnableInterrupt library, at https://github.com/GreyGnome/EnableInterrupt .
It is faster and easier to use. Thank you.
—- DESCRIPTION —————————————————————
This is the PinChangeInt library for the Arduino. It provides an extension to the interrupt support for ATmega328 and ATmega2560-based Arduinos, and some ATmega32u4 and Sanguinos. It adds pin change interrupts, giving a way for users
to have interrupts drive off of any pin (ATmega328-based Arduinos), by the Port B, J, and K pins on the Arduino Mega and its ilk, and on the appropriate ports (including Port A) on the Sanguino and its ilk. Yun and Sanguino support are weak, quite honestly, as I don’t have either a Sanguino or a Yun. Theoretically the library would work with a Leonardo but I have no reports regarding that platform. The ATmega32u4 has port B (8 pins) pin change interrupts only.

───

 

落在

GreyGnome/EnableInterrupt

EnableInterrupt

New Arduino interrupt library, designed for all versions of the Arduino. NEW: ATtiny support! ATtiny 44/84 and 45/85 supported!

Functions:

enableInterrupt- Enables interrupt on a selected Arduino pin.
disableInterrupt - Disables interrupt on the selected Arduino pin.

 

*What’s New? Fri Nov 6 19:13:20 CST 2015 Version 0.9.4 of the library has been released. Thanks to RobertClemenzi, who pointed out 2 bugs: 1. A spurious comma in some array definitions caused a compiler warning, and 2. More importantly, it was impossible to use arduinoInterruptedPin if this library was used to support other libraries.

IMPORTANT NOTE: In 0.9.2 I discovered a rather pernicious bug, wherein the library was setting the global interrupt enable bit. This could cause a serious and difficult-to-debug race condition, as it is not the job of the library to manage that bit. The chips come with interrupts enabled so existing code should not be affected, but if you were relying on that behavior note that it has changed. My thanks to http://gammon.com.au/interrupts (the ‘How are interrupts queued?’ section).

The EnableInterrupt library is an Arduino interrupt library, designed for all versions of the Arduino- at this writing, the Uno (and other ATmega328p-based boards, like the mini), Due, Zero, Leonardo (and other ATmega32u4-based boards, like the Micro), the Mega2560 (and other ATmega2560-based boards, like the MegaADK), and for non-Arduino chips: the 644/1284p (using the Mighty1284 for support files and pin numbering), ATtiny 44/84, and ATtiny 45/85 (using DA Mellis’ support files). The library enables you to assign an interrupt to pins on your chip that support them, and presents a common interface to all supported chips. This means that on the Arduino Uno and Mega you don’t give it an interrupt number, as per http://arduino.cc/en/Reference/attachInterrupt. Rather, your first argument is a pin number of a pin that’s supported on that chip (see https://github.com/GreyGnome/EnableInterrupt/wiki/Usage#pin–port-bestiary ).

───

 

的耶!!??