-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTT_RaspCarDec.py
279 lines (242 loc) · 5.96 KB
/
MQTT_RaspCarDec.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
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
import RPi.GPIO as GPIO
import time
import paho.mqtt.client as mqtt
import threading
time.sleep(1)
ENABLE_PRINT = 1
in1 = 17
in2 = 18
in3 = 27
in4 = 22
bled = 10
buzz = 23
rled1 = 25
rled2 = 24
ena = 13
enb = 12
pwm1 = 20
pwm2 = 40
pwm3 = 60
pwm4 = 80
pwm5 = 100
speedpwm = 100
print("Initializing GPIO pins.")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(ena, GPIO.OUT)
GPIO.setup(enb, GPIO.OUT)
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)
GPIO.setup(in3, GPIO.OUT)
GPIO.setup(in4, GPIO.OUT)
p1 = GPIO.PWM(ena, 1000)
p2 = GPIO.PWM(enb, 1000)
p1.start(pwm3)
p2.start(pwm3)
GPIO.setup(bled, GPIO.OUT)
GPIO.setup(buzz, GPIO.OUT)
GPIO.setup(rled1, GPIO.OUT)
GPIO.setup(rled2, GPIO.OUT)
GPIO.output(buzz, GPIO.HIGH)
GPIO.output(bled, GPIO.HIGH)
GPIO.output(rled1, GPIO.HIGH)
GPIO.output(rled2, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(buzz, GPIO.LOW)
GPIO.output(bled, GPIO.LOW)
GPIO.output(rled1, GPIO.LOW)
GPIO.output(rled2, GPIO.LOW)
def destroy():
print("\ndestroy")
GPIO.output(ena, GPIO.LOW)
GPIO.output(enb, GPIO.LOW)
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.LOW)
GPIO.output(in3, GPIO.LOW)
GPIO.output(in4, GPIO.LOW)
GPIO.output(rled2, GPIO.LOW)
GPIO.output(rled1, GPIO.LOW)
GPIO.output(buzz, GPIO.HIGH)
GPIO.output(rled2, GPIO.HIGH)
GPIO.output(rled1, GPIO.HIGH)
time.sleep(1)
GPIO.output(buzz, GPIO.LOW)
GPIO.output(rled2, GPIO.LOW)
GPIO.output(rled1, GPIO.LOW)
GPIO.cleanup()
def forward():
print("\nforward")
p1.ChangeDutyCycle(speedpwm)
p2.ChangeDutyCycle(speedpwm)
GPIO.output(rled2, GPIO.LOW)
GPIO.output(rled1, GPIO.LOW)
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.HIGH)
GPIO.output(in3, GPIO.LOW)
GPIO.output(in4, GPIO.HIGH)
def reverse():
print("\nreverse")
p1.ChangeDutyCycle(speedpwm)
p2.ChangeDutyCycle(speedpwm)
GPIO.output(rled2, GPIO.LOW)
GPIO.output(rled1, GPIO.LOW)
GPIO.output(in1, GPIO.HIGH)
GPIO.output(in2, GPIO.LOW)
GPIO.output(in3, GPIO.HIGH)
GPIO.output(in4, GPIO.LOW)
def turn_left():
print("\nturn_left")
p1.ChangeDutyCycle(speedpwm)
p2.ChangeDutyCycle(speedpwm)
GPIO.output(rled1, GPIO.LOW)
GPIO.output(in1, GPIO.HIGH)
GPIO.output(in2, GPIO.LOW)
GPIO.output(in3, GPIO.LOW)
GPIO.output(in4, GPIO.HIGH)
GPIO.output(rled2, GPIO.HIGH)
def turn_right():
print("\nturn_right")
p2.ChangeDutyCycle(speedpwm)
p1.ChangeDutyCycle(speedpwm)
GPIO.output(rled2, GPIO.LOW)
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.HIGH)
GPIO.output(in3, GPIO.HIGH)
GPIO.output(in4, GPIO.LOW)
GPIO.output(rled1, GPIO.HIGH)
def stop():
print("\nstop")
GPIO.output(rled2, GPIO.LOW)
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.LOW)
GPIO.output(in3, GPIO.LOW)
GPIO.output(in4, GPIO.LOW)
GPIO.output(rled1, GPIO.LOW)
def honk():
print("\nhonk")
GPIO.output(buzz, GPIO.HIGH)
time.sleep(1)
GPIO.output(buzz, GPIO.LOW)
def speed_one():
global speedpwm
global pwm1
print("\nspeed_one")
speedpwm = pwm1
def speed_two():
global speedpwm
global pwm2
print("\nspeed_two")
speedpwm = pwm2
def speed_three():
global speedpwm
global pwm3
print("\nspeed_three")
speedpwm = pwm3
def speed_four():
global speedpwm
global pwm4
print("\nspeed_three")
speedpwm = pwm4
def speed_five():
global speedpwm
global pwm5
print("\nspeed_three")
speedpwm = pwm5
def speed_changer(pcmd, scmd):
if pcmd == "f":
scmd()
forward()
elif pcmd == "r":
scmd()
reverse()
elif pcmd == "tl":
scmd()
turn_left()
elif pcmd == "tr":
scmd()
turn_right()
elif pcmd == "s":
scmd()
stop()
text = 0
def on_message(client, userdata, message):
global text
string_message = str(message.payload.decode("utf-8"))
text = string_message
print(text)
broker_address = "proxima.boldmoon.in"
client = mqtt.Client("RV3")
client.username_pw_set(username="uname", password="pwd")
client.on_message = on_message
time.sleep(0.5)
client.connect(broker_address)
client.loop_start()
print("Connected to Server.")
time.sleep(0.5)
client.subscribe("V3")
time.sleep(0.5)
print("Ready to drive")
GPIO.output(buzz, GPIO.HIGH)
GPIO.output(rled2, GPIO.HIGH)
GPIO.output(rled1, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(buzz, GPIO.LOW)
GPIO.output(rled2, GPIO.LOW)
GPIO.output(rled1, GPIO.LOW)
time.sleep(0.2)
GPIO.output(buzz, GPIO.HIGH)
GPIO.output(rled2, GPIO.HIGH)
GPIO.output(rled1, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(buzz, GPIO.LOW)
GPIO.output(rled2, GPIO.LOW)
GPIO.output(rled1, GPIO.LOW)
prevcmd = "s"
while True:
if text == "h":
h1 = threading.Thread(target=honk)
h1.start()
text = 0
elif text == "f":
forward()
prevcmd = "f"
text = 0
elif text == "r":
reverse()
prevcmd = "r"
text = 0
elif text == "tl":
stop()
turn_left()
prevcmd = "tl"
text = 0
elif text == "tr":
stop()
turn_right()
prevcmd = "tr"
text = 0
elif text == "s":
stop()
prevcmd = "s"
text = 0
elif text == "s1":
speed_changer(prevcmd, speed_one)
text = 0
elif text == "s2":
speed_changer(prevcmd, speed_two)
text = 0
elif text == "s3":
speed_changer(prevcmd, speed_three)
text = 0
elif text == "s4":
speed_changer(prevcmd, speed_four)
text = 0
elif text == "s5":
speed_changer(prevcmd, speed_five)
text = 0
elif text == "d":
destroy()
text = 0
break
print("Bye")
client.loop_stop()