| 
 | 
沙发
 
 
 楼主 |
发表于 2025-7-15 09:03:26
|
只看该作者
 
 
 
 
#include <ESP8266WiFi.h> 
#include <ESP8266HTTPClient.h> 
 
const char* ssid = "WiFi"; 
const char* password = "WiFi密码"; 
const char* server_url = "http://派IP地址:5000/uwb"; 
 
// 基站坐标(单位:米) 
const float x1 = 2.5, y1_ = 11.6; 
const float x2 = 0.7, y2 = 0.7; 
const float x3 = 7.7, y3 = 5.7; 
 
struct TagData { 
float x, y; 
unsigned long timestamp; 
bool valid; 
} tags[3]; // 0: ID 0x11, 1: ID 0x12, 2: ID 0x13 
 
const unsigned long timeout_ms = 1000; // 最大允许时间差 
 
void setup() { 
Serial.begin(115200); 
WiFi.begin(ssid, password); 
Serial.print("连接WiFi..."); 
while (WiFi.status() != WL_CONNECTED) { 
delay(500); Serial.print("."); 
} 
Serial.println("\nWiFi连接成功!"); 
Serial.print("IP地址:"); Serial.println(WiFi.localIP()); 
 
// 初始化所有标签数据为无效 
for (int i = 0; i < 3; i++) tags[i].valid = false; 
} 
//////////////////////////////////////////////////////////////////////// 
//计算距离函数 
void computePosition(float d1, float d2, float d3, float& x, float& y) { 
float A = 2 * (x2 - x1); 
float B = 2 * (y2 - y1_); 
float C = d1*d1 - d2*d2 - x1*x1 + x2*x2 - y1_*y1_ + y2*y2; 
float D = 2 * (x3 - x2); 
float E = 2 * (y3 - y2); 
float F = d2*d2 - d3*d3 - x2*x2 + x3*x3 - y2*y2 + y3*y3; 
 
float denominator = A*E - B*D; 
if (abs(denominator) > 0.01) { 
x = (C*E - F*B) / denominator; 
y = (C*D - A*F) / (B*D - A*E); 
} else { 
x = 0; 
y = 0; 
} 
} 
 
void loop() { 
static bool sync = false; // 是否已经识别帧头 
static byte frame[16]; // 接收缓冲区,存储一帧数据 
static int index = 0; // 当前已接收字节数 
 
while (Serial.available()) { 
byte b = Serial.read(); 
 
// 寻找帧头 "mr" 
if (!sync) { 
if (b == 'm') { 
byte next = Serial.read(); // 读取下一个字节 
if (next == 'r') { 
frame[0] = 'm'; 
frame[1] = 'r'; 
sync = true; 
index = 2; 
} 
} 
} else { 
frame[index++] = b; 
if (index >= 16) { 
sync = false; 
index = 0; 
 
// 判断是否为定位帧 
// 如果帧类型是0x02(表示距离测量帧) 
if (frame[2] == 0x02) { 
byte tag_id = frame[3]; // 第四个字节是标签ID 
int tag_index = -1; 
if (tag_id == 0x11) tag_index = 0; 
else if (tag_id == 0x12) tag_index = 1; 
else if (tag_id == 0x13) tag_index = 2; 
 
if (tag_index != -1) { 
// 提取3个基站距离,单位是厘米,除以100换成米 
float d1 = ((frame[7] << 8) | frame[6]) / 100.0; 
float d2 = ((frame[9] << 8) | frame[8]) / 100.0; 
float d3 = ((frame[11] << 8) | frame[10]) / 100.0; 
 
float x, y; 
computePosition(d1, d2, d3, x, y);// 调用解算函数 
 
// 存入对应标签缓存区 
tags[tag_index].x = x; 
tags[tag_index].y = y; 
tags[tag_index].timestamp = millis();// 当前时间戳 
tags[tag_index].valid = true; // 标记为有效数据 
 
Serial.printf("标签0x%X 定位:x=%.2f, y=%.2f\n", tag_id, x, y); 
} 
//////////////////////////////////////////////////////////////////////////////////////////// 
// 检查是否三个标签都已更新,且在 timeout 时间内 
unsigned long now = millis(); 
if (tags[0].valid && tags[1].valid && tags[2].valid && 
(now - tags[0].timestamp < timeout_ms) && 
(now - tags[1].timestamp < timeout_ms) && 
(now - tags[2].timestamp < timeout_ms)) { 
 
if (WiFi.status() == WL_CONNECTED) { 
WiFiClient client; 
HTTPClient http; 
http.begin(client, server_url); 
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
 
String postData = "x=" + String(tags[0].x, 2) + ",y=" + String(tags[0].y, 2) 
+ ",x=" + String(tags[1].x, 2) + ",y=" + String(tags[1].y, 2) 
+ ",x=" + String(tags[2].x, 2) + ",y=" + String(tags[2].y, 2); 
 
int code = http.POST("data=" + postData); 
Serial.print("POST内容:"); Serial.println(postData); 
Serial.print("HTTP响应码:"); Serial.println(code); 
http.end(); 
} 
 
// 重置状态,等待下一轮三个标签 
for (int i = 0; i < 3; i++) tags[i].valid = false; 
} 
} 
} 
} 
} 
 
delay(10); // 减少CPU负载 
} 
 |   
 
 
 
 |