AI视觉小车基础--7.四路巡线模块状态打印

一、实验准备

        读取四路巡线模块的状态并打印。

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)

参数解释:读取四路巡线模块的状态数据

  1. 0x0a:四路巡线模块的地址,1:读取一个字节。
  2. 返回值:返回四路巡线模块上4个传感器数据的数组。数组里的数据为0和1。0为遇到黑线返回值,1为不是黑线返回值

四、实验现象

        程序运行后,可以读取到四路巡线模块上4个传感器的状态,并在oled屏显示状态

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值