2024-12-12 15:38:11 +08:00
|
|
|
#include <Arduino.h>
|
2024-12-27 09:57:09 +08:00
|
|
|
#include "motor.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "ble.h"
|
|
|
|
#include "led.h"
|
2024-12-27 11:32:09 +08:00
|
|
|
#include "ir.h"
|
2024-12-28 00:38:42 +08:00
|
|
|
#include "tracking.h"
|
|
|
|
#include "mode.h"
|
2024-12-18 14:16:02 +08:00
|
|
|
#include "consts.h"
|
2024-12-15 18:09:40 +08:00
|
|
|
|
2024-12-12 15:38:11 +08:00
|
|
|
void setup()
|
|
|
|
{
|
2024-12-19 19:48:52 +08:00
|
|
|
// 初始化口
|
2024-12-12 15:38:11 +08:00
|
|
|
Serial.begin(115200);
|
2024-12-15 18:09:40 +08:00
|
|
|
|
2024-12-18 16:10:26 +08:00
|
|
|
// 初始化 EEPROM
|
2024-12-27 09:47:56 +08:00
|
|
|
Storage::init();
|
|
|
|
|
2024-12-27 09:52:50 +08:00
|
|
|
// 初始化 BLE
|
2024-12-27 10:16:08 +08:00
|
|
|
BLEManager::init(Storage::getName());
|
2024-12-27 09:52:50 +08:00
|
|
|
|
2024-12-27 09:47:56 +08:00
|
|
|
// 初始化电机
|
|
|
|
MotorController::init(
|
|
|
|
{MOTOR_A_PWMA, MOTOR_A_AIN1, MOTOR_A_AIN2},
|
|
|
|
{MOTOR_B_PWMB, MOTOR_B_BIN1, MOTOR_B_BIN2},
|
|
|
|
{MOTOR_C_PWMA, MOTOR_C_AIN1, MOTOR_C_AIN2},
|
|
|
|
{MOTOR_D_PWMB, MOTOR_D_BIN1, MOTOR_D_BIN2});
|
2024-12-18 16:10:26 +08:00
|
|
|
|
2024-12-27 11:32:09 +08:00
|
|
|
// 初始化红外循迹模块
|
2025-01-01 13:21:24 +08:00
|
|
|
IR::init();
|
2024-12-27 11:32:09 +08:00
|
|
|
|
2025-01-01 19:22:37 +08:00
|
|
|
// 初始化超声波
|
|
|
|
Ultrasonic::init({US_TRIG, US_ECHO}, US_SERVO_PIN);
|
|
|
|
|
2024-12-27 09:57:09 +08:00
|
|
|
// 初始化状态灯
|
|
|
|
if (STATUS_LED_ENABLE)
|
|
|
|
{
|
|
|
|
LED::init(STATUS_LED);
|
|
|
|
}
|
2024-12-28 00:38:42 +08:00
|
|
|
|
|
|
|
// 初始化循迹控制器
|
|
|
|
TrackingController::init();
|
2024-12-19 01:08:39 +08:00
|
|
|
}
|
|
|
|
|
2024-12-15 18:09:40 +08:00
|
|
|
void loop()
|
|
|
|
{
|
2024-12-28 00:38:42 +08:00
|
|
|
// 循环 1 部分,获取传感器数据
|
2024-12-27 11:33:31 +08:00
|
|
|
IR::update();
|
2024-12-28 00:38:42 +08:00
|
|
|
|
|
|
|
// 循环 2 部分,更新状态灯
|
2024-12-27 09:57:09 +08:00
|
|
|
if (STATUS_LED_ENABLE)
|
|
|
|
{
|
|
|
|
LED::updateStatusLED(BLEManager::deviceConnected);
|
|
|
|
}
|
2024-12-28 00:38:42 +08:00
|
|
|
|
|
|
|
// 循环 3 部分,处理设备模式
|
|
|
|
Mode::processDeviceMode();
|
|
|
|
|
|
|
|
// 循环 4 部分,反馈状态
|
2024-12-27 09:47:56 +08:00
|
|
|
if (BLEManager::deviceConnected)
|
2024-12-19 01:08:39 +08:00
|
|
|
{
|
2024-12-28 00:38:42 +08:00
|
|
|
sendStatus();
|
2024-12-19 01:08:39 +08:00
|
|
|
}
|
2024-12-20 00:39:33 +08:00
|
|
|
}
|