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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351 | # -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import socket
import time
# import string
# import threading
# 小车状态值定义
STATE_STOP = 0;
STATE_FORWORD = 1;
STATE_BACK = 2;
STATE_LEFT = 3;
STATE_RIGHT = 4;
STATE_LEFT_FRONT = 5;
STATE_RIGHT_FRONT = 6;
STATE_LEFT_BACK = 7;
STATE_RIGHT_BACK = 8;
STATE_CLOCKWISE = 9;
STATE_COUNTERCLOCKWISE = 10;
STATE_TEST = 11;
# 小车点击引脚定义; 小车电机按照顺时针方向 1,2,3,4 排列
# 左前方 白紫蓝
MOTOR_LEFT_FRONT_A = 22
MOTOR_LEFT_FRONT_B = 27
MOTOR_LEFT_FRONT_PWM = 17
# 右前方 绿黄橙
MOTOR_RIGHT_FRONT_A = 5
MOTOR_RIGHT_FRONT_B = 6
MOTOR_RIGHT_FRONT_PWM = 13
# 右后方 绿黄橙
MOTOR_RIGHT_BACK_A = 12
MOTOR_RIGHT_BACK_B = 16
MOTOR_RIGHT_BACK_PWM = 20
# 左后方 白紫蓝
MOTOR_LEFT_BACK_A = 19
MOTOR_LEFT_BACK_B = 26
MOTOR_LEFT_BACK_PWM = 21
# 设置GPIO口为BCM编码方式
GPIO.setmode(GPIO.BCM)
# 忽略警告信息
GPIO.setwarnings(False)
class Motor:
"""电机对象"""
def __init__(self,motorA,motorB,motorPwm):
self.motorA = motorA;
self.motorB = motorB;
self.motorPwm = motorPwm;
self.pwm = None;
self.init_motor()
# 小车速度变量
self.carSpeed = 50
def init_motor(self):
GPIO.setup(self.motorA, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(self.motorB, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(self.motorPwm, GPIO.OUT, initial=GPIO.HIGH)
self.pwm = GPIO.PWM(self.motorPwm, 2000)
self.pwm.start(0)
def forward(self,value=0):
GPIO.setup(self.motorA, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(self.motorB, GPIO.OUT, initial=GPIO.LOW)
self.pwm.ChangeDutyCycle(self.carSpeed)
def back(self,value=0):
GPIO.setup(self.motorA, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(self.motorB, GPIO.OUT, initial=GPIO.HIGH)
self.pwm.ChangeDutyCycle(self.carSpeed)
def stop(self):
GPIO.setup(self.motorA, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(self.motorB, GPIO.OUT, initial=GPIO.LOW)
self.pwm.ChangeDutyCycle(0)
class HeiCar:
def __init__(self):
self.currentState = 0;
# 按照顺时针的方向
# 左前方
self.motor1 = Motor(MOTOR_LEFT_FRONT_A, MOTOR_LEFT_FRONT_B, MOTOR_LEFT_FRONT_PWM)
# 右前方
self.motor2 = Motor(MOTOR_RIGHT_FRONT_A, MOTOR_RIGHT_FRONT_B, MOTOR_RIGHT_FRONT_PWM)
# 右后方
self.motor3 = Motor(MOTOR_RIGHT_BACK_A, MOTOR_RIGHT_BACK_B, MOTOR_RIGHT_BACK_PWM);
# 左后方
self.motor4 = Motor(MOTOR_LEFT_BACK_A, MOTOR_LEFT_BACK_B, MOTOR_LEFT_BACK_PWM);
self.motors = [self.motor1,self.motor2,self.motor3,self.motor4];
def forword(self):
""" 小车前进"""
for motor in self.motors:
motor.forward();
def back(self):
"""小车后退"""
for motor in self.motors:
motor.back();
def stop(self):
"""停止"""
for motor in self.motors:
motor.stop();
def left(self):
"""
小车向左边 横着走
"""
self.motor1.back()
self.motor4.forward();
self.motor2.forward()
self.motor3.back()
def right(self):
""" 小车向右边 横着走"""
self.motor1.forward()
self.motor4.back()
self.motor2.back()
self.motor3.forward()
def left_front(self):
""" 向左前方 45度 """
self.motor1.stop()
self.motor4.forward()
self.motor2.forward()
self.motor3.stop()
def right_front(self):
""" 向右前方 45度 """
self.motor1.forward()
self.motor4.stop()
self.motor2.stop()
self.motor3.forward()
def left_back(self):
""" 向左后45 度"""
self.motor1.back()
self.motor4.stop()
self.motor2.stop()
self.motor3.back()
def right_back(self):
""" 右后 45 度"""
self.motor1.stop()
self.motor4.back()
self.motor2.back()
self.motor3.stop()
def clockwise(self):
""" 顺时针"""
self.motor1.forward()
self.motor4.forward()
self.motor2.back()
self.motor3.back()
def counterclockwise(self):
""" 逆时针 """
self.motor1.back()
self.motor4.back()
self.motor2.forward()
self.motor3.forward()
try:
car = HeiCar();
global connectflag
connectflag = 0
count = 50
# 通过socket创建监听套接字并设置为非阻塞模式
tcpservicesock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpservicesock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpservicesock.setblocking(0)
# 填充绑定服务器的ip地址和端口号
# 注意:这里一定要根据自己的树莓派的ip地址来填
tcpservicesock.bind(('192.168.12.1', 8888))
# 监听客户端的连接
tcpservicesock.listen(5)
print("waiting for connection....")
# 创建监听列表
clientAddrList = []
print("服务器已经开启啦!")
while True:
try:
# 准备接受客户端的连接并返回连接套接字
tcpclientsock, addr = tcpservicesock.accept()
if tcpclientsock:
connectflag = 1
print("连接客户端成功啦!")
except:
pass
else:
# print("new user :%s " % str(addr))
# 设置连接套接字为非阻塞的模式并将连接的套接字放入到监听列表中
tcpclientsock.setblocking(0)
clientAddrList.append((tcpclientsock, addr))
for tcpclientsock, addr in clientAddrList:
try:
global recvbuf
global sendbuf
recvbuf = ''
# TCP接受数据
# print("Start recv!",addr)
recvbuf = tcpclientsock.recv(128)
# print("Start recv over!")
except:
pass
else:
if len(recvbuf) > 0:
# print("recvbuf", recvbuf)
state = int(recvbuf);
car.currentState = state;
# 根据解析的数据让小车做出相应的运动
if state == STATE_STOP:
print("=====停止1=====")
car.stop()
elif state == STATE_FORWORD:
print("=====前进=====")
car.forword()
elif state == STATE_BACK:
print("=====后退=====")
car.back()
elif state == STATE_LEFT:
print("=====左移=====")
car.left()
elif state == STATE_RIGHT:
print("=====右移=====")
car.right()
elif state == STATE_LEFT_FRONT:
print("=====左前方=====")
car.left_front()
elif state == STATE_RIGHT_FRONT:
print("=====右前方=====")
car.right_front()
elif state == STATE_LEFT_BACK:
print("=====左后方=====")
car.left_back()
elif state == STATE_RIGHT_BACK:
print("=====右后方=====")
car.right_back()
elif state == STATE_CLOCKWISE:
print("=====顺时针=====")
car.clockwise()
elif state == STATE_COUNTERCLOCKWISE:
print("=====逆时针=====")
car.counterclockwise()
elif state == STATE_TEST:
car.forword()
time.sleep(0.5)
car.stop()
time.sleep(0.5)
car.back()
time.sleep(0.5)
car.stop()
car.left()
time.sleep(0.5)
car.stop()
time.sleep(0.5)
car.right()
time.sleep(0.5)
car.stop()
time.sleep(0.5)
car.left_front()
time.sleep(0.5)
car.stop()
time.sleep(0.5)
car.right_back()
time.sleep(0.5)
car.stop()
time.sleep(0.5)
car.right_front()
time.sleep(0.5)
car.stop()
time.sleep(0.5)
car.left_back()
time.sleep(0.5)
car.stop()
time.sleep(0.5)
car.clockwise()
time.sleep(0.5)
car.stop()
time.sleep(0.5)
car.counterclockwise()
time.sleep(0.5)
else:
print("=====停止2=====")
car.stop()
# 让小车运动0.5s 即停止
time.sleep(0.5)
car.stop()
time.sleep(0.5)
else:
tcpclientsock.close()
clientAddrList.remove((tcpclientsock, addr))
car.stop()
except KeyboardInterrupt:
pass
tcpclientsock.close()
tcpservicesock.close()
GPIO.cleanup()
|