1. 功能说明
本文示例将实现R023样机小型双轮差速底盘跟随人移动的功能。在小型双轮差速底盘前方按下图所示安装3个 近红外传感器,制作一个红外线发射源,实现当红外发射源在机器人的检测范围内任意放置或移动时,机器人能追踪该发射源。
2. 电子硬件
在这个示例中,我们采用了以下硬件,请大家参考:
主控板 | Basra主控板(兼容Arduino Uno) |
扩展板 | Bigfish2.1扩展板 |
传感器 | 近红外传感器 |
电池 | 7.4V锂电池 |
电路连接:
① 三个近红外传感器从左至右分别连接在Bigfish扩展板的A0,A4,A3端口。
② 左轮直流电机连在Bigfish扩展板的D9,D10接口;右轮直流电机连在Bigfish扩展板的D5,D6接口。
3. 功能实现
编程环境:Arduino 1.8.19
下面提供一个小型双轮差速底盘跟随人移动的参考例程(Infrareda_following.ino):
/*------------------------------------------------------------------------------------版权说明:Copyright 2023 Robottime(Beijing) Technology Co., Ltd. All Rights Reserved.Distributed under MIT license.See file LICENSE for detail or copy athttps://opensource.org/licenses/MITby 机器谱 2023-07-20 https://www.robotway.com/------------------------------*/
void Right();
void Left();
void Stop();
void Back();
void Forward();void setup()
{pinMode( 17, INPUT);pinMode( 18, INPUT);pinMode( 14, INPUT);pinMode( 10, OUTPUT);pinMode( 6, OUTPUT);pinMode( 5, OUTPUT);pinMode( 9, OUTPUT);
}void loop()
{if (!( digitalRead(18) )){Stop();}if (!( digitalRead(14) )){Right();}if (!( digitalRead(17) )){Left();}if (digitalRead(18)){Forward();}
}void Left()
{analogWrite(5 , 0);analogWrite(6 , 0);analogWrite(9 , 0);analogWrite(10 , 100);
}void Stop()
{analogWrite(5 , 0);analogWrite(6 , 0);analogWrite(9 , 0);analogWrite(10 , 0);
}void Forward()
{analogWrite(5 , 0);analogWrite(6 , 125);analogWrite(9 , 0);analogWrite(10 , 100);
}void Back()
{analogWrite(5 , 125);analogWrite(6 , 0);analogWrite(9 , 100);analogWrite(10 , 0);
}void Right()
{analogWrite(5 , 0);analogWrite(6 , 125);analogWrite(9 , 0);analogWrite(10 , 0);
}
红外跟随-程序源代码资料内容详见 小型双轮差速底盘-红外跟随