-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker_new.py
177 lines (138 loc) · 4.18 KB
/
tracker_new.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 14 10:16:41 2020
@author: mesut
"""
import cv2
import numpy as np
import math
import time
import serial
_pre_errorY=0
_pre_errorX=0
_integralY=0
_integralX=0
period=200
myTime=0
#y axes is pv get from camera - current position
#dt is time interval
# def calculate(setpoint,pv,_Kp,_Ki,_Kd,_dt):
# error = setpoint - pv
# Pout = _Kp * error
# _integral += error * _dt
# Iout = _Ki *_integral
#
# derivative = (error - _pre_error) / _dt
# Dout = _Kd * derivative
#
# # Calculate total output
# output = Pout + Iout + Dout
#
# _pre_error = error
#
# return output
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.flush()
font = cv2.FONT_HERSHEY_SIMPLEX
setPointY=120 #it will get from desktop or mobile for y axes
setPointX=120 #it will get from desktop or mobile for y axes
cap = cv2.VideoCapture(0)
cap.set(3,320)
cap.set(4,240)
x_medium = 0
y_medium = 0
tmpX = 0
tmpY = 0
tmpD = 0
while True:
_, frame = cap.read()
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
sensitivity = 15
low_red = np.array([2,85,51])
high_red = np.array([33,223,253])
red_mask = cv2.inRange(hsv_frame,low_red,high_red)
_ ,contours, _= cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x:cv2.contourArea(x), reverse=True)
#x_medium = 0
#y_medium = 0
for cnt in contours:
(x,y,w,h) = cv2.boundingRect(cnt)
distancei = (2*3.14 * 180)/(w+h*360)*1000 + 3
#print distancei
distance = distancei *2.54
distance = math.floor(distancei/2)
#cv2.rectangle(frame,(x,y),(x+w,y+h), (0,255,0),2)
x_medium = int((x+x+w)/2)
y_medium = int((y+y+h)/2)
cv2.line(frame, (x_medium,0), (x_medium,480), (0,255,0),1)
cv2.line(frame, (780,y_medium), (0,y_medium), (0,255,0),1)
tmpX=x_medium
tmpY=y_medium
tmpD=distance
break
position = (3,30)
cv2.putText(frame,'Distance = ' + str(tmpD) + ' cm', (3,50),font,0.5,(255,255,255),1)
cv2.putText(
frame, #numpy array on which text is written
" x pos : "+str(tmpX)+" \n y pos : "+str(tmpY)+"", #text
position, #position at which writing has to start
cv2.FONT_HERSHEY_SIMPLEX, #font family
0.5, #font size
(255, 255, 0, 255),1)
cv2.imshow("Frame",frame)
#cv2.imshow("", red_mask)
key = cv2.waitKey(1)
# print calculate(setPoint,y_medium,100,0.1,0.01,0.5)
millis = 0
millis = int(round(time.time() * 1000))
dy = 0
dz = 0
ki= 0.1
kp= 30
kd= 50
dt = 0.2
if(millis > myTime + period):
myTime= int(round(time.time() * 1000))
errorY = setPointY - tmpY
errorX = setPointX - tmpX
PoutY = kp * errorY
PoutX= kp * errorX
_integralY += errorY * ki
_integralX += errorX * ki
IoutY = _integralY
IoutX = _integralX
derivativeY = (errorY - _pre_errorY) / dt
DoutY = kd * derivativeY
derivativeX = (errorX - _pre_errorX) / dt
DoutX = kd * derivativeX
# Calculate total output
if(errorY > -15 or errorY < 15):
IoutY = 0
if(errorX > -15 or errorX < 15):
IoutX = 0
outputY = PoutY + IoutY +DoutY
outputX = PoutX + IoutX +DoutX
_pre_errorY = errorY
_pre_errorX = errorX
axisX=10
axisY=-5
print(str(outputY) +" :::: "+str(errorY)+"\n")
print(str(outputX) +" :::: "+str(errorX)+"\n")
dy = int((outputY +5000) * (axisX) / (10000) + axisY)
dx = int((outputX +5000) * (axisX) / (10000) + axisY)
if(dy > 7):
dy=7
if(dy < -7):
dy=-7
if(dx > 7):
dx=7
if(dx < -7):
dx=-7
dz = -1*dy
da = -1*dx
print(str(dy)+"\n")
print(str(dx)+"\n")
ser.write(str(dy)+":"+str(da)+":"+str(dx)+":"+str(dz)+":5000\n")
#ser.write(str(da)+":0:0:"+str(dx)+":5000\n")
if key==27:
break