微控制器是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
}
}
我正在尝试为边缘避免者写作。它具有检测黑色的相同传感器。 但是现在那些感觉器必须在桌子上移动时避开一个坑。我无法决定如何做到这一点。
注意:如果凹坑下方没有任何物体,传感器可以探测到凹坑。但是我如何编写代码时遇到问题。
答案 0 :(得分: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;
}