Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth Xe mô hình tránh vật cản điều khiển bằng bluetooth
Các linh kiện cụ thể
Arduino UNO R3
Một board mạch chủ chính dùng để truyền và nhận dữ liệu từ người sử dụng đến các thiết bị điện tử khác.
Các thông số cơ bản:
Vi điều khiển ATmega328 họ 8bit Điện áp hoạt động 5V DC (chỉ được cấp qua cổng USB)
Tần số hoạt động 16 MHz
Dòng tiêu thụ khoảng 30mA Điện áp vào khuyên dùng 7-12V DC Điện áp vào giới hạn 6-20V DC
Số chân Digital I/O 14 (6 chân hardware PWM)
Số chân Analog 6 (độ phân giải 10bit)
Dòng tối đa trên mỗi chân I/O 30 mA
Dòng ra tối đa (5V) 500 mA
Dòng ra tối đa (3.3V) 50 mA
Bộ nhớ flash 32 KB (ATmega328) với 0.5KB dùng bởi bootloader
Module điều khiển động cơ L298 mạch cầu H
Driver: L298N tích hợp hai mạch cầu H.
Dòng tối đa cho mỗi cầu H là: 2A (=>2A cho mỗi motor)
Điện áp của tín hiệu điều khiển: +5 V ~ +7 V
Dòng của tín hiệu điều khiển: 0 ~ 36mA
Công suất hao phí: 20W (khi nhiệt độ T = 75 ℃ )
12V power, 5V power Đây là 2 chân cấp nguồn trực tiếp đến động cơ.
Chúng ta có thể điều chỉnh tốc độ động cơ DC bằng cách kiểm soát điện áp đầu vào Phương pháp phổ biến nhất để thực hiện điều này là sử dụng tín hiệu PWM.
PWM (Pulse Width Modulation) hay thay đổi độ rộng xung là kỹ thuật điều chỉnh điện áp trung bình đến thiết bị điện tử bằng cách bật và tắt nguồn nhanh chóng Giá trị điện áp trung bình phụ thuộc vào chu kỳ xung, tức là tỷ lệ thời gian tín hiệu BẬT so với thời gian tín hiệu TẮT trong một khoảng thời gian nhất định.
Vì vậy: Tùy thuộc vào kích thước của động cơ, chúng ta có thể chỉ cần kết nối đầu ra
Arduino có thể điều khiển tốc độ động cơ bằng cách sử dụng chân cua điện trở hoặc chân MOSFET thông qua tín hiệu PWM Tín hiệu công suất thấp từ Arduino bật và tắt chân MOSFET, cho phép điều khiển động cơ công suất cao Một phương pháp phổ biến để điều khiển động cơ DC là sử dụng mạch cầu H.
Để điều khiển hướng quay của động cơ, chúng ta cần đảo ngược dòng điện, và mạch cầu H là phương pháp phổ biến nhất để thực hiện điều này Mạch cầu H bao gồm bốn chân chuyển mạch, điện trở hoặc MOSFET, với động cơ nằm ở trung tâm tạo thành hình chữ H Bằng cách kích hoạt hai công tắc đồng thời, chúng ta có thể thay đổi hướng dòng điện và do đó thay đổi hướng quay của động cơ.
Cảm biến khoảng cách HC-SR04
Cảm biến siêu âm đo khoảng cách bằng cách sử dụng công thức tính toán dựa trên thời gian và vận tốc của sóng cao tần mà nó phát ra.
VCC Cấp nguồn cho cảm biến (5V) hoặc 3.3V ở cảm biến 3V3
TRIGGER Chân phát sóng âm Là chu kỳ của của điện cao /thấp diễn ra.
ECHO Trạng thái ban dầu là 0V, khi có tín hiệu trả về sẽ là 5V và sau đó trở về 0V GND Nối cực âm của mạch
Nguyên lý hoạt động của cảm biến siêu âm là phát một xung ngắn 5 microSeconds từ chân Trig Sau đó, cảm biến sẽ tạo ra một xung HIGH ở chân Echo cho đến khi nhận được sóng phản xạ Chiều rộng của xung này tương ứng với thời gian sóng siêu âm phát ra và quay trở lại.
Tốc độ âm thanh trong không khí là 340 m/s, tương đương với 29,412 microSeconds/cm Để tính khoảng cách, ta chia thời gian đã tính cho 29,412 và sau đó chia tiếp cho 6.
Bluetooth HC-06
Module Bluetooth SLAVE cho phép vi điều khiển kết nối với các thiết bị ngoại vi như smartphone, laptop và USB Bluetooth Giao tiếp Serial được sử dụng để gửi và nhận tín hiệu, giúp việc truyền tải dữ liệu trở nên dễ dàng và hiệu quả.
2 chiều Module bluetooth được tích hợp trên board cho phép bạn sử dụng nguồn từ
Module Bluetooth SLAVE cho phép vi điều khiển kết nối dễ dàng với các thiết bị ngoại vi như smartphone, laptop và USB Bluetooth Giao tiếp Serial được sử dụng để gửi và nhận tín hiệu, tạo điều kiện thuận lợi cho việc truyền dữ liệu giữa các thiết bị.
Module Bluetooth tích hợp trên bo mạch cho phép sử dụng nguồn từ 3.5V đến 6V, giúp tránh lo ngại về sự chênh lệch điện áp 3V - 5V có thể gây hỏng bo mạch.
- Module Bluetooth gồm 6 chân theo thứ tự: KEY, VCC, GND, TX, RX,
Module Bluetooth SLAVE không cho phép vi điều khiển tự kết nối, mà cần sử dụng smartphone, laptop hoặc Bluetooth USB để tìm kiếm và kết nối tín hiệu Sau khi kết nối thành công, bạn có thể gửi và nhận tín hiệu giữa vi điều khiển và các thiết bị này.
- Không được gắn lộn dây Vcc và Gnd vì sẽ gây hỏng module bluetooth ngay lập tức.Mặc định, module bluetooth SLAVE sử dụng baud rate là 9600, PIN là 1234.
Điện áp hoạt động: 3.3 ~ 5VDC.
Điện áp giao tiếp: TTL tương thích 3.3VDC và 5VDC.
Baudrate UART( tốc độ hỗ trợ) có thể chọn được: 1200, 2400, 4800, 9600, 19200,
Dải tần sóng hoạt động: Bluetooth 2.4GHz
Phạm vi hoạt động 8m ( hiệu quả 5m)
Kích thước của module chính: 28 mm x 15 mm x 2.35 mm
Slave module Bluetooth hc06 giúp chúng ta kết nối các thiết bị có hỗ trợ
Bluetooth với VDK cho phép gửi và nhận dữ liệu giữa hai thiết bị thông qua chuẩn giao tiếp UART, giúp dễ dàng kết nối với VDK Tốc độ baud và mật khẩu của module được cài đặt thông qua lệnh AT Module Bluetooth HC 06 được cấu hình mặc định là chế độ slave, nghĩa là nó chỉ có khả năng giao tiếp với các thiết bị Bluetooth ở chế độ master.
*Chức năng từng chân của modul HC-06
+Chân UART_TXD có chức năng truyền dữ liệu tới RXD, nối với chân RXD của vi điều khiển.
+Chân UART_RXD có chức năng nhận dữ liệu do chân TXD truyền tới, nối với chân TXD của vi điều khiển.
+Chân VCC nối với nguồn từ 3-5V
Động cơ Servo SG -90
Servo là một loại động cơ điện đặc biệt, khác với động cơ thông thường, servo chỉ quay khi được điều khiển bằng xung PPM với góc quay từ 0 đến 180 độ Mỗi loại servo có kích thước và khối lượng khác nhau, từ những loại nhẹ chỉ 9g, thường sử dụng cho máy bay mô hình, đến những loại có momen lực mạnh mẽ lên tới vài chục Newton/m và các loại có thiết kế chắc chắn với nhông sắc.
Động cơ servo hoạt động trong hệ thống hồi tiếp vòng kín, với tín hiệu ra được kết nối đến mạch điều khiển Khi động cơ quay, tốc độ và vị trí sẽ được gửi trở lại mạch điều khiển Nếu có bất kỳ trở ngại nào ngăn cản chuyển động, cơ cấu hồi tiếp sẽ phát hiện tín hiệu ra chưa đạt yêu cầu Mạch điều khiển sẽ điều chỉnh sai lệch để động cơ đạt được vị trí chính xác Động cơ servo điều khiển qua sóng vô tuyến được gọi là động cơ servo RC, nhưng thực tế, nó không trực tiếp điều khiển bằng vô tuyến mà nhận tín hiệu từ máy thu vô tuyến trên máy bay hoặc xe hơi.
Tốc độ hoạt động: 60 độ trong 0.1 giây
Nhiệt độ hoạt động: 0 ºC – 55 ºC
Kết nối dây màu đỏ với nguồn 5V, dây màu nâu với mass, và dây màu cam với chân phát xung của vi điều khiển Tạo xung từ 1ms đến 2ms tại chân xung để điều chỉnh góc quay theo ý muốn.
III Phương thức giao tiếp UART
UART, hay Giao thức Truyền thông Không đồng bộ, là một phương pháp giao tiếp phần cứng sử dụng kết nối nối tiếp với tốc độ có thể điều chỉnh Đặc điểm nổi bật của UART là không sử dụng tín hiệu đồng hồ để đồng bộ hóa các bit dữ liệu giữa thiết bị phát và thiết bị nhận.
Trong giao tiếp UART, hai thiết bị UART tương tác trực tiếp bằng cách chuyển đổi dữ liệu từ dạng song song sang dạng nối tiếp Dữ liệu từ thiết bị điều khiển như CPU được gửi dưới dạng nối tiếp đến UART nhận, nơi nó được chuyển đổi trở lại thành dữ liệu song song cho thiết bị nhận.
• Hai đường dây mà mỗi thiết bị UART sử dụng để truyền dữ liệu đó là:
UART là giao thức truyền dữ liệu không đồng bộ, không sử dụng tín hiệu đồng hồ để đồng bộ hóa việc truyền và nhận bit Thay vào đó, UART thêm các bit start và stop vào gói dữ liệu, giúp xác định điểm bắt đầu và kết thúc của gói Điều này cho phép UART nhận biết thời điểm bắt đầu đọc các bit dữ liệu.
Khi UART phát hiện một bit start, nó bắt đầu đọc các bit tiếp theo với một tần số nhất định gọi là tốc độ truyền (baud rate) Tốc độ truyền, được đo bằng bit trên giây (bps), là chỉ số quan trọng cho việc truyền dữ liệu Để đảm bảo hoạt động hiệu quả, cả hai UART truyền và nhận cần phải sử dụng cùng một tốc độ truyền, với sự chênh lệch tối đa khoảng 10% để tránh sai lệch thời gian giữa các bit.
UART nhận dữ liệu từ bus dữ liệu, được cung cấp bởi các thiết bị như CPU, bộ nhớ hoặc vi điều khiển, dưới dạng song song Sau khi nhận, UART thêm một bit start, một bit chẵn lẻ và một bit stop để tạo thành gói dữ liệu Gói dữ liệu này sau đó được truyền ra dưới dạng nối tiếp từng bit qua chân Tx Tại chân Rx, UART đọc gói dữ liệu từng bit, chuyển đổi trở lại dạng song song và loại bỏ các bit start, chẵn lẻ và stop Cuối cùng, UART gửi gói dữ liệu song song trở lại bus dữ liệu qua đầu nhận.
Sơ đồ nối mạch và sơ đồ thuật toán
Sơ đồ thuật toán
14 a Sơ đồ điều khiển bằng winform b.Sơ đồ chạy tự động
Lập trình
Code Arduino
#define enB 11 int x=0; int spd=0;
{ pinMode(inA1, OUTPUT); pinMode(inA2, OUTPUT); pinMode(inB1, OUTPUT); pinMode(inB2, OUTPUT); pinMode(trig,OUTPUT); pinMode(echo,INPUT);
Serial.begin(9600); myservo.attach(5); myservo.write(90); delay(500);
Tranh_Vat_Can(40); break; case '1': spd0; break; case '2': spd5; break; case '3': spd0; break; case '4': spd 0; break; case '5': spd%5; break;
{ analogWrite(enA,spd-15); analogWrite(enB,spd-30); digitalWrite(inA1, HIGH); digitalWrite(inA2, LOW); digitalWrite(inB1, HIGH); digitalWrite(inB2, LOW);
{ analogWrite(enA,spd-15); analogWrite(enB,spd-30); digitalWrite(inA1, LOW); digitalWrite(inA2, HIGH); digitalWrite(inB1, LOW); digitalWrite(inB2, HIGH);
{ analogWrite(enA,spd-15); analogWrite(enB,spd-30); digitalWrite(inA2,LOW);
18 digitalWrite(inA1,HIGH); digitalWrite(inB1,LOW); digitalWrite(inB2,LOW);
{ analogWrite(enA,spd-15); analogWrite(enB,spd-30); digitalWrite(inA1,LOW); digitalWrite(inA2,LOW); digitalWrite(inB2,LOW); digitalWrite(inB1,HIGH);
{ digitalWrite(inA1, LOW); digitalWrite(inA2, LOW); digitalWrite(inB1, LOW); digitalWrite(inB2, LOW);
{ myservo.write(goc); delay(500); unsigned long times; int KC; digitalWrite(trig,0); delayMicroseconds(2); digitalWrite(trig,1); delayMicroseconds(5); digitalWrite(trig,0); times = pulseIn(echo,HIGH);
KC = int(times/2/29.412); return KC;
} void Tranh_Vat_Can(byte KC_choPhep) {
//Tien(); int KC_phiaTruoc = TinhKC(90); if (KC_phiaTruoc > KC_choPhep)
} else //(KC_phiaTruoc comboBox2->DataSource = tocdotruyen; comboBox2->SelectedIndex = 1; this->comboBox3->DataSource = speed; this->comboBox1->DataSource = oBarray; comboBox1->SelectedIndex = 1;*/
/// Clean up any resources being used.
The code snippet defines a user interface for a Windows Forms application, featuring multiple buttons and labels for interaction It includes eight buttons (button1 to button8) and five labels (label1 to label5) to display information Additionally, there are three combo boxes (comboBox1 to comboBox3) for user selections The application also utilizes a serial port (serialPort1) for communication, and it is structured to include a component container for managing its components effectively.
#pragma region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
This code snippet initializes a new component container and creates several user interface elements, including a button and a label, using the Windows Forms framework Specifically, it defines five components: a container, a button labeled "button1," a label labeled "label1," and three additional buttons labeled "button2," "button3," and "button4."
In this code snippet, several Windows Forms components are being initialized, including buttons (button5, button6, button7, button8), labels (label2, label3, label4, label5), and combo boxes (comboBox1, comboBox2, comboBox3) Additionally, a serial port (serialPort1) is created to facilitate communication with external devices The layout of the form is temporarily suspended with the `SuspendLayout()` method to optimize the rendering process.
The code snippet initializes a button control named "button1" in a Windows Forms application It sets the button's location to coordinates (468, 33) and defines its size as 75 by 30 pixels The button is assigned a tab index of 0, labeled "Auto," and configured to use visual styles Additionally, an event handler is attached to the button's click event, linking it to the method "button1_Click" in the "MyForm" class.
The label is configured to automatically adjust its size and is displayed in the menu highlight color Positioned at coordinates (111, 33), it is named "label1" with dimensions of 100 by 17 pixels It has a tab index of 1 and currently shows the text "Not connected."
In this code snippet, a button named "button2" is created and configured within a form It is positioned at coordinates (42, 350) and has a size of 90 by 28 pixels The button is assigned a tab index of 2 and displays the text "Connect." Additionally, it is set to use the visual style of the operating system and is linked to an event handler that triggers the "button2_Click" method when clicked.
// this->button3->Location = System::Drawing::Point(42, 392); this->button3->Name = L"button3"; this->button3->Size = System::Drawing::Size(90, 28); this->button3->TabIndex = 3; this->button3->Text = L"Disconnect"; this->button3->UseVisualStyleBackColor = true;
23 this->button3->Click += gcnew System::EventHandler(this, &MyForm::button3_Click); //
The button4 control is positioned at coordinates (270, 81) and is named "button4." It has a size of 60 by 55 pixels and is assigned a tab index of 4 The button displays the text "Down" and utilizes the visual style settings Additionally, it is configured to respond to mouse down events.
System::Windows::Forms::MouseEventHandler(this, &MyForm::button4_MouseDown); this->button4->MouseUp += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button4_MouseUp);
The code snippet defines a button in a graphical user interface with specific properties The button, named "button5," is positioned at coordinates (270, 233) and has a size of 60 by 55 pixels It is assigned a tab index of 5 and labeled "Up." The button is designed to use the visual styles of the operating system and is set up to respond to mouse down events.
System::Windows::Forms::MouseEventHandler(this, &MyForm::button5_MouseDown); this->button5->MouseUp += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button5_MouseUp);
The code snippet initializes a button in a graphical user interface with specific properties The button, named "button6," is positioned at coordinates (348, 156) and has a size of 60x55 pixels It is assigned a tab index of 6 and displays the text "Right." Additionally, the button is set to use the visual style of the application and is configured to respond to mouse down events.
System::Windows::Forms::MouseEventHandler(this, &MyForm::button6_MouseDown); this->button6->MouseUp += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button6_MouseUp);
In this code snippet, a button named "button7" is created with specific properties, including its location set to the coordinates (187, 156) and a size of 60 by 55 pixels The button is assigned a tab index of 7 and displays the text "Left." Additionally, it is configured to use the visual styles of the application, and a mouse down event handler is attached to it for further interaction.
System::Windows::Forms::MouseEventHandler(this, &MyForm::button7_MouseDown);
System::Windows::Forms::MouseEventHandler(this, &MyForm::button7_MouseUp);
The code snippet defines a button in a graphical user interface (GUI) with the name "button8," positioned at coordinates (270, 156) It has a size of 60 by 55 pixels and is assigned a tab index of 8 The button displays the text "Stop" and utilizes the visual style settings of the application Additionally, an event handler is attached to the button for the MouseDown event, allowing for interactive functionality.
System::Windows::Forms::MouseEventHandler(this, &MyForm::button8_MouseDown);
The label2 control is configured with auto-sizing enabled, a dark green foreground color, and is positioned at coordinates (39, 33) It is named "label2" and has a size of 48 by 17 pixels, with a tab index of 9 The text displayed on the label is "Status."
// this->label3->AutoSize = true; this->label3->Location = System::Drawing::Point(453, 350); this->label3->Name = L"label3"; this->label3->Size = System::Drawing::Size(39, 17); this->label3->TabIndex = 10; this->label3->Text = L"COM";
// this->label4->AutoSize = true; this->label4->Location = System::Drawing::Point(453, 392); this->label4->Name = L"label4"; this->label4->Size = System::Drawing::Size(70, 17); this->label4->TabIndex = 11; this->label4->Text = L"Baud rate";
// this->label5->AutoSize = true; this->label5->Location = System::Drawing::Point(453, 432); this->label5->Name = L"label5"; this->label5->Size = System::Drawing::Size(49, 17); this->label5->TabIndex = 12; this->label5->Text = L"Speed";
// this->comboBox1->FormattingEnabled = true; this->comboBox1->Location = System::Drawing::Point(234, 350); this->comboBox1->Name = L"comboBox1"; this->comboBox1->Size = System::Drawing::Size(130, 24); this->comboBox1->TabIndex = 13;
// this->comboBox2->FormattingEnabled = true; this->comboBox2->Location = System::Drawing::Point(234, 392); this->comboBox2->Name = L"comboBox2"; this->comboBox2->Size = System::Drawing::Size(130, 24); this->comboBox2->TabIndex = 14;
The code snippet initializes a ComboBox control in a Windows Forms application The ComboBox is enabled for formatting, positioned at coordinates (234, 432), and named "comboBox3." It has a size of 130 by 24 pixels and is assigned a tab index of 15 Additionally, an event handler is attached to the ComboBox to respond to changes in the selected value.
The MyForm class is designed with an AutoScaleDimensions of 8 by 16 and an AutoScaleMode set to Font, ensuring optimal display across different screen sizes The form's client area measures 588 by 482 pixels, incorporating various controls including three combo boxes, multiple labels, and several buttons The form is named "MyForm" and is initialized with a text title of "MyForm." Additionally, it includes an event handler for the Load event, allowing for custom loading behavior The layout is managed effectively through resume and perform layout methods to ensure a coherent user interface.
#pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { this->serialPort1->Write("f");
MessageBox::Show("Ban da cho che do tu dong!");
} private: System::Void button4_MouseDown(System::Object^ sender,
} private: System::Void button8_MouseDown(System::Object^ sender,
} private: System::Void button5_MouseDown(System::Object^ sender,
} private: System::Void button6_MouseDown(System::Object^ sender,
} private: System::Void button7_MouseDown(System::Object^ sender,