Files
2026-07-27 13:51:19 +08:00

47 lines
1.8 KiB
C++

#ifndef E_STOP_RELAY_H
#define E_STOP_RELAY_H
#include <ros/ros.h>
#include <serial/serial.h>
#include "e_stop_relay/EStopService.h"
class EStopRelay {
public:
EStopRelay();
void e_stop_readSerialData();
void relay_readSerialData();
void e_stop_parseModbusFrame(const std::vector<uint8_t> &frame);
void relay_parseModbusFrame(const std::vector<uint8_t> &frame);
void run();
bool handleEStopService(e_stop_relay::EStopService::Request &req, e_stop_relay::EStopService::Response &res);
bool setLightStatus(bool status);
private:
ros::NodeHandle nh_;
ros::NodeHandle private_nh_;
serial::Serial ser_;
ros::Publisher e_stop_pub_;
ros::ServiceServer e_stop_service_;
std::string port_name_;
int baudrate_;
std::string topic_name_;
std::vector<uint8_t> serial_buffer_;
ros::Time lastReceivedTime_;
std::string output_e_stop_topic_;
bool relay_status;
// 新增:重连控制参数(保留原有注释结构)
ros::Duration reconnect_interval_; // 重连间隔时间(秒)
ros::Time last_reconnect_attempt_; // 上次重连尝试的时间戳
// Modbus指令定义(原有注释保留)
const unsigned char e_stop_pressed_data[7] = {0x01, 0x04, 0x02 ,0x00, 0x01, 0x78, 0xF0};
const unsigned char e_stop_released_data[7] = {0x01 ,0x04 ,0x02 ,0x00 ,0x00 ,0xB9 ,0x30};
const unsigned char relay_turnon_cmd[13] = {0x01 ,0x10 ,0x00 ,0x01 ,0x00 ,0x02 ,0x04 ,0x00 ,0x01 ,0x00 ,0x01 ,0xA2 ,0x63};
const unsigned char relay_turnoff_cmd[13] = {0x01 ,0x10 ,0x00 ,0x01 ,0x00 ,0x02 ,0x04 ,0x00 ,0x00 ,0x00 ,0x00 ,0x32 ,0x63};
// 串口初始化函数(原有声明保留)
bool initSerial();
};
#endif // E_STOP_RELAY_H