總網頁瀏覽量

2024年3月14日 星期四

一些PYTHON的疑難雜症(Some difficult and complicated diseases of Python)

在Python中,整數列表與整數列表相比較,即使它們的內容相同,也不會返回 True,因為它們是不同的物件。
例如:
unpacked_data = [10000, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99] 
expected_data = [10000, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99]
response_data = [10000, 100, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99]
因此,即使 unpacked_data 和 expected_data 的內容相同,它們也不會被判斷為相等。 
以下程式片斷:
if  unpacked_data == expected_data: 
     packed_data_to_send = struct.pack('16i', *response_data)
     client_socket.sendall(packed_data_to_send)
     print('Sent', repr(response_data))
else:
     client_socket.sendall(b'ERROR DATA')
     print('Sent ERROR DATA') 

總是會執行else: client_socket.sendall(b'ERROR DATA')。 
解決方法是將3個列表(list)定義為元組(tuple)。元組可以與另一個元組比較,如果元組中的元素相同,則會返回 True。這樣一來,就可以使用 == 來比較 unpacked_data 和 expected_data,而不需要任何進一步的處理。
把列表[ ]改為元組( )就可解決。
unpacked_data = (10000, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99) 
expected_data = (10000, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99) 
response_data = (10000, 100, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99) 

註(note): 
串列(List): 
ex:    list=[01, 23, 45, 67, 89 ] 
元組(Tuple): 
ex:    number = (1, 2, 3 ,4 ,5)

2023年10月24日 星期二

In a highly noisy environment, the noise is successfully filtered out to obtain the signal, resulting in an extremely high signal-to-noise ratio. The input signal is +/- 15 mv Sine wave, which passes through a 24-bit A/D converter and then undergoes signal processing to produce a noise-free Sine wave signal.
The following picture is the signal obtained after noise removal processing. The straight line is the position diagram for synchronously controlling the operation of the servo motor.

2022年3月6日 星期日

float to ascii code

sprintf() can be used to convert float to ascii. It's simple and only need the following several line code. Ex: char str[6]; float x=23.58; sprintf(str,"%f",x); According to the application,the ascii code can be transmitted to the output from uart interface.

2021年1月9日 星期六

了解馬達Back EMF原理

1. 這是一篇了解馬達Back EMF原理很好的文章,並可連結許多優秀資源 http://programmermagazine.github.io/201310/htm/science1.html
2. Back Emf 的原理請參考下列影片:
(1)Back Emf 動畫原理 (讚!): Commutators: Basics on AC and DC Generation http://www.youtube.com/watch?v=ATFqX2Cl3-w
(2)Back Emf 原理說明: (Back Emf of a DC Motor) http://www.youtube.com/watch?v=qwtiX3pFCIE
(3)Back Emf 量測實驗: ("Back EMF" Theory and Practice Paul Wesley Lewis) http://www.youtube.com/watch?v=VwDot8TMIqA

OnLine

counter