77 lines
2.3 KiB
Python
77 lines
2.3 KiB
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 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()
|