python3小车控制程序 python3实现raspberry pi(树莓派)4驱小车控制程序
weixin_42534940 人气:00. 写在前面:
一两个月前偶然得到一个树莓派一代,发现还是挺强大的,然后就考虑着自己实现一个四驱小车。因为本身是学软件的,所以利用软件来控制实实在在可见的硬件一直是自己的一个梦想。所以当时也是花了很多时间来琢磨、实验小车控制方面的东西。因为时间过去比较久了,这里也就只是记录一下当时踩过的坑、实现的一些思路以及绝大部分源代码。截止当前,我的树莓派小车实现的主要功能是:
- 控制小车前进、后退、左转、右转、后退左转、后退右转
- 摄像头两路舵机控制(实现摄像头上下左右旋转)
- 网页版小车控制程序(小车控制程序、实时监控、摄像头旋转控制)
- 微信小程序版小车控制程序(功能基本同上)
(本文只是介绍上述的第一个功能,其他功能的实现在后续的博文中介绍)
1. 准备:
- 给树莓派安装系统(这个网上资料太多,一搜一大把,我装的是raspbian)
- 安装python3
- 准备四个步进电机,电机驱动,并组装连接好(我都是在某宝上买的,电机及其组件是在“亚博智能科技”买的,驱动是在“深圳市艾尔赛科技”上买的L298N四路电机驱动)
一些可能遇到的坑:
- 首先要注意电机驱动的输入电压,电压过低很可能无法带动电机(当然也不可过高)
- 电机驱动输入有一个GND,这个一定要连接树莓派的一个GND输出,否则可能出现电机驱动异常(我当时遇到的情况是:手接触驱动电路中的金属电机就可以正常转动,手一松开电机就不能正常运转了)
2. 基本思路:
- 将连接端口作为一个配置文件,该配置文件定义小车需要使用的端口
- 控制程序读取配置文件,并实现小车的方向控制
- 小车控制程序定义了私有的向各个方向移动的方法,然后定义一个方法接受方向参数,该方法内部再调用具体的移动方法
需要说明的是:由于使用引脚的不同,以及电机安装位置的差异,所以不能保证以下程序能够直接使用。
3. 配置文件内容如下(文件名为:config.ini):
[car] # This is the parmaters that will control the car's wheels # The number is the interface number of GPIO (GPIO.BOARD) LEFT_FRONT_1 = 7 LEFT_FRONT_2 = 11 RIGHT_FRONT_1 = 13 RIGHT_FRONT_2 = 15 LEFT_BEHIND_1 = 31 LEFT_BEHIND_2 = 33 RIGHT_BEHIND_1 = 35 RIGHT_BEHIND_2 = 37
4. 小车控制程序(文件名为:car_controler.py):
# coding=utf-8 import RPi.GPIO as GPIO import time import configparser class FourWheelDriveCar(): # Define the number of all the GPIO that will used for the 4wd car def __init__(self): ''' 1. Read pin number from configure file 2. Init all GPIO configureation ''' config = configparser.ConfigParser() config.read("config.ini") self.LEFT_FRONT_1 = config.getint("car", "LEFT_FRONT_1") self.LEFT_FRONT_2 = config.getint("car", "LEFT_FRONT_2") self.RIGHT_FRONT_1 = config.getint("car", "RIGHT_FRONT_1") self.RIGHT_FRONT_2 = config.getint("car", "RIGHT_FRONT_2") self.LEFT_BEHIND_1 = config.getint("car", "LEFT_BEHIND_1") self.LEFT_BEHIND_2 = config.getint("car", "LEFT_BEHIND_2") self.RIGHT_BEHIND_1 = config.getint("car", "RIGHT_BEHIND_1") self.RIGHT_BEHIND_2 = config.getint("car", "RIGHT_BEHIND_2") GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(self.LEFT_FRONT_1, GPIO.OUT) GPIO.setup(self.LEFT_FRONT_2, GPIO.OUT) GPIO.setup(self.RIGHT_FRONT_1, GPIO.OUT) GPIO.setup(self.RIGHT_FRONT_2, GPIO.OUT) GPIO.setup(self.LEFT_BEHIND_1, GPIO.OUT) GPIO.setup(self.LEFT_BEHIND_2, GPIO.OUT) GPIO.setup(self.RIGHT_BEHIND_1, GPIO.OUT) GPIO.setup(self.RIGHT_BEHIND_2, GPIO.OUT) def reset(self): # Rest all the GPIO as LOW GPIO.output(self.LEFT_FRONT_1, GPIO.LOW) GPIO.output(self.LEFT_FRONT_2, GPIO.LOW) GPIO.output(self.RIGHT_FRONT_1, GPIO.LOW) GPIO.output(self.RIGHT_FRONT_2, GPIO.LOW) GPIO.output(self.LEFT_BEHIND_1, GPIO.LOW) GPIO.output(self.LEFT_BEHIND_2, GPIO.LOW) GPIO.output(self.RIGHT_BEHIND_1, GPIO.LOW) GPIO.output(self.RIGHT_BEHIND_2, GPIO.LOW) def __forword(self): self.reset() GPIO.output(self.LEFT_FRONT_1, GPIO.HIGH) GPIO.output(self.LEFT_FRONT_2, GPIO.LOW) GPIO.output(self.RIGHT_FRONT_1, GPIO.HIGH) GPIO.output(self.RIGHT_FRONT_2, GPIO.LOW) GPIO.output(self.LEFT_BEHIND_1, GPIO.HIGH) GPIO.output(self.LEFT_BEHIND_2, GPIO.LOW) GPIO.output(self.RIGHT_BEHIND_1, GPIO.HIGH) GPIO.output(self.RIGHT_BEHIND_2, GPIO.LOW) def __backword(self): self.reset() GPIO.output(self.LEFT_FRONT_2, GPIO.HIGH) GPIO.output(self.LEFT_FRONT_1, GPIO.LOW) GPIO.output(self.RIGHT_FRONT_2, GPIO.HIGH) GPIO.output(self.RIGHT_FRONT_1, GPIO.LOW) GPIO.output(self.LEFT_BEHIND_2, GPIO.HIGH) GPIO.output(self.LEFT_BEHIND_1, GPIO.LOW) GPIO.output(self.RIGHT_BEHIND_2, GPIO.HIGH) GPIO.output(self.RIGHT_BEHIND_1, GPIO.LOW) def __turnLeft(self): ''' To turn left, the LEFT_FRONT wheel will move backword All other wheels move forword ''' self.reset() GPIO.output(self.LEFT_FRONT_2, GPIO.HIGH) GPIO.output(self.LEFT_FRONT_1, GPIO.LOW) GPIO.output(self.RIGHT_FRONT_1, GPIO.HIGH) GPIO.output(self.RIGHT_FRONT_2, GPIO.LOW) GPIO.output(self.LEFT_BEHIND_1, GPIO.HIGH) GPIO.output(self.LEFT_BEHIND_2, GPIO.LOW) GPIO.output(self.RIGHT_BEHIND_1, GPIO.HIGH) GPIO.output(self.RIGHT_BEHIND_2, GPIO.LOW) def __turnRight(self): ''' To turn right, the RIGHT_FRONT wheel move backword All other wheels move forword ''' self.reset() GPIO.output(self.LEFT_FRONT_1, GPIO.HIGH) GPIO.output(self.LEFT_FRONT_2, GPIO.LOW) GPIO.output(self.RIGHT_FRONT_2, GPIO.HIGH) GPIO.output(self.RIGHT_FRONT_1, GPIO.LOW) GPIO.output(self.LEFT_BEHIND_1, GPIO.HIGH) GPIO.output(self.LEFT_BEHIND_2, GPIO.LOW) GPIO.output(self.RIGHT_BEHIND_1, GPIO.HIGH) GPIO.output(self.RIGHT_BEHIND_2, GPIO.LOW) def __backLeft(self): ''' To go backword and turn left, the LEFT_BEHIND wheel move forword All other wheels move backword ''' self.reset() GPIO.output(self.LEFT_FRONT_2, GPIO.HIGH) GPIO.output(self.LEFT_FRONT_1, GPIO.LOW) GPIO.output(self.RIGHT_FRONT_2, GPIO.HIGH) GPIO.output(self.RIGHT_FRONT_1, GPIO.LOW) GPIO.output(self.LEFT_BEHIND_1, GPIO.HIGH) GPIO.output(self.LEFT_BEHIND_2, GPIO.LOW) GPIO.output(self.RIGHT_BEHIND_2, GPIO.HIGH) GPIO.output(self.RIGHT_BEHIND_1, GPIO.LOW) def __backRight(self): ''' To go backword and turn right, the RIGHT_BEHIND wheel move forword All other wheels move backword ''' self.reset() GPIO.output(self.LEFT_FRONT_2, GPIO.HIGH) GPIO.output(self.LEFT_FRONT_1, GPIO.LOW) GPIO.output(self.RIGHT_FRONT_2, GPIO.HIGH) GPIO.output(self.RIGHT_FRONT_1, GPIO.LOW) GPIO.output(self.LEFT_BEHIND_2, GPIO.HIGH) GPIO.output(self.LEFT_BEHIND_1, GPIO.LOW) GPIO.output(self.RIGHT_BEHIND_1, GPIO.HIGH) GPIO.output(self.RIGHT_BEHIND_2, GPIO.LOW) def __stop(self): self.reset() def carMove(self, direction): ''' Car move according to the input paramter - direction ''' if direction == 'F': self.__forword() elif direction == 'B': self.__backword() elif direction == 'L': self.__turnLeft() elif direction == 'R': self.__turnRight() elif direction == 'BL': self.__backLeft() elif direction == 'BR': self.__backRight() elif direction == 'S': self.__stop() else: print("The input direction is wrong! You can just input: F, B, L, R, BL,BR or S") if __name__ == "__main__": raspCar = FourWheelDriveCar() while(True): direction = input("Please input direction: ") raspCar.carMove(direction)
加载全部内容