diff --git a/gripperHelper.py b/gripperHelper.py new file mode 100644 index 0000000..5a3be9a --- /dev/null +++ b/gripperHelper.py @@ -0,0 +1,84 @@ +import serial +import time + + +class gripperArdunio(): + globalSerial = 0 + MAX_SERV = 7 + def __init__(self): + port = "/dev/ttyACM0" + baud = 9600 + self.globalSerial = serial.Serial(port, baud, timeout=1) + def sendData(self,ser, data: str): + dataSend = "1" + data + ser.write(dataSend.encode("utf-8")) + print("==DATA SEND==") + + def buildAndSendData(self,data): + sendThis = "" + for x in range(0, self.MAX_SERV): + sendThis += str(data[x]) + "," + sendThis = sendThis[:-1] + print(sendThis) + self.sendData(self.globalSerial,sendThis) + time.sleep(2) + + def normalMove(self): + time.sleep(1) + data = [10, 1, 100, 80, 0, 0, 0] + self.buildAndSendData(data) + + def testMove(self,degrees): + data = [degrees, 1, 100, 80, 0, 0, 0] + self.buildAndSendData(data) + data = [degrees, 1, 100, 150, 0, 0, 0] + self.buildAndSendData(data) + data = [degrees, 1, 100, 80, 0, 0, 0] + self.buildAndSendData(data) + def testPlace(self,degrees): + data = [degrees, 1, 100, 80, 0, 0, 0] + self.buildAndSendData(data) + data = [degrees, 20, 100, 150, 0, 0, 0] + self.buildAndSendData(data) + data = [degrees, 1, 100, 80, 0, 0, 0] + self.buildAndSendData(data) + def testRotate(self,degrees): + data = [degrees, 1, 100, 80, 0, 0, 0] + self.buildAndSendData(data) + + def grabK(self): + data = [0, 0, 90, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 0, 60, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 18, 60, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 18, 70, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 26, 70, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 26, 75, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 29, 75, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 29, 78, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 33, 78, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 33, 82, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 37, 88, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 38, 88, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 38, 88, 90, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 38, 88, 63, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 20, 90, 63, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 0, 90, 63, 0, 0, 0] + self.buildAndSendData(data) + data = [0, 0, 90, 90, 0, 0, 0] + self.buildAndSendData(data) + diff --git a/marker_attack_3_real.py b/marker_attack_3_real.py index 6572750..4e09f15 100644 --- a/marker_attack_3_real.py +++ b/marker_attack_3_real.py @@ -6,12 +6,17 @@ import sys import rospy import cv2 import time +import random import numpy as np from std_msgs.msg import String from sensor_msgs.msg import Image from cv_bridge import CvBridge, CvBridgeError from geometry_msgs.msg import Twist +import gripperHelper +import bDepth + + class gripper_mover(): def wait_for_subscribers(self): while self.pub.get_num_connections() == 0: @@ -27,38 +32,58 @@ class gripper_mover(): print("Waiting for {} and {}".format(astra_camera_rgb_topic,astra_camera_depth_topic)) rospy.sleep(1) - def __init__(self, astra_camera_rgb_topic : str, astra_camera_depth_topic : str ,speed_emul : float = 1, start_id : int = 1): + turtle_is_here = 0 + def wait_for_here_callback(self,data): + rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data) + if data.data == "IMHERE": + self.turtle_is_here = 1 + def wait_for_here(self): + sub = rospy.Subscriber("chatter", String, self.wait_for_here_callback) + while self.turtle_is_here == 0: + print("Turtle is not here") + self.rate.sleep() + sub.unregister() + print("Turtle IS here") + + + + + def __init__(self, astra_camera_rgb_topic : str, astra_camera_depth_topic : str , start_id : int = 1): - self.speed_mul = speed_emul self.target_id = start_id #We target one tag at a time self.arucoDict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_ARUCO_ORIGINAL) self.arucoParams = cv2.aruco.DetectorParameters_create() - self.maxMinDepth = 0.5 + + self.gHelper = gripperHelper.gripperArdunio() + #self.gHelper.testMove(10) + self.gHelper.grabK() + self.bDepth = bDepth.betterDepth() ### For turtle controll # self.wait_for_subscribers() self.wait_for_publisher(astra_camera_rgb_topic,astra_camera_depth_topic) self.bridge = CvBridge() rospy.init_node("turtle_control", anonymous=True) - self.rate = rospy.Rate(12) # update rate can be increased + self.rate = rospy.Rate(30) # update rate can be increased + self.wait_for_here() + self.depth_point = (-1,-1) #This is the point from where we get the depth - self.depth_point2 = (-1,-1) self.last_depth = -1 #This is the last depth from the depth_point self.last_depth2 = -1 self.depth_counter = 0 #Make sure we are really there by counting how many time we got this result self.rState = 0 + self.lastMove = 0 self.image_sub = rospy.Subscriber( astra_camera_rgb_topic, Image, self.callback_rgb ) self.depth_bridge = CvBridge() + self.lastTime = time.time() self.depth_image_sub = rospy.Subscriber(astra_camera_depth_topic,Image,self.callback_depth) ### Markers haven been detected, now do something def markDetected(self, img, corners): - thresh_left = self.w//2 - 100 - thresh_right = self.w//2 + 100 for markerCorner in corners: (top_left, top_right, bottom_right, bottom_left) = markerCorner.reshape((4,2)) top_left = (int(top_left[0]), int(top_left[1])) @@ -71,21 +96,6 @@ class gripper_mover(): cv2.circle(img, self.depth_point, 8, (0,0,255), -1) - def markDetected2(self, img, corners): - thresh_left = self.w//2 - 100 - thresh_right = self.w//2 + 100 - for markerCorner in corners: - (top_left, top_right, bottom_right, bottom_left) = markerCorner.reshape((4,2)) - top_left = (int(top_left[0]), int(top_left[1])) - top_right = (int(top_right[0]), int(top_right[1])) - bottom_right = (int(bottom_right[0]), int(bottom_right[1])) - bottom_left = (int(bottom_left[0]), int(bottom_left[1])) - cv2.line(img, top_left, bottom_right, (0,255,0), 2) - cv2.line(img, top_right, bottom_left, (0,255,0), 2) - self.depth_point2 = (int((top_left[0] + bottom_right[0]) / 2), int((top_left[1] + bottom_right[1]) / 2)) - - cv2.circle(img, self.depth_point2, 8, (0,0,255), -1) - def callback_rgb(self, data): try: cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8") @@ -98,18 +108,13 @@ class gripper_mover(): cv_image, self.arucoDict, parameters=self.arucoParams ) - if corners: + if corners: #marker corners #Now seleted the corners of the tag with the right id => the tag that indicates the distance to the gripper - i = np.where(ids == self.target_id) + i = np.where(ids == self.target_id) # wahrscheinlich 1 und 0 if len(i[0]) > 0: fitting_corners = corners[i[0][0]] self.markDetected(cv_image, fitting_corners) tM = np.where(ids == 0) #Now try to get the marker that indicates the turtlebot - # if len(tM[0]) > 0: - # fitting_corners = corners[tM[0][0]] - # self.markDetected2(cv_image, fitting_corners) - # else: - # self.depth_point2 = (-1,-1) else: self.depth_point = (-1,-1) @@ -120,10 +125,10 @@ class gripper_mover(): #Give some additional informations idString = "IDs:" + str(ids) + " TargetID:" + str(self.target_id) cv2.putText(cv_image, idString, (10,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (200, 200, 200), 2, cv2.LINE_8) - depthString = "DepthPoint:" + str(self.depth_point[0]) +"x" +str(self.depth_point[1]) + " Distance to gripper:" + str(self.last_depth) + depthString = "DepthPoint:" + str(self.depth_point[0]) +"x" +str(self.depth_point[1]) + "Distance:" + str(self.last_depth) cv2.putText(cv_image, depthString, (10,60), cv2.FONT_HERSHEY_TRIPLEX , 0.8, (200, 200, 200), 2, cv2.LINE_8) - depthString2 = "Second DepthPoint:" + str(self.depth_point2[0]) +"x" +str(self.depth_point2[1]) + " Distance:" + str(self.last_depth2) + depthString2 = "LastMove:" + str(self.lastMove) cv2.putText(cv_image, depthString2, (10,90), cv2.FONT_HERSHEY_TRIPLEX , 0.8, (200, 200, 200), 2, cv2.LINE_8) self.rate.sleep() @@ -136,13 +141,28 @@ class gripper_mover(): #In 32FC1 each pixel is 4 bytes. Apart from that its a numpy array) cv_depth_image = self.depth_bridge.imgmsg_to_cv2(data, "passthrough") if self.depth_point != (-1,-1): + self.currentTime = time.time() # Here x and y are swapped. I dont know why. - notNull = cv_depth_image[self.depth_point[1]][self.depth_point[0]] - self.last_depth = notNull - - if self.depth_point2 != (-1,-1): - notNull = cv_depth_image[self.depth_point2[1]][self.depth_point2[0]] - self.last_depth2 = notNull + #notNull = cv_depth_image[self.depth_point[1]][self.depth_point[0]] + last_depth_2 = self.bDepth.wDepthImage(cv_depth_image,self.depth_point[1],self.depth_point[0]) + if last_depth_2 == 0 or last_depth_2 > 2000: + return + self.last_depth = last_depth_2 + if (self.currentTime - self.lastTime) > 4: + rr = self.last_depth*-0.2478+253.15 + st=0 + #self.gHelper.testRotate(int(rr)) + self.lastTime = time.time() + if self.last_depth < 1000: + self.lastMove = self.last_depth + rr = self.last_depth*-0.2478+253.15 + st=0 + while rr>st : + self.gHelper.testRotate(int(st)) + st=st+20 + self.gHelper.testPlace(int(rr)) + self.depth_point = (-1,-1) + rospy.signal_shutdown(1) def main(): if len(sys.argv) != 3: