-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathraspberry_main.py
150 lines (119 loc) · 4.53 KB
/
raspberry_main.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
from camera import Camera
from claw import Arm
from tracker import Detector, Tracker
from motors import MotorController
from utils import Utils
from time import sleep, time
from Stage0 import Distance
import webinterface
from speech import Speech
import cv2
turnSpeed = 0.0
TURNFACTOR = -0.6
if __name__ == "__main__":
Camera.begin()
MotorController.begin()
webinterface.begin()
Distance.begin()
speech=Speech()
detector = Detector()
detector.begin()
Tracker.begin()
speech.speak(speech.ON)
arm = Arm()
webinterface.setClawObj(arm)
Utils.pickupPhase = 0
frameNumber = 0
print("Starting")
if Utils.mode == "manual":
speech.speak(Speech.AUTOOFF)
else:
speech.speak(Speech.AUTOON)
for img in Camera.waitFrame():
draw = img.copy()
#===Begin automatic mode===
if Utils.mode == "auto":
Distance.loop()
if Utils.pickupPhase == 3:
arm.openClaw()
arm.armReach()
MotorController.speed = 0.7
MotorController.forward()
arm.closeClaw()
MotorController.speed = 0.9
MotorController.stop()
arm.armRestingPos()
print("paper ball grabbed")
Utils.pickupPhase=4
if Utils.pickupPhase == 4:
arm.rotateClawBack()
arm.openClaw()
arm.rotateClawFront()
arm.closeClaw()
print("paper ball put in dustbin")
Utils.pickupPhase=0
Tracker.AllTrackers = []
if Utils.pickupPhase == 2:
if len(Tracker.AllTrackers) > 0:
Tracker.AllTrackers[0].track(img)
t = time()
while time() - t < 0.2:
d = Distance.distance()
print(d)
if d <= 25.0:
Utils.pickupPhase = 3
break
tObj = Tracker.AllTrackers[0]
#print("Tracker at:", tObj.x, tObj.y, tObj.w, tObj.h)
#Calculate middle of rectangle, which is the actual value
actualX = (tObj.x+tObj.w/2)
targetX = 0.5
xError = targetX - actualX
print("Error value is {:.3f}".format(xError))
turnSpeed = xError * TURNFACTOR
MotorController.customControl((max(min(0.7 + turnSpeed, 1), 0), max(min(0.7 - turnSpeed, 1), 0)))
MotorController.stop()
#~ 2 frames
#sleep(0.5)
else:
#Object lost, go back to detecting
Utils.pickupPhase = 0
#===End of automatic mode===
#Draw trackers
for t in Tracker.AllTrackers:
x, y, w, h = t.getPosTupleImage(draw)
color = (0, 0, 255)
if t.isTracking:
color = (0, 255, 0)
text = "ID {}".format(t.id)
cv2.putText(draw, text, (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.rectangle(draw, (x, y), (x + w, y + h), color)
frameNumber += 1
cv2.imshow("Win", draw)
k = cv2.waitKey(16)
if k == ord("s"):
box = cv2.selectROI("Win", img, fromCenter=False,
showCrosshair=True)
bbox = [0,0,0,0]
bbox[0] = box[0] / img.shape[1]
bbox[1] = box[1] / img.shape[0]
bbox[2] = box[2] / img.shape[1]
bbox[3] = box[3] / img.shape[0]
Tracker(bbox, img)
Utils.pickupPhase = 2
if k == ord("c"):
Tracker.AllTrackers = []
speech.speak(speech.Restart)
Utils.pickupPhase = 0
if k == ord('m'):
webinterface.toggleMode()
if k == ord('q') or k == 27:
speech.speak(speech.OFF)
break
print("Quit")
MotorController.stop()
detector.end()
Tracker.end()
Camera.end()
cv2.destroyAllWindows()