树莓派智能小车(循迹、避障、测距)

1、基本运动(move.py)

import RPi.GPIO as GPIO
import time
AIN1=18
AIN2=19
PWMA=13
BIN1=20
BIN2=21
PWMB=12

def setup():
    global l_motor
    global r_motor
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(PWMA,GPIO.OUT)
    GPIO.setup(PWMB,GPIO.OUT)
    GPIO.setup(AIN1,GPIO.OUT)
    GPIO.setup(AIN2,GPIO.OUT)
    GPIO.setup(BIN1,GPIO.OUT)
    GPIO.setup(BIN2,GPIO.OUT)
    l_motor=GPIO.PWM(PWMA,500)
    r_motor=GPIO.PWM(PWMB,500)
    l_motor.start(0)
    r_motor.start(0)

def turn_up(speed,t):
    l_motor.ChangeDutyCycle(speed)
    r_motor.ChangeDutyCycle(speed)
    GPIO.output(AIN1,0)
    GPIO.output(BIN1,0)
    GPIO.output(AIN2,1)
    GPIO.output(BIN2,1)
    time.sleep(t)

def turn_down(speed,t):
    l_motor.ChangeDutyCycle(speed)
    r_motor.ChangeDutyCycle(speed)
    GPIO.output(AIN1,1)
    GPIO.output(BIN1,1)
    GPIO.output(AIN2,0)
    GPIO.output(BIN2,0)
    time.sleep(t)

def turn_right(speed,t):
    l_motor.ChangeDutyCycle(speed)
    r_motor.ChangeDutyCycle(speed)
    GPIO.output(AIN1,1)
    GPIO.output(BIN1,0)
    GPIO.output(AIN2,0)
    GPIO.output(BIN2,1)
    time.sleep(t)

def turn_left(speed,t):
    l_motor.ChangeDutyCycle(speed)
    r_motor.ChangeDutyCycle(speed)
    GPIO.output(AIN1,0)
    GPIO.output(BIN1,1)
    GPIO.output(AIN2,1)
    GPIO.output(BIN2,0)
    time.sleep(t)

def stop(t):
    GPIO.output(AIN1,0)
    GPIO.output(BIN1,0)
    GPIO.output(AIN2,0)
    GPIO.output(BIN2,0)
    time.sleep(t)

def loop():
    while 1:
        turn_up(30,0)

def destory():
    stop(0)
    GPIO.cleanup()
    l_motor.stop()
    r_motor.stop()

if __name__=='__main__':
    setup()
    try:
        loop()
    except KeyboardInterrupt:
        destory()

2.红外循迹

import move
import RPi.GPIO as GPIO
sensor_left=25
sensor_right=24

def setup():
    GPIO.setup(sensor_left,GPIO.IN)
    GPIO.setup(sensor_right,GPIO.IN)

def loop():
    while 1:
        SR=GPIO.input(sensor_right)
        SL=GPIO.input(sensor_left)
        if SR==1 and SL==0:
            move.turn_right(50,0)
        elif SR==0 and SL==1:
            move.turn_left(50,0)
        elif SR==0 and SL==0:
            move.turn_up(50,0)
        else:
            move.stop(0)

if __name__=='__main__':
    move.setup()
    setup()
    try:
        loop()
    except KeyboardInterrupt:
        move.destory()

3.红外避障

import move
import RPi.GPIO as GPIO
sensor_left=17
sensor_right=16

def setup():
    GPIO.setup(sensor_left,GPIO.IN)
    GPIO.setup(sensor_right,GPIO.IN)

def loop():
    while 1:
        SR=GPIO.input(sensor_right)
        SL=GPIO.input(sensor_left)
        if SR==1 and SL==1:
            move.turn_up(50,0)
        elif SR==0 and SL==1:
            move.turn_left(50,0)
        elif SR==1 and SL==0:
            move.turn_right(50,0)
        else:
            move.stop(1)
            move.turn_down(50,0.5)
            move.turn_left(50,0.5)

if __name__=='__main__':
    move.setup()
    setup()
    try:
        loop()

    except KeyboardInterrupt:
        move.destory()

4.超声波测距

import time
import move
import RPi.GPIO as GPIO
trig=27
echo=26

def setup():
    GPIO.setup(trig,GPIO.OUT)
    GPIO.setup(echo,GPIO.IN)

def distance():
    GPIO.output(trig,0)
    time.sleep(0.000002)
    GPIO.output(trig,1)
    time.sleep(0.00001)
    GPIO.output(trig,0)
    while GPIO.input(echo)==0:
        pass
    t1=time.time()
    while GPIO.input(echo)==1:
        pass
    t2=time.time()
    dur=t2-t1
    print(dur*340/2*100)
    return dur*340/2*100

def loop():
    while 1:
        dist=distance()
        if dist<60:
            move.turn_down(50,1)
            move.turn_left(50,1)
        else:
            move.turn_up(50,1)


if __name__=='__main__':
    try:
        move.setup()
        setup()
        loop()
    except KeyboardInterrupt:
        move.destory()

5.硬件清单:

树莓派4b主板×1

亚克力板×2

车轮×4

直流电机×4

L298N电机驱动模块×2

红外避障模块×2

红外循迹模块×2

超声波模块×1

7.4V可充电锂电池×2

3.7V可充电锂电池×1

面包板(选配)

T型扩展版(选配)

通孔铜柱、螺丝、螺母、杜邦线若干

效果图:

树莓派智能小车(循迹、避障、测距)_第1张图片

附github项目地址https://github.com/code-wjh/smartCar

你可能感兴趣的:(嵌入式硬件)