Raspberry Pi expansion board --- basic movement of the car

Motor Schematic

  • PWMA and PWMB control plus ++ duty cycle control enable AB
  • AIN1 AIN2 control enable A corresponding interface output input ----- high and low level
  • BIN1 BIN2 control enable B
  • Control the steering of the trolley through 4 input and output interfaces----refer to the L298N motor module
  • Enable A to control the left wheel
  • Enable B to control the right wheel

Want to adjust the speed --- first set PWMA and PWMB ----- frequency

In setting the corresponding duty cycle of AB - high level duty cycle - 0~100

Wiring diagram:

 Basic Motion Code - Autonomous Driving

#!/home/pi/server
# coding=utf-8
#小车基本运动
#GPIO±àÂ뷽ʽΪBOARD£¡£¡
import  RPi.GPIO as GPIO
import time
#设置使能AB通道!!!
PWMA = 18
AIN1   =  22
AIN2   =  27

PWMB = 23
BIN1   = 25
BIN2  =  24
#前进函数
def t_up(speed,t_time):         #速度通过占空比控制0--100;加上运动时间
        L_Motor.ChangeDutyCycle(speed)#AIN左边轮子
        GPIO.output(AIN2,False)#AIN2####1高2低----前进
        GPIO.output(AIN1,True) #AIN1

        R_Motor.ChangeDutyCycle(speed)#BIN控制右边轮子转动
        GPIO.output(BIN2,False)#BIN2#3高4低正传
        GPIO.output(BIN1,True) #BIN1
        time.sleep(t_time)
#停止
def t_stop(t_time):
        L_Motor.ChangeDutyCycle(0)      #占空比为零
        GPIO.output(AIN2,False)#AIN2    #各个电机为低电平
        GPIO.output(AIN1,False) #AIN1

        R_Motor.ChangeDutyCycle(0)
        GPIO.output(BIN2,False)#BIN2
        GPIO.output(BIN1,False) #BIN1
        time.sleep(t_time)
        
def t_down(speed,t_time):       #后退
        L_Motor.ChangeDutyCycle(speed)
        GPIO.output(AIN2,True)#AIN2
        GPIO.output(AIN1,False) #AIN1

        R_Motor.ChangeDutyCycle(speed)
        GPIO.output(BIN2,True)#BIN2
        GPIO.output(BIN1,False) #BIN1
        time.sleep(t_time)

def t_left(speed,t_time):#左转########左边两个轮子后转。,右边前传
        L_Motor.ChangeDutyCycle(speed)
        GPIO.output(AIN2,True)#AIN2     #1低2高反转
        GPIO.output(AIN1,False) #AIN1

        R_Motor.ChangeDutyCycle(speed)#右边3高4低正转
        GPIO.output(BIN2,False)#BIN2
        GPIO.output(BIN1,True) #BIN1
        time.sleep(t_time)

def t_right(speed,t_time):
        L_Motor.ChangeDutyCycle(speed)##设置左边PWMA使能A的占空比
        GPIO.output(AIN2,False)#AIN2
        GPIO.output(AIN1,True) #AIN1
#1/0   True/False   GPIO.HIGH控制高低电平  ----转向
        R_Motor.ChangeDutyCycle(speed)
        GPIO.output(BIN2,True)#BIN2
        GPIO.output(BIN1,False) #BIN1
        time.sleep(t_time)    
        
GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BCM)        #设置通道
GPIO.setup(AIN2,GPIO.OUT)
GPIO.setup(AIN1,GPIO.OUT)
GPIO.setup(PWMA,GPIO.OUT)

GPIO.setup(BIN1,GPIO.OUT)
GPIO.setup(BIN2,GPIO.OUT)
GPIO.setup(PWMB,GPIO.OUT)

L_Motor= GPIO.PWM(PWMA,100)#设置PWMA输出频率为100#左边PWMA使能
L_Motor.start(0)#使得频率一定,开始PWM

R_Motor = GPIO.PWM(PWMB,100)#右边PWMB使能频率
R_Motor.start(0)

try:
    while True:
        t_up(50,3)#前进速度50dc,三秒钟
        t_down(50,3)
        t_left(50,3)
        t_right(50,3)
        t_stop(3)#停止三秒
except KeyboardInterrupt:
    GPIO.cleanup()

2. Control basic movement 

#通过input()函数来进行输入操作,电脑按键控制小车前进
#!/home/pi/server
# coding=utf-8
#小车基本运动

import  RPi.GPIO as GPIO
import time
#设置使能AB通道!!!
PWMA = 18
AIN1   =  22
AIN2   =  27

PWMB = 23
BIN1   = 25
BIN2  =  24
#前进函数
def t_up(speed,t_time):         #速度通过占空比控制0--100;加上运动时间
        L_Motor.ChangeDutyCycle(speed)#AIN左边轮子
        GPIO.output(AIN2,False)#AIN2####1高2低----前进
        GPIO.output(AIN1,True) #AIN1

        R_Motor.ChangeDutyCycle(speed)#BIN控制右边轮子转动
        GPIO.output(BIN2,False)#BIN2#3高4低正传
        GPIO.output(BIN1,True) #BIN1
        time.sleep(t_time)
#停止
def t_stop(t_time):
        L_Motor.ChangeDutyCycle(0)      #占空比为零
        GPIO.output(AIN2,False)#AIN2    #各个电机为低电平
        GPIO.output(AIN1,False) #AIN1

        R_Motor.ChangeDutyCycle(0)
        GPIO.output(BIN2,False)#BIN2
        GPIO.output(BIN1,False) #BIN1
        time.sleep(t_time)
        
def t_down(speed,t_time):       #后退
        L_Motor.ChangeDutyCycle(speed)
        GPIO.output(AIN2,True)#AIN2
        GPIO.output(AIN1,False) #AIN1

        R_Motor.ChangeDutyCycle(speed)
        GPIO.output(BIN2,True)#BIN2
        GPIO.output(BIN1,False) #BIN1
        time.sleep(t_time)

def t_left(speed,t_time):#左转########左边两个轮子后转。,右边前传
        L_Motor.ChangeDutyCycle(speed)
        GPIO.output(AIN2,True)#AIN2     #1低2高反转
        GPIO.output(AIN1,False) #AIN1

        R_Motor.ChangeDutyCycle(speed)#右边3高4低正转
        GPIO.output(BIN2,False)#BIN2
        GPIO.output(BIN1,True) #BIN1
        time.sleep(t_time)

def t_right(speed,t_time):
        L_Motor.ChangeDutyCycle(speed)##设置左边PWMA使能A的占空比
        GPIO.output(AIN2,False)#AIN2
        GPIO.output(AIN1,True) #AIN1
#1/0   True/False   GPIO.HIGH控制高低电平  ----转向
        R_Motor.ChangeDutyCycle(speed)
        GPIO.output(BIN2,True)#BIN2
        GPIO.output(BIN1,False) #BIN1
        time.sleep(t_time)    
        
GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BCM)
GPIO.setup(AIN2,GPIO.OUT)
GPIO.setup(AIN1,GPIO.OUT)
GPIO.setup(PWMA,GPIO.OUT)

GPIO.setup(BIN1,GPIO.OUT)
GPIO.setup(BIN2,GPIO.OUT)
GPIO.setup(PWMB,GPIO.OUT)

L_Motor= GPIO.PWM(PWMA,100)#设置PWMA输出频率为100#左边PWMA使能
L_Motor.start(0)#使得频率一定,开始PWM

R_Motor = GPIO.PWM(PWMB,100)#右边PWMB使能频率
R_Motor.start(0)

try:
    while True:
        res = input(" 输入: ")
        if res =="w"or res=="W":
                t_up(50,1.5)
        elif res =="s" or "S":  #输入后退
                t_down(50,1.5)
        if res == "a" or "A":
                t_left(50,1)
        if res == "r" or "R":
                t_right(50,1)
        if res == "p" or "P":
                t_stop(2)
        """
         t_up(50,3)#前进速度50,三秒钟
        t_down(50,3)
        t_left(50,3)
        t_right(50,3)
        t_stop(3)
       #停止三秒"""
except KeyboardInterrupt:
    GPIO.cleanup()


 

Guess you like

Origin blog.csdn.net/JLwwfs/article/details/126111784