-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_final.py
292 lines (230 loc) · 7.16 KB
/
controller_final.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
# Team ID:eYRC#HB#1776
# Author List:Dhanvantraj M, Vinoth B, Winston Doss, Madhusudhanan K
# Filename: finalcontroller.py
#Theme: Hola bot
#Functions: signal_handler, cleanup, endSignalCb, coordgen, graphgen, velocity,main
################### IMPORT MODULES #######################
import socket
import time
import signal # To handle Signals by OS/user
import sys
#for coordinates fxn
import cv2
import imutils
from imutils.video import VideoStream
import math
import numpy
from cv_basics.msg import aruco_data
import rospy
from std_msgs.msg import String
from std_msgs.msg import Int32
############################ Global Variables##################################
ip = "" #Enter IP address of laptop after connecting it to WIFI hotspot
k=0
t=1
finalvel='x:'+str(0.00)+' y:'+str(0.00)+' z:'+str(0.00)
prev_vel_x,prev_vel_y,prev_vel_z=0,0,0
velocityPub=rospy.Publisher('/velocity',String,queue_size=10)
velData=String()
contourPub = rospy.Publisher('/contours', String, queue_size=10)
cData = String()
taskStatusPub = rospy.Publisher('/taskStatus', Int32, queue_size=10)
taskStatus = Int32()
penPub = rospy.Publisher('/penStatus', Int32, queue_size=10)
penData = Int32()
taskStatus.data = 0 #indicating start of the run
penData.data=0 #Indicating pen pulled up
#############################Functions##################################
def signal_handler(sig, frame):
print('Clean-up !')
cleanup()
sys.exit(0)
def cleanup():
global s
s.close()
print("cleanup done")
def endSignalCb(status):
if(status.data):
cleanup()
#coordinates generating f
def coordgen(img_location):
img = cv2.imread(img_location, cv2.IMREAD_UNCHANGED)
img=imutils.resize(img,width=500,height=500)
#convert to grey
img_grey = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#fix threshhold
thresh = 100
ret,thresh_img = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)
#extract contours
contours, hierarchy = cv2.findContours(thresh_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
global x_dL
global y_dL
global theta_dL
global t
global xListFinal
global yListFinal
global thetaListFinal
x_dL=[]
y_dL=[]
theta_dL=[]
i=0
xList , yList , thetaList, xListFinal , yListFinal, thetaListFinal = [] , [] , [] , [], [], []
for i in contours:
xList.clear()
yList.clear()
for j in i:
xList.append(int(j[0][0]))
yList.append(int(j[0][1]))
thetaList.append(0)
xListFinal.append(xList)
yListFinal.append(yList)
thetaListFinal.append(thetaList)
cData.data = str([xListFinal,yListFinal])
contourPub.publish(cData)
x_dL,y_dL,theta_dL=xListFinal[t],yListFinal[t],thetaListFinal[t]
'''x_dL=[350,150,150,350]
y_dL=[300,300,150,150]
theta_dL=[0,0,0,0]'''
'''for i in range(0,len(contours[1]),14):
x_dLval=contours[1][i][0][0]
y_dLval=500-(contours[1][i][0][1])
x_dL.append(x_dLval)
y_dL.append(y_dLval)
theta_dL.append(0)'''
#graph generating
def graphgen():
global x_dL
global y_dL
global theta_dL
x_dL=[]
y_dL=[]
theta_dL=[]
t=0
while(t<=2*math.pi):
x = 200*math.cos(t)
y = 100*math.sin(2*t)
theta = (math.pi/4)*math.sin(t)
x+=250
y+=250
x_dL.append(x)
y_dL.append(y)
theta_dL.append(theta)
t+=0.15
#velocity calculation
def velocity(msg):
rospy.loginfo("Velocity function started execution")
#x_dL,y_dL,theta_dL=coordgen('/home/winston/catkin_ws/src/hola_bot/scripts/snapchat.png')
global k
global t
global prev_vel_x
global prev_vel_y
global prev_vel_z
global finalvel
global x_dL
global y_dL
global theta_dL
global xListFinal
global yListFinal
global thetaListFinal
holax=msg.x
holay=msg.y
theta=msg.theta
x_d=x_dL[k]
y_d=y_dL[k]
theta_d=theta_dL[k]
rospy.loginfo("x_d:{},y_d:{},theta_d:{}".format(x_d,y_d,theta_d))
rospy.loginfo("x:{},y:{},theta:{}".format(holax,holay,theta))
# Calculate Error from feedback
ex=x_d-(holax)
ey=y_d-(holay)
etheta=((theta)-(theta_d))
# Change the frame by using Rotation Matrix (If you find it required)
betheta=(etheta)
bex=(ex*math.cos(round(theta,1))+ey*math.sin(round(theta,1)))
bey=ey*math.cos(round(theta,1))-ex*math.sin(round(theta,1))
rospy.loginfo("bex:{},bey:{},betheta:{}".format(bex,bey,betheta))
# Calculate the required velocity of bot for the next iteration(s)
vel_x=6*bex#-0.5*(prev_vel_x)
vel_y=6*bey #- 0.5*(prev_vel_y)
vel_z=30*betheta #- 0.5*(prev_vel_z)
#rospy.loginfo("vel_x:{},vel_y:{},vel_z:{}".format(vel_x,vel_y,vel_z))
# Find the required force vectors for individual wheels from it.(Inverse Kinematics)
uf=(vel_z+vel_x)/2
ur=vel_z-(vel_x/2)-((math.sqrt(3)/2)*vel_y)
ul=vel_z-(vel_x/2)+((math.sqrt(3)/2)*vel_y)
rospy.loginfo("uf:{},ur:{},ul:{}".format(uf,ur,ul))
uf=-uf
ur=-ur
ul=-ul
prev_vel_x=vel_x
prev_vel_y=vel_y
prev_vel_z=vel_z
#finalvel='x:'+str(round(ul,2))+' y:'+str(round(uf,2))+' z:'+str(round(ur,2))
finalvel='x:'+str(round(ur,2))+' y:'+str(round(uf,2))+' z:'+str(round(ul,2))
#finalvel='x:'+str(round(uf,2))+' y:'+str(round(ur,2))+' z:'+str(round(ul,2))
#rospy.loginfo("hiii"+finalvel)
# Modify the condition to Switch to Next goal (given position in pixels instead of meters)
if(((abs(ex)-3)<=0) and ((abs(ey)-3)<=0) and ((abs(etheta)-0.5)<=0)):
uf=0
ur=0
ul=0
if(k==0):
print("Pen pull down")#Write code for pen pull down
penData.data=1 #Indicating pen pulled down
k+=14
if(k>=(len(x_dL))):
#endsig=0
#taskStatus.data=1
#taskStatusPub.publish(taskStatus.data)
t+=1
k=0
print("Pen pull up")
penData.data=0 #Indicating pen pulled up
#Pen pull up
if(t>=(len(xListFinal))):
taskStatus.data=1
taskStatusPub.publish(taskStatus.data)
rospy.signal_shutdown("Goals Reached")
x_dL,y_dL,theta_dL=xListFinal[t],yListFinal[t],thetaListFinal[t]
#cleanup()#check
#rospy.signal_shutdown("Goals Reached!")
time.sleep(4)
rospy.loginfo("REEEEEEEEEEEEEAAAAAAAAAAAAAACCCCCCCCCCCCCCCCHHHHHHHHHHHHHHEEEEEEEEEEEEEEEEED")
#finalvel='x:'+str(round(ul,2))+' y:'+str(round(uf,2))+' z:'+str(round(ur,2))
finalvel='x:'+str(round(ur,2))+' y:'+str(round(uf,2))+' z:'+str(round(ul,2))
#finalvel='x:'+str(round(uf,2))+' y:'+str(round(ur,2))+' z:'+str(round(ul,2))
velocityPub.publish(finalvel)
taskStatusPub.publish(taskStatus.data)
################################main##################################################
def main():
global finalvel
rospy.init_node('Controller_node')
flag=0
if(flag==0):
coordgen('/home/winston/catkin_ws/src/hola_bot/scripts/robotFinal.png')
else:
graphgen()
rospy.Subscriber('/detected_aruco',aruco_data,velocity)
rospy.Subscriber('/endSignal',Int32,endSignalCb)
'''with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((ip, 8002))
s.listen()
conn,addr = s.accept()
with conn:
print(f"Connected by {addr}")
while not rospy.is_shutdown():
data= conn.recv(1024)
rospy.Subscriber('/detected_aruco',aruco_data,velocity)
rospy.Subscriber('/endSignal',Int32,endSignalCb)
rospy.loginfo(data)
rospy.loginfo("hiiiiiiii"+finalvel)
conn.sendall(str.encode(str(finalvel)))
time.sleep(0.1)
key=cv2.waitKey(1) & 0xFF
if key== ord("q"):
break'''
rospy.spin()
if __name__=='__main__':
main()