树莓派自动小车

先秀一波小车

logo

本文主要使用的微信小程序作为遥控,python作为服务器

后端框架图如下:

logo

数据库用mysql,服务采用flask框架。
分别利用左右轮胎的转速来控制小车的转向
具体控制小车行动的代码如下(截取了片段):

__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import RPi.GPIO as GPIO
def ctrl_id():
#开启接口
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
#控制方向
if id == 't_left':
t_left()
time.sleep(0.5)
t_stop()
return jsonify({"msg":"left"})
elif id == 't_right':
t_right()
time.sleep(0.5)
t_stop()
return jsonify({"msg":"right"})
elif id == 't_up':
t_up()
time.sleep(0.5)
t_stop()
return jsonify({"msg":"up"})
elif id == 't_down':
t_down()
time.sleep(0.5)
t_stop()
return jsonify({"msg" : "down"})
elif id == 't_stop':
t_stop()
return jsonify({"msg" : "stop"})
# 小车停止
def t_stop():
GPIO.output(12, False)
GPIO.output(16, False)
GPIO.output(18, False)
GPIO.output(22, False)
# 小车向前
def t_up():
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
GPIO.output(22, False)
# 小车向后
def t_down():
GPIO.output(12, False)
GPIO.output(16, True)
GPIO.output(18, False)
GPIO.output(22, True)
# 小车向左
def t_left():
GPIO.output(12, False)
GPIO.output(16, True)
GPIO.output(18, True)
GPIO.output(22, False)
# 小车向右
def t_right():
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, False)
GPIO.output(22, True)

至于云台舵机,写了Steer.py文件去控制,具体可以看代码

人脸跟踪拍照

代码具体如下:

face.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import cv2
from main.Steer import Steering
import RPi.GPIO as GPIO
import time

def init():
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)


def left_up():
GPIO.output(12, True)
GPIO.output(16, True)
GPIO.output(18, True)
GPIO.output(22, False)


def t_up():
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
GPIO.output(22, False)

def right_up():
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
GPIO.output(22, True)


def t_down():
GPIO.output(12, False)
GPIO.output(16, True)
GPIO.output(18, False)
GPIO.output(22, True)


def right_down():
GPIO.output(12, False)
GPIO.output(16, True)
GPIO.output(18, True)
GPIO.output(22, True)

def left_down():
GPIO.output(12, True)
GPIO.output(16, True)
GPIO.output(18, False)
GPIO.output(22, True)


def t_left():
GPIO.output(12, False)
GPIO.output(16, True)
GPIO.output(18, True)
GPIO.output(22, False)


def t_right():
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, False)
GPIO.output(22, True)


def t_stop():
GPIO.output(12, False)
GPIO.output(16, False)
GPIO.output(18, False)
GPIO.output(22, False)

detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
steer=Steering(3,0,180,5,0,180,90,80) #初始位置为36和160,此时云台是正对前方,通过调试得到这两个值
steer.setup()
a=20
b=80
steer.specify(a,b)
f=0
while True and not f:
ret, img = cap.read()
shape=img.shape
height= shape[0]
width = shape[1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
if not f:
if w * h < 80 * 80:
init()
if y + h / 2 < height / 4:
t_down()
else:
t_up()
if x+w/2 < width/4:
t_right()
elif x+w/2 > width/4*3:
t_left()
time.sleep(0.1)
t_stop()
elif w * h > 200 * 200:
init()
t_down()
time.sleep(0.1)
t_stop()
else:
# 往左边
if x+w/2 < width/2 and b < 180:
b += 3
# 往右边
elif b > 0:
b -= 3
# 往前
if y+h/2 < height/2 and a > 0:
a -= 3
elif a < 90:
a += 3

if abs(x+w/2 - width/2) < 90 and abs(y+h/2 - height/2) < 90 and w*h < 200*200 and w*h > 80*80:
cv2.imwrite("1.jpg", img)
f = 1
break
steer.specify(a, b)
cv2.imshow('frame', img)
cv2.waitKey(100)
print(faces)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

微信小程序

logo

小程序实现了遥控、拍照、温湿度等功能,代码详见github

本项目代码已开源

树莓派源码 微信小程序源码