forked from lihuang3/ur5_ROS-Gazebo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathur5_vision.py
executable file
·110 lines (94 loc) · 3.95 KB
/
ur5_vision.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
#!/usr/bin/env python
"""
moveit_cartesian_path.py - Version 0.1 2016-07-28
Based on the R. Patrick Goebel's moveit_cartesian_demo.py demo code.
Plan and execute a Cartesian path for the end-effector.
Created for the Pi Robot Project: http://www.pirobot.org
Copyright (c) 2014 Patrick Goebel. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.5
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/licenses/gpl.html
"""
import rospy, sys, numpy as np
import moveit_commander
from copy import deepcopy
import geometry_msgs.msg
from ur5_notebook.msg import Tracker
import moveit_msgs.msg
import cv2, cv_bridge
from sensor_msgs.msg import Image
from std_msgs.msg import Header
from trajectory_msgs.msg import JointTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint
tracker = Tracker()
class ur5_vision:
def __init__(self):
rospy.init_node("ur5_vision", anonymous=False)
self.track_flag = False
self.default_pose_flag = True
self.cx = 400.0
self.cy = 400.0
self.bridge = cv_bridge.CvBridge()
self.image_sub = rospy.Subscriber('/ur5/usbcam/image_raw', Image, self.image_callback)
self.cxy_pub = rospy.Publisher('cxy', Tracker, queue_size=1)
def image_callback(self,msg):
# BEGIN BRIDGE
image = self.bridge.imgmsg_to_cv2(msg,desired_encoding='bgr8')
# END BRIDGE
# BEGIN HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# END HSV
# BEGIN FILTER
lower_red = np.array([ 0, 100, 100])
upper_red = np.array([10, 255, 255])
mask = cv2.inRange(hsv, lower_red, upper_red)
(_, cnts, _) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#area = cv2.contourArea(cnts)
h, w, d = image.shape
# print h, w, d (800,800,3)
#BEGIN FINDER
M = cv2.moments(mask)
if M['m00'] > 0:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
# cx range (55,750) cy range( 55, ~ )
# END FINDER
# Isolate largest contour
# contour_sizes = [(cv2.contourArea(contour), contour) for contour in cnts]
# biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
for i, c in enumerate(cnts):
area = cv2.contourArea(c)
if area > 7500:
self.track_flag = True
self.cx = cx
self.cy = cy
self.error_x = self.cx - w/2
self.error_y = self.cy - (h/2+195)
tracker.x = cx
tracker.y = cy
tracker.flag1 = self.track_flag
tracker.error_x = self.error_x
tracker.error_y = self.error_y
#(_,_,w_b,h_b)=cv2.boundingRect(c)
#print w_b,h_b
# BEGIN circle
cv2.circle(image, (cx, cy), 10, (0,0,0), -1)
cv2.putText(image, "({}, {})".format(int(cx), int(cy)), (int(cx-5), int(cy+15)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.drawContours(image, cnts, -1, (255, 255, 255),1)
#BGIN CONTROL
break
else:
self.track_flag = False
tracker.flag1 = self.track_flag
self.cxy_pub.publish(tracker)
cv2.namedWindow("window", 1)
cv2.imshow("window", image )
cv2.waitKey(1)
follower=ur5_vision()
rospy.spin()