1. Trang chủ
  2. » Luận Văn - Báo Cáo

(Đồ án hcmute) thiết kế robot dọn rác ứng dụng xử lý ảnh

127 6 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

BỘ GIÁO DỤC VÀ ĐÀO TẠO TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT THÀNH PHỐ HỒ CHÍ MINH ĐỒ ÁN TỐT NGHIỆP NGÀNH CNKT ĐIỀU KHIỂN VÀ TỰ ĐỘNG HÓA THIẾT KẾ ROBOT ỨNG DỤNG XỬ LÝ ẢNH GVHD: NGUYỄN MINH TÂM SVTH: HOÀNG VĂN LÂM MSSV: 14151058 SKL 0 Tp Hồ Chí Minh, tháng 01/2020 an ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH i an GVHD: PGS.TS.NGUYỄN MINH TÂM ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH ii an GVHD: PGS.TS.NGUYỄN MINH TÂM ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH iii an GVHD: PGS.TS.NGUYỄN MINH TÂM ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH iv an GVHD: PGS.TS.NGUYỄN MINH TÂM ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH GVHD: PGS.TS.NGUYỄN MINH TÂM LỜI CÁM ƠN Để hoàn thành luận văn với đề tài “Thiết kế Robot dọn rác ứng dụng xử lý ảnh.”, bên cạnh nỗ lực thân vận dụng kiến thức tiếp thu được, tìm tịi học hỏi thu thập thông tin số liệu liên quan đến đề tài, chúng em nhận giúp đỡ tận tình thầy cơ, đồng nghiệp, bạn bè Trước hết, chúng em xin tỏ lòng biết ơn gửi lời cám ơn chân thành đến Phó Giáo Sư Tiến Sĩ Nguyễn Minh Tâm, người trực tiếp hướng dẫn luận văn, tận tình bảo hướng dẫn chúng em tìm hướng nghiên cứu, tiếp cận thực tế, tìm kiếm tài liệu, xử lý phân tích số liệu, giải vấn đề… nhờ đó chúng em hồn thành luận văn cao học Trong quá trình làm đề tài nhóm phát sinh nhiều khó khăn, nhiều lúc gặp vấn đề tưởng chừng giải Và với kinh nghiệm người thầy, thầy theo sát, gợi ý đưa hướng xử lý Thầy không giúp chúng em việc giải vấn đề mà động viên khích lệ tinh thần giúp chúng em hồn thành tốt nhiệm vụ đề tài Chúng em xin chân thành cảm ơn thầy PGS.TS Nguyễn Minh Tâm Để hồn thiện đề tài tích lũy kiến thức năm học đại học yếu tố quan trọng chúng em xin chân thành cảm ơn các thầy cô khoa nhiệt tình hỗ trợ, giúp đỡ vật chất tinh thần, kiến thức để giúp chúng em vượt qua lúc khó khăn, cung cấp kiến thức hữu ích khơng cho việc thực đề tài điều khiển ổn định hệ thống, xử lý ảnh cơng nghiệp… mà cịn kiến thức sau công việc sau trường Tp.Hồ Chí Minh, ngày 10 tháng năm 2020 Sinh viên thực (Ký & ghi rõ họ tên Hoàng Văn Lâm v an ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH GVHD: PGS.TS.NGUYỄN MINH TÂM TÓM TẮT Luận Văn: “THIẾT KẾ, CHẾ TẠO ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH” đề tài hay, ứng dụng kỹ thuật điện tử phục vụ đời sống người Không giới hạn lĩnh vực sống, ứng dụng ngành ứng dụng rộng rãi các lĩnh vực khác sinh học, y tế, công nghiệp tự động hóa,… Ý tưởng kết hợp dựa nhận thấy mạnh Raspberry Pi Arduino bổ sung cho nhau, Raspberry Pi vượt trội ứng dụng phần mềm Arduino xử lý mạnh mẽ cho dự án phần cứng Robot bánh có khả giữ cân tốt, di chuyển linh hoạt, theo dõi bám đối tượng, cụ thể rác bụi Thông qua đề tài, nhóm hiểu rõ cách điều khiển hệ thống ổn định, nguyên lý cách sử dụng Raspberry Pi, động Step lập trình xử lý ảnh ngơn ngữ lập trình Python Luận văn trình bày bao gồm lý thuyết, thuật toán áp dụng phần cứng: Arduino Pro Mini, Raspberry Pi, động bước, Picamera, … vi an ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH GVHD: PGS.TS.NGUYỄN MINH TÂM ABSTRACT The thesis: “DESIGNING, CREATING Robot TO BE DUST WITH PHOTO HANDLING APPLICATIONS” is a good topic, one of the applications of mechatronics techniques for human life Not only limited in the field of life, the application of the industry is also widely applied in other fields such as biology, health, automation industry, etc The idea of this combination is based on recognizing the strengths of the Raspberry Pi and the Arduino as a complement to each other, the Raspberry Pi excels in software applications while the Arduino is powerful processor for pure projects Hardware 3-wheel Robot has the ability to keep balance, move flexibly, track and track objects, namely rubbish and dust Through the topic, the team understood how to control a stable system, the principles and use of Raspberry Pi, Step Motors and image processing programming in the Python programming language This thesis will cover the theory, applied algorithms and hardware: Arduino Pro Mini, Raspberry Pi, Stepper Motor, Picamera, etc vii an ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH GVHD: PGS.TS.NGUYỄN MINH TÂM TRANG PHỤ BÌA NHIỆM VỤ ĐỒ ÁN TỐT NGHIỆP i PHIẾU NHẬN XÉT CỦA GIÁO VIÊN HƯỚNG DẪN ii PHIẾU NHẬN XÉT CỦA GIÁO VIÊN HƯỚNG DẪN iv LỜI CẢM ƠN v TÓM TẮT vi ABSTRACT vii MỤC LỤC viii DANH MỤC CÁC TỪ VIẾT TẮT ix DANH MỤC CÁC BẢNG BIỂU x DANH MỤC CÁC HÌNH ẢNH xiii CHƯƠNG 1: TỔNG QUAN 1.1 Đặt vấn đề 1.2 Nhiệm vụ đồ án 1.2.1 Lý hình thành đề tài 1.2.2 Ý nghĩa khoa học thực tiễn 1.2.2.1 Ý nghĩa khoa học 1.2.2.2 Ý nghĩa thực tiễn 1.2.3 Mục tiêu nghiên cứu 1.2.4 Đối tượng 1.2.5 Phạm vi đề tài 1.3 Cấu trúc đồ án CHƯƠNG 2: CƠ SỞ LÝ THUYẾT 2.1 Khái niệm xử lý ảnh viii an ROBOT DỌN RÁC ỨNG DỤNG XỬ LÝ ẢNH GVHD: PGS.TS.NGUYỄN MINH TÂM 2.2 Các khái niệm xử lý ảnh 2.2.1 Độ phân giải ảnh - Resolution 2.2.2 Pixel 2.2.3 Thành phần màu ảnh 2.2.4 Mức xám (Gray level) 10 2.3 Thư Viện OpenCV 11 2.3.1 Giới Thiệu Thư Viện OpenCV 11 2.3.2 Giới Thiệu Thư Viện OpenCV 12 2.3.2.1 Không gian màu HSV, Chuyển đổi không gian màu 12 2.3.2.2 Histogram 15 2.3.2.3 Xử lý theo ngưỡng (Thresholding) 16 2.3.2.4 Lọc không gian (Spatial filter) 19 2.3.2.5 Tần số lọc thông thấp, thông cao 21 2.3.2.6 Nhiễu (Noise) 22 2.3.2.7 Biến đổi Hough (Hough transform) 24 2.3.3 Chức có Thư Viện OpenCV 26 2.3.4 Ứng dụng Thư Viện OpenCV 26 2.3.5 Hệ điều hành cho Raspberry Pi 27 2.3.5.1 Hệ điều hành Raspbian 27 2.3.5.2 Cài đặt Hệ điều hành Raspbian 28 2.3.5.1 Cài đặt Python OpenCV 31 CHƯƠNG LỰA CHỌN CÁC THIẾT BỊ PHẦN CỨNG 35 3.1 Sơ đồ chung kết nối phần cứng 35 3.2 Lựa chọn thiết bị phần cứng 35 ix an GPIO.output(MODE_CONTROL_2, False) GPIO.output(MODE_CONTROL_3, False) GPIO.output(MODE_CONTROL_1, GPIO.LOW) GPIO.output(MODE_CONTROL_2, GPIO.LOW) GPIO.output(MODE_CONTROL_3, GPIO.LOW) GPIO.output(CLEARN_MOTOR, False) def handleprocess(error): output = error print(output) if(output > 15): #quay trai: print("Quay trai") GPIO.output(MODE_CONTROL_1, GPIO.LOW) GPIO.output(MODE_CONTROL_2, GPIO.LOW) GPIO.output(MODE_CONTROL_3, GPIO.HIGH) if(output < -15): #quay phai: print("Quay phai") GPIO.output(MODE_CONTROL_1, GPIO.LOW) GPIO.output(MODE_CONTROL_2, GPIO.HIGH) GPIO.output(MODE_CONTROL_3, GPIO.HIGH) if ((output = -15)): #dithang: print("Di thang") GPIO.output(MODE_CONTROL_1, GPIO.HIGH) GPIO.output(MODE_CONTROL_2, GPIO.LOW) an GPIO.output(MODE_CONTROL_3, GPIO.LOW) flagy = flagb = #Camera setup camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 20 rawCapture = PiRGBArray(camera, size = (640, 480)) time.sleep(0.1) #Blue HLB = 91; SLB = 100; VLB = 100; HUB =111; SUB = 255; VUB = 255; #Yellow HLY = 15; SLY = 100; VLY = 100; HUY = 35; SUY = 255; VUY = 255; icol1 = (HLB, SLB, VLB, HUB, SUB, VUB) # Blue icol2 = (HLY, SLY, VLY, HUY, SUY, VUY) # Yellow colors = {'blue':(255,0,0), 'yellow':(0,198,234 # define the lower and upper boundaries of the colors in the HSV color space lower = {'blue':(HLB, SLB, VLB),'yellow':(HLY,SLY, VLY)} upper = { 'blue':(HUB,SUB,VUB),'yellow':(HUY,SUY,VUY)} an for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): image = frame.array height, width, channel = image.shape lower = {'blue':(HLB, SLB, VLB),'yellow':(HLY, SLY, VLY)} upper = {'blue':(HUB, SUB, VUB),'yellow':(HUY, SUY, VUY)} blurred = cv2.GaussianBlur(image, (11, 11), 0) hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) for key, value in upper.items(): if key == 'blue': mask = cv2.inRange(hsv, lower[key], upper[key]) if key == 'yellow': mask = cv2.inRange(hsv, lower[key], upper[key]) Cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] #print(cnts) center = None if len(cnts) > : c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) if M["m00"] >0 : center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) if radius > 5: #print("ok fine") an cv2.line(image,(0,int(y)),(640,int(y)),colors[key],1) cv2.line(image,(int(x),0),(int(x),480),colors[key],1) cv2.line(image,(320,240),(int(x), int(y)),colors[key],2) cv2.putText(image,key + "item", (int(x-radius),int(y-radius)), cv2.FONT_HERSHEY_SIMPLEX, 0.6,colors[key],1) if key == 'blue': flagb = flagy = if (flagb == 1) posxb = int(x) posyb = int(y) errorx_b = 320 - posxb errory_b = 240 - posyb if not (((posyb < 480) and (posyb > 440)) and ((errorx_b < 160) and (errorx_b > -160))) handleprocess(errorx_b) GPIO.output(CLEARN_MOTOR, GPIO.LOW) #print("Chuong trinh precess blue") else: current_time = time.time() while True: print("Clean mode") print("di thang") GPIO.output(MODE_CONTROL_1, GPIO.HIGH) GPIO.output(MODE_CONTROL_2, GPIO.LOW) GPIO.output(MODE_CONTROL_3, GPIO.LOW) an if(time.time() - current_time > 3): GPIO.output(CLEARN_MOTOR, GPIO.HIGH) if(time.time() - current_time > 6): GPIO.output(CLEARN_MOTOR, GPIO.LOW) flagb = flagy = break time.sleep(0.1) cv2.putText(image,"Sai so x = " + str(errorx_b), (10,10), cv2.FONT_HERSHEY_SIMPLEX, 0.3,colors[key],1) cv2.putText(image,"Sai so y = " + str(errory_b), (10,20), cv2.FONT_HERSHEY_SIMPLEX, 0.3,colors[key],1) elif key == 'yellow': if (flagy == 1): posxg = int(x) posyg = int(y) errorx_g = 320 - posxg errory_g = 240 - posyg if not (((posyg < 480) and (posyg > 440)) and ((errorx_g < 160) and (errorx_g > -160))): handleprocess(errorx_g) GPIO.output(CLEARN_MOTOR, GPIO.LOW) #print("Chuong trinh precess yellow") else: current_time = time.time() while True: print("Clean mode") #add clearn mode later an print("di thang") GPIO.output(MODE_CONTROL_1, GPIO.HIGH) GPIO.output(MODE_CONTROL_2, GPIO.LOW) GPIO.output(MODE_CONTROL_3, GPIO.LOW) if(time.time() - current_time > 3): GPIO.output(CLEARN_MOTOR, GPIO.HIGH) if(time.time() - current_time > 6): GPIO.output(CLEARN_MOTOR, GPIO.LOW) flagb = flagy = break time.sleep(0.1) cv2.putText(image,"Sai so x = " + str(errorx_g), (100,10), cv2.FONT_HERSHEY_SIMPLEX, 0.3,colors[key],1) cv2.putText(image,"Sai so y = " + str(errory_g), (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.3,colors[key],1) else: print("Finding trash") GPIO.output(MODE_CONTROL_1, GPIO.LOW) GPIO.output(MODE_CONTROL_2, GPIO.LOW) GPIO.output(MODE_CONTROL_3, GPIO.HIGH) GPIO.output(CLEARN_MOTOR, GPIO.LOW) print("khong co ") cv2.rectangle(image, (160, 480), (480, 440), (480, 480), 2) cv2.putText(image,"Clearn zone", (282,450),cv2.FONT_HERSHEY_SIMPLEX, 0.4,colors[key],1) cv2.line(image,(0,240),(640,240),(111,0,250),2) an cv2.line(image,(320,0),(320,480),(111,0,250),2) #cv2.imshow("image", image) #cv2.imshow("hsv", hsv) rawCapture.truncate(0) #print("cmm") key = cv2.waitKey(1) & 0xFF if key == ord('q'): break GPIO.output(MODE_CONTROL_1, GPIO.LOW) GPIO.output(MODE_CONTROL_2, GPIO.LOW) GPIO.output(MODE_CONTROL_3, GPIO.LOW) GPIO.output(CLEARN_MOTOR, GPIO.LOW) cv2.destroyAllWindows() camera.close()  Code chương trình điều khiển Step Motor Arduino define BAUD (9600) #Pin definition int MOTORR_ENABLE_PIN = 2; int MOTORR_STEP_PIN = 3; int MOTORR_DIR_PIN = 4; int MOTORL_ENABLE_PIN = 5; int MOTORL_STEP_PIN = 6; int MOTORL_DIR_PIN = 7; int RELAY_TRIGGER_PIN = 8; int RASP_TRIGGER1_PIN = 10; int RASP_TRIGGER2_PIN = 11; an int RASP_TRIGGER3_PIN = 12; #constant definition const long timeDelayOneStep = 15000; const long timeUpdateSensor = 2000; #Variable definition int checkMotorStep = 0; unsigned long currentMicros = 0; unsigned long preSensorMicros = 0; unsigned long preMotorMicros = 0; #All flags for program int flagRunningMotor = 0; #Function Prototype void startRobot(); void stopRobot(); void goAhead(); void goBack(); void turnLeft(); void turnRight(); void runRobot(); void setup() { Serial.begin(BAUD); pinMode(MOTORR_ENABLE_PIN, OUTPUT); an pinMode(MOTORR_STEP_PIN, OUTPUT); pinMode(MOTORR_DIR_PIN, OUTPUT); pinMode(MOTORL_ENABLE_PIN, OUTPUT); pinMode(MOTORL_STEP_PIN, OUTPUT); pinMode(MOTORL_DIR_PIN, OUTPUT); pinMode(RASP_TRIGGER1_PIN, INPUT); pinMode(RASP_TRIGGER2_PIN, INPUT); pinMode(RASP_TRIGGER3_PIN, INPUT); digitalWrite(MOTORR_ENABLE_PIN, HIGH); digitalWrite(MOTORL_ENABLE_PIN, HIGH); startRobot(); delay(1000); } void loop() { //currentMicros = micros(); //if (currentMicros - preSensorMicros >= timeUpdateSensor) { //preSensorMicros = currentMicros; if ((digitalRead(RASP_TRIGGER3_PIN) == 0) && (digitalRead(RASP_TRIGGER2_PIN) == 0) && (digitalRead(RASP_TRIGGER1_PIN) == 1)) { startRobot(); goAhead(); runRobot(); } an if ((digitalRead(RASP_TRIGGER3_PIN) == 0) && (digitalRead(RASP_TRIGGER2_PIN) == 1) && (digitalRead(RASP_TRIGGER1_PIN) == 0)) { startRobot(); goBack(); runRobot(); } if ((digitalRead(RASP_TRIGGER3_PIN) == 1) && (digitalRead(RASP_TRIGGER2_PIN) == 0) && (digitalRead(RASP_TRIGGER1_PIN) == 0)) { startRobot(); turnLeft(); runRobot(); } if ((digitalRead(RASP_TRIGGER3_PIN) == 1) && (digitalRead(RASP_TRIGGER2_PIN) == 1) && (digitalRead(RASP_TRIGGER1_PIN) == 0)) { startRobot(); turnRight(); runRobot(); } if ((digitalRead(RASP_TRIGGER3_PIN) == 0) && (digitalRead(RASP_TRIGGER2_PIN) == 0) && (digitalRead(RASP_TRIGGER1_PIN) == 0)) { stopRobot(); an } if (currentMicros - preMotorMicros >= timeDelayOneStep) { preMotorMicros = currentMicros; runRobot(); } } #Function Definition void startRobot() { digitalWrite(MOTORR_ENABLE_PIN, LOW); digitalWrite(MOTORL_ENABLE_PIN, LOW); } void stopRobot() { digitalWrite(MOTORR_ENABLE_PIN, HIGH); digitalWrite(MOTORL_ENABLE_PIN, HIGH); } void goAhead() { digitalWrite(MOTORR_DIR_PIN, LOW); digitalWrite(MOTORL_DIR_PIN, HIGH); } an an void goBack() { digitalWrite(MOTORR_DIR_PIN, HIGH); digitalWrite(MOTORL_DIR_PIN, LOW); } void turnLeft() { digitalWrite(MOTORR_DIR_PIN, LOW); digitalWrite(MOTORL_DIR_PIN, LOW); } void turnRight() { digitalWrite(MOTORR_DIR_PIN, HIGH); digitalWrite(MOTORL_DIR_PIN, HIGH); } void runRobot() { if (checkMotorStep == 0) { digitalWrite(MOTORR_STEP_PIN, HIGH); digitalWrite(MOTORL_STEP_PIN, HIGH); delay(20); checkMotorStep = 1; } an else { digitalWrite(MOTORR_STEP_PIN, LOW); digitalWrite(MOTORL_STEP_PIN, LOW); delay(20); checkMotorStep = 0; } } an an

Ngày đăng: 27/12/2023, 03:31

Xem thêm:

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN