This commit is contained in:
2024-02-14 16:48:29 +01:00
parent 6064d27577
commit f549aa72ee
+72 -57
View File
@@ -15,29 +15,14 @@ from cv_bridge import CvBridge, CvBridgeError
from geometry_msgs.msg import Twist
TARGET = 0
DEPTH = 400
DEPTH = 550
POSITIONS = ["left", "right", "middle"]
DEVICE = 'dev/ttyACM'
BAUD = 9600
def main():
if len(sys.argv) != 4:
print("Please give the following arguments <turtlebot-navi-topic> and <camera-img-topic> and <camera-depth-topic>")
print("Like: marker_attack.py /cmd_vel_mux/input/navi /camera/rgb/image_raw /camera/depth/image_raw")
return 1
ic = tutel(str(sys.argv[1]),str(sys.argv[2]),str(sys.argv[3]))
try:
rospy.spin()
except KeyboardInterrupt:
print("Shutting down")
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
class tutel():
def init(self, tutel_topic : str, astra_rgb_topic : str, astra_depth_topic : str, velocity_factor : float = 1.0):
def __init__(self, tutel_topic : str, 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.vel_fac = velocity_factor
@@ -45,16 +30,16 @@ class tutel():
#auruco init, best dictionary we found atm
self.arucoDict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_ARUCO_ORIGINAL)
self.aurcoParams = cv2.aruco.DetectorParameters_create()
self.arucoParams = cv2.aruco.DetectorParameters_create()
#minimal depth we recognize, still testing for values!
self.maxMinDepth = DEPTH
#Serial Pipeline init
self.com = self.init_serial()
self.device = DEVICE+str(self.com)
self.baud = BAUD
self.serial_pipeline = serial.Serial(self.device, self.baud, timeout = 1)
#self.com = self.init_serial()
#self.device = DEVICE+str(self.com)
#self.baud = BAUD
#self.serial_pipeline = serial.Serial(self.device, self.baud, timeout = 1)
#Tutel_control
self.pub = rospy.Publisher(tutel_topic, Twist, queue_size = 1)
@@ -70,8 +55,13 @@ class tutel():
#depth related
self.depth_bridge = CvBridge()
self.depth_state = False
self.last_depth = 10
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
#ros node related
rospy.init_node("turtle_control", anonymous=True)
@@ -116,7 +106,7 @@ class tutel():
if self.search:
self.t.linear.x = 0.0 * self.vel_fac
self.t.angular.z = 0.4 * self.vel_fac
self.t.angular.z = 0.1 * self.vel_fac
self.pub.publish(self.t)
if corners:
self.target_id = ids[0]
@@ -125,10 +115,10 @@ class tutel():
if corners:
x = np.where(ids == self.target_id)
if len(x[0]) > 0:
fitting_corners = corners(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:
elif(self.lastConernerTime - time.time()) < 1./2:
self.depth_state = False
if self.position is POSITIONS[0]:
cv2.rectangle(rgb_image, (0,0), (20, self.h), (0,0,255), cv2.FILLED)
@@ -138,7 +128,7 @@ class tutel():
elif self.position is POSITIONS[1]:
cv2.rectangle(rgb_image, (self.w-20,0), (self.w, self.h), (0,0,255), cv2.FILLED)
self.t.linear.x = 0.0 * self.vel_fac
self.t.angular.z = 0.3 * self.vel_fac
self.t.angular.z = -0.3 * self.vel_fac
self.pub.publish(self.t)
#some additional info on screen
@@ -158,73 +148,98 @@ class tutel():
if self.depth_state:
depth_image = self.depth_bridge.imgmsg_to_cv2(data, "passthrough")
if self.depth_counter == 0:
#pass
self.last_depth = 0
self.number_of_operations = 0
#self.number_of_operations = 0
#x and y are swapped, because
for x in range(self.top_left.x, self.top_right.x+1):
for y in range(self.top_left.y, self.bottom_left.y+1):
print(str(self.top_left[0]) + " " + str(self.top_right[0]) + " " + str(self.top_left[1]) + " " + str(self.bottom_right[1]))
for x in range(self.top_left[0], self.top_right[0]+1):
for y in range(self.top_left[1], self.bottom_left[1]+1):
print("First loop (" + str(x) + "," + str(y) + ")" + " " + str(depth_image[y][x]))
self.last_depth += depth_image[y][x]
self.number_of_operations += 1
for y in range(self.top_left.y, self.top_right.y+1):
for x in range(self.top_left.x, self.bottom_left.x+1):
for y in range(self.top_left[1], self.top_right[1]+1):
for x in range(self.top_left[0], self.bottom_left[0]+1):
print("Second loop (" + str(x) + "," + str(y) + ")" + " " + str(depth_image[y][x]))
self.last_depth += depth_image[y][x]
self.number_of_operations += 1
self.last_depth /= number_of_operations
self.last_depth /= self.number_of_operations
print(self.depth_counter, self.last_depth, self.number_of_operations)
if self.last_depth < self.maxMinDepth and self.last_depth != 0:
self.depth_counter += 1
if self.depth_counter > 4:
print("Reached our destination")
self.t.linear.x = 0.0 * self.vel_fac
self.t.angular.z = 0.0 * self.vel_fac
self.pub.publish(t)
self.pub.publish(self.t)
self.position = POSITIONS[2]
self.move_arm(self)
self.depth_state = False
self.number_of_operations = 0
#self.move_arm(self)
else:
self.depth_counter = 0
self.number_of_operations = 0
self.depth_state = False
def markDetected(self, image, 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]))
(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, top_left, bottom_right, (0,255,0), 2)
cv2.line(image, top_right, bottom_right, (0,255,0), 2)
cv2.line(image, self.top_left, self.bottom_right, (0,255,0), 2)
cv2.line(image, self.top_right, self.bottom_right, (0,255,0), 2)
self.depth_state = True
self.last_marker_position = ((top_left), (bottom_right))
if ((top_left[0] > thresh_left and top_left[0] < thresh_right) or
(top_right[0] > thresh_left and top_right[0] < thresh_right) or
(bottom_left[0] > thresh_left and bottom_left[0] < thresh_right) or
(bottom_right[0] > thresh_left and bottom_right[0] < thresh_right)):
self.t.linear.x = 0.1 * self.vel_fac
self.last_marker_position = ((self.top_left), (self.bottom_right))
if ((self.top_left[0] > thresh_left and self.top_left[0] < thresh_right) or
(self.top_right[0] > thresh_left and self.top_right[0] < thresh_right) or
(self.bottom_left[0] > thresh_left and self.bottom_left[0] < thresh_right) or
(self.bottom_right[0] > thresh_left and self.bottom_right[0] < thresh_right)):
#self.t.linear.x = 0.1 * self.vel_fac
self.t.angular.z = 0.0 * self.vel_fac
self.pub.publish(self.t)
elif(top_right[0] < thresh_left or
bottom_right[0] < thresh_left):
elif(self.top_right[0] < thresh_left or
self.bottom_right[0] < thresh_left):
self.t.linear.x = 0.0 * self.vel_fac
self.t.angular.z = 0.3 * self.vel_fac
self.t.angular.z = 0.1 * self.vel_fac
self.pub.publish(self.t)
elif(top_left[0] > thresh_right or
bottom_left[0] > thresh_right):
elif(self.top_left[0] > thresh_right or
self.bottom_left[0] > thresh_right):
self.t.linear.x = 0.0 * self.vel_fac
self.t.angular.z = -0.3 * self.vel_fac
self.t.angular.z = -0.1 * self.vel_fac
self.pub.publish(self.t)
if int(self.w / 2) > top_left[0]:
if int(self.w / 2) > self.top_left[0]:
self.position = POSITIONS[0]
elif self.w - int(self.w / 2) < top_left[0]:
elif self.w - int(self.w / 2) < self.top_left[0]:
self.position = POSITIONS[1]
return img
return image
def move_arm(self):
self.s.write('1'.encode('utf-8'))
time.sleep(1)
def main():
if len(sys.argv) != 4:
print("Please give the following arguments <turtlebot-navi-topic> and <camera-img-topic> and <camera-depth-topic>")
print("Like: marker_attack.py /cmd_vel_mux/input/navi /camera/rgb/image_raw /camera/depth/image_raw")
return 1
ic = tutel(str(sys.argv[1]),str(sys.argv[2]),str(sys.argv[3]))
try:
rospy.spin()
except KeyboardInterrupt:
print("Shutting down")
cv2.destroyAllWindows()
if __name__ == "__main__":
main()