PIC Vietnam

Go Back   PIC Vietnam > Robotics > Thực hành

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Thực hành Bắt đầu làm một robot như thế nào? Mẹo vặt? Kỹ thuật? Công nghệ?... Hãy bắt tay vào việc...

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 09-05-2007, 06:40 PM   #8
omlun
Đệ tử 1 túi
 
Tham gia ngày: Mar 2007
Bài gửi: 25
:
Trích:
Nguyên văn bởi bluepine View Post
Chào mọi người, em hiện đã điều khiển được động cơ dùng PID, số liệu khá chính xác (sai lệch về tốc độ rất nhỏ). Tuy nhiên chỉ điều khiển được với một tải cố định thôi, khi gỡ cục tải ra thì không còn đúng nữa nên em nghĩ các thông số PID thay đổi khi tải thay đổi. Anh F cho em hỏi có cách nào tính các thông số PID theo sự thay đổi của tải không?
Đây là chương trình em đã chạy tốt với 1 tải cố định

#include <16f877a.h>
#fuses HS,NOPROTECT,NOWDT,NOBROWNOUT
#use delay (clock = 20000000)
// Giao tiep vi dieu khien
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8) // Giao tiep RS232

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include<ctype.h>

#byte PR2 = 0x92

int16 e2;
int16 e1;
///////////////////////////////////////////////////////

int16 v_set = 250;

/////////////////////////////////////////////////////////
int16 v_cur;
int16 e_sum;
int16 e_del;
int16 flag_timer1;


///////////////////////////////////////////////////////////
unsigned long kp =5;
unsigned long ki = 0.62;
unsigned long kd = 10;
////////////////////////////////////////////////////////////


int16 pw_duty;
int16 counted_round_value;
int16 temp_timer0;
int16 update_counted_round_value;

#byte TMR0 = 0x01 //timer0 REGISTER
#byte TMR1_L = 0X0E
#byte TMR1_H = 0X0F

#define START_VALUE_TIMER0 55 //tri khoi tao ban dau cua timer0 55, so xung dem duoc moi khi ngat la 255 - 55
#define START_VALUE_TIMER1 15535 //tri khoi tao ban dau cua timer1 65536-15536

#INT_TIMER0 // ngat timer0 tang bien len 1
void TIMER0_int()
{

set_timer0(START_VALUE_TIMER0);
counted_round_value++;

}

#INT_TIMER1 // ngat timer0 tang bien len 1
void timer1_int()
{

set_timer1(START_VALUE_TIMER1);


temp_timer0 = get_timer0();

if (temp_timer0 == 0){
temp_timer0 = 255;
}
v_cur = counted_round_value*400 + 2*(temp_timer0 - START_VALUE_TIMER0);
set_timer0(START_VALUE_TIMER0);
counted_round_value = 0;
//printf("%ld\t",pw_duty);
flag_timer1 = 1;
}


void init_timer0()
{

setup_timer_0(RTCC_EXT_H_TO_L | RTCC_DIV_2); // timer0 dung xung ngoai va chia xung vao


setup_timer_1(T1_INTERNAL | T1_DIV_BY_4); // moi lan tran timer1 la 50000*4* (1/5) uS = 40ms

set_timer0(START_VALUE_TIMER0);
set_timer1(START_VALUE_TIMER1);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
}

void init_PWM(int16 frequency){

setup_ccp1(CCP_PWM); // initiate PWM
PR2 = 20000000/4/frequency - 1; // set PWM period

setup_timer_2(T2_DIV_BY_1,255,1); // initiate time 2 The cycle time will be (1/clock)*4*t2div*(period+1)
// (1/20000000)*4*1*(255+1) = 51.2 us( will over flow every 51.2 us, will intrup every 51.2 uS or 19.5 khz;

}

void cal_pid(){

long temp_kp;
long temp_ki;
long temp_kd;
e2 = v_set - v_cur;
e_sum += e2;
e_del = e2 - e1;
e1 = e2;

temp_kp = kp*e2;
temp_ki = ki*e_sum;
temp_kd = kd*e_del;
pw_duty +=temp_kp;

if (pw_duty <1000)
pw_duty += temp_ki;
if (pw_duty <1000)
pw_duty += temp_kd;
if (pw_duty <256) // vi khi pw_duty <256 thi PWM chi dieu rong xung 8 bit / //chu khong phai 10 bit,
pw_duty = 256;
if (pw_duty >1000) // bao hoa
pw_duty = 1000;

printf(" \t%ld", v_cur); // truyen toc do xung ve may tinh hien thi tren Hyperterminal
}

void main()
{
int8 timer0_value;

init_timer0();
init_PWM(19500);

while(true){
if (flag_timer1 ==1){
flag_timer1 = 0;
cal_pid();
set_pwm1_duty(pw_duty);
}
}
}

mến

bạn có thể pót sơ đồ nguyên lý của mạch điều khiển động cơ l;ên ko??
xung encoder pha A ban nối vào chân nào vậy?
đọc xong code của bạn tớ đang tính làm 1 mạch để tét thử
omlun vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 05:10 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam