From f59e0d2f23c4a397625a4cee320e488fce1a5a36 Mon Sep 17 00:00:00 2001 From: becker Date: Thu, 18 Jan 2024 13:58:17 +0100 Subject: [PATCH] add It just works --- serial/marker_x_serial.ino.ino | 23 ++++++++ serial/marker_x_serial.py | 101 +++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 serial/marker_x_serial.ino.ino create mode 100644 serial/marker_x_serial.py diff --git a/serial/marker_x_serial.ino.ino b/serial/marker_x_serial.ino.ino new file mode 100644 index 0000000..7ac6179 --- /dev/null +++ b/serial/marker_x_serial.ino.ino @@ -0,0 +1,23 @@ +#include + +#define LED 13 +char msg; +Servo s; +unsigned int rr = 0; +void setup() { + //start serial connection + Serial.begin(9600); + pinMode(LED, OUTPUT); + s.attach(2); +} + +void loop() { + msg = Serial.read(); + if(msg == '1'){ + digitalWrite(LED, HIGH); + s.write(rr); + rr = (rr +10) % 180; + //delay(100); + } + // else digitalWrite(LED, LOW); +} diff --git a/serial/marker_x_serial.py b/serial/marker_x_serial.py new file mode 100644 index 0000000..a2eb605 --- /dev/null +++ b/serial/marker_x_serial.py @@ -0,0 +1,101 @@ +#!/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 + +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): + 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, 0) + cv2.imshow("markerXserial", frame) + cv2.waitKey(1) + +def marker_detected(frame, corners, ser): + (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/ttyACM0' + 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() + talker(dictionary=dictionary, params=params, videostream=vs, port=com) + +if __name__ == "__main__": + main()