-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_car.py
211 lines (200 loc) · 5.89 KB
/
wifi_car.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
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
#-*- coding:UTF-8 -*-
import RPi.GPIO as GPIO
import thread
import serial
import socket
import time
import threading
from threading import Thread
import os
import socket,fcntl,struct
import signal
import atexit
atexit.register(GPIO.cleanup)
GPIO.setmode(GPIO.BOARD)
ser=serial.Serial('/dev/ttyAMA0',9600,timeout=1)
data=''
SLEEP_TIME=0.1
#定义GPIO信号
INT1=11
INT2=12
INT3=13
INT4=15
INT_CHAOSHENG_TRI=3
INT_CHAOSHENG_ECHO=5
CHAOSHENG_VALUE=-1
flag_chao=[0]
#定义角度
jiaodu=0
#i=0
GPIO.setup(INT_CHAOSHENG_TRI,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(INT_CHAOSHENG_ECHO,GPIO.IN)
GPIO.setup(38,GPIO.OUT,initial=False)
p1=GPIO.PWM(38,50)
GPIO.setup(40,GPIO.OUT,initial=False)
p2=GPIO.PWM(40,50)
p1.start(0)
p2.start(0)
def print_time(threadName,delay,temp_var):
count=0
while count <5 and temp_var[0]==1:
if(temp_var[0]==1):
temp_distance1=checkdist()
temp_distance2=checkdist()
temp_distance3=checkdist()
temp_distance4=checkdist()
temp_distance5=checkdist()
total_distance=temp_distance1+temp_distance2+temp_distance3+temp_distance4+temp_distance5
temp_distance=(total_distance)/5.0
#print "超声波测定距离为:%f"%temp_distance
if (temp_distance<=0.3 ):
go_back(0.5)
go_left(0.5)
go_stop(SLEEP_TIME)
print "障碍物太近,已调整方向"
#获取ip
def get_ip(ifname):
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(),0x8915,struct.pack('256s',ifname[:15]))[20:24])
#tcp连接
def tcplink(sock,addr):
sock.send('欢迎使用观月堂小车系统!')
i=0
while True:
data=sock.recv(1024)
time.sleep(0.1)
if data=='exit' or not data:
break
#sock.send('%s!'%data)
if data=='2':
flag_chao[0]=1
thread.start_new_thread(print_time,("Thread-1",0.1,flag_chao))
go_forward(SLEEP_TIME)
print "前进"
elif data=='5':
flag_chao[0]=0
go_back(SLEEP_TIME)
print "后退"
elif data=='4':
flag_chao[0]=0
go_left(SLEEP_TIME)
print "左转"
elif data=='6':
flag_chao[0]=0
go_right(SLEEP_TIME)
print "右转"
elif data=='1':
flag_chao[0]=0
go_stop(SLEEP_TIME)
print "刹车"
elif data=='a':
i=(i+10)%180
p1.ChangeDutyCycle(2.5+10*i/180)
time.sleep(0.02)
p1.ChangeDutyCycle(0)
time.sleep(0.2)
elif data=='b':
i=(i-10)%180
p1.ChangeDutyCycle(2.5+10*i/180)
time.sleep(0.02)
p1.ChangeDutyCycle(0)
time.sleep(0.2)
elif data=='c':
i=(i+10)%180
p2.ChangeDutyCycle(2.5+10*i/180)
time.sleep(0.02)
p2.ChangeDutyCycle(0)
time.sleep(0.2)
elif data=='d':
i=(i-10)%180
p2.ChangeDutyCycle(2.5+10*i/180)
time.sleep(0.02)
p2.ChangeDutyCycle(0)
time.sleep(0.2)
else:
pass
sock.close()
print '来自 %s:%s 的连接已中断.'%addr
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#IP连接('192.168.0.101',9999)
my_ip=get_ip('wlan0')
s.bind((my_ip,9999))
s.listen(5)
def checkdist():
GPIO.output(INT_CHAOSHENG_TRI,GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(INT_CHAOSHENG_TRI,GPIO.LOW)
while not GPIO.input(INT_CHAOSHENG_ECHO):
pass
t1=time.time()
while GPIO.input(INT_CHAOSHENG_ECHO):
pass
t2=time.time()
return (t2-t1)*340/2
def recv(serial):
while True:
data=serial.read()
if data=='':
continue
else:
break
sleep(0.02)
serial.flushInput()
return data
#初始化
def init():
GPIO.setup(INT1,GPIO.OUT)
GPIO.setup(INT2,GPIO.OUT)
GPIO.setup(INT3,GPIO.OUT)
GPIO.setup(INT4,GPIO.OUT)
#前进
def go_forward(sleep_time):
GPIO.output(INT1,GPIO.HIGH)
GPIO.output(INT2,GPIO.LOW)
GPIO.output(INT3,GPIO.HIGH)
GPIO.output(INT4,GPIO.LOW)
time.sleep(sleep_time)
#后退
def go_back(sleep_time):
GPIO.output(INT2,GPIO.HIGH)
GPIO.output(INT1,GPIO.LOW)
GPIO.output(INT4,GPIO.HIGH)
GPIO.output(INT3,GPIO.LOW)
time.sleep(sleep_time)
#左转
def go_left(sleep_time):
GPIO.output(INT1,GPIO.HIGH)
GPIO.output(INT2,GPIO.LOW)
GPIO.output(INT3,GPIO.LOW)
GPIO.output(INT4,GPIO.HIGH)
time.sleep(sleep_time)
#右转
def go_right(sleep_time):
GPIO.output(INT1,GPIO.LOW)
GPIO.output(INT2,GPIO.HIGH)
GPIO.output(INT3,GPIO.HIGH)
GPIO.output(INT4,GPIO.LOW)
time.sleep(sleep_time)
#刹车
def go_stop(sleep_time):
GPIO.output(INT1,False)
GPIO.output(INT2,False)
GPIO.output(INT3,False)
GPIO.output(INT4,False)
init();
go_stop(1)
i=0
try:
while True:
i=0
#thread.start_new_thread(print_time,("Thread-1",0.1,flag_chao))
sock,addr=s.accept()
t=threading.Thread(target=tcplink,args=(sock,addr))
#print "i:%d"%i
i=i+1
t.start()
except KeyboardInterrupt:
sock.close()
s.close()
ser.close();
GPIO.cleanup()