From 6c00f2c95950f5d2a40bb9120cb05682be4c3bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=96=E5=8F=81?= Date: Wed, 1 Jan 2025 20:11:08 +0800 Subject: [PATCH] 1 --- include/consts.example.h | 3 + include/storage.h | 25 +++++- src/storage.cpp | 161 ++++++++++++++++++++++++--------------- 3 files changed, 128 insertions(+), 61 deletions(-) diff --git a/include/consts.example.h b/include/consts.example.h index 701098b..4c50d76 100644 --- a/include/consts.example.h +++ b/include/consts.example.h @@ -74,4 +74,7 @@ #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" +// 是否启用存储功能 +#define STORAGE_ENABLE 1 // 设置为 0 可以禁用存储功能 + #endif // __CONSTS_H__ diff --git a/include/storage.h b/include/storage.h index 55f1689..6c94141 100644 --- a/include/storage.h +++ b/include/storage.h @@ -2,14 +2,37 @@ #define STORAGE_H #include -#include #include "consts.h" +#if STORAGE_ENABLE +#include +#endif + class Storage { public: static bool isMounted; static void init(); + + // 默认参数定义 + static constexpr uint8_t DEFAULT_BASE_SPEED = 50; + static constexpr uint8_t DEFAULT_TURN_SPEED = 50; + static constexpr uint8_t DEFAULT_ROTATE_SENSITIVE = 3; + static constexpr unsigned int DEFAULT_SENSITIVITY = 0xFF; + static constexpr float DEFAULT_KP = 1.0f; + static constexpr float DEFAULT_KI = 0.0f; + static constexpr float DEFAULT_KD = 0.0f; + + // 当前参数(用于非存储模式) + static uint8_t currentBaseSpeed; + static uint8_t currentTurnSpeed; + static uint8_t currentRotateSensitive; + static unsigned int currentSensitivity; + static float currentKp; + static float currentKi; + static float currentKd; + static String currentName; + static void setPID(float kp, float ki, float kd); static void getPID(float &kp, float &ki, float &kd); static void setSensitivity(unsigned int sensitivity); diff --git a/src/storage.cpp b/src/storage.cpp index 310ef18..88b40f2 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -1,9 +1,18 @@ #include "storage.h" -bool Storage::isMounted; +bool Storage::isMounted = false; +uint8_t Storage::currentBaseSpeed = DEFAULT_BASE_SPEED; +uint8_t Storage::currentTurnSpeed = DEFAULT_TURN_SPEED; +uint8_t Storage::currentRotateSensitive = DEFAULT_ROTATE_SENSITIVE; +unsigned int Storage::currentSensitivity = DEFAULT_SENSITIVITY; +float Storage::currentKp = DEFAULT_KP; +float Storage::currentKi = DEFAULT_KI; +float Storage::currentKd = DEFAULT_KD; +String Storage::currentName = DEVICE_NAME; void Storage::init() { +#if STORAGE_ENABLE if (!SPIFFS.begin(true)) { Serial.println("SPIFFS Mount Failed"); @@ -14,6 +23,97 @@ void Storage::init() Serial.println("SPIFFS Mount Success"); isMounted = true; } +#else + Serial.println("Storage disabled"); + isMounted = false; +#endif +} + +void Storage::setTrackingParams(uint8_t baseSpeed, uint8_t turnSpeed, uint8_t rotateSensitive) +{ + currentBaseSpeed = baseSpeed; + currentTurnSpeed = turnSpeed; + currentRotateSensitive = rotateSensitive; + +#if STORAGE_ENABLE + if (!isMounted) return; + + File file = SPIFFS.open("/tracking.txt", "w"); + file.println(baseSpeed); + file.println(turnSpeed); + file.println(rotateSensitive); + file.close(); +#endif +} + +void Storage::getTrackingParams(uint8_t &baseSpeed, uint8_t &turnSpeed, uint8_t &rotateSensitive) +{ +#if STORAGE_ENABLE + if (!isMounted || !SPIFFS.exists("/tracking.txt")) { + baseSpeed = currentBaseSpeed; + turnSpeed = currentTurnSpeed; + rotateSensitive = currentRotateSensitive; + return; + } + + File file = SPIFFS.open("/tracking.txt", "r"); + baseSpeed = file.readStringUntil('\n').toInt(); + turnSpeed = file.readStringUntil('\n').toInt(); + rotateSensitive = file.readStringUntil('\n').toInt(); + file.close(); + + // 更新当前值 + currentBaseSpeed = baseSpeed; + currentTurnSpeed = turnSpeed; + currentRotateSensitive = rotateSensitive; +#else + baseSpeed = currentBaseSpeed; + turnSpeed = currentTurnSpeed; + rotateSensitive = currentRotateSensitive; +#endif +} + +void Storage::setPID(float kp, float ki, float kd) +{ + currentKp = kp; + currentKi = ki; + currentKd = kd; + +#if STORAGE_ENABLE + if (!isMounted) return; + + File file = SPIFFS.open("/pid.txt", "w"); + file.println(kp); + file.println(ki); + file.println(kd); + file.close(); +#endif +} + +void Storage::getPID(float &kp, float &ki, float &kd) +{ +#if STORAGE_ENABLE + if (!isMounted || !SPIFFS.exists("/pid.txt")) { + kp = currentKp; + ki = currentKi; + kd = currentKd; + return; + } + + File file = SPIFFS.open("/pid.txt", "r"); + kp = file.readStringUntil('\n').toFloat(); + ki = file.readStringUntil('\n').toFloat(); + kd = file.readStringUntil('\n').toFloat(); + file.close(); + + currentKp = kp; + currentKi = ki; + currentKd = kd; +#else + kp = currentKp; + ki = currentKi; + kd = currentKd; +#endif } unsigned int Storage::getSensitivity() @@ -41,30 +141,6 @@ void Storage::setSensitivity(unsigned int sensitivity) file.close(); } -void Storage::setPID(float kp, float ki, float kd) -{ - if (!isMounted) - return; - - File file = SPIFFS.open("/pid.txt", "w"); - file.println(kp); - file.println(ki); - file.println(kd); - file.close(); -} - -void Storage::getPID(float &kp, float &ki, float &kd) -{ - if (!isMounted) - return; - - File file = SPIFFS.open("/pid.txt", "r"); - kp = file.readStringUntil('\n').toFloat(); - ki = file.readStringUntil('\n').toFloat(); - kd = file.readStringUntil('\n').toFloat(); - file.close(); -} - void Storage::setName(String name) { if (!isMounted) @@ -86,38 +162,3 @@ String Storage::getName() return name; } - -void Storage::setTrackingParams(uint8_t baseSpeed, uint8_t turnSpeed, uint8_t rotateSensitive) -{ - if (!isMounted) - return; - - File file = SPIFFS.open("/tracking.txt", "w"); - file.println(baseSpeed); - file.println(turnSpeed); - file.println(rotateSensitive); - file.close(); -} - -void Storage::getTrackingParams(uint8_t &baseSpeed, uint8_t &turnSpeed, uint8_t &rotateSensitive) -{ - if (!isMounted) { - baseSpeed = 50; - turnSpeed = 50; - rotateSensitive = 3; - return; - } - - File file = SPIFFS.open("/tracking.txt", "r"); - if (!file) { - baseSpeed = 50; - turnSpeed = 50; - rotateSensitive = 3; - return; - } - - baseSpeed = file.readStringUntil('\n').toInt(); - turnSpeed = file.readStringUntil('\n').toInt(); - rotateSensitive = file.readStringUntil('\n').toInt(); - file.close(); -}