0

微控制器是 AVR,其中的处理器是 ATMEGA8。我写了一篇跟随黑色的文章。它像是 :

#include<avr/io.h>
#include<util/delay.h>
int main()
{
int rs,ls; // denotes the right sensor and left sensor respectively 
DDRC=0x00; // DENOTES THAT WE TAKE INPUT FROM THE SENSER
DDRB=0xFF; // DENOTES THAT PORT IS MEANT FOR THE OUTPUT FROM THE MOTOR
while(1) {
 ls=PINC&0b0000001; // last bit on
 rs=PINC&0b0001000; // 1st bit on

 if((ls==0b0000001)&(rs==0b0001000))
  PORTB=0b00010010; // forward
 if((ls==0b0000001)&(rs==0b0000000))
  PORTB=0b00000010; // right
 if( (ls==0b0000000) & (rs==0b0001000))
  PORTB=0b00010000; // left
 if((ls==0b0000000)&(rs==0b0000000) )
  PORTB=0b00010010; // stop
}
}

我正在尝试为边缘避免者写作。它具有检测黑色的相同传感器。但现在那些感应器在桌子上移动时不得不避开一个坑。我无法决定如何做到这一点。

注意:如果坑下方没有任何东西,传感器能够检测到坑。但是我如何在编写代码时遇到问题。

4

1 回答 1

0

Edge Avoider 可以有很多解决方案。我见过的一种实现是 -

解释 -

  • 如果没有(左和右)是黑色的(黑色 = 边缘,白色 = 桌面),您可以继续前进。
  • 如果任何一个/两个都是黑色的,你需要稍微后退(注意:这很棘手,在后退时,你也可能会摔倒),然后改变方向。

您需要尝试延迟,以获得适当的工作。

这些是下面需要的值(我之前省略了它们,因为它们取决于配置)-

int move_backward = 0b00000110;
int right_turn = 0b00001010;
int move_forward = 0b00001001;
int left_turn = 0b00000101;

int left_sensor_on = 0b0010000;
int right_sensor_on = 0b0100000;
int left_sensor_off = 0b0000000;
int right_sensor_off = 0b0000000;

下面的代码应该进入while (1)

left_sensor = (PINC & 0b0000001); // This depends upon your configuration
right_sensor = (PINC & 0b0001000); // So I have used, the one from your code

if ((left_sensor==left_sensor_off) & (right_sensor==right_sensor_off))
{
    PORTB = move_backward;       //move backward
    _delay_ms(50);
    PORTB=right_turn;           //take a right turn
    _delay_ms(30);
    left_sensor = 0;
    right_sensor = 0;
}
if ((left_sensor==left_sensor_on) & (right_sensor==right_sensor_on))
{
    PORTB = move_forward;        //move forward
    left_sensor = 0;
    right_sensor = 0;
} 
if ((left_sensor==left_sensor_off) & (right_sensor==right_sensor_on))
{
    PORTB = move_backward;
    _delay_ms(50);
    PORTB = right_turn;
    _delay_ms(30);
    left_sensor = 0;
    right_sensor = 0;
}

if ((left_sensor==left_sensor_on) & (right_sensor==right_sensor_off))
{
    PORTB = move_backward;
    _delay_ms(50);
    PORTB = left_turn;
    _delay_ms(30);
    left_sensor = 0;
    right_sensor = 0;
}
于 2012-09-23T07:35:48.373 回答