Compare commits

...

2 Commits

Author SHA1 Message Date
litian.zhuang 11f2a6a56f 20220929 2022-09-29 21:29:05 +08:00
litian.zhuang 2dacb5064c 20220928 2022-09-28 21:46:04 +08:00
2 changed files with 38 additions and 12 deletions

View File

@ -61,6 +61,7 @@ class GraspObject():
self.pub1.publish(pos) self.pub1.publish(pos)
def grasp_cp(self, msg): def grasp_cp(self, msg):
global height
if msg.data == '1': if msg.data == '1':
# 订阅摄像头话题,对图像信息进行处理 # 订阅摄像头话题,对图像信息进行处理
self.sub = rospy.Subscriber("/camera/rgb/image_raw", Image, self.image_cb, queue_size=1) self.sub = rospy.Subscriber("/camera/rgb/image_raw", Image, self.image_cb, queue_size=1)
@ -160,6 +161,7 @@ class GraspObject():
print((pos.x,pos.y)) print((pos.x,pos.y))
# 开始吸取物体 # 开始吸取物体
self.pub2.publish(1) self.pub2.publish(1)
print('nmmsl')
r2.sleep() r2.sleep()
# 提起物体 # 提起物体
@ -174,6 +176,9 @@ class GraspObject():
# change to opencv # change to opencv
try: try:
cv_image1 = CvBridge().imgmsg_to_cv2(data, "bgr8") cv_image1 = CvBridge().imgmsg_to_cv2(data, "bgr8")
ht = len(cv_image1)
wit= len(cv_image1[0])
cv_image1[0:ht//3,0:wit]=[0,0,0]
except CvBridgeError as e: except CvBridgeError as e:
print('error') print('error')
@ -181,8 +186,8 @@ class GraspObject():
cv_image2 = cv2.cvtColor(cv_image1, cv2.COLOR_BGR2HSV) cv_image2 = cv2.cvtColor(cv_image1, cv2.COLOR_BGR2HSV)
# 蓝色物体颜色检测范围 # 蓝色物体颜色检测范围
LowerBlue = np.array([90, 95, 120]) LowerBlue = np.array([95, 90, 120])
UpperBlue = np.array([140, 255, 255]) UpperBlue = np.array([130, 255, 255])
# LowerBlue = np.array([4, 103, 162]) # LowerBlue = np.array([4, 103, 162])
# UpperBlue = np.array([85, 136, 181]) # UpperBlue = np.array([85, 136, 181])
mask = cv2.inRange(cv_image2, LowerBlue, UpperBlue) mask = cv2.inRange(cv_image2, LowerBlue, UpperBlue)
@ -213,11 +218,28 @@ class GraspObject():
rect = cv2.minAreaRect(c) rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect) box = cv2.boxPoints(rect)
box = np.int0(box) box = np.int0(box)
x_mid = (box[0][0] + box[2][0] + box[1][0] + box[3][0]) / 4 # if abs(y_mid) > 250 or abs(x_mid)>270:
y_mid = (box[0][1] + box[2][1] + box[1][1] + box[3][1]) / 4 # continue
w = math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2) w = math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2)
h = math.sqrt((box[0][0] - box[3][0]) ** 2 + (box[0][1] - box[3][1]) ** 2) h = math.sqrt((box[0][0] - box[3][0]) ** 2 + (box[0][1] - box[3][1]) ** 2)
if w>h:
tmp = h
h = w
w = tmp
if w !=0 and h/w >=1.3:
Ph0MidX = (box[0][0]+box[3][0])/2
Ph0MidY = (box[0][1]+box[1][1])/2
Ph1MidX = (box[1][0]+box[2][0])/2
Ph1MidY = (box[1][1]+box[2][1])/2
x_mid = (Ph0MidX+Ph1MidX+box[0][0]+box[1][0])/4
y_mid = (Ph0MidY+Ph1MidY+box[0][1]+box[1][1])/4
size.append(w * h/2)
else:
x_mid = (box[0][0] + box[2][0] + box[1][0] + box[3][0]) / 4
y_mid = (box[0][1] + box[2][1] + box[1][1] + box[3][1]) / 4
size.append(w * h) size.append(w * h)
# cv2.circle(pic, (int(x_mid),int(y_mid)), 3, (0, 0, 255), 3)
if size[i] > size_max: if size[i] > size_max:
size_max = size[i] size_max = size[i]
index = i index = i
@ -249,17 +271,21 @@ class GraspObject():
pos.y = 0 pos.y = 0
if height == 0: if height == 0:
pos.z = -40 pos.z = -40
self.pub1.publish(pos)
elif height ==1: elif height ==1:
pos.z = -40 pos.z = -40
elif height == 2:
pos.z = 55
elif height ==3:
pos.z = 150 #-80
self.pub1.publish(pos) self.pub1.publish(pos)
r1.sleep() elif height == 2:
pos.z = 80
self.pub1.publish(pos)
elif height ==3:
print('nmsl')
self.pub2.publish(0)
time.sleep(2)
# stop pump # stop pump
self.pub2.publish(0) self.pub2.publish(0)
r1.sleep(0.1) time.sleep(1)
# r1.sleep()
pos.x = 120 pos.x = 120
pos.y = 0 pos.y = 0
pos.z = 35 pos.z = 35

View File

@ -104,7 +104,7 @@ def keyboardLoop():
elif ch == 'z': elif ch == 'z':
if can_release: if can_release:
msg=String() msg=String()
msg.data='2' msg.data='4'
grasp_pub.publish(msg) grasp_pub.publish(msg)
can_release=False can_release=False
speed = 0 speed = 0