Reprinted please indicate: @Xiaowuyi http://www.cnblogs.com/xiaowuyi
I. Laboratory Equipment
1. TPYboard V102 board
2. Motor Drive Module L298N
3. Motor
4. Car chassis
5. Ultrasound Module
6, 5110 screens
II. Ultrasound Module
1. What is an ultrasonic module?
Ultrasound sensor is a sensor based on the characteristics of ultrasound. It works by transmitting an ultrasound (much higher than the range of human hearing) and providing an output pulse corresponding to the time required for the burst echo to return to the sensor. Ultrasound sensors are widely used in non-contact measurement, such as detecting liquid water level (especially corrosive liquids such as sulphuric acid and nitric acid), anti-collision system for backing up vehicles, metal/non-metal flaw detection, etc., all of which can be used as ultrasonic distance sensors.
2. Principle of ultrasonic module ranging
(1) Trigger ranging with IO port TRIG, and submit a high-level message to a minimum of 10us.
(2) The module automatically sends 8 40 kHz square waves and automatically detects whether there is a return signal.
(3) The signal returns, and a high level is output through the IO port ECHO. The duration of the high level is the time from transmitting to returning of the ultrasound. Test distance = high level time * sound speed (340M/S)/2.
3. Experimentation 1: Ultrasound module ranging, using 5110 to display
1. Connection method
(1) Connection Method of Ultrasound Module
VCC for 5V power supply, GND for ground wire, TRIG trigger control signal input, X2 board, ECHO echo signal output, X1 board, four interface terminals.
(2) 5110 connection and its use
See: http://www.cnblogs.com/xiaowuyi/p/6347336.html
2. Original code
Import font.py file and upcd8544.py file and write main.py
The main.py code is as follows:
import pyb from pyb import Pin from pyb import Timer import upcd8544 from machine import SPI,Pin Trig = Pin('X2',Pin.OUT_PP) Echo = Pin('X1',Pin.IN) num=0 flag=0 run=1 def start(t): global flag global num if(flag==0): num=0 else: num=num+1 def stop(t): global run if(run==0): run=1 start1=Timer(1,freq=10000,callback=start) stop1=Timer(4,freq=2,callback=stop) while True: if(run==1): SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK #DIN =>SPI(1).MOSI 'X8' data flow (Master out, Slave in) #CLK =>SPI(1).SCK 'X6' SPI clock RST = pyb.Pin('Y10') CE = pyb.Pin('Y11') DC = pyb.Pin('Y9') LIGHT = pyb.Pin('Y12') lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT) Trig.value(1) pyb.udelay(100) Trig.value(0) while(Echo.value()==0): Trig.value(1) pyb.udelay(100) Trig.value(0) flag=0 if(Echo.value()==1): flag=1 while(Echo.value()==1): flag=1 if(num!=0): #print('num:',num) distance=num/10000*34299/2 print('Distance:') print(distance,'cm') lcd_5110.lcd_write_string('Distance',0,0) lcd_5110.lcd_write_string(str(distance),0,1) lcd_5110.lcd_write_string('cm',50,1) flag=0 run=0
3. Effect
(1) Close to obstacles
(2) Keep away from obstacles
4. Experiment 2: Barrier Avoidance Car
1. What is the motor drive module?
Motor drive module can mainly control the operation of motor: speed regulation, operation, stop, step-by-step, uniform speed, etc.
2. Connection and usage of L298N
L298N module is driven by two H-bridge, so two motors can be driven at the same time. After ENA ENB is enabled as shown in the figure, the speed and direction of motor 1 can be driven by PWM signal from IN1 IN2, and the speed and direction of motor 2 can be driven by PWM signal from IN3 IN4, respectively. We connect the OUT1 of motor 1 interface with the positive and negative poles of one motor of the car, and the OUT3 of motor 2 interface with the positive and negative poles of another motor of the car. Then connect the two terminals, i.e. the power supply positive pole (the middle terminal is grounded) to the VIN of TPYboard, the middle terminal is grounded, the GND of TPYBoard, In1-In4 to the Y1,Y2, Y3,Y4 of TPYBoard, through the high and low levels of Y1,Y2 and Y3,Y4, to control the rotation of the motor, so as to let the car forward, and after. Back, left, right.
3. Use chassis + V102 + ultrasonic + L298N. When the car is moving forward, it will turn and avoid obstacles by itself.
(1) original code main,py file
# main.py -- put your code here! import pyb from pyb import Pin from pyb import Timer def start(t): global flag global num if(flag==0): num=0 else: num=num+1 def stop(t): global run if(run==0): run=1 def left(): x1.high() x2.low() y1.high() y2.low() def go(): x1.high() x2.low() y1.low() y2.high() def back(): x1.low() x2.high() y1.high() y2.low() def right(): x1.low() x2.high() y1.low() y2.high() def stop(): x1.low() x2.low() y1.low() y2.low() Trig = Pin('X9',Pin.OUT_PP) Echo = Pin('X10',Pin.IN) num=0 flag=0 run=1 start1=Timer(1,freq=10000,callback=start) stop1=Timer(4,freq=2,callback=stop) x1 = Pin('X1', Pin.OUT_PP) x2 = Pin('X2', Pin.OUT_PP) y1 = Pin('Y1', Pin.OUT_PP) y2 = Pin('Y2', Pin.OUT_PP) while True: if(run==1): Trig.value(1) pyb.udelay(100) Trig.value(0) while(Echo.value()==0): Trig.value(1) pyb.udelay(100) Trig.value(0) flag=0 if(Echo.value()==1): flag=1 while(Echo.value()==1): flag=1 if(num!=0): #print('num:',num) distance=num/10000*34299/2 print('Distance') print(distance,'cm') if distance>=20: go() if distance<=20: stop() back() flag=0 run=0
(2) Achieving results