(2022/12/08)
一、教學資料
官網:https://www.yahboom.com/study/Pico_Robot (提取 (碼:eggd) mmmmmmmmmjj
開發板為Raspberry Pi Pico W (簡稱Pico W)
Raspberry Pi Pico W 式款低成本且高靈活應用的 RP2040 開發平台,此外還有2.4GHz 無線接口和以下主要功能:
內建 2MB 外部閃存的 RP2040 微控制器晶片
2.4GHz 無線 (802.11n) 介開發板載
用於電源、數據與對閃存重新編程的微型 USB 接口
帶有 0.1 英寸通孔針的 40 針 21mm×51mm 'DIP' 型 1mm 厚的邊緣齒型 PCB
外露 26 個多功能 3.3V 通用 I/O (GPIO)
有 23 個僅適用於數字與另外 3 個支援 ADC 的 GPIO
可作為模組進行表面貼裝
使用Thonny來寫micropython程式
Thonny官網:https://thonny.org/
更新Pico W韌體
參考之前寫的入門文章 https://sites.google.com/site/wenyunotify/08-%E6%A8%B9%E8%8E%93%E6%B4%BE/01-pico%E5%85%A5%E9%96%80
點擊【工具】/【選項】/【直譯器】,如下圖
寫入小車用的程式庫到Pico W
先開啟pico_car.py
再另存到載板
二、基礎課程 (取自亞博官網)
控制板載LED燈閃爍
說明:
與最初的 Raspberry Pi Pico 不同,Pico W 上的板載 LED 沒有連接到 RP2040 上的引腳,而是連接到無線芯片上的 GPIO 引腳。
import machine 機器庫包含MicroPython需要與Pico和其他MicroPython相容的設備通信的所有指令,擴展了物理計算的語言。
import time “time”庫。 這個庫處理所有與時間有關的事情,從測量它到將延遲插入到程式中。單位為秒。
Pico的程式 (內建LED燈在GPIO 25)
import machine
import time
led_onboard = machine.Pin(25, machine.Pin.OUT)
while True:
led_onboard.value(1)
time.sleep(1)
led_onboard.value(0)
time.sleep(1)
Pico W的程式 (要改用"LED")
import machine
import time
led_onboard = machine.Pin("LED", machine.Pin.OUT)
while True:
led_onboard.value(1)
time.sleep(1)
led_onboard.value(0)
time.sleep(1)
更簡單的寫法
from machine import Pin
led = Pin("LED", Pin.OUT)
led.on() #開燈
led.off() #關燈
板載溫度感測器
說明:
一個ADC有兩個關鍵的特點:它的解析度,以數位位元測量,它的通道,或它可以接受和轉換多少模 擬信號一次。 您的PICO中的ADC的解析度為12位,這意味著它可以將類比信號轉換為數位信號,其數 字從0到4095不等-儘管這是在MicroPython中處理的,轉換為從0到65,535的16位元數字,因此它的行 為與其他MicroPython微控制器上的ADC相同。 它有三個通道被帶到GPIO引腳:GP26、GP27和 GP28,它們也被稱為類比頻道0、1和2的GP26_ADC0、GP27_ADC1和GP28_ADC2。 還有第四個ADC 通道,它連接到一個內置在RP2040中的溫度感測器。
程式:
import machine
import time
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
while True:
reading = sensor_temp.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706)/0.001721
print(temperature)
time.sleep(2)
無源蜂鳴器
說明:
無源蜂鳴器利用電磁感應現象,為音圈接入交變電流後形成的電磁鐵與永磁鐵相吸或相斥而推動振膜發聲,接入直流電只能持續推動振膜而無法產生聲音,在控制中我們一般使用PWM來控制無源蜂鳴器發出聲音。
將IO22設置為PWM輸出引腳,用於控制蜂鳴器。
程式:
from machine import Pin, PWM
import time
# set buzzer pin
BZ = PWM(Pin(22))
BZ.freq(1000)
# Initialize music
CM = [0, 330, 350, 393, 441, 495, 556, 624]
song = [CM[1],CM[1],CM[5],CM[5],CM[6],CM[6],CM[5],CM[4],CM[4],CM[3],CM[3],CM[2],CM[2],CM[1],]
beat = [ 0.5,0.5,0.5,0.5,0.5,0.5,1,0.5,0.5,0.5,0.5,0.5,0.5,1,]
# music
def music():
print('Playing song ...')
for i in range(len(song)):
BZ.duty_u16(500)
BZ.freq(song[i])
time.sleep(beat[i])
BZ.duty_u16(0)
time.sleep(0.01)
# play music
music()
print("Ending")
呼吸燈
說明:
小車板載8顆可程式設計RGB燈,可以實現炫彩燈效,8顆可程式設計燈內置ws2812晶片,只需要一個埠 通過時序控制即可同時控制8個燈,時序控制函數封裝在庫中,我們只需要調用設置燈的顏色即可。
ws2812晶片所用腳位 IO6
程式:
import time
from pico_car import ws2812b
num_leds = 8 # Number of NeoPixels
# Pin where NeoPixels are connected
pixels = ws2812b(num_leds, 0)
# Set all led off
pixels.fill(0,0,0)
pixels.show()
# Define variables
i = 0
brightness = 0
fadeAmount = 1
# Breathing
while True:
for i in range(num_leds):
pixels.set_pixel(i,0,brightness,brightness)
pixels.show()
brightness = brightness + fadeAmount
if brightness <= 0 or brightness >= 200:
fadeAmount = -fadeAmount
time.sleep(0.005)
跑馬燈
程式
import time
from pico_car import ws2812b
num_leds = 8 # Number of NeoPixels
# Pin where NeoPixels are connected
pixels = ws2812b(num_leds, 0)
# Set all led
pixels.fill(10,10,10)
pixels.show()
# horse race lamp
while True:
for i in range(num_leds):
for j in range(num_leds):
#pixel_num, red, green, blue
pixels.set_pixel(j,abs(i+j)%10,abs(i-(j+3))%10,abs(i-(j+6))%10)
pixels.show()
time.sleep(0.05)
OLED顯示
說明:
OLED顯示幕是利用有機電自發光二極體製成的顯示幕。由於同時具備自發光有機電激發光二極 管,不需背光源、對比度高、厚度薄、視角廣、反應速度快、使用溫度範圍廣、構造及制程較簡單等優異之特性。我們使用的0.91寸OLED屏使用IIC通信,節約了IO引腳,簡化了控制方式。
OLED所用腳位 I2C(scl=Pin(15),sda=Pin(14)),解析度:128*32
程式: (顯示Hello World 一點)
from machine import Pin, I2C
from pico_car import SSD1306_I2C
import time
# set IIC pin
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
# initialization oled
oled = SSD1306_I2C(128, 32, i2c)
# oled show hello at 0,0
oled.text('Hello', 0, 0)
oled.show()
oled.fill(0)
time.sleep(1)
# oled show World at 0,10
oled.text('World', 0, 10)
oled.show()
oled.fill(0)
time.sleep(1)
# oled show spot at 100,30
oled.pixel(100, 30, 1)
oled.show()
oled.fill(0)
time.sleep(1)
電池電量顯示
說明:
電量檢測的硬體基本原理是通過電阻把電池電壓調整到合適的數值,輸入給主控,主控通過ADC去檢測電壓從而判斷電量。
ADC端口28
程式:
from machine import Pin, I2C, ADC
from pico_car import SSD1306_I2C
import time
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
#initialization ADC
Quantity_of_electricity = machine.ADC(28)
while True:
#Display power on OLED
#Under 20000, there is no power at all
oled.text('Battery:', 0, 0)
oled.text(str(Quantity_of_electricity.read_u16()), 65, 0)
oled.show()
oled.fill(0)
time.sleep(0.1)
三、傳感器課程(取自亞博官網)
光敏電阻
說明:
光敏電阻是用硫化鎘或硒化鎘等半導體材料製成的特殊電阻器,其工作原理是基於內光電效應。光 照愈強,阻值就愈低,隨著光照強度的升高,電阻值迅速降低。光敏電阻器對光的敏感性(即光譜特 性)與人眼對可見光(0.4~0.76)μm的回應很接近,只要人眼可感受的光,都會引起它的阻值變化。
光敏電阻腳位:#Light1 -> GP27、#Light2 -> GP26
jumper要調到Light
程式
from pico_car import ds, SSD1306_I2C
from machine import Pin, I2C, ADC
import time
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
#Light1 -> GP27
#Light2 -> GP26
light1 = machine.ADC(27)
light2 = machine.ADC(26)
while True:
#get value
LightS1 = light1.read_u16()
LightS2 = light2.read_u16()
print("light1 is %d"%(LightS1) )
print("light2 is %d"%(LightS2) )
#Display sound on OLED
oled.text('Light1:', 0, 0)
oled.text(str(LightS1), 60, 0)
oled.text('Light2:', 0, 10)
oled.text(str(LightS2), 60, 10)
oled.show()
oled.fill(0)
time.sleep(0.5)
呈現結果:
一般亮度時:約17000~20000
遮光時:約60000左右
聲音感測器
說明:
感測器內置一個對聲音敏感的電容式駐極體話筒,聲波使話筒內的駐極體薄膜振動,導致電容的變 化,而產生與之對應變化的微小電壓,這一電壓通過運算放大轉化成合適的電壓,並傳送給PICO開發 板,因為沒有檢測噪音的能力,使用時儘量在較安靜的環境中。
聲音感測器:GP27
jumper要調到Voice
程式:
from pico_car import SSD1306_I2C
from machine import Pin, I2C, ADC
import time
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
#initialization ADC
Sound = machine.ADC(27)
while True:
#get value
sounds = Sound.read_u16()
print(sounds)
oled.text('Sound:', 0, 0)
oled.text(str(sounds), 50, 0)
#Display sound on OLED
for i in range(10):
oled.pixel(i, 30, 1)
oled.pixel(i, 29, 1)
if sounds > 5000:
for i in range(10):
for j in range(4):
oled.pixel(i+10, 27+j, 1)
if sounds > 10000:
for i in range(10):
for j in range(10):
oled.pixel(i+20, 21+j, 1)
if sounds > 20000:
for i in range(10):
for j in range(20):
oled.pixel(i+30, 11+j, 1)
oled.show()
oled.fill(0)
time.sleep(0.1)
巡線感測器
說明:
巡線感測器是利用紅外線來進行資料處理的一種感測器,有靈敏度高等優點,感測器上有一個紅外 發射管和一個紅外接收管,當地面為黑色,吸收所有光, 接收管電阻增大,當地面為白色,反射所有光, 接收管電阻減小,再通過板上的電壓比較電路,把檢測到的狀態變成0/1的數值。
巡線感測器腳位:2、3、4、5
程式:
from machine import Pin, I2C
from pico_car import SSD1306_I2C
import time
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
#Define the tracking sensor, 1-4 from left to right
#recognize that black is 0 and white is 1
#Tracing_1 Tracing_2 Tracing_3 Tracing_4
# 2 3 4 5
Tracing_1 = machine.Pin(2, machine.Pin.IN)
Tracing_2 = machine.Pin(3, machine.Pin.IN)
Tracing_3 = machine.Pin(4, machine.Pin.IN)
Tracing_4 = machine.Pin(5, machine.Pin.IN)
while True:
oled.text('T1', 5, 0)
oled.text('T2', 35, 0)
oled.text('T3', 65, 0)
oled.text('T4', 95, 0)
print("T1: %d T2: %d T3: %d T4: %d "%(Tracing_1.value(),Tracing_2.value(),Tracing_3.value(),Tracing_4.value()))
# Tracing1 display
if Tracing_1.value() == 1:
oled.text('1', 9, 10)
for i in range(10):
for j in range(10):
oled.pixel(i+8, 20+j, 1)
elif Tracing_1.value() == 0:
oled.text('0', 9, 10)
for i in range(10):
oled.pixel(i+8, 20, 1)
oled.pixel(i+8, 29, 1)
for j in range(8):
oled.pixel(8, 21+j, 1)
for j in range(8):
oled.pixel(17, 21+j, 1)
# Tracing2 display
if Tracing_2.value() == 1:
oled.text('1', 39, 10)
for i in range(10):
for j in range(10):
oled.pixel(i+38, 20+j, 1)
elif Tracing_2.value() == 0:
oled.text('0', 39, 10)
for i in range(10):
oled.pixel(i+38, 20, 1)
oled.pixel(i+38, 29, 1)
for j in range(8):
oled.pixel(38, 21+j, 1)
for j in range(8):
oled.pixel(47, 21+j, 1)
# Tracing3 display
if Tracing_3.value() == 1:
oled.text('1', 69, 10)
for i in range(10):
for j in range(10):
oled.pixel(i+68, 20+j, 1)
elif Tracing_3.value() == 0:
oled.text('0', 69, 10)
for i in range(10):
oled.pixel(i+68, 20, 1)
oled.pixel(i+68, 29, 1)
for j in range(8):
oled.pixel(68, 21+j, 1)
for j in range(8):
oled.pixel(77, 21+j, 1)
# Tracing4 display
if Tracing_4.value() == 1:
oled.text('1', 99, 10)
for i in range(10):
for j in range(10):
oled.pixel(i+98, 20+j, 1)
elif Tracing_4.value() == 0:
oled.text('0', 99, 10)
for i in range(10):
oled.pixel(i+98, 20, 1)
oled.pixel(i+98, 29, 1)
for j in range(8):
oled.pixel(98, 21+j, 1)
for j in range(8):
oled.pixel(107, 21+j, 1)
oled.show()
oled.fill(0)
time.sleep(0.1)
呈現結果:
在白線:其值為1,車上感應燈滅
在黑線:其值為0,車上感應燈亮
超聲波感測器
說明:
超聲波感測器腳位:Trig = Pin(0, Pin.OUT)、Echo = Pin(1, Pin.IN)
程式:
import time
from machine import Pin, I2C
from pico_car import SSD1306_I2C, ultrasonic
#initialization ultrasonic
ultrasonic = ultrasonic()
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
while True:
#get distance
distance = ultrasonic.Distance_accurate()
print("distance is %d cm"%(distance) )
#display distance
oled.text('distance:', 0, 0)
oled.text(str(distance), 75, 0)
oled.show()
oled.fill(0)
time.sleep(1)
呈現結果:
單位為公分
當超出範圍(約 >350cm) 呈現999
紅外遙控顯示
說明:
紅外線的光譜位於紅色光之外, 波長是0.76~1.5μm,比紅光的波長還長。紅外遙控是利用紅外線 進行傳遞資訊的一種控制方式,紅外遙控具有抗干擾,電路簡單,容易編碼和解碼,功耗小,成本低的 優點。紅外遙控幾乎適用所有家電的控制。紅外接收頭中內置了光電元件,可以接收到對應波長的紅外 光,轉換成數位信號,通過讀取信號數值,判斷不同的遙控按鍵。
紅外線接收器腳位:IO7
程式:
import time
from machine import Pin, I2C
from pico_car import SSD1306_I2C, ir
#initialization ir
Ir = ir()
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
while True:
#get value
value = Ir.Getir()
time.sleep(0.01)
if value != None:
print(value)
#display press
if value == 0:
while value == 0:
value = Ir.Getir()
oled.text('Press:Power', 0, 0)
oled.show()
oled.fill(0)
elif value == 1:
while value == 1:
value = Ir.Getir()
oled.text('Press:Up', 0, 0)
oled.show()
oled.fill(0)
elif value == 2:
while value == 2:
value = Ir.Getir()
oled.text('Press:Light', 0, 0)
oled.show()
oled.fill(0)
elif value == 4:
while value == 4:
value = Ir.Getir()
oled.text('Press:Left', 0, 0)
oled.show()
oled.fill(0)
elif value == 5:
while value == 5:
value = Ir.Getir()
oled.text('Press:Sound', 0, 0)
oled.show()
oled.fill(0)
elif value == 6:
while value == 6:
value = Ir.Getir()
oled.text('Press:Right', 0, 0)
oled.show()
oled.fill(0)
elif value == 8:
while value == 8:
value = Ir.Getir()
oled.text('Press:Turn Left', 0, 0)
oled.show()
oled.fill(0)
elif value == 9:
while value == 9:
value = Ir.Getir()
oled.text('Press:Down', 0, 0)
oled.show()
oled.fill(0)
elif value == 10:
while value == 10:
value = Ir.Getir()
oled.text('Press:Turn Right', 0, 0)
oled.show()
oled.fill(0)
elif value == 12:
while value == 12:
value = Ir.Getir()
oled.text('Press:+', 0, 0)
oled.show()
oled.fill(0)
elif value == 13:
while value == 13:
value = Ir.Getir()
oled.text('Press:0', 0, 0)
oled.show()
oled.fill(0)
elif value == 14:
while value == 14:
value = Ir.Getir()
oled.text('Press:-', 0, 0)
oled.show()
oled.fill(0)
elif value == 16:
while value == 16:
value = Ir.Getir()
oled.text('Press:1', 0, 0)
oled.show()
oled.fill(0)
elif value == 17:
while value == 17:
value = Ir.Getir()
oled.text('Press:2', 0, 0)
oled.show()
oled.fill(0)
elif value == 18:
while value == 18:
value = Ir.Getir()
oled.text('Press:3', 0, 0)
oled.show()
oled.fill(0)
elif value == 20:
while value == 20:
value = Ir.Getir()
oled.text('Press:4', 0, 0)
oled.show()
oled.fill(0)
elif value == 21:
while value == 21:
value = Ir.Getir()
oled.text('Press:5', 0, 0)
oled.show()
oled.fill(0)
elif value == 22:
while value == 22:
value = Ir.Getir()
oled.text('Press:6', 0, 0)
oled.show()
oled.fill(0)
elif value == 24:
while value == 24:
value = Ir.Getir()
oled.text('Press:7', 0, 0)
oled.show()
oled.fill(0)
elif value == 25:
while value == 25:
value = Ir.Getir()
oled.text('Press:8', 0, 0)
oled.show()
oled.fill(0)
elif value == 26:
while value == 26:
value = Ir.Getir()
oled.text('Press:9', 0, 0)
oled.show()
oled.fill(0)
value = None
四、小車課程(取自亞博官網)
小車前進
說明:
在小車的擴展板上我們集成了電機驅動電路,只需要使用PWM即可控制電機方向和轉速,通過調整 PWM的占空比,高電平時間越長電機速度越快。在電機內部,通過線圈將電流變成磁場,在磁鐵的作用下實現電機的轉動。
程式
from pico_car import pico_car
import time
Motor = pico_car()
#Car forward,parameter(Left motor speed,Right motor speed),speed 0-255
Motor.Car_Run(255,255)
time.sleep(1)
#Car stop
Motor.Car_Stop()
小車花式動作
程式
from pico_car import pico_car
import time
Motor = pico_car()
#Car forward,parameter(Left motor speed,Right motor speed),speed 0-255
Motor.Car_Run(255,255)
time.sleep(1)
#Car back
Motor.Car_Back(255,255)
time.sleep(1)
#left
Motor.Car_Run(0,255)
time.sleep(1)
#right
Motor.Car_Run(255,0)
time.sleep(1)
#Turn left
Motor.Car_Left(255,255)
time.sleep(1)
#Turn right
Motor.Car_Right(255,255)
time.sleep(1)
#Car stop
Motor.Car_Stop()
小車唱歌跳舞
程式
from pico_car import pico_car, ws2812b
import time
from machine import Pin, PWM
Motor = pico_car()
# set buzzer pin
BZ = PWM(Pin(22))
BZ.freq(1000)
num_leds = 8 # Number of NeoPixels
# Pin where NeoPixels are connected
pixels = ws2812b(num_leds, 0)
# Set all led off
pixels.fill(0,0,0)
pixels.show()
# Initialize music
CM = [0, 330, 350, 393, 441, 495, 556, 624]
song = [CM[1],CM[1],CM[5],CM[5],CM[6],CM[6],CM[5],
CM[4],CM[4],CM[3],CM[3],CM[2],CM[2],CM[1],]
beat = [ 0.5,0.5,0.5,0.5,0.5,0.5,1,0.5,0.5,0.5,0.5,0.5,0.5,1,]
# music
def music_Run():
for i in range(0,2):
BZ.duty_u16(500)
BZ.freq(song[i])
time.sleep(beat[i])
BZ.duty_u16(0)
time.sleep(0.01)
def music_Back():
for i in range(2,4):
BZ.duty_u16(500)
BZ.freq(song[i])
time.sleep(beat[i])
BZ.duty_u16(0)
time.sleep(0.01)
def music_Left():
for i in range(4,7):
BZ.duty_u16(500)
BZ.freq(song[i])
time.sleep(beat[i])
BZ.duty_u16(0)
time.sleep(0.01)
def music_Right():
for i in range(7,9):
BZ.duty_u16(500)
BZ.freq(song[i])
time.sleep(beat[i])
BZ.duty_u16(0)
time.sleep(0.01)
def music_TLeft():
for i in range(9,11):
BZ.duty_u16(500)
BZ.freq(song[i])
time.sleep(beat[i])
BZ.duty_u16(0)
time.sleep(0.01)
def music_TRight():
for i in range(11,14):
BZ.duty_u16(500)
BZ.freq(song[i])
time.sleep(beat[i])
BZ.duty_u16(0)
time.sleep(0.01)
#Car forward
Motor.Car_Run(255,255)
for i in range(num_leds):
pixels.set_pixel(i,255,255,0)
pixels.show()
music_Run()
#Car back
Motor.Car_Back(255,255)
for i in range(num_leds):
pixels.set_pixel(i,0,255,255)
pixels.show()
music_Back()
#left
Motor.Car_Run(0,255)
for i in range(num_leds):
pixels.set_pixel(i,255,0,255)
pixels.show()
music_Left()
#right
Motor.Car_Run(255,0)
for i in range(num_leds):
pixels.set_pixel(i,255,0,0)
pixels.show()
music_Right()
#Turn left
Motor.Car_Left(255,255)
for i in range(num_leds):
pixels.set_pixel(i,0,255,0)
pixels.show()
music_TLeft()
#Turn right
Motor.Car_Right(255,255)
for i in range(num_leds):
pixels.set_pixel(i,0,0,255)
pixels.show()
music_TRight()
#Car stop
Motor.Car_Stop()
for i in range(num_leds):
pixels.set_pixel(i,0,0,0)
pixels.show()
巡線小車
說明:
在程式中我們通過判斷每個巡線感測器接收到的資料值,將結果分成七種不同的情況,確定小車的 運行狀態,從而實現小車巡線。
程式
from machine import Pin, I2C
from pico_car import pico_car, ws2812b, SSD1306_I2C
import time
Motor = pico_car()
num_leds = 8 # Number of NeoPixels
# Pin where NeoPixels are connected
pixels = ws2812b(num_leds, 0)
# Set all led off
pixels.fill(0,0,0)
pixels.show()
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
#Define the tracking sensor, 1-4 from left to right
#recognize that black is 0 and white is 1
#Tracing_1 Tracing_2 Tracing_3 Tracing_4
# 2 3 4 5
Tracing_1 = machine.Pin(2, machine.Pin.IN)
Tracing_2 = machine.Pin(3, machine.Pin.IN)
Tracing_3 = machine.Pin(4, machine.Pin.IN)
Tracing_4 = machine.Pin(5, machine.Pin.IN)
while True:
#四路循迹引脚电平状态
#Four channel tracking pin level status
# 0 0 X 0 (右邊在白線上)
# 1 0 X 0
# 0 1 X 0
#处理右锐角和右直角的转动
#Handle the rotation of right acute angle and right right right angle
if (Tracing_1.value() == 0 or Tracing_2.value() == 0) and Tracing_4.value() == 0:
Motor.Car_Right(120,120)
for i in range(num_leds):
pixels.set_pixel(i,0,255,0)
oled.text('Turn Right', 0, 0)
#time.sleep(0.08)
#四路循迹引脚电平状态
#Four channel tracking pin level status
# 0 X 0 0 (左邊在白線上)
# 0 X 0 1
# 0 X 1 0
#处理左锐角和左直角的转动
#Handle the rotation of left sharp angle and left right angle
elif Tracing_1.value() == 0 and (Tracing_3.value() == 0 or Tracing_4.value() == 0):
Motor.Car_Left(120,120)
for i in range(num_leds):
pixels.set_pixel(i,0,0,255)
oled.text('Turn Left', 0, 0)
#time.sleep(0.08)
# 0 X X X
#最左边检测到
#Leftmost detected
elif Tracing_1.value() == 0:
Motor.Car_Left(100,100)
for i in range(num_leds):
pixels.set_pixel(i,0,0,255)
oled.text('Turn Left', 0, 0)
# X X X 0
#最右边检测到
#Rightmost detected
elif Tracing_4.value() == 0:
Motor.Car_Right(100,100)
for i in range(num_leds):
pixels.set_pixel(i,0,255,0)
oled.text('Turn Right', 0, 0)
# X 0 1 X
#处理左小弯
#Deal with small left bend
elif Tracing_2.value() == 0 and Tracing_3.value() == 1:
Motor.Car_Run(0,100)
for i in range(num_leds):
pixels.set_pixel(i,0,0,255)
oled.text('Left', 0, 0)
# X 1 0 X
#处理右小弯
#Handle small right bend
elif Tracing_2.value() == 1 and Tracing_3.value() == 0:
Motor.Car_Run(100,0)
for i in range(num_leds):
pixels.set_pixel(i,0,255,0)
oled.text('Right', 0, 0)
# X 0 0 X
#处理直线
#Processing line
elif Tracing_2.value() == 0 and Tracing_3.value() == 0:
Motor.Car_Run(200,200)
for i in range(num_leds):
pixels.set_pixel(i,255,255,255)
oled.text('Run', 0, 0)
pixels.show()
oled.show()
oled.fill(0)
#其他时小车保持上一个小车运行状态
#In other cases, the trolley keeps the previous trolley running
超聲波避障
說明
程式
import time
from machine import Pin, I2C, PWM
from pico_car import SSD1306_I2C, ultrasonic, pico_car, ws2812b
Motor = pico_car()
Motor.Car_Stop()
num_leds = 8 # Number of NeoPixels
# Pin where NeoPixels are connected
pixels = ws2812b(num_leds, 0)
pixels.fill(0,0,0)
pixels.show()
# set buzzer pin
BZ = PWM(Pin(22))
BZ.freq(1000)
# Initialize music
CM = [0, 330, 350, 393, 441, 495, 556, 624]
#initialization ultrasonic
ultrasonic = ultrasonic()
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
while True:
#get distance
distance = ultrasonic.Distance_accurate()
print("distance is %d cm"%(distance) )
#display distance
oled.text('distance:', 0, 0)
oled.text(str(distance), 75, 0)
oled.show()
oled.fill(0)
#Control action
if distance < 10:
for i in range(num_leds):
pixels.set_pixel(i,255,0,0)
pixels.show()
Motor.Car_Back(150,150)
BZ.duty_u16(500)
BZ.freq(CM[7])
time.sleep(0.2)
Motor.Car_Right(150,150)
BZ.duty_u16(500)
BZ.freq(CM[5])
time.sleep(0.2)
BZ.duty_u16(0)
elif distance >= 10 and distance < 30:
for i in range(num_leds):
pixels.set_pixel(i,255,255,0)
pixels.show()
Motor.Car_Run(100,100)
else:
for i in range(num_leds):
pixels.set_pixel(i,0,255,0)
pixels.show()
Motor.Car_Run(100,100)
time.sleep(0.1)