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 | #-*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import socket
import time
import string
import threading
#小车状态值定义
enSTOP = 0
enRUN =1
enBACK = 2
enLEFT = 3
enRIGHT = 4
enTLEFT =5
enTRIGHT = 6
#小车电机引脚定义
IN1 = 20
IN2 = 21
IN3 = 19
IN4 = 26
ENA = 16
ENB = 13
#小车和舵机状态变量
g_CarState = 0
#小车速度变量
CarSpeedControl = 35
# 小车转向速度变量
CarDirectSpeedControl=80
#设置GPIO口为BCM编码方式
GPIO.setmode(GPIO.BCM)
#忽略警告信息
GPIO.setwarnings(False)
#电机引脚初始化为输出模式
def init():
global pwm_ENA
global pwm_ENB
GPIO.setup(ENA,GPIO.OUT,initial=GPIO.HIGH)
GPIO.setup(IN1,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(IN2,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(ENB,GPIO.OUT,initial=GPIO.HIGH)
GPIO.setup(IN3,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(IN4,GPIO.OUT,initial=GPIO.LOW)
#设置pwm引脚和频率为2000hz
pwm_ENA = GPIO.PWM(ENA, 2000)
pwm_ENB = GPIO.PWM(ENB, 2000)
pwm_ENA.start(0)
pwm_ENB.start(0)
#小车前进
def run():
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(CarSpeedControl)
pwm_ENB.ChangeDutyCycle(CarDirectSpeedControl)
#小车后退
def back():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(CarSpeedControl)
pwm_ENB.ChangeDutyCycle(CarDirectSpeedControl)
#小车左转
def left():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(CarSpeedControl)
pwm_ENB.ChangeDutyCycle(CarDirectSpeedControl)
#小车右转
def right():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
pwm_ENA.ChangeDutyCycle(CarSpeedControl)
pwm_ENB.ChangeDutyCycle(CarDirectSpeedControl)
#小车原地左转
def spin_left():
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(CarSpeedControl)
pwm_ENB.ChangeDutyCycle(CarSpeedControl)
#小车原地右转
def spin_right():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
pwm_ENA.ChangeDutyCycle(CarSpeedControl)
pwm_ENB.ChangeDutyCycle(CarSpeedControl)
#小车停止
def brake():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
try:
init()
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)
g_CarState = int(recvbuf);
# 根据解析的数据让小车做出相应的运动
if g_CarState == enSTOP:
brake()
elif g_CarState == enRUN:
run()
elif g_CarState == enLEFT:
print("左转")
left()
elif g_CarState == enRIGHT:
right()
elif g_CarState == enBACK:
back()
elif g_CarState == enTLEFT:
spin_left()
elif g_CarState == enTRIGHT:
spin_right()
else:
brake()
else:
tcpclientsock.close()
clientAddrList.remove((tcpclientsock,addr))
except KeyboardInterrupt:
pass
tcpclientsock.close()
tcpservicesock.close()
pwm_ENA.stop()
pwm_ENB.stop()
GPIO.cleanup()
|