Điều khiển xe robot bằng giọng nói với raspberry pi

81 366 0
Điều khiển xe robot bằng giọng nói với raspberry pi

Đ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 & ĐÀO TẠO TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP HỒ CHÍ MINH KHOA ĐIỆN – ĐIỆN TỬ BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH ĐỒ ÁN TỐT NGHIỆP NGÀNH CÔNG NGHỆ KỸ THUẬT ĐIỆN TỬ TRUYỀN THƠNG ĐỀ TÀI: ĐIỀU KHIỂN XE ROBOT BẰNG GIỌNG NĨI VỚI RASPBERRY PI GVHD: ThS Nguyễn Duy Thảo SVTH: Phan Thanh Tồn MSSV: 11141222 Tp Hồ Chí Minh - 07/2018 BỘ GIÁO DỤC & ĐÀO TẠO TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP HỒ CHÍ MINH KHOA ĐIỆN – ĐIỆN TỬ BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH - ĐỒ ÁN TỐT NGHIỆP NGÀNH KỸ THUẬT ĐIỆN TỬ TRUYỀN THÔNG ĐỀ TÀI: ĐIỀU KHIỂN XE ROBOT BẰNG GIỌNG NÓI VỚI RASPBERRY PI GVHD: Th.S Nguyễn Duy Thảo SVTH: Phan Thanh Tồn MSSV: 11141222 Tp Hồ Chí Minh – 07/2018 TRƯỜNG ĐH SPKT TP HỒ CHÍ MINH KHOA ĐIỆN-ĐIỆN TỬ BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM ĐỘC LẬP - TỰ DO - HẠNH PHÚC o0o Tp:HCM ngày 01 thánh 07 năm 2018 NHIỆM VỤ ĐỒ ÁN TỐT NGHIỆP Họ tên sinh viên: Phanh Thanh Toàn MSSV: 11141222 Võ Hoàng Khánh MSSV: 11141102 Chuyên ngành: Điện Tử Công Nghiệp Mã ngành: 141 Hệ đào tạo: Đại học quy Mã hệ: 11 Khóa : 11 Lớp 11141DT1A I TÊN ĐỀ TÀI: ĐIỀU KHIỂN XE ROBOT BẰNG GIỌNG NÓI VỚI RASPBERRY PI II NHIỆM VỤ Các số liệu ban đầu: (ghi thông số, tập tài liệu tín hiệu, hình ảnh,…) Thơng số ban đầu xử lý tín hiệu âm Tài liệu: Giáo trình Kỹ thuật truyền số liệu Nội dung thực hiện: (ghi nội dung cần thực phần tổng quan) Đặt vấn đề , nêu mục tiêu đề tài,những nội dung nghiên cứu, bố cục đề tài cuối giới hạn đề tài III NGÀY GIAO NHIỆM VỤ: 20/03/2018 IV NGÀY HOÀN THÀNH NHIỆM VỤ: 30/06/2018 V HỌ VÀ TÊN CÁN BỘ HƯỚNG DẪN: Th.S Nguyễn Duy Thảo CÁN BỘ HƯỚNG DẪN BM ĐIỆN TỬ CÔNG NGHIỆP – Y SINH TRƯỜNG ĐH SPKT TP HỒ CHÍ MINH KHOA ĐIỆN-ĐIỆN TỬ BỘ MƠN ĐIỆN TỬ CƠNG NGHIỆP – Y SINH CỘNG HỊA XÃ HỘI CHỦ NGHĨA VIỆT NAM ĐỘC LẬP - TỰ DO - HẠNH PHÚC o0o Tp HCM, ngày 20 tháng 03 năm 2018 LỊCH TRÌNH THỰC HIỆN ĐỒ ÁN TỐT NGHIỆP Họ tên sinh viên 1: Phan Thanh Toàn Lớp: 11141DT1A MSSV:11141222 Họ tên sinh viên 2: Võ Hoàng Khánh Lớp: 11141DT1A MSSV:11141102 Tên đề tài: ĐIỀU KHIỂN XE ROBOT BẰNG GIỌNG NÓI VỚI RASPBERRY PI Tuần/ngày Nội dung Tuần1 Chọn đề tài Tuần Tìm hiểu nhận đề tài Tuần Nhận đề tài để thực hiên Tuần Nghiên cứu đề tài Tuần Tìm tài liệu đề tài Tuần Chuẩn bị mua linh kiện Tuần Tiến hành thi công phần cứng Tuần Thi công phần cứng Tuần Thi cơng phần cứng Tuần 10 Viết chương trình Tuần 11 Viết chương trình Tuần 12 Nạp code chạy đề mode Tuần 13 Hoàng thành 100% sản phẩm Tuần 14 Viết báo cáo đề tài Tuần 15 Hoàng thành file báo cáo Xác nhận GVHD GV HƯỚNG DẪN (Ký ghi rõ họ tên LỜI CAM ĐOAN Đề tài tự thực dựa vào số tài liệu trước khơng chép từ tài liệu hay cơng trình có trước Người thực đề tài Phan Thanh Tồn Võ Hồng Khánh LỜI CẢM ƠN Lời nhóm sinh viên thực đề tài xin gửi lời cám ơn chân thành tới thầy cô giáo trường Đại Học Sư Phạm Kỹ Thuật Thành Phố Hồ Chí Minh nói chung thầy giáo khoa Điện – Điện tử nói riêng tận tình giảng dạy, truyền đạt cho kiến thức, kinh nghiệm quý báu suốt thời gian qua Đặc biệt xin gửi lời cảm ơn sâu sắc đến thầy Nguyễn Duy Thảo định hướng, hướng dẫn tận tình tạo điều kiện tốt cho suốt q trình chúng tơi thực đề tài Sau xin gửi lời cảm ơn tới anh, chị, bạn giúp đỡ, chia sẻ kinh nghiệm, đóng góp ý kiến cho chúng tơi để hồn thành tốt đề tài Trong trình thực đề tài khơng tránh khỏi thiếu xót Rất mong nhận góp ý q thầy bạn để đề tài hồn thiện Nhóm thực đề tài xin chân thành cảm ơn! Nhóm thực đề tài Phan Thanh Toàn Võ Hoàng Khánh MỤC LỤC Trang bìa I Nhiệm vụ đồ án II Lịch trình III Cam đoan IV Lời cảm ơn V Mục lục VI Liệt kê hình vẽ VII Liệt kê bảng vẽ …………………………………….…………………………… VIII Tóm tắt IX CHƯƠNG TỔNG QUAN 1.1.Đặt vấn đề 1.2.Mục tiêu 1.3.Nội dung nghiên cứu 1.4.Giới hạn 1.5.Bố cục CHƯƠNG CƠ SỞ LÝ THUYẾT 2.1.Tổng quan xe robot điều khiển giọng nói……………………………5 2.1.2 Ưu điểm xe robot điều khiển giọng nói 2.2 Giới thiệu phần cứng 2.2.1 Giới thiệu Raspberry Pi 2.2.2 Hệ điều hành phần mềm 15 2.2.3 Mạch công suất cầu H (L298N)………………………………… ………14 2.2.4 Cảm biến siêu âm 17 2.2.5 Board Arduino 20 2.2.6 USB Sound Card 21 2.2.7 Micro không dây Daile V10 24 2.2.8 Động DC 26 2.2.9 Led Matrix 8x8 MAX7219 26 CHƯƠNG TÍNH TỐN VÀ THIẾT KẾ 27 3.1 Giới thiệu 28 3.2 Tính tốn thiết kế hệ thống 28 3.2.1 Thiết kế sơ đồ khối hệ thống 32 3.2.2 Sơ đồ nguyên lý toàn mạch 32 CHƯƠNG THI CÔNG HỆ THỐNG 44 4.1 Giới thiệu 44 4.2.Thi công hệ thống 44 4.2.1.Chuẩn bị phần cứng 44 4.2.2 Lắp ráp kiểm tra 45 4.3 Lập trình hệ thống 54 4.3.1 Lưu đồ hệ thống điều khiển xe robot giọng nói 54 4.3.2 Phần mềm lập trình visual studio 2017 55 CHƯƠNG KẾT QUẢ_NHẬN XÉT_VÀ ĐÁNH GIÁ 60 5.1 Cảm biến 61 5.2.Bộ điều khiển động 62 5.3.Bộ vi điều khiển 63 5.4.Kết đạt được…………………………………………………………………63 CHƯƠNG KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN…………………67 6.1 Kết luận …………………………………………………………………… 67 6.2 Những hạn chế đề tài……………………………………………………67 6.3 Hướng phát triển…………………………………………………………….67 TÀI LIỆU THAM KHẢO PHU LỤC LIỆT KÊ HÌNH VẼ Hình Trang Hình 2.1 Hình ảnh xe robot điều khiển giọng nói… Hình 2.2 Bo mạch Raspberry Pi Hình 2.3 Sơ đồ cấu tạo Raspberry Pi .7 Hinh 2.4 Sơ đồ chân GPIO Raspberry pi…………………… ……… Hình 2.7 Hệ điều hành SNAPPY ………… ……………….…………… …… 11 Hình 2.10 Module mạch cầu H L298N……………………………………………14 Hình 2.11 Sơ đồ nguyên lý L298N 15 Hình 2.16 Phản xạ sóng siêu âm theo góc 20 Hình 2.17 Vùng phát SRF04 20 Hình 2.18 Board Arduino Mega 2560 20 Hình 2.19 Hình ảnh vi điều khiển board Arduino Mega 2560 21 Hình 2.20.Hình ảnh chân ngõ vào/ra board Arduino Mega 2560………… …21 Hình 2.23 Động DC GA25 26 Hình 2.24 Module Led Matrix 8x8 MAX7219 27 Hình 3.1 Sơ đồ khối hệ thống 28 Hình 3.2 Sơ đồ nguyên lý mạch nguồn 29 Hình 3.3 Sơ đồ nguyên lý mạch hạ áp 30 Hình 3.4 Sơ đồ chân Raspberry pi 30 Hình 3.5 Sơ đồ nguyên lý Module điều khiển động L298N 31 Hình 3.6 Sơ nguyên lý Module Cảm biến Siêu âm SRF04 31 Hình 3.7 Sơ đồ ngun lý Tồn mạch 32 Hình 3.8 Sơ đồ kết nối Raspberry pi với module điều khiển động L298N … 33 Hình 3.9 Sơ đồ kết nối module Cảm biến siêu âm với Raspberry pi 33 Hình 4.1 động bánh xe 45 Hình 4.2 khung xe robot 45 Hình 4.3 Pin dự phòng 5v 46 Hình 4.5 Pin cell 3.7v 47 Hình 4.6 Mạch giảm áp DC-DC 47 Hình 4.7 Module led matrix 47 Hình 4.8 Hình ảnh họa kết nối sai Raspberry pi với Srf04……….…….48 Hình 4.9 Hình ảnh minh họa kết nối Raspberry pi với Srf04 48 Hình 4.10 Mạch cầu phân áp 49 Hình 4.11 Hình ảnh thực tế kết nối raspberry pi với module SRF04 50 Hình 4.12 Hình ảnh thực tế kết nối raspberry pi với module L298N 50 Hình 4.13 Mơ hình thực tế xe robot phía trước 51 Hình 4.14 Mơ hình thực tế xe robot phía sau 52 Hình 4.16 Giao diện đăng nhập Windows 10 IoT Core web 55 Hình 4.18 Giao diện kết nối Windows 10 IoT Core qua PowerShell 56 Hình 4.20 Giao diện điều khiển Windows 10 IoT Core qua PowerShell 57 LIỆT KÊ BẢNG Bảng Trang Bảng 4.2.Bảngso sánh vài thuộc tính hệ điều hành Windows………… 58 CHƯƠNG KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN CHƯƠNG KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN 6.1.KẾT LUẬN  Nắm rõ giao tiếp Raspberry pi với SRF04 giúp cho xe phát vật cản  Nắm rõ giao tiếp Raspberry pi với L298 giúp cho hệ thống xe điều khiển động chạy thuận hay chạy nghịch  Xử lý tín hiệu giọng nói giúp điều khiển hệ thống thơng qua giọng nói người điều khiển  Nhúng chương trình ARM vào Raspberry pi  Hiểu hệ điều hành win 10 Iot core , Hệ điều hành win 10 Iot core tích hợp nhiều ứng dụng Trong ứng dung xử lý âm dự mã nguồn mở giúp cho nhóm thực đề tài điều khiển xe giọng nói  Nhóm hồn thành đề tài khoảng 95% với muchj đích đề  Sản phẩm nhóm chạy ổn định va mơ hình xe đẹp mắt 6.2 NHỮNG HẠN CHẾ CỦA ĐỀ TÀI  Tín hiệu điều khiển tiếng anh  Còn nhiễu tín hiệu nhiều  Đây la vần đề nhóm chưa làm 6.3.HƯỚNG PHÁT TRIỂN 6.3.1 Hướng khắc phục cách hạn chế đề tài  Tín hiệu điều khiển tiếng việt  Sử dụng thiết bị thu nhận âm tốt để lọc nhiễu tiếng ồn tốt 6.3.2 Hướng phát triển tương lai  Nhóm phát triển thành hệ thống xe lăn điều khiển giọng nói  Phát triển hệ thống người robot làm việc điều khiển giọng nói BỘ MƠN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 55 TÀI LIỆU THAM KHẢO TÀI LIỆU THAM KHẢO [1] Đồn Văn Ban, phân tích thiết kế hướng đối tượng, NXB Khoa học Kỹ thuật.1999 [2] Cộng đồng Microsoft,Trang web www.support.microsoft.com [3] Nguyễn Việt Hùng, Nguyễn Ngơ Lâm, Nguyễn Văn Phúc, Giáo trình Kỹ thuật truyền số liệu, Trường ĐH Sư Phạm Kỹ Thuật, 9.2011 [4] Ivar Jacobson Object – Oriented Software Engineering, Addison-Wesley Publishing Company, 1992 [5] Cộng đồng Raspberry Việt Nam, Trang web www Raspberry.com.vn BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 56 PHỤ LỤC PHỤ LỤC: CODE CHƯƠNG TRÌNH ĐIỀU KHIỂN XE ROBOT BẰNG GIỌNG NĨI  Code chương trình using System; using System.Diagnostics; using System.Threading.Tasks; using Windows.ApplicationModel; using Windows.Media.SpeechRecognition; using Windows.Storage; using Windows.UI.Xaml.Controls; namespace SpeechControlledRobot RPi2_ { public sealed partial class MainPage : Page { private SpeechRecognizer MyRecognizer; private bool AvoidObstacle = true; public MainPage() { this.InitializeComponent(); MotorDriver.InitializePins(MotorDriver.AvailableGpioPin.GpioPin_5, MotorDriver.AvailableGpioPin.GpioPin_6, MotorDriver.AvailableGpioPin.GpioPin_13, MotorDriver.AvailableGpioPin.GpioPin_26); InitializeSpeechRecognizer(); Sensor.UltrasonicDistanceSensor Front_DistanceSensor = new Sensor.UltrasonicDistanceSensor(Sensor.UltrasonicDistanceSensor.AvailableGpioP in.GpioPin_12, Sensor.UltrasonicDistanceSensor.AvailableGpioPin.GpioPin_16); Sensor.ObstacleDetection Front_ObstacleDetection = new Sensor.ObstacleDetection(Front_DistanceSensor); Front_ObstacleDetection.ObstacleDetected += Front_ObstacleDetection_ObstacleDetected; } BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 57 PHỤ LỤC private void Front_ObstacleDetection_ObstacleDetected(Sensor.ObstacleDetection.DetectionSta te arg1, double arg2) { if (AvoidObstacle == true) { if (arg1 == Sensor.ObstacleDetection.DetectionState.Detected && MotorDriver.CurrentState == MotorDriver.State.MovingForward) { MotorDriver.Stop(); Debug.WriteLine("Robot stopped! (Obstacle Detected)"); } } if (AvoidObstacle == true) { if (arg1 == Sensor.ObstacleDetection.DetectionState.Detected && MotorDriver.CurrentState == MotorDriver.State.MovingReverse) { MotorDriver.Stop(); Debug.WriteLine("Robot stopped! (Obstacle Detected)"); } } } /// /// Initializes MyRecognizer and Loads Grammar from File 'Grammar\MyGrammar.xaml' /// private async void InitializeSpeechRecognizer() { MyRecognizer = new SpeechRecognizer(); MyRecognizer.StateChanged += MyRecognizer_StateChanged; MyRecognizer.ContinuousRecognitionSession.ResultGenerated += MyRecognizer_ResultGenerated; BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 58 PHỤ LỤC StorageFile GrammarContentFile = await Package.Current.InstalledLocation.GetFileAsync(@"Grammar\MyGrammar.xml"); SpeechRecognitionGrammarFileConstraint GrammarConstraint = new SpeechRecognitionGrammarFileConstraint(GrammarContentFile); MyRecognizer.Constraints.Add(GrammarConstraint); SpeechRecognitionCompilationResult CompilationResult = await MyRecognizer.CompileConstraintsAsync(); Debug.WriteLine("Status: " + CompilationResult.Status.ToString()); if (CompilationResult.Status == SpeechRecognitionResultStatus.Success) { await MyRecognizer.ContinuousRecognitionSession.StartAsync(); } } /// /// Fires when MyRecognizer successfully parses a speech /// /// /// private void MyRecognizer_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args) { Debug.WriteLine(args.Result.Text); switch (args.Result.Text) { case "move forward": MotorDriver.MoveForward(); break; case "move reverse": MotorDriver.MoveReverse(); break; case "turn right": MotorDriver.TurnRight(); break; case "turn left": BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 59 PHỤ LỤC MotorDriver.TurnLeft(); break; case "stop": MotorDriver.Stop(); break; case "engage obstacle detection": AvoidObstacle = true; break; case "disengage obstacle detection": AvoidObstacle = false; break; default: break; } } /// /// Fires when MyRecognizer's state changed /// /// /// private void MyRecognizer_StateChanged(SpeechRecognizer sender, SpeechRecognizerStateChangedEventArgs args) { Debug.WriteLine(args.State); } } }  Code chương trình con:Drive_động using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Devices.Gpio; namespace SpeechControlledRobot RPi2_ { BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 60 PHỤ LỤC /// /// Provides abstraction layer to drive robot /// public static class MotorDriver { private static GpioPin MotorLeft_A, MotorLeft_B; private static GpioPin MotorRight_A, MotorRight_B; private static State _CurrentState; /// /// Available Gpio Pins Refer: /// public enum AvailableGpioPin : int { /// /// Raspberry Pi - Header Pin Number : 29 /// GpioPin_5 = 5, /// /// Raspberry Pi - Header Pin Number : 31 /// GpioPin_6 = 6, /// /// Raspberry Pi - Header Pin Number : 32 /// GpioPin_12 = 12, /// /// Raspberry Pi - Header Pin Number : 33 /// GpioPin_13 = 13, /// /// Raspberry Pi - Header Pin Number : 36 /// GpioPin_16 = 16, /// /// Raspberry Pi - Header Pin Number : 12 /// GpioPin_18 = 18, /// /// Raspberry Pi - Header Pin Number : 15 BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 61 PHỤ LỤC /// GpioPin_22 = 22, /// /// Raspberry Pi - Header Pin Number : 16 /// GpioPin_23 = 23, /// /// Raspberry Pi - Header Pin Number : 18 /// GpioPin_24 = 24, /// /// Raspberry Pi - Header Pin Number : 22 /// GpioPin_25 = 25, /// /// Raspberry Pi - Header Pin Number : 37 /// GpioPin_26 = 26, /// /// Raspberry Pi - Header Pin Number : 13 /// GpioPin_27 = 27 } /// /// Available driving states /// public enum State : byte { MovingForward, MovingReverse, RotatingRight, RotatingLeft, TurningRight, TurningLeft, Stopped } public static State CurrentState { BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 62 PHỤ LỤC get { return _CurrentState; } } /// /// Initializes four pins for motor driver /// /// Left motor's Pin A. /// Left motor's Pin B. /// Right motor's Pin A. /// Right motor's Pin B. public static void InitializePins(AvailableGpioPin Pin_MotorLeft_A, AvailableGpioPin Pin_MotorLeft_B, AvailableGpioPin Pin_MotorRight_A, AvailableGpioPin Pin_MotorRight_B) { GpioController MyGpioConroller = GpioController.GetDefault(); if (MotorLeft_A != null) { MotorLeft_A = null; MotorLeft_B = null; MotorRight_A = null; MotorRight_B = null; } MotorLeft_A = MyGpioConroller.OpenPin((int)Pin_MotorLeft_A); MotorLeft_B = MyGpioConroller.OpenPin((int)Pin_MotorLeft_B); MotorRight_A = MyGpioConroller.OpenPin((int)Pin_MotorRight_A); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 63 PHỤ LỤC MotorRight_B = MyGpioConroller.OpenPin((int)Pin_MotorRight_B); MotorLeft_A.SetDriveMode(GpioPinDriveMode.Output); MotorLeft_B.SetDriveMode(GpioPinDriveMode.Output); MotorRight_A.SetDriveMode(GpioPinDriveMode.Output); MotorRight_B.SetDriveMode(GpioPinDriveMode.Output); _CurrentState = State.Stopped; } public static void MoveForward() { if (MotorLeft_A == null) return; MotorLeft_A.Write(GpioPinValue.Low); MotorLeft_B.Write(GpioPinValue.High); MotorRight_A.Write(GpioPinValue.Low); MotorRight_B.Write(GpioPinValue.High); _CurrentState = State.MovingForward; } public static void MoveReverse() { if (MotorLeft_A == null) return; MotorLeft_A.Write(GpioPinValue.High); MotorLeft_B.Write(GpioPinValue.Low); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 64 PHỤ LỤC MotorRight_A.Write(GpioPinValue.High); MotorRight_B.Write(GpioPinValue.Low); _CurrentState = State.MovingReverse; } public static void TurnRight() { // Return if pins are not initialized if (MotorLeft_A == null) return; // Left Motor MotorLeft_A.Write(GpioPinValue.High); MotorLeft_B.Write(GpioPinValue.Low); // Right Motor MotorRight_A.Write(GpioPinValue.Low); MotorRight_B.Write(GpioPinValue.High); // Set current state _CurrentState = State.RotatingRight; } public static void TurnLeft() { if (MotorLeft_A == null) return; MotorLeft_A.Write(GpioPinValue.Low); MotorLeft_B.Write(GpioPinValue.High); MotorRight_A.Write(GpioPinValue.High); MotorRight_B.Write(GpioPinValue.Low); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 65 PHỤ LỤC _CurrentState = State.RotatingLeft; } public static void Stop() { if (MotorLeft_A == null) return; MotorLeft_A.Write(GpioPinValue.Low); MotorLeft_B.Write(GpioPinValue.Low); MotorRight_A.Write(GpioPinValue.Low); MotorRight_B.Write(GpioPinValue.Low) _CurrentState = State.Stopped; } } }  Code chương trình con:cảm biến siêu âm SRF04 using System; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using Windows.Devices.Gpio; namespace SpeechControlledRobot RPi2_.Sensor { public class UltrasonicDistanceSensor { private GpioPin Pin_Trig, Pin_Echo; private Stopwatch _StopWatch; private double? _Distance; BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 66 PHỤ LỤC /// /// Available Gpio Pins Refer: https://ms-iot.github.io/content/enUS/win10/samples/PinMappingsRPi2.htm /// public enum AvailableGpioPin : int { /// /// Raspberry Pi - Header Pin Number : 29 /// GpioPin_5 = 5, /// /// Raspberry Pi - Header Pin Number : 31 /// GpioPin_6 = 6, /// /// Raspberry Pi - Header Pin Number : 32 /// GpioPin_12 = 12, /// /// Raspberry Pi - Header Pin Number : 33 /// GpioPin_13 = 13, /// /// Raspberry Pi - Header Pin Number : 36 /// GpioPin_16 = 16, /// /// Raspberry Pi - Header Pin Number : 12 /// GpioPin_18 = 18, /// /// Raspberry Pi - Header Pin Number : 15 /// GpioPin_22 = 22, /// /// Raspberry Pi - Header Pin Number : 16 /// GpioPin_23 = 23, BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 67 PHỤ LỤC /// /// Raspberry Pi - Header Pin Number : 18 /// GpioPin_24 = 24, /// /// Raspberry Pi - Header Pin Number : 22 /// GpioPin_25 = 25, /// /// Raspberry Pi - Header Pin Number : 37 /// GpioPin_26 = 26, /// /// Raspberry Pi - Header Pin Number : 13 /// GpioPin_27 = 27 } public UltrasonicDistanceSensor(AvailableGpioPin TrigPin, AvailableGpioPin EchoPin) { _StopWatch = new Stopwatch(); var gpio = GpioController.GetDefault(); Pin_Trig = gpio.OpenPin((int)TrigPin); Pin_Trig.SetDriveMode(GpioPinDriveMode.Output); Pin_Trig.Write(GpioPinValue.Low); Pin_Echo = gpio.OpenPin((int)EchoPin); Pin_Echo.SetDriveMode(GpioPinDriveMode.Input); } public double GetDistance() { var mre = new ManualResetEventSlim(false); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 68 PHỤ LỤC Pin_Trig.Write(GpioPinValue.High); mre.Wait(TimeSpan.FromMilliseconds(0.01)); Pin_Trig.Write(GpioPinValue.Low); var time = PulseIn(Pin_Echo, GpioPinValue.High,500); var distance = time * 17000; return distance; } private double PulseIn(GpioPin pin, GpioPinValue value, ushort timeout) { var sw = new Stopwatch(); var sw_timeout = new Stopwatch(); sw_timeout.Start(); while (pin.Read() != value) { if (sw_timeout.ElapsedMilliseconds > timeout) return 3.5; } sw.Start(); while (pin.Read() == value) { if (sw_timeout.ElapsedMilliseconds > timeout) return 3.4; } sw.Stop(); return sw.Elapsed.TotalSeconds; } } } BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 69 ... 2.1.TỔNG QUAN VỀ XE ROBOT ĐIỀU KHIỂN BẰNG GIỌNG NÓI 2.1.1 Định nghĩa xe robot điều khiển giọng nói Xe robot điều khiển giọng nói (tiếng Anh:speech controlled car robot) tí hiệu tiếng nói qua micro... năng:  Xe chay tới, lui, qua trái, qua phải, đướng lại  Xe gặp vật cản đứng lại Hình 2.1 Hình ảnh xe robot điều khiển giọng nói 2.1.2 Ưu điểm xe điều khiển giọng nói Xe điều khiển giọng nói có... 2.1.Tổng quan xe robot điều khiển giọng nói …………………………5 2.1.2 Ưu điểm xe robot điều khiển giọng nói 2.2 Giới thiệu phần cứng 2.2.1 Giới thiệu Raspberry Pi 2.2.2 Hệ điều hành

Ngày đăng: 11/03/2019, 16:44

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan