Salted Fish ZTMR Example-Getting Started

Salted Fish ZTMR Example-Getting Started

Before, we used the tracking module to make a simple function to prevent the car from falling down, but tracking is not simple (/ funny). This time we use tracking to make a line-tracking function. Simply put, we draw a line and let the car follow the line ~~

Line patrol is more difficult than anti-drop. As long as the sensor does not return data, the car can be stopped. When patrolling the line, the line is uncertain, and it is more difficult to process the data in various situations. In order to facilitate understanding, let's do a simple trace. The venue is as shown below (simple enough): the
Insert picture description here
above picture is our "track", the car is driving to the right, we have to detect the "track" and follow the track line. The sensor only returns two signals 0 and 1 (low level and high level), so we set it first

  1. When a black line is detected , the corresponding indicator of the tracking module lights up, and the port level is LOW (0)
  2. When no black line is detected , the corresponding indicator of the tracking module is off, the port level is HIGH (1)
    "Need to adjust the sensor accuracy"

Signal 2 or signal 3 should be on at the straight line.
Insert picture description here

Signal 4 detects that the black line should be on when turning. Turn right at this time
Insert picture description here

Signal 1 is used for correction here. (If signal 1 detects a black line, it means that the car needs to turn left inside the track until signal 2 and signal 3 light up at the same time.)
Insert picture description here

Main control board: ZTMR1.1python development board

N20 gear motor ( motor on four-wheel drive)
Insert picture description here

ZT infrared tracking
Insert picture description here

Pin description

Pin Explanation
VCC 5v
GND GND
x1~x4 All for input

code show as below

main.py

# main.py -- put your code here!
from car import Car            #调用car.py里的Car类。
from pyb import Pin, Timer,LED
from time import sleep_us,ticks_us,sleep
#定义引脚,低电平时,指示灯亮。
xun1 = Pin(("B1"),Pin.IN)
xun4 = Pin(("B0"),Pin.IN)
xun3 = Pin(("C7"),Pin.IN)
xun2 = Pin(("C6"),Pin.IN)
while True:
    pyb.udelay(1000)
    print('xun1:%d,xun2:%d,xun3:%d,xun4:%d' %(xun1.value(),xun2.value(),xun3.value(),xun4.value()))
    #检测到黑线时循迹模块相应的指示灯亮,端口电平为(0)
        #未检测到黑线时循迹模块相应的指示灯灭,端口电平为(1)
    if(xun1.value()==0):
            #四路循迹引脚电平状态
            #信号1检测到黑线电平值为0,左转(或者原地左旋转)
            # 0 1 1 1
            #Car.left(10)
            Car.spin_left(20)               
    elif(xun4.value()==0)#四路循迹引脚电平状态
            # 信号4检测到黑线了,右转即可
            # 1 0 0 0
            # 1 1 0 0
            # 1 0 1 0                
            Car.right(20)
    else:
            #四路循迹引脚电平状态
            # 信号2或信号3能检测到黑线直行即可,有以下6中情况
            # 1 0 0 0
            # 1 1 0 0
            # 1 0 1 0
            # 1 0 1 1
            # 1 1 0 1
            # 1 0 0 1                
            Car.go(40)             

car.py

# main.py -- put your code here!
from pyb import Pin, Timer,delay
from time import sleep_us,ticks_us,sleep
cs = Pin('B10',Pin.OUT_PP)    #B10设置为输出引脚输出高电平
cs(1)
ch1 =None
ch2 =None     #初始化
AI1 = Pin('B12',Pin.OUT_PP)    #右侧马达
AI2 = Pin('B13',Pin.OUT_PP)
BI1 = Pin('B14',Pin.OUT_PP)    #左侧马达
BI2 = Pin('B15',Pin.OUT_PP)

#A电机(右)
p1 = Pin('B8') 
tim1 = Timer(10, freq=120)                  
ch1 = tim1.channel(1, Timer.PWM, pin=p1)
#B电机(左)
p2 = Pin('B9') 
tim2 = Timer(4, freq=120)                  
ch2 = tim2.channel(4, Timer.PWM, pin=p2)

#小车状态
class Car():          #把小车行驶状态存入Car类中
    def go(speed):    #直行状态                
            ch1.pulse_width_percent(speed)   
            ch2.pulse_width_percent(speed)
            AI1(0)        
            AI2(1)
            BI1(1)
            BI2(0)
      
    def back(speed):  #逆行
            ch1.pulse_width_percent(speed)
            ch2.pulse_width_percent(speed)
            AI1(1)
            AI2(0)
            BI1(0)
            BI2(1)

    def stopdj(): #停止
            ch1.pulse_width_percent(0)
            ch2.pulse_width_percent(0)
            

    def spin_left(speed): #左旋
            ch1.pulse_width_percent(0)#右
            ch2.pulse_width_percent(speed)
            AI1(0)
            AI2(0)
            BI1(1)
            BI2(0)

    def spin_right(speed):#右旋
            ch1.pulse_width_percent(speed)
            ch2.pulse_width_percent(0)
            AI1(1)
            AI2(0)
            BI1(0)
            BI2(0)

    def left(speed):      #左转
            ch1.pulse_width_percent(0)#右
            ch2.pulse_width_percent(speed)
            AI1(0)
            AI2(0)
            BI1(1)
            BI2(0)

    def right(speed):     #右转
            ch1.pulse_width_percent(speed)
            ch2.pulse_width_percent(0)
            AI1(1)
            AI2(0)
            BI1(0)
            BI2(0)
Published 166 original articles · 22 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/105574613