Dateien nach "Aufraeumroboter_Drakkar/Dennis_Dirk_Becker" hochladen
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env python3
|
||||
# license removed for brevity
|
||||
import rospy
|
||||
from geometry_msgs.msg import Twist
|
||||
from imutils.video import VideoStream
|
||||
import argparse
|
||||
import imutils
|
||||
import time
|
||||
import cv2
|
||||
import sys
|
||||
import serial
|
||||
import numpy as np
|
||||
from cv_bridge import CvBridge, CvBridgeError
|
||||
import subprocess
|
||||
|
||||
def init():
|
||||
arucoDict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_ARUCO_ORIGINAL)
|
||||
arucoParams = cv2.aruco.DetectorParameters_create()
|
||||
vs = VideoStream(src=0).start()
|
||||
time.sleep(2.0)
|
||||
com = init_serial()
|
||||
#pub = rospy.Publisher('turtle1/cmd_vel', Twist, queue_size=10)
|
||||
#rospy.init_node('turtle_control', anonymous=True)
|
||||
#rate = rospy.Rate(10) #
|
||||
#t = Twist()
|
||||
return arucoDict, arucoParams, vs, com, #pub, rate, t
|
||||
|
||||
|
||||
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 talker(dictionary, params, videostream, port):
|
||||
DEVICE = '/dev/ttyACM' + str(port)
|
||||
#DEVICE = '/dev/ttyACM0'
|
||||
BAUD = 9600
|
||||
ser = serial.Serial(DEVICE, BAUD, timeout=1)
|
||||
#greeting = '0'
|
||||
#while 1:
|
||||
# if greeting == b'1':
|
||||
# break
|
||||
# print(greeting)
|
||||
# greeting = ser.read()
|
||||
#ser.write('0'.encode("utf-8"))
|
||||
while 1:
|
||||
frame = videostream.read()
|
||||
frame = imutils.resize(frame)
|
||||
(corners, ids, rejected) = cv2.aruco.detectMarkers(frame, dictionary, parameters=params)
|
||||
if len(corners) > 0:
|
||||
frame = marker_detected(frame, corners, port)
|
||||
cv2.imshow("markerXserial", frame)
|
||||
cv2.waitKey(1)
|
||||
|
||||
def marker_detected(frame, corners, port):
|
||||
(h, w) = frame.shape[:2]
|
||||
thresh_left = w//2 -100
|
||||
thresh_right = w//2 +100
|
||||
for markerCorner in corners:
|
||||
(tl, tr, bl, br) = markerCorner.reshape(4,2)
|
||||
tl = (int(tl[0]), int(tl[1]))
|
||||
tr = (int(tr[0]), int(tr[1]))
|
||||
bl = (int(bl[0]), int(bl[1]))
|
||||
br = (int(br[0]), int(br[1]))
|
||||
cv2.line(frame, tl, br, (0,255,0), 2)
|
||||
cv2.line(frame, tr, bl, (0,255,0), 2)
|
||||
depth_point = (int((tl[0]+br[0])//2), int((tl[1]+br[1])//2))
|
||||
cv2.circle(frame, depth_point, 8, (255,0,0), -1)
|
||||
if((tl[0] > thresh_left and tl[0] < thresh_right) or
|
||||
(tr[0] > thresh_left and tr[0] < thresh_right) or
|
||||
(bl[0] > thresh_left and bl[0] < thresh_right) or
|
||||
(br[0] > thresh_left and br[0] < thresh_right)):
|
||||
print('stop123')
|
||||
DEVICE = '/dev/ttyACM'+str(port)
|
||||
BAUD = 9600
|
||||
ser2 = serial.Serial(DEVICE, BAUD, timeout=1)
|
||||
ser2.write('1'.encode('utf-8'))
|
||||
time.sleep(1)
|
||||
#time.sleep(10)
|
||||
elif(tr[0] < thresh_left or br[0] < thresh_left):
|
||||
#ser.write('0'.encode('utf-8'))
|
||||
print('need to go right')
|
||||
#time.sleep(10)
|
||||
elif(tl[0] > thresh_right or bl[0] > thresh_right):
|
||||
#ser.write('0'.encode('utf-8'))
|
||||
print('need to go left')
|
||||
#time.sleep(10)
|
||||
return frame
|
||||
|
||||
def main():
|
||||
(dictionary, params, vs, com) = init()
|
||||
print(com)
|
||||
talker(dictionary=dictionary, params=params, videostream=vs, port=com)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user