本文主要是介绍树莓派gpio+python遥控三轮小车,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
树莓派遥控两轮小车
旧硬件堆里翻出来的小车,轮子有点歪哈哈
使用四按键 硬件遥控模块(按下输出高电平)
电机驱动为L298N模块
主控使用树莓派zero w(更加小巧),设置中打开GPIO和焊接好针脚
电池为两节18650串联7.5V为电机模块供电,没找到18650电源板就并联了两个7805降压输出5V1.4A为树莓派、电机模块、遥控模块供电;所以供电不太足导致左右转速度不一样哈哈
实现4键控制启动、停止、前进、后退、左转、右转、倒车左转、倒车右转,且前进后退为双向开关
这里用定时任务控制程序启动,也可以配开机启动rc.local
crontab -e
添加一行设置程序开机启动
@reboot python /home/pi/car_tele.py
# -*- coding: utf-8 -*-#将RPi.GPIO 模块映射为GPIO来使用
from RPi import GPIO
#引入time模块
import time#采用BCM引脚顺序编号
GPIO.setmode(GPIO.BCM)
#关闭警告
GPIO.setwarnings(False)#输入IO
INPUT_PIN0 = 06
INPUT_PIN1 = 13
INPUT_PIN2 = 19
INPUT_PIN3 = 26
# 设置GPIO输入模式, 使用GPIO内置的下拉电阻, 即开关断开情况下输入为LOW,适用于高电平输入
GPIO.setup(INPUT_PIN0, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(INPUT_PIN1, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(INPUT_PIN2, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(INPUT_PIN3, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# 设置GPIO输入模式, 使用GPIO内置的上拉电阻, 即开关断开情况下输入为HIGH,适用于接地的开关
#GPIO.setup(INPUT_PIN0, GPIO.IN, pull_up_down=GPIO.PUD_UP) #输出IO
GPIO_PIN0 = 17
GPIO_PIN1 = 27
GPIO_PIN2 = 22
GPIO_PIN3 = 04
#配置输出IO方向
GPIO.setup(GPIO_PIN0, GPIO.OUT)
GPIO.setup(GPIO_PIN1, GPIO.OUT)
GPIO.setup(GPIO_PIN2, GPIO.OUT)
GPIO.setup(GPIO_PIN3, GPIO.OUT)
#输出IO初始化设置为低电平
GPIO.output(GPIO_PIN0, GPIO.LOW)
GPIO.output(GPIO_PIN1, GPIO.LOW)
GPIO.output(GPIO_PIN2, GPIO.LOW)
GPIO.output(GPIO_PIN3, GPIO.LOW)# 检测输入LOW -> HIGH的变化
GPIO.add_event_detect(INPUT_PIN0, GPIO.RISING, bouncetime = 200)
GPIO.add_event_detect(INPUT_PIN1, GPIO.RISING, bouncetime = 200)
GPIO.add_event_detect(INPUT_PIN2, GPIO.RISING, bouncetime = 200)
GPIO.add_event_detect(INPUT_PIN3, GPIO.RISING, bouncetime = 200)#按键自锁判断变量
led0Status = 0
led1Status = 0#重置,所有按键归零
def reset():GPIO.output(GPIO_PIN0, GPIO.LOW) GPIO.output(GPIO_PIN1, GPIO.LOW)GPIO.output(GPIO_PIN2, GPIO.LOW)GPIO.output(GPIO_PIN3, GPIO.LOW)#直行
def forward():global led0Statusglobal led1Statusif GPIO.event_detected(INPUT_PIN0):reset()led1Status = 0 #自锁开关切换,双向开关led0Status = not led0Statusif led0Status:GPIO.output(GPIO_PIN0, GPIO.HIGH)GPIO.output(GPIO_PIN1, GPIO.HIGH)else:reset()time.sleep(0.01) # 10毫秒的检测间隔#倒车
def backward():global led0Statusglobal led1Statusif GPIO.event_detected(INPUT_PIN1):reset()led0Status = 0 #自锁开关切换,双向开关led1Status = not led1Statusif led1Status:GPIO.output(GPIO_PIN2, GPIO.HIGH)GPIO.output(GPIO_PIN3, GPIO.HIGH)else:reset()time.sleep(0.01) # 10毫秒的检测间隔#向前和向后状态下左转
def left():global led0Statusglobal led1Statusif led0Status == True:while (GPIO.input(INPUT_PIN2) and led0Status == True):reset()GPIO.output(GPIO_PIN0, GPIO.HIGH)else:reset()GPIO.output(GPIO_PIN0, GPIO.HIGH)GPIO.output(GPIO_PIN1, GPIO.HIGH)time.sleep(0.01) # 10毫秒的检测间隔if led1Status == True:while (GPIO.input(INPUT_PIN2) and led1Status == True):reset()GPIO.output(GPIO_PIN2, GPIO.HIGH)else:reset()GPIO.output(GPIO_PIN2, GPIO.HIGH)GPIO.output(GPIO_PIN3, GPIO.HIGH)time.sleep(0.01) # 10毫秒的检测间隔#向前和向后状态下右转
def right():global led0Statusglobal led1Statusif led0Status == True:while (GPIO.input(INPUT_PIN3) and led0Status == True):reset()GPIO.output(GPIO_PIN1, GPIO.HIGH)else:if led0Status == True:reset()GPIO.output(GPIO_PIN0, GPIO.HIGH)GPIO.output(GPIO_PIN1, GPIO.HIGH)time.sleep(0.01) # 10毫秒的检测间隔if led1Status == True:while (GPIO.input(INPUT_PIN3) and led1Status == True):reset()GPIO.output(GPIO_PIN3, GPIO.HIGH)else:if led1Status == True:reset()GPIO.output(GPIO_PIN2, GPIO.HIGH)GPIO.output(GPIO_PIN3, GPIO.HIGH)time.sleep(0.01) # 10毫秒的检测间隔try:while True:if GPIO.input(INPUT_PIN0):forward();elif GPIO.input(INPUT_PIN1):backward();elif GPIO.input(INPUT_PIN2):left();elif GPIO.input(INPUT_PIN3):right();#程序中止时清理GPIO资源
except KeyboardInterrupt:GPIO.cleanup() print(' program stopped !')
这篇关于树莓派gpio+python遥控三轮小车的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!