KẾT QUẢ THỰC NGHIỆM

Một phần của tài liệu Nghiên cứu thiết kế và chế tạo thiết bị đo và giám sát nồng độ bụi thông minh có tích hợp truyền thông không dây (Trang 63 - 99)

Mạch thu phát tín hiệu đo nồng độ bụi đƣợc đƣa ra ở hình 3.23 và 3.24.

Hình 3.24. Mạch Slave (mạch thu tín hiệu)

Các khối này đƣợc kết nối với máy tính đo nồng độ bụi nhƣ hình 2.35.

Hình 3.25. Mạch Master và Slave trong mô hình chạy thực nghiệm Kết quả nồng độ bụi đƣợc đo hiển thị lên giao diện máy tính nhƣ hình 3.36.

CHƢƠNG 4

KẾT LUẬN VÀ HƢỚNG PHÁT TRIỂN CỦA ĐỀ TÀI

- Qua quá trình thực hiện luận văn học viên rút ra các kết luận sau:

- Nắm đƣợc các phƣơng pháp đo nồng độ bụi khác nhau nhƣ: phƣơng pháp thu gom bụi và cân bụi; phƣơng pháp hấp thụ tia β; phƣơng pháp tán xạ ánh sáng; phƣơng pháp trọng lực (gravimetric methods), sau đó đã lựa chọn đƣợc cảm biến đo phù hợp với điều kiện ở nƣớc ta là: GP2Y1010AU0F

- Thiết kế đƣợc mạch đo nồng độ bụi dùng vi điều khiển ATMega8. - Thiết kế đƣợc truyền thông không dây dùng bộ: RF24L01.

- Xây dựng phần mềm cho Vi điều khiển dùng ngôn ngữ: CodeVisionAVR - Viết đƣợc phần mềm trên máy tính giám sát dùng Visual Studio 2008. - Hƣớng phát triển của đề tài:

+ Hoàn thiện các thiết kế

+ Viết các thuật giải thông minh cho thiết bị (nhƣ khả năng tự động thay đổi thang đo, khả năng lọc nhiễu thích nghi...)

TÀI LIỆU THAM KHẢO

[1] Quy chuẩn kỹ thuật quốc gia về chất lƣợng không khí xung quanh QCVN05: 2009/BTNMT.

[2] Bộ Tài nguyên môi trƣờng, Thông tƣ quy định quy chuẩn kỹ thuật quốc gia về môi trƣờng, 07/06/2009.

[3] Liang H., Wang F. „‟An Improved Back Scattering Photoelectric Dust Sensor‟‟, Photonics and Optoelectronics (SOPO), 2011 Symposium on, pp.14, Wuhan, China, 2011.

[4] Budde, M.; TECO/Pervasive Comput. Syst., Karlsruhe Inst. Of Technol., Karlsruhe, Germany; Busse, M.; Beigl, M. „‟Investigating the use of commodity dust sensors for the embedded measurement of particulate matter‟‟, Networked Sensing Systems (INSS), 2012 Ninth International Conference on, Antwerp, Germany 2012.

[5] Trần Văn Mạnh, Trần Quốc Tuấn, Hoàng Đức Việt, Vũ Bá Hậu, Đỗ Văn Công, Nguyễn Chung, Trần Anh Tuấn, Bùi Đăng Thảnh.Tạp trí tự động hóa ngày nay (Tự động hóa ngày nay, số 155+156|tháng 1+2/2014), Thiết kế và chế tạo thiết bị giám sát nồng độ bụi không dây, Viện Kỹ thuật điều khiển và Tự động hóa – Trƣờng ĐHBK Hà Nội.

PHỤ LỤC C: MÃ CHƢƠNG TRÌNH LẬP TRÌNH GIAO DIỆN: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //Thu vien giao tiep Serial Port using System.IO;

using System.IO.Ports; using System.Xml;

// Khai bao thu vien ve do thi using ZedGraph;

//Khai bao thu vien am thanh using System.Media; namespace Giao_tiep_UART { publicpartialclassForm1 :Form { int i; string s;

SerialPort P = newSerialPort(); // Khai bao 1 Object Serial Port

delegatevoidSetTextCallback(string text); //Khai bao delegate SetTextCallBack voi tham so tring double Nong_do = 0; string MyFilename = ""; public Form1() { InitializeComponent();

// Cai dat cac thong so cho COM

// Mang string port de chua tat ca cac cong com tren may tinh string[] ports = SerialPort.GetPortNames();

// Them toan bo cac COM da tim duoc vao combox cbCOM cbCom.Items.AddRange(ports);

// Su dung AddRange thay vi dung foreach P.ReadTimeout = 1000;

// Khai bao ham delegate bang phuong thuc DataReceived cua Object SerialPort; // Cai nay khi co su kien nhan du lieu se nhay den phuong thuc DataReceive P.DataReceived += newSerialDataReceivedEventHandler(DataReceive); // Cai dat cho BaudRate

string[] BaudRate = { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" };

cbRate.Items.AddRange(BaudRate);

// Cai dat cho DataBits

string[] Databits = { "6", "7", "8" }; cbBits.Items.AddRange(Databits);

string[] Parity = { "None", "Odd", "Even" }; cbParity.Items.AddRange(Parity);

// Cai Stop bit

string[] stopbit = { "1", "1.5", "2" }; cbBit.Items.AddRange(stopbit); }

// Goi su kien nhan du lieu

privatevoid DataReceive(object obj, SerialDataReceivedEventArgs e) {

InputData = P.ReadExisting(); if (InputData != String.Empty)

{

// txtIn.Text = InputData; // Ko dùng đc nhƣ thế này vì khác threads .

SetText(InputData); //Chinh vi vay phai su dung uy quyen tai day.Goi delegate da khai bao truoc do

//if (!File.Exists(MyFilename)) //{ // File.Create(MyFilename); // File. //} } } string sViet = "";

privatevoid SetText(string text) {

if (this.txtkq.InvokeRequired) {

SetTextCallback d = newSetTextCallback(SetText); // khởi tạo 1 delegate mới gọi đến SetText

this.Invoke(d, newobject[] { text }); }

else

{ //this.txtkq.Text = text;

if (text.StartsWith("HT") && text.IndexOf("5CS") > 0) { sViet = text.Substring(2, 6); txtkq.Text = sViet; } //mã hóa //char[] a = sViet.ToCharArray(); //s = ""; //for (i = 0; i < 6; i++) //{ // int x = Convert.ToInt16(a[i]) - 9; // if (x == 46)

// s += (x - 48).ToString();

//}

//txtkq.Text = s; //tao file Text

StreamWriter SViet = newStreamWriter(MyFilename, true);

SViet.WriteLine(InputData); SViet.Close(); Application.DoEvents(); try { Nong_do = Convert.ToDouble(this.txtkq.Text); } catch { //Nong_do = 0; } } }

// Đến hàm gửi data xuống COM

{

if (P.IsOpen) {

if (txtSend.Text == "") MessageBox.Show("Chƣa có dữ liệu!", "Thông Báo"); else P.Write(txtSend.Text);

}

elseMessageBox.Show("COM chƣa mở.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

txtSend.Clear(); }

// Để chọn tiện trong việc Test thì chúng ta sẽ làm thêm bƣớc nữa. cho mấy cái thông số

// hay dùng đƣợc chọn. Ko cần thiết nếu bạn cảm thấy ko cần.

privatevoid Form1_Load(object sender, EventArgs e) // sẽ đƣợc gọi khi mở chƣơng trình. { cbCom.SelectedIndex = 0; cbRate.SelectedIndex = 3; // 9600 cbBits.SelectedIndex = 2; // 8 cbParity.SelectedIndex = 0; // None cbBit.SelectedIndex = 0; // None // Hiện thị Status cho Pro tí

status.Text = "Hãy chọn 1 cổng COM để kết nối."; //mat ket noi

btKetNoi.Enabled = true; btNgat.Enabled = false; // Hiện thị Status

// khi khởi động sẽ đƣợc chạy

GraphPane myPane = zedGraphControl1.GraphPane; // Khai báo sửa dụng Graph loại GraphPane;

// Các thông tin cho đồ thị của mình

myPane.Title.Text = "Đồ thị nồng độ bụi"; myPane.XAxis.Title.Text = "Thời gian (s)"; myPane.YAxis.Title.Text = "Nồng độ (mg/m3)"; // Fill the axis background with a gradient

myPane.Chart.Fill = newFill(Color.White, Color.FromArgb(255, Color.White), 45.0F);

// Định nghĩa list để vẽ đồ thị. Để các bạn hiểu rõ cơ chế làm việc ở đây khai báo 2 list điểm <=> 2 đƣờng đồ thị

//RollingPointPairList list1 = new RollingPointPairList(1200); // Ở đây sử dụng list với 1200 điểm (có thể thêm nhiều liệu tại đây) RollingPointPairList list2 = newRollingPointPairList(1200);

// dòng dƣới là định nghĩa curve để vẽ.

// LineItem curve1 = myPane.AddCurve("Giới Hạn", list1, Color.Blue, SymbolType.None); // Color màu đỏ, đặc trƣng cho đƣờng 1

// SymbolType là kiểu biểu thị đồ thị : điểm, đƣờng tròn, tam giác ....

LineItem curve2 = myPane.AddCurve("Nồng Độ Bụi Theo Thời Gian", list2, Color.Red, SymbolType.None); // Color màu Xanh, đặc trƣng cho đƣờng 2 curve2.Line.Width =3.5F; curve2.Line.IsAntiAlias = true; curve2.Symbol.Fill = newFill(Color.White); curve2.Symbol.Size = 7; // ví dụ khoảng cách là 50ms 1 lần timer1.Interval = 100;

//timer1.Enabled = true; // Kích hoạt cho timer1 //timer1.Start(); // Chạy Timer1

// // Định hiện thị cho trục thời gian (Trục y) myPane.YAxis.Scale.Min = 0; // Min = 0; myPane.YAxis.Scale.Max = 0.6; // Max = 1;

myPane.YAxis.Scale.MinorStep = 0.05; // Đơn vị chia nhỏ nhất 1 myPane.YAxis.Scale.MajorStep = 0.1; // Đơn vị chia lớn 5

// Định hiện thị cho trục thời gian (Trục X)

myPane.XAxis.Scale.Min = 0; // Min = 0; myPane.XAxis.Scale.Max = 30; // Mã = 30;

myPane.XAxis.Scale.MinorStep = 1; // Đơn vị chia nhỏ nhất 1 myPane.XAxis.Scale.MajorStep = 5; // Đơn vị chia lớn 5 //

// Gọi hàm xác định cỡ trục

zedGraphControl1.AxisChange();

// Khởi động timer về vị trí ban đầu tickStart = Environment.TickCount; }

privatevoid btKetNoi_Click(object sender, EventArgs e) { try { P.Open(); btNgat.Enabled = true; btKetNoi.Enabled = false;

timer1.Enabled = true; // Khởi động timer về vị trí ban đầu

if (check == 0) { tickStart = Environment.TickCount; check = 1; }

MyFilename = Application.StartupPath + "\\Test\\DUST" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".txt";

}

catch (Exception) {

MessageBox.Show("Không kết nối đƣợc.", "Thử lại", MessageBoxButtons.OK, MessageBoxIcon.Error);

} }

privatevoid btNgat_Click(object sender, EventArgs e) { P.Close(); btKetNoi.Enabled = true; btNgat.Enabled = false; // Hiện thị Status status.Text = "Đã Ngắt Kết Nối"; timer1.Enabled = false; }

privatevoid btThoat_Click(object sender, EventArgs e) {

DialogResult kq = MessageBox.Show("Bạn thực sự muốn thoát", "HiTech", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (kq == DialogResult.Yes) {

this.Close(); } }

privatevoid btXoa_Click(object sender, EventArgs e) {

txtkq.Text = null; txtSend.Text = null; }

privatevoid cbCom_SelectedIndexChanged_1(object sender, EventArgs e) {

if (P.IsOpen) {

P.Close(); // Nếu đang mở Port thì phải đóng lại }

P.PortName = cbCom.SelectedItem.ToString(); // Gán PortName bằng COM đã chọn

}

privatevoid cbRate_SelectedIndexChanged_1(object sender, EventArgs e) { if (P.IsOpen) { P.Close(); } P.BaudRate = Convert.ToInt32(cbRate.Text); }

privatevoid cbBits_SelectedIndexChanged_1(object sender, EventArgs e) {

P.Close(); }

P.DataBits = Convert.ToInt32(cbBits.Text); }

privatevoid cbParity_SelectedIndexChanged_1(object sender, EventArgs e) {

if (P.IsOpen) { P.Close(); }

// Với thằng Parity hơn lằng nhằng. Nhƣng cũng OK thôi. ^_^ switch (cbParity.SelectedItem.ToString()) { case"Odd": P.Parity = Parity.Odd; break; case"None": P.Parity = Parity.None; break; case"Even": P.Parity = Parity.Even; break; } }

privatevoid cbBit_SelectedIndexChanged_1(object sender, EventArgs e) {

if (P.IsOpen) { P.Close();

} switch (cbBit.SelectedItem.ToString()) { case"1": P.StopBits = StopBits.One; break; case"1.5": P.StopBits = StopBits.OnePointFive; break; case"2": P.StopBits = StopBits.Two; break; } }

privatevoid txtkq_TextChanged(object sender, EventArgs e) {

}

privatevoid txtSend_TextChanged(object sender, EventArgs e) {

}

// Khai báo 1 biến dùng cho timer, chạy cột thời gian tính bằng ms int tickStart = 0;

// Để tiện cho việc sử dụng chúng ta sẽ xây dựng 1 hàm draw phục vụ cho việc vẽ đồ thị

{ float fValue = 0; try { //myParam[myParam.Count - 1] fValue = float.Parse(txtkq.Text); //textBox1.Text = fValue.ToString(); } catch { } if (zedGraphControl1.GraphPane.CurveList.Count <= 0) return;

// Kiểm tra việc khởi tạo các đƣờng curve // Đƣa về điểm xuất phát

//LineItem curve1 = zedGraphControl1.GraphPane.CurveList[0] as LineItem; LineItem curve2 = zedGraphControl1.GraphPane.CurveList[0] asLineItem; //if (curve1 == null)

// return;

//if (curve2 == null) // return;

// list chứa các điểm. // Get the PointPairList

//IPointListEdit list1 = curve1.Points as IPointListEdit; IPointListEdit list2 = curve2.Points asIPointListEdit;

//if (list1 == null) // return; if (list2 == null)

return;

// Time đƣợc tính bằng ms

double time = (Environment.TickCount - tickStart) / 1000.0;

// Tính toán giá trị hiện thị

// Muốn hiện thị cái gì thì chỉ việc thay vào setpointx

//list1.Add(time, setpoint1); // Đây chính là hàm hiển thị dữ liệu của mình lên đồ thị // list2.Add(time, setpoint2); // Đây chính là hàm hiển thị dữ liệu của mình lên đồ thị

// Ko vẽ setpoint2 mà thử vẽ đồ thị hình sin với 3 seconds per cycle list2.Add(time, fValue);

// đoạn chƣơng trình thực hiện vẽ đồ thị

Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale; if (time > xScale.Max - xScale.MajorStep)

{

//xScale.Max = time + xScale.MajorStep;

// xScale.Min = xScale.Max - 30.0; // Timer chạy qua 30 sẽ tự động dịch chuyển tịnh tiến sang trái

// Nếu ko muốn dịch chuyển mà chạy bắt đầu từ 0 thì : xScale.Min = 0; xScale.Max = time + xScale.MajorStep;

xScale.Min = xScale.Max - 30.0; }

// Vẽ đồ thị

}

privatevoid timer1_Tick(object sender, EventArgs e) { if (Nong_do < 0.2) { //xanh pictureBox1.Load(Application.StartupPath + "\\xanh.png"); } elseif (Nong_do > 0.4) { //do pictureBox1.Load(Application.StartupPath + "\\do.png"); } else { //vang pictureBox1.Load(Application.StartupPath + "\\vang.png"); } draw(0); } int check = 0;

privatevoid zedGraphControl1_ZoomEvent(ZedGraphControl sender, ZoomState oldState, ZoomState newState)

}

privatevoid zedGraphControl1_Load(object sender, EventArgs e) {

}

} }

LẬP TRÌNH CODE KHỐI PHÁT: MASTE

/***************************************************** This program was produced by the

CodeWizardAVR V2.05.6 Evaluation Automatic Program Generator

© Copyright 1998-2012 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com

Project : Do nong do bui Version : V2

Date : 10/17/2013 Author :Tran Anh Tuan Company : 2012BDKTD Comments:

Chip type : ATmega8 Program type : Application

AVR Core Clock frequency: 1.000000 MHz Memory model : Small

External RAM size : 0 Data Stack size : 256

*****************************************************/ #include <mega8.h> #include <delay.h> #include <alcd.h> #include <stdio.h> #include <string.h>

#include "NRF24L01.h"

//*******Khai bao thang do***********// #define thangdo0 0

#define thangdo1 1 #define thangdo2 2 #define MAX 1023

//******Khai bao bien toan cuc********// float ndbui=0;

unsigned char dis[10],thangdo=0; #define ADC_VREF_TYPE 0x00

// Read the AD conversion result

unsigned int read_adc(unsigned char adc_input) {

ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);

// Delay needed for the stabilization of the ADC input voltage delay_us(10);

// Start the AD conversion ADCSRA|=0x40;

// Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0);

ADCSRA|=0x10; return ADCW; }

//**********************************************// char xacdinh_thangdo()

PORTC.3=0; delay_us(280);//0.28ms x=read_adc(0); delay_us(40);//0.04ms PORTC.3=1; delay_us(9680); // PORTC.3=0; delay_us(280);//0.28ms y=read_adc(1); delay_us(40);//0.04ms PORTC.3=1; delay_us(9680); // PORTC.3=0; delay_us(280);//0.28ms z=read_adc(2); delay_us(40);//0.04ms PORTC.3=1; delay_us(9680); if(x>=MAX) { if(y>=MAX) { ndbui=z; return thangdo2; } else {

ndbui=y; return thangdo1; } } else { ndbui=x; return thangdo0; } } //********************************//

float noi_suy(float x1,float y1,float x2,float y2,float x) { return (y1+(((x-x1)*(y1-y2))/(x1-x2))); } /***************************************/ void docnong_dobui() { if(ndbui<122.88) {ndbui=0;} else { switch(thangdo) {

case thangdo0 : ndbui=noi_suy(122.88,0,1023,0.2,ndbui);break; case thangdo1 : ndbui=noi_suy(122.88,0,1023,0.4,ndbui);break; case thangdo2 : ndbui=noi_suy(122.88,0,1023,0.5,ndbui);break;

} sprintf(dis,"%0.4f",ndbui); lcd_gotoxy(0,1); lcd_puts(dis); } //*********************************************// // Declare your global variables here

void main(void) {

// Declare your local variables here DDRC |=(1<<3);//Output PORTC |=(1<<3);//=1

// Input/Output Ports initialization

// USART initialization

// Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On

// USART Transmitter: On // USART Mode: Asynchronous

// USART Baud Rate: 9600 (Double Speed Mode) UCSRA=0x02;

UCSRB=0x18; UCSRC=0x86; UBRRH=0x00; UBRRL=0x0C;

// ADC initialization

// ADC Clock frequency: 500.000 kHz // ADC Voltage Reference: AREF pin ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x81;

// Alphanumeric LCD initialization // Connections are specified in the

// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu: // RS - PORTD Bit 4 // RD - PORTB Bit 6 // EN - PORTB Bit 7 // D4 - PORTD Bit 5 // D5 - PORTD Bit 6 // D6 - PORTD Bit 7 // D7 - PORTB Bit 0 // Characters/line: 16 lcd_init(16); lcd_clear(); lcd_puts("Dust Sensor"); delay_ms(2000); lcd_clear(); lcd_puts("THANG DO :"); lcd_gotoxy(8,1); lcd_puts("MG/M3"); init_NRF24L01(); SetRX_Mode();

// Place your code here thangdo=xacdinh_thangdo(); lcd_gotoxy(11,0); lcd_putchar(thangdo+0x30); docnong_dobui(); // SetTX_Mode(); delay_ms(10); sprintf(TxBuf,"HT%0.4f5CS",ndbui); nRF24L01_TxPacket(TxBuf); SPI_RW_Reg(WRITE_REG+STATUS,0xFF); SetRX_Mode(); printf("HT%.4f5CS\n",ndbui); } }

LẬP TRÌNH CODE KHỐI THU SLAVE

/***************************************************** This program was produced by the

CodeWizardAVR V2.05.6 Automatic Program Generator

© Copyright 1998-2012 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com

Project : Khoi nhan tin hieu Version : V1

Date : 10/9/2013 Author :Tran Anh Tuan

Company : DHBK Comments:

Chip type : ATmega8 Program type : Application

AVR Core Clock frequency: 1.000000 MHz Memory model : Small

External RAM size : 0 Data Stack size : 256

*****************************************************/ #include <mega8.h> #include <alcd.h> #include <stdio.h> #include <string.h> #include "NRF24L01.h" char RxBuf[32]="",dis[32]="",*s=""; //*************************************// void giai_ma() { int i=0; if((RxBuf[0]=='H')&&(RxBuf[1]=='T')&& (RxBuf[8]=='5')&&(RxBuf[9]=='C')&&(RxBuf[10]=='S')) { strncpy(dis,RxBuf,8);//cat bo "5CS" s=strstr(dis,"97");//Cat bo HT sprintf(dis,"%s",s);

dis[i]=dis[i]-9; } } }

// Declare your global variables here

void main(void) {

// Declare your local variables here // USART initialization

// Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On

// USART Transmitter: On // USART Mode: Asynchronous

// USART Baud Rate: 9600 (Double Speed Mode) UCSRA=0x02; UCSRB=0x18; UCSRC=0x86; UBRRH=0x00; UBRRL=0x0C; // Alphanumeric LCD initialization // Connections are specified in the

// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu: // RS - PORTD Bit 4 // RD - PORTB Bit 6 // EN - PORTB Bit 7 // D4 - PORTD Bit 5 // D5 - PORTD Bit 6 // D6 - PORTD Bit 7

// D7 - PORTB Bit 0 // Characters/line: 16 lcd_init(16); lcd_clear(); lcd_puts("Welcom HiTech"); init_NRF24L01(); SetRX_Mode(); while (1) {

// Place your code here

if(nRF24L01_RxPacket(RxBuf)==1)//Neu nhan dc du lieu {

//Place your code here to process data received lcd_gotoxy(0,1); giai_ma(); lcd_puts(dis); lcd_gotoxy(8,1); lcd_puts("MG/M3"); printf("HT%s5CS\n",dis); SetRX_Mode(); } } }

Một phần của tài liệu Nghiên cứu thiết kế và chế tạo thiết bị đo và giám sát nồng độ bụi thông minh có tích hợp truyền thông không dây (Trang 63 - 99)