Dateien nach "Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/old_scripts" hochladen
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
#!/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
|
||||
|
||||
class marker_attacker():
|
||||
def wait_for_subscribers(self):
|
||||
while self.pub.get_num_connections() == 0:
|
||||
print("Waiting for subscriber on {}".format(self.pub.name))
|
||||
rospy.sleep(1)
|
||||
if rospy.is_shutdown():
|
||||
raise Exception("Got shutdown request before subscribers connected")
|
||||
def wait_for_publisher(self,astra_camera_rgb_topic,astra_camera_depth_topic):
|
||||
while 1:
|
||||
x = rospy.get_published_topics()
|
||||
if any(astra_camera_depth_topic in subl for subl in x) and any(astra_camera_depth_topic in subl for subl in x):
|
||||
break
|
||||
print("Waiting for {} and {}".format(astra_camera_rgb_topic,astra_camera_depth_topic))
|
||||
rospy.sleep(1)
|
||||
|
||||
def __init__(self, turlte_bot_topic : str, astra_camera_rgb_topic : str, astra_camera_depth_topic : str ,speed_emul : float = 1, start_id : int = 0):
|
||||
|
||||
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 = 550
|
||||
self.com = self.init_serial()
|
||||
DEVICE = '/dev/ttyACM'+str(self.com)
|
||||
BAUD = 9600
|
||||
self.s = serial.Serial(DEVICE, BAUD, timeout=1)
|
||||
### For turtle controll
|
||||
self.pub = rospy.Publisher(turlte_bot_topic, Twist, queue_size=1)
|
||||
# self.wait_for_subscribers()
|
||||
self.wait_for_publisher(astra_camera_rgb_topic,astra_camera_depth_topic)
|
||||
self.t = Twist()
|
||||
self.leftright = 0
|
||||
self.lastConernerTime = time.time()
|
||||
self.bridge = CvBridge()
|
||||
rospy.init_node("turtle_control", anonymous=True)
|
||||
self.rate = rospy.Rate(15) # update rate can be increased
|
||||
|
||||
self.depth_point = (-1,-1) #This is the point from where we get the depth
|
||||
self.last_depth = 10 #This is the last depth from the depth_point
|
||||
|
||||
self.depth_counter = 0 #Make sure we are really there by counting how many time we got this result
|
||||
self.rState = 0
|
||||
self.image_sub = rospy.Subscriber(
|
||||
astra_camera_rgb_topic, Image, self.callback_rgb
|
||||
)
|
||||
|
||||
self.depth_bridge = CvBridge()
|
||||
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]))
|
||||
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_point = (int((top_left[0] + bottom_right[0]) / 2), int((top_left[1] + bottom_right[1]) / 2))
|
||||
|
||||
cv2.circle(img, self.depth_point, 8, (0,0,255), -1)
|
||||
|
||||
self.lastMarkerPos = ((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.speed_mul
|
||||
self.t.angular.z = 0.0
|
||||
self.pub.publish(self.t)
|
||||
elif(top_right[0] < thresh_left or
|
||||
bottom_right[0] < thresh_left):
|
||||
self.t.linear.x = 0.0
|
||||
self.t.angular.z = 0.3 * self.speed_mul
|
||||
self.pub.publish(self.t)
|
||||
elif(top_left[0] > thresh_right or
|
||||
bottom_left[0] > thresh_right):
|
||||
self.t.linear.x = 0.0
|
||||
self.t.angular.z = -0.3 * self.speed_mul
|
||||
self.pub.publish(self.t)
|
||||
if int(self.w / 2) > top_left[0]:
|
||||
self.leftright = 1
|
||||
elif self.w - int(self.w / 2) < top_left[0]:
|
||||
self.leftright = 2
|
||||
|
||||
return img
|
||||
|
||||
def callback_rgb(self, data):
|
||||
try:
|
||||
cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
|
||||
(self.h, self.w) = cv_image.shape[:2]
|
||||
except CvBridgeError as e:
|
||||
print(e)
|
||||
|
||||
#(rows, cols, channels) = cv_image.shape
|
||||
(corners, ids, rejected) = cv2.aruco.detectMarkers(
|
||||
cv_image, self.arucoDict, parameters=self.arucoParams
|
||||
)
|
||||
|
||||
if self.rState == 0:
|
||||
#This is a searching state, we are searching for right tag
|
||||
self.t.linear.x = 0.0
|
||||
self.t.angular.z = 0.4 * self.speed_mul
|
||||
self.pub.publish(self.t)
|
||||
if corners:
|
||||
i = np.where(ids == self.target_id)
|
||||
if len(i[0]) > 0:
|
||||
self.rState = 1
|
||||
elif self.rState == 1:
|
||||
#####
|
||||
# verify *at least* one ArUCo marker was detected
|
||||
if corners:
|
||||
#Now seleted the corners of the tag with the right id
|
||||
i = np.where(ids == self.target_id)
|
||||
if len(i[0]) > 0:
|
||||
fitting_corners = corners[i[0][0]]
|
||||
cv_image = self.markDetected(cv_image, fitting_corners)
|
||||
self.lastConernerTime = time.time()
|
||||
|
||||
elif (self.lastConernerTime - time.time()) < 1./2:
|
||||
self.depth_point = (-1,-1) #Indicates that we dont need depth to be processed
|
||||
if self.leftright == 1:
|
||||
cv2.rectangle(cv_image, (0, 0), (20, self.h), (0,0,255), cv2.FILLED)
|
||||
self.t.linear.x = 0.0
|
||||
self.t.angular.z = 0.3 * self.speed_mul
|
||||
self.pub.publish(self.t)
|
||||
elif self.leftright == 2:
|
||||
cv2.rectangle(cv_image, (self.w-20, 0), (self.w, self.h), (0,0,255), cv2.FILLED)
|
||||
self.t.linear.x = 0.0
|
||||
self.t.angular.z = -0.3 * self.speed_mul
|
||||
self.pub.publish(self.t)
|
||||
|
||||
#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, (255, 0, 0), 2, cv2.LINE_8)
|
||||
depthString = "DepthPoint:" + str(self.depth_point[0]) +"x" +str(self.depth_point[1]) + " Depth:" + str(self.last_depth)
|
||||
cv2.putText(cv_image, depthString, (10,60), cv2.FONT_HERSHEY_TRIPLEX , 0.8, (255, 0, 0), 2, cv2.LINE_8)
|
||||
|
||||
depthString = "Res:" + str(self.w) + "x" + str(self.h) + " RState:" + str(self.rState) + " MMDepth:" + str(self.maxMinDepth)
|
||||
cv2.putText(cv_image, depthString, (10,90), cv2.FONT_HERSHEY_TRIPLEX , 0.8, (255, 0, 0), 2, cv2.LINE_8)
|
||||
self.rate.sleep()
|
||||
cv2.imshow("Window", cv_image)
|
||||
cv2.waitKey(1)
|
||||
|
||||
def callback_depth(self, data):
|
||||
if self.depth_point != (-1,-1):
|
||||
#It would be possible to convert this to 32FC1
|
||||
#32FC1 means that each pixel value is stored as one channel floating point with single precision
|
||||
#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")
|
||||
# Here x and y are swapped. I dont know why.
|
||||
self.last_depth = cv_depth_image[self.depth_point[1]][self.depth_point[0]]
|
||||
if self.last_depth < self.maxMinDepth and self.last_depth != 0:
|
||||
self.depth_counter += 1 #Sometimes the data is inacurate make sure we get the data at least 5times
|
||||
if self.depth_counter > 4:
|
||||
print("We are really there")
|
||||
#self.target_id += 1
|
||||
#self.target_id = self.target_id % 2
|
||||
self.move_arm(self)
|
||||
#self.depth_counter = 0
|
||||
#self.last_depth = 10
|
||||
#self.depth_point = (-1,-1)
|
||||
#self.leftright = -1
|
||||
#self.rState = 0 #Start searching for the next tag
|
||||
else:
|
||||
self.depth_counter = 0
|
||||
#We dont need to sleep twice
|
||||
#self.rate.sleep()
|
||||
|
||||
def init_serial():
|
||||
for com in range(0,4):
|
||||
subprocess.run(["fuser","/dev/ttyACM"+str(com),"-k"])
|
||||
try:
|
||||
PORT = '/dev/ttyACM' + str(com)
|
||||
BAUD = 9600
|
||||
board = serial.Serial(PORT, BAUD)
|
||||
board.close()
|
||||
break
|
||||
except:
|
||||
pass
|
||||
return com
|
||||
|
||||
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 = marker_attacker(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()
|
||||
@@ -0,0 +1,253 @@
|
||||
#!/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 = 550
|
||||
POSITIONS = ["left", "right", "middle"]
|
||||
DEVICE = 'dev/ttyACM'
|
||||
BAUD = 9600
|
||||
|
||||
|
||||
class tutel():
|
||||
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
|
||||
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.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)
|
||||
|
||||
#Tutel_control
|
||||
self.pub = rospy.Publisher(tutel_topic, Twist, queue_size = 1)
|
||||
self.wait_publishers(astra_rgb_topic, astra_depth_topic)
|
||||
self.t = Twist()
|
||||
|
||||
#cv related
|
||||
#rgb related
|
||||
self.position = POSITIONS[-1]
|
||||
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
|
||||
|
||||
#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)
|
||||
|
||||
#finding specific port for arduino communication
|
||||
def init_serial(self):
|
||||
for com in range(0,4):
|
||||
subprocess.run(["fuser", "/dev/ttyACM"+str(com), "-k"])
|
||||
try:
|
||||
port = DEVICE+str(com)
|
||||
baud = BAUD
|
||||
board = serial.Serial(port, baud)
|
||||
board.close()
|
||||
break
|
||||
except:
|
||||
pass
|
||||
return com
|
||||
|
||||
#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")
|
||||
(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 self.search:
|
||||
self.t.linear.x = 0.0 * self.vel_fac
|
||||
self.t.angular.z = 0.1 * self.vel_fac
|
||||
self.pub.publish(self.t)
|
||||
if corners:
|
||||
self.target_id = ids[0]
|
||||
self.search = False
|
||||
else:
|
||||
if corners:
|
||||
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
|
||||
if self.position is POSITIONS[0]:
|
||||
cv2.rectangle(rgb_image, (0,0), (20, 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.pub.publish(self.t)
|
||||
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.pub.publish(self.t)
|
||||
|
||||
#some additional info on screen
|
||||
id_str = "IDs: " + str(ids) + " Target_ID:" + str(self.target_id)
|
||||
cv2.putText(rgb_image, id_str, (10,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
|
||||
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)
|
||||
info_str = "Resolution: " + str(self.w) + "x" + str(self.h) + " Search_State: " + str(self.search) + " Max_Min_Depth: " + str(self.maxMinDepth)
|
||||
cv2.putText(rgb_image, info_str, (10,90), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
|
||||
|
||||
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")
|
||||
if self.depth_counter == 0:
|
||||
#pass
|
||||
self.last_depth = 10.0
|
||||
x = (0,0)
|
||||
y = (0,0)
|
||||
#self.number_of_operations = 0
|
||||
#x and y are swapped, because
|
||||
print(str(self.top_left[0]) + " " + str(self.top_right[0]) + " " + str(self.top_left[1]) + " " + str(self.bottom_right[1]))
|
||||
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])
|
||||
cv2.line(depth_image, (x[0],y[0]), (x[1],y[1]), (0,255,0), 2)
|
||||
for i in range(x[0], x[1]):
|
||||
for j in range(y[0], y[1]):
|
||||
self.last_depth += depth_image[j][i]
|
||||
self.number_of_operations += 1
|
||||
self.last_depth /= self.number_of_operations
|
||||
print(self.depth_counter, self.last_depth, self.number_of_operations)
|
||||
self.number_of_operations = 0
|
||||
cv2.imshow("Tutel_Depth", depth_image)
|
||||
if self.last_depth < self.maxMinDepth and self.last_depth != 0:
|
||||
self.depth_counter += 1
|
||||
if self.depth_counter > 0:
|
||||
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(self.t)
|
||||
self.position = POSITIONS[2]
|
||||
self.depth_counter = 0
|
||||
self.depth_state = False
|
||||
self.last_depth = 10.0
|
||||
#self.move_arm(self)
|
||||
else:
|
||||
self.depth_counter = 0
|
||||
|
||||
|
||||
def markDetected(self, image, corners):
|
||||
thresh_left = self.w//2 - 100
|
||||
thresh_right = self.w//2 + 100
|
||||
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.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 = ((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(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.1 * self.vel_fac
|
||||
self.pub.publish(self.t)
|
||||
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.1 * self.vel_fac
|
||||
self.pub.publish(self.t)
|
||||
if int(self.w / 2) > self.top_left[0]:
|
||||
self.position = POSITIONS[0]
|
||||
elif self.w - int(self.w / 2) < self.top_left[0]:
|
||||
self.position = POSITIONS[1]
|
||||
|
||||
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()
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
#!/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 = 550
|
||||
|
||||
class Tutel_Depth():
|
||||
def __init__(self, tutel_topic : str, astra_rgb_topic : str, astra_depth_topic : str):
|
||||
#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.maxMinDepth = DEPTH
|
||||
|
||||
#Tutel_control
|
||||
#self.pub = rospy.Publisher(tutel_topic, Twist, queue_size = 1)
|
||||
self.wait_publishers(astra_rgb_topic, astra_depth_topic)
|
||||
#self.t = Twist()
|
||||
|
||||
#cv related
|
||||
#rgb related
|
||||
self.rgb_bridge = CvBridge()
|
||||
self.lastConernerTime = time.time()
|
||||
#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
|
||||
|
||||
#ros node related
|
||||
rospy.init_node("turtle_depth", 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")
|
||||
(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:
|
||||
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
|
||||
id_str = "IDs: " + str(ids) + " Target_ID:" + str(self.target_id)
|
||||
cv2.putText(rgb_image, id_str, (10,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
|
||||
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)
|
||||
info_str = "Resolution: " + str(self.w) + "x" + str(self.h) + " Max_Min_Depth: " + str(self.maxMinDepth)
|
||||
cv2.putText(rgb_image, info_str, (10,90), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
|
||||
|
||||
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")
|
||||
if self.depth_counter == 0:
|
||||
#pass
|
||||
self.last_depth = 10.0
|
||||
x = (0,0)
|
||||
y = (0,0)
|
||||
#self.number_of_operations = 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])
|
||||
cv2.line(depth_image, (x[0],y[0]), (x[1],y[1]), (255,255,255), 2)
|
||||
for i in range(x[0], x[1]):
|
||||
for j in range(y[0], y[1]):
|
||||
print(depth_image[j][i])
|
||||
self.last_depth += depth_image[j][i]
|
||||
self.number_of_operations += 1
|
||||
self.last_depth /= self.number_of_operations
|
||||
print(self.depth_counter, self.last_depth, self.number_of_operations)
|
||||
self.number_of_operations = 0
|
||||
#cv2.imshow("Tutel_Depth", depth_image)
|
||||
if self.last_depth < self.maxMinDepth and self.last_depth != 0:
|
||||
self.depth_counter += 1
|
||||
if self.depth_counter > 0:
|
||||
print("Reached our destination")
|
||||
self.depth_counter = 0
|
||||
self.depth_state = False
|
||||
self.last_depth = 10.0
|
||||
else:
|
||||
self.depth_counter = 0
|
||||
|
||||
|
||||
def markDetected(self, image, corners):
|
||||
thresh_left = self.w//2 - 100
|
||||
thresh_right = self.w//2 + 100
|
||||
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.bottom_right, (0,255,0), 2)
|
||||
cv2.line(image, self.top_right, self.bottom_right, (0,255,0), 2)
|
||||
|
||||
self.depth_state = True
|
||||
|
||||
return image
|
||||
|
||||
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_Depth(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()
|
||||
+254
@@ -0,0 +1,254 @@
|
||||
#!/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 = 400
|
||||
POSITIONS = ["left", "right", "middle"]
|
||||
DEVICE = 'dev/ttyACM'
|
||||
BAUD = 9600
|
||||
|
||||
|
||||
class tutel():
|
||||
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
|
||||
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.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)
|
||||
|
||||
#Tutel_control
|
||||
self.pub = rospy.Publisher(tutel_topic, Twist, queue_size = 1)
|
||||
self.wait_publishers(astra_rgb_topic, astra_depth_topic)
|
||||
self.t = Twist()
|
||||
|
||||
#cv related
|
||||
#rgb related
|
||||
self.position = POSITIONS[-1]
|
||||
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
|
||||
|
||||
#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)
|
||||
|
||||
#finding specific port for arduino communication
|
||||
def init_serial(self):
|
||||
for com in range(0,4):
|
||||
subprocess.run(["fuser", "/dev/ttyACM"+str(com), "-k"])
|
||||
try:
|
||||
port = DEVICE+str(com)
|
||||
baud = BAUD
|
||||
board = serial.Serial(port, baud)
|
||||
board.close()
|
||||
break
|
||||
except:
|
||||
pass
|
||||
return com
|
||||
|
||||
#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")
|
||||
(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 self.search:
|
||||
self.t.linear.x = 0.0 * self.vel_fac
|
||||
self.t.angular.z = 0.0 * self.vel_fac
|
||||
self.pub.publish(self.t)
|
||||
if corners:
|
||||
self.target_id = ids[0]
|
||||
self.search = False
|
||||
else:
|
||||
if corners:
|
||||
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
|
||||
if self.position is POSITIONS[0]:
|
||||
cv2.rectangle(rgb_image, (0,0), (20, 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.pub.publish(self.t)
|
||||
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.pub.publish(self.t)
|
||||
|
||||
#some additional info on screen
|
||||
id_str = "IDs: " + str(ids) + " Target_ID:" + str(self.target_id)
|
||||
cv2.putText(rgb_image, id_str, (10,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
|
||||
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)
|
||||
info_str = "Resolution: " + str(self.w) + "x" + str(self.h) + " Search_State: " + str(self.search) + " Max_Min_Depth: " + str(self.maxMinDepth)
|
||||
cv2.putText(rgb_image, info_str, (10,90), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
|
||||
|
||||
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")
|
||||
if self.depth_counter == 0:
|
||||
#pass
|
||||
self.last_depth = 10.0
|
||||
x = (0,0)
|
||||
y = (0,0)
|
||||
#self.number_of_operations = 0
|
||||
#x and y are swapped, because
|
||||
print(str(self.top_left[0]) + " " + str(self.top_right[0]) + " " + str(self.top_left[1]) + " " + str(self.bottom_right[1]))
|
||||
if (self.top_left[0] - self.bottom_left[0]) in range (-10,10):
|
||||
x = [(self.top_right[0],self.top_left[0]), (self.top_left[0],self.top_right[0])][self.top_left[0] < self.top_right[0]]
|
||||
y = [(self.bottom_left[1], self.top_left[1]), (self.top_left[1], self.bottom_left[1])][self.top_left[1] < self.bottom_left[1]]
|
||||
elif (self.top_left[0] - self.top_right[0]) in range (-10,10):
|
||||
x = [(self.bottom_left[0], self.top_left[0]), (self.top_left[0], self.bottom_left[0])][self.bottom_left[0] < self.top_left[0]]
|
||||
y = [(self.bottom_right[1], self.bottom_left[1]), (self.bottom_left[1], self.bottom_right[1])][self.bottom_left[1] < self.bottom_right[1]]
|
||||
cv2.line(depth_image, (x[0],y[0]), (x[1],y[1]), (0,255,0), 2)
|
||||
depth_values = []
|
||||
for i in range(x[0], x[1]):
|
||||
for j in range(y[0], y[1]):
|
||||
if depth_image[j][i] > self.maxMinDepth:
|
||||
depth_values.append(depth_image[j][i])
|
||||
for k in depth_values:
|
||||
if k > (max(depth_values) - 10):
|
||||
self.last_depth += k
|
||||
self.number_of_operations += 1
|
||||
self.last_depth [0, self.last_depth / self.number_of_operations][self.number_of_operations > 0]
|
||||
print(self.depth_counter, self.last_depth, self.number_of_operations)
|
||||
self.number_of_operations = 0
|
||||
cv2.imshow("Tutel_Depth", depth_image)
|
||||
if self.last_depth < self.maxMinDepth and self.last_depth != 0:
|
||||
self.depth_counter += 1
|
||||
if self.depth_counter > 0:
|
||||
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(self.t)
|
||||
self.position = POSITIONS[2]
|
||||
self.depth_counter = 0
|
||||
self.depth_state = False
|
||||
self.last_depth = 10.0
|
||||
#self.move_arm(self)
|
||||
else:
|
||||
self.depth_counter = 0
|
||||
|
||||
|
||||
def markDetected(self, image, corners):
|
||||
thresh_left = self.w//2 - 100
|
||||
thresh_right = self.w//2 + 100
|
||||
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.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 = ((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(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.1 * self.vel_fac
|
||||
self.pub.publish(self.t)
|
||||
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.1 * self.vel_fac
|
||||
self.pub.publish(self.t)
|
||||
if int(self.w / 2) > self.top_left[0]:
|
||||
self.position = POSITIONS[0]
|
||||
elif self.w - int(self.w / 2) < self.top_left[0]:
|
||||
self.position = POSITIONS[1]
|
||||
|
||||
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()
|
||||
|
||||
+256
@@ -0,0 +1,256 @@
|
||||
#!/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 = 400
|
||||
POSITIONS = ["left", "right", "middle"]
|
||||
DEVICE = 'dev/ttyACM'
|
||||
BAUD = 9600
|
||||
|
||||
|
||||
class tutel():
|
||||
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
|
||||
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.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)
|
||||
|
||||
#Tutel_control
|
||||
self.pub = rospy.Publisher(tutel_topic, Twist, queue_size = 1)
|
||||
self.wait_publishers(astra_rgb_topic, astra_depth_topic)
|
||||
self.t = Twist()
|
||||
|
||||
#cv related
|
||||
#rgb related
|
||||
self.position = POSITIONS[-1]
|
||||
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
|
||||
|
||||
#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)
|
||||
|
||||
#finding specific port for arduino communication
|
||||
def init_serial(self):
|
||||
for com in range(0,4):
|
||||
subprocess.run(["fuser", "/dev/ttyACM"+str(com), "-k"])
|
||||
try:
|
||||
port = DEVICE+str(com)
|
||||
baud = BAUD
|
||||
board = serial.Serial(port, baud)
|
||||
board.close()
|
||||
break
|
||||
except:
|
||||
pass
|
||||
return com
|
||||
|
||||
#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")
|
||||
(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 self.search:
|
||||
self.t.linear.x = 0.0 * self.vel_fac
|
||||
self.t.angular.z = 0.0 * self.vel_fac
|
||||
self.pub.publish(self.t)
|
||||
if corners:
|
||||
self.target_id = ids[0]
|
||||
self.search = False
|
||||
else:
|
||||
if corners:
|
||||
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
|
||||
if self.position is POSITIONS[0]:
|
||||
cv2.rectangle(rgb_image, (0,0), (20, 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.pub.publish(self.t)
|
||||
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.pub.publish(self.t)
|
||||
|
||||
#some additional info on screen
|
||||
id_str = "IDs: " + str(ids) + " Target_ID:" + str(self.target_id)
|
||||
cv2.putText(rgb_image, id_str, (10,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
|
||||
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)
|
||||
info_str = "Resolution: " + str(self.w) + "x" + str(self.h) + " Search_State: " + str(self.search) + " Max_Min_Depth: " + str(self.maxMinDepth)
|
||||
cv2.putText(rgb_image, info_str, (10,90), cv2.FONT_HERSHEY_TRIPLEX, 1, (255,0,0), 2, cv2.LINE_8)
|
||||
|
||||
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")
|
||||
if self.depth_counter == 0:
|
||||
#pass
|
||||
self.last_depth = 10.0
|
||||
x = (0,0)
|
||||
y = (0,0)
|
||||
#self.number_of_operations = 0
|
||||
#x and y are swapped, because
|
||||
print(str(self.top_left[0]) + " " + str(self.top_right[0]) + " " + str(self.top_left[1]) + " " + str(self.bottom_right[1]))
|
||||
if (self.top_left[0] - self.bottom_left[0]) in range (-10,10):
|
||||
x = [(self.top_right[0],self.top_left[0]), (self.top_left[0],self.top_right[0])][self.top_left[0] < self.top_right[0]]
|
||||
y = [(self.bottom_left[1], self.top_left[1]), (self.top_left[1], self.bottom_left[1])][self.top_left[1] < self.bottom_left[1]]
|
||||
elif (self.top_left[0] - self.top_right[0]) in range (-10,10):
|
||||
x = [(self.bottom_left[0], self.top_left[0]), (self.top_left[0], self.bottom_left[0])][self.bottom_left[0] < self.top_left[0]]
|
||||
y = [(self.bottom_right[1], self.bottom_left[1]), (self.bottom_left[1], self.bottom_right[1])][self.bottom_left[1] < self.bottom_right[1]]
|
||||
cv2.line(depth_image, (x[0],y[0]), (x[1],y[1]), (0,255,0), 2)
|
||||
depth_values = []
|
||||
for i in range(x[0], x[1]):
|
||||
for j in range(y[0], y[1]):
|
||||
if self.depth_image[j][i] > self.maxMinDepth:
|
||||
depth_values.append(depth_image[j][i])
|
||||
for k in depth_values:
|
||||
if k < in range(0, max(depth_image)-9): pass
|
||||
else:
|
||||
self.last_depth += k
|
||||
self.number_of_operations += 1
|
||||
self.last_depth [0, self.last_depth / self.number_of_operations][self.number_of_operations > 0]
|
||||
print(self.depth_counter, self.last_depth, self.number_of_operations)
|
||||
self.number_of_operations = 0
|
||||
cv2.imshow("Tutel_Depth", depth_image)
|
||||
if self.last_depth < self.maxMinDepth and self.last_depth != 0:
|
||||
self.depth_counter += 1
|
||||
if self.depth_counter > 0:
|
||||
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(self.t)
|
||||
self.position = POSITIONS[2]
|
||||
self.depth_counter = 0
|
||||
self.depth_state = False
|
||||
self.last_depth = 10.0
|
||||
#self.move_arm(self)
|
||||
else:
|
||||
self.depth_counter = 0
|
||||
|
||||
|
||||
def markDetected(self, image, corners):
|
||||
thresh_left = self.w//2 - 100
|
||||
thresh_right = self.w//2 + 100
|
||||
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.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 = ((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(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.1 * self.vel_fac
|
||||
self.pub.publish(self.t)
|
||||
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.1 * self.vel_fac
|
||||
self.pub.publish(self.t)
|
||||
if int(self.w / 2) > self.top_left[0]:
|
||||
self.position = POSITIONS[0]
|
||||
elif self.w - int(self.w / 2) < self.top_left[0]:
|
||||
self.position = POSITIONS[1]
|
||||
|
||||
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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user