BOT LAYER (MẠCH ĐIỀU KHIỂN BẰNG TAY)

Một phần của tài liệu ỨNG DỤNG SÓNG CAO TẦN VÀO ĐIỀU KHIỂN ROBOT (Trang 61)

3.11.3 Mạch sau khi thi cơng

3.12 GIỚI HẠN ĐỀ TÀI

Do đây là đề tài làm luận văn tốt nghiệp, thời gian thực hiện và tài chính để hồn thành đề tài cĩ giới hạn nên khơng cĩ tầm mở rộng cao và đi sâu nghiên cứu phát huy những ưu điểm vượt trội của kỹ thuật điều khiển từ xa. Nên đề tài chỉ thực hiện những vấn đề sau:

 Ứng dụng điều khiển cho Robot di chuyển.

 Thiết kế và thi cơng mạch xử lí giao tiếp qua sĩng RF.

 Điều khiển phần cơng suất thơng qua các linh kiện cách ly như Opto, relay.  Phần khuếch đại với IC 2803 và FET IRF540N.

 Dùng kỹ thuật điều xung PWM để điều khiển cơng suất động cơ.  Giao tiếp với máy tính để điều khiển thiết bị bằng sĩng RF.

 Kết hợp thêm nếu cĩ thể với một mạch điều khiển bằng tay để tăng độ linh hoạt cho việc điều khiển.

Chương 4

PHẦN MỀM VÀ CHƯƠNG TRÌNH ĐIỀU KHIỂN 4.1 PHẦN MỀM DOCK LIGHT

 Giao diện trực quan, dễ sử dụng.  Kết nối với cổng COM của máy tính.

 Hiển thị dữ liệu truyền chính xác với kiểu BIN DEC HEX OCT.

 Cho phép thiết lập tốc độ baud, số bit trong frame cũng như truyền trực tiếp ký tự từ bàn phím ra cổng serial.

(Cách sử dụng chương trình khơng được trình bày trong luận văn.)

4.2 CHƯƠNG TRÌNH ĐIỀU KHIỂN

Nhằm tạo sự thuận lợi trong việc bảo trì và nâng cấp, cũng là để chương trình trở nên dễ đọc hơn, chương trình điều khiển sẽ được bố cục theo từng module dưới dạng các chương trình con trong các file.c. Các module này sẽ hỗ trợ và gọi chương trình con từ nhau.

4.2.1 CHƯƠNG TRÌNH CHÍNH Main.c

Chương trình chính sẽ gọi các hàm khởi động, PCA, UART và các thiết lập ban đầu, sau đĩ là thực hiện cơng việc được gửi từ bàn phím.

#include <declare.h> uint_8 xdata memory[20]; uint_8 xdata memory_temp[20];

int speed_motor = 0, SPEED_TURN = 0, gear = 0, gear_turn = 0, uart_data_receive = 0;

void main() {

init_PCA();

//sau init_PCA() cac chan dieu khien dong co da duoc dieu xung pwm voi Fosc = //Fxtal/6;

//duty cycle = 0%; init_uart(); delay_us(100); while(1)

{ } }

4.2.2 HEADER declare.h

HEADER declare.h chứa một file header khai bào định nghĩa tên biến và các chương trình con sẽ dùng tới như điều khiển robot di chuyển, nhận tín hiệu, gửi tín hiệu, v..v.. tất cả được khai báo ở đây.

#ifndef _DELARE_H_ #define _DELARE_H_

#include <define_sfr_const.h> //func of delay_func.c/// void delay_ms(uint_8 time); void delay_us(uint_8 time); //func of int_PCA_timer.c // void init_PCA();

//int UART///////// void init_uart(void);

///////////////////////////// (adsbygoogle = window.adsbygoogle || []).push({});

//void receive_data() interrupt 4; //read data va process data

void read_data_uart(); void process_data(); ///////////////////// void choose_speed(); void inc_gear_turn(); void dec_gear_turn();

/////////xu ly hanh dong - Action.c //////////// void go_ahead(); void go_back(); void turn_left(); void turn_right(); void inc_gear(); void dec_gear();

//////////xu li di chuyen - Moving.c - 2 dong co left va right void switch_forward();

void switch_return();

void power_motor();//cap nguon cho dong co void power_motor_turn_left();

void power_motor_turn();

void power_motor_turn_right();

/////////xu ly gap va nha vat the (neu co) /* void switch_forward_up(); void switch_forward_cap(); void switch_return_up(); void switch_return_cap(); */ #endif 4.2.3 HEADER define_sfr_const.h

Module này chứa header p89v51.h của nhà sản xuất và thêm vào các định nghĩa bit để tiện bảo trì hay sửa chữa source. Header p89v5.h1 sẽ khơng được trình bày trong luận văn này.

#ifndef _DEFINEE_H_ #define _DEFINEE_H_ #include <P89V51.h>

typedef unsigned char uint_8; typedef unsigned int uint_16;

///////////////////////////////// //////////////////////////////////// sfr speed_motor_left = 0xFA; //sfr speed_motor_right = 0xFB; sfr speed_motor_right = 0xFE; sfr speed_motor_up = 0xFC; sfr speed_motor_cap = 0xFD; /////////////////////////////////////////

sbit p_switch_motor_left = P2^6; //pin controls switch for motor left -- = Ctrl_RELAY0

//sbit p_switch_motor_right = P2^7; //pin controls switch for motor right -- = Ctrl_RELAY1

sbit p_switch_motor_right = P1^2;

sbit p_switch_motor_up = P1^0; //pin controls switch for motor up - down -- = Ctrl_RELAY2

sbit p_switch_motor_cap = P1^1; //pin controls switch for motor cap - release -- = Ctrl_RELAY3

/////////////////////////////////////////// //define cho doc gamepad hoac matrix button /*

sbit p_key_1 = P3^5; sbit p_key_2 = P3^6;

sbit n_read_key_0 = P3^2; sbit n_read_key_1 = P3^3; */

#define p_en_switch_motor ET1; ////////////////////////////////////// #define SPEED_0 0 #define SPEED_1 70 #define SPEED_2 110 #define SPEED_3 160 #define SPEED_4 210 /////////////////////////////////////// #define SPEED_TURN_0 15 #define SPEED_TURN_1 25 /* #define SPEED_TURN_2 20 #define SPEED_TURN_3 25 */ ///////////////////////////////////// #define SPEED_MOTOR_CAP_OUT 170 #define SPEED_MOTOR_CAP_IN 170 #define SPEED_MOTOR_DOWN 170 #define SPEED_MOTOR_UP 140 //////////////////////////////////////// #define SPEED_DIFFER 10 ////////////////////////////////////// #define key_press 1 #define key_not_press 0 #define SWITCH_RETURN 0 ////////////////////////////////////////

#define SWITCH_FORWARD 1 //quy uoc de linh hoat trong viec #define SWITCH_RETURN 0 // cap nguon va xoay chieu

// 2 motor di dung cho di chuyen ve co ban la xoay nguoc chieu nhau khi lap vao than robo

// nhung se duoc thay doi de dang ve phan cung bang cach dau lai chieu cap nguon

// do do ta quy uoc 1-cap cho role de motor quay chay toi // 0-cap cho role de motor quay chay lui /*

ta quy dinh la:

- trong mang memory chua gia tri khoi dong cua cac phim nhan cu the la:

0/ len 1/ xuong 2/ trai 3/ phai

5/ so 2: dua xuong 6/ so 3: gap 7/ so 4: nha */ #endif 4.2.4 MODULE init_PCA_timer.c (adsbygoogle = window.adsbygoogle || []).push({});

Module init_PCA_timer.c chứa chương trình con thiết lập giá trị ban đầu và khởi động PCA để điều khiển 5 động cơ.

#include <declare.h> void init_PCA()

{

CCAPM0 = 0x42; //set P1.3 pwm mode CCAPM1 = 0x42; //set P1.4 pwm mode CCAPM2 = 0x42; //set P1.5 pwm mode CCAPM3 = 0x42; //set P1.6 pwm mode CCAPM4 = 0x42; //set P1.7 pwm mode

CMOD = 0x00; // thiet lap chia Fosc internal cho 6 lam pulse cho PCA

//thong wa 2 bit CPS1 va CPS0 trong CMOD // Internal clock tu Xtal

CCAP0H = 0;//thanh ghi gia tri dieu khien duty cycle cua 1 chu ki pwm

//CCAP0H Module 0 Capture HIGH add: FAH

CCAP0L = 0;//CCAP0L Module 0 Capture LOW add: EAH CCAP1H = 0; CCAP1L = 0; CCAP2H = 0; CCAP2L = 0; CCAP3H = 0; CCAP3L = 0; CCAP4H = 0; CCAP4L = 0;

//voi cac gia tri deu la 0 de bao dam motor dung yen sau khi reset

CR = 1;

/*The CCON SFR contains the run control bit for the PCA (CR) and the flags for the

PCA timer (CF) and each module (CCF4:0). To run the PCA the CR bit (CCON.6)

must be set by software. The PCA is shut off by clearing this bit. The CF bit (CCON.7)

is set when the PCA counter overflows and an interrupt will be generated if the ECF

bit in the CMOD register is set. The CF bit can only be cleared by software.*/ delay_ms(1); } /* void init_timer() {

T2MOD = 0; //T2MOD Bit 7 6 5 4 3 2 1 0 // Symbol - - - T2OE DCEN //T2OE:Timer 2 Output Enable bit. //DCEN:Down Count Enable bit.

T2CON = 0; //T2CON - Timer/counter 2 control register (address C8H) bit allocation

// Bit addressable; Reset value: 00H // Bit 7 6 5 4 3 2 1 0

// Symbol TF2 EXF2 RCLK TCLK EXEN2 TR2 C/T2 CP/RL2 // DATA SHEET PAGE 31/80 TO KNOW FUNCTION OF EACH BIT

RCAP2H = 0xFF;

RCAP2L = 0x01;//RCAP2H Timer2 Capture HIGH ADD CBH // RCAP2L Timer2 Capture LOW ADD CAH

ET2 = 1; //ET2 Timer 2 Interrupt Enable.

EA = 1; //EA Interrupt Enable Bit: EA = 1 interrupt(s) can be serviced, EA = 0

//interrupt servicing disabled.

TR2 = 1; //TR2 Start/stop control for Timer 2. A logic ‘1’ enables the timer to run.

/////////////////////// }

*/

4.2.5 MODULE delay_func.c

Module delay_func.c chứa các hàm xử lý delay, được tính tốn và căn chỉnh thời gian trên mơ phỏng proteus. (adsbygoogle = window.adsbygoogle || []).push({});

#include <declare.h>

void delay_ms(uint_8 time) { while(time--) { uint_8 a = 165; while(a--); } }

void delay_us(uint_8 time) {

while(time--); }

4.2.6 MODULE init_UART.c

Module init_UART.c chứa các hàm xử lý giá trị ban đầu cho giao tiếp UART, sử dụng timer tạo tốc độ baud, các hàm gửi và nhận dữ liệu.

#include <declare.h>

extern uint_8 xdata memory[20]; extern uint_8 xdata memory_temp[20];

extern int speed_motor, SPEED_TURN, gear, gear_turn, uart_data_receive;

/************************************************************* **********

Chuc nang: Khoi tao UART o mode 1, -Su dung dao dong Osc 11.0592Mhz -BAUDRATE: 9600

Gia tri tra ve: Khong co

De dung ket hop voi ngat uart, phai dat EA=1

************************************************************** **********/

/*void init_uart() {

//Cau hinh cho UART //Chon mode 1

SM0=0; SM1=1;

SM2=0; //Chu y neu load file header tu trang ban dan cua Philips

//phai sua lai SM3 thanh SM2!

//Chon Timer 2 la nguon sinh toc do Baud

//Cau hinh cho Timer 2, xem them o phan Timer 2 trong Datasheet

TCLK=1;

//T2OE=0; luu y thanh ghi T2MOD khong can thiep tung bit duoc //T2OE Timer 2 Output Enable bit. Used in programmable clock- out mode

//only.

T2MOD|=0x02;

//Cho Timer 2 chay TR2=1;

//Toc do Baud la 9600, luu y cong thuc 3 hoac bang 24 trong Datasheet

RCAP2H=0xFF; RCAP2L=0xB9;

// Khong co uu tien ngat //IP0&=0xEF;

//IP0H&=0xEF; (adsbygoogle = window.adsbygoogle || []).push({});

// Cho phep ngat noi tiep ES = 1;

} //Ket thuc ham khoi tao UART */

void init_uart()//dung trong truyen thong noi tiep {

SCON=0x50;//che do 8 bit du lieu,1 bit sart,1 bit stop TMOD&=0x0F;//

TMOD|=0x20;//timer1 in mode2

TH1=0xFD;//set baut rate at 9600(11.0592Mhz) TL1=0xFD;

ES=1;//cho phep ngat noi tiep

EA=1;//cho fep tat ca cac ngat TR1=1;//chay timer1

}

void send_data(char uart_data)//gui du lieu qua cong COM {

TI=0; //Transmit interrupt flag. Set by hardware at the end of the 8th bit time in

//mode 0, or at the stop bit in the other modes, in any serial //transmission. Must be cleared by software.

//SBUF=0x00; SBUF=uart_data;

while(TI==0){}//cho trong khi dang gui du lieu TI=0;

}

void receive_data() interrupt 4//nhan du lieu { RI=0; uart_data_receive=SBUF; read_data_uart(); //test_key(); process_data(); //process_key(); } 4.2.7 MODULE process_motor.c

Module process_motor.c chứa các hàm nhận dữ liệu điều khiển, sau đĩ thi hành lệnh. #include <declare.h>

///////////////////////////// extern uint_8 xdata memory[20]; extern uint_8 xdata memory_temp[20];

extern int speed_motor, SPEED_TURN, gear, gear_turn, uart_data_receive;

/////////////////////////// void read_data_uart()

{ if (uart_data_receive == 'w') memory[0] = key_press; else memory[0] = key_not_press; if (uart_data_receive == 's') memory[1]= key_press; else memory[1] = key_not_press; if (uart_data_receive == 'a') memory[2] = key_press; else memory[2] = key_not_press; if (uart_data_receive == 'd') memory[3] = key_press; else memory[3] = key_not_press; } /////////////////////////// void process_data()

{ /*ham process_key() la de tac dong chu yeu len 4 dong co mac dinh khi chua dung tang toc va ha toc thi dong co chay voi 1/2 cong suat

do RCAP2H = 0xFF; RCAP2L = 0x01; trong init.c*/

// thang, lui, trai, phai, ha toc, tang toc tac dong den 2 dong co

//////VIET LAI//////////

//neu nhan chong nhieu phim thi se khong co lenh nao duoc thuc hien

//////////// toi

if ((memory[0] == key_press) && (memory[1] != key_press) && (memory[2] != key_press) && (memory[3] != key_press)) ////////////nhan nut cho chay thang

go_ahead(); /////////// lui

if ((memory[1] == key_press) && (memory[0] != key_press) && (memory[2] != key_press) && (memory[3] != key_press)) (adsbygoogle = window.adsbygoogle || []).push({});

go_back();

/////////// trai

if ((memory[2] == key_press) && (memory[0] != key_press) && (memory[1] != key_press) && (memory[3] != key_press))

turn_left(); //////////phai

if ((memory[3] == key_press) && (memory[1] != key_press) && (memory[2] != key_press) && (memory[0] != key_press))

turn_right(); ////////////////////////////////////////////////////////////// /////////////////////////////////////////// memory[0] = key_not_press; memory[1] = key_not_press; memory[2] = key_not_press; memory[3] = key_not_press; uart_data_receive = 0; } }

4.2.8 MODULE moving.c

Module moving.c chứa các hành động tác động lên cơng suất điều khiển động cơ cũng như chiều quay của động cơ.

#include <declare.h>

///////////////////////////// extern uint_8 xdata memory[20]; extern uint_8 xdata memory_temp[20];

extern int speed_motor, SPEED_TURN, gear, gear_turn, uart_data_receive;

//////////////////////////////////

void power_motor() //cho phep dong co chay chu chua co chieu quay cua dong co

{

speed_motor_left = speed_motor;

speed_motor_right = speed_motor;// + SPEED_DIFFER; }

void power_motor_turn() //cho phep dong co chay chu chua co chieu quay cua dong co

{

speed_motor_left = SPEED_TURN;

speed_motor_right = SPEED_TURN;// + SPEED_DIFFER;

Một phần của tài liệu ỨNG DỤNG SÓNG CAO TẦN VÀO ĐIỀU KHIỂN ROBOT (Trang 61)