/* LAB4.C */ /* DC motor with proportional+integral control of velocity */ #include #include #include #include #include #include /* Declaration of user variables and constants */ #define ampgain 5. /* Amplifier gain */ #define maxpoints 4000 /* Number of points to be stored on disk */ #define twopi 6.2831852 /* 2 pi */ FILE *fp; char fname[]="DATA.M"; float ref[maxpoints],pos[maxpoints],vel[maxpoints]; float keybin,y,r,e,u,uda,kp,kf,ki,kid,ist,oldy,om;int clock,npoints; /* Keyboard routine */ void user_interface() { scanf("%f",&keybin); } /* Initialization routine */ void user_init() { put_da(0,0.); encoder_init(); freq=500.; clock=0; printf("Enter kp, ki, kf:\n"); /* PI parameters */ scanf("%f",&kp); scanf("%f",&ki); scanf("%f",&kf); kid=ki/freq;ist=0.;oldy=0.; printf("Enter reference velocity in rpm, type Ctrl-Break to stop:\n"); scanf("%f",&keybin); } /* Data storage on disk -- File readable by MATLAB, simply typing "data" */ void write_data() { npoints=clock; fp=fopen(fname,"w"); fprintf(fp,"t=[\n"); for (clock=0;clock rad/s */ y=get_encoder(); /* Get position measurement in rad */ om=(y-oldy)*freq; oldy=y; e=r-om; ist=ist+e; u=kf*kp*r-kp*om+kid*ist; /* PI control law */ uda=u/ampgain; put_da(0,uda); if (clock