|
集红外遥控、巡迹、避障、超声波跟随于一体的小车。
当然啦,其实每个功能都是独立的,这里全功能的意思是一次下载程序后,可以用遥控器来选择不同的功能。
不过由于超声波用到的7和8引脚与其它传感器有冲突,所以在选择和不选择超声波功能的时候需要接上或拨下超声波的接线。
下面展示主程序框架,每个功能做成一个类于另外的文件中,有需要新加功能就新写一个类就可以啦,比较方便扩展新功能。
完整的程序见附件:
altar.zip
(9.19 KB, 下载次数: 124)
主程序:
[mw_shl_code=cpp,true]
// ----------------------------------------------------------------------------
// altar.ino
//
// Created 2016-11-26
// By seesea <seesea2517#gmail#com>
//
// altar 小车
// ----------------------------------------------------------------------------
#include "CarWithL9110.h"
#include "AvoidCrashController.h"
#include "IRRemoteController.h"
#include "ControllerManager.h"
#include "FollowLineController.h"
#include "FollowController.h"
const uint8_t LEFT_FORWARD_PIN = 5;
const uint8_t LEFT_BACK_PIN = 3;
const uint8_t RIGHT_FORWARD_PIN = 6;
const uint8_t RIGHT_BACK_PIN = 9;
const uint8_t IRREMOTE_SENSOR_PIN = A0;
const uint8_t LEFT_CRASH_SENSOR_PIN = 11;
const uint8_t RIGHT_CRASH_SENSOR_PIN = 8;
const uint8_t LEFT_FOLLOWLINE_SENSOR_PIN = 4;
const uint8_t RIGHT_FOLLOWLINE_SENSOR_PIN = 2;
const uint8_t SERVO_PIN = 10;
const uint8_t SR04_TRIGGER_PIN = 7;
const uint8_t SR04_ECHO_PIN = 8;
CarWithL9110 altar(LEFT_FORWARD_PIN, LEFT_BACK_PIN, RIGHT_FORWARD_PIN, RIGHT_BACK_PIN);
ControllerManager controllerMgr;
IRRemoteController irRemoteController(&altar, &controllerMgr, IRREMOTE_SENSOR_PIN);
AvoidCrashController avoidCrashController(&altar, LEFT_CRASH_SENSOR_PIN, RIGHT_CRASH_SENSOR_PIN);
FollowLineController followLineController(&altar, LEFT_FOLLOWLINE_SENSOR_PIN, RIGHT_FOLLOWLINE_SENSOR_PIN);
FollowController followController(&altar, SERVO_PIN, SR04_TRIGGER_PIN, SR04_ECHO_PIN);
void setup()
{
Serial.begin(9600);
altar.init();
altar.setSpeed(0);
avoidCrashController.init();
irRemoteController.init();
followLineController.init();
followController.init();
controllerMgr.registerController(&irRemoteController, ControllerManager::IRREMOTE, true);
controllerMgr.registerController(&avoidCrashController, ControllerManager::AVOID_CRASH);
controllerMgr.registerController(&followLineController, ControllerManager::FOLLOW_LINE);
controllerMgr.registerController(&followController, ControllerManager::FOLLOW);
}
void loop()
{
controllerMgr.update();
delay(20);
}
[/mw_shl_code]
视频演示:
|
|