-
Notifications
You must be signed in to change notification settings - Fork 0
/
go2.py
170 lines (149 loc) · 5.74 KB
/
go2.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
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 28 21:43:35 2015
@author: christopher
"""
# Libraries
import communication
import pi2go
import time
from crashing_constants import *
# Initail Values
state = 'INIT'
mode = 'STOP'
prev_mode = 'STOP'
distance = 0
SQUAD = []
prev_messurement_time = 0
message_buffer_slave = []
message_buffer_master = []
# Programm
try:
while True:
if STATE == 'INIT':
pi2go.init()
sock = communication.init_nonblocking_receiver('',PORT)
for x in range(SQUAD_SIZE):
SQUAD.append(True)
message_buffer_slave.append(True)
OWN_IP = communication.get_ip()
OWN_ID = communication.get_id_from_ip(OWN_IP)
print OWN_ID
STATE = 'RUNNING'
elif STATE == 'RUNNING':
# distance
if time.time() - prev_messurement_time > WAIT_DIST:
prev_messurement_time = time.time()
distance = pi2go.getdistance()
# Obstacle = 1, No Obstacle = 0
irCentre = pi2go.irCentre()
# Obstacle Analysis
if irCentre or (distance < DIST_MIN):
distance_level = 0
elif distance > DIST_MAX:
distance_level = 2
else:
distance_level = 1
# Receive
data = 'new_round'
while data != '':
data, addr = communication.receive_message(sock)
if data != '':
ID = communication.get_id_from_ip(addr[0])
if ID == OWN_ID:
print 'OWN: ' , ID, ' : ' , data
continue
if ID >= SQUAD_START and ID <= SQUAD_START+SQUAD_SIZE:
print 'ROBOT: ', ID, ' : ' , data
if data == 'PROBLEM':
curr_STATUS = False
#SQUAD[ID-SQUAD_START] = curr_STATUS
SQUAD[ID] = curr_STATUS
elif data == 'RELEASE':
curr_STATUS = True
#SQUAD[ID-SQUAD_START] = curr_STATUS
SQUAD[ID] = curr_STATUS
else:
print 'MASTER:' , ID , ' : ' , data
# make List with Master commands an react on this
# change mode, STATUS, SPEED, distanceLIMITS
# Analyse --> Calculate mode
prev_mode = mode
if distance_level == 0:
mode = 'STOP'
elif distance_level == 1 and all(SQUAD):
mode = 'SLOW'
elif distance_level == 2 and all(SQUAD):
mode = 'RUN'
elif distance_level != 0 and not all(SQUAD):
mode = 'WARN'
else:
print 'check mode-Conditions'
break
# Set own SQUAD_VALUE
if mode != prev_mode:
if mode == 'STOP':
#SQUAD[OWN_ID-SQUAD_START] = False
SQUAD[OWN_ID] = False
else:
#SQUAD[OWN_ID-SQUAD_START] = True
SQUAD[OWN_ID] = True
# LEDs
if mode != prev_mode:
if mode == 'RUN':
pi2go.setAllLEDs(LED_OFF,LED_ON,LED_OFF)
elif mode == 'SLOW':
pi2go.setAllLEDs(LED_OFF,LED_OFF,LED_ON)
elif mode == 'WARN':
pi2go.setAllLEDs(LED_ON,LED_ON,LED_OFF)
elif mode == 'STOP':
pi2go.setAllLEDs(LED_ON,LED_OFF,LED_OFF)
# distance controller
if mode == 'SLOW':
SPEED_SLOW = SPEED_RUN - (DIST_REF-distance) * KP
# Controlllimits
if SPEED_SLOW > SPEED_CONTROL_MAX:
SPEED_SLOW = SPEED_CONTROL_MAX
elif SPEED_SLOW < SPEED_CONTROL_MIN:
SPEED_SLOW = SPEED_CONTROL_MIN
print 'DIST: ', distance , 'SPEED: ', SPEED_SLOW
# Motor
if mode != prev_mode:
if mode == 'RUN':
SPEED = SPEED_RUN
elif mode == 'SLOW':
SPEED = SPEED_SLOW
elif mode == 'WARN':
SPEED = SPEED_WARN
elif mode == 'STOP':
SPEED = SPEED_STOP
# Speedlimits
if SPEED > 100:
SPEED = 100
elif SPEED < 0:
SPEED = 0
pi2go.go(SPEED,SPEED)
# Send
if mode != prev_mode:
if prev_mode == 'STOP':
#print 'RELEASE'
message = 'RELEASE'
for x in range(SENDING_ATTEMPTS):
communication.send_broadcast_message(PORT, message)
time.sleep(WAIT_SEND)
elif mode == 'STOP':
#print 'PROBLEM'
message = 'PROBLEM'
for x in range(PUSH):
communication.send_broadcast_message(PORT, message)
time.sleep(WAIT_SEND)
else:
print 'impossible STATE'
STATE == 'RUNNING'
except KeyboardInterrupt:
print 'KEYBOARD'
finally:
pi2go.stop()
pi2go.cleanup()
sock.close()
print 'END'