第一次測試智能小車行駛,見到智能小車可以自動行駛,遇到障礙物會後退轉向,雖然發現了很多地方需要改善,但是總括來說是成功的!所以智能小車將會加裝感應器和軟件改良。
智能小車第一次行駛測試 |
智能小車第一次行駛測試的程式流程圖 |
智能小車第一次行駛測試的程式:
void main(void)
{
int16 adc_value;
int16 ratio;
float steps, step1, step2;
float volts;
char dir1, dir2;
char car_stop=0;
char scale;
long int time;
lcd_init();
lcd_putc("﹨f"); // Clear Screen before
printing buffer
printf(lcd_putc, "﹨f%s﹨n","BWS US-020 v1.1");
set_tris_b(0b11001111);
dir1 = 1;
dir2 = 1;
output_bit(PIN_B4, dir1);
// output logical 1 to PIN_B4 FORWARD
output_bit(PIN_B5, dir2); // output logical 1 to PIN_B5
FORWARD
setup_ccp1(CCP_PWM);
// Setup CCP1 = PWM
setup_ccp2(CCP_PWM);
// Setup CCP2 = PWM
set_pwm1_duty(0);
// Set CCP1 PWM = 0
set_pwm2_duty(0);
// Set CCP2 PWM = 0
setup_timer_2(T2_DIV_BY_16, 255, 1);
// Setup Timer2 for PWM
while(TRUE)
{
// Trigger Ultrasonic
Sensor and Echo the refection
output_bit(PIN_E0,
1); // output logical 1 to PIN_A0
delay_us(12); // Delay 10us
output_bit(PIN_E0,
0); // output logical 1 to
PIN_A0
wait_low_to_high(); // Wait for Echo from LOW to HIGH
set_timer0(0); // Start the Timer0
wait_for_low(); // Wait for the Pulse go LOW
time = get_timer0(); // Get the time result
// Check the Ultrasonic Sensor Distance
if (time >30) {
step1 = 615; // Fixed the Speed Left 620,615,280,0
step2 = 615; // Fixed the Speed Right
dir2 = 1;
car_stop= 0;
output_bit(PIN_B5, 1);
} else
if (time <30 && time>20) {
step1 = 615; // Fixed the Speed Left 620,615,280,0
step2 = 615; // Fixed the Speed Right
dir2 = 1;
car_stop= 0;
output_bit(PIN_B5, 1);
} else
if (time <20 && time> 10)
{
step1 = 615; // Fixed the Speed Left 620,615,280,0
step2 = 615; // Fixed the Speed Right
dir2 = 1;
car_stop= 0;
output_bit(PIN_B5, 1);
} else
if (time <10) {
step1 = 0; // Fixed the Speed Left
step2 = 0; // Fixed the Speed Right
car_stop=car_stop+1;
if (car_stop > 10)
{
// Car Reverse
dir1 = 0;
dir2 = 0;
step1 = 280;
step2 = 280;
if ( dir1 == 0)
output_bit(PIN_B4, 0); // output
logical 0 to PIN_B4 Reverse
if ( dir2 == 0)
output_bit(PIN_B5, 0); // output
logical 0 to PIN_B5 Reverse
set_pwm1_duty(step1*0.392); // Fixed the
Speed
set_pwm2_duty(step2*0.392); // Fixed the
Speed
delay_ms(200);
// Car Reverse Turn Left
dir1 = 1;
dir2 = 0;
step1 = 0;
step2 = 280;
}
}
if ( dir1 == 0) {output_bit(PIN_B4, 0);}
else {output_bit(PIN_B4,
1);} // output logical 0 to PIN_B4
Reverse
if ( dir2 == 0) {output_bit(PIN_B5, 0);}
else {output_bit(PIN_B5,
1);} // output logical 0 to PIN_B5
Reverse
printf(lcd_putc, "D=%3Ld
%3.0f %3.0f %2d﹨n",
time,step1,step2,car_stop);
set_pwm1_duty(step1*0.392);
// Fixed the Speed
set_pwm2_duty(step2*0.392);
// Fixed the Speed
output_bit(PIN_A0, 0); // output logical 1 to PIN_A0
delay_ms(100);
}
} // End main
|
PWM 速度數據資料:
Step
|
Speed
|
RPM
|
620
|
Fast
|
?
|
615
|
Slow
|
?
|
280
|
Fast
|
0
|
0
|
Stop
|
0
|
智能小車距離和速度資料:
距離
|
方向
|
Step1
|
Step2
|
Dir1
|
Dir2
|
Stop
|
>30cm
|
Fast Forward
|
620
|
620
|
1
|
1
|
0
|
20 -30 cm
|
Slow Forward
|
615
|
615
|
1
|
1
|
0
|
10 – 20 cm
|
Slow Forward
|
615
|
615
|
1
|
1
|
0
|
<10mm
|
Stop
|
0
|
0
|
1
|
1
|
+1 (Count)
|
Stop Count>10
|
Reverse
|
280
|
280
|
0
|
0
|
0 (200ms)
|
Stop Count>10
|
Reverse Turn Left
|
0
|
280
|
1
|
0
|
0
|
DIY - 智能小車:第一次行駛測試 (二十七):
2013年5月6日 天氣報告
氣溫:23.7 度 @ 22:20
相對濕度:百分之90%
天氣:大致多雲
呵呵, 好棒喔!!
回覆刪除謝謝!仍然在改進中。
刪除