树莓派智能小车

最近在研究树莓派,也从CSDN上面汲取了很多大神的代码。
我的小车目前正在开发中,已经基本成型。
我把我的代码和物理连接设计图共享出来。
希望能够和树莓派爱好者们一起开发出高水平的树莓派机器人。
有问题想讨论可以发邮件给我,或者Github。
https://gist.github.com/CC12345PI
物理连接图:
树莓派智能小车_第1张图片

设计图:
树莓派智能小车_第2张图片


# -*- coding: utf-8 -*-
"""
Created on Sat May 26 11:03:58 2018
small car control main progress 
Platform is python+raspberrypi 3B. 
Follow scripts not include rear wheel and LED control. 
Those part will updated soon. 
@author: [email protected]
"""



def system_ini():
    GPIO.setmode(GPIO.BCM)  
    GPIO.setup(Red,GPIO.OUT) #RED LED
    GPIO.setup(Blue,GPIO.OUT)  #BLUE LED
    GPIO.setup(Green,GPIO.OUT)  #GREEN LED  
    GPIO.setup(Enable1_Pin,GPIO.OUT,initial=GPIO.LOW)  # 
    GPIO.setup(input3_pin,GPIO.OUT,initial=GPIO.LOW)  # 
    GPIO.setup(input4_pin,GPIO.OUT,initial=GPIO.LOW)  # 
    GPIO.setup(Enable2_Pin,GPIO.OUT,initial=GPIO.LOW)  # 
    GPIO.setup(input1_pin,GPIO.OUT,initial=GPIO.LOW)  # 
    GPIO.setup(input2_pin,GPIO.OUT,initial=GPIO.LOW)  #  
    print ("初始化完成")
def cleanup():

    GPIO.output(input3_pin, GPIO.HIGH)
    GPIO.output(input4_pin, GPIO.HIGH)
    GPIO.output(Enable1_Pin, GPIO.HIGH) 
    GPIO.output(input1_pin, GPIO.HIGH)
    GPIO.output(input2_pin, GPIO.HIGH)
    GPIO.output(Enable2_Pin, GPIO.HIGH)  
    GPIO.cleanup()
    print ("清理完成")


def forward():
    system_ini()
    #left-front正传
    print ("前进forward")
    #left-front
    GPIO.output(input3_pin, GPIO.LOW)
    GPIO.output(input4_pin, GPIO.HIGH)
    GPIO.output(Enable1_Pin, GPIO.HIGH)
    #right-front
    GPIO.output(input1_pin, GPIO.LOW)
    GPIO.output(input2_pin, GPIO.HIGH)
    GPIO.output(Enable2_Pin, GPIO.HIGH)     
    GPIO.output(Red,GPIO.HIGH)
    time.sleep(0.05)

def backward():
    system_ini() 
    print ("后退backward")
    #left-front
    GPIO.output(input3_pin, GPIO.HIGH)
    GPIO.output(input4_pin, GPIO.LOW)
    GPIO.output(Enable1_Pin, GPIO.HIGH) 
    GPIO.output(Blue, GPIO.HIGH)
    #right-front
    GPIO.output(input1_pin, GPIO.HIGH)
    GPIO.output(input2_pin, GPIO.LOW)
    GPIO.output(Enable2_Pin, GPIO.HIGH)
    time.sleep(0.05)  
def left90D():
    #此代码不包括后轮的转向。
    #直流马达在DC3V的情况下是,100转/分, 所以0.15秒对应0.25转, 0.3秒对应0.5转。 
    #我们的转向按照0.25转为单位微调。配合车前后的传感器进行检测是否撞墙。   
    system_ini()
    print ("turn left 90 degree")
    #left-front stop
    GPIO.output(input3_pin, GPIO.HIGH)
    GPIO.output(input4_pin, GPIO.HIGH)
    GPIO.output(Enable1_Pin, GPIO.HIGH) 
    GPIO.output(Blue, GPIO.HIGH)
    #right-front forward 0.15 second
    GPIO.output(input1_pin, GPIO.LOW)
    GPIO.output(input2_pin, GPIO.HIGH)
    GPIO.output(Enable2_Pin, GPIO.HIGH)  
    time.sleep(0.05)
def right90D():
    #此代码不包括后轮的转向。
    #直流马达在DC3V的情况下是,100转/分, 所以0.15秒对应0.25转, 0.3秒对应0.5转。 
    #我们的转向按照0.25转为单位微调。配合车前后的传感器进行检测是否撞墙。   
    system_ini()
    print ("turn rigth 90 degree")
    #left-front forward 0.15 second
    GPIO.output(input3_pin, GPIO.LOW)
    GPIO.output(input4_pin, GPIO.HIGH)
    GPIO.output(Enable1_Pin, GPIO.HIGH) 
    GPIO.output(Blue, GPIO.HIGH)
    #right-front stop for 5 second
    GPIO.output(input1_pin, GPIO.HIGH)
    GPIO.output(input2_pin, GPIO.HIGH)
    GPIO.output(Enable2_Pin, GPIO.HIGH)  
    time.sleep(0.05)


def stop():
    print ("stop")
    #left-front forward  second
    GPIO.output(input3_pin, GPIO.LOW)
    GPIO.output(input4_pin, GPIO.LOW)
    GPIO.output(Enable1_Pin, GPIO.LOW) 
    GPIO.output(Blue, GPIO.HIGH)
    #right-front stop for 5 second
    GPIO.output(input1_pin, GPIO.LOW)
    GPIO.output(input2_pin, GPIO.LOW)
    GPIO.output(Enable2_Pin, GPIO.LOW)


if __name__ == '__main__':
    try:
    #run your code 
        import RPi.GPIO as GPIO
        import time  
        from evdev import InputDevice
        from select import select
        dev = InputDevice('/dev/input/event0')
        GPIO.setmode(GPIO.BCM) 
        Red = 24
        Blue = 25
        Green = 26   
        #left-front 
        Enable1_Pin = 4
        input3_pin = 5
        input4_pin = 6
        #right-front
        Enable2_Pin = 17
        input1_pin = 16
        input2_pin = 13 
        #系统初始化      
        system_ini()
        #   
        #控制电压输出
        while True:   
            select([dev], [], [])
            for event in  dev.read():
                print 'code:%s value:%s' % (event.code, event.value)
                if event.code == 103 and event.value == 2:
                    print ("forward")
                    forward() 
                elif event.code == 103 and  event.value == 0: 
                    print("stop forward")
                    stop()
                elif event.code == 108 and  event.value == 2: 
                    print("backward")
                    backward()  
                elif event.code == 108 and  event.value == 0: 
                    print("backward STOP ")
                    stop()  
                elif event.code == 105 and  event.value == 2: 
                    print("LEFT")
                    left90D()   
                elif event.code == 105 and  event.value == 0: 
                    print("stop ")
                    stop()
                elif event.code == 106 and  event.value == 2: 
                    print("Right")
                    right90D()  
                elif event.code == 106 and  event.value == 0: 
                    print("stop ")
                    stop() 
                elif event.code == 57:  
                    stop()   

    except(BaseException),e:
        print(e)
    finally:
        GPIO.cleanup()

你可能感兴趣的:(机器人)