1. 功能说明
本文示例实现的功能为:利用 探索者Birdmen手柄扩展板和 探索者NRF无线通信模块 遥控R023样机小型双轮差速底盘做出前进、后退以及转向的动作。
2. 电子硬件
在这个示例中,我们采用了以下硬件,请大家参考:
主控板 | Basra主控板(兼容Arduino Uno) x2 |
扩展板 | Bigfish2.1扩展板x1 |
Birdmen手柄扩展板x1 | |
通信 | NRF无线通信模块x2 |
电池 | 7.4V锂电池 |
3. 功能实现
编程环境:Arduino 1.8.19
3.1下载固件
分别下载master_.ino和slave_.ino到两个Basra主控板(软件解析:master_.ino为上位机程序,slave_.ino为下位机程序)。
注意:先不要堆叠NRF无线通信模块,因为会占用串口,造成下载失败。
① 参考例程(master_.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-21 https://www.robotway.com/------------------------------*/int _ABVAR_1_A0Value = 0 ;int _ABVAR_2_A1Value = 0 ;void ADGet();void SerialPrint();void setup(){Serial.begin(9600);}void loop(){ADGet();SerialPrint();delay( 100 );}void SerialPrint(){if (( ( ( _ABVAR_1_A0Value ) == ( 0 ) ) && ( ( _ABVAR_2_A1Value ) == ( 1 ) ) )){Serial.print("1");Serial.println();}if (( ( ( _ABVAR_1_A0Value ) == ( 1 ) ) && ( ( _ABVAR_2_A1Value ) == ( 0 ) ) )){Serial.print("2");Serial.println();}if (( ( ( _ABVAR_1_A0Value ) == ( 1 ) ) && ( ( _ABVAR_2_A1Value ) == ( 1 ) ) )){Serial.print("3");Serial.println();}if (( ( ( _ABVAR_1_A0Value ) == ( 1 ) ) && ( ( _ABVAR_2_A1Value ) == ( 2 ) ) )){Serial.print("4");Serial.println();}if (( ( ( _ABVAR_1_A0Value ) == ( 2 ) ) && ( ( _ABVAR_2_A1Value ) == ( 1 ) ) )){Serial.print("5");Serial.println();}}void ADGet(){_ABVAR_1_A0Value = analogRead(14) ;_ABVAR_2_A1Value = analogRead(15) ;_ABVAR_1_A0Value = map ( _ABVAR_1_A0Value , 0 , 1024 , 0 , 3 ) ;_ABVAR_2_A1Value = map ( _ABVAR_2_A1Value , 0 , 1024 , 0 , 3 ) ;}
② 参考例程(slave_.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-21 https://www.robotway.com/------------------------------*/int _ABVAR_1__data = 0 ;int _ABVAR_2__data_his = 0 ;int _ABVAR_3_0 = 0 ;void Right();void Select();void Left();void Stop();void Forward();void Back();void setup(){Serial.begin(9600);pinMode( 5 , OUTPUT);pinMode( 6 , OUTPUT);pinMode( 9 , OUTPUT);pinMode( 10 , OUTPUT);_ABVAR_1__data = 3 ;_ABVAR_2__data_his = 3 ;}void loop(){_ABVAR_1__data = Serial.parseInt() ;if (( ( _ABVAR_1__data ) > ( 0 ) )){Select();}else{_ABVAR_1__data = _ABVAR_2__data_his ;Select();}_ABVAR_2__data_his = _ABVAR_1__data ;_ABVAR_1__data = _ABVAR_3_0 ;delay( 50 );}void Left(){digitalWrite( 5 , LOW );digitalWrite( 6 , HIGH );digitalWrite( 9 , HIGH );digitalWrite( 10 , LOW );}void Stop(){digitalWrite( 5 , LOW );digitalWrite( 6 , LOW );digitalWrite( 9 , LOW );digitalWrite( 10 , LOW );}void Back(){digitalWrite( 5 , LOW );digitalWrite( 6 , HIGH );digitalWrite( 9 , LOW );digitalWrite( 10 , HIGH );}void Forward(){digitalWrite( 5 , HIGH );digitalWrite( 6 , LOW );digitalWrite( 9 , HIGH );digitalWrite( 10 , LOW );}void Select(){if (( ( _ABVAR_1__data ) == ( 1 ) )){Forward();}if (( ( _ABVAR_1__data ) == ( 2 ) )){Back();}if (( ( _ABVAR_1__data ) == ( 3 ) )){Stop();}if (( ( _ABVAR_1__data ) == ( 4 ) )){Left();}if (( ( _ABVAR_1__data ) == ( 5 ) )){Right();}}void Right(){digitalWrite( 5 , HIGH );digitalWrite( 6 , LOW );digitalWrite( 9 , LOW );digitalWrite( 10 , HIGH );}
3.2 硬件连接
① 将Birdmen手柄扩展板堆叠到下载了master_.ino的Basra主控板上,然后堆叠上NRF无线通信模块。
② 将Bigfish扩展板堆叠到下载了slave_.ino的Basra主控板上,再堆叠上NRF无线通信模块。
③ 将小型双轮差速底盘的直流电机连到Bigfish扩展板的电机接口上。
④ 用master右侧的摇杆遥控slave,调整电机的接线,直到摇杆动作和小车动作匹配。
程序源代码资料下载详见 小型双轮差速底盘-无线遥控