CHƯƠNG V : THUẬT TOÁN ĐIỀU KHIỂN5.1.Ý tưởng : Dùng máy tính thiết kế giao diện truyền thông số vị trí xuống vi điều khiển, sau đó vi điiều khiển sẽ nhận và xử lí tín hiệu đó rồi cho động
Trang 1CHƯƠNG V : THUẬT TOÁN ĐIỀU KHIỂN
5.1.Ý tưởng :
Dùng máy tính thiết kế giao diện truyền thông số vị trí xuống vi điều khiển, sau đó
vi điiều khiển sẽ nhận và xử lí tín hiệu đó rồi cho động cơ chạy đến vị trí định trước
5.2 Thực hiện
5.2.1.Thiết kế giao diện và lập trình dùng Visual Basic 6.0
Giao diện giúp người dùng truyền 2 byte chứa vị trí cần đi của vít-me và các thông
số về chiều quay
a.Giao diện
Hình 5.1 Giao diện VB
Trang 2b.VB code
Private Sub cmd1_Click()
If cmd1.Caption = "Vitri" Then vitri = Val(txt1.Text) * 199
malenh(0) = 251
bytetosend(0) = vitri And 255 bytetosend(1) = vitri \ 256 And 255 MSComm1.Output = bytetosend() MSComm1.Output = malenh() Else
cmd1.Caption = "Vitri"
cmd5.Enabled = True
vitri = Val(txt1.Text) * 197
malenh(0) = 251
bytetosend(0) = vitri And 255 bytetosend(1) = vitri \ 256 And 255 MSComm1.Output = bytetosend() MSComm1.Output = malenh() End If
End Sub
Private Sub cmd2_Click()
malenh(0) = 253
MSComm1.Output = malenh() End Sub
Private Sub cmd3_Click()
malenh(0) = 254
MSComm1.Output = malenh() End Sub
Private Sub cmd4_Click()
malenh(0) = 252
MSComm1.Output = malenh() End Sub
Private Sub cmd5_Click()
cmd5.Enabled = False
cmd1.Caption = "Ready"
malenh(0) = 254
MSComm1.Output = malenh() End Sub
Trang 3Private Sub Form_Load()
With MSComm1
.CommPort = 1 'chose COM1
.Settings = "9600,N,8,1" 'baud 9600bps, none parity, 8 bit data, 1 bit stop InBufferSize = 1024
.InputLen = 0
.RThreshold = 1
.SThreshold = 0
.OutBufferSize = 512
.InputMode = comInputModeBinary
'.InputMode = comInputModeText
.PortOpen = True
End With
cmd2.Enabled = False
cmd3.Enabled = False
End Sub
Private Sub test_Click()
If test.Caption = "Test" Then
txt1.Enabled = False
cmd5.Enabled = False
cmd1.Enabled = False
cmd2.Enabled = True
cmd3.Enabled = True
test.Caption = "Untest"
Else
txt1.Enabled = True
cmd2.Enabled = False
cmd3.Enabled = False
test.Caption = "Test"
cmd5.Enabled = True
cmd1.Enabled = True
End If
End Sub
Trang 45.2.1.Lập trình điều khiển dùng Atmega8
Sau khi nhận vị trí do máy tính gửi xuống lúc này VĐK sẽ cho trục vít-me quay đến đúng vị trí thì dừng
Code :
#include <mega8.h>
#include <delay.h>
#define thuan PORTC.5
#define nghich PORTC.4
int flag;
unsigned int xung,xung1;
unsigned int vitri;
char m;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
xung=xung+1;
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
// Place your code here
nghich=0;
thuan=0;
OCR1A=0 ;
}
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
Trang 5// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void) {
char status,data;
status=UCSRA;
data=UDR;
if (data == 253)
{
flag = 1;
thuan=1 ;nghich=0;OCR1A=80 ;
}
else if (data == 254)
{
flag =2 ;
thuan=0 ;nghich=1;OCR1A=80 ;
}
else if (data==252)
{
flag=3;
nghich=0;thuan=0 ;OCR1A=0 ;
}
else if (data==251)
{
xung1=xung+(unsigned int)vitri;
flag=4;
thuan=1;nghich=0;OCR1A=80;
}
else
{
m++;
switch (m) {
Trang 6case 1:
vitri=(unsigned int)data;
break;
case 2: vitri=((unsigned int)data*256) + vitri;
m=0;
break;
};
}
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
rx_counter;
#asm("sei")
return data;
}
#pragma
used-#endif
// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];
#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif
// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
Trang 7{
tx_counter;
UDR=tx_buffer[tx_rd_index];
if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0; };
}
#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0)) {
tx_buffer[tx_wr_index]=c;
if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0; ++tx_counter;
}
else
UDR=c;
#asm("sei")
}
#pragma
used-#endif
// Standard Input/Output functions
#include <stdio.h>
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
Trang 8// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=T PORTB=0x00;
DDRB=0x02;
// Port C initialization
// Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In // State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x30;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=P State2=P State1=T State0=T PORTD=0x0C;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 250.000 kHz
TCCR0=0x03;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 16000.000 kHz
// Mode: Fast PWM top=00FFh
// OC1A output: Non-Inv
// OC1B output: Discon
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x83;
TCCR1B=0x09;
TCNT1H=0x00;
Trang 9ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Any change
// INT1: On
// INT1 Mode: Falling Edge
GICR|=0xC0;
MCUCR=0x09;
GIFR=0xC0;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x67;
// Analog Comparator initialization
Trang 10// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
//vitri=1000;
while (1)
{
if ((xung>=xung1)&&(flag==4))
{
thuan=0;
nghich=0;
flag=0;
}
};
}
5.3 Kết quả :
a.Những việc đã thực hiện được:
- Thi công xong mô hình
- Điều khiển được vị trí của vít-me bi
- Thiết kế giao diện truyền dữ liệu từ máy tính xuống VĐK
b Hạn chế:
- Do không dùng thuật toán PID nên có sai số cộng dồn
- Nút nhấn bị nhiễu quá nhiều gây sai lệc trong quá trình điều khiển