目录
0. 写在前面:
1. 准备:
2. 基本思路:
3. 配置文件内容如下(文件名为:config.ini):
4. 小车控制程序(文件名为:car_controler.py):
一两个月前偶然得到一个树莓派一代,发现还是挺强大的,然后就考虑着自己实现一个四驱小车。因为本身是学软件的,所以利用软件来控制实实在在可见的硬件一直是自己的一个梦想。所以当时也是花了很多时间来琢磨、实验小车控制方面的东西。因为时间过去比较久了,这里也就只是记录一下当时踩过的坑、实现的一些思路以及绝大部分源代码。截止当前,我的树莓派小车实现的主要功能是:
(本文只是介绍上述的第一个功能,其他功能的实现在后续的博文中介绍)
一些可能遇到的坑:
需要说明的是:由于使用引脚的不同,以及电机安装位置的差异,所以不能保证以下程序能够直接使用。
[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
# 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)