diff --git a/ardunio.ino b/ardunio.ino new file mode 100644 index 0000000..823d15b --- /dev/null +++ b/ardunio.ino @@ -0,0 +1,68 @@ +#include +#include + +int cX = 0; +int cY = 0; +#define LED 13 +Servo s; +Servo s3; +unsigned int rr = 0; +void setup() { + //start serial connection + Serial.begin(9600); + pinMode(LED, OUTPUT); + s.attach(2); + s3.attach(3); +} + +//Lets assume 640 × 480p +void moveRightLeft() +{ + if(cX > 320) + { + s.write(180); + } + else + { + s.write(0); + } + + delay(300); +} + +void moveUpDown() +{ + if(cY > 200) + { + s3.write(180); + } + else + { + s3.write(0); + } + delay(300); +} + + + +void loop() { + if (Serial.available() > 0) { + digitalWrite(LED, HIGH); + String str = Serial.readString(); + str.trim(); + Serial.println(str); + if(str.charAt(0) == '1') + { + str.remove(0,1); + sscanf(str.c_str(),"%d,%d",&cX,&cY); + Serial.println(cX); + Serial.println(cY); + Serial.println("======"); + + moveRightLeft(); + moveUpDown(); + } + + digitalWrite(LED, LOW); + } +} diff --git a/moveGripper.py b/moveGripper.py new file mode 100644 index 0000000..679befe --- /dev/null +++ b/moveGripper.py @@ -0,0 +1,75 @@ +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 + + +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]) + 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, fitting_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 ") + 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()