data.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2021, Qiayuan Liao
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *******************************************************************************/
33 
34 //
35 // Created by peter on 2021/7/17.
36 //
37 
38 #pragma once
39 
40 #include <ros/ros.h>
41 #include <unistd.h>
42 #include <serial/serial.h>
43 #include <nav_msgs/Odometry.h>
44 #include <sensor_msgs/JointState.h>
45 #include <std_msgs/Float64.h>
46 #include <std_msgs/Int8MultiArray.h>
47 #include <tf2_ros/buffer.h>
50 #include "std_msgs/UInt32.h"
51 #include "rm_msgs/VisualizeStateData.h"
52 
54 
55 #include <rm_msgs/ShootCmd.h>
56 #include <rm_msgs/ShootState.h>
57 #include <rm_msgs/DbusData.h>
58 #include <rm_msgs/StateCmd.h>
59 #include <rm_msgs/EventData.h>
60 #include <rm_msgs/GimbalCmd.h>
61 #include <rm_msgs/RobotHurt.h>
62 #include <rm_msgs/ShootData.h>
63 #include <rm_msgs/DartStatus.h>
64 #include <rm_msgs/ChassisCmd.h>
65 #include <rm_msgs/GameStatus.h>
66 #include <rm_msgs/RfidStatus.h>
67 #include <rm_msgs/EngineerUi.h>
68 #include <rm_msgs/GameRobotHp.h>
69 #include <rm_msgs/BalanceState.h>
70 #include <rm_msgs/DartClientCmd.h>
71 #include <rm_msgs/ActuatorState.h>
72 #include <rm_msgs/MapSentryData.h>
73 #include <rm_msgs/RadarMarkData.h>
74 #include <rm_msgs/PowerHeatData.h>
75 #include <rm_msgs/GimbalDesError.h>
76 #include <rm_msgs/BulletAllowance.h>
77 #include <rm_msgs/GameRobotStatus.h>
78 #include <rm_msgs/ManualToReferee.h>
79 #include <rm_msgs/ClientMapSendData.h>
80 #include <rm_msgs/RobotsPositionData.h>
81 #include <rm_msgs/DartInfo.h>
82 #include <rm_msgs/StatusChangeRequest.h>
83 #include <rm_msgs/ClientMapReceiveData.h>
84 #include <rm_msgs/SupplyProjectileAction.h>
85 #include <rm_msgs/IcraBuffDebuffZoneStatus.h>
86 #include <rm_msgs/GameRobotPosData.h>
87 #include "rm_msgs/SentryInfo.h"
88 #include "rm_msgs/SentryCmd.h"
89 #include "rm_msgs/RadarInfo.h"
90 #include "rm_msgs/Buff.h"
91 #include "rm_msgs/TrackData.h"
92 #include "rm_msgs/SentryAttackingTarget.h"
93 #include "rm_msgs/RadarToSentry.h"
94 #include <rm_msgs/PowerManagementSampleAndStatusData.h>
95 #include <rm_msgs/PowerManagementSystemExceptionData.h>
96 #include <rm_msgs/PowerManagementInitializationExceptionData.h>
97 #include <rm_msgs/PowerManagementProcessStackOverflowData.h>
98 #include <rm_msgs/PowerManagementUnknownExceptionData.h>
99 
100 namespace rm_referee
101 {
102 struct CapacityData
103 {
104  double chassis_power;
105  double limit_power;
106  double buffer_power;
107  double cap_power;
108  bool is_online = false;
109 };
110 
111 class Base
112 {
113 public:
115 
116  int client_id_ = 0; // recipient's id
117  int robot_id_ = 0; // recent robot's id
119  std::string robot_color_;
120  bool referee_data_is_online_ = false;
121 
122  void initSerial()
123  {
125  serial_.setPort("/dev/usbReferee");
126  serial_.setBaudrate(115200);
128  if (serial_.isOpen())
129  return;
130  try
131  {
132  serial_.open();
133  }
134  catch (serial::IOException& e)
135  {
136  ROS_ERROR("Cannot open referee port");
137  }
138  }
139 
140  // CRC check
141  uint8_t getCRC8CheckSum(unsigned char* pch_message, unsigned int dw_length, unsigned char uc_crc_8)
142  {
143  unsigned char uc_index;
144  while (dw_length--)
145  {
146  uc_index = uc_crc_8 ^ (*pch_message++);
147  uc_crc_8 = rm_referee::kCrc8Table[uc_index];
148  }
149  return (uc_crc_8);
150  }
151 
152  uint32_t verifyCRC8CheckSum(unsigned char* pch_message, unsigned int dw_length)
153  {
154  unsigned char uc_expected;
155  if ((pch_message == nullptr) || (dw_length <= 2))
156  return 0;
157  uc_expected = getCRC8CheckSum(pch_message, dw_length - 1, rm_referee::kCrc8Init);
158  return (uc_expected == pch_message[dw_length - 1]);
159  }
160 
161  void appendCRC8CheckSum(unsigned char* pch_message, unsigned int dw_length)
162  {
163  unsigned char uc_crc;
164  if ((pch_message == nullptr) || (dw_length <= 2))
165  return;
166  uc_crc = getCRC8CheckSum((unsigned char*)pch_message, dw_length - 1, rm_referee::kCrc8Init);
167  pch_message[dw_length - 1] = uc_crc;
168  }
169 
170  uint16_t getCRC16CheckSum(uint8_t* pch_message, uint32_t dw_length, uint16_t w_crc)
171  {
172  uint8_t chData;
173  if (pch_message == nullptr)
174  return 0xFFFF;
175  while (dw_length--)
176  {
177  chData = *pch_message++;
178  (w_crc) = (static_cast<uint16_t>(w_crc) >> 8) ^
179  rm_referee::wCRC_table[(static_cast<uint16_t>(w_crc) ^ static_cast<uint16_t>(chData)) & 0x00ff];
180  }
181  return w_crc;
182  }
183 
184  uint32_t verifyCRC16CheckSum(uint8_t* pch_message, uint32_t dw_length)
185  {
186  uint16_t w_expected;
187  if ((pch_message == nullptr) || (dw_length <= 2))
188  return 0;
189  w_expected = getCRC16CheckSum(pch_message, dw_length - 2, rm_referee::kCrc16Init);
190  return ((w_expected & 0xff) == pch_message[dw_length - 2] &&
191  ((w_expected >> 8) & 0xff) == pch_message[dw_length - 1]);
192  }
193 
194  void appendCRC16CheckSum(uint8_t* pch_message, uint32_t dw_length)
195  {
196  uint16_t wCRC;
197  if ((pch_message == nullptr) || (dw_length <= 2))
198  return;
199  wCRC = getCRC16CheckSum(static_cast<uint8_t*>(pch_message), dw_length - 2, rm_referee::kCrc16Init);
200  pch_message[dw_length - 2] = static_cast<uint8_t>((wCRC & 0x00ff));
201  pch_message[dw_length - 1] = static_cast<uint8_t>(((wCRC >> 8) & 0x00ff));
202  }
203 };
204 } // namespace rm_referee
serial::Serial::setBaudrate
void setBaudrate(uint32_t baudrate)
rm_referee::kCrc16Init
const uint16_t kCrc16Init
Definition: protocol.h:710
rm_referee::Base::robot_id_
int robot_id_
Definition: data.h:148
rm_referee::Base
Definition: data.h:142
rm_referee
Definition: data.h:100
rm_referee::kCrc8Table
const uint8_t kCrc8Table[256]
Definition: protocol.h:694
rm_referee::CapacityData::limit_power
double limit_power
Definition: data.h:167
serial::Serial::setTimeout
void setTimeout(Timeout &timeout)
ros.h
rm_referee::wCRC_table
const uint16_t wCRC_table[256]
Definition: protocol.h:711
serial.h
rm_referee::Base::client_id_
int client_id_
Definition: data.h:147
rm_referee::Base::capacity_recent_mode_
int capacity_recent_mode_
Definition: data.h:149
buffer.h
rm_referee::Base::robot_color_
std::string robot_color_
Definition: data.h:150
rm_referee::Base::appendCRC8CheckSum
void appendCRC8CheckSum(unsigned char *pch_message, unsigned int dw_length)
Definition: data.h:192
protocol.h
serial::IOException
rm_referee::Base::referee_data_is_online_
bool referee_data_is_online_
Definition: data.h:151
rm_referee::Base::initSerial
void initSerial()
Definition: data.h:153
rm_referee::CapacityData::buffer_power
double buffer_power
Definition: data.h:168
rm_referee::CapacityData::cap_power
double cap_power
Definition: data.h:169
rm_referee::CapacityData::is_online
bool is_online
Definition: data.h:170
rm_referee::Base::appendCRC16CheckSum
void appendCRC16CheckSum(uint8_t *pch_message, uint32_t dw_length)
Definition: data.h:225
timeout
timeout
serial::Timeout::simpleTimeout
static Timeout simpleTimeout(uint32_t timeout)
serial::Serial::setPort
void setPort(const std::string &port)
serial::Serial
transform_listener.h
rm_referee::Base::getCRC16CheckSum
uint16_t getCRC16CheckSum(uint8_t *pch_message, uint32_t dw_length, uint16_t w_crc)
Definition: data.h:201
serial::Serial::open
void open()
rm_referee::Base::verifyCRC16CheckSum
uint32_t verifyCRC16CheckSum(uint8_t *pch_message, uint32_t dw_length)
Definition: data.h:215
rm_referee::Base::verifyCRC8CheckSum
uint32_t verifyCRC8CheckSum(unsigned char *pch_message, unsigned int dw_length)
Definition: data.h:183
serial::Serial::isOpen
bool isOpen() const
ROS_ERROR
#define ROS_ERROR(...)
rm_referee::Base::capacity_expect_mode_
int capacity_expect_mode_
Definition: data.h:149
tf2_geometry_msgs.h
rm_referee::CapacityData
Definition: data.h:133
rm_referee::Base::serial_
serial::Serial serial_
Definition: data.h:145
rm_referee::CapacityData::chassis_power
double chassis_power
Definition: data.h:166
rm_referee::Base::getCRC8CheckSum
uint8_t getCRC8CheckSum(unsigned char *pch_message, unsigned int dw_length, unsigned char uc_crc_8)
Definition: data.h:172
rm_referee::kCrc8Init
const uint8_t kCrc8Init
Definition: protocol.h:693
serial::Timeout


rm_referee
Author(s): Qiayuan Liao
autogenerated on Tue May 6 2025 02:23:49