esp32-car/src/main.cpp

44 lines
972 B
C++
Raw Normal View History

2024-12-12 15:38:11 +08:00
#include <Arduino.h>
2024-12-27 09:47:56 +08:00
#include <motor.h>
#include <utils.h>
#include <storage.h>
#include <ble.h>
2024-12-18 14:16:02 +08:00
#include "consts.h"
2024-12-15 18:09:40 +08:00
2024-12-16 00:16:06 +08:00
// 全局变量
int currentSpeed = 0; // 当前速度
int turnOffset = 0; // 转向偏移量 (-100 到 100)
bool isMoving = false; // 运动状态
bool isTurning = false; // 转向状态
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-16 00:16:06 +08:00
// 初始化 BLE
2024-12-27 09:47:56 +08:00
BLEManager::init(DEVICE_NAME);
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();
// 初始化电机
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-12 15:38:11 +08:00
// 设置引脚模式
2024-12-15 18:09:40 +08:00
pinMode(STATUS_LED, OUTPUT);
digitalWrite(STATUS_LED, HIGH);
2024-12-19 01:08:39 +08:00
}
2024-12-15 18:09:40 +08:00
void loop()
{
2024-12-27 09:47:56 +08:00
updateStatusLED(BLEManager::deviceConnected, STATUS_LED);
if (BLEManager::deviceConnected)
2024-12-19 01:08:39 +08:00
{
2024-12-27 09:47:56 +08:00
sendStatus(*BLEManager::pTxCharacteristic);
2024-12-19 01:08:39 +08:00
}
}