-
Notifications
You must be signed in to change notification settings - Fork 0
/
mazeBot.py
253 lines (201 loc) · 6.88 KB
/
mazeBot.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
#!/usr/bin/env python3
# Import the necessary libraries
import time
import math
from ev3dev2.motor import *
from ev3dev2.sound import Sound
from ev3dev2.sensor import *
from ev3dev2.sensor.lego import *
from ev3dev2.sensor.virtual import *
# Create the sensors and motors objects
motorA = LargeMotor(OUTPUT_A)
motorB = LargeMotor(OUTPUT_B)
left_motor = motorA
right_motor = motorB
tank_drive = MoveTank(OUTPUT_A, OUTPUT_B)
steering_drive = MoveSteering(OUTPUT_A, OUTPUT_B)
spkr = Sound()
radio = Radio()
color_sensor_in1 = ColorSensor(INPUT_1)
ultrasonic_sensor_in2 = UltrasonicSensor(INPUT_2)
gyro_sensor_in3 = GyroSensor(INPUT_3)
gps_sensor_in4 = GPSSensor(INPUT_4)
pen_in5 = Pen(INPUT_5)
touch_in6 = TouchSensor(INPUT_6)
motorC = LargeMotor(OUTPUT_C) # Magnet
# VARS
sq_distance = 30 #DEFAULT = 30 ( actually 40cm )
bumped = False
turn_speed = 3 #DEFAULT = 3
forwards_time = 2.55 # DEFAULT = 2.2
forwards_percent = 20 # DEFAULT = 20
backwards_time = 1.5 # DEFAULT = 1.5
backwards_percent = 20 # DEFAULT = 20
maze_end_colour = "Red"
maze_start_colour = "Green"
distance_get_colour = "Blue"
# Function Defining
# This function gets the robot to say something
def speak( text ):
spkr.speak(text, play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)
# This function gets the robot to beep
def beep(tone, time):
spkr.play_tone(tone, time, play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)
# This function gets the colour from the colour sensor
def getColour():
return color_sensor_in1.color_name
# This function gets whether the touch sensor has been pressed or not
def touchPressed():
return touch_in6.is_pressed
# This function gets the distance from the ultrasonic sensor
def get_distance():
return ultrasonic_sensor_in2.distance_centimeters
# This function stops all motors on the robot
def stop_all_motors_brake():
motorA.off(brake=True)
motorB.off(brake=True)
# This function reverses both motors on the robot
def full_backwards(percent, stime):
motorA.on(-percent)
motorB.on(-percent)
time.sleep(stime)
stop_all_motors_brake()
#END full_backwards()
# This is a function that helps define if the touch sensor has been pressed and stops the motors accordingly
def full_forwardsR():
if(touchPressed() == 1 ):
stop_all_motors_brake()
speak("Ooof")
# This function will make the robot go forwards with both motors in a straight line
def full_forwards(percent, stime):
stime = stime*10
motorA.on(percent)
motorB.on(percent)
i = 0
while ( i <= stime and touchPressed() == 0):
#for x in range(0, int(stime)):
print(( i <= stime and touchPressed() == 0), " Distance: ", i, " Units ")
full_forwardsR()
time.sleep(0.1)
if(touch_in6.is_pressed == 1):
full_backwards(backwards_percent, backwards_time)
speak("Turning Right")
turn_right_deg(turn_speed, 90)
break
i += 1
#END LOOP
#stop_all_motors_brake()
beep(400, 0.5)
# this next piece of code caused issues with stopping
#steering_drive.on_for_rotations(0, 20, 1) # direction, speed, rotations
#END full_forwards
# OLD TURN FUNCTIONS - FOR NON GYRO ROBOT
# This function is to turn right
def tturn_right(percent, stime):
tturn(percent, stime)
# This function is to turn left
def tturn_left(percent, stime):
tturn(-percent, stime)
# This function is to specify exact turn direction
def tturn(percent, stime):
left_motor.on(percent)
right_motor.on(-percent)
time.sleep(stime)
stop_all_motors_brake()
# END OF OLD TURN FUNCTIONS
# This function defines what the robot should do each "step" ( square of the maze travelled ) and how to traverse it
def step():
if( get_distance() > ( sq_distance ) ):
# free space to left
speak("Turning Left")
turn_left_deg(turn_speed, 90)
print("turning left")
full_forwards(forwards_percent, forwards_time)
else:
full_forwards(forwards_percent, forwards_time)
# END IF
# PSEUDOCODE for this function (can be removed soon)
#if free to left
#rotate left
#move 1 forwards
#next step
#else
#move forwards
#if bumped
#turn right
#next step
# This function uses the gyro sensor to turn the robot right a specific amount of degrees
def turn_right_deg(percent, deg):
stop_all_motors_brake()
gyro_sensor_in3.reset()
time.sleep(0.02)
# BUG - sometimes skips number and never stops spinning - need to rework
while not gyro_sensor_in3.angle == deg + 1:
left_motor.on(percent)
right_motor.on(-percent)
print("Deg: ", gyro_sensor_in3.angle)
# time.sleep(0.1)
stop_all_motors_brake()
# END FUNC
# This function uses the gyro sensor to turn the robot left a specific amount of degrees
def turn_left_deg(percent, deg):
turn_right_deg(-percent, -deg-1)
# This function defines what the robot does when in maze solving mode
def solve_maze():
solved = False
while( not solved ):
print(getColour(), "Looking for: ", maze_end_colour )
if getColour() == maze_end_colour:
speak("Maze Complete")
stop_all_motors_brake()
solved = True
turn_left_deg(20, 360)
break
else:
step()
#if color sensor detects ( finish colour ) - break && solved = True
# This function defines the algorithm for finding the closest object to the robot
def closest_object():
speak("finding closest object")
gyro_sensor_in3.reset()
gyro_temp = gyro_sensor_in3.angle
closest_distance = get_distance()
stop_all_motors_brake()
# time.sleep(0.1)
left_motor.on(10)
right_motor.on(-10)
while( gyro_sensor_in3.angle <= 360):
if( ultrasonic_sensor_in2.distance_centimeters < closest_distance):
closest_distance = get_distance()
else:
#do nothing
print("not Closer")
#now to turn
gyro_temp =+ gyro_sensor_in3.angle
print(gyro_temp)
stop_all_motors_brake()
print("closest Object is: " + str(closest_distance))
speak("Closest object is: " + str(round(closest_distance, 2)) + " away")
full_forwards(forwards_percent, forwards_time)
# END OF FUNCTION DECLARATION
# START OF CODE
#This function is the main function
def main():
print(getColour())
#speak("hi there")
while( True ):
if ( getColour() == maze_start_colour ):
speak("Solving maze")
solve_maze()
elif ( getColour() == distance_get_colour ):
closest_object()
# END WHILE
# END MAIN FUNCTION
# TEST CODE
main()
# full_forwards(20, 2.8)
#turn_left_deg(10, 90)
# full_forwards(20, 10)
# time.sleep(1)
# full_backwards(20, 1.5)
# turn_right_deg(10, 90)