RflySimSDK v4.00
RflySimSDK说明文档
载入中...
搜索中...
未找到
RflyUdpBigModeComm.hpp
1#ifndef RflyUdpBigModeComm_hpp
2#define RflyUdpBigModeComm_hpp
3
4#include <cstdint>
5#include <vector>
6#include <winsock2.h>
7#include <ws2tcpip.h>
8
9#pragma comment(lib, "Ws2_32.lib") // 链接 Windows 套接字库
10
11#define MAX_WAYPOINTS 20 // 最多支持航点数量,输出维度 = MAX_WAYPOINTS * 4
12
13#pragma pack(push, 1)
14
15// 数据包头结构体
16struct HeaderData {
17 uint16_t header; // 固定值 0xAA55
18 uint16_t packet_id; // 包编号,用于ACK确认
19 uint8_t num_points; // 本包包含的航点数量
20 uint8_t reserved; // 保留位
21};
22
23// 航点结构体,每个航点24字节
24struct Waypoint {
25 double x; // 经度或X坐标
26 double y; // 纬度或Y坐标
27 float z; // 高度
28 float yaw; // 偏航角
29};
30
31// ACK包结构体
32struct AckPacket {
33 uint16_t header; // 固定值 0xAA55
34 uint16_t packet_id; // 响应哪个数据包
35 uint8_t status; // 状态码:0x01=成功,0x02=CRC错误
36 uint8_t reserved; // 保留
37};
38
39#pragma pack(pop)
40
41// UDP接收器类
43public:
44 RflyUdpBigModeComm(); // 构造函数:初始化Winsock、绑定端口
45 ~RflyUdpBigModeComm(); // 析构函数:释放资源
46
47 bool receivePacket(); // 接收并解析UDP数据包
48 void getWaypointData(float* output); // 输出航点数据到 float 数组(供Simulink使用)
49
50private:
51 SOCKET sockfd;
52 sockaddr_in serverAddr, clientAddr;
53 int addrLen;
54
55 HeaderData header;
56 Waypoint waypoints[MAX_WAYPOINTS];
57 uint8_t recvBuffer[2048];
58 int waypointCount;
59
60 // 自定义CRC32计算函数(无需zlib)
61 uint32_t crc32Simple(const uint8_t* data, size_t len);
62 bool checkCRC(const uint8_t* data, size_t len, uint32_t received_crc);
63
64 void sendAck(uint16_t packet_id, uint8_t status); // 发送ACK响应
65};
66
67#endif
定义 RflyUdpBigModeComm.hpp:42
定义 RflyUdpBigModeComm.hpp:32
定义 RflyUdpBigModeComm.hpp:16
定义 RflyUdpBigModeComm.hpp:24