diff --git a/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/approximation_comprehension.py b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/approximation_comprehension.py new file mode 100644 index 0000000..08f7692 --- /dev/null +++ b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/approximation_comprehension.py @@ -0,0 +1,113 @@ +pixel_obere_kante = [26, 28.5, 30.5, 32, 34.5, 36, 38.5, 42, 46, 50, 55, 61.5, 69, 79, 92.5, 111, 139.5, 186, 276] +pixel_durchschnitt = [27.5, 28.75, 30.5, 32, 34.25, 36.75, 39.25, 42.5, 46.375, 50, 55.25, 61, 68.75, 78.75, 91.64666667, 110.25, 137, 181.75, 266.25] +pixel_max = [28, 29, 31, 33, 35, 37, 40, 43, 46, 51, 56, 62, 70, 80, 93, 113, 141, 188, 281] + +def approximation_obere_kante(y): + return (y/2914.2987296469)**(-1/1.0159029035) + +def approximation_durchschnitt(y): + return (y/2675.4910012317)**(-1/0.993434027) + +def approximation_max(y): + return (y/2858.4636643479)**(-1/1.0056840993) + +def main(): + i = 100 + for y in pixel_obere_kante: + obere_kante = approximation_obere_kante(y) + durchschnitt = approximation_durchschnitt(y) + maximum = approximation_max(y) + mittel_obere_kante_durchschnitt = (obere_kante + durchschnitt)/2 + mittel_obere_kante_maximum = (obere_kante + maximum)/2 + mittel_durchschnitt_maximum = (durchschnitt + maximum)/2 + mittel_all = (obere_kante + durchschnitt + maximum)/3 + abweichung_obere_kante = abs(i - obere_kante) + abweichung_durchschnitt = abs(i - durchschnitt) + abweichung_maximum = abs(i - maximum) + abweichung_mittel_obere_kante_durchschnitt = abs(i - mittel_obere_kante_durchschnitt) + abweichung_mittel_obere_kante_maximum = abs(i - mittel_obere_kante_maximum) + abweichung_durchschnitt_maximum = abs(i - mittel_durchschnitt_maximum) + abweichung_all = abs(i - mittel_all) + print("Gewünschtes Ergebnis: " + str(i)) + print("Approximation Obere Kante: " + str(obere_kante)) + print("Approximation Durchschnitt: " + str(durchschnitt)) + print("Approximation Max: " + str(maximum)) + print("Durchschnitt Obere Kante und Durchschnitt: " + str(mittel_obere_kante_durchschnitt)) + print("Durchschnitt Obere Kante und Maximum: " + str(mittel_obere_kante_maximum)) + print("Durchschnitt Durchschnitt und Maximum: " + str(mittel_durchschnitt_maximum)) + print("Durchschnitt Alle: " + str(mittel_all)) + print("Abweichung Obere Kante: " + str(abweichung_obere_kante)) + print("Abweichung Durchschnitt: " + str(abweichung_durchschnitt)) + print("Abweichung Maximum: " + str(abweichung_maximum)) + print("Abweichung Durchschnitt Obere Kante und Durchschnitt: " + str(abweichung_mittel_obere_kante_durchschnitt)) + print("Abweichung Durchschnitt Obere Kante und Maximum: " + str(abweichung_mittel_obere_kante_maximum)) + print("Abweichung Durschnitt Durchschnitt und Maximum: " + str(abweichung_durchschnitt_maximum)) + print("Abweichung Alle: " + str(abweichung_all)) + i = i - 5 + i = 100 + for y in pixel_durchschnitt: + obere_kante = approximation_obere_kante(y) + durchschnitt = approximation_durchschnitt(y) + maximum = approximation_max(y) + mittel_obere_kante_durchschnitt = (obere_kante + durchschnitt)/2 + mittel_obere_kante_maximum = (obere_kante + maximum)/2 + mittel_durchschnitt_maximum = (durchschnitt + maximum)/2 + mittel_all = (obere_kante + durchschnitt + maximum)/3 + abweichung_obere_kante = abs(i - obere_kante) + abweichung_durchschnitt = abs(i - durchschnitt) + abweichung_maximum = abs(i - maximum) + abweichung_mittel_obere_kante_durchschnitt = abs(i - mittel_obere_kante_durchschnitt) + abweichung_mittel_obere_kante_maximum = abs(i - mittel_obere_kante_maximum) + abweichung_durchschnitt_maximum = abs(i - mittel_durchschnitt_maximum) + abweichung_all = abs(i - mittel_all) + print("Gewünschtes Ergebnis: " + str(i)) + print("Approximation Obere Kante: " + str(obere_kante)) + print("Approximation Durchschnitt: " + str(durchschnitt)) + print("Approximation Max: " + str(maximum)) + print("Durchschnitt Obere Kante und Durchschnitt: " + str(mittel_obere_kante_durchschnitt)) + print("Durchschnitt Obere Kante und Maximum: " + str(mittel_obere_kante_maximum)) + print("Durchschnitt Durchschnitt und Maximum: " + str(mittel_durchschnitt_maximum)) + print("Durchschnitt Alle: " + str(mittel_all)) + print("Abweichung Obere Kante: " + str(abweichung_obere_kante)) + print("Abweichung Durchschnitt: " + str(abweichung_durchschnitt)) + print("Abweichung Maximum: " + str(abweichung_maximum)) + print("Abweichung Durchschnitt Obere Kante und Durchschnitt: " + str(abweichung_mittel_obere_kante_durchschnitt)) + print("Abweichung Durchschnitt Obere Kante und Maximum: " + str(abweichung_mittel_obere_kante_maximum)) + print("Abweichung Durschnitt Durchschnitt und Maximum: " + str(abweichung_durchschnitt_maximum)) + print("Abweichung Alle: " + str(abweichung_all)) + i = i - 5 + i = 100 + for y in pixel_max: + obere_kante = approximation_obere_kante(y) + durchschnitt = approximation_durchschnitt(y) + maximum = approximation_max(y) + mittel_obere_kante_durchschnitt = (obere_kante + durchschnitt)/2 + mittel_obere_kante_maximum = (obere_kante + maximum)/2 + mittel_durchschnitt_maximum = (durchschnitt + maximum)/2 + mittel_all = (obere_kante + durchschnitt + maximum)/3 + abweichung_obere_kante = abs(i - obere_kante) + abweichung_durchschnitt = abs(i - durchschnitt) + abweichung_maximum = abs(i - maximum) + abweichung_mittel_obere_kante_durchschnitt = abs(i - mittel_obere_kante_durchschnitt) + abweichung_mittel_obere_kante_maximum = abs(i - mittel_obere_kante_maximum) + abweichung_durchschnitt_maximum = abs(i - mittel_durchschnitt_maximum) + abweichung_all = abs(i - mittel_all) + print("Gewünschtes Ergebnis: " + str(i)) + print("Approximation Obere Kante: " + str(obere_kante)) + print("Approximation Durchschnitt: " + str(durchschnitt)) + print("Approximation Max: " + str(maximum)) + print("Durchschnitt Obere Kante und Durchschnitt: " + str(mittel_obere_kante_durchschnitt)) + print("Durchschnitt Obere Kante und Maximum: " + str(mittel_obere_kante_maximum)) + print("Durchschnitt Durchschnitt und Maximum: " + str(mittel_durchschnitt_maximum)) + print("Durchschnitt Alle: " + str(mittel_all)) + print("Abweichung Obere Kante: " + str(abweichung_obere_kante)) + print("Abweichung Durchschnitt: " + str(abweichung_durchschnitt)) + print("Abweichung Maximum: " + str(abweichung_maximum)) + print("Abweichung Durchschnitt Obere Kante und Durchschnitt: " + str(abweichung_mittel_obere_kante_durchschnitt)) + print("Abweichung Durchschnitt Obere Kante und Maximum: " + str(abweichung_mittel_obere_kante_maximum)) + print("Abweichung Durschnitt Durchschnitt und Maximum: " + str(abweichung_durchschnitt_maximum)) + print("Abweichung Alle: " + str(abweichung_all)) + i = i - 5 + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/depth_reader.py b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/depth_reader.py new file mode 100644 index 0000000..2a616a6 --- /dev/null +++ b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/depth_reader.py @@ -0,0 +1,208 @@ +#!/usr/bin/env python +from __future__ import print_function + +import roslib +import sys +import rospy +import cv2 +import time +import numpy as np +import serial +import subprocess +from std_msgs.msg import String +from sensor_msgs.msg import Image +from cv_bridge import CvBridge, CvBridgeError +from geometry_msgs.msg import Twist + +TARGET = 0 +DEPTH = 460 +POSITIONS = ["left", "right", "middle"] +DEVICE = 'dev/ttyACM' +BAUD = 9600 + + +class tutel(): + def __init__(self, astra_rgb_topic : str, astra_depth_topic : str, velocity_factor : float = 1.0): + #needed variables, velocity factor is a multiplier for the bot, might be changed later + #target_id used to remember the last id the tutel found, set to 0 at the beginning + self.target_id = TARGET + + #auruco init, best dictionary we found atm + self.arucoDict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_ARUCO_ORIGINAL) + self.arucoParams = cv2.aruco.DetectorParameters_create() + + #minimal depth we recognize, still testing for values! + self.min_threshold = DEPTH + self.max_threshold = 1200 + + #cv related + #rgb related + self.lastConernerTime = time.time() + self.rgb_bridge = CvBridge() + self.search = True + #depth related + self.depth_bridge = CvBridge() + self.depth_state = False + self.last_depth = 10.0 + self.depth_counter = 0 + self.top_left = (-1,-1) + self.top_right = (-1,-1) + self.bottom_right = (-1,-1) + self.bottom_left = (-1,-1) + self.number_of_operations = 0 + self.number_of_pixels = 0 + + #ros node related + rospy.init_node("turtle_control", anonymous=True) + + #update rate can be increased + self.rate = rospy.Rate(15) + + #Sub init + self.rgb_sub = rospy.Subscriber(astra_rgb_topic, Image, self.callback_rgb) + self.depth_sub = rospy.Subscriber(astra_depth_topic, Image, self.callback_depth) + + #we wait for both publishers, camera topics, to send information + def wait_publishers(self, astra_rgb_topic, astra_depth_topic): + while True: + if any(astra_rgb_topic in topic_list for topic_list in rospy.get_published_topics()) and any(astra_depth_topic in topic_list for topic_list in rospy.get_published_topics()): + break + print("Waiting for {} and {} to start.".format(astra_rgb_topic, astra_depth_topic)) + rospy.sleep(1) + + def callback_rgb(self, data): + try: + rgb_image = self.rgb_bridge.imgmsg_to_cv2(data, "bgr8") + #cv2.rotate(rgb_image, cv2.ROTATE_180, rgb_image) + (self.h, self.w) = rgb_image.shape[:2] + except CvBridgeError as e: + print(e) + + (corners, ids, rejected) = cv2.aruco.detectMarkers(rgb_image, self.arucoDict, parameters = self.arucoParams) + if corners: + for id_x in ids: + if id_x != 0: + self.target_id = id_x + break + x = np.where(ids == self.target_id) + if len(x[0]) > 0: + fitting_corners = corners[x[0][0]] + rgb_image = self.markDetected(rgb_image, fitting_corners) + self.lastConernerTime = time.time() + elif(self.lastConernerTime - time.time()) < 1./2: + self.depth_state = False + + #some additional info on screen + depth_str = "Depth_State:" + str(self.depth_state) + " Depth:" + str(self.last_depth) + cv2.putText(rgb_image, depth_str, (10,60), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8) + pixel_str = "Number_of_pixel:" + str(self.number_of_pixels) + cv2.putText(rgb_image, pixel_str, (10,90), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8) + cv2.line(rgb_image, (self.w//2, 0), (self.w//2, self.h), (0,255,0), 2) + self.rate.sleep() + cv2.imshow("Tutel", rgb_image) + cv2.waitKey(1) + + + def callback_depth(self, data): + if self.depth_state: + depth_image = self.depth_bridge.imgmsg_to_cv2(data, "passthrough") + #cv2.rotate(depth_image, cv2.ROTATE_180, depth_image) + if self.depth_counter == 0: + #pass + self.last_depth = 10.0 + x = (0,0) + y = (0,0) + #x and y are swapped, because + if (self.top_left[0] - self.bottom_left[0]) in range (-10,10): + if self.top_left[0] < self.top_right[0]: x = (self.top_left[0],self.top_right[0]) + else: x = (self.top_right[0],self.top_left[0]) + if self.top_left[1] < self.bottom_left[1]: y = (self.top_left[1],self.bottom_left[1]) + else: y = (self.bottom_left[1],self.top_left[1]) + elif (self.top_left[0] - self.top_right[0]) in range (-10,10): + if self.bottom_left[0] < self.top_left[0]: x = (self.bottom_left[0],self.top_left[0]) + else: x = (self.top_left[0],self.bottom_left[0]) + if self.bottom_left[1] < self.bottom_right[1]: y = (self.bottom_left[1],self.bottom_right[1]) + else: y = (self.bottom_right[1],self.bottom_left[1]) + depth_values = [] + pixels = (abs(self.top_left[0]-self.top_right[0]), abs(self.top_left[1] - self.bottom_left[1]), abs(self.top_right[1]-self.bottom_right[1]), abs(self.bottom_right[0]-self.bottom_left[0])) + self.pixel_length = max(pixels) + self.pixel_depth = ((self.pixel_length/2858.4636643479)**(-1/1.0056840993))*10 + #self.pixel_depth = (self.pixel_depth + ((((self.x[1] - self.x[0] + self.y[1] - self.y[0]) / 2)/2914.29872964688)**(-1/1.01590290349592))*10)/2 + depth_values = [] + M = ((x[0] + (x[1]-x[0])//2), (y[0] + (y[1]-y[0])//2)) + depth_values.append(depth_image[M[1]][M[0]]) + #for i in range(self.x[0], self.x[1]): + #for j in range(self.y[0], self.y[1]): + #if depth_image[j][i] > self.min_threshold and depth_image[j][i] < self.max_threshold and depth_image[j][i] < self.pixel_depth: + #depth_values.append(depth_image[j][i]) + #print(self.pixel_depth, depth_values, self.last_depth) + if self.pixel_depth > self.max_threshold: + if self.pixel_depth < self.last_depth or self.last_depth == 10: + self.last_depth = self.pixel_depth + else: + self.last_depth = self.last_depth + else: + if len(depth_values)>0 and ((max(depth_values) > self.min_threshold and max(depth_values) < self.last_depth) or self.last_depth == 10): + self.last_depth = max(depth_values) + elif (len(depth_values)>0 and max(depth_values) < self.min_threshold) or len(depth_values)<1: + self.last_depth = self.pixel_depth + else: + self.last_depth = self.last_depth + lengths = [abs(self.top_left[0]-self.top_right[0]), abs(self.top_left[1] - self.bottom_left[1]), abs(self.top_right[1]-self.bottom_right[1]), abs(self.bottom_right[0]-self.bottom_left[0])] + #pixel_length = (abs(self.top_left[0]-self.top_right[0]) + abs(self.top_left[1] - self.bottom_left[1]) + abs(self.top_right[1]-self.bottom_right[1]) + abs(self.bottom_right[0]-self.bottom_left[0]))/4 + pixel_length = max(lengths) + M_Kante = (M[0], y[0]) + print(pixel_length, M_Kante) + self.number_of_operations = 0 + self.number_of_pixels = 0 + pixel_length = 0 + #cv2.imshow("Tutel_Depth", depth_image) + if self.last_depth < self.min_threshold and self.last_depth != 0: + self.depth_counter += 1 + if self.depth_counter > 0: + self.depth_state = False + self.last_depth = 10.0 + self.depth_counter = 0 + else: + self.depth_counter = 0 + + + def markDetected(self, image, corners): + for markerCorner in corners: + (self.top_left, self.top_right, self.bottom_right, self.bottom_left) = markerCorner.reshape((4,2)) + self.top_left = (int(self.top_left[0]), int(self.top_left[1])) + self.top_right = (int(self.top_right[0]), int(self.top_right[1])) + self.bottom_right = (int(self.bottom_right[0]), int(self.bottom_right[1])) + self.bottom_left = (int(self.bottom_left[0]), int(self.bottom_left[1])) + + cv2.line(image, self.top_left, self.top_right, (0,255,0), 2) + cv2.line(image, self.top_right, self.bottom_right, (0,255,0), 2) + cv2.line(image, self.bottom_right, self.bottom_left, (0,255,0), 2) + cv2.line(image, self.bottom_left, self.top_left, (0,255,0), 2) + + self.depth_state = True + return image + + + def move_arm(self): + self.s.write('1'.encode('utf-8')) + time.sleep(1) + +def main(): + if len(sys.argv)!=3: + print("Please give the following arguments and and ") + print("Like: depth_reader.py /camera/rgb/image_raw /camera/depth/image_raw") + return 1 + ic = tutel(str(sys.argv[1]),str(sys.argv[2])) + try: + rospy.spin() + except KeyboardInterrupt: + print("Shutting down") + cv2.destroyAllWindows() + + +if __name__ == "__main__": + main() + + + diff --git a/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/tiefeXpixel.ods b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/tiefeXpixel.ods new file mode 100644 index 0000000..45ef9ac Binary files /dev/null and b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/tiefeXpixel.ods differ diff --git a/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/tiefeXpixel[110mmX110mm].ods b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/tiefeXpixel[110mmX110mm].ods new file mode 100644 index 0000000..8915dc3 Binary files /dev/null and b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/depth_related/tiefeXpixel[110mmX110mm].ods differ