Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

lOMoARcPSD|10256393

báo cáo kĩ thuật lập trình cơ điện tử

Kĩ thuật lập trình (Trường Đại học Bách khoa Hà


Nội)
StuDocu is not sponsored or endorsed by any college or university
Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)
lOMoARcPSD|10256393

TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI


VIỆN CƠ KHÍ
**********************

BÁO CÁO BÀI TẬP LỚN MÔN KĨ THUẬT LẬP TRÌNH

Đề tài: Xe mô hình tránh vật cản điều khiển bằng Bluetooth

Giảng viên hướng dẫn: TS. Trương Công Tuấn

Lớp: Cơ điện tử

Nhóm sinh viên thực hiện:

Hà Nội 8-2021

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

Mục lục

Tóm tắt công trình....................................................................................................................... 3


I. Đặt vấn đề:............................................................................................................................... 4
II. Các linh kiện cụ thể................................................................................................................4
1. Arduino UNO R3................................................................................................................4
2. Module điều khiển động cơ L298 mạch cầu H....................................................................5
3. Cảm biến khoảng cách HC-SR04 :.....................................................................................8
4. Bluetooth HC-06:................................................................................................................9
5. Động cơ Servo SG -90:.....................................................................................................11
IV. Sơ đồ nối mạch và sơ đồ thuật toán:....................................................................................14
1. Sơ đồ nối mạch..................................................................................................................14
2. Sơ đồ thuật toán.................................................................................................................15
a. Sơ đồ điều khiển bằng winform.........................................................................................15
b.Sơ đồ chạy tự động............................................................................................................. 16
V. Lập trình............................................................................................................................... 16
1. Code Arduino:................................................................................................................... 16
2. Code Visual Studio giao diện điều khiển:.........................................................................21
VI. Tài liệu tham khảo:..............................................................................................................30

Tóm tắt công trình


1. Sản phẩm: Xe mô hình tránh vật cản điều khiển bằng Bluetooth
2. Chức năng:
Xe tránh vật cản điều khiển bằng 2 chế độ thông qua window form và modul bluetooth
HC-06:
• Tự động
• Tự điều khiển bằng nút bấm trên window form
3. Linh kiện cấu tạo :
• Giá đỡ kích thước 70*85mm
• Hai bánh xe đường kính 35mm gắn vào 2 trục động cơ khác nhau
• Một bánh xe đa hướng gắn ở đầu xe
• 2 động cơ DC một chiều
• Nguồn pin 12V
2

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

• Arduino UNO
• Module điều khiển động cơ L298 mạch cầu
• Cảm biến khoảng các HC-SR04
• Động cơ SERVO
• Bluetooth HC-06
I. Đặt vấn đề:
- Các loại máy móc thuần cơ khí ngày nay đã trở nên lỗi thời, việc sử dụng chúng
trong sản xuất mất nhiều công sức và thời gian của người sử dụng. Do đó, sự kết hợp
giữa các hệ thống cơ điện với điện tử và lập trình tạo ra những đột phá và công dụng vô
cùng to lớn. Để chuẩn bị cho thời đại công nghiệp hiện nay, ngay từ khi ngồi trên giảng
đường, sinh viên nên học và thực hành với những sản phẩm mang sự kết hợp như vậy.
- Mục đích chính của đề tài là áp dụng các kiến thức đã học từ môn học Kĩ thuật Lập
trình trong Cơ Điện Tử vào việc thiết kế 1 chiếc xe tự hành có khả năng tự động di
chuyển tránh vật cản và điều khiển được qua Bluetooth.
- Robot có nhiệm vụ di chuyển theo bảng điều khiển trên máy tính đã được lập trình
sẵn thông qua Bluetooth có thể tiến, lùi, quay trái, quay phải, hoặc có thể tự nhận biết
vật cản, khoảng cách và tránh vật cản đó.

II. Các linh kiện cụ thể

1. 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.

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

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
32 KB (ATmega328) với 0.5KB dùng bởi
Bộ nhớ flash
bootloader
SRAM 2 KB (ATmega328)
EEPROM 1 KB (ATmega328)

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

2. Module điều khiển động cơ L298 mạch cầu H

Thông số kỹ thuật:

• Driver: L298N tích hợp hai mạch cầu H.


• Điện áp điều khiển: +5 V ~ +12 V
• 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 ℃)  Nhiệt độ bảo quản: -25 ℃ ~
+130 ℃  L298 gồm các chân:
• 12V power, 5V power. Đây là 2 chân cấp nguồn trực tiếp đến động cơ.  Có thể
cấp nguồn 9-12V ở 12V.

*Nguyên lí hoạt động:


- Chúng ta có thể điều khiển tốc độ của động cơ DC bằng cách điều khiển điện áp
đầu vào của động cơ và phương pháp phổ biến nhất để làm điều đó là sử dụng tín hiệu
PWM.
- PWM hay thay đổi độ rộng xung là một kỹ thuật cho phép chúng ta điều chỉnh giá
trị trung bình của điện áp đến thiết bị điện tử bằng cách bật và tắt nguồn với tốc độ
nhanh. Điện áp trung bình phụ thuộc vào chu kỳ xung, hoặc lượng thời gian tín hiệu
BẬT so với lượng thời gian tín hiệu TẮT trong một khoảng thời gian quy định.

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

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 với chân cua điện trở hoặc chân của MOSFET và điều khiển tốc độ của động
cơ bằng cách điều khiển đầu ra PWM. Tín hiệu Arduino công suất thấp bật và tắt chân
tại MOSFET thông qua đó động cơ công suất cao được điều khiển.

Điều khiển động cơ DC bằng mạch cầu H


Mặt khác, để điều khiển hướng quay, chúng ta chỉ cần đảo ngược hướng của dòng điện
qua động cơ, và phương pháp phổ biến nhất để làm điều đó là sử dụng mạch cầu H. Một
mạch cầu H chứa bốn chân chuyển mạch, điện trở hoặc MOSFET, với động cơ ở trung
tâm tạo thành một cấu hình giống như chữ H. Bằng cách kích hoạt hai công tắc cụ thể
cùng một lúc, chúng ta có thể thay đổi hướng của dòng điện, do đó thay đổi hướng quay
của động cơ.

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

3. Cảm biến khoảng cách HC-SR04 :

Cảm biến siêu âm có chức năng để đo khoảng cách bằng cách áp dụng công thức tính
toán khoảng cách dựa trên thời gian và vận tốc của sóng cao tần do nó phát ra.

*Thông số kĩ thuật:

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

• Điện áp: 5V DC
• Dòng hoạt động: < 2mA
• Mức cao: 5V
• Mức thấp: 0V
• Góc tối đa: 15 độ
• Khoảng cách: 2cm – 450cm (4.5m)
• Độ chính xác: 3mm
*Các chân chức năng:

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 OUT
Không sử dụng

*Nguyên lí hoạt động:

Để đo khoảng cách, ta sẽ phát 1 xung rất ngắn (5 microSeconds) từ chân Trig. Sau
đó, cảm biến siêu âm sẽ tạo ra 1 xung HIGH ở chân Echo cho đến khi nhận lại được
sóng phản xạ ở pin này. Chiều rộng của xung sẽ bằng với thời gian sóng siêu âm
được phát từ cảm biển và quay trở lại.

Tốc độ của âm thanh trong không khí là 340 m/s (hằng số vật lý), tương đương với
29,412 microSeconds/cm (106 / (340*100)). Khi đã tính được thời gian, ta sẽ chia cho
29,412 để nhận được khoảng cách.

4. 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:
smartphone, laptop, usb bluetooth... thông qua giao tiếp Serial gửi và nhận tín hiệu

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

2 chiều. Module bluetooth được tích hợp trên board cho phép bạn sử dụng nguồn từ
DC 3.5 - 5V.
- 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: smartphone, laptop, usb bluetooth... thông qua giao tiếp Serial gửi và nhận
tín hiệu 2 chiều.
- Module bluetooth được tích hợp trên board cho phép bạn sử dụng nguồn từ
3.5V đến 6V cung cấp cho board mà không cần lo lắng về chênh lệch điện áp 3V -
5V gây hỏng board.
- Module Bluetooth gồm 6 chân theo thứ tự: KEY, VCC, GND, TX, RX,
STATE.Đây là module bluetooth SLAVE nghĩa là bạn không thể chủ động kết nối
bằng vi điều khiển, mà cần sử dụng smartphone, laptop, bluetooth usb... để dò tín
hiệu và kết nối (pair) từ smartphone, laptop, bluetooth usb... Sau khi kết nối thành
công, bạn có thể gửi và nhận tín hiệu từ vi điều khiển đến 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.

Thông số kỹ thuật:
• Điện áp hoạt động: 3.3 ~ 5VDC.
• Điện áp giao tiếp: TTL tương thích 3.3VDC và 5VDC.
• Chip CSR Bluetooth V 2 .0
• Baudrate UART( tốc độ hỗ trợ) có thể chọn được: 1200, 2400, 4800, 9600, 19200,
38400, 57600, 115200
9

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

• Dải tần sóng hoạt động: Bluetooth 2.4GHz


• Phạm vi hoạt động 8m ( hiệu quả 5m)
• Dòng hoạt động : 20-30mA  Tốc độ :
+Asynchronous : 2.1Mbps/160kbps
+Synchronous : 1Mbps/1Mbps.
• Kích thước của module chính: 28 mm x 15 mm x 2.35 mm
• Nhiệt độ làm việc: 20-750C
• Độ nhạy: -80dBm 2.1
• 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 ta có thể gửi hoặc nhận dữ liệu giữa hai thiết bị module sử
dụng chuẩn giao tiếp UART giúp dễ dàng giao tiếp với VDK tốc độ baud tên
module mật khẩu được cài đặt bởi tập lệnh AT. Module Bluetooth HC 06 được
cài đặt mặc định là slave nên chỉ có thể giao tiếp với các thiết bị Bluetooth ở dạng
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
+Chân GND nối đất

5. Động cơ Servo SG -90:


-Servo là một dạng động cơ điện đặc biệt. Không giống như động cơ thông thường cứ
cắm điện vào là quay liên tục, servo chỉ quay khi được điều khiển (bằng xung PPM) với
góc quay nằm trong khoảng bất kì từ 00 – 1800. Mỗi loại servo có kích thước, khối lượng
và cấu tạo khác nhau. Có loại thì nặng chỉ 9g (chủ yếu dùng trên máy bay mô mình), có
loại thì sở hữu một momen lực bá đạo (vài chục Newton/m), hoặc có loại thì khỏe và
nhông sắc chắc chắn,...
- Động cơ servo được thiết kế những hệ thống hồi tiếp vòng kín. Tín hiệu ra của động cơ
được nối với một mạch điều khiển. Khi động cơ quay, vận tốc và vị trí sẽ được hồi tiếp
về mạch điều khiển này. Nếu có bầt kỳ lý do nào ngăn cản chuyển động quay của động
cơ, cơ cấu hồi tiếp sẽ nhận thấy tín hiệu ra chưa đạt được vị trí mong muốn. Mạch điều
khiển tiếp tục chỉnh sai lệch cho động cơ đạt được điểm chính xác. Các động cơ servo
điều khiển bằng liên lạc vô tuyến được gọi là động cơ servo RC (radio-controlled).
Trong thực tế, bản thân động cơ servo không phải được điều khiển bằng vô tuyến, nó
chỉ nối với máy thu vô tuyến trên máy bay hay xe hơi. Động cơ servo nhận tín hiệu từ
máy thu này.
10

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

*Thông số kĩ thuật:
• Khối lượng : 9g
• Kích thước: 22.2x11.8.32 mm
• Momen xoắn: 1.8kg/cm
• Tốc độ hoạt động: 60 độ trong 0.1 giây
• Điện áp hoạt động: 4.8V(~5V)
• Nhiệt độ hoạt động: 0 ºC – 55 ºC
• Kết nối dây màu đỏ với 5V, dây màu nâu với mass, dây màu cam với chân phát
xung của vi điều khiển. Ở chân xung cấp một xung từ 1ms-2ms theo để điều khiển
góc quay theo ý muốn.

III. Phương thức giao tiếp UART


Chuẩn giao tiếp UART:
• Theo định nghĩa, UART là một giao thức truyền thông phần cứng sử dụng giao
tiếp nối tiếp không đồng bộ với tốc độ có thể định cấu hình. Không đồng bộ có
nghĩa là không có tín hiệu đồng hồ để đồng bộ hóa các bit đầu ra từ thiết bị truyền
đi đến bên nhận.
• Trong giao tiếp UART, hai UART giao tiếp trực tiếp với nhau. UART truyền
chuyển đổi dữ liệu song song từ một thiết bị điều khiển như CPU thành dạng nối
tiếp, truyền nó nối tiếp đến UART nhận, sau đó chuyển đổi dữ liệu nối tiếp 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à:
+ Transmitter (Tx)
+ Receiver (Rx)

11

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

• UART truyền dữ liệu không đồng bộ, có nghĩa là không có tín hiệu đồng hồ để
đồng bộ hóa đầu ra của các bit từ UART truyền đến việc lấy mẫu các bit bởi
UART nhận. Thay vì tín hiệu đồng hồ, UART truyền thêm các bit start và stop
vào gói dữ liệu được chuyển. Các bit này xác định điểm bắt đầu và điểm kết thúc
của gói dữ liệu để UART nhận biết khi nào bắt đầu đọc các bit.
• Khi UART nhận phát hiện một bit start, nó bắt đầu đọc các bit đến ở một tần số cụ
thể được gọi là tốc độ truyền (baud rate). Tốc độ truyền là thước đo tốc độ truyền
dữ liệu, được biểu thị bằng bit trên giây (bps – bit per second). Cả hai UART đều
phải hoạt động ở cùng một tốc độ truyền. Tốc độ truyền giữa UART truyền và
nhận chỉ có thể chênh lệch khoảng 10% trước khi thời gian của các bit bị lệch quá
xa.
• UART sẽ truyền dữ liệu nhận được từ một bus dữ liệu (Data Bus). Bus dữ liệu
được sử dụng để gửi dữ liệu đến UART bởi một thiết bị khác như CPU, bộ nhớ
hoặc vi điều khiển. Dữ liệu được chuyển từ bus dữ liệu đến UART truyền ở dạng
song song. Sau khi UART truyền nhận dữ liệu song song từ bus dữ liệu, nó sẽ
thêm một bit start, một bit chẵn lẻ và một bit stop, tạo ra gói dữ liệu. Tiếp theo,
gói dữ liệu được xuất ra nối tiếp từng bit tại chân Tx. UART nhận đọc gói dữ liệu
từng bit tại chân Rx của nó. UART nhận sau đó chuyển đổi dữ liệu trở lại dạng
song song và loại bỏ bit start, bit chẵn lẻ và bit stop. Cuối cùng, UART nhận
chuyển gói dữ liệu song song với bus dữ liệu ở đầu nhận.

12

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

IV. Sơ đồ nối mạch và sơ đồ thuật toán:

1. Sơ đồ nối mạch

13

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

2. Sơ đồ thuật toán
a. Sơ đồ điều khiển bằng winform

14

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

b.Sơ đồ chạy tự động

V. Lập trình

1. Code
Arduino:

#include <Servo.h>
#define trig 3
#define echo 2

Servo myservo;
#define enA 6
#define inA1 7
#define inA2 8
#define inB1 9

15

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

#define inB2 10
#define enB 11
int x=0; int
spd=0;
void setup()
{ 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); }

void loop()
{
//Tranh_Vat_Can(40);

bluetoothRobot(); }

void bluetoothRobot()
{
if (Serial.available() > 0)
{ x = Serial.read(); }
switch (x) { case 'a':
Tien(); break; case
'b': Lui(); break;
case 'd': Retrai();
break; case 'c':
Rephai(); break;
case 'e': Dung();
break;
case 'f':

Tranh_Vat_Can(40);
break; case '1':
spd=100; break;
case '2': spd=125;
16

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

break; case '3':


spd=150; break;
case '4': spd=200;
break; case '5':
spd=255; break; }
}

void Tien()
{ analogWrite(enA,spd-
15); analogWrite(enB,spd-
30); digitalWrite(inA1,
HIGH);
digitalWrite(inA2, LOW);
digitalWrite(inB1, HIGH);
digitalWrite(inB2, LOW); }
void Lui()
{ analogWrite(enA,spd-
15); analogWrite(enB,spd-
30); digitalWrite(inA1,
LOW); digitalWrite(inA2,
HIGH); digitalWrite(inB1,
LOW); digitalWrite(inB2,
HIGH); }
void Retrai()
{ analogWrite(enA,spd-
15);
analogWrite(enB,spd-30);
digitalWrite(inA2,LOW);
digitalWrite(inA1,HIGH);
digitalWrite(inB1,LOW);
digitalWrite(inB2,LOW); }
void Rephai()
{ analogWrite(enA,spd-
15);
analogWrite(enB,spd-30);
digitalWrite(inA1,LOW);
digitalWrite(inA2,LOW);
digitalWrite(inB2,LOW);
digitalWrite(inB1,HIGH);
}

void Dung()
{ digitalWrite(inA1,
17

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

LOW);
digitalWrite(inA2, LOW);
digitalWrite(inB1, LOW);
digitalWrite(inB2,
LOW); }

int TinhKC (byte goc)


{ 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)
{ Tien(); delay(100);
}
else //(KC_phiaTruoc <= KC_choPhep)
{ Lui(); delay(200);
Dung(); int KC_Trai =
TinhKC(180); delay (300);
int KC_Phai = TinhKC(0);
delay (300); int KC_max =
max(KC_Trai,KC_Phai);
if(KC_max <=KC_choPhep)
{
Lui(); delay(500);
Dung(); int KC_Trai1 =
TinhKC(180); delay (200);
int KC_Phai1 = TinhKC(0);
delay (200);
if(KC_Trai1>=KC_Phai1)
18

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

{ Retrai();
delay(80);
} else
{
Rephai();
delay(90);
}
//Retrai();
//delay(100);
} else
{
if (KC_max == KC_Trai)
{
Retrai();

delay (150);
}
else
{
Rephai();

delay (180);
}
}
}
}
2. Code Visual Studio giao diện điều
khiển:
#pragma once namespace ProjectWinform1 {

using namespace System; using namespace


System::ComponentModel; using
namespace System::Collections; using
namespace System::Windows::Forms; using
namespace System::Data; using namespace
System::Drawing; using namespace
System::IO::Ports; using namespace
System::Text;

/// Summary for


MyForm /// </summary>
public ref class MyForm : public System::Windows::Forms::Form
19

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

{
public:
array<String^>^ tocdotruyen;
array<String^>^ speed;
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
tocdotruyen = gcnew array<String^>{"4800", "9600", "19200"};
speed = gcnew array<String^>{"100", "125", "150", "200",
"255"}; /*array<Object^>^ oBarray = SerialPort::GetPortNames();
this->comboBox2->DataSource = tocdotruyen; comboBox2-
>SelectedIndex = 1; this->comboBox3->DataSource = speed; this-
>comboBox1->DataSource = oBarray; comboBox1-
>SelectedIndex = 1;*/

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{ if (components)
{ delete components;
}
} private: System::Windows::Forms::Button^
button1; private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ button2; private:
System::Windows::Forms::Button^ button3; private:
System::Windows::Forms::Button^ button4; private:
System::Windows::Forms::Button^ button5; private:
System::Windows::Forms::Button^ button6; private:
System::Windows::Forms::Button^ button7; private:
System::Windows::Forms::Button^ button8; private:
System::Windows::Forms::Label^ label2; private:
System::Windows::Forms::Label^ label3; private:
System::Windows::Forms::Label^ label4; private:
System::Windows::Forms::Label^ label5; private:
System::Windows::Forms::ComboBox^ comboBox1;
private: System::Windows::Forms::ComboBox^
comboBox2; private:
System::Windows::Forms::ComboBox^ comboBox3;

private:

public: System::IO::Ports::SerialPort^ serialPort1;


private:
20

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

private: System::ComponentModel::IContainer^ components;


protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>

#pragma region Windows Form Designer generated code


/// <summary>
/// Required method for Designer support - do not
modify /// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{ this->components = (gcnew System::ComponentModel::Container()); this-
>button1 = (gcnew System::Windows::Forms::Button()); this->label1
= (gcnew System::Windows::Forms::Label()); this->button2 =
(gcnew System::Windows::Forms::Button()); this->button3 = (gcnew
System::Windows::Forms::Button()); this->button4 = (gcnew
System::Windows::Forms::Button());
this->button5 = (gcnew System::Windows::Forms::Button()); this->button6 =
(gcnew System::Windows::Forms::Button()); this->button7 = (gcnew
System::Windows::Forms::Button()); this->button8 = (gcnew
System::Windows::Forms::Button()); this->label2 = (gcnew
System::Windows::Forms::Label()); this->label3 = (gcnew
System::Windows::Forms::Label()); this->label4 = (gcnew
System::Windows::Forms::Label()); this->label5 = (gcnew
System::Windows::Forms::Label()); this->comboBox1 = (gcnew
System::Windows::Forms::ComboBox()); this->comboBox2 = (gcnew
System::Windows::Forms::ComboBox()); this->comboBox3 = (gcnew
System::Windows::Forms::ComboBox()); this->serialPort1 = (gcnew
System::IO::Ports::SerialPort(this->components)); this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(468, 33); this-
>button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 30);
this->button1->TabIndex = 0; this->button1->Text =
L"Auto"; this->button1->UseVisualStyleBackColor =
true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->ForeColor = System::Drawing::SystemColors::MenuHighlight;
this->label1->Location = System::Drawing::Point(111, 33); this->label1-
>Name = L"label1";
21

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

this->label1->Size = System::Drawing::Size(100, 17);


this->label1->TabIndex = 1; this->label1->Text =
L"Not connected";
//
// button2
//
this->button2->Location = System::Drawing::Point(42, 350); this-
>button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(90, 28);
this->button2->TabIndex = 2; this->button2->Text =
L"Connect"; this->button2-
>UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
//
// button3
//
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; this->button3-
>Click += gcnew System::EventHandler(this,
&MyForm::button3_Click);
//
// button4
//
this->button4->Location = System::Drawing::Point(270, 81); this-
>button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(60, 55);
this->button4->TabIndex = 4; this->button4->Text =
L"Down";
this->button4->UseVisualStyleBackColor = true; this->button4-
>MouseDown += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button4_MouseDown);
this->button4->MouseUp += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button4_MouseUp);
//
// button5
//
this->button5->Location = System::Drawing::Point(270, 233); this-
>button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(60, 55);
this->button5->TabIndex = 5; this->button5->Text =
L"Up";
this->button5->UseVisualStyleBackColor = true; this->button5-
>MouseDown += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button5_MouseDown);
this->button5->MouseUp += gcnew

22

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

System::Windows::Forms::MouseEventHandler(this, &MyForm::button5_MouseUp);
//
// button6
//
this->button6->Location = System::Drawing::Point(348, 156); this-
>button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(60, 55);
this->button6->TabIndex = 6; this->button6->Text =
L"Right";
this->button6->UseVisualStyleBackColor = true; this->button6-
>MouseDown += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button6_MouseDown);
this->button6->MouseUp += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button6_MouseUp);
//
// button7
//
this->button7->Location = System::Drawing::Point(187, 156); this-
>button7->Name = L"button7";
this->button7->Size = System::Drawing::Size(60, 55);
this->button7->TabIndex = 7; this->button7->Text =
L"Left";
this->button7->UseVisualStyleBackColor = true; this->button7-
>MouseDown += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button7_MouseDown);
this->button7->MouseUp += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button7_MouseUp);
//
// button8
//
this->button8->Location = System::Drawing::Point(270, 156); this-
>button8->Name = L"button8";
this->button8->Size = System::Drawing::Size(60, 55);
this->button8->TabIndex = 8; this->button8->Text =
L"Stop"; this->button8->UseVisualStyleBackColor =
true; this->button8->MouseDown += gcnew
System::Windows::Forms::MouseEventHandler(this, &MyForm::button8_MouseDown);
//
// label2
//
this->label2->AutoSize = true; this->label2->ForeColor =
System::Drawing::Color::DarkGreen; this->label2->Location =
System::Drawing::Point(39, 33); this->label2->Name =
L"label2";
this->label2->Size = System::Drawing::Size(48, 17); this->label2-
>TabIndex = 9;
this->label2->Text = L"Status";
//
// label3

23

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

//
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";
//
// label4
//
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";
//
// label5
//
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";
//
// comboBox1
//
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;
//
// comboBox2
//
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;
//
// comboBox3
//
this->comboBox3->FormattingEnabled = true;
this->comboBox3->Location = System::Drawing::Point(234, 432);
this->comboBox3->Name = L"comboBox3"; this->comboBox3-
>Size = System::Drawing::Size(130, 24); this->comboBox3-
>TabIndex = 15;

24

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

this->comboBox3->SelectedValueChanged += gcnew System::EventHandler(this,


&MyForm::comboBox3_SelectedValueChanged);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); this-
>AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(588, 482);
this->Controls->Add(this->comboBox3);
this->Controls->Add(this->comboBox2);
this->Controls->Add(this->comboBox1);
this->Controls->Add(this->label5); this-
>Controls->Add(this->label4); this-
>Controls->Add(this->label3); this-
>Controls->Add(this->label2); this-
>Controls->Add(this->button8); this-
>Controls->Add(this->button7); this-
>Controls->Add(this->button6); this-
>Controls->Add(this->button5); this-
>Controls->Add(this->button4); this-
>Controls->Add(this->button3); this-
>Controls->Add(this->button2); this-
>Controls->Add(this->label1); this-
>Controls->Add(this->button1); this-
>Name = L"MyForm"; this->Text =
L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->ResumeLayout(false); this->PerformLayout();
}
#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,
System::Windows::Forms::MouseEventArgs^ e)
{ this->serialPort1->Write("a");
} private: System::Void button8_MouseDown(System::Object^
sender,
System::Windows::Forms::MouseEventArgs^ e)
{ this->serialPort1->Write("e");
}
private: System::Void button5_MouseDown(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e)
{ this->serialPort1->Write("b");
}
private: System::Void button6_MouseDown(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e)
{ this->serialPort1->Write("c");
25

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

}
private: System::Void button7_MouseDown(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e)
{ this->serialPort1->Write("d");
}
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e)
{ array<Object^>^ oBarray = SerialPort::GetPortNames(); this-
>comboBox2->DataSource = tocdotruyen; this-
>comboBox2->SelectedIndex = 1; this->comboBox3-
>DataSource = speed; this->comboBox1->DataSource
= oBarray; this->comboBox1->SelectedIndex = 0;
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{

label1->Text = "Connected"; label1-


>ForeColor = Color::Coral;
this->serialPort1->BaudRate = System::Convert::ToInt32(this->comboBox2->Text-
>ToString()); this->serialPort1->PortName = this->comboBox1-
>Text; this->serialPort1->Open();
//serialPort1->Write(this->comboBox3->Text->ToString()); comboBox1-
>Enabled = false;
comboBox2->Enabled = false; comboBox3-
>Enabled = false;

}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{ label1->Text = "Disconnected";
label1->ForeColor =
Color::Red; serialPort1-
>Close(); comboBox1->Enabled
= true; comboBox2->Enabled =
true; comboBox3->Enabled =
true;

}
private: System::Void button4_MouseUp(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e)
{ serialPort1->Write("e");
}
private: System::Void button6_MouseUp(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e)
{ serialPort1->Write("e");
}
private: System::Void button5_MouseUp(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e)
{ serialPort1->Write("e");
}

26

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

private: System::Void button7_MouseUp(System::Object^ sender,


System::Windows::Forms::MouseEventArgs^ e)
{ serialPort1->Write("e");
}
private: System::Void comboBox3_SelectedValueChanged(System::Object^ sender,
System::EventArgs^ e)
{ this->serialPort1->BaudRate = System::Convert::ToInt32(this->comboBox2->Text-
>ToString()); this->serialPort1->PortName = this->comboBox1-
>Text;
this->serialPort1->Open(); if (this-
>comboBox3->Text == "100")
{ serialPort1->Write("1");
}
else if (this->comboBox3->Text == "125")
{ serialPort1->Write("2");
}
else if (this->comboBox3->Text == "150")
{ serialPort1->Write("3");
}
else if (this->comboBox3->Text == "200")
{ serialPort1->Write("4");
}
else if (this->comboBox3->Text == "255")
{ serialPort1->Write("5");
}
serialPort1->Close();
}
};
}

27

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)


lOMoARcPSD|10256393

V. Kết quả thu được và kết luận

Kết luận :

Hướng của đề tài :


Ứng dụng công nghệ Bluetooth vào các hệ thống, điều khiển tự động khác thông
qua điều khiển bằng giao diện trên My form.

Tích cực :
- Hiểu được nguyên lý, cách thức hoạt động của cảm biến, linh kiện điện tử.
- Biết cách xây dựng, thiết kế xe tự hành, điều khiển xe thông qua Bluetooth.
- Sử dụng kiến thức đã học để lập trình điều khiển, xây dựng giao diện My form.
- Nâng cao khả năng hoạt động nhóm, biết phân chia công việc, hoạt động hiệu quả
hơn.

Hạn chế :
- Không thể gặp mặt nhau trực tiếp để cùng nhau lắp đặt , kiểm tra và vận hành.
- Khó khăn trong việc giao tiếp, cách diễn đạt ý để cho các thành viên khác hiểu,
các sự cố kỹ thuật, mạng.
- Khó khăn khi mua các linh kiện.

Quá trình thực hiện đề tài đã giúp chúng em thu được kết quả tốt, bổ sung nhiều phần
kiến thức chưa hiểu rõ trong quá trình học tập và hiểu rõ quá trình thiết kế, thực hiện 1 ý
tưởng kỹ thuật đơn giản. Môn học đã cho sinh viên chúng em môi trường làm việc thân
thiện, sáng tạo, kích thích sự tìm tòi. Chúng em xin gửi lời cảm ơn tới TS Trương Công
Tuấn đã tận tình hướng dẫn, góp ý chúng em trong quá trình thực hiện đề tài.

VI. Tài liệu tham khảo:


1. https://arduino.vn/
2. https://stackoverflow.com/

28

Downloaded by Nam Ph??ng (valeriymb8@hotmail.com)

You might also like