Salted fish ZTMR example-advanced line patrol

Salted fish ZTMR example-advanced line patrol

A simple line tour can't satisfy the wishes of friends, because most of the 'tracks' have various 'pits', such as right angle bends, obtuse angle bends, crossings and other routes. In order to facilitate learning, the code is arranged for testing, and then the code can be selected according to the track situation. Look at the track first and ask if you are afraid.
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,Tracing          #调用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)

def xunji():  
  while True:
    pyb.udelay(50)
    print('xun1:%d,xun2:%d,xun3:%d,xun4:%d' %(xun1.value(),xun2.value(),xun3.value(),xun4.value()))
    #检测到黑线时循迹模块相应的指示灯亮,端口电平为LOW
    #未检测到黑线时循迹模块相应的指示灯灭,端口电平为HIGH
    #四路循迹引脚电平状态
        # 0 0 X 0
        # 1 0 X 0
        # 0 1 X 0
        #以上6种电平状态时小车原地右转
        #处理右锐角和右直角的转动
    if((xun1.value() == 0 or xun2.value() == 0) and  xun4.value() == 0):
           pyb.LED(4).on()
           right(60)
           pyb.delay(800)
           pyb.LED(4).off()
           #print(1)
 
        #四路循迹引脚电平状态
        # 0 X 0 0       
        # 0 X 0 1 
        # 0 X 1 0       
        #处理左锐角和左直角的转动
    elif(xun1.value() == 0 and (xun3.value() == 0 or  xun4.value() == 0)):
           pyb.LED(3).on()
           left(60)
           pyb.delay(800)
           pyb.LED(3).off()
  
        # 0 X X X
        #最左边检测到
    elif(xun1.value() == 0):
           spin_left(40)
     
        # X X X 0
        #最右边检测到
    elif(xun4.value() == 0):
           spin_right(40)
   
        #四路循迹引脚电平状态
        # X 0 1 X
        #处理左小弯
    elif(xun2.value() == 0 and xun3.value() == 1):
           left(40)
   
        #四路循迹引脚电平状态
        # X 1 0 X  
        #处理右小弯
    elif(xun2.value() == 1 and xun3.value() == 0):
           right(40)
   
        #四路循迹引脚电平状态
        # X 0 0 X
        #处理直线
    elif(xun2.value() == 0 and xun3.value() == 0):
           go(50)
    #go(20)
        #当为1 1 1 1时小车保持上一个小车运行状态

if __name__ == '__main__':
    xunji()               

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/105580222