/
fish_control.py
159 lines (143 loc) · 3.74 KB
/
fish_control.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
'''
fish_control.py
main program for fishbot
Author:
Ankush Gola, Joseph Bolling
'''
import Adafruit_BBIO.GPIO as GPIO
import Adafruit_BBIO.PWM as PWM
from fish_tracker import FishTracker
import cv2
class Motor:
'''
A Motor. YEE.
'''
def __init__(self, p1, p2, pwm, forward):
'''
set up the motor pin assignments
'''
self.p1 = p1
self.p2 = p2
self.pwm = pwm
self.forward = forward
GPIO.setup(self.p1, GPIO.OUT)
GPIO.setup(self.p2, GPIO.OUT)
PWM.start(self.pwm,50)
def set(self, speed, forward):
'''
set motor speed, direction
speed is a number from 1 to 50
forward is True if you want the motor to spin forward.
'''
if self.forward:
if forward == True:
GPIO.output(self.p1, GPIO.HIGH)
GPIO.output(self.p2, GPIO.LOW)
else:
GPIO.output(self.p1, GPIO.LOW)
GPIO.output(self.p2, GPIO.HIGH)
else:
if forward == True:
GPIO.output(self.p1, GPIO.LOW)
GPIO.output(self.p2, GPIO.HIGH)
else:
GPIO.output(self.p1, GPIO.HIGH)
GPIO.output(self.p2, GPIO.LOW)
PWM.set_duty_cycle(self.pwm, speed)
def getzone(x, y, thresh_1x, thresh_2x, thresh_1y, thresh_2y):
'''
get the zone according to the x,y position
'''
if x < thresh_1x and y < thresh_1y:
return 'FORWARD_RIGHT'
elif x < thresh_1x and (y >= thresh_1y and y < thresh_2y):
return 'FORWARD_CENTER'
elif x < thresh_1x and y >= thresh_2y:
return 'FORWARD_LEFT'
elif (x >= thresh_1x and x < thresh_2x) and y < thresh_1y:
return 'STALL_RIGHT'
elif (x >= thresh_1x and x < thresh_2x) and (y >= thresh_1y and y < thresh_2y):
return 'NO_FLEX_ZONE'
elif (x >= thresh_1x and x < thresh_2x) and y >= thresh_2y:
return 'STALL_LEFT'
elif x >= thresh_2x and y < thresh_1y:
return 'REAR_RIGHT'
elif x >= thresh_2x and (y >= thresh_1y and y < thresh_2y):
return 'REAR_CENTER'
elif x >= thresh_2x and y >= thresh_2y:
return 'REAR_LEFT'
def main():
'''
main driver for fish control of RC car
'''
PWM_front_drive = "P9_14"
PWM_rear_drive = "P9_21"
PWM_steer = "P9_16"
STANDBY = "P9_23"
fd1, fd2 = "P9_11", "P9_27"
rd1, rd2 = "P9_15", "P9_24"
steer_1, steer_2 = "P9_13", "P9_12"
height = 240
width = 360
STEP = 3
thresh_1x = width*1.0/STEP
thresh_2x = width*2.0/STEP
thresh_1y = height*1.0/STEP
thresh_2y = height*2.0/STEP
# intialize stuff
GPIO.setup(STANDBY, GPIO.OUT)
GPIO.output(STANDBY, GPIO.HIGH)
front = Motor(fd1, fd2, PWM_front_drive, True)
rear = Motor(rd1, rd2, PWM_rear_drive, False)
steer = Motor(steer_1, steer_2, PWM_steer, True)
ft = FishTracker(cap=0, filter_tap=0.5, height=height, width=width)
ft.set_hsv_lo((0, 158, 83))
ft.set_hsv_hi((29, 255, 218))
while True:
(res, state) = ft.detect_fish(show_res=False)
#print state
x, y = state
zone = getzone(int(x), int(y), thresh_1x, thresh_2x, thresh_1y, thresh_2y)
print zone
if zone == 'FORWARD_RIGHT':
front.set(25, True)
rear.set(25, True)
steer.set(20, True)
elif zone == 'FORWARD_CENTER':
front.set(25, True)
rear.set(25, True)
steer.set(0, True)
elif zone == 'FORWARD_LEFT':
front.set(25, True)
rear.set(25, True)
steer.set(20, False)
elif zone == 'STALL_RIGHT':
front.set(0, True)
rear.set(0, True)
steer.set(20, True)
elif zone == 'NO_FLEX_ZONE':
front.set(0, True)
rear.set(0, True)
steer.set(0, False)
elif zone == 'STALL_LEFT':
front.set(0, True)
rear.set(0, True)
steer.set(20, False)
elif zone == 'REAR_RIGHT':
front.set(25, False)
rear.set(25, False)
steer.set(20, True)
elif zone == 'REAR_CENTER':
front.set(25, False)
rear.set(25, False)
steer.set(0, True)
elif zone == 'REAR_LEFT':
front.set(25, False)
rear.set(25, False)
steer.set(20, False)
#cv2.imshow('result',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
ft.release_cap()
main()