44#include <serial/serial.h>
45#include <nav_msgs/Odometry.h>
46#include <sensor_msgs/JointState.h>
47#include <std_msgs/Float64.h>
48#include <std_msgs/Int8MultiArray.h>
49#include <tf2_ros/buffer.h>
50#include <tf2_ros/transform_listener.h>
51#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
52#include "std_msgs/UInt32.h"
53#include "rm_msgs/VisualizeStateData.h"
57#include <rm_msgs/ShootCmd.h>
58#include <rm_msgs/ShootState.h>
59#include <rm_msgs/DbusData.h>
60#include <rm_msgs/StateCmd.h>
61#include <rm_msgs/EventData.h>
62#include <rm_msgs/GimbalCmd.h>
63#include <rm_msgs/RobotHurt.h>
64#include <rm_msgs/ShootData.h>
65#include <rm_msgs/DartStatus.h>
66#include <rm_msgs/ChassisCmd.h>
67#include <rm_msgs/GameStatus.h>
68#include <rm_msgs/RfidStatus.h>
69#include <rm_msgs/EngineerUi.h>
70#include <rm_msgs/GameRobotHp.h>
71#include <rm_msgs/BalanceState.h>
72#include <rm_msgs/DartClientCmd.h>
73#include <rm_msgs/ActuatorState.h>
74#include <rm_msgs/MapSentryData.h>
75#include <rm_msgs/RadarMarkData.h>
76#include <rm_msgs/PowerHeatData.h>
77#include <rm_msgs/GimbalDesError.h>
78#include <rm_msgs/BulletAllowance.h>
79#include <rm_msgs/GameRobotStatus.h>
80#include <rm_msgs/ManualToReferee.h>
81#include <rm_msgs/ClientMapSendData.h>
82#include <rm_msgs/RobotsPositionData.h>
83#include <rm_msgs/DartInfo.h>
84#include <rm_msgs/StatusChangeRequest.h>
85#include <rm_msgs/ClientMapReceiveData.h>
86#include <rm_msgs/SupplyProjectileAction.h>
87#include <rm_msgs/IcraBuffDebuffZoneStatus.h>
88#include <rm_msgs/GameRobotPosData.h>
89#include "rm_msgs/SentryInfo.h"
90#include "rm_msgs/SentryCmd.h"
91#include "rm_msgs/RadarInfo.h"
92#include "rm_msgs/RadarCmd.h"
93#include "rm_msgs/Buff.h"
94#include "rm_msgs/TrackData.h"
95#include "rm_msgs/SentryAttackingTarget.h"
96#include "rm_msgs/RadarToSentry.h"
97#include <rm_msgs/PowerManagementSampleAndStatusData.h>
98#include <rm_msgs/PowerManagementSystemExceptionData.h>
99#include <rm_msgs/PowerManagementInitializationExceptionData.h>
100#include <rm_msgs/PowerManagementProcessStackOverflowData.h>
101#include <rm_msgs/PowerManagementUnknownExceptionData.h>
141 std::vector<std::string> devices;
142 if (!nh.getParam(
"serial_ports", devices) || devices.empty())
143 devices = {
"/dev/usbReferee" };
146 ROS_WARN(
"rm_referee serial_ports size %zu exceeds max %zu, truncating", devices.size(),
kMaxSerialPorts);
150 for (
const auto& device : devices)
155 port.device = device;
156 port.configured =
true;
157 port.last_valid_frame_time = ros::Time();
158 port.next_open_attempt_time = ros::Time();
163 port.device =
"/dev/usbReferee";
164 port.configured =
true;
176 if (!port.configured)
178 if (port.serial.isOpen())
180 if (!port.next_open_attempt_time.isZero() && now < port.next_open_attempt_time)
183 serial::Timeout timeout = serial::Timeout::simpleTimeout(50);
184 port.serial.setPort(port.device);
185 port.serial.setBaudrate(115200);
186 port.serial.setTimeout(timeout);
190 port.next_open_attempt_time = ros::Time();
191 ROS_INFO_STREAM(
"Opened referee port: " << port.device);
194 catch (
const std::exception& e)
197 ROS_WARN_STREAM(
"Cannot open referee port " << port.device <<
": " << e.what());
202 void handlePortIoFailure(
size_t index,
const ros::Time& now,
const std::string& action,
const std::exception& e)
207 if (port.serial.isOpen())
209 port.last_valid_frame_time = ros::Time();
213 ROS_WARN_STREAM(
"Referee port " << port.device <<
" " << action <<
" failed: " << e.what());
216 bool isPortFresh(
size_t index,
const ros::Time& now,
const ros::Duration& freshness)
const
221 return !port.last_valid_frame_time.isZero() && (now - port.last_valid_frame_time) <= freshness;
229 bool shouldProcessPort(
size_t index,
const ros::Time& now,
const ros::Duration& freshness)
const
244 const std::string from =
247 ROS_INFO_STREAM(
"Switched active referee port from " << from <<
" to " <<
serial_ports_[index].device);
250 void markValidFrame(
size_t index,
const ros::Time& stamp,
const ros::Duration& freshness)
281 if (!port.serial.isOpen())
283 port.serial.write(data, data_len);
288 uint8_t
getCRC8CheckSum(
unsigned char* pch_message,
unsigned int dw_length,
unsigned char uc_crc_8)
290 unsigned char uc_index;
293 uc_index = uc_crc_8 ^ (*pch_message++);
301 unsigned char uc_expected;
302 if ((pch_message ==
nullptr) || (dw_length <= 2))
305 return (uc_expected == pch_message[dw_length - 1]);
310 unsigned char uc_crc;
311 if ((pch_message ==
nullptr) || (dw_length <= 2))
314 pch_message[dw_length - 1] = uc_crc;
320 if (pch_message ==
nullptr)
324 chData = *pch_message++;
325 (w_crc) = (
static_cast<uint16_t
>(w_crc) >> 8) ^
334 if ((pch_message ==
nullptr) || (dw_length <= 2))
337 return ((w_expected & 0xff) == pch_message[dw_length - 2] &&
338 ((w_expected >> 8) & 0xff) == pch_message[dw_length - 1]);
344 if ((pch_message ==
nullptr) || (dw_length <= 2))
347 pch_message[dw_length - 2] =
static_cast<uint8_t
>((wCRC & 0x00ff));
348 pch_message[dw_length - 1] =
static_cast<uint8_t
>(((wCRC >> 8) & 0x00ff));
bool ensurePortOpen(size_t index, const ros::Time &now)
Definition data.h:171
size_t serial_port_count_
Definition data.h:129
int capacity_recent_mode_
Definition data.h:135
bool writeActive(const uint8_t *data, size_t data_len)
Definition data.h:273
int client_id_
Definition data.h:133
uint8_t getCRC8CheckSum(unsigned char *pch_message, unsigned int dw_length, unsigned char uc_crc_8)
Definition data.h:288
std::string robot_color_
Definition data.h:136
int active_port_index_
Definition data.h:130
bool shouldProcessPort(size_t index, const ros::Time &now, const ros::Duration &freshness) const
Definition data.h:229
void appendCRC8CheckSum(unsigned char *pch_message, unsigned int dw_length)
Definition data.h:308
void handlePortIoFailure(size_t index, const ros::Time &now, const std::string &action, const std::exception &e)
Definition data.h:202
void appendCRC16CheckSum(uint8_t *pch_message, uint32_t dw_length)
Definition data.h:341
uint32_t verifyCRC8CheckSum(unsigned char *pch_message, unsigned int dw_length)
Definition data.h:299
bool isPortFresh(size_t index, const ros::Time &now, const ros::Duration &freshness) const
Definition data.h:216
std::array< SerialPortState, kMaxSerialPorts > serial_ports_
Definition data.h:128
void activatePort(size_t index)
Definition data.h:238
void initSerial(ros::NodeHandle &nh)
Definition data.h:139
bool referee_data_is_online_
Definition data.h:137
int robot_id_
Definition data.h:134
uint16_t getCRC16CheckSum(uint8_t *pch_message, uint32_t dw_length, uint16_t w_crc)
Definition data.h:317
int capacity_expect_mode_
Definition data.h:135
void markValidFrame(size_t index, const ros::Time &stamp, const ros::Duration &freshness)
Definition data.h:250
ros::Duration reopen_interval_
Definition data.h:131
static constexpr size_t kMaxSerialPorts
Definition data.h:117
void refreshOnlineState(const ros::Time &now, const ros::Duration &freshness)
Definition data.h:260
bool isActivePortFresh(const ros::Time &now, const ros::Duration &freshness) const
Definition data.h:224
uint32_t verifyCRC16CheckSum(uint8_t *pch_message, uint32_t dw_length)
Definition data.h:331
const uint8_t kCrc8Init
Definition protocol.h:749
const uint8_t kCrc8Table[256]
Definition protocol.h:750
const uint16_t kCrc16Init
Definition protocol.h:766
const uint16_t wCRC_table[256]
Definition protocol.h:767
ros::Time last_valid_frame_time
Definition data.h:123
bool configured
Definition data.h:125
serial::Serial serial
Definition data.h:121
std::string device
Definition data.h:122
ros::Time next_open_attempt_time
Definition data.h:124
double chassis_power
Definition data.h:107
double limit_power
Definition data.h:108
double cap_power
Definition data.h:110
bool is_online
Definition data.h:111
double buffer_power
Definition data.h:109