Commit 42ea7193 authored by 学习的菜鸟's avatar 学习的菜鸟

减少bug

parent 59a60cb2
...@@ -86,17 +86,19 @@ void DeviceControl::updateLedBlood() { ...@@ -86,17 +86,19 @@ void DeviceControl::updateLedBlood() {
// 读取传感器值实现 // 读取传感器值实现
double DeviceControl::readSensor() { int DeviceControl::readSensor() {
int rawValue = analogRead(_sensorPin); // int rawValue = analogRead(_sensorPin);
// 将读数转换为电压(假设 ADC 12位,参考电压 3.3V 或 2.5V) // // 将读数转换为电压(假设 ADC 12位,参考电压 3.3V 或 2.5V)
// ESP32C3 ADC 可以是 12位 (0-4095),参考电压通常是 3.3V 内部或外部。 // // ESP32C3 ADC 可以是 12位 (0-4095),参考电压通常是 3.3V 内部或外部。
// 原始代码使用了 2.5,我们假设是 2.5V 参考电压,或者原始代码有误。 // // 原始代码使用了 2.5,我们假设是 2.5V 参考电压,或者原始代码有误。
// 使用 3.3V 参考电压是更常见的做法: rawValue * 3.3 / 4095.0 // // 使用 3.3V 参考电压是更常见的做法: rawValue * 3.3 / 4095.0
// 如果你的电路使用了外部 2.5V 参考或其他方式,请调整下面的乘数 // // 如果你的电路使用了外部 2.5V 参考或其他方式,请调整下面的乘数
const double referenceVoltage = 3.3; // 假设使用 3.3V 参考电压 // const double referenceVoltage = 3.3; // 假设使用 3.3V 参考电压
double voltage = (double)rawValue * referenceVoltage / 4095.0; // double voltage = (double)rawValue * referenceVoltage / 4095.0;
// Serial.printf("Sensor raw: %d, Voltage: %.2fV\n", rawValue, voltage); // 调试信息 // Serial.printf("Sensor raw: %d, Voltage: %.2fV\n", rawValue, voltage); // 调试信息
return voltage; int digitalValue = digitalRead(_sensorPin);
return (digitalValue == HIGH) ? 1 : 0;
// return voltage;
} }
// 处理来自 MQTT 的控制指令 // 处理来自 MQTT 的控制指令
...@@ -194,25 +196,29 @@ void DeviceControl::update() { ...@@ -194,25 +196,29 @@ void DeviceControl::update() {
// 简单的实现:每隔一定时间或阈值触发时发布 // 简单的实现:每隔一定时间或阈值触发时发布
// static unsigned long lastSensorPublishTime = 0; // static unsigned long lastSensorPublishTime = 0;
// const unsigned long sensorPublishInterval = 50; // 每 1 秒发布一次传感器数据 // const unsigned long sensorPublishInterval = 50; // 每 1 秒发布一次传感器数据
_shot_count++;
if(_shot_count < 6){ if(_shot_count < 6){
return ; return ;
} }
// if (millis() - lastSensorPublishTime >= sensorPublishInterval) { // if (millis() - lastSensorPublishTime >= sensorPublishInterval) {
double sensorVoltage = readSensor(); //double sensorVoltage = readSensor();
// 原始代码中的阈值判断 // 原始代码中的阈值判断
if (sensorVoltage >= 1.25&& mqttManager.wifi_connected_is()==0) { // 传感器值超过阈值 if (readSensor() == 1&& mqttManager.wifi_connected_is()==0) { // 传感器值超过阈值
if (_mqttManager) { if (_mqttManager) {
// 发布 "shoted" 消息 // 发布 "shoted" 消息
_mqttManager->publishJson("shoted", 1); _mqttManager->publishJson("shoted", 1);
Serial.print(sensorVoltage); audioManager.requestPlay();
// Serial.print(sensorVoltage);
} else { } else {
Serial.println("MQTTManager not set, cannot publish sensor data."); Serial.println("MQTTManager not set, cannot publish sensor data.");
} }
_shot_count=0;
} }
// lastSensorPublishTime = millis(); // lastSensorPublishTime = millis();
// } // }
_shot_count=0; _shot_count=9;
} }
......
...@@ -33,7 +33,7 @@ private: ...@@ -33,7 +33,7 @@ private:
void updateLedBlood(); void updateLedBlood();
// 读取传感器值 // 读取传感器值
double readSensor(); int readSensor();
public: public:
// 构造函数 // 构造函数
......
// #include <string> // For std::string
// #include "button_heartbeat.h"
// #ifndef BUTTON_HEARTBEAT_H
// #define BUTTON_HEARTBEAT_H
// class ButtonHeartbeat {
// public:
// // 按键检测常量
// const unsigned long LONG_PRESS_DURATION_MS = 5000; // 长按触发时间:5秒
// const unsigned long DEBOUNCE_DELAY_MS = 50; // 按键去抖动时间:50毫秒
// private:
// }
// #endif
\ No newline at end of file
#include <Arduino.h>
HardwareSerial mySerial(2); // UART2
#define TXD2 17
#define RXD2 16
#define RX_MAX 128
uint8_t variable[RX_MAX];
int index = 0;
typedef struct {
int16_t x;
int16_t y;
int16_t angle;
int16_t c;
int16_t line;
} PGV_H;
PGV_H Last_cal_data_hds;
// CRC16-Modbus 校验函数
uint16_t Modbus_CRC16(const uint8_t *buf, uint8_t len) {
uint16_t crc = 0xFFFF;
for (uint8_t pos = 0; pos < len; pos++) {
crc ^= (uint16_t)buf[pos];
for (uint8_t i = 0; i < 8; i++) {
if ((crc & 0x0001) != 0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
void update_data() {
if ((variable[0] == 0x01) && (variable[1] == 0x03) && (variable[2] == 0x0A)) {
uint16_t received_crc = variable[13] | (variable[14] << 8);
uint16_t calc_crc = Modbus_CRC16(variable, 13);
if (received_crc != calc_crc) {
Serial.println("CRC 校验失败!");
return;
}
Last_cal_data_hds.x = ((uint16_t)variable[3] << 8) + variable[4];
Last_cal_data_hds.y = ((uint16_t)variable[5] << 8) + variable[6];
Last_cal_data_hds.angle = ((uint16_t)variable[7] << 8) + variable[8];
Last_cal_data_hds.c = ((uint16_t)variable[9] << 8) + variable[10];
Last_cal_data_hds.line = ((uint16_t)variable[11] << 8) + variable[12];
Serial.printf("X: %d, Y: %d, Angle: %d, Color: %d, Line: %d\n",
Last_cal_data_hds.x,
Last_cal_data_hds.y,
Last_cal_data_hds.angle,
Last_cal_data_hds.c,
Last_cal_data_hds.line);
}
}
void setup() {
mySerial.begin(115200, SERIAL_8N1, RXD2, TXD2);
Serial.begin(115200);
Serial.println("PGV 系统初始化完成");
}
void loop() {
const uint8_t base_cmd[] = {0x01, 0x03, 0x00, 0x3B, 0x00, 0x06};
uint8_t full_cmd[8];
memcpy(full_cmd, base_cmd, 6);
uint16_t crc = Modbus_CRC16(full_cmd, 6);
full_cmd[6] = crc & 0xFF;
full_cmd[7] = (crc >> 8) & 0xFF;
mySerial.write(full_cmd, 8);
delay(100);
index = 0;
while (mySerial.available() && index < RX_MAX) {
variable[index++] = mySerial.read();
}
if (index >= 15) { // 13字节数据 + 2字节 CRC
update_data();
}
delay(500);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment