This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field1.py
185 lines (177 loc) · 5.68 KB
/
field1.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
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from math import *
hub = PrimeHub()
# declare sensors + motors
RMotor = Motor("A")
LMotor = Motor("B")
motor_pair = MotorPair("B", "A")
RColor = ColorSensor("D")
LColor = ColorSensor("C")
ultrasonic = DistanceSensor("F")
enterZone = False
GrabMotor = Motor("E")
#Line Tracing function
angle = hub.motion_sensor.get_yaw_angle() - 90
if angle <= -180:
angle = 180 - abs(angle - -180)
RSpeed = 15
LSpeed = 15
bumpCount = 0
boxPos = 0
posOne = 0
posTwo = 0
countOne = 0
countTwo = 0
temp = 0
def trace():
global distance
global extra
RValue = RColor.get_color()
LValue = LColor.get_color()
if RValue == None and LValue == "white":
motor_pair.start_tank(LSpeed, RSpeed)
wait_for_seconds(0.7)
motor_pair.start_tank(0, 0)
wait_for_seconds(0.2)
motor_pair.start_tank(-40, 40)
wait_for_seconds(0.55)
motor_pair.start_tank(20, 20)
wait_for_seconds(0.4)
elif RValue == "white" and LValue == None:
motor_pair.start_tank(LSpeed, RSpeed)
wait_for_seconds(0.7)
motor_pair.start_tank(0, 0)
wait_for_seconds(0.2)
motor_pair.start_tank(40, -40)
wait_for_seconds(0.55)
motor_pair.start_tank(20, 20)
wait_for_seconds(0.4)
elif RValue == "white" and LValue == "white":
motor_pair.start_tank(LSpeed, RSpeed)
elif RValue == "black" and LValue == "white":
motor_pair.start_tank(-20, 40)
elif RValue == "white" and LValue == "black":
motor_pair.start_tank(40, -20)
if hub.motion_sensor.was_gesture('shaken'):
motor_pair.start_tank(-40, -40)
wait_for_seconds(0.5)
motor_pair.start_tank(100, 100)
wait_for_seconds(1.6)
motor_pair.start_tank(0, 0)
wait_for_seconds(0.5)
motor_pair.start_tank(40, -40)
wait_for_seconds(0.8)
motor_pair.start_tank(0, 0)
wait_for_seconds(2)
motor_pair.start_tank(LSpeed, RSpeed)
wait_for_seconds(4)
extra = ultrasonic.get_distance_cm()
if extra != None:
distance = extra
if distance <= 8 and hub.motion_sensor.get_yaw_angle() < angle + 45 and hub.motion_sensor.get_yaw_angle() > angle - 45:
motor_pair.start_tank(0, 0)
motor_pair.start_tank(-40, -40)
motor_pair.start_tank(40, -40)
wait_for_seconds(0.55)
motor_pair.start_tank(LSpeed, RSpeed)
wait_for_seconds(3)
motor_pair.start_tank(-40, 40)
wait_for_seconds(0.6)
motor_pair.start_tank(LSpeed, RSpeed)
wait_for_seconds(7.5)
motor_pair.start_tank(-40, 40)
wait_for_seconds(0.6)
motor_pair.start_tank(LSpeed, RSpeed)
wait_for_seconds(3)
motor_pair.start_tank(40, -40)
wait_for_seconds(0.6)
# print(hub.motion_sensor.get_pitch_angle())
# if hub.motion_sensor.get_pitch_angle() >= 4 or hub.motion_sensor.get_pitch_angle() <= -4:
# if bumpCount == 1:
# motor_pair.start_tank(24, 24)
# wait_for_seconds(0.3)
# motor_pair.start_tank(-40, 40)
# wait_for_seconds(0.55)
# motor_pair.start_tank(24, 24)
# else:
# bumpCount += 1
# wait_for_seconds(0.5)
# find box position
def findBox():
global posOne
global posTwo
global countOne
global countTwo
global temp
motor_pair.start_tank(40, 0)
wait_for_seconds(0.5)
motor_pair.start_tank(10, 10)
wait_for_seconds(0.5)
motor_pair.start_tank(0, 40)
wait_for_seconds(0.5)
motor_pair.start_tank(0, 0)
for x in range (20):
temp = ultrasonic.get_distance_cm()
if type(temp) is int:
posOne += temp
countOne += 1
if countOne == 0:
countOne = 1
wait_for_seconds(0.5)
motor_pair.start_tank(-40, -40)
wait_for_seconds(0.6)
motor_pair.start_tank(-40, 40)
wait_for_seconds(0.7)
motor_pair.start_tank(0, 0)
for x in range (20):
temp = ultrasonic.get_distance_cm()
if type(temp) is int:
posTwo += temp
countTwo += 1
if countTwo == 0:
countTwo = 1
#Main Tracing Loop:
while True:
trace()
# while enterZone == False:
# if RColor.get_color() == "red" or LColor.get_color() == "red":
# enterZone = True
# else:
# motor_pair.start_tank(LSpeed, RSpeed)
# motor_pair.start_tank(40, 40)
# wait_for_seconds(1)
# motor_pair.start_tank(0, 0)
# findBox()
# print(posOne/20)
# print(posTwo/20)
# if posOne / countOne >= 55 and posOne / countOne <= 85:
# motor_pair.start_tank(-40, 40)
# wait_for_seconds(0.55)
# motor_pair.start_tank(-60, -60)
# wait_for_seconds(2.7)
# motor_pair.start_tank(0, 0)
# wait_for_seconds(0.5)
# motor_pair.start_tank(60, 60)
# wait_for_seconds(1)
# motor_pair.start_tank(0, 0)
# elif posTwo / countTwo >= 50 and posTwo / countTwo <= 90:
# motor_pair.start_tank(-40, 40)
# wait_for_seconds(1.1)
# motor_pair.start_tank(-80, -80)
# wait_for_seconds(1.5)
# motor_pair.start_tank(0, 0)
# wait_for_seconds(1)
# motor_pair.start_tank(60, 60)
# wait_for_seconds(1)
# motor_pair.start_tank(0, 0)
# else:
# motor_pair.start_tank(-40, 40)
# wait_for_seconds(0.7725)
# motor_pair.start_tank(-80, -80)
# wait_for_seconds(2.7)
# motor_pair.start_tank(0, 0)
# wait_for_seconds(1)
# motor_pair.start_tank(60, 60)
# wait_for_seconds(1)
# motor_pair.start_tank(0, 0)