本帖最后由 机器谱 于 2023-3-15 09:25 编辑
1. 功能说明
本实验使用的样机为R026a样机小型平行履带底盘。在样机前方安装1个 超声波传感器 ,如果遇到障碍,机器人后退、转向,否则机器人前进,实现机器人避障的效果。
2. 电子硬件
在这个示例中,我们采用了以下硬件,请大家参考:
主控板 | Basra(兼容Arduino Uno) | 扩展板 | Bigfish2.1 | 传感器 | 超声波传感器 |
两个直流电机连在D5,D6以及D9,D10接口上;超声波传感器连在A4接口上。
3. 示例程序
编程环境:Arduino 1.8.19
编写并烧录以下程序(text.ino),该程序将实现演示动图中的动作。
- /*------------------------------------------------------------------------------------
- 版权说明:Copyright 2023 Robottime(Beijing) Technology Co., Ltd. All Rights Reserved.
- Distributed under MIT license.See file LICENSE for detail or copy at
- https://opensource.org/licenses/MIT
- by 机器谱 2023-02-10 https://www.robotway.com/
- ------------------------------------------------------------------------------------*/
- int _ABVAR_1_i = 0 ;
- int ardublockUltrasonicSensorCodeAutoGeneratedReturnCM(int trigPin, int echoPin)
- {
- long duration;
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(20);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- duration = duration / 59;
- if ((duration < 2) || (duration > 300)) return false;
- return duration;
- }
- void turnLeft();
- void forward();
- void back();
- void setup()
- {
- pinMode( 10, OUTPUT);
- pinMode( 6, OUTPUT);
- pinMode( 5, OUTPUT);
- pinMode( 9, OUTPUT);
- digitalWrite( 19 , LOW );
- }
- void loop()
- {
- _ABVAR_1_i = ardublockUltrasonicSensorCodeAutoGeneratedReturnCM( 19 , 18 ) ;
- if (( ( ( _ABVAR_1_i ) <= ( 5 ) ) && ( ( _ABVAR_1_i ) > ( 0 ) ) ))
- {
- back();
- delay( 1000 );
- turnLeft();
- delay( 1500 );
- }
- else
- {
- forward();
- }
- }
- void forward()
- {
- analogWrite(5 , 0);
- analogWrite(6 , 165);
- analogWrite(9 , 0);
- analogWrite(10 , 165);
- }
- void back()
- {
- analogWrite(5 , 165);
- analogWrite(6 , 0);
- analogWrite(9 , 165);
- analogWrite(10 , 0);
- }
- void turnLeft()
- {
- analogWrite(5 , 165);
- analogWrite(6 , 0);
- analogWrite(9 , 0);
- analogWrite(10 , 165);
- }
复制代码
5. 资料内容
躲避悬崖-例程源代码
资料下载地址:https://www.robotway.com/h-col-114.html
想了解更多机器人开源项目资料请关注 机器谱网站 https://www.robotway.com |