-
Notifications
You must be signed in to change notification settings - Fork 1
/
RPiMower.py
executable file
·362 lines (288 loc) · 9.71 KB
/
RPiMower.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
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
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python
__author__ = "Bernd Gewehr"
# import python libraries
import logging
import os
import signal
import sys
import time
import random
import numpy as np
# import RPiMower libraries
import lib_l298n as move
# import RPiMower actions
import act_uln2003 as VSS
import act_esc as ESC
# import RPiMower libraries
import lib_mqtt as MQTT
# Initialize variables
APPNAME = os.path.splitext(os.path.basename(__file__))[0]
LOGFILE = os.getenv('LOGFILE', APPNAME + '.log')
DEBUG = False
MQTT_TOPIC_IN = "/RPiMower/#"
MQTT_QOS = 0
STOP = "/RPiMower/stop"
START = "/RPiMower/start"
TURN = "/RPiMower/turn"
HOME = "/RPiMower/return"
Stop = False
MIN_DISTANCE = 20
WORLD = ["FrontUS",0,"BackUS",0,"GroundColour",0, "Compass",0, "Pitch",0, "Roll",0, "map",[], "UserCMD","","Running",0]
W_FRONT_SONAR = 1
W_BACK_SONAR = 3
W_GROUND_COLOUR = 5
W_COMPASS = 7
W_PITCH = 9
W_ROLL = 11
W_MAP = 13
W_USERCMD = 15
W_RUNNING = 17
WEST_FENCE = 90
NORTH_FENCE = 180
data=[]
# Initialize logging
LOGFORMAT = '%(asctime)-15s %(levelname)-5s %(message)s'
if DEBUG:
logging.basicConfig(filename=LOGFILE,
level=logging.DEBUG,
format=LOGFORMAT)
else:
logging.basicConfig(filename=LOGFILE,
level=logging.INFO,
format=LOGFORMAT)
logging.info("Starting " + APPNAME)
logging.info("INFO MODE")
logging.debug("DEBUG MODE")
logging.debug("LOGFILE = %s" % LOGFILE)
def output():
os.system('cls' if os.name == 'nt' else 'clear')
print "FrontUS: %-5s Compass: %-5s\nPitch: %-5s Roll: %-5s\nGroundCOLOR: %-5s\nRunning: %-5s" % (WORLD[W_FRONT_SONAR], WORLD[W_COMPASS], WORLD[W_PITCH], WORLD[W_ROLL], WORLD[W_GROUND_COLOUR], WORLD[W_RUNNING]), "\n"
def on_message(mosq, obj, msg):
"""
Handle incoming messages
"""
if DEBUG:
print msg.topic, msg.payload
if msg.topic == START:
logging.debug("RPiMower start...")
WORLD[W_USERCMD] = "start"
elif msg.topic == STOP:
logging.debug("RPiMower stop...")
WORLD[W_USERCMD] = "stop"
elif msg.topic == TURN:
logging.debug("RPiMower turn...")
WORLD[W_USERCMD] = "turn"
elif msg.topic == HOME:
logging.debug("RPiMower return...")
WORLD[W_USERCMD] = "return"
topicparts = msg.topic.split("/")
# if DEBUG:
# for i in range(0,len(topicparts)-1):
# print i, topicparts[i]
if topicparts[2] == "FrontUS":
WORLD[W_FRONT_SONAR] = msg.payload
if topicparts[2] == "Ground_Colour":
WORLD[W_GROUND_COLOUR] = msg.payload
if topicparts[2] == "Compass":
WORLD[W_COMPASS] = int(float(msg.payload))
if topicparts[2] == "Pitch":
WORLD[W_PITCH] = int(float(msg.payload))
if topicparts[2] == "Roll":
WORLD[W_ROLL] = int(float(msg.payload))
# End of MQTT callbacks
def cleanup(signum, frame):
"""
Signal handler to ensure we disconnect cleanly
in the event of a SIGTERM or SIGINT.
"""
# Cleanup modules
logging.debug("Clean up modules")
move.cleanup()
VSS.cleanup()
MQTT.cleanup()
# Exit from application
logging.info("Exiting on signal %d" % (signum))
sys.exit(signum)
def detect_blocking(point):
while len(data) > 10:
data.pop()
data.insert(0,point)
if len(data) < 3:
return False
std = np.std(np.array(data).astype(np.float))
#print std, data
return std < 0.5
def compass_turn(target):
DC = 20
while abs(target - WORLD[W_COMPASS]) > 20:
output()
print "from - to: ", WORLD[W_COMPASS], target
if WORLD[W_COMPASS] < target:
if abs(WORLD[W_COMPASS] - target)<180:
#Rechtsrum ist kuerzester Weg
move.right180(DC, DC)
else:
#Linksrum ist kuerzester Weg
move.left180(DC, DC)
else:
if abs(WORLD[W_COMPASS] - target)<180:
#Linksrum ist kuerzester Weg
move.left180(DC, DC)
else:
#Rechtsrum ist kuerzester Weg
move.right180(DC, DC)
time.sleep(0.02)
move.stop()
time.sleep(0.02)
def compass_turn_rel(angle):
compass_turn(WORLD[W_COMPASS] + angle)
def build_map():
global WORLD
move.stop()
# wait for the first real compass result
time.sleep(2)
current_angle = WORLD[W_COMPASS]
print "Starting at angle: ", current_angle
for angle in range(current_angle, current_angle + 359, 10):
if angle > 360:
target = angle - 360
else:
target = angle
print "Turning to angle: ", target
compass_turn(target)
output()
print "World Map: ", WORLD[W_COMPASS], WORLD[W_FRONT_SONAR]
WORLD[W_MAP].append([WORLD[W_COMPASS], float(WORLD[W_FRONT_SONAR])+47])
#print WORLD[W_MAP]
WORLD_CARTESIAN = [[int(np.cos(np.radians(i[0]))*float(i[1])*10)/10, int(np.sin(np.radians(i[0]))*float(i[1])*10)/10] for i in WORLD[W_MAP]]
#print WORLD_CARTESIAN
MQTT.mqttc.publish("/RPiMower/World/Cartesian", str(WORLD_CARTESIAN))
move.stop()
WORLD[W_USERCMD]="start"
def return_home():
global Stop
move.stop()
compass_turn(NORTH_FENCE)
i = float(WORLD[W_FRONT_SONAR])
while (i < 50.0):
output()
i = float(WORLD[W_FRONT_SONAR])
print "Getting in 50cm distance to north fence. Current distance: ", i
time.sleep(.05)
if (abs(WORLD[W_COMPASS] - NORTH_FENCE) > 15):
compass_turn(NORTH_FENCE)
move.backward()
move.stop()
while (i > 50.0):
output()
i = float(WORLD[W_FRONT_SONAR])
print "Getting in 50cm distance to north fence. Current distance: ", i
time.sleep(.05)
if (abs(WORLD[W_COMPASS] - NORTH_FENCE) > 15):
compass_turn(NORTH_FENCE)
move.forward()
move.stop()
compass_turn(WEST_FENCE)
while (i > 15.0):
output()
print "Getting in 15cm distance to west fence. Current distance: ", i
i = float(WORLD[W_FRONT_SONAR])
time.sleep(.05)
if (abs(WORLD[W_COMPASS] - WEST_FENCE - 90) > 10):
compass_turn(WEST_FENCE - 90)
move.forward()
move.stop()
compass_turn(WEST_FENCE + 90)
while i > 15.0:
output()
print "Getting in 50cm distance to north fence. Current distance: ", i
i = float(WORLD[W_FRONT_SONAR])
time.sleep(.05)
if (abs(WORLD[W_COMPASS] - WEST_FENCE + 90) > 10):
compass_turn(WEST_FENCE + 90)
move.forward()
move.stop()
compass_turn_rel(180)
Stop = True
def mow():
"""
The main loop in which we mow the lawn.
"""
global Stop
blocking = False
running = False
#Sendeverzoegerung fuer MQTT
k = 0
#Zaehler fuer die Maehdauer
m = 0
while True:
output()
time.sleep(0.05)
k = k + 1
m = m + 1
if k == 50: #update WORLD information on MQTT every 50 loops
if DEBUG:
print WORLD[W_FRONT_SONAR], WORLD[W_GROUND_COLOUR], blocking, m
MQTT.mqttc.publish("/RPiMower/World/Polar", str(WORLD[W_MAP]), qos=0, retain=True)
MQTT.mqttc.publish("/RPiMower/World/Compass", str(WORLD[W_COMPASS]), qos=0, retain=True)
MQTT.mqttc.publish("/RPiMower/World/FrontUS", str(WORLD[W_FRONT_SONAR]), qos=0, retain=True)
#MQTT.mqttc.publish("/RPiMower/World/BackUS", WORLD[WORLD_BACK_SONAR], qos=0, retain=True)
MQTT.mqttc.publish("/RPiMower/World/GroundColour", WORLD[W_GROUND_COLOUR], qos=0, retain=True)
k = 1
if WORLD[W_USERCMD] <> "":
if WORLD[W_USERCMD] == "start":
print "Command 'Start' received. Starting mower"
move.forward()
VSS.on([5])
ESC.setThrottle(8.5)
WORLD[W_RUNNING] = True
elif WORLD[W_USERCMD] == "stop":
print "Command 'Stop' received. Stopping mower"
move.stop()
VSS.off([5])
ESC.setThrottle(7.5)
WORLD[W_RUNNING] = False
elif WORLD[W_USERCMD] == "turn":
print "Usercommand 'turn' received. Turning mower 20*"
move.turn(20)
elif WORLD[W_USERCMD] == "return":
print "Usercommand 'return' received. Return mower to home base"
return_home()
WORLD[W_USERCMD] = ""
blocking = False;
detect_blocking(WORLD[W_FRONT_SONAR])
obstacle = (float(WORLD[W_FRONT_SONAR]) < MIN_DISTANCE)
off_field = False;
#(WORLD[W_GROUND_COLOUR] != 'green')
tilt = ((abs(WORLD[W_PITCH]) > 35) or (abs(WORLD[W_ROLL]) > 35))
if tilt:
print "Stopping mower, RPiMower pitched at ", WORLD[W_PITCH]
ESC.setThrottle(7.5)
move.stop()
if blocking or obstacle or off_field:
if DEBUG:
print WORLD[W_FRONT_SONAR], WORLD[W_GROUND_COLOUR], blocking
print "Front obstacle detected or left the green, turning...", blocking, obstacle, off_field
move.stop()
VSS.off([5])
move.turn(random.uniform(-1.0,1.0))
move.forward()
if m > 1000000:
print "Mowing max time reached, exiting!"
move.stop()
return
move.stop()
# Use the signal module to handle signals
for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
signal.signal(sig, cleanup)
# Initialise our libraries
move.init()
VSS.init([5])
ESC.init(12)
MQTT.init()
MQTT.mqttc.on_message = on_message
MQTT.mqttc.subscribe(MQTT_TOPIC_IN, qos=MQTT_QOS)
# start main procedure
build_map()
mow()
#return_home()