顯示具有 感測器 標籤的文章。 顯示所有文章
顯示具有 感測器 標籤的文章。 顯示所有文章

2025年9月4日 星期四

[Python硬體控制教學] 如何用 I2C 精準讀寫單一 Bit 或特定位元?

Quesion: 

在GPIO_I2C通訊協定與EEPROM存取"章節中,教的I2C存取是以1個byte(8個bit)為單位做讀及寫,請問有模組(module)或function可以import,只單獨做I2C 1個bit讀寫,或是連續的2或3個bit讀寫嗎?


  沒有這樣的函數, 原因是I2C 傳輸資料的最小的單位就是一個位元組 (8個bits),
  每傳輸完一個位元組,接收方就會回傳一個 ACK/NACK位元
   你要寫入bit, 一般作法都是 將值讀回後, 進行位元運算 bitwise operation, 再將值寫回
  例如:

    original_value = bus.read_byte_data(DEVICE_ADDR, CONFIG_REGISTER)
    new_value = original_value | (1 << 2)
    bus.write_byte_data(DEVICE_ADDR, CONFIG_REGISTER, new_value)

2020年10月5日 星期一

AI邊緣運算實作: TensorFlow Lite for MCU




AI邊緣運算


隨著物聯網與人工智慧發展,工作負載開始由雲端移轉至終端,AI也隨之進入到嵌入式系統及物聯網終端裝置中。在終端或所謂邊緣裝置這類的超低功耗微處理器上所實現的深度學習,被稱呼為微型深度學習。然而在MCU本身運算速度就不快,記憶體空間也有限的情況下,AI模型也不能太大,因此要能確保AI 效能,同時兼顧低功耗、成本與縮短開發時間,都是研發人員所必須面對的挑戰。

什麼是TensorFlow Lite

TensorFlow Lite for MCU正是專為邊緣裝置設計的TensorFlow模型預測框架,是TensorFlow的精簡版本,讓開發者可以在物聯網與嵌入式裝置中部署微型機器學習模型。

AI模型佈署於微控制器,需要那些知識 ?

首先,必須先知道TensorFlow Lite for MCU 在實作上會涉及Deep Learning 觀念及MCU開發技術,然而這是兩個截然不同的技術領域的人, 故要能有效整合出好的AI系統,必須能同時理解這兩邊的技術。簡而言之,你會需要對於Deep Learning 有一定的概念、還要有模型優化(如Quantization)、TensorFlow Lite的框架以及MCU程式開發的相關知識。技術不能說很難,但對於初學者,沒有人引導,是有困難度的,因為開發環境包含AI環境與MCU的BSP環境都不太熟悉的情況下,產生的錯誤常常搞不清楚問題在那 。

在板子跑AI和我在電腦跑AI有何差別?

在電腦上跑AI,對於輸入資料多半是.csv或圖片,以檔案方式存在。 然而在邊緣運算上, 輸入的資料通常是來自硬體的感測器,因此要能正確且即時的對資料預處理並且轉換成模型所須要的x輸入型狀(x input shape)才能代入模型。但資料抓取上要如何切成一個剛好完整的x 來進行推論呢? 例如聲音訊號是連續波, 不可能每次都剛好切出一段完整的聲音丟入模型,而得到很好的正確率。還有,像是感測器訊號也可能有雜訊產生,也會造成模型誤判。所以在系統上,需要有一個處理的機制來讓我們的AI系統達到穏定。

在板子上讀取硬體訊號,作為模型輸入的資料 $x$

板子上的感測器,可能像是G-Sensor 、Microphone 這類的。接著我們要讀出感測訊號並觀察訊號。務必確認感測訊號是正確的,因為不對的$x$, 就會是GIGO (Garbage In Garbage Out)。
然後對原始訊號做訊號處理 (即數據的前處理) ,獲得訊號的特徵向量 $x'$ 後 ,再餵入模型 $\hat{y}=f(x')$

在Sparkfun edge board 讀取G-Sensor X, Y,Z 的變化


在Sparkfun edge board 讀取Microphone 左右聲道變化


Tiny DL Demo
底下Demo 幾個AI邊緣運算分別在圖像、聲音、感測訊號的應用

2017年9月3日 星期日

PH 值感測器校正方法


1. 備好標準液 pH=7、pH=4 (或 pH=10)、蒸餾水及面紙
2. 用兩點的PH值產生斜率, PH Meter得以校正。





2017年9月1日 星期五

EC 感測器校正方式


EC感測器第一次都要校正過, 才能使用. 

感測器有其Domain Knowledge, 代表不同應用下的感測器其所需要學習的領域知識。

例如,  EC 感測器 (用來量測水中的電導度 (單位: ms/cm 或 us/cm) ) , 其校正方式如下

1. 購買原廠校正標準液(較建議,因為數值較精準)或自行配置標準 
的 氯化鉀 KCL溶液(1公升以上,量大比較精準),用於電導度計校正。

2.將電導度電極置於校正標準液內,測量數值是否吻合,若數值有差異
時,請找到電導度計校正數值設定參數,調整校正到正確值。

在25℃下,0.01 M KCl溶液導電性為1413μΩ-1/cm(單位即μS/cm)


參考資料:
http://104lab.blogspot.tw/2011/12/conductivity-controller-use-and.html

=======================
回憶高中化學: 

1) 氯化鉀 : 白色結晶或結晶性粉末,易溶於甘油,難溶於,不溶於丙酮


2) 莫耳濃度定義:以1升溶液中所含溶質莫耳數來表示溶液的濃度,稱為容積莫耳濃度,簡稱莫耳濃度,常以M表之,其單位為莫耳/升(mol/L 或M)

莫耳濃度(M) = (溶質重 / 溶質分子量) / 溶液體積(以公升L為單位)
又溶質莫耳數=溶質重 / 溶質分子量
18公克的葡萄糖(C6H1206)溶於100ml的水,莫耳濃度=?
(原子量:C=12,H=1,O=16)
葡萄糖分子量 = 12 x 6+1 x 12+16 x 6 = 180,體積100 ml=0.1 L,可得到莫耳濃度 = (18/180) / 0.1 = 1 M

2017年8月23日 星期三

PT100 溫度感測器



電阻系數

對於一般物體,電阻R,電阻率p、長度 L 與截面積A之間的關係如下:
在上式中,
  • 電阻 R單位為歐姆
  • 長度 L單位為米
  • 截面面積 A 單位為平方米
  • 電阻率 p 單位為歐姆·米
這個方程被稱為電阻定律。

由於 p 很小, 所以A 導線截面積要很小, 才會有R (電阻)可以量, 不然沒有R的訊號

電阻與溫度的關係

電阻率一般會隨溫度變化而變化。在溫度變化不大時,電阻率p與溫度之間存在線性關係:
pt=p0(1+aT)
其中 P0是該材料在0攝氏度時的電阻率,Pt是T攝氏度時的電阻率,a是電阻率的溫度係數。 a在溫度變化不大的範圍內可以認為是常量。多數金屬常溫下的電阻率溫度係數a 約為0.004。 

PT100


大部分電阻式溫度感測 器是以金屬作成的,其中以鉑(Pt)作成的電阻式溫度檢測器,最為穩定-耐酸堿、不會變 質、相當線性...,最受工業界採用。  

PT100 溫度感測器是一種以鉑(Pt) (俗稱白金)作成的電阻式溫度感測器,屬於正電阻係數,其電阻和 溫度變化的關係式如下:R=Ro(1+αT)     其中α=0.00392,Ro 為 100Ω (在 0℃的電阻值),T 為攝氏溫度,因此白金作成的電阻式溫度感測器,又稱為 PT100

PT100 溫度感測器 0℃時電阻值為 100Ω,電阻變化率為 0.3851Ω/℃。

pt100 的積體電路,要注意 的是這個積體電路採集的不是電流信號,而是電阻值。

PT100 原理及分度表 電阻式溫度感測器(RTD,Resistance Temperature Detector)-一種物質材料作成的電阻,它 會隨溫度的上升而改變電阻值,如果它隨溫度的上升而電阻值也跟著上升就稱為正電阻 係數,如果它隨溫度的上升而電阻值反而下降就稱為負電阻係數。

參考:

http://www.dmatek.com.tw/tn/download/SensorData_E390.pdf


2017年8月17日 星期四

嵌入式系統常用的通訊協定-RS485/Modbus



RS485:
  • 為半雙工(同一時間只能選擇傳送或接收的動作)
  • 為差動傳輸,使用纜線兩端(D+、D-)的電壓差來表示傳遞訊號
  • 常用於單機傳送(Master),多機接收(Slave)的通訊鏈結。可將數個感應裝置的數值回傳給Master
Modbus over RS485:
  • Modbus是一種串列通訊協定
  • Modbus為master/slave架構。一個裝置為Master節點,其他節點為slave。
  • 每一個slave裝置都有一個唯一的位址/ID (1~247)
  • Since a single byte is normally used to define the slave address and each slave on a network requires a unique address, the number of slaves on a network is limited to 256. The limit defined in the modbus specification is even lower at 247.
  • master和slave之間走的Modbus 協定其封包格式有分ASCII 或RTU兩種
  • ASCII 為文字模式, 用character 傳送, RTU 為binary 模式傳送
  • 封包格式:   Slave ID  | Function Code | Data | CRC
RTU 訊息格式
  • Information is stored in the Slave device in four different tables.
  • Two tables store on/off discrete values (coils) and two store numerical values (registers). The coils and registers each have a read-only table and read-write table.
  • Each table has 9999 values.
    Each coil or contact is 1 bit and assigned a data address between 0000 and 270E.
    Each register is 1 word = 16 bits = 2 bytes and also has data address between 0000 and 270E.

Coil/Register Numbers
Data Addresses
TypeTable Name
1-9999
0000 to 270E(9999)
Read-WriteDiscrete Output Coils
10001-19999
0000 to 270E
Read-OnlyDiscrete Input Contacts
30001-39999
0000 to 270ERead-OnlyAnalog Input Registers
40001-49999
0000 to 270ERead-WriteAnalog Output Holding Registers

Coil/Register Numbers can be thought of as location names since they do not appear in the actual messages. The Data Addresses are used in the messages.For example, the first Holding Register, number 40001, has the Data Address 0000. The difference between these two values is the offset.
Each table has a different offset. 1, 10001, 30001 and 40001.

只有Data Address(Offset)才出現在送出的Message 中, 如何知道查的是那一張表呢?
答案是利用Function Code 來決定


What is a function code?

The second byte sent by the Master is the Function code. This number tells the slave which table to access and whether to read from or write to the table.

Function Code (Command) 3 is to read 4xxx registers, and  4 for 3xxx registers. 

Function CodeActionTable Name
01 (01 hex)ReadDiscrete Output Coils
05 (05 hex)Write singleDiscrete Output Coil
15 (0F hex)Write multipleDiscrete Output Coils
02 (02 hex)ReadDiscrete Input Contacts
04 (04 hex)ReadAnalog Input Registers
03 (03 hex)ReadAnalog Output Holding Registers
06 (06 hex)Write singleAnalog Output Holding Register
16 (10 hex)Write multipleAnalog Output Holding Registers

Example: 


Read Holding Registers (FC=03)
Request
This command is requesting the content of analog output holding registers # 40108 to
 40110 from the slave device with address 17.
11 03 006B 0003 7687
11: The Slave Address (11 hex = address17 )
03: The Function Code 3 (read Analog Output Holding Registers)
006B: The Data Address of the first register requested.
             ( 006B hex = 107 , + 40001 offset = input #40108 )
0003: The total number of registers requested. (read 3 registers 40108 to 40110)
7687: The CRC (cyclic redundancy check) for error checking.
Response
11 03 06 AE41 5652 4340 49AD
11: The Slave Address (11 hex = address17 )
03: The Function Code 3 (read Analog Output Holding Registers)
06: The number of data bytes to follow (3 registers x 2 bytes each = 6 bytes)
AE41: The contents of register 40108
5652: The contents of register 40109
4340: The contents of register 40110
49AD: The CRC (cyclic redundancy check).


Read Coil Status (FC=01)

Request
This command is requesting the ON/OFF status of discrete coils # 20 to 56
from the slave device with address 17.
11 01 0013 0025 0E84

11: The Slave Address (11 hex = address17 )
01: The Function Code 1 (read Coil Status)
0013: The Data Address of the first coil to read.
             ( 0013 hex = 19 , + 1 offset = coil #20 )
0025: The total number of coils requested.  (25 hex = 37,  inputs 20 to 56 )
0E84: The CRC (cyclic redundancy check) for error checking.
Response
11 01 05 CD6BB20E1B 45E6
11: The Slave Address (11 hex = address17 )
01: The Function Code 1 (read Coil Status)
05: The number of data bytes to follow (37 Coils / 8 bits per byte = 5 bytes)
CD: Coils 27 - 20 (1100 1101)
6B: Coils 35 - 28 (0110 1011)
B2: Coils 43 - 36 (1011 0010)
0E: Coils 51 - 44 (0000 1110)
1B: 3 space holders & Coils 56 - 52 (0001 1011)
45E6: The CRC (cyclic redundancy check).
The more significant bits contain the higher coil variables. This shows that coil 36 is off (0) and 43 is on (1). Due to the number of coils requested, the last data field1Bcontains the status of only 5 coils.  The three most significant bits in this data field are filled in with zeroes.





RS485 模組 (MAX485)的Pin 腳定義:


  • pin 1 :RO (receive out)
  • pin 2 RE (receive enable) 
  • pin 3 :DE (data enable) 
  • pin 4 :DI (data in)
  • pin 5, pin 8 :  Gnd  and Vcc onnected
  • pin 6,7 : A and B  the RS485 pair

RO------->UART TX
DI<------- UART RX

把/RE and DE 短路, 然後MCU/Pi 用一根GPIO 決定 /RE 或 DE.(即是決定是在送還是在收的狀態, 因為RS485是單工)















讀出來若是以Float 表示, 則需進行轉換成十進制

須考慮 Byte ordering的問題!

http://www.61131.com/download.htm






 AQUAS-SR04











相關文章: