3 /* Define globle variables */
15 int left
[5]={3800,4000,4100,4200,4300};
16 int right
[5]={3650,3500,3400,3300,3100};
19 struct ctrl_data cdata
={1,M
,S0
};
20 struct mod_data mdata
={0,0,PIEX
>>1,0,PIEX
>>1,PIEX
>>1};
21 struct pid_data lpid
={5,0,0};
23 /* Functions implement */
31 for(j
=0; j
<3338; j
++);
35 void SetBusCLK_64M(void)
37 CLKSEL
=0X00;//disengage PLL to system
38 PLLCTL_PLLON
=1;//turn on PLL
41 POSTDIV
=0x00; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=128MHz;
42 _asm(nop
); //BUS CLOCK=64M
44 while(!(CRGFLG_LOCK
==1)); //when pll is steady ,then use it;
45 CLKSEL_PLLSEL
=1; //engage PLL to system;
48 /* Initialze drivers */
54 /* Enable 33886 chip MIDS*/
58 PTT_PTT3
=1; // 1 enable; 0 disable
60 /* Enable MOTOR control module */
65 /* Initial MOTOR control module */
66 WPLUS_SetRatio16(3500);
71 // Send image frame data. Debug func
76 // for(j=0;j<PIEX;j++){
79 while(ERR_OK
!=SPI_SendChar(j
));
83 // Send image frame data, Debug func
84 void transchar_n(void){
89 while(ERR_OK
!=SPI_SendChar(data
[i
][j
]));
99 for (i
= 1; i
<= LINE
; i
++){
100 for (j
= 0; j
<PIEX
; j
++){
104 if (avg
== 0) avg
=cntl
;
105 else avg
= (avg
+cntl
)>>1;
108 ENVB
= avg
; //Updata ENVB
110 void data_proc(void){
113 int i
=0,j
=PIEX
,t
=0,f
=0,m
=0,top
=LINE
-1,lft
=PIEX
,rit
=0,cnt
=LINE
>>1;
124 if(data
[LINE
-1][i
]<black
) black
=data
[LINE
-1][i
];
127 for(i
=0;i
<PIEX
;i
++){ // Searching white spots
128 if(data
[LINE
-1][i
]>brt
){
129 if(j
>=i
) j
=i
; // Left
130 if(t
<=i
) t
=i
; // Right
142 // Otherwise locating blackspot
160 if(data
[LINE
-1][i
]<black
+10) {
167 // Loop searching next one
168 for(i
=LINE
-2; i
>=0; i
--){
172 while(j
==-1 && f
<LINE
&& m
<4){
183 }else if(data
[i
][j
-1]<brt
&& j
-1<PIEX
) datap
[i
]=j
-1;
184 else if (data
[i
][j
+1]<brt
&& j
+1<PIEX
) datap
[i
]=j
+1;
185 else if (data
[i
][j
-2]<brt
&& j
-2<PIEX
) datap
[i
]=j
-2;
186 else if (data
[i
][j
+2]<brt
&& j
+2<PIEX
) datap
[i
]=j
+2;
221 void speed_acq(void){
222 // unsigned int temp;
226 /* PID Algorithm Implement */
235 /* Module generating */
242 tempr
=mdata
.RIT
-mdata
.LFT
;
245 while(datap[i]!=-1 && i>=0){
250 mdata.CNT=temp; // Updata CNT
252 // Reference spot set as middle one?
255 } else if(mdata
.TOP
>20) {
256 mdata
.MOD
=1; // 'U' module. If uncontinus spot detected?
257 if(mdata
.STA
==0) mdata
.STA
=1;
258 if(mdata
.STA
==2) mdata
.STA
=2;
260 else if( tempr
<10) { // Trace at only in one side OR offset<=3
261 mdata
.MOD
=0; // LINE module
265 mdata
.MOD
=2; // 'S' module
266 if(mdata
.STA
==0) mdata
.STA
=1;
267 if(mdata
.STA
==2) mdata
.STA
=2;
271 /* Motor & WPLUS control pad. */
284 offset
= (datap
[mdata
.TOP
]+datap
[LINE
-1])>>1;
286 // Calculating control paraments
288 case 0: // Line control
307 // Implement driver control
310 if(cdata
.WPLUS
<= M
+WLFT
&& cdata
.WPLUS
>=M
+WRIT
){
311 PWM2_SetRatio16(cdata
.MOTOR
);
312 WPLUS_SetRatio16(cdata
.WPLUS
);
315 WPLUS_SetRatio16(cdata
.WPLUS
);
332 offset
=datap
[mdata
.TOP
]; // Using NEARest spot as reference
333 if(offset
>PV2
-2 && offset
<PV2
+2){
335 r
=feedback(cdata
.LOR
, r
, lpid
.kp
);
341 }else if(datap
[mdata
.TOP
]<=PV2
&& offset
>0){
343 foo
=(PV2
-offset
)*foo
;
345 w
=foo
+M
; // Most to 50%
346 r
=feedback(cdata
.LOR
, w
,lpid
.kp
);
351 }else if(datap
[mdata
.TOP
]>PV2
&& offset
<PIEX
-1){
353 foo
=(offset
-PV2
)*foo
;
356 r
=feedback(cdata
.LOR
, w
,lpid
.kp
);
368 offset
=(mdata
.RIT
+mdata
.LFT
)>>1;
371 cdata
.WPLUS
=right
[4];
378 if(datap
[mdata
.TOP
]>PV2
){
380 if(offset
<40) cdata
.WPLUS
=right
[0];
381 else if(offset
<45) cdata
.WPLUS
=right
[0];
382 else if(offset
<48) cdata
.WPLUS
=right
[2];
383 else cdata
.WPLUS
=right
[3];
385 if(offset
>15) cdata
.WPLUS
=left
[1];
386 else if(offset
>10) cdata
.WPLUS
=left
[4];
387 else if(offset
>5) cdata
.WPLUS
=left
[4];
388 else cdata
.WPLUS
=left
[4];
400 if(offset
>PV2
-8 && offset
<PV2
+8){
403 }else if(offset
<=PV2
-8 && offset
>0){
405 foo
=(PV2
-offset
)*foo
;
407 w
=foo
+M
; // Most to 50%
410 }else if(offset
>=PV2
+8 && offset
<PIEX
-1){
412 foo
=(offset
-PV2
)*foo
;
419 foo
=(15+mdata
.TOP
)*foo
;
421 w
=foo
+M
; // Most to 70%
425 }else if(offset
>=PIEX
-1){
427 foo
=(15+mdata
.TOP
)*foo
;
435 /* Feedback WPLUS control */
436 int feedback(int last
, int cur
, struct pid_data pid
){
437 int kp
=pid
.kp
; // Set full as 10
441 if(last
<(M
+WRIT
) || last
>(M
+WLFT
)) last
=M
;
442 if(cur
<(M
+WRIT
) || cur
>(M
+WLFT
)) return last
;
447 if(re
<(M
+WRIT
) || re
>(M
+WLFT
)) re
=last
;