Use Python to control the car with the keyboard, and use python to control the car through the Raspberry Pi GPIO (Raspberry Pi car, Raspberry Pi+Python+LN298 control car)

(Raspberry Pi car, Raspberry Pi+Python+LN298 control car)

1. Python code to obtain real-time keyboard key positions

2. Raspberry Pi comes with GPIO control motor

You're done! It’s not easy to write. If you succeed, please follow or like. Thank you~~


1. Python code to obtain real-time keyboard key positions

Several libraries that need to be called first----tty, sys, select, termios

Define a function to get the key position in the py file:

#可以自己先试试这个代码,可以直接获取到key值
import tty,sys,select,termios

def getKey(settings):
    tty.setraw(sys.stdin.fileno())
    rlist = select.select([sys.stdin],[],[],0.1)
    
    if rlist:
        key = sys.stdin.read(1)
    else:
        key = ""
    
    termios.tcsetattr(sys.stdin,termios.TCSADRAIN,settings)
    return key

2. Raspberry Pi comes with GPIO control motor

Next, use the RPi.GPIO library that comes with the Raspberry Pi to output signals directly through the pins.

Here I directly define the simplest way to control the operation of two motors, giving only high and low voltages. No PWM speed regulation is used

# -*- coding: utf-8 -*-                 #通过声明可以在程序中书写中文
import RPi.GPIO as GPIO                 #引入RPi.GPIO库函数命名为GPIO
import tty,sys,select,termios

def getKey(settings):
    tty.setraw(sys.stdin.fileno())
    rlist = select.select([sys.stdin],[],[],0.1)
    
    if rlist:
        key = sys.stdin.read(1)
    else:
        key = ""
    
    termios.tcsetattr(sys.stdin,termios.TCSADRAIN,settings)
    return key

# BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BOARD)                #将GPIO编程方式设置为BOARD模式

#接口定义,注意对照树莓派的引脚图
INT1 = 11                              
INT2 = 12
INT3 = 13                              
INT4 = 15

#输出模式
GPIO.setup(INT1,GPIO.OUT)
GPIO.setup(INT2,GPIO.OUT)
GPIO.setup(INT3,GPIO.OUT)
GPIO.setup(INT4,GPIO.OUT)


#前进
def Forward():
    GPIO.output(INT1,GPIO.HIGH)
    GPIO.output(INT2,GPIO.LOW)
    GPIO.output(INT3,GPIO.HIGH)
    GPIO.output(INT4,GPIO.LOW)
#后退
def Back():
    GPIO.output(INT1,GPIO.LOW)
    GPIO.output(INT2,GPIO.HIGH)
    GPIO.output(INT3,GPIO.LOW)
    GPIO.output(INT4,GPIO.HIGH)
#右转
def Right():
    GPIO.output(INT1,GPIO.HIGH)
    GPIO.output(INT2,GPIO.LOW)
    GPIO.output(INT3,GPIO.LOW)
    GPIO.output(INT4,GPIO.LOW)
#左转    
def Left():
    GPIO.output(INT1,GPIO.LOW)
    GPIO.output(INT2,GPIO.LOW)
    GPIO.output(INT3,GPIO.HIGH)
    GPIO.output(INT4,GPIO.LOW)
#停止    
def Stop():
    GPIO.output(INT1,GPIO.LOW)
    GPIO.output(INT2,GPIO.LOW)
    GPIO.output(INT3,GPIO.LOW)
    GPIO.output(INT4,GPIO.LOW)

while(1):
    setting = termios.tcgetattr(sys.stdin)
    InPut = getKey(setting)
    if InPut == "w":
        Forward()
    elif InPut == "s":
        Stop()
    elif InPut == "x":
        Back()
    elif InPut == "a":
        Left()
    elif InPut == "d":
        Right()
    else:
        print("Input Error,Please give a true index!!")
        break

The Raspberry Pi pin diagram is as follows:

Note: When inserting pins here, you should look at the gray digital port, not the number of GPIOs. For example, if ports 11 and 12 are defined in the code, then the corresponding pins in the picture are GPIO17 and GPIO18.


You're done! It’s not easy to write. If you succeed, please follow or like. Thank you~~


Guess you like

Origin blog.csdn.net/Callme_TeacherPi/article/details/124228502