198 lines
5.3 KiB
C++
198 lines
5.3 KiB
C++
#ifndef MEMS_HARDWARE_SERVICE_H
|
||
#define MEMS_HARDWARE_SERVICE_H
|
||
|
||
#include <cstdint>
|
||
#include <string>
|
||
#include <vector>
|
||
#include <cstddef>
|
||
|
||
/* ================== 错误码定义 ================== */
|
||
#ifndef MEMS_ERR_OK
|
||
#define MEMS_ERR_OK 0 // 成功
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_SERIAL
|
||
#define MEMS_ERR_SERIAL -1 // 串口未打开或无效
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_SEND_CMD
|
||
#define MEMS_ERR_SEND_CMD -2 // 发送命令失败
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_NO_RESPONSE
|
||
#define MEMS_ERR_NO_RESPONSE -3 // 无响应
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_SELECT
|
||
#define MEMS_ERR_SELECT -4 // select() 系统调用失败
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_HEADER
|
||
#define MEMS_ERR_HEADER -5 // 帧头错误
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_DATA
|
||
#define MEMS_ERR_DATA -6 // 数据解析失败
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_UNKNOWN
|
||
#define MEMS_ERR_UNKNOWN -7 // 未知错误
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_LEN
|
||
#define MEMS_ERR_LEN -8 // 帧长度错误
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_CHECKSUM
|
||
#define MEMS_ERR_CHECKSUM -9 // 校验和错误
|
||
#endif
|
||
|
||
#ifndef MEMS_ERR_TIMEOUT
|
||
#define MEMS_ERR_TIMEOUT -10 // 接收数据超时
|
||
#endif
|
||
|
||
/* ================== 常量定义 ================== */
|
||
#define MEMS_FRAME_LENGTH 44
|
||
#define MAX_BUF_SIZE 256
|
||
#define MAX_RETRY_COUNT 10
|
||
|
||
/* ================== 类型定义 ================== */
|
||
|
||
// MEMS配置结构体
|
||
typedef struct {
|
||
std::string device; // 串口设备
|
||
int baudrate; // 波特率
|
||
} MemsConfig;
|
||
|
||
// MEMS输出数据帧
|
||
#pragma pack(push, 1)
|
||
typedef struct {
|
||
// 角增量数据 (rad)
|
||
float x_angle_delta;
|
||
float y_angle_delta;
|
||
float z_angle_delta;
|
||
uint8_t on_off_status; // 设备开关状态 (1=正常,0=异常)
|
||
uint16_t header_err_cnt; // 帧头错误累计
|
||
uint16_t check_err_cnt; // 校验错误累计
|
||
uint16_t length_err_cnt; // 长度错误累计
|
||
uint16_t uart_reset_cnt; // 串口复位累计
|
||
uint16_t send_cmd_err_cnt; // 发送命令错误累计
|
||
uint16_t fault_cnt; // 故障计数(所有错误的总和)
|
||
} MEMS_Info_Frame;
|
||
#pragma pack(pop)
|
||
|
||
/* ================== 设备状态结构体(内部使用)================== */
|
||
typedef struct {
|
||
int fd; // 串口文件描述符
|
||
const char *dev; // 设备节点
|
||
int baudrate; // 波特率
|
||
uint16_t header_err_cnt; // 帧头错误计数
|
||
uint16_t check_err_cnt; // 校验错误计数
|
||
uint16_t length_err_cnt; // 帧长错误计数
|
||
uint16_t uart_reset_cnt; // 串口复位计数
|
||
uint16_t send_cmd_err_cnt; // 发送命令错误计数
|
||
uint16_t fault_cnt; // 故障计数
|
||
uint16_t send_cmd_cnt; // 发送命令计数(连续无响应计数)
|
||
uint8_t on_off_status; // 上电状态
|
||
float mems_K[3]; // 标度因数
|
||
float mems_w0[3]; // 零偏
|
||
} MEMS_Device_Status;
|
||
|
||
/* ================== FastDDS接口声明 ================== */
|
||
|
||
/**
|
||
* @brief 初始化FastDDS硬件服务化
|
||
* @param domainid 域名
|
||
* @param appname 服务名称
|
||
*/
|
||
void Fastdds_init(uint8_t domainid, std::string appname);
|
||
|
||
/**
|
||
* @brief 发布MEMS遥测数据
|
||
* @param data 数据指针
|
||
* @param dest 目标地址
|
||
* @param len 数据长度
|
||
*/
|
||
void telemetry_Pub(uint8_t *data, const std::string& dest, uint16_t len);
|
||
|
||
/**
|
||
* @brief 遥测回调函数
|
||
*/
|
||
void telemetry_callback(std::string src, std::string dest, std::string type,
|
||
std::string reserve1, std::string reserve2,
|
||
std::vector<uint8_t>& data);
|
||
|
||
/* ================== 工具函数 ================== */
|
||
|
||
/**
|
||
* @brief 验证MEMS数据的校验和
|
||
* @param data 指向数据缓冲区的指针
|
||
* @param len 数据长度
|
||
* @return 错误码
|
||
*/
|
||
int mems_checksum_verify(const uint8_t *data, int len);
|
||
|
||
/**
|
||
* @brief 24位补码转int32
|
||
* @param data 24位数据指针
|
||
* @return 转换后的32位整数
|
||
*/
|
||
int32_t mems_24bit_to_int32(const uint8_t *data);
|
||
|
||
/**
|
||
* @brief 大端16位转主机字节序
|
||
*/
|
||
uint16_t mems_be16_to_host(uint8_t high, uint8_t low);
|
||
|
||
/* ================== 硬件操作函数声明 ================== */
|
||
|
||
/**
|
||
* @brief 初始化MEMS串口(奇校验)
|
||
* @param dev 串口设备路径
|
||
* @param baudrate 波特率
|
||
* @return 错误码
|
||
*/
|
||
int mems_uart_init(const char *dev, int baudrate);
|
||
|
||
/**
|
||
* @brief 发送MEMS取遥测命令
|
||
* @return 错误码
|
||
*/
|
||
int send_mems_cmd(void);
|
||
|
||
/**
|
||
* @brief 读取MEMS数据
|
||
* @param frame 输出数据帧
|
||
* @return 错误码
|
||
*/
|
||
int read_mems_data(MEMS_Info_Frame *frame);
|
||
|
||
/**
|
||
* @brief 解析MEMS原始数据
|
||
* @param buf 原始数据缓冲区
|
||
* @param info 输出解析后的信息
|
||
* @return 错误码
|
||
*/
|
||
int mems_data_extraction(const uint8_t *buf, MEMS_Info_Frame *info);
|
||
|
||
/**
|
||
* @brief 解析JSON配置文件
|
||
* @param filename 配置文件名
|
||
* @param config 输出配置信息
|
||
* @return 成功返回0,失败返回-1
|
||
*/
|
||
int parse_mems_config_file(const char* filename, MemsConfig *config);
|
||
|
||
/**
|
||
* @brief 清理硬件服务化资源
|
||
*/
|
||
void MEMS_hardware_cleanup(void);
|
||
|
||
/**
|
||
* @brief 获取错误码对应的字符串描述
|
||
* @param err 错误码
|
||
* @return 错误描述字符串
|
||
*/
|
||
const char *mems_strerror(int err);
|
||
|
||
#endif /* MEMS_HARDWARE_SERVICE_H */ |