Example of salted fish ZTMR—cart to prevent falling

Example of salted fish ZTMR—cart to prevent falling


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

The principle of drop is that when the four level values ​​are all 0. Stop the car (it's that simple). We made a "garage" before, now we call the garage to complete the anti-drop effect, the code is as follows

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()))
	#循环打印电平值	
	#如果4个引脚返回的电平值都是0 则说明传感器未检测到物体。则让车停止运行
	if(xun1.value()==xun2.value()==xun3.value()==xun4.value()==0):
                Car.stopdj()
    	else:
                Car.go(40)
                

Insert picture description here
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/105556742