一、实验准备
读取四路巡线模块的状态并打印。
1.1 接线
如下图所示,将四路巡线模块接在扩展板上。
1.2 调试四路巡线模块
(1)将小车四路巡线模块放在白纸上或者其他白色背景的上面。
(2)将第一个探头放在黑线上方,调节旋钮,调节成指示灯亮。
(3)接着继续调第二、第三个探头,调节旋钮,调节成指示灯。依此类推,调试第四个探头
最终调试成探头在黑线上方时,指示灯亮;探头不在黑线上方时灭。
二、运行代码
import time
from Raspbot_Lib import Raspbot
from ipywidgets import interact
import ipywidgets as widgets
import sys
sys.path.append('/home/pi/software/oled_yahboom/')
from yahboom_oled import *
# Create the Raspbot object bot
bot = Raspbot()
# Create an oled object
oled = Yahboom_OLED(debug=False)
def main():
try:
oled.init_oled_process() # Initialize oled process
while True:
# Read the status of the line patrol sensor
track_data = bot.read_data_array(0x0a, 1)
track = int(track_data[0])
# Analyze the status of the line patrol sensor
x1 = (track >> 3) & 0x01
x2 = (track >> 2) & 0x01
x3 = (track >> 1) & 0x01
x4 = track & 0x01
# Print the status of the line-following sensor
print(f"Line Tracker Status: {x2}, {x1}, {x3}, {x4}")
oled.clear()
lts_str=f' {x2} {x1} {x3} {x4}'
oled.add_line('Line Tracker Status:', 1)
oled.add_line(' x2 x1 x3 x4', 3)
oled.add_line(lts_str, 4)
oled.refresh()
# Pause for a short time to read the data again
time.sleep(0.1)
except KeyboardInterrupt:
# When the user presses Ctrl+C, the program will end
# Restore basic data display on screen
os.system("python3 /home/pi/software/oled_yahboom/yahboom_oled.py &")
print("Line tracking stopped by user.")
if __name__ == "__main__":
main()
del bot
三、核心代码解析
读取四路巡线模块的状态需要用到的Raspbot_Lib库函数:
read_data_array(0x0a, 1)
参数解释:读取四路巡线模块的状态数据
- 0x0a:四路巡线模块的地址,1:读取一个字节。
- 返回值:返回四路巡线模块上4个传感器数据的数组。数组里的数据为0和1。0为遇到黑线返回值,1为不是黑线返回值
四、实验现象
程序运行后,可以读取到四路巡线模块上4个传感器的状态,并在oled屏显示状态