Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e091b667c | ||
|
|
172886efbd | ||
|
|
7185753455 | ||
|
|
e4797d435c | ||
|
|
2f6eee89c1 | ||
|
|
9e280919f1 | ||
|
|
74627e2456 | ||
|
|
ce862fd07b | ||
|
|
d181e9a14c | ||
|
|
67d7a51331 | ||
|
|
9cf2c5f479 | ||
|
|
401f154a5b | ||
|
|
f0f0d17523 | ||
|
|
618d7f28df | ||
|
|
7a7a0b7012 |
@@ -0,0 +1,194 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
|
||||
import roslib
|
||||
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:
|
||||
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)
|
||||
|
||||
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("chatterabc", 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 say_im_finish(self):
|
||||
for x in range(0, 50):
|
||||
hello_str = "GRIPFINISH"
|
||||
print("IM FINISHED")
|
||||
self.pub2.publish(hello_str)
|
||||
self.rate.sleep()
|
||||
|
||||
|
||||
|
||||
def __init__(self, astra_camera_rgb_topic : str, astra_camera_depth_topic : str , start_id : int = 1):
|
||||
|
||||
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.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()
|
||||
self.pub2 = rospy.Publisher('grippertalker', String, queue_size=10)
|
||||
rospy.init_node("turtle_control", anonymous=True)
|
||||
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.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):
|
||||
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)
|
||||
|
||||
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 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) # 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
|
||||
else:
|
||||
self.depth_point = (-1,-1)
|
||||
|
||||
|
||||
|
||||
|
||||
cv2.rectangle(cv_image,(0,0),(700,120),(10, 10, 10),-1)
|
||||
#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:" + 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 = "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()
|
||||
cv2.imshow("Window", cv_image)
|
||||
cv2.waitKey(1)
|
||||
|
||||
def callback_depth(self, data):
|
||||
#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")
|
||||
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]]
|
||||
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
|
||||
print("st:" + str(st))
|
||||
print("rr:" + str(rr))
|
||||
print("==here")
|
||||
self.gHelper.testPlace(int(rr))
|
||||
self.depth_point = (-1,-1)
|
||||
self.say_im_finish()
|
||||
rospy.signal_shutdown(1)
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3:
|
||||
print("Please give the following arguments <camera-img-topic> and <camera-depth-topic>")
|
||||
print("Like: marker_attack.py /camera/rgb/image_raw /camera/depth/image_raw")
|
||||
return 1
|
||||
print("OK")
|
||||
ic = gripper_mover(str(sys.argv[1]),str(sys.argv[2]))
|
||||
try:
|
||||
rospy.spin()
|
||||
except KeyboardInterrupt:
|
||||
print("Shutting down")
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,33 @@
|
||||
class betterDepth():
|
||||
lastResult = 0.0
|
||||
maxDistance = 0
|
||||
def __init__(self,maxDistante : int = 2000):
|
||||
self.maxDistante = maxDistante
|
||||
|
||||
def getProperDepth(self,vListe : list) -> float:
|
||||
def checkList(ele):
|
||||
if ele > 400 and ele < self.maxDistante:
|
||||
return True
|
||||
return False
|
||||
self.lastResult = 0.0
|
||||
vNewListe = list(filter(checkList, vListe)) #Filter the list before we use it
|
||||
if len(vNewListe) == 0:
|
||||
return self.lastResult
|
||||
for i,val in enumerate(vNewListe):
|
||||
self.lastResult += val
|
||||
self.lastResult = self.lastResult / len(vNewListe)
|
||||
return self.lastResult
|
||||
#Pass the depthimage that has already been converted and the depth point x, y
|
||||
#The idea here is that one depth point is not enough so we use the 9 most adjacent points
|
||||
def wDepthImage(self, cv_depth_image : list, dX : int, dY : int) -> float:
|
||||
vDepthPoints = []
|
||||
for z in range(0, 8):
|
||||
vDepthPoints.append(cv_depth_image[dX - 4 + z][dY - 4 + z])
|
||||
self.getProperDepth(vDepthPoints)
|
||||
return self.lastResult
|
||||
|
||||
if __name__ == "__main__":
|
||||
vListe = [1,500,600,500,600,450,650,1999]
|
||||
bD = betterDepth()
|
||||
mmDepth = bD.getProperDepth(vListe)
|
||||
print(mmDepth)
|
||||
@@ -0,0 +1,133 @@
|
||||
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, 0, 90, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 0, 90, 63, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 5, 90, 63, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 10, 88, 63, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 15, 90, 63, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 20, 90, 63, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 25, 90, 63, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 30, 88, 63, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
#data = [degrees, 38, 88, 63, 0, 0, 0]
|
||||
#self.buildAndSendData(data)
|
||||
data = [degrees, 30, 88, 75, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 30, 88, 80, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
#data = [degrees, 37, 88, 90, 0, 0, 0]
|
||||
#self.buildAndSendData(data)
|
||||
#data = [degrees, 37, 82, 90, 0, 0, 0]
|
||||
#self.buildAndSendData(data)
|
||||
data = [degrees, 33, 82, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees,33, 78, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 29,78, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 29, 78, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 26, 75, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 26, 65, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 18, 65, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 18, 60, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 0, 60, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [degrees, 0, 90, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
return
|
||||
|
||||
def testRotate(self,degrees):
|
||||
data = [degrees, 1, 100, 80, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
|
||||
def movInit(self):
|
||||
data = [0, 0, 90, 90, 0, 0, 0]
|
||||
self.buildAndSendData(data)
|
||||
data = [0, 0, 60, 90, 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)
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
|
||||
import roslib
|
||||
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:
|
||||
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)
|
||||
|
||||
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("chatterabc", 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 say_im_finish(self):
|
||||
for x in range(0, 120):
|
||||
hello_str = "GRIPFINISH"
|
||||
print("IM FINISHED")
|
||||
self.pub2.publish(hello_str)
|
||||
self.rate.sleep()
|
||||
|
||||
|
||||
|
||||
def __init__(self, astra_camera_rgb_topic : str, astra_camera_depth_topic : str , start_id : int = 1):
|
||||
|
||||
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.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()
|
||||
self.pub2 = rospy.Publisher('grippertalker', String, queue_size=10)
|
||||
rospy.init_node("turtle_control", anonymous=True)
|
||||
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.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):
|
||||
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)
|
||||
|
||||
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 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) # 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
|
||||
else:
|
||||
self.depth_point = (-1,-1)
|
||||
|
||||
|
||||
|
||||
|
||||
cv2.rectangle(cv_image,(0,0),(700,120),(10, 10, 10),-1)
|
||||
#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:" + 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 = "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()
|
||||
cv2.imshow("Window", cv_image)
|
||||
cv2.waitKey(1)
|
||||
|
||||
def callback_depth(self, data):
|
||||
#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")
|
||||
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]]
|
||||
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 < 900:
|
||||
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
|
||||
print("st:" + str(st))
|
||||
print("rr:" + str(rr))
|
||||
print("==here")
|
||||
self.gHelper.testPlace(int(rr))
|
||||
self.gHelper.movInit()
|
||||
time.sleep(2)
|
||||
self.depth_point = (-1,-1)
|
||||
self.say_im_finish()
|
||||
rospy.signal_shutdown(1)
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3:
|
||||
print("Please give the following arguments <camera-img-topic> and <camera-depth-topic>")
|
||||
print("Like: marker_attack.py /camera/rgb/image_raw /camera/depth/image_raw")
|
||||
return 1
|
||||
print("OK")
|
||||
ic = gripper_mover(str(sys.argv[1]),str(sys.argv[2]))
|
||||
try:
|
||||
rospy.spin()
|
||||
except KeyboardInterrupt:
|
||||
print("Shutting down")
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,249 @@
|
||||
#!/usr/bin/env python
|
||||
#pip3 install pencv-python3==4.9.0.80
|
||||
#pip3 install rospy
|
||||
|
||||
from __future__ import print_function
|
||||
import roslib
|
||||
import sys
|
||||
import rospy
|
||||
import cv2
|
||||
import time
|
||||
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
|
||||
|
||||
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 say_im_here(self):
|
||||
for x in range(0, 200):
|
||||
hello_str = "IMHERE"
|
||||
rospy.loginfo(hello_str)
|
||||
self.pub2.publish(hello_str)
|
||||
self.rate.sleep()
|
||||
|
||||
gripper_finish = 0
|
||||
|
||||
stopAll = 0
|
||||
|
||||
def wait_for_gripper_callback(self,data):
|
||||
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
|
||||
if data.data == "GRIPFINISH":
|
||||
self.gripper_finish = 1
|
||||
|
||||
def wait_for_gripper(self):
|
||||
sub = rospy.Subscriber("grippertalker", String, self.wait_for_gripper_callback)
|
||||
while self.gripper_finish == 0:
|
||||
print("Gripper has not finished")
|
||||
self.rate.sleep()
|
||||
print("Gripper finished")
|
||||
sub.unregister()
|
||||
self.stopAll = 0
|
||||
|
||||
def __init__(self, turlte_bot_topic : str, astra_camera_rgb_topic : str, astra_camera_depth_topic : str ,speed_emul : float = 1, start_id : int = 5):
|
||||
|
||||
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.stopAt = 850
|
||||
self.markerIds = [497,666]
|
||||
self.mmID = 0
|
||||
|
||||
### For turtle controll
|
||||
self.pub = rospy.Publisher(turlte_bot_topic, Twist, queue_size=1)
|
||||
self.pub2 = rospy.Publisher('chatterabc', String, queue_size=10)
|
||||
# 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=False)
|
||||
self.rate = rospy.Rate(12) # 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 - 50
|
||||
thresh_right = self.w//2 + 50
|
||||
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.1
|
||||
self.t.angular.z = 0.2 * 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.1
|
||||
self.t.angular.z = -0.2 * 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.stopAll == 1:
|
||||
return 0
|
||||
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.1
|
||||
self.t.angular.z = 0.15 * 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.1
|
||||
self.t.angular.z = -0.15 * 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)
|
||||
|
||||
depthString2 = "Res:" + str(self.w) + "x" + str(self.h) + " RState:" + str(self.rState) + " MMDepth:" + str(self.maxMinDepth)
|
||||
# cv2.putText(cv_image, depthString2, (10,90), cv2.FONT_HERSHEY_TRIPLEX , 0.8, (255, 0, 0), 2, cv2.LINE_8)
|
||||
self.rate.sleep()
|
||||
print(idString)
|
||||
print(depthString)
|
||||
print(depthString2)
|
||||
print("=======")
|
||||
# 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 > 530:
|
||||
self.depth_counter += 1 #Sometimes the data is inacurate make sure we get the data at least 5times
|
||||
if self.last_depth < self.stopAt:
|
||||
if self.depth_counter > 0:
|
||||
print("We are really there")
|
||||
self.target_id = self.markerIds[self.mmID]
|
||||
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
|
||||
if self.mmID == 0:
|
||||
self.stopAll = 1
|
||||
self.say_im_here()
|
||||
self.wait_for_gripper()
|
||||
self.stopAt = 580
|
||||
self.mmID = (self.mmID + 1 ) % 2
|
||||
else:
|
||||
self.depth_counter = 0
|
||||
#We dont need to sleep twice
|
||||
#self.rate.sleep()
|
||||
|
||||
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()
|
||||
|
||||
ic = marker_attacker(str(sys.argv[1]),str(sys.argv[2]),str(sys.argv[3]),1.0,666)
|
||||
try:
|
||||
rospy.spin()
|
||||
except KeyboardInterrupt:
|
||||
print("Shutting down")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
(END)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
#include <string.h>
|
||||
#include <Servo.h>
|
||||
#define LED 13
|
||||
|
||||
|
||||
|
||||
#define PIN_OFFSET 2 //At which pin is the first connection, must be in row
|
||||
#define SERVO_NUM 7
|
||||
|
||||
int servoPos[SERVO_NUM] = {0};
|
||||
Servo ser[SERVO_NUM];
|
||||
|
||||
//VarSpeedServo myServo;
|
||||
|
||||
|
||||
void setup() {
|
||||
//start serial connection
|
||||
Serial.begin(9600);
|
||||
pinMode(LED, OUTPUT);
|
||||
|
||||
for(int i = 0;i<SERVO_NUM;i++)
|
||||
{
|
||||
ser[i].attach(i + PIN_OFFSET);
|
||||
}
|
||||
Serial.println("START");
|
||||
}
|
||||
|
||||
void setAllZero()
|
||||
{
|
||||
for(int i = 0;i<SERVO_NUM;i++)
|
||||
{
|
||||
servoPos[i] = 0;
|
||||
delay(200);
|
||||
}
|
||||
}
|
||||
|
||||
void moveServoSlowly(int servo,int pos)
|
||||
{
|
||||
for(int i = 0;i< pos;i++)
|
||||
{
|
||||
ser[servo].write(i);
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void moveServo()
|
||||
{
|
||||
for(int i = 0;i<SERVO_NUM;i++)
|
||||
{
|
||||
ser[i].write(servoPos[i]);
|
||||
// moveServoSlowly(i,servoPos[i]);
|
||||
delay(200);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void printAll()
|
||||
{
|
||||
for(int i = 0;i<SERVO_NUM;i++)
|
||||
{
|
||||
Serial.println(servoPos[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
String str = Serial.readString();
|
||||
// String str = "1456,677,123,456,789,999,111";
|
||||
str.trim();
|
||||
if(str[0] == '1')
|
||||
{
|
||||
digitalWrite(LED, HIGH);
|
||||
Serial.println("OK1");
|
||||
str.remove(0,1);
|
||||
sscanf(str.c_str(),"%d,%d,%d,%d,%d,%d,%d",&servoPos[0],&servoPos[1],&servoPos[2],&servoPos[3],&servoPos[4],&servoPos[5],&servoPos[6]);
|
||||
printAll();
|
||||
moveServo();
|
||||
digitalWrite(LED, LOW);
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
class betterDepth():
|
||||
lastResult = 0.0
|
||||
maxDistance = 0
|
||||
def __init__(self,maxDistante : int = 2000):
|
||||
self.maxDistante = maxDistante
|
||||
|
||||
def getProperDepth(self,vListe : list) -> float:
|
||||
def checkList(ele):
|
||||
if ele > 400 and ele < self.maxDistante:
|
||||
return True
|
||||
return False
|
||||
self.lastResult = 0.0
|
||||
vNewListe = list(filter(checkList, vListe)) #Filter the list before we use it
|
||||
if len(vNewListe) == 0:
|
||||
return self.lastResult
|
||||
for i,val in enumerate(vNewListe):
|
||||
self.lastResult += val
|
||||
self.lastResult = self.lastResult / len(vNewListe)
|
||||
return self.lastResult
|
||||
#Pass the depthimage that has already been converted and the depth point x, y
|
||||
#The idea here is that one depth point is not enough so we use the 9 most adjacent points
|
||||
def wDepthImage(self, cv_depth_image : list, dX : int, dY : int) -> float:
|
||||
vDepthPoints = []
|
||||
for z in range(0, 8):
|
||||
vDepthPoints.append(cv_depth_image[dX - 4 + z][dY - 4 + z])
|
||||
self.getProperDepth(vDepthPoints)
|
||||
return self.lastResult
|
||||
|
||||
if __name__ == "__main__":
|
||||
vListe = [1,500,600,500,600,450,650,1999]
|
||||
bD = betterDepth()
|
||||
mmDepth = bD.getProperDepth(vListe)
|
||||
print(mmDepth)
|
||||
@@ -0,0 +1,84 @@
|
||||
import serial
|
||||
import time
|
||||
|
||||
|
||||
class gripperArdunio():
|
||||
globalSerial = 0
|
||||
MAX_SERV = 7
|
||||
def __init__(self):
|
||||
port = "/dev/ttyACM1"
|
||||
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)
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
|
||||
import roslib
|
||||
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:
|
||||
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)
|
||||
|
||||
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("chatterabc", 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 say_im_finish(self):
|
||||
pub = rospy.Publisher('grippertalker', String, queue_size=10)
|
||||
for x in range(0, 50):
|
||||
hello_str = "GRIPFINISH"
|
||||
print("IM FINISHED")
|
||||
pub.publish(hello_str)
|
||||
self.rate.sleep()
|
||||
|
||||
|
||||
|
||||
def __init__(self, astra_camera_rgb_topic : str, astra_camera_depth_topic : str , start_id : int = 1):
|
||||
|
||||
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.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(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.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):
|
||||
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)
|
||||
|
||||
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 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) # 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
|
||||
else:
|
||||
self.depth_point = (-1,-1)
|
||||
|
||||
|
||||
|
||||
|
||||
cv2.rectangle(cv_image,(0,0),(700,120),(10, 10, 10),-1)
|
||||
#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:" + 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 = "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()
|
||||
cv2.imshow("Window", cv_image)
|
||||
cv2.waitKey(1)
|
||||
|
||||
def callback_depth(self, data):
|
||||
#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")
|
||||
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]]
|
||||
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
|
||||
print("st:" + str(st))
|
||||
print("rr:" + str(rr))
|
||||
print("==here")
|
||||
self.gHelper.testPlace(int(rr))
|
||||
self.depth_point = (-1,-1)
|
||||
self.say_im_finish()
|
||||
rospy.signal_shutdown(1)
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3:
|
||||
print("Please give the following arguments <camera-img-topic> and <camera-depth-topic>")
|
||||
print("Like: marker_attack.py /camera/rgb/image_raw /camera/depth/image_raw")
|
||||
return 1
|
||||
print("OK")
|
||||
ic = gripper_mover(str(sys.argv[1]),str(sys.argv[2]))
|
||||
try:
|
||||
rospy.spin()
|
||||
except KeyboardInterrupt:
|
||||
print("Shutting down")
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,76 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import roslib
|
||||
import sys
|
||||
import rospy
|
||||
import cv2
|
||||
import time
|
||||
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 serial
|
||||
|
||||
def openSerial():
|
||||
port = '/dev/ttyACM0'
|
||||
baud = 9600
|
||||
ser = serial.Serial(port, baud, timeout=1)
|
||||
return ser
|
||||
|
||||
|
||||
def sendData(ser,data):
|
||||
dataSend = "1" + str(data[0]) + "," + str(data[1])
|
||||
print(dataSend)
|
||||
ser.write(dataSend.encode('utf-8'))
|
||||
print("=Data written=")
|
||||
|
||||
|
||||
|
||||
class camera_marker_finder():
|
||||
def __init__(self, astra_rgb_topic : str):
|
||||
self.bridge = CvBridge()
|
||||
self.arucoDict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_ARUCO_ORIGINAL)
|
||||
self.arucoParams = cv2.aruco.DetectorParameters_create()
|
||||
|
||||
rospy.init_node("gripper_master", anonymous=True)
|
||||
self.rate = rospy.Rate(10)
|
||||
self.image_sub = rospy.Subscriber(astra_rgb_topic, Image, self.callback_rgb)
|
||||
self.serial = openSerial()
|
||||
|
||||
def markDetected(self, img, corners):
|
||||
for markerCorner in corners:
|
||||
(top_left, top_right, bottom_right, bottom_left) = markerCorner.reshape((4,2))
|
||||
return (int((top_left[0] + bottom_right[0]) / 2), int((top_left[1] + bottom_right[1]) / 2))
|
||||
|
||||
def callback_rgb(self, data):
|
||||
cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
|
||||
(self.h, self.w) = cv_image.shape[:2]
|
||||
(corners, ids, rejected) = cv2.aruco.detectMarkers(cv_image, self.arucoDict, parameters=self.arucoParams)
|
||||
if corners:
|
||||
results = self.markDetected(cv_image, corners)
|
||||
print("Yes I see a marker at X:" + str(results[0]) + " Y:" + str(results[1]))
|
||||
sendData(self.serial,results)
|
||||
else:
|
||||
print("No marker in image")
|
||||
|
||||
self.rate.sleep()
|
||||
cv2.waitKey(1)
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print("Please give the following arguments <camera-img-topic>")
|
||||
print("Like: {} /camera/rgb/image_raw".format(str(sys.argv[0])))
|
||||
return 1
|
||||
ic = camera_marker_finder(str(sys.argv[1]))
|
||||
try:
|
||||
rospy.spin()
|
||||
except KeyboardInterrupt:
|
||||
print("Shutting down")
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,143 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
|
||||
import roslib
|
||||
import sys
|
||||
import rospy
|
||||
import cv2
|
||||
import time
|
||||
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 bDepth
|
||||
|
||||
|
||||
class gripper_mover():
|
||||
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, astra_camera_rgb_topic : str, astra_camera_depth_topic : str, start_id : int = 1):
|
||||
|
||||
self.target_id = start_id #We target one tag at a time
|
||||
self.arucoDict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_ARUCO_ORIGINAL)
|
||||
self.arucoParams_test = cv2.aruco.DetectorParameters_create()
|
||||
self.arucoParams = self.arucoParams_test
|
||||
self.maxMinDepth = 0.5
|
||||
|
||||
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(15) # update rate can be increased
|
||||
|
||||
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.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):
|
||||
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)
|
||||
|
||||
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 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) # 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
|
||||
else:
|
||||
self.depth_point = (-1,-1)
|
||||
|
||||
|
||||
|
||||
|
||||
cv2.rectangle(cv_image,(0,0),(700,120),(10, 10, 10),-1)
|
||||
#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])
|
||||
cv2.putText(cv_image, depthString, (10,60), cv2.FONT_HERSHEY_TRIPLEX , 0.8, (200, 200, 200), 2, cv2.LINE_8)
|
||||
|
||||
depthString2 = "Distance to marker:" + str(self.last_depth)
|
||||
cv2.putText(cv_image, depthString2, (10,90), cv2.FONT_HERSHEY_TRIPLEX , 0.8, (200, 200, 200), 2, cv2.LINE_8)
|
||||
|
||||
self.rate.sleep()
|
||||
cv2.imshow("Window", cv_image)
|
||||
cv2.waitKey(1)
|
||||
|
||||
def callback_depth(self, data):
|
||||
#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")
|
||||
if self.depth_point != (-1,-1):
|
||||
# Here x and y are swapped. I dont know why.
|
||||
#notNull = cv_depth_image[self.depth_point[1]][self.depth_point[0]]
|
||||
testDepth = self.bDepth.wDepthImage(cv_depth_image,self.depth_point[1],self.depth_point[0])
|
||||
if testDepth < 500:
|
||||
return
|
||||
self.last_depth = self.bDepth.wDepthImage(cv_depth_image,self.depth_point[1],self.depth_point[0])
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3:
|
||||
print("Please give the following arguments <camera-img-topic> and <camera-depth-topic>")
|
||||
print("Like: marker_attack.py /camera/rgb/image_raw /camera/depth/image_raw")
|
||||
return 1
|
||||
print("OK")
|
||||
ic = gripper_mover(str(sys.argv[1]),str(sys.argv[2]))
|
||||
try:
|
||||
rospy.spin()
|
||||
except KeyboardInterrupt:
|
||||
print("Shutting down")
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+57
-37
@@ -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:
|
||||
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 802 KiB |
BIN
Binary file not shown.
|
After Width: | Height: | Size: 146 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 187 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
@@ -0,0 +1,236 @@
|
||||
#!/usr/bin/env python
|
||||
#pip3 install pencv-python3==4.9.0.80
|
||||
#pip3 install rospy
|
||||
|
||||
from __future__ import print_function
|
||||
import roslib
|
||||
import sys
|
||||
import rospy
|
||||
import cv2
|
||||
import time
|
||||
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
|
||||
|
||||
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 say_im_here(self):
|
||||
for x in range(0, 40):
|
||||
hello_str = "IMHERE"
|
||||
rospy.loginfo(hello_str)
|
||||
self.pub2.publish(hello_str)
|
||||
self.rate.sleep()
|
||||
|
||||
gripper_finish = 0
|
||||
|
||||
stopAll = 0
|
||||
|
||||
def wait_for_gripper_callback(selfi,data):
|
||||
spy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
|
||||
if data.data == "GRIPFINISH":
|
||||
self.gripper_finish = 1
|
||||
|
||||
def wait_for_gripper(self):
|
||||
sub = rospy.Subscriber("grippertalker", String, self.wait_for_gripper_callback)
|
||||
while self.gripper_finish == 0:
|
||||
print("Gripper has not finished")
|
||||
self.rate.sleep()
|
||||
print("Gripper finished")
|
||||
sub.unregister()
|
||||
self.stopAll = 0
|
||||
|
||||
def __init__(self, turlte_bot_topic : str, astra_camera_rgb_topic : str, astra_camera_depth_topic : str ,speed_emul : float = 1, start_id : int = 5):
|
||||
|
||||
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
|
||||
|
||||
### For turtle controll
|
||||
self.pub = rospy.Publisher(turlte_bot_topic, Twist, queue_size=1)
|
||||
self.pub2 = rospy.Publisher('chatterabc', String, queue_size=10)
|
||||
# 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(30) # 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.stopAll == 1:
|
||||
return 0
|
||||
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.15 * 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.15 * 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)
|
||||
|
||||
depthString2 = "Res:" + str(self.w) + "x" + str(self.h) + " RState:" + str(self.rState) + " MMDepth:" + str(self.maxMinDepth)
|
||||
# cv2.putText(cv_image, depthString2, (10,90), cv2.FONT_HERSHEY_TRIPLEX , 0.8, (255, 0, 0), 2, cv2.LINE_8)
|
||||
self.rate.sleep()
|
||||
print(idString)
|
||||
print(depthString)
|
||||
print(depthString2)
|
||||
print("=======")
|
||||
# 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 > 500:
|
||||
self.depth_counter += 1 #Sometimes the data is inacurate make sure we get the data at least 5times
|
||||
if self.last_depth < 800:
|
||||
if self.depth_counter > 0:
|
||||
print("We are really there")
|
||||
# self.target_id += 1
|
||||
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
|
||||
self.stopAll = 1
|
||||
self.say_im_here()
|
||||
self.wait_for_gripper()
|
||||
else:
|
||||
self.depth_counter = 0
|
||||
#We dont need to sleep twice
|
||||
#self.rate.sleep()
|
||||
|
||||
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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user