commit 88cf8c6a7bbedd392c74f69ec7d93e5d06f8693d
Author: Robofish <1683502971@qq.com>
Date: Sun Apr 6 12:50:53 2025 +0800
RMUL2025
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b346cd7
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# QUT MOVE参加RMUL2025 哨兵代码
diff --git a/ai.sh b/ai.sh
new file mode 100755
index 0000000..a909c12
--- /dev/null
+++ b/ai.sh
@@ -0,0 +1 @@
+/bin/python3 /home/robofish/Move_AI/src/move_ai/move_ai/simple_ai_cali.py
\ No newline at end of file
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..844d82e
--- /dev/null
+++ b/build.sh
@@ -0,0 +1 @@
+colcon build --allow-overriding armor_detector armor_tracker auto_aim_interfaces rm_msgs
\ No newline at end of file
diff --git a/color.sh b/color.sh
new file mode 100644
index 0000000..5e5b825
--- /dev/null
+++ b/color.sh
@@ -0,0 +1 @@
+ros2 param set /armor_detector detect_color 1
\ No newline at end of file
diff --git a/filtered_pointcloud.pcd b/filtered_pointcloud.pcd
new file mode 100644
index 0000000..805e6d5
Binary files /dev/null and b/filtered_pointcloud.pcd differ
diff --git a/filtered_rotated_pointcloud.pcd b/filtered_rotated_pointcloud.pcd
new file mode 100644
index 0000000..70388e9
Binary files /dev/null and b/filtered_rotated_pointcloud.pcd differ
diff --git a/foxglove_bridge.sh b/foxglove_bridge.sh
new file mode 100644
index 0000000..c2566db
--- /dev/null
+++ b/foxglove_bridge.sh
@@ -0,0 +1,2 @@
+source install/setup.bash
+ros2 launch foxglove_bridge foxglove_bridge_launch.xml port:=8765
\ No newline at end of file
diff --git a/frames_2025-03-11_15.01.30.gv b/frames_2025-03-11_15.01.30.gv
new file mode 100644
index 0000000..c6c2b57
--- /dev/null
+++ b/frames_2025-03-11_15.01.30.gv
@@ -0,0 +1,15 @@
+digraph G {
+"odom" -> "lidar_odom"[label=" Broadcaster: default_authority\nAverage rate: 10000.0\nBuffer length: 0.0\nMost recent transform: 0.0\nOldest transform: 0.0\n"];
+"map" -> "odom"[label=" Broadcaster: default_authority\nAverage rate: 100.315\nBuffer length: 3.16\nMost recent transform: 1741676490.005607\nOldest transform: 1741676486.845556\n"];
+"lidar_odom" -> "base_link"[label=" Broadcaster: default_authority\nAverage rate: 10.227\nBuffer length: 4.4\nMost recent transform: 1741676488.665145\nOldest transform: 1741676484.26504\n"];
+"map" -> "goal_pose"[label=" Broadcaster: default_authority\nAverage rate: 10.217\nBuffer length: 4.6\nMost recent transform: 1741676489.937992\nOldest transform: 1741676485.337933\n"];
+"base_link" -> "wheel_1"[label=" Broadcaster: default_authority\nAverage rate: 10000.0\nBuffer length: 0.0\nMost recent transform: 0.0\nOldest transform: 0.0\n"];
+"base_link" -> "wheel_2"[label=" Broadcaster: default_authority\nAverage rate: 10000.0\nBuffer length: 0.0\nMost recent transform: 0.0\nOldest transform: 0.0\n"];
+"base_link" -> "wheel_3"[label=" Broadcaster: default_authority\nAverage rate: 10000.0\nBuffer length: 0.0\nMost recent transform: 0.0\nOldest transform: 0.0\n"];
+"base_link" -> "wheel_4"[label=" Broadcaster: default_authority\nAverage rate: 10000.0\nBuffer length: 0.0\nMost recent transform: 0.0\nOldest transform: 0.0\n"];
+"base_link" -> "livox_frame"[label=" Broadcaster: default_authority\nAverage rate: 10000.0\nBuffer length: 0.0\nMost recent transform: 0.0\nOldest transform: 0.0\n"];
+edge [style=invis];
+ subgraph cluster_legend { style=bold; color=black; label ="view_frames Result";
+"Recorded at time: 1741676490.0194058"[ shape=plaintext ] ;
+}->"map";
+}
\ No newline at end of file
diff --git a/frames_2025-03-11_15.01.30.pdf b/frames_2025-03-11_15.01.30.pdf
new file mode 100644
index 0000000..741aad0
Binary files /dev/null and b/frames_2025-03-11_15.01.30.pdf differ
diff --git a/nav_start.sh b/nav_start.sh
new file mode 100644
index 0000000..2c0cae7
--- /dev/null
+++ b/nav_start.sh
@@ -0,0 +1,14 @@
+cd /home/robofish/Move_AI
+source install/setup.bash
+commands=(
+ "ros2 launch rm_vision_bringup vision_bringup.launch.py"
+ "ros2 launch rm_serial_driver rm_serial_driver.launch.py"
+ "ros2 launch armor_tracker tracker.launch.py"
+ # "ros2 param set /armor_detector detect_color 1"
+
+)
+
+for cmd in "${commands[@]}"; do
+ gnome-terminal -- bash -c "source install/setup.bash; $cmd; exec bash"
+ sleep 0.5
+done
\ No newline at end of file
diff --git a/output.pgm b/output.pgm
new file mode 100644
index 0000000..1e6534c
Binary files /dev/null and b/output.pgm differ
diff --git a/output.yaml b/output.yaml
new file mode 100644
index 0000000..ce17042
--- /dev/null
+++ b/output.yaml
@@ -0,0 +1,9 @@
+free_thresh: 0.196
+image: output.pgm
+negate: 0
+occupied_thresh: 0.65
+origin:
+- -25.127134323120117
+- -19.90888023376465
+- 0.0
+resolution: 0.05
diff --git a/pitch.png b/pitch.png
new file mode 100644
index 0000000..2b3b98a
Binary files /dev/null and b/pitch.png differ
diff --git a/pitch.py b/pitch.py
new file mode 100644
index 0000000..c61390e
--- /dev/null
+++ b/pitch.py
@@ -0,0 +1,29 @@
+import numpy as np
+import matplotlib.pyplot as plt
+
+# 数据
+x = np.array([3.59, 2.2, 1.53, 1.07, 0.7])
+y = np.array([-0.0011, -0.0011, -0.0010, -0.0010, -0.0015])
+
+# 拟合多项式
+coefficients = np.polyfit(x, y, 2) # 二次多项式拟合
+polynomial = np.poly1d(coefficients)
+
+# 生成拟合曲线的x值
+x_fit = np.linspace(min(x), max(x), 100)
+y_fit = polynomial(x_fit)
+
+# 绘制数据点和拟合曲线
+plt.scatter(x, y, color='red', label='数据点')
+plt.plot(x_fit, y_fit, label='拟合曲线')
+
+# 添加标题和标签
+plt.title('数据拟合')
+plt.xlabel('X')
+plt.ylabel('Y')
+plt.legend()
+
+# 保存图片
+plt.savefig('pitch.png')
+#打印拟合多项式
+print(polynomial)
\ No newline at end of file
diff --git a/save_grid_map.sh b/save_grid_map.sh
new file mode 100755
index 0000000..f216fdb
--- /dev/null
+++ b/save_grid_map.sh
@@ -0,0 +1 @@
+ros2 run nav2_map_server map_saver_cli -f src/rm_nav_bringup/map/YOUR_MAP_NAME
\ No newline at end of file
diff --git a/save_pcd.sh b/save_pcd.sh
new file mode 100755
index 0000000..b2515b7
--- /dev/null
+++ b/save_pcd.sh
@@ -0,0 +1 @@
+ros2 service call /map_save std_srvs/srv/Trigger
\ No newline at end of file
diff --git a/serial.sh b/serial.sh
new file mode 100755
index 0000000..c605cf5
--- /dev/null
+++ b/serial.sh
@@ -0,0 +1 @@
+ros2 launch rm_serial_driver rm_serial_driver.launch.py
\ No newline at end of file
diff --git a/src/move_ai/move_ai/__init__.py b/src/move_ai/move_ai/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/move_ai/move_ai/plot.py b/src/move_ai/move_ai/plot.py
new file mode 100644
index 0000000..74942aa
--- /dev/null
+++ b/src/move_ai/move_ai/plot.py
@@ -0,0 +1,42 @@
+import matplotlib.pyplot as plt
+
+# 定义点的坐标和标签
+points = [
+ {'x': -0.36, 'y': 0.8, 'label': '基地补给区(启动点)0'},
+ {'x': -0.43, 'y': 1.29, 'label': '基地补给区(中心点)1'},
+ {'x': 4.82, 'y': -1.12, 'label': '中央增益区(中心点)2'},
+ {'x': 5.32, 'y': -1.12, 'label': '中央增益区(a点)3'},
+ {'x': 4.82, 'y': -0.62, 'label': '中央增益区(b点)4'},
+ {'x': 4.32, 'y': -1.12, 'label': '中央增益区(c点)5'},
+ {'x': 4.82, 'y': -1.62, 'label': '中央增益区(d点)6'},
+ {'x': 2.0, 'y': -2.5, 'label': '快速站模式途径点 7'},
+ {'x': 4.7, 'y': 1.4, 'label': '回血模式途径点 8'},
+ {'x': 4.71, 'y': 1.41, 'label': '回血模式途径点 9'},
+ {'x': -0.359, 'y': 0.81, 'label': '基地补给区(测试点)10'},
+ {'x': 4.81, 'y': -1.11, 'label': '中央增益区(测试点)11'}
+]
+
+# 创建图形
+plt.figure(figsize=(10, 8))
+
+# 绘制点
+for point in points:
+ plt.scatter(point['x'], point['y'], label=point['label'])
+
+# 添加标签
+for point in points:
+ plt.text(point['x'], point['y'], point['label'], fontsize=9, ha='right')
+
+# 设置标题和坐标轴标签
+plt.title('目标点展示')
+plt.xlabel('X 坐标')
+plt.ylabel('Y 坐标')
+
+# 显示图例
+plt.legend()
+
+# 显示图形
+plt.show()
+
+#保存图形
+plt.savefig('目标点展示.png')
\ No newline at end of file
diff --git a/src/move_ai/move_ai/simple_ai copy.py b/src/move_ai/move_ai/simple_ai copy.py
new file mode 100644
index 0000000..608e86b
--- /dev/null
+++ b/src/move_ai/move_ai/simple_ai copy.py
@@ -0,0 +1,212 @@
+import rclpy
+from rclpy.node import Node
+from rm_msgs.msg import DataRef, DataNav, MoveGoal, DataAI
+import threading
+import time
+
+SEARCH = 0b00010000
+TRACK = 0b00000000
+SHOOT = 0b00001000
+
+FAST_SPEED = 10.0
+SLOW_SPEED = 1.0
+STOP_SPEED = 0.0
+
+front = 0.0
+back = 3.14
+left = 1.57
+right = -1.57
+
+
+class ModeController(Node):
+
+ def __init__(self):
+ super().__init__('mode_controller')
+ self.subscription_ref = self.create_subscription(DataRef,'/chassis/data_ref',self.ref_callback,10)
+ self.subscription_nav = self.create_subscription(DataNav,'chassis/data_nav',self.nav_callback,10)
+ self.subscription_ai = self.create_subscription(DataAI,'gimbal_left/data_ai',self.ai_left_callback,10)
+ self.subscription_ai = self.create_subscription(DataAI,'gimbal_right/data_ai',self.ai_right_callback,10)
+ self.publisher_ = self.create_publisher(MoveGoal, '/move_goal', 10)
+ self.goal_poses = [
+ {'x': -0.36, 'y': 0.9}, # 基地补给区(启动点)0
+ # {'x': -0.43, 'y': 1.5},# 基地补给区(中心点)1
+ {'x': -0.43, 'y': 1.5},# 基地补给区(中心点)1
+ {'x': 4.82, 'y': -1.62},# 中央增益区(中心点)2
+ {'x': 5.32, 'y': -1.62},# 中央增益区(a点)3
+ {'x': 4.82, 'y': -1.12},# 中央增益区(b点)4
+ {'x': 4.32, 'y': -1.62},# 中央增益区(c点)5
+ {'x': 4.82, 'y': -2.12},# 中央增益区(d点)6
+ {'x': 1.6, 'y': -2.0},# 快速站模式途径点 7
+ {'x': 4.7, 'y': 1.7},# 回血模式途径点 8
+ {'x': 4.71, 'y': 1.71},# 回血模式途径点 9
+ {'x': -0.359, 'y': 0.81}, # 基地补给区(测试点)10
+ {'x': 4.81, 'y': -1.11},# 中央增益区(测试点)11
+ ]
+ self.game_progress = 0 # 4: 比赛开始
+ self.remain_hp = 400 # 剩余血量
+ self.reached = False # 是否到达目标点
+ self.mode = '等待比赛开始' # 模式
+ self.goal_index = 0 # 目标点索引
+ self.last_goal_pose = None # 上一个目标点
+ self.tracker_left = False # 左侧是否有目标
+ self.tracker_right = False # 右侧是否有目标
+ self.allowance_17mm = 750 # 17mm允许发弹量
+ self.countdown_thread = None
+ self.offset_x = 0.0
+ self.offset_y = 0.0
+ self.setting = False
+ self.publish_goal_pose(10, SLOW_SPEED, front, 0.1, True)
+ self.get_logger().info('Mode controller initialized')
+
+
+ def ref_callback(self, msg):
+ self.game_progress = msg.game_progress # 4: 比赛开始 # 3: 准备比赛
+ # self.game_progress = 2
+ self.remain_hp = msg.remain_hp # 剩余血量
+ self.stage_remain_time = msg.stage_remain_time # 比赛时间
+ # self.allowance_17mm = msg.allowance_17mm # 17mm允许发弹量
+ self.update_mode()
+ return
+
+ def nav_callback(self, msg):
+ if self.setting == False:
+ self.offset_x = msg.x
+ self.offset_y = msg.y
+ self.get_logger().info(f"Offset set to: x={self.offset_x}, y={self.offset_y}")
+ self.setting = True
+ return
+ else:
+ self.get_logger().info(f'mode: {self.mode}, goal_index: {self.goal_index}, reached: {msg.reached}')
+ self.reached = msg.reached # 是否到达目标点
+ #快速站点:
+ if self.goal_index == 7 and self.reached and self.mode == '快速占点':
+ self.publish_goal_pose(2, FAST_SPEED, front, 0.5, False)
+ time.sleep(0.5)
+ return
+ if self.goal_index == 2 and self.reached and self.mode == '快速占点':
+ self.mode = '站点搜敌模式'
+ self.publish_goal_pose(3, SLOW_SPEED, front, 0.1, True)
+ return
+ #站点搜敌模式:
+ if self.mode == '站点搜敌模式' :
+ if self.tracker_left or self.tracker_right:
+ self.publish_goal_pose(self.goal_index, STOP_SPEED, front, 0.1, True)
+ return
+ else:
+ if self.reached:
+ self.goal_index = self.goal_index + 1
+ if self.goal_index > 6:
+ self.goal_index = 3
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.2, True)
+ return
+ else:
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.2, True)
+ return
+
+ #站点模式:
+ if self.mode == '站点模式' and self.reached:
+ self.goal_index = self.goal_index + 1
+ if self.goal_index > 6:
+ self.goal_index = 3
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.1, True)
+ return
+
+ #回血模式:
+ if self.goal_index == 8 and self.reached and self.mode == '回血模式':
+ self.publish_goal_pose(1, FAST_SPEED, front, 0.1, True)
+ return
+ if self.goal_index == 1 and self.mode == '回血模式' and self.remain_hp >= 390:
+ self.publish_goal_pose(9, FAST_SPEED, front, 0.2, True)
+ return
+ if self.goal_index == 9 and self.reached and self.mode == '回血模式':
+ self.publish_goal_pose(2, FAST_SPEED, front, 0.1, True)
+ return
+ if self.goal_index == 2 and self.reached and self.mode == '回血模式':
+ self.mode = '站点搜敌模式'
+ self.publish_goal_pose(3, FAST_SPEED, front, 0.1, True)
+ return
+ return
+
+ def ai_left_callback(self, msg):
+ if msg.notice == SEARCH:
+ self.tracker_left = False
+ else:
+ self.tracker_left = True
+ return
+
+ def ai_right_callback(self, msg):
+ if msg.notice == SEARCH:
+ self.tracker_right = False
+ else:
+ self.tracker_right = True
+ return
+
+ def update_mode(self):
+ if self.game_progress == 4:
+ if self.mode == '等待比赛开始':
+ self.mode = '快速占点'
+ self.publish_goal_pose(7, FAST_SPEED, front, 0.6, False)
+ return
+ if self.remain_hp < 200 and self.mode != '回血模式':
+ self.mode = '回血模式'
+ self.publish_goal_pose(8, FAST_SPEED, front, 0.5, True)
+ return
+ elif self.game_progress == 3:
+ if self.countdown_thread is None:
+ self.countdown_thread = threading.Thread(target=self.start_countdown, args=(self.stage_remain_time,))
+ self.countdown_thread.start()
+ return
+ else:
+ self.publish_goal_pose(0, SLOW_SPEED, front, 0.1, False)
+ self.get_logger().info('Invalid game progress')
+ return
+
+ def start_countdown(self, countdown_time):
+ self.get_logger().info(f'Countdown started: {countdown_time} seconds remaining.')
+ while countdown_time > 0:
+ self.get_logger().info(f'Time remaining: {countdown_time} seconds.')
+ time.sleep(1)
+ countdown_time -= 1
+ self.mode = '快速占点'
+ self.publish_goal_pose(7, FAST_SPEED, front, 0.8, False)
+ self.get_logger().info('Countdown finished. Mode changed to 快速占点.')
+ return
+
+ # 发布目标点
+ def publish_goal_pose(self, index, max_speed, angle, tolerance, rotor):
+ if index < 0 or index >= len(self.goal_poses):
+ self.get_logger().error('Invalid index')
+ return
+
+ goal_pose = MoveGoal()
+ goal_pose.x = self.goal_poses[index]['x'] + self.offset_x
+ goal_pose.y = self.goal_poses[index]['y'] + self.offset_y
+ goal_pose.angle = angle
+ goal_pose.max_speed = max_speed
+ goal_pose.tolerance = tolerance
+ goal_pose.rotor = rotor
+
+ # 检查新发布的目标点信息是否与上一次发布的一样
+ if self.goal_index > 3 and self.goal_index < 6:
+ if self.last_goal_pose and self.last_goal_pose.x == goal_pose.x and self.last_goal_pose.y == goal_pose.y and self.last_goal_pose.angle == goal_pose.angle and self.last_goal_pose.max_speed == goal_pose.max_speed and self.last_goal_pose.tolerance == goal_pose.tolerance and self.last_goal_pose.rotor == goal_pose.rotor:
+ # self.get_logger().info('Goal pose is the same as the last one, not publishing.')
+ return
+ if self.goal_index == 7 or self.goal_index == 0:
+ self.publisher_.publish(goal_pose)
+ time.sleep(0.5)
+
+ self.publisher_.publish(goal_pose)
+ self.goal_index = index
+ self.last_goal_pose = goal_pose
+ self.get_logger().info(f'Published goal pose: {goal_pose}')
+ return
+
+def main(args=None):
+ rclpy.init(args=args)
+ mode_controller = ModeController()
+ rclpy.spin(mode_controller)
+ mode_controller.destroy_node()
+ rclpy.shutdown()
+
+if __name__ == '__main__':
+ main()
\ No newline at end of file
diff --git a/src/move_ai/move_ai/simple_ai.py b/src/move_ai/move_ai/simple_ai.py
new file mode 100644
index 0000000..af70806
--- /dev/null
+++ b/src/move_ai/move_ai/simple_ai.py
@@ -0,0 +1,200 @@
+import rclpy
+from rclpy.node import Node
+from rm_msgs.msg import DataRef, DataNav, MoveGoal, DataAI
+import threading
+import time
+
+SEARCH = 0b00010000
+TRACK = 0b00000000
+SHOOT = 0b00001000
+
+FAST_SPEED = 10.0
+SLOW_SPEED = 1.0
+STOP_SPEED = 0.0
+
+front = 0.0
+back = 3.14
+left = 1.57
+right = -1.57
+
+
+class ModeController(Node):
+
+ def __init__(self):
+ super().__init__('mode_controller')
+ self.subscription_ref = self.create_subscription(DataRef,'/chassis/data_ref',self.ref_callback,10)
+ self.subscription_nav = self.create_subscription(DataNav,'chassis/data_nav',self.nav_callback,10)
+ self.subscription_ai = self.create_subscription(DataAI,'gimbal_left/data_ai',self.ai_left_callback,10)
+ self.subscription_ai = self.create_subscription(DataAI,'gimbal_right/data_ai',self.ai_right_callback,10)
+ self.publisher_ = self.create_publisher(MoveGoal, '/move_goal', 10)
+ self.goal_poses = [
+ {'x': -0.36, 'y': 0.8}, # 基地补给区(启动点)0
+ {'x': -0.43, 'y': 1.5},# 基地补给区(中心点)1
+ {'x': 4.82, 'y': -1.62},# 中央增益区(中心点)2
+ {'x': 5.32, 'y': -1.62},# 中央增益区(a点)3
+ {'x': 4.82, 'y': -1.12},# 中央增益区(b点)4
+ {'x': 4.32, 'y': -1.62},# 中央增益区(c点)5
+ {'x': 4.82, 'y': -2.12},# 中央增益区(d点)6
+ {'x': 1.6, 'y': -2.0},# 快速站模式途径点 7
+ {'x': 4.7, 'y': 1.7},# 回血模式途径点 8
+ {'x': 4.71, 'y': 1.71},# 回血模式途径点 9
+ {'x': -0.359, 'y': 0.81}, # 基地补给区(测试点)10
+ {'x': 4.81, 'y': -1.11},# 中央增益区(测试点)11
+ ]
+ self.game_progress = 0 # 4: 比赛开始
+ self.remain_hp = 400 # 剩余血量
+ self.reached = False # 是否到达目标点
+ self.mode = '等待比赛开始' # 模式
+ self.goal_index = 0 # 目标点索引
+ self.last_goal_pose = None # 上一个目标点
+ self.tracker_left = False # 左侧是否有目标
+ self.tracker_right = False # 右侧是否有目标
+ self.allowance_17mm = 750 # 17mm允许发弹量
+ self.countdown_thread = None
+ self.publish_goal_pose(10, SLOW_SPEED, front, 0.1, True)
+ self.get_logger().info('Mode controller initialized')
+
+ def ref_callback(self, msg):
+ self.game_progress = msg.game_progress # 4: 比赛开始 # 3: 准备比赛
+ # self.game_progress = 4
+ self.remain_hp = msg.remain_hp # 剩余血量
+ self.stage_remain_time = msg.stage_remain_time # 比赛时间
+ # self.allowance_17mm = msg.allowance_17mm # 17mm允许发弹量
+ self.update_mode()
+ return
+
+ def nav_callback(self, msg):
+ self.get_logger().info(f'mode: {self.mode}, goal_index: {self.goal_index}, reached: {msg.reached}')
+ self.reached = msg.reached # 是否到达目标点
+ #快速站点:
+ if self.goal_index == 7 and self.reached and self.mode == '快速占点':
+ self.publish_goal_pose(2, FAST_SPEED, front, 0.5, False)
+ time.sleep(0.5)
+ return
+ if self.goal_index == 2 and self.reached and self.mode == '快速占点':
+ self.mode = '站点搜敌模式'
+ self.publish_goal_pose(3, SLOW_SPEED, front, 0.1, True)
+ return
+ #站点搜敌模式:
+ if self.mode == '站点搜敌模式' :
+ if self.tracker_left or self.tracker_right:
+ self.publish_goal_pose(self.goal_index, STOP_SPEED, front, 0.1, True)
+ return
+ else:
+ if self.reached:
+ self.goal_index = self.goal_index + 1
+ if self.goal_index > 6:
+ self.goal_index = 3
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.2, True)
+ return
+ else:
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.2, True)
+ return
+
+ #站点模式:
+ if self.mode == '站点模式' and self.reached:
+ self.goal_index = self.goal_index + 1
+ if self.goal_index > 6:
+ self.goal_index = 3
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.1, True)
+ return
+
+ #回血模式:
+ if self.goal_index == 8 and self.reached and self.mode == '回血模式':
+ self.publish_goal_pose(1, FAST_SPEED, front, 0.1, True)
+ return
+ if self.goal_index == 1 and self.mode == '回血模式' and self.remain_hp >= 390:
+ self.publish_goal_pose(9, FAST_SPEED, front, 0.2, True)
+ return
+ if self.goal_index == 9 and self.reached and self.mode == '回血模式':
+ self.publish_goal_pose(2, FAST_SPEED, front, 0.1, True)
+ return
+ if self.goal_index == 2 and self.reached and self.mode == '回血模式':
+ self.mode = '站点搜敌模式'
+ self.publish_goal_pose(3, FAST_SPEED, front, 0.1, True)
+ return
+ return
+
+ def ai_left_callback(self, msg):
+ if msg.notice == SEARCH:
+ self.tracker_left = False
+ else:
+ self.tracker_left = True
+ return
+
+ def ai_right_callback(self, msg):
+ if msg.notice == SEARCH:
+ self.tracker_right = False
+ else:
+ self.tracker_right = True
+ return
+
+ def update_mode(self):
+ if self.game_progress == 4:
+ if self.mode == '等待比赛开始':
+ self.mode = '快速占点'
+ self.publish_goal_pose(7, FAST_SPEED, front, 0.4, False)
+ return
+ if self.remain_hp < 200 and self.mode != '回血模式':
+ self.mode = '回血模式'
+ self.publish_goal_pose(8, FAST_SPEED, front, 0.5, True)
+ return
+ elif self.game_progress == 3:
+ if self.countdown_thread is None:
+ self.countdown_thread = threading.Thread(target=self.start_countdown, args=(self.stage_remain_time,))
+ self.countdown_thread.start()
+ return
+ else:
+ self.publish_goal_pose(0, SLOW_SPEED, front, 0.1, False)
+ self.get_logger().info('Invalid game progress')
+ return
+
+ def start_countdown(self, countdown_time):
+ self.get_logger().info(f'Countdown started: {countdown_time} seconds remaining.')
+ while countdown_time > 0:
+ self.get_logger().info(f'Time remaining: {countdown_time} seconds.')
+ time.sleep(1)
+ countdown_time -= 1
+ self.mode = '快速占点'
+ self.publish_goal_pose(7, FAST_SPEED, front, 0.8, False)
+ self.get_logger().info('Countdown finished. Mode changed to 快速占点.')
+ return
+
+ # 发布目标点
+ def publish_goal_pose(self, index, max_speed, angle, tolerance, rotor):
+ if index < 0 or index >= len(self.goal_poses):
+ self.get_logger().error('Invalid index')
+ return
+
+ goal_pose = MoveGoal()
+ goal_pose.x = self.goal_poses[index]['x']
+ goal_pose.y = self.goal_poses[index]['y']
+ goal_pose.angle = angle
+ goal_pose.max_speed = max_speed
+ goal_pose.tolerance = tolerance
+ goal_pose.rotor = rotor
+
+ # 检查新发布的目标点信息是否与上一次发布的一样
+ if self.goal_index > 3 and self.goal_index < 6:
+ if self.last_goal_pose and self.last_goal_pose.x == goal_pose.x and self.last_goal_pose.y == goal_pose.y and self.last_goal_pose.angle == goal_pose.angle and self.last_goal_pose.max_speed == goal_pose.max_speed and self.last_goal_pose.tolerance == goal_pose.tolerance and self.last_goal_pose.rotor == goal_pose.rotor:
+ # self.get_logger().info('Goal pose is the same as the last one, not publishing.')
+ return
+ if self.goal_index == 7 or self.goal_index == 0:
+ self.publisher_.publish(goal_pose)
+ time.sleep(0.5)
+
+ self.publisher_.publish(goal_pose)
+ self.goal_index = index
+ self.last_goal_pose = goal_pose
+ self.get_logger().info(f'Published goal pose: {goal_pose}')
+ return
+
+def main(args=None):
+ rclpy.init(args=args)
+ mode_controller = ModeController()
+ rclpy.spin(mode_controller)
+ mode_controller.destroy_node()
+ rclpy.shutdown()
+
+if __name__ == '__main__':
+ main()
\ No newline at end of file
diff --git a/src/move_ai/move_ai/simple_ai_cali.py b/src/move_ai/move_ai/simple_ai_cali.py
new file mode 100644
index 0000000..da9832e
--- /dev/null
+++ b/src/move_ai/move_ai/simple_ai_cali.py
@@ -0,0 +1,214 @@
+import rclpy
+from rclpy.node import Node
+from rm_msgs.msg import DataRef, DataNav, MoveGoal, DataAI
+import threading
+import time
+
+SEARCH = 0b00010000
+TRACK = 0b00000000
+SHOOT = 0b00001000
+
+FAST_SPEED = 10.0
+SLOW_SPEED = 1.0
+STOP_SPEED = 0.0
+
+front = 0.0
+back = 3.14
+left = 1.57
+right = -1.57
+
+
+class ModeController(Node):
+
+ def __init__(self):
+ super().__init__('mode_controller')
+ self.subscription_ref = self.create_subscription(DataRef,'/chassis/data_ref',self.ref_callback,10)
+ self.subscription_nav = self.create_subscription(DataNav,'chassis/data_nav',self.nav_callback,10)
+ self.subscription_ai = self.create_subscription(DataAI,'gimbal_left/data_ai',self.ai_left_callback,10)
+ self.subscription_ai = self.create_subscription(DataAI,'gimbal_right/data_ai',self.ai_right_callback,10)
+ self.publisher_ = self.create_publisher(MoveGoal, '/move_goal', 10)
+ self.goal_poses = [
+ {'x': -0.36, 'y': 0.9}, # 基地补给区(启动点)0
+ # {'x': -0.43, 'y': 1.5},# 基地补给区(中心点)1
+ {'x': -0.43, 'y': 1.5},# 基地补给区(中心点)1
+ {'x': 4.82, 'y': -1.62},# 中央增益区(中心点)2
+ {'x': 5.32, 'y': -1.62},# 中央增益区(a点)3
+ {'x': 4.82, 'y': -1.12},# 中央增益区(b点)4
+ {'x': 4.32, 'y': -1.62},# 中央增益区(c点)5
+ {'x': 4.82, 'y': -2.12},# 中央增益区(d点)6
+ {'x': 1.6, 'y': -2.0},# 快速站模式途径点 7
+ {'x': 4.7, 'y': 1.7},# 回血模式途径点 8
+ {'x': 4.71, 'y': 1.71},# 回血模式途径点 9
+ {'x': -0.359, 'y': 0.81}, # 基地补给区(测试点)10
+ {'x': 4.81, 'y': -1.11},# 中央增益区(测试点)11
+ ]
+ self.game_progress = 0 # 4: 比赛开始
+ self.remain_hp = 400 # 剩余血量
+ self.reached = False # 是否到达目标点
+ self.mode = '等待比赛开始' # 模式
+ self.goal_index = 0 # 目标点索引
+ self.last_goal_pose = None # 上一个目标点
+ self.tracker_left = False # 左侧是否有目标
+ self.tracker_right = False # 右侧是否有目标
+ self.allowance_17mm = 750 # 17mm允许发弹量
+ self.countdown_thread = None
+ self.offset_x = 0.0
+ self.offset_y = 0.0
+ self.setting = False
+ self.countdown_started = False
+ self.publish_goal_pose(10, SLOW_SPEED, front, 0.1, True)
+ self.get_logger().info('Mode controller initialized')
+
+
+ def ref_callback(self, msg):
+ self.game_progress = msg.game_progress # 4: 比赛开始 # 3: 准备比赛
+ # self.game_progress = 2
+ self.remain_hp = msg.remain_hp # 剩余血量
+ self.stage_remain_time = msg.stage_remain_time # 比赛时间
+ # self.allowance_17mm = msg.allowance_17mm # 17mm允许发弹量
+ self.update_mode()
+ return
+
+ def nav_callback(self, msg):
+ if self.setting == False:
+ self.offset_x = msg.x
+ self.offset_y = msg.y
+ self.get_logger().info(f"Offset set to: x={self.offset_x}, y={self.offset_y}")
+ self.setting = True
+ return
+ else:
+ self.get_logger().info(f'mode: {self.mode}, goal_index: {self.goal_index}, reached: {msg.reached}')
+ self.reached = msg.reached # 是否到达目标点
+ #快速站点:
+ if self.goal_index == 7 and self.reached and self.mode == '快速占点':
+ self.publish_goal_pose(2, FAST_SPEED, front, 0.5, False)
+ time.sleep(0.5)
+ return
+ if self.goal_index == 2 and self.reached and self.mode == '快速占点':
+ self.mode = '站点搜敌模式'
+ self.publish_goal_pose(3, SLOW_SPEED, front, 0.1, True)
+ return
+ #站点搜敌模式:
+ if self.mode == '站点搜敌模式' :
+ if self.tracker_left or self.tracker_right:
+ self.publish_goal_pose(self.goal_index, STOP_SPEED, front, 0.1, True)
+ return
+ else:
+ if self.reached:
+ self.goal_index = self.goal_index + 1
+ if self.goal_index > 6:
+ self.goal_index = 3
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.2, True)
+ return
+ else:
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.2, True)
+ return
+
+ #站点模式:
+ if self.mode == '站点模式' and self.reached:
+ self.goal_index = self.goal_index + 1
+ if self.goal_index > 6:
+ self.goal_index = 3
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.1, True)
+ return
+
+ #回血模式:
+ if self.goal_index == 8 and self.reached and self.mode == '回血模式':
+ self.publish_goal_pose(1, FAST_SPEED, front, 0.1, True)
+ return
+ if self.goal_index == 1 and self.mode == '回血模式' and self.remain_hp >= 390:
+ self.publish_goal_pose(9, FAST_SPEED, front, 0.2, True)
+ return
+ if self.goal_index == 9 and self.reached and self.mode == '回血模式':
+ self.publish_goal_pose(2, FAST_SPEED, front, 0.1, True)
+ return
+ if self.goal_index == 2 and self.reached and self.mode == '回血模式':
+ self.mode = '站点搜敌模式'
+ self.publish_goal_pose(3, FAST_SPEED, front, 0.1, True)
+ return
+ return
+
+ def ai_left_callback(self, msg):
+ if msg.notice == SEARCH:
+ self.tracker_left = False
+ else:
+ self.tracker_left = True
+ return
+
+ def ai_right_callback(self, msg):
+ if msg.notice == SEARCH:
+ self.tracker_right = False
+ else:
+ self.tracker_right = True
+ return
+
+ def update_mode(self):
+ if self.game_progress == 4:
+ if self.mode == '等待比赛开始':
+ self.mode = '快速占点'
+ self.publish_goal_pose(7, FAST_SPEED, front, 0.6, False)
+ return
+ if self.remain_hp < 200 and self.mode != '回血模式':
+ self.mode = '回血模式'
+ self.publish_goal_pose(8, FAST_SPEED, front, 0.5, True)
+ return
+ elif self.game_progress == 3:
+ if not self.countdown_started: # 检查标志位
+ self.countdown_thread = threading.Thread(target=self.start_countdown, args=(self.stage_remain_time,))
+ self.countdown_thread.start()
+ self.countdown_started = True # 设置标志位
+ return
+ else:
+ self.publish_goal_pose(0, SLOW_SPEED, front, 0.1, False)
+ self.get_logger().info('Invalid game progress')
+ return
+
+ def start_countdown(self, countdown_time):
+ self.get_logger().info(f'Countdown started: {countdown_time} seconds remaining.')
+ while countdown_time > 0:
+ self.get_logger().info(f'Time remaining: {countdown_time} seconds.')
+ time.sleep(1)
+ countdown_time -= 1
+ self.mode = '快速占点'
+ self.publish_goal_pose(7, FAST_SPEED, front, 0.8, False)
+ self.get_logger().info('Countdown finished. Mode changed to 快速占点.')
+ return
+
+ # 发布目标点
+ def publish_goal_pose(self, index, max_speed, angle, tolerance, rotor):
+ if index < 0 or index >= len(self.goal_poses):
+ self.get_logger().error('Invalid index')
+ return
+
+ goal_pose = MoveGoal()
+ goal_pose.x = self.goal_poses[index]['x'] + self.offset_x
+ goal_pose.y = self.goal_poses[index]['y'] + self.offset_y
+ goal_pose.angle = angle
+ goal_pose.max_speed = max_speed
+ goal_pose.tolerance = tolerance
+ goal_pose.rotor = rotor
+
+ # 检查新发布的目标点信息是否与上一次发布的一样
+ if self.goal_index > 3 and self.goal_index < 6:
+ if self.last_goal_pose and self.last_goal_pose.x == goal_pose.x and self.last_goal_pose.y == goal_pose.y and self.last_goal_pose.angle == goal_pose.angle and self.last_goal_pose.max_speed == goal_pose.max_speed and self.last_goal_pose.tolerance == goal_pose.tolerance and self.last_goal_pose.rotor == goal_pose.rotor:
+ # self.get_logger().info('Goal pose is the same as the last one, not publishing.')
+ return
+ if self.goal_index == 7 or self.goal_index == 0:
+ self.publisher_.publish(goal_pose)
+ time.sleep(0.5)
+
+ self.publisher_.publish(goal_pose)
+ self.goal_index = index
+ self.last_goal_pose = goal_pose
+ self.get_logger().info(f'Published goal pose: {goal_pose}')
+ return
+
+def main(args=None):
+ rclpy.init(args=args)
+ mode_controller = ModeController()
+ rclpy.spin(mode_controller)
+ mode_controller.destroy_node()
+ rclpy.shutdown()
+
+if __name__ == '__main__':
+ main()
\ No newline at end of file
diff --git a/src/move_ai/package.xml b/src/move_ai/package.xml
new file mode 100644
index 0000000..7ad530c
--- /dev/null
+++ b/src/move_ai/package.xml
@@ -0,0 +1,21 @@
+
+
+ move_ai
+ 0.0.0
+ TODO: Package description
+ Your Name
+ TODO: License declaration
+
+ ament_cmake
+ rclpy
+ rm_msgs
+ rclpy
+ rm_msgs
+
+ ament_lint_auto
+ ament_lint_common
+
+
+ ament_python
+
+
\ No newline at end of file
diff --git a/src/move_ai/resource/move_ai b/src/move_ai/resource/move_ai
new file mode 100644
index 0000000..e69de29
diff --git a/src/move_ai/setup.cfg b/src/move_ai/setup.cfg
new file mode 100644
index 0000000..0bc04a3
--- /dev/null
+++ b/src/move_ai/setup.cfg
@@ -0,0 +1,4 @@
+[develop]
+script_dir=$base/lib/move_ai
+[install]
+install_scripts=$base/lib/move_ai
diff --git a/src/move_ai/setup.py b/src/move_ai/setup.py
new file mode 100644
index 0000000..22474da
--- /dev/null
+++ b/src/move_ai/setup.py
@@ -0,0 +1,27 @@
+# filepath: /home/robofish/Move_AI/src/move_ai/setup.py
+from setuptools import setup
+
+package_name = 'move_ai'
+
+setup(
+ name=package_name,
+ version='0.0.0',
+ packages=[package_name],
+ data_files=[
+ ('share/ament_index/resource_index/packages',
+ ['resource/' + package_name]),
+ ('share/' + package_name, ['package.xml']),
+ ],
+ install_requires=['setuptools'],
+ zip_safe=True,
+ maintainer='Your Name',
+ maintainer_email='your.email@example.com',
+ description='TODO: Package description',
+ license='TODO: License declaration',
+ tests_require=['pytest'],
+ entry_points={
+ 'console_scripts': [
+ 'simple_ai = move_ai.simple_ai:main'
+ ],
+ },
+)
\ No newline at end of file
diff --git a/src/move_ai/test/test_copyright.py b/src/move_ai/test/test_copyright.py
new file mode 100644
index 0000000..97a3919
--- /dev/null
+++ b/src/move_ai/test/test_copyright.py
@@ -0,0 +1,25 @@
+# Copyright 2015 Open Source Robotics Foundation, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from ament_copyright.main import main
+import pytest
+
+
+# Remove the `skip` decorator once the source file(s) have a copyright header
+@pytest.mark.skip(reason='No copyright header has been placed in the generated source file.')
+@pytest.mark.copyright
+@pytest.mark.linter
+def test_copyright():
+ rc = main(argv=['.', 'test'])
+ assert rc == 0, 'Found errors'
diff --git a/src/move_ai/test/test_flake8.py b/src/move_ai/test/test_flake8.py
new file mode 100644
index 0000000..27ee107
--- /dev/null
+++ b/src/move_ai/test/test_flake8.py
@@ -0,0 +1,25 @@
+# Copyright 2017 Open Source Robotics Foundation, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from ament_flake8.main import main_with_errors
+import pytest
+
+
+@pytest.mark.flake8
+@pytest.mark.linter
+def test_flake8():
+ rc, errors = main_with_errors(argv=[])
+ assert rc == 0, \
+ 'Found %d code style errors / warnings:\n' % len(errors) + \
+ '\n'.join(errors)
diff --git a/src/move_ai/test/test_pep257.py b/src/move_ai/test/test_pep257.py
new file mode 100644
index 0000000..b234a38
--- /dev/null
+++ b/src/move_ai/test/test_pep257.py
@@ -0,0 +1,23 @@
+# Copyright 2015 Open Source Robotics Foundation, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from ament_pep257.main import main
+import pytest
+
+
+@pytest.mark.linter
+@pytest.mark.pep257
+def test_pep257():
+ rc = main(argv=['.', 'test'])
+ assert rc == 0, 'Found code style errors / warnings'
diff --git a/src/rm_decision/decision.py b/src/rm_decision/decision.py
new file mode 100644
index 0000000..0048c81
--- /dev/null
+++ b/src/rm_decision/decision.py
@@ -0,0 +1,166 @@
+import rclpy
+from rclpy.node import Node
+from rm_msgs.msg import DataRef, DataNav, GoalPose, DataAI
+
+SEARCH = 0b00010000
+TRACK = 0b00000000
+SHOOT = 0b00001000
+
+class ModeController(Node):
+
+ def __init__(self):
+ super().__init__('mode_controller')
+ self.subscription_ref = self.create_subscription(
+ DataRef,
+ '/chassis/data_ref',
+ self.ref_callback,
+ 10)
+ self.subscription_nav = self.create_subscription(
+ DataNav,
+ 'chassis/data_nav',
+ self.nav_callback,
+ 10)
+ self.subscription_ai = self.create_subscription(
+ DataAI,
+ 'gimbal_left/data_ai',
+ self.ai_callback,
+ 10)
+ self.subscription_ai = self.create_subscription(
+ DataAI,
+ 'gimbal_right/data_ai',
+ self.ai_callback,
+ 10)
+ self.publisher_ = self.create_publisher(GoalPose, 'goal_pose', 10)
+ self.goal_poses = [
+ {'x': 0.0, 'y': 0.0, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': False},#起点坐标右上(无陀螺)0
+ {'x': 0.0, 'y': 0.0, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#起点坐标右上(有陀螺) 1
+ {'x': 0.0, 'y': 0.0, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': False},#起点坐标左上(无陀螺)2
+ {'x': 0.0, 'y': 0.0, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#起点坐标左上(有陀螺)3
+ {'x': 0.0, 'y': 0.0, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': False},#补给点坐标(有陀螺)4
+ {'x': 2.5, 'y': -2.5, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.8, 'rotor': False},#快速上线途径点(无陀螺)5
+ {'x': 3.0, 'y': -1.0, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.5, 'rotor': False},#中心增益点坐标(无陀螺)6
+ {'x': 3.0, 'y': -1.0, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': False},#中心增益点坐标(有陀螺)7
+ {'x': 3.7, 'y': -2.1, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#a点坐标(有陀螺)8
+ {'x': 3.1, 'y': -1.7, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#b点坐标(有陀螺)9
+ {'x': 2.5, 'y': -2.5, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#c点坐标(有陀螺)10
+ {'x': 3.0, 'y': -3.2, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#d点坐标(有陀螺)11
+ {'x': 2.4, 'y': 0.1, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': False},#回城途径点坐标11(有陀螺)
+ {'x': 2.4, 'y': 0.1, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': False},#回城途径点坐标2(有陀螺)
+ {'x': 4.2, 'y': -1.6, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#爽射点坐标1(有陀螺)
+ {'x': 4.2, 'y': -1.6, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#爽射点坐标2(有陀螺)
+ {'x': 4.2, 'y': -1.6, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#狙击点坐标1(有陀螺)
+ {'x': 4.2, 'y': -1.6, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},#狙击点坐标2(有陀螺)
+ ]
+ self.game_progress = 0
+ self.remain_hp = 0
+ self.reached = False
+ self.mode = '等待比赛开始'
+ self.goal_index = 0
+ self.tracker_left = False
+ self.tracker_right = False
+ self.allowance_17mm = 750
+ self.publish_goal_pose(0)
+
+ def ref_callback(self, msg):
+ self.game_progress = msg.game_progress # 4: 比赛开始
+ self.game_progress = 4
+ self.remain_hp = msg.remain_hp # 剩余血量
+ # self.allowance_17mm = msg.allowance_17mm # 17mm允许发弹量
+ self.update_mode()
+
+ def nav_callback(self, msg):
+ self.reached = msg.reached # 是否到达目标点
+ if self.mode == '快速占点':
+ if self.goal_index == 5 and self.reached:
+ self.publish_goal_pose(6)
+ elif self.goal_index == 6 and self.reached:
+ self.publish_goal_pose(7)
+ elif self.goal_index == 7 and self.reached:
+ self.mode = '站点模式'
+ self.publish_goal_pose(8)
+ elif self.mode == '回血模式':
+ if self.goal_index == 12 and self.reached:
+ self.publish_goal_pose(4)
+ elif self.mode == '回点模式':
+ if self.goal_index == 13 and self.reached:
+ self.publish_goal_pose(6)
+ elif self.goal_index == 6 and self.reached:
+ self.mode = '站点模式'
+ self.publish_goal_pose(8)
+ elif self.mode == '站点模式':
+ # 8, 9, 10, 11循环跑
+ if self.goal_index == 8 and self.reached:
+ self.publish_goal_pose(9)
+ elif self.goal_index == 9 and self.reached:
+ self.publish_goal_pose(10)
+ elif self.goal_index == 10 and self.reached:
+ self.publish_goal_pose(11)
+ elif self.goal_index == 11 and self.reached:
+ self.publish_goal_pose(8)
+
+ def ai_callback(self, msg):
+
+
+ def update_mode(self):
+ if self.game_progress == 4:
+ if self.mode == '等待比赛开始':
+ self.mode = '快速占点'
+ self.publish_goal_pose(5)
+ elif self.remain_hp < 200 and self.mode != '回血模式':
+ self.mode = '回血模式'
+ self.publish_goal_pose(12)
+ elif self.mode == '回血模式' and self.remain_hp >= 240:
+ self.mode = '回点模式'
+ self.publish_goal_pose(13)
+ # elif self.mode == '站点模式':
+
+ else:
+ self.mode = '等待比赛开始'
+ self.get_logger().info('等待比赛开始。。。')
+
+ # self.get_logger().info(f'当前模式: {self.mode}')
+
+ def publish_goal_pose(self, index):
+ if index < 0 or index >= len(self.goal_poses):
+ self.get_logger().error('Invalid index')
+ return
+
+ goal_pose = GoalPose()
+ goal_pose.x = self.goal_poses[index]['x']
+ goal_pose.y = self.goal_poses[index]['y']
+ goal_pose.angle = self.goal_poses[index]['angle']
+ goal_pose.max_speed = self.goal_poses[index]['max_speed']
+ goal_pose.tolerance = self.goal_poses[index]['tolerance']
+ goal_pose.rotor = self.goal_poses[index]['rotor']
+
+ self.publisher_.publish(goal_pose)
+ self.goal_index = index
+ self.get_logger().info(f'Published goal pose: {goal_pose}')
+
+ # 一个可以单独控制max_speed的函数
+ def publish_goal_pose(self, index, max_speed):
+ if index < 0 or index >= len(self.goal_poses):
+ self.get_logger().error('Invalid index')
+ return
+
+ goal_pose = GoalPose()
+ goal_pose.x = self.goal_poses[index]['x']
+ goal_pose.y = self.goal_poses[index]['y']
+ goal_pose.angle = self.goal_poses[index]['angle']
+ goal_pose.max_speed = max_speed
+ goal_pose.tolerance = self.goal_poses[index]['tolerance']
+ goal_pose.rotor = self.goal_poses[index]['rotor']
+
+ self.publisher_.publish(goal_pose)
+ self.goal_index = index
+ self.get_logger().info(f'Published goal pose: {goal_pose}')
+
+def main(args=None):
+ rclpy.init(args=args)
+ mode_controller = ModeController()
+ rclpy.spin(mode_controller)
+ mode_controller.destroy_node()
+ rclpy.shutdown()
+
+if __name__ == '__main__':
+ main()
\ No newline at end of file
diff --git a/src/rm_decision/move_test.py b/src/rm_decision/move_test.py
new file mode 100644
index 0000000..105c565
--- /dev/null
+++ b/src/rm_decision/move_test.py
@@ -0,0 +1,47 @@
+import rclpy
+from rclpy.node import Node
+from rm_msgs.msg import MoveGoal
+
+class GoalPosePublisher(Node):
+ def __init__(self):
+ super().__init__('goal_pose_publisher')
+ self.publisher_ = self.create_publisher(MoveGoal, 'move_goal', 10)
+ self.goal_poses = [
+ {'x': -0.8, 'y': 0.0, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': False},
+ {'x': 2.8, 'y': -2.6, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': False},
+ {'x': 4.2, 'y': -1.6, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': False},
+ {'x': 4.2, 'y': -1.6, 'angle': 0.0, 'max_speed': 10.0, 'tolerance': 0.1, 'rotor': True},
+ ]
+
+ def publish_goal_pose(self, index):
+ if index < 0 or index >= len(self.goal_poses):
+ self.get_logger().error('Invalid index')
+ return
+
+ goal_pose = MoveGoal()
+ goal_pose.x = self.goal_poses[index]['x']
+ goal_pose.y = self.goal_poses[index]['y']
+ goal_pose.angle = self.goal_poses[index]['angle']
+ goal_pose.max_speed = self.goal_poses[index]['max_speed']
+ goal_pose.tolerance = self.goal_poses[index]['tolerance']
+ goal_pose.rotor = self.goal_poses[index]['rotor']
+
+ self.publisher_.publish(goal_pose)
+ self.get_logger().info(f'Published goal pose: {goal_pose}')
+
+def main(args=None):
+ rclpy.init(args=args)
+ node = GoalPosePublisher()
+
+ try:
+ while rclpy.ok():
+ index = int(input('Enter the index of the goal pose to publish: '))
+ node.publish_goal_pose(index)
+ except KeyboardInterrupt:
+ pass
+
+ node.destroy_node()
+ rclpy.shutdown()
+
+if __name__ == '__main__':
+ main()
\ No newline at end of file
diff --git a/src/rm_decision/simple_ai.py b/src/rm_decision/simple_ai.py
new file mode 100644
index 0000000..4db9251
--- /dev/null
+++ b/src/rm_decision/simple_ai.py
@@ -0,0 +1,190 @@
+import rclpy
+from rclpy.node import Node
+from rm_msgs.msg import DataRef, DataNav, MoveGoal, DataAI
+import threading
+import time
+
+SEARCH = 0b00010000
+TRACK = 0b00000000
+SHOOT = 0b00001000
+
+FAST_SPEED = 10.0
+SLOW_SPEED = 1.0
+STOP_SPEED = 0.0
+
+front = 0.0
+back = 3.14
+left = 1.57
+right = -1.57
+
+
+class ModeController(Node):
+
+ def __init__(self):
+ super().__init__('mode_controller')
+ self.subscription_ref = self.create_subscription(DataRef,'/chassis/data_ref',self.ref_callback,10)
+ self.subscription_nav = self.create_subscription(DataNav,'chassis/data_nav',self.nav_callback,10)
+ self.subscription_ai = self.create_subscription(DataAI,'gimbal_left/data_ai',self.ai_left_callback,10)
+ self.subscription_ai = self.create_subscription(DataAI,'gimbal_right/data_ai',self.ai_right_callback,10)
+ self.publisher_ = self.create_publisher(MoveGoal, '/move_goal', 10)
+ self.goal_poses = [
+ {'x': -0.05, 'y': -0.3}, # 基地补给区(启动点)0
+ {'x': -0.05, 'y': 0.4},# 基地补给区(中心点)1
+ {'x': 3.55, 'y': -1.8},# 中央增益区(中心点)2
+ {'x': 3.8, 'y': -1.8},# 中央增益区(a点) 3
+ {'x': 3.3, 'y': -1.0},# 中央增益区(b点) 4
+ {'x': 2.8, 'y': -1.8},# 中央增益区(c点) 5
+ {'x': 3.3, 'y': -2.3},# 中央增益区(d点) 6
+ {'x': 2.0, 'y': -2.6},# 快速站模式途径点 7
+ {'x': 1.8, 'y': 0.4},# 回血模式途径点 8
+ {'x': 1.8, 'y': 0.4},# 回血模式途径点 9
+ {'x': -0.051, 'y': -0.3}, # 基地补给区(测试点)10
+ ]
+ self.game_progress = 0 # 4: 比赛开始
+ self.remain_hp = 400 # 剩余血量
+ self.reached = False # 是否到达目标点
+ self.mode = '等待比赛开始' # 模式
+ self.goal_index = 0 # 目标点索引
+ self.last_goal_pose = None # 上一个目标点
+ self.tracker_left = False # 左侧是否有目标
+ self.tracker_right = False # 右侧是否有目标
+ self.allowance_17mm = 750 # 17mm允许发弹量
+ self.countdown_thread = None
+ self.publish_goal_pose(10, SLOW_SPEED, front, 0.1, True)
+ self.get_logger().info('Mode controller initialized')
+ # self.publish_goal_pose(10, SLOW_SPEED, front, 0.1, True)
+ # time.sleep(1)
+ # self.publish_goal_pose(0, SLOW_SPEED, front, 0.1, True)
+ # time.sleep(1)
+
+ def ref_callback(self, msg):
+ self.game_progress = msg.game_progress # 4: 比赛开始 # 3: 准备比赛
+ self.game_progress = 4
+ self.remain_hp = msg.remain_hp # 剩余血量
+ self.stage_remain_time = msg.stage_remain_time # 比赛时间
+ # self.allowance_17mm = msg.allowance_17mm # 17mm允许发弹量
+ self.update_mode()
+
+ def nav_callback(self, msg):
+ self.reached = msg.reached # 是否到达目标点
+ #快速站点:
+ if self.goal_index == 7 and self.reached and self.mode == '快速占点':
+ self.publish_goal_pose(2, FAST_SPEED, front, 0.2, False)
+ time.sleep(1)
+ if self.goal_index == 2 and self.reached and self.mode == '快速占点':
+ self.mode = '站点搜敌模式'
+ self.publish_goal_pose(3, SLOW_SPEED, front, 0.1, True)
+ #站点搜敌模式:
+ if self.mode == '站点搜敌模式' :
+ if self.tracker_left or self.tracker_right:
+ self.publish_goal_pose(self.goal_index, STOP_SPEED, front, 0.1, True)
+ else:
+ if self.reached:
+ self.goal_index = self.goal_index + 1
+ if self.goal_index > 6:
+ self.goal_index = 3
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.3, True)
+ else:
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.3, True)
+
+ #站点模式:
+ if self.mode == '站点模式' and self.reached:
+ self.goal_index = self.goal_index + 1
+ if self.goal_index > 6:
+ self.goal_index = 3
+ self.publish_goal_pose(self.goal_index, SLOW_SPEED, front, 0.1, True)
+
+ #回血模式:
+ if self.goal_index == 8 and self.reached and self.mode == '回血模式':
+ self.publish_goal_pose(1, FAST_SPEED, front, 0.1, True)
+ if self.goal_index == 1 and self.mode == '回血模式' and self.remain_hp >= 390:
+ self.publish_goal_pose(9, FAST_SPEED, front, 0.2, True)
+ if self.goal_index == 9 and self.reached and self.mode == '回血模式':
+ self.publish_goal_pose(2, FAST_SPEED, front, 0.1, True)
+ if self.goal_index == 2 and self.reached and self.mode == '回血模式':
+ # if self.allowance_17mm < 2:
+ # self.mode = '站点模式'
+ # else :
+ self.mode = '站点搜敌模式'
+ self.publish_goal_pose(3, FAST_SPEED, front, 0.1, False)
+
+ def ai_left_callback(self, msg):
+ if msg.notice == SEARCH:
+ self.tracker_left = False
+ else:
+ self.tracker_left = True
+
+ def ai_right_callback(self, msg):
+ if msg.notice == SEARCH:
+ self.tracker_right = False
+ else:
+ self.tracker_right = True
+
+ def update_mode(self):
+ if self.game_progress == 4:
+ if self.mode == '等待比赛开始':
+ self.mode = '快速占点'
+ self.publish_goal_pose(7, FAST_SPEED, front, 0.4, False)
+ if self.remain_hp < 200 and self.mode != '回血模式':
+ self.mode = '回血模式'
+ self.publish_goal_pose(8, FAST_SPEED, front, 0.5, True)
+ elif self.game_progress == 3:
+ if self.countdown_thread is None:
+ self.countdown_thread = threading.Thread(target=self.start_countdown, args=(self.stage_remain_time,))
+ self.countdown_thread.start()
+ # elif self.game_progress == 2:
+ # self.mode = '等待比赛开始'
+ # #延时1s
+ # time.sleep(1)
+ # self.publish_goal_pose(0, SLOW_SPEED, front, 0.1, True)
+ # self.get_logger().info('等待比赛开始。。。')
+ else:
+ self.publish_goal_pose(10, SLOW_SPEED, front, 0.1, True)
+ self.get_logger().info('Invalid game progress')
+
+ def start_countdown(self, countdown_time):
+ self.get_logger().info(f'Countdown started: {countdown_time} seconds remaining.')
+ while countdown_time > 0:
+ self.get_logger().info(f'Time remaining: {countdown_time} seconds.')
+ time.sleep(1)
+ countdown_time -= 1
+ self.mode = '快速占点'
+ self.publish_goal_pose(7, FAST_SPEED, front, 0.8, False)
+ self.get_logger().info('Countdown finished. Mode changed to 快速占点.')
+
+ # 发布目标点
+ def publish_goal_pose(self, index, max_speed, angle, tolerance, rotor):
+ if index < 0 or index >= len(self.goal_poses):
+ self.get_logger().error('Invalid index')
+ return
+
+ goal_pose = MoveGoal()
+ goal_pose.x = self.goal_poses[index]['x']
+ goal_pose.y = self.goal_poses[index]['y']
+ goal_pose.angle = angle
+ goal_pose.max_speed = max_speed
+ goal_pose.tolerance = tolerance
+ goal_pose.rotor = rotor
+
+ # 检查新发布的目标点信息是否与上一次发布的一样
+ if self.goal_index <3 or self.goal_index >6:
+ if self.last_goal_pose and self.last_goal_pose.x == goal_pose.x and self.last_goal_pose.y == goal_pose.y and self.last_goal_pose.angle == goal_pose.angle and self.last_goal_pose.max_speed == goal_pose.max_speed and self.last_goal_pose.tolerance == goal_pose.tolerance and self.last_goal_pose.rotor == goal_pose.rotor:
+ # self.get_logger().info('Goal pose is the same as the last one, not publishing.')
+ return
+
+ self.publisher_.publish(goal_pose)
+ # time.sleep(0.2)
+ # self.publisher_.publish(goal_pose)
+ self.goal_index = index
+ self.last_goal_pose = goal_pose
+ self.get_logger().info(f'Published goal pose: {goal_pose}')
+
+def main(args=None):
+ rclpy.init(args=args)
+ mode_controller = ModeController()
+ rclpy.spin(mode_controller)
+ mode_controller.destroy_node()
+ rclpy.shutdown()
+
+if __name__ == '__main__':
+ main()
\ No newline at end of file
diff --git a/src/rm_decision/src/simple_ai.cpp b/src/rm_decision/src/simple_ai.cpp
new file mode 100644
index 0000000..7150189
--- /dev/null
+++ b/src/rm_decision/src/simple_ai.cpp
@@ -0,0 +1,274 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+using namespace std::chrono_literals;
+
+const uint8_t SEARCH = 0b00010000;
+const uint8_t TRACK = 0b00000000;
+const uint8_t SHOOT = 0b00001000;
+
+const double FAST_SPEED = 10.0;
+const double SLOW_SPEED = 1.0;
+const double STOP_SPEED = 0.0;
+
+const double front = 0.0;
+const double back = 3.14;
+const double left = 1.57;
+const double right = -1.57;
+
+class ModeController : public rclcpp::Node
+{
+public:
+ ModeController() : Node("mode_controller")
+ {
+ subscription_ref_ = this->create_subscription(
+ "/chassis/data_ref", 10, std::bind(&ModeController::ref_callback, this, std::placeholders::_1));
+ subscription_nav_ = this->create_subscription(
+ "chassis/data_nav", 10, std::bind(&ModeController::nav_callback, this, std::placeholders::_1));
+ subscription_ai_left_ = this->create_subscription(
+ "gimbal_left/data_ai", 10, std::bind(&ModeController::ai_left_callback, this, std::placeholders::_1));
+ subscription_ai_right_ = this->create_subscription(
+ "gimbal_right/data_ai", 10, std::bind(&ModeController::ai_right_callback, this, std::placeholders::_1));
+ publisher_ = this->create_publisher("/goal", 10);
+
+ goal_poses_ = {
+ { -0.05, -0.3 }, // 基地补给区(启动点)0
+ { -0.05, 0.4 }, // 基地补给区(中心点)1
+ { 3.2, -1.8 }, // 中央增益区(中心点)2
+ { 3.8, -1.8 }, // 中央增益区(a点) 3
+ { 3.3, -1.0 }, // 中央增益区(b点) 4
+ { 2.8, -1.8 }, // 中央增益区(c点) 5
+ { 3.3, -2.3 }, // 中央增益区(d点) 6
+ { 2.0, -2.6 }, // 快速站模式途径点 7
+ { 1.8, 0.4 }, // 回血模式途径点 8
+ { 1.8, 0.4 } // 回血模式途径点 9
+ };
+
+ game_progress_ = 0; // 4: 比赛开始
+ remain_hp_ = 400; // 剩余血量
+ reached_ = false; // 是否到达目标点
+ mode_ = "等待比赛开始"; // 模式
+ goal_index_ = 0; // 目标点索引
+ last_goal_pose_ = nullptr; // 上一个目标点
+ tracker_left_ = false; // 左侧是否有目标
+ tracker_right_ = false; // 右侧是否有目标
+ allowance_17mm_ = 750; // 17mm允许发弹量
+ countdown_thread_ = nullptr;
+ publish_goal_pose(0, SLOW_SPEED, front, 0.1, true);
+ }
+
+private:
+ void ref_callback(const rm_msgs::msg::DataRef::SharedPtr msg)
+ {
+ game_progress_ = msg->game_progress; // 4: 比赛开始 // 3: 准备比赛
+ remain_hp_ = msg->remain_hp; // 剩余血量
+ stage_remain_time_ = msg->stage_remain_time; // 比赛时间
+ update_mode();
+ }
+
+ void nav_callback(const rm_msgs::msg::DataNav::SharedPtr msg)
+ {
+ reached_ = msg->reached; // 是否到达目标点
+ //快速站点:
+ if (goal_index_ == 7 && reached_ && mode_ == "快速占点")
+ {
+ publish_goal_pose(2, FAST_SPEED, front, 0.5, false);
+ }
+ if (goal_index_ == 2 && reached_ && mode_ == "快速占点")
+ {
+ mode_ = "站点搜敌模式";
+ publish_goal_pose(3, SLOW_SPEED, front, 0.1, true);
+ }
+ //站点搜敌模式:
+ if (mode_ == "站点搜敌模式")
+ {
+ if (tracker_left_ || tracker_right_)
+ {
+ publish_goal_pose(goal_index_, STOP_SPEED, front, 0.1, true);
+ }
+ else
+ {
+ if (reached_)
+ {
+ goal_index_++;
+ if (goal_index_ > 6)
+ {
+ goal_index_ = 3;
+ }
+ publish_goal_pose(goal_index_, SLOW_SPEED, front, 0.1, true);
+ }
+ else
+ {
+ publish_goal_pose(goal_index_, SLOW_SPEED, front, 0.1, true);
+ }
+ }
+ }
+
+ //站点模式:
+ if (mode_ == "站点模式" && reached_)
+ {
+ goal_index_++;
+ if (goal_index_ > 6)
+ {
+ goal_index_ = 3;
+ }
+ publish_goal_pose(goal_index_, SLOW_SPEED, front, 0.1, true);
+ }
+
+ //回血模式:
+ if (goal_index_ == 8 && reached_ && mode_ == "回血模式")
+ {
+ publish_goal_pose(1, FAST_SPEED, front, 0.1, true);
+ }
+ if (goal_index_ == 1 && mode_ == "回血模式" && remain_hp_ >= 390)
+ {
+ publish_goal_pose(9, FAST_SPEED, front, 0.1, true);
+ }
+ if (goal_index_ == 9 && reached_ && mode_ == "回血模式")
+ {
+ publish_goal_pose(2, FAST_SPEED, front, 0.1, true);
+ }
+ if (goal_index_ == 2 && reached_ && mode_ == "回血模式")
+ {
+ if (allowance_17mm_ < 2)
+ {
+ mode_ = "站点模式";
+ }
+ else
+ {
+ mode_ = "站点搜敌模式";
+ }
+ publish_goal_pose(3, FAST_SPEED, front, 0.1, true);
+ }
+ }
+
+ void ai_left_callback(const rm_msgs::msg::DataAI::SharedPtr msg)
+ {
+ if (msg->notice == SEARCH)
+ {
+ tracker_left_ = false;
+ }
+ else
+ {
+ tracker_left_ = true;
+ }
+ }
+
+ void ai_right_callback(const rm_msgs::msg::DataAI::SharedPtr msg)
+ {
+ if (msg->notice == SEARCH)
+ {
+ tracker_right_ = false;
+ }
+ else
+ {
+ tracker_right_ = true;
+ }
+ }
+
+ void update_mode()
+ {
+ if (game_progress_ == 4)
+ {
+ if (mode_ == "等待比赛开始")
+ {
+ mode_ = "快速占点";
+ publish_goal_pose(7, FAST_SPEED, front, 0.8, false);
+ }
+ if (remain_hp_ < 200 && mode_ != "回血模式")
+ {
+ mode_ = "回血模式";
+ publish_goal_pose(8, FAST_SPEED, front, 0.5, true);
+ }
+ }
+ else if (game_progress_ == 3)
+ {
+ if (countdown_thread_ == nullptr)
+ {
+ countdown_thread_ = std::make_shared(&ModeController::start_countdown, this, stage_remain_time_);
+ countdown_thread_->detach();
+ }
+ }
+ else
+ {
+ mode_ = "等待比赛开始";
+ }
+ }
+
+ void start_countdown(int countdown_time)
+ {
+ RCLCPP_INFO(this->get_logger(), "Countdown started: %d seconds remaining.", countdown_time);
+ while (countdown_time > 0)
+ {
+ RCLCPP_INFO(this->get_logger(), "Time remaining: %d seconds.", countdown_time);
+ std::this_thread::sleep_for(1s);
+ countdown_time--;
+ }
+ mode_ = "快速占点";
+ publish_goal_pose(7, FAST_SPEED, front, 0.8, false);
+ RCLCPP_INFO(this->get_logger(), "Countdown finished. Mode changed to 快速占点.");
+ }
+
+ void publish_goal_pose(int index, double max_speed, double angle, double tolerance, bool rotor)
+ {
+ if (index < 0 || index >= goal_poses_.size())
+ {
+ RCLCPP_ERROR(this->get_logger(), "Invalid index");
+ return;
+ }
+
+ auto goal_pose = rm_msgs::msg::Goal();
+ goal_pose.x = goal_poses_[index].first;
+ goal_pose.y = goal_poses_[index].second;
+ goal_pose.angle = angle;
+ goal_pose.max_speed = max_speed;
+ goal_pose.tolerance = tolerance;
+ goal_pose.rotor = rotor;
+
+ if (last_goal_pose_ && last_goal_pose_->x == goal_pose.x && last_goal_pose_->y == goal_pose.y &&
+ last_goal_pose_->angle == goal_pose.angle && last_goal_pose_->max_speed == goal_pose.max_speed &&
+ last_goal_pose_->tolerance == goal_pose.tolerance && last_goal_pose_->rotor == goal_pose.rotor)
+ {
+ return;
+ }
+
+ publisher_->publish(goal_pose);
+ goal_index_ = index;
+ last_goal_pose_ = std::make_shared(goal_pose);
+ RCLCPP_INFO(this->get_logger(), "Published goal pose: x=%f, y=%f, angle=%f, max_speed=%f, tolerance=%f, rotor=%d",
+ goal_pose.x, goal_pose.y, goal_pose.angle, goal_pose.max_speed, goal_pose.tolerance, goal_pose.rotor);
+ }
+
+ rclcpp::Subscription::SharedPtr subscription_ref_;
+ rclcpp::Subscription::SharedPtr subscription_nav_;
+ rclcpp::Subscription::SharedPtr subscription_ai_left_;
+ rclcpp::Subscription::SharedPtr subscription_ai_right_;
+ rclcpp::Publisher::SharedPtr publisher_;
+
+ std::vector> goal_poses_;
+ int game_progress_;
+ int remain_hp_;
+ bool reached_;
+ std::string mode_;
+ int goal_index_;
+ rm_msgs::msg::Goal::SharedPtr last_goal_pose_;
+ bool tracker_left_;
+ bool tracker_right_;
+ int allowance_17mm_;
+ std::shared_ptr countdown_thread_;
+ int stage_remain_time_;
+};
+
+int main(int argc, char *argv[])
+{
+ rclcpp::init(argc, argv);
+ auto node = std::make_shared();
+ rclcpp::spin(node);
+ rclcpp::shutdown();
+ return 0;
+}
\ No newline at end of file
diff --git a/src/rm_driver/livox_ros_driver2/.gitignore b/src/rm_driver/livox_ros_driver2/.gitignore
new file mode 100644
index 0000000..429bd72
--- /dev/null
+++ b/src/rm_driver/livox_ros_driver2/.gitignore
@@ -0,0 +1,10 @@
+devel/
+build/
+install/
+log/
+
+.vscode/
+__pycache__/
+.catkin_workspace
+*.gv
+*.pdf
\ No newline at end of file
diff --git a/src/rm_driver/livox_ros_driver2/LICENSE b/src/rm_driver/livox_ros_driver2/LICENSE
new file mode 100644
index 0000000..180f9d2
--- /dev/null
+++ b/src/rm_driver/livox_ros_driver2/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2023 ziknagXie
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/src/rm_driver/livox_ros_driver2/README.md b/src/rm_driver/livox_ros_driver2/README.md
new file mode 100644
index 0000000..d93cfe3
--- /dev/null
+++ b/src/rm_driver/livox_ros_driver2/README.md
@@ -0,0 +1,329 @@
+# Livox ROS Driver 2
+
+Livox ROS Driver 2 is the 2nd-generation driver package used to connect LiDAR products produced by Livox, applicable for ROS (noetic recommended) and ROS2 (foxy or humble recommended).
+
+## ATTENTION
+
+本仓库为深圳北理莫斯科大学北极熊战队内部修改版,非原版 livox_ros_driver2 。
+
+相较于原版,修改了消息发布机制和 ```MID360_config.json``` 的 ip
+
+```ros2 launch msg_MID360_launch.py``` 时会同时发布 ```CustomMsg``` 和 ```PointCloud2``` 两种类型消息。
+
+ **Topic name** | **Type** | **Note**
+:------------------------:|:-------------------------------:|:--------------:
+ /livox/lidar | livox_ros_driver2/msg/CustomMsg | mid360 自定义消息类型
+ /livox/lidar/pointcloud | sensor_msgs/msg/PointCloud2 | ROS2 点云消息格式
+ /livox/imu | sensor_msgs/msg/Imu | mid360 机内 imu
+
+## 1. Preparation
+
+### 1.1 OS requirements
+
+ * Ubuntu 22.04 for ROS2 Humble;
+
+ **Tips:**
+
+ Colcon is a build tool used in ROS2.
+
+ How to install colcon: [Colcon installation instructions](https://docs.ros.org/en/foxy/Tutorials/Beginner-Client-Libraries/Colcon-Tutorial.html)
+
+### 1.2 Install ROS2 Humble
+
+For ROS2 Humble installation, please refer to:
+[ROS Humble installation instructions](https://docs.ros.org/en/humble/Installation/Ubuntu-Install-Debians.html)
+
+Desktop-Full installation is recommend.
+
+## 2. Build & Run Livox ROS Driver 2
+
+### 2.1 Clone Livox ROS Driver 2 source code:
+
+```shell
+git clone https://gitee.com/SMBU-POLARBEAR/livox_ros_driver2_humble.git
+```
+
+ **Note :**
+
+ Be sure to clone the source code in a '[work_space]/src/' folder (as shown above), otherwise compilation errors will occur due to the compilation tool restriction.
+
+### 2.2 Build & install the Livox-SDK2
+
+ **Note :**
+
+ Please follow the guidance of installation in the [Livox-SDK2/README.md](https://github.com/Livox-SDK/Livox-SDK2/blob/master/README.md)
+
+### 2.3 Build the Livox ROS Driver 2:
+
+### For ROS2 Humble:
+```shell
+colcon build --symlink-install
+```
+
+### 2.4 Run Livox ROS Driver 2:
+
+```shell
+source install/setup.sh
+ros2 launch livox_ros_driver2 [launch file]
+```
+
+in which,
+
+* **[launch file]** : is the ROS2 launch file you want to use; the 'launch_ROS2' folder contains several launch samples for your reference.
+
+A rviz launch example for HAP LiDAR would be:
+
+```shell
+ros2 launch livox_ros_driver2 rviz_HAP_launch.py
+```
+
+## 3. Launch file and livox_ros_driver2 internal parameter configuration instructions
+
+### 3.1 Launch file configuration instructions
+
+Launch files of ROS are in the "ws_livox/src/livox_ros_driver2/launch_ROS1" directory and launch files of ROS2 are in the "ws_livox/src/livox_ros_driver2/launch_ROS2" directory. Different launch files have different configuration parameter values and are used in different scenarios:
+
+| launch file name | Description |
+| ------------------------- | ------------------------------------------------------------ |
+| rviz_HAP.launch | Connect to HAP LiDAR device
Publish pointcloud2 format data
Autoload rviz |
+| msg_HAP.launch | Connect to HAP LiDAR device
Publish livox customized pointcloud data|
+| rviz_MID360.launch | Connect to MID360 LiDAR device
Publish pointcloud2 format data
Autoload rviz|
+| msg_MID360.launch | Connect to MID360 LiDAR device
Publish livox customized pointcloud data |
+| rviz_mixed.launch | Connect to HAP and MID360 LiDAR device
Publish pointcloud2 format data
Autoload rviz|
+| msg_mixed.launch | Connect to HAP and MID360 LiDAR device
Publish livox customized pointcloud data |
+
+### 3.2 Livox ros driver 2 internal main parameter configuration instructions
+
+All internal parameters of Livox_ros_driver2 are in the launch file. Below are detailed descriptions of the three commonly used parameters :
+
+| Parameter | Detailed description | Default |
+| ------------ | ------------------------------------------------------------ | ------- |
+| publish_freq | Set the frequency of point cloud publish
Floating-point data type, recommended values 5.0, 10.0, 20.0, 50.0, etc. The maximum publish frequency is 100.0 Hz.| 10.0 |
+| multi_topic | If the LiDAR device has an independent topic to publish pointcloud data
0 -- All LiDAR devices use the same topic to publish pointcloud data
1 -- Each LiDAR device has its own topic to publish point cloud data | 0 |
+| xfer_format | Set pointcloud format
0 -- Livox pointcloud2(PointXYZRTLT) pointcloud format
1 -- Livox customized pointcloud format
2 -- Standard pointcloud2 (pcl :: PointXYZI) pointcloud format in the PCL library (just for ROS) | 0 |
+
+ **Note :**
+
+ Other parameters not mentioned in this table are not suggested to be changed unless fully understood.
+
+ ***Livox_ros_driver2 pointcloud data detailed description :***
+
+1. Livox pointcloud2 (PointXYZRTLT) point cloud format, as follows :
+
+```c
+float32 x # X axis, unit:m
+float32 y # Y axis, unit:m
+float32 z # Z axis, unit:m
+float32 intensity # the value is reflectivity, 0.0~255.0
+uint8 tag # livox tag
+uint8 line # laser number in lidar
+float64 timestamp # Timestamp of point
+```
+ **Note :**
+
+ The number of points in the frame may be different, but each point provides a timestamp.
+
+2. Livox customized data package format, as follows :
+
+```c
+std_msgs/Header header # ROS standard message header
+uint64 timebase # The time of first point
+uint32 point_num # Total number of pointclouds
+uint8 lidar_id # Lidar device id number
+uint8[3] rsvd # Reserved use
+CustomPoint[] points # Pointcloud data
+```
+
+ Customized Point Cloud (CustomPoint) format in the above customized data package :
+
+```c
+uint32 offset_time # offset time relative to the base time
+float32 x # X axis, unit:m
+float32 y # Y axis, unit:m
+float32 z # Z axis, unit:m
+uint8 reflectivity # reflectivity, 0~255
+uint8 tag # livox tag
+uint8 line # laser number in lidar
+```
+
+3. The standard pointcloud2 (pcl :: PointXYZI) format in the PCL library (only ROS can publish):
+
+ Please refer to the pcl :: PointXYZI data structure in the point_types.hpp file of the PCL library.
+
+## 4. LiDAR config
+
+LiDAR Configurations (such as ip, port, data type... etc.) can be set via a json-style config file. Config files for single HAP, Mid360 and mixed-LiDARs are in the "config" folder. The parameter naming *'user_config_path'* in launch files indicates such json file path.
+
+1. Follow is a configuration example for HAP LiDAR (located in config/HAP_config.json):
+
+```json
+{
+ "lidar_summary_info" : {
+ "lidar_type": 8 # protocol type index, please don't revise this value
+ },
+ "HAP": {
+ "device_type" : "HAP",
+ "lidar_ipaddr": "",
+ "lidar_net_info" : {
+ "cmd_data_port": 56000, # command port
+ "push_msg_port": 0,
+ "point_data_port": 57000,
+ "imu_data_port": 58000,
+ "log_data_port": 59000
+ },
+ "host_net_info" : {
+ "cmd_data_ip" : "192.168.1.5", # host ip (it can be revised)
+ "cmd_data_port": 56000,
+ "push_msg_ip": "",
+ "push_msg_port": 0,
+ "point_data_ip": "192.168.1.5", # host ip
+ "point_data_port": 57000,
+ "imu_data_ip" : "192.168.1.5", # host ip
+ "imu_data_port": 58000,
+ "log_data_ip" : "",
+ "log_data_port": 59000
+ }
+ },
+ "lidar_configs" : [
+ {
+ "ip" : "192.168.1.100", # ip of the LiDAR you want to config
+ "pcl_data_type" : 1,
+ "pattern_mode" : 0,
+ "blind_spot_set" : 50,
+ "extrinsic_parameter" : {
+ "roll": 0.0,
+ "pitch": 0.0,
+ "yaw": 0.0,
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
+ ]
+}
+```
+
+The parameter attributes in the above json file are described in the following table :
+
+**LiDAR configuration parameter**
+| Parameter | Type | Description | Default |
+| :------------------------- | ------- | ------------------------------------------------------------ | --------------- |
+| ip | String | Ip of the LiDAR you want to config | 192.168.1.100 |
+| pcl_data_type | Int | Choose the resolution of the point cloud data to send
1 -- Cartesian coordinate data (32 bits)
2 -- Cartesian coordinate data (16 bits)
3 --Spherical coordinate data| 1 |
+| pattern_mode | Int | Space scan pattern
0 -- non-repeating scanning pattern mode
1 -- repeating scanning pattern mode
2 -- repeating scanning pattern mode (low scanning rate) | 0 |
+| blind_spot_set (Only for HAP LiDAR) | Int | Set blind spot
Range from 50 cm to 200 cm | 50 |
+| extrinsic_parameter | | Set extrinsic parameter
The data types of "roll" "picth" "yaw" are float
The data types of "x" "y" "z" are int
|
+
+For more infomation about the HAP config, please refer to:
+[HAP Config File Description](https://github.com/Livox-SDK/Livox-SDK2/wiki/hap-config-file-description)
+
+2. When connecting multiple LiDARs, add objects corresponding to different LiDARs to the "lidar_configs" array. Examples of mixed-LiDARs config file contents are as follows :
+
+```json
+{
+ "lidar_summary_info" : {
+ "lidar_type": 8 # protocol type index, please don't revise this value
+ },
+ "HAP": {
+ "lidar_net_info" : { # HAP ports, please don't revise these values
+ "cmd_data_port": 56000, # HAP command port
+ "push_msg_port": 0,
+ "point_data_port": 57000,
+ "imu_data_port": 58000,
+ "log_data_port": 59000
+ },
+ "host_net_info" : {
+ "cmd_data_ip" : "192.168.1.5", # host ip
+ "cmd_data_port": 56000,
+ "push_msg_ip": "",
+ "push_msg_port": 0,
+ "point_data_ip": "192.168.1.5", # host ip
+ "point_data_port": 57000,
+ "imu_data_ip" : "192.168.1.5", # host ip
+ "imu_data_port": 58000,
+ "log_data_ip" : "",
+ "log_data_port": 59000
+ }
+ },
+ "MID360": {
+ "lidar_net_info" : { # Mid360 ports, please don't revise these values
+ "cmd_data_port": 56100, # Mid360 command port
+ "push_msg_port": 56200,
+ "point_data_port": 56300,
+ "imu_data_port": 56400,
+ "log_data_port": 56500
+ },
+ "host_net_info" : {
+ "cmd_data_ip" : "192.168.1.5", # host ip
+ "cmd_data_port": 56101,
+ "push_msg_ip": "192.168.1.5", # host ip
+ "push_msg_port": 56201,
+ "point_data_ip": "192.168.1.5", # host ip
+ "point_data_port": 56301,
+ "imu_data_ip" : "192.168.1.5", # host ip
+ "imu_data_port": 56401,
+ "log_data_ip" : "",
+ "log_data_port": 56501
+ }
+ },
+ "lidar_configs" : [
+ {
+ "ip" : "192.168.1.100", # ip of the HAP you want to config
+ "pcl_data_type" : 1,
+ "pattern_mode" : 0,
+ "blind_spot_set" : 50,
+ "extrinsic_parameter" : {
+ "roll": 0.0,
+ "pitch": 0.0,
+ "yaw": 0.0,
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ },
+ {
+ "ip" : "192.168.1.12", # ip of the Mid360 you want to config
+ "pcl_data_type" : 1,
+ "pattern_mode" : 0,
+ "extrinsic_parameter" : {
+ "roll": 0.0,
+ "pitch": 0.0,
+ "yaw": 0.0,
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
+ ]
+}
+```
+
+## 5. Supported LiDAR list
+
+* HAP
+* Mid360
+* (more types are comming soon...)
+
+## 6. FAQ
+
+### 6.1 launch with "livox_lidar_rviz_HAP.launch" but no point cloud display on the grid?
+
+Please check the "Global Options - Fixed Frame" field in the RViz "Display" pannel. Set the field value to "livox_frame" and check the "PointCloud2" option in the pannel.
+
+### 6.2 launch with command "ros2 launch livox_lidar_rviz_HAP_launch.py" but cannot open shared object file "liblivox_sdk_shared.so" ?
+
+Please add '/usr/local/lib' to the env LD_LIBRARY_PATH.
+
+* If you want to add to current terminal:
+
+ ```shell
+ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib
+ ```
+
+* If you want to add to current user:
+
+ ```shell
+ vim ~/.bashrc
+ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib
+ source ~/.bashrc
+ ```
diff --git a/src/rm_driver/livox_ros_driver2/src/3rdparty/rapidjson/allocators.h b/src/rm_driver/livox_ros_driver2/src/3rdparty/rapidjson/allocators.h
new file mode 100644
index 0000000..03010d5
--- /dev/null
+++ b/src/rm_driver/livox_ros_driver2/src/3rdparty/rapidjson/allocators.h
@@ -0,0 +1,308 @@
+// Tencent is pleased to support the open source community by making RapidJSON
+// available.
+//
+// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
+// rights reserved.
+//
+// Licensed under the MIT License (the "License"); you may not use this file
+// except in compliance with the License. You may obtain a copy of the License
+// at
+//
+// http://opensource.org/licenses/MIT
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#ifndef RAPIDJSON_ALLOCATORS_H_
+#define RAPIDJSON_ALLOCATORS_H_
+
+#include "rapidjson.h"
+
+RAPIDJSON_NAMESPACE_BEGIN
+
+///////////////////////////////////////////////////////////////////////////////
+// Allocator
+
+/*! \class rapidjson::Allocator
+ \brief Concept for allocating, resizing and freeing memory block.
+
+ Note that Malloc() and Realloc() are non-static but Free() is static.
+
+ So if an allocator need to support Free(), it needs to put its pointer in
+ the header of memory block.
+
+\code
+concept Allocator {
+ static const bool kNeedFree; //!< Whether this allocator needs to call
+Free().
+
+ // Allocate a memory block.
+ // \param size of the memory block in bytes.
+ // \returns pointer to the memory block.
+ void* Malloc(size_t size);
+
+ // Resize a memory block.
+ // \param originalPtr The pointer to current memory block. Null pointer is
+permitted.
+ // \param originalSize The current size in bytes. (Design issue: since some
+allocator may not book-keep this, explicitly pass to it can save memory.)
+ // \param newSize the new size in bytes.
+ void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
+
+ // Free a memory block.
+ // \param pointer to the memory block. Null pointer is permitted.
+ static void Free(void *ptr);
+};
+\endcode
+*/
+
+/*! \def RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
+ \ingroup RAPIDJSON_CONFIG
+ \brief User-defined kDefaultChunkCapacity definition.
+
+ User can define this as any \c size that is a power of 2.
+*/
+
+#ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
+#define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024)
+#endif
+
+///////////////////////////////////////////////////////////////////////////////
+// CrtAllocator
+
+//! C-runtime library allocator.
+/*! This class is just wrapper for standard C library memory routines.
+ \note implements Allocator concept
+*/
+class CrtAllocator {
+ public:
+ static const bool kNeedFree = true;
+ void *Malloc(size_t size) {
+ if (size) // behavior of malloc(0) is implementation defined.
+ return std::malloc(size);
+ else
+ return NULL; // standardize to returning NULL.
+ }
+ void *Realloc(void *originalPtr, size_t originalSize, size_t newSize) {
+ (void)originalSize;
+ if (newSize == 0) {
+ std::free(originalPtr);
+ return NULL;
+ }
+ return std::realloc(originalPtr, newSize);
+ }
+ static void Free(void *ptr) { std::free(ptr); }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+// MemoryPoolAllocator
+
+//! Default memory allocator used by the parser and DOM.
+/*! This allocator allocate memory blocks from pre-allocated memory chunks.
+
+ It does not free memory blocks. And Realloc() only allocate new memory.
+
+ The memory chunks are allocated by BaseAllocator, which is CrtAllocator by
+ default.
+
+ User may also supply a buffer as the first chunk.
+
+ If the user-buffer is full then additional chunks are allocated by
+ BaseAllocator.
+
+ The user-buffer is not deallocated by this allocator.
+
+ \tparam BaseAllocator the allocator type for allocating memory chunks.
+ Default is CrtAllocator. \note implements Allocator concept
+*/
+template
+class MemoryPoolAllocator {
+ public:
+ static const bool kNeedFree =
+ false; //!< Tell users that no need to call Free() with this allocator.
+ //!< (concept Allocator)
+
+ //! Constructor with chunkSize.
+ /*! \param chunkSize The size of memory chunk. The default is
+ kDefaultChunkSize. \param baseAllocator The allocator for allocating memory
+ chunks.
+ */
+ MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity,
+ BaseAllocator *baseAllocator = 0)
+ : chunkHead_(0),
+ chunk_capacity_(chunkSize),
+ userBuffer_(0),
+ baseAllocator_(baseAllocator),
+ ownBaseAllocator_(0) {}
+
+ //! Constructor with user-supplied buffer.
+ /*! The user buffer will be used firstly. When it is full, memory pool
+ allocates new chunk with chunk size.
+
+ The user buffer will not be deallocated when this allocator is destructed.
+
+ \param buffer User supplied buffer.
+ \param size Size of the buffer in bytes. It must at least larger than
+ sizeof(ChunkHeader). \param chunkSize The size of memory chunk. The default
+ is kDefaultChunkSize. \param baseAllocator The allocator for allocating
+ memory chunks.
+ */
+ MemoryPoolAllocator(void *buffer, size_t size,
+ size_t chunkSize = kDefaultChunkCapacity,
+ BaseAllocator *baseAllocator = 0)
+ : chunkHead_(0),
+ chunk_capacity_(chunkSize),
+ userBuffer_(buffer),
+ baseAllocator_(baseAllocator),
+ ownBaseAllocator_(0) {
+ RAPIDJSON_ASSERT(buffer != 0);
+ RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
+ chunkHead_ = reinterpret_cast(buffer);
+ chunkHead_->capacity = size - sizeof(ChunkHeader);
+ chunkHead_->size = 0;
+ chunkHead_->next = 0;
+ }
+
+ //! Destructor.
+ /*! This deallocates all memory chunks, excluding the user-supplied buffer.
+ */
+ ~MemoryPoolAllocator() {
+ Clear();
+ RAPIDJSON_DELETE(ownBaseAllocator_);
+ }
+
+ //! Deallocates all memory chunks, excluding the user-supplied buffer.
+ void Clear() {
+ while (chunkHead_ && chunkHead_ != userBuffer_) {
+ ChunkHeader *next = chunkHead_->next;
+ baseAllocator_->Free(chunkHead_);
+ chunkHead_ = next;
+ }
+ if (chunkHead_ && chunkHead_ == userBuffer_)
+ chunkHead_->size = 0; // Clear user buffer
+ }
+
+ //! Computes the total capacity of allocated memory chunks.
+ /*! \return total capacity in bytes.
+ */
+ size_t Capacity() const {
+ size_t capacity = 0;
+ for (ChunkHeader *c = chunkHead_; c != 0; c = c->next)
+ capacity += c->capacity;
+ return capacity;
+ }
+
+ //! Computes the memory blocks allocated.
+ /*! \return total used bytes.
+ */
+ size_t Size() const {
+ size_t size = 0;
+ for (ChunkHeader *c = chunkHead_; c != 0; c = c->next) size += c->size;
+ return size;
+ }
+
+ //! Allocates a memory block. (concept Allocator)
+ void *Malloc(size_t size) {
+ if (!size) return NULL;
+
+ size = RAPIDJSON_ALIGN(size);
+ if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
+ if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
+ return NULL;
+
+ void *buffer = reinterpret_cast(chunkHead_) +
+ RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
+ chunkHead_->size += size;
+ return buffer;
+ }
+
+ //! Resizes a memory block (concept Allocator)
+ void *Realloc(void *originalPtr, size_t originalSize, size_t newSize) {
+ if (originalPtr == 0) return Malloc(newSize);
+
+ if (newSize == 0) return NULL;
+
+ originalSize = RAPIDJSON_ALIGN(originalSize);
+ newSize = RAPIDJSON_ALIGN(newSize);
+
+ // Do not shrink if new size is smaller than original
+ if (originalSize >= newSize) return originalPtr;
+
+ // Simply expand it if it is the last allocation and there is sufficient
+ // space
+ if (originalPtr ==
+ reinterpret_cast(chunkHead_) +
+ RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size -
+ originalSize) {
+ size_t increment = static_cast(newSize - originalSize);
+ if (chunkHead_->size + increment <= chunkHead_->capacity) {
+ chunkHead_->size += increment;
+ return originalPtr;
+ }
+ }
+
+ // Realloc process: allocate and copy memory, do not free original buffer.
+ if (void *newBuffer = Malloc(newSize)) {
+ if (originalSize) std::memcpy(newBuffer, originalPtr, originalSize);
+ return newBuffer;
+ } else
+ return NULL;
+ }
+
+ //! Frees a memory block (concept Allocator)
+ static void Free(void *ptr) { (void)ptr; } // Do nothing
+
+ private:
+ //! Copy constructor is not permitted.
+ MemoryPoolAllocator(const MemoryPoolAllocator &rhs) /* = delete */;
+ //! Copy assignment operator is not permitted.
+ MemoryPoolAllocator &operator=(const MemoryPoolAllocator &rhs) /* = delete */;
+
+ //! Creates a new chunk.
+ /*! \param capacity Capacity of the chunk in bytes.
+ \return true if success.
+ */
+ bool AddChunk(size_t capacity) {
+ if (!baseAllocator_)
+ ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)();
+ if (ChunkHeader *chunk =
+ reinterpret_cast(baseAllocator_->Malloc(
+ RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) {
+ chunk->capacity = capacity;
+ chunk->size = 0;
+ chunk->next = chunkHead_;
+ chunkHead_ = chunk;
+ return true;
+ } else
+ return false;
+ }
+
+ static const int kDefaultChunkCapacity =
+ RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY; //!< Default chunk capacity.
+
+ //! Chunk header for perpending to each chunk.
+ /*! Chunks are stored as a singly linked list.
+ */
+ struct ChunkHeader {
+ size_t capacity; //!< Capacity of the chunk in bytes (excluding the header
+ //!< itself).
+ size_t size; //!< Current size of allocated memory in bytes.
+ ChunkHeader *next; //!< Next chunk in the linked list.
+ };
+
+ ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head
+ //!< chunk serves allocation.
+ size_t chunk_capacity_; //!< The minimum capacity of chunk when they are
+ //!< allocated.
+ void *userBuffer_; //!< User supplied buffer.
+ BaseAllocator
+ *baseAllocator_; //!< base allocator for allocating memory chunks.
+ BaseAllocator *ownBaseAllocator_; //!< base allocator created by this object.
+};
+
+RAPIDJSON_NAMESPACE_END
+
+#endif // RAPIDJSON_ENCODINGS_H_
diff --git a/src/rm_driver/livox_ros_driver2/src/3rdparty/rapidjson/cursorstreamwrapper.h b/src/rm_driver/livox_ros_driver2/src/3rdparty/rapidjson/cursorstreamwrapper.h
new file mode 100644
index 0000000..045ddb8
--- /dev/null
+++ b/src/rm_driver/livox_ros_driver2/src/3rdparty/rapidjson/cursorstreamwrapper.h
@@ -0,0 +1,81 @@
+// Tencent is pleased to support the open source community by making RapidJSON
+// available.
+//
+// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
+// rights reserved.
+//
+// Licensed under the MIT License (the "License"); you may not use this file
+// except in compliance with the License. You may obtain a copy of the License
+// at
+//
+// http://opensource.org/licenses/MIT
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#ifndef RAPIDJSON_CURSORSTREAMWRAPPER_H_
+#define RAPIDJSON_CURSORSTREAMWRAPPER_H_
+
+#include "stream.h"
+
+#if defined(__GNUC__)
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(effc++)
+#endif
+
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(4702) // unreachable code
+RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
+#endif
+
+RAPIDJSON_NAMESPACE_BEGIN
+
+//! Cursor stream wrapper for counting line and column number if error exists.
+/*!
+ \tparam InputStream Any stream that implements Stream Concept
+*/
+template >
+class CursorStreamWrapper : public GenericStreamWrapper {
+ public:
+ typedef typename Encoding::Ch Ch;
+
+ CursorStreamWrapper(InputStream &is)
+ : GenericStreamWrapper(is), line_(1), col_(0) {}
+
+ // counting line and column number
+ Ch Take() {
+ Ch ch = this->is_.Take();
+ if (ch == '\n') {
+ line_++;
+ col_ = 0;
+ } else {
+ col_++;
+ }
+ return ch;
+ }
+
+ //! Get the error line number, if error exists.
+ size_t GetLine() const { return line_; }
+ //! Get the error column number, if error exists.
+ size_t GetColumn() const { return col_; }
+
+ private:
+ size_t line_; //!< Current Line
+ size_t col_; //!< Current Column
+};
+
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+RAPIDJSON_DIAG_POP
+#endif
+
+#if defined(__GNUC__)
+RAPIDJSON_DIAG_POP
+#endif
+
+RAPIDJSON_NAMESPACE_END
+
+#endif // RAPIDJSON_CURSORSTREAMWRAPPER_H_
diff --git a/src/rm_driver/livox_ros_driver2/src/3rdparty/rapidjson/document.h b/src/rm_driver/livox_ros_driver2/src/3rdparty/rapidjson/document.h
new file mode 100644
index 0000000..27addb7
--- /dev/null
+++ b/src/rm_driver/livox_ros_driver2/src/3rdparty/rapidjson/document.h
@@ -0,0 +1,3309 @@
+// Tencent is pleased to support the open source community by making RapidJSON
+// available.
+//
+// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
+// rights reserved.
+//
+// Licensed under the MIT License (the "License"); you may not use this file
+// except in compliance with the License. You may obtain a copy of the License
+// at
+//
+// http://opensource.org/licenses/MIT
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#ifndef RAPIDJSON_DOCUMENT_H_
+#define RAPIDJSON_DOCUMENT_H_
+
+/*! \file document.h */
+
+#include
+#include // placement new
+#include "encodedstream.h"
+#include "internal/meta.h"
+#include "internal/strfunc.h"
+#include "memorystream.h"
+#include "reader.h"
+
+RAPIDJSON_DIAG_PUSH
+#ifdef __clang__
+RAPIDJSON_DIAG_OFF(padded)
+RAPIDJSON_DIAG_OFF(switch - enum)
+RAPIDJSON_DIAG_OFF(c++ 98 - compat)
+#elif defined(_MSC_VER)
+RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
+RAPIDJSON_DIAG_OFF(
+ 4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data
+#endif
+
+#ifdef __GNUC__
+RAPIDJSON_DIAG_OFF(effc++)
+#endif // __GNUC__
+
+#ifndef RAPIDJSON_NOMEMBERITERATORCLASS
+#include // std::random_access_iterator_tag
+#endif
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+#include // std::move
+#endif
+
+RAPIDJSON_NAMESPACE_BEGIN
+
+// Forward declaration.
+template
+class GenericValue;
+
+template
+class GenericDocument;
+
+//! Name-value pair in a JSON object value.
+/*!
+ This class was internal to GenericValue. It used to be a inner struct.
+ But a compiler (IBM XL C/C++ for AIX) have reported to have problem with
+ that so it moved as a namespace scope struct.
+ https://code.google.com/p/rapidjson/issues/detail?id=64
+*/
+template
+class GenericMember {
+ public:
+ GenericValue
+ name; //!< name of member (must be a string)
+ GenericValue value; //!< value of member.
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ //! Move constructor in C++11
+ GenericMember(GenericMember &&rhs) RAPIDJSON_NOEXCEPT
+ : name(std::move(rhs.name)),
+ value(std::move(rhs.value)) {}
+
+ //! Move assignment in C++11
+ GenericMember &operator=(GenericMember &&rhs) RAPIDJSON_NOEXCEPT {
+ return *this = static_cast(rhs);
+ }
+#endif
+
+ //! Assignment with move semantics.
+ /*! \param rhs Source of the assignment. Its name and value will become a null
+ * value after assignment.
+ */
+ GenericMember &operator=(GenericMember &rhs) RAPIDJSON_NOEXCEPT {
+ if (RAPIDJSON_LIKELY(this != &rhs)) {
+ name = rhs.name;
+ value = rhs.value;
+ }
+ return *this;
+ }
+
+ // swap() for std::sort() and other potential use in STL.
+ friend inline void swap(GenericMember &a,
+ GenericMember &b) RAPIDJSON_NOEXCEPT {
+ a.name.Swap(b.name);
+ a.value.Swap(b.value);
+ }
+
+ private:
+ //! Copy constructor is not permitted.
+ GenericMember(const GenericMember &rhs);
+};
+
+///////////////////////////////////////////////////////////////////////////////
+// GenericMemberIterator
+
+#ifndef RAPIDJSON_NOMEMBERITERATORCLASS
+
+//! (Constant) member iterator for a JSON object value
+/*!
+ \tparam Const Is this a constant iterator?
+ \tparam Encoding Encoding of the value. (Even non-string values need to
+ have the same encoding in a document) \tparam Allocator Allocator type for
+ allocating memory of object, array and string.
+
+ This class implements a Random Access Iterator for GenericMember elements
+ of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1
+ [lib.iterator.requirements].
+
+ \note This iterator implementation is mainly intended to avoid implicit
+ conversions from iterator values to \c NULL,
+ e.g. from GenericValue::FindMember.
+
+ \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a
+ pointer-based implementation, if your platform doesn't provide
+ the C++ header.
+
+ \see GenericMember, GenericValue::MemberIterator,
+ GenericValue::ConstMemberIterator
+ */
+template
+class GenericMemberIterator {
+ friend class GenericValue;
+ template
+ friend class GenericMemberIterator;
+
+ typedef GenericMember PlainType;
+ typedef typename internal::MaybeAddConst::Type ValueType;
+
+ public:
+ //! Iterator type itself
+ typedef GenericMemberIterator Iterator;
+ //! Constant iterator type
+ typedef GenericMemberIterator ConstIterator;
+ //! Non-constant iterator type
+ typedef GenericMemberIterator NonConstIterator;
+
+ /** \name std::iterator_traits support */
+ //@{
+ typedef ValueType value_type;
+ typedef ValueType *pointer;
+ typedef ValueType &reference;
+ typedef std::ptrdiff_t difference_type;
+ typedef std::random_access_iterator_tag iterator_category;
+ //@}
+
+ //! Pointer to (const) GenericMember
+ typedef pointer Pointer;
+ //! Reference to (const) GenericMember
+ typedef reference Reference;
+ //! Signed integer type (e.g. \c ptrdiff_t)
+ typedef difference_type DifferenceType;
+
+ //! Default constructor (singular value)
+ /*! Creates an iterator pointing to no element.
+ \note All operations, except for comparisons, are undefined on such
+ values.
+ */
+ GenericMemberIterator() : ptr_() {}
+
+ //! Iterator conversions to more const
+ /*!
+ \param it (Non-const) iterator to copy from
+
+ Allows the creation of an iterator from another GenericMemberIterator
+ that is "less const". Especially, creating a non-constant iterator
+ from a constant iterator are disabled:
+ \li const -> non-const (not ok)
+ \li const -> const (ok)
+ \li non-const -> const (ok)
+ \li non-const -> non-const (ok)
+
+ \note If the \c Const template parameter is already \c false, this
+ constructor effectively defines a regular copy-constructor.
+ Otherwise, the copy constructor is implicitly defined.
+ */
+ GenericMemberIterator(const NonConstIterator &it) : ptr_(it.ptr_) {}
+ Iterator &operator=(const NonConstIterator &it) {
+ ptr_ = it.ptr_;
+ return *this;
+ }
+
+ //! @name stepping
+ //@{
+ Iterator &operator++() {
+ ++ptr_;
+ return *this;
+ }
+ Iterator &operator--() {
+ --ptr_;
+ return *this;
+ }
+ Iterator operator++(int) {
+ Iterator old(*this);
+ ++ptr_;
+ return old;
+ }
+ Iterator operator--(int) {
+ Iterator old(*this);
+ --ptr_;
+ return old;
+ }
+ //@}
+
+ //! @name increment/decrement
+ //@{
+ Iterator operator+(DifferenceType n) const { return Iterator(ptr_ + n); }
+ Iterator operator-(DifferenceType n) const { return Iterator(ptr_ - n); }
+
+ Iterator &operator+=(DifferenceType n) {
+ ptr_ += n;
+ return *this;
+ }
+ Iterator &operator-=(DifferenceType n) {
+ ptr_ -= n;
+ return *this;
+ }
+ //@}
+
+ //! @name relations
+ //@{
+ bool operator==(ConstIterator that) const { return ptr_ == that.ptr_; }
+ bool operator!=(ConstIterator that) const { return ptr_ != that.ptr_; }
+ bool operator<=(ConstIterator that) const { return ptr_ <= that.ptr_; }
+ bool operator>=(ConstIterator that) const { return ptr_ >= that.ptr_; }
+ bool operator<(ConstIterator that) const { return ptr_ < that.ptr_; }
+ bool operator>(ConstIterator that) const { return ptr_ > that.ptr_; }
+ //@}
+
+ //! @name dereference
+ //@{
+ Reference operator*() const { return *ptr_; }
+ Pointer operator->() const { return ptr_; }
+ Reference operator[](DifferenceType n) const { return ptr_[n]; }
+ //@}
+
+ //! Distance
+ DifferenceType operator-(ConstIterator that) const {
+ return ptr_ - that.ptr_;
+ }
+
+ private:
+ //! Internal constructor from plain pointer
+ explicit GenericMemberIterator(Pointer p) : ptr_(p) {}
+
+ Pointer ptr_; //!< raw pointer
+};
+
+#else // RAPIDJSON_NOMEMBERITERATORCLASS
+
+// class-based member iterator implementation disabled, use plain pointers
+
+template
+class GenericMemberIterator;
+
+//! non-const GenericMemberIterator
+template
+class GenericMemberIterator {
+ //! use plain pointer as iterator type
+ typedef GenericMember *Iterator;
+};
+//! const GenericMemberIterator
+template
+class GenericMemberIterator {
+ //! use plain const pointer as iterator type
+ typedef const GenericMember *Iterator;
+};
+
+#endif // RAPIDJSON_NOMEMBERITERATORCLASS
+
+///////////////////////////////////////////////////////////////////////////////
+// GenericStringRef
+
+//! Reference to a constant string (not taking a copy)
+/*!
+ \tparam CharType character type of the string
+
+ This helper class is used to automatically infer constant string
+ references for string literals, especially from \c const \b (!)
+ character arrays.
+
+ The main use is for creating JSON string values without copying the
+ source string via an \ref Allocator. This requires that the referenced
+ string pointers have a sufficient lifetime, which exceeds the lifetime
+ of the associated GenericValue.
+
+ \b Example
+ \code
+ Value v("foo"); // ok, no need to copy & calculate length
+ const char foo[] = "foo";
+ v.SetString(foo); // ok
+
+ const char* bar = foo;
+ // Value x(bar); // not ok, can't rely on bar's lifetime
+ Value x(StringRef(bar)); // lifetime explicitly guaranteed by user
+ Value y(StringRef(bar, 3)); // ok, explicitly pass length
+ \endcode
+
+ \see StringRef, GenericValue::SetString
+*/
+template
+struct GenericStringRef {
+ typedef CharType Ch; //!< character type of the string
+
+//! Create string reference from \c const character array
+#ifndef __clang__ // -Wdocumentation
+ /*!
+ This constructor implicitly creates a constant string reference from
+ a \c const character array. It has better performance than
+ \ref StringRef(const CharType*) by inferring the string \ref length
+ from the array length, and also supports strings containing null
+ characters.
+
+ \tparam N length of the string, automatically inferred
+
+ \param str Constant character array, lifetime assumed to be longer
+ than the use of the string in e.g. a GenericValue
+
+ \post \ref s == str
+
+ \note Constant complexity.
+ \note There is a hidden, private overload to disallow references to
+ non-const character arrays to be created via this constructor.
+ By this, e.g. function-scope arrays used to be filled via
+ \c snprintf are excluded from consideration.
+ In such cases, the referenced string should be \b copied to the
+ GenericValue instead.
+ */
+#endif
+ template
+ GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT
+ : s(str),
+ length(N - 1) {}
+
+//! Explicitly create string reference from \c const character pointer
+#ifndef __clang__ // -Wdocumentation
+ /*!
+ This constructor can be used to \b explicitly create a reference to
+ a constant string pointer.
+
+ \see StringRef(const CharType*)
+
+ \param str Constant character pointer, lifetime assumed to be longer
+ than the use of the string in e.g. a GenericValue
+
+ \post \ref s == str
+
+ \note There is a hidden, private overload to disallow references to
+ non-const character arrays to be created via this constructor.
+ By this, e.g. function-scope arrays used to be filled via
+ \c snprintf are excluded from consideration.
+ In such cases, the referenced string should be \b copied to the
+ GenericValue instead.
+ */
+#endif
+ explicit GenericStringRef(const CharType *str)
+ : s(str), length(NotNullStrLen(str)) {}
+
+//! Create constant string reference from pointer and length
+#ifndef __clang__ // -Wdocumentation
+/*! \param str constant string, lifetime assumed to be longer than the use of
+ the string in e.g. a GenericValue \param len length of the string,
+ excluding the trailing NULL terminator
+
+ \post \ref s == str && \ref length == len
+ \note Constant complexity.
+ */
+#endif
+ GenericStringRef(const CharType *str, SizeType len)
+ : s(RAPIDJSON_LIKELY(str) ? str : emptyString), length(len) {
+ RAPIDJSON_ASSERT(str != 0 || len == 0u);
+ }
+
+ GenericStringRef(const GenericStringRef &rhs)
+ : s(rhs.s), length(rhs.length) {}
+
+ //! implicit conversion to plain CharType pointer
+ operator const Ch *() const { return s; }
+
+ const Ch *const s; //!< plain CharType pointer
+ const SizeType length; //!< length of the string (excluding the trailing NULL
+ //!terminator)
+
+ private:
+ SizeType NotNullStrLen(const CharType *str) {
+ RAPIDJSON_ASSERT(str != 0);
+ return internal::StrLen(str);
+ }
+
+ /// Empty string - used when passing in a NULL pointer
+ static const Ch emptyString[];
+
+ //! Disallow construction from non-const array
+ template
+ GenericStringRef(CharType (&str)[N]) /* = delete */;
+ //! Copy assignment operator not permitted - immutable type
+ GenericStringRef &operator=(const GenericStringRef &rhs) /* = delete */;
+};
+
+template
+const CharType GenericStringRef::emptyString[] = {CharType()};
+
+//! Mark a character pointer as constant string
+/*! Mark a plain character pointer as a "string literal". This function
+ can be used to avoid copying a character string to be referenced as a
+ value in a JSON GenericValue object, if the string's lifetime is known
+ to be valid long enough.
+ \tparam CharType Character type of the string
+ \param str Constant string, lifetime assumed to be longer than the use of
+ the string in e.g. a GenericValue \return GenericStringRef string reference
+ object \relatesalso GenericStringRef
+
+ \see GenericValue::GenericValue(StringRefType),
+ GenericValue::operator=(StringRefType),
+ GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType,
+ Allocator&), GenericValue::AddMember
+*/
+template
+inline GenericStringRef StringRef(const CharType *str) {
+ return GenericStringRef(str);
+}
+
+//! Mark a character pointer as constant string
+/*! Mark a plain character pointer as a "string literal". This function
+ can be used to avoid copying a character string to be referenced as a
+ value in a JSON GenericValue object, if the string's lifetime is known
+ to be valid long enough.
+
+ This version has better performance with supplied length, and also
+ supports string containing null characters.
+
+ \tparam CharType character type of the string
+ \param str Constant string, lifetime assumed to be longer than the use of
+ the string in e.g. a GenericValue \param length The length of source string.
+ \return GenericStringRef string reference object
+ \relatesalso GenericStringRef
+*/
+template
+inline GenericStringRef StringRef(const CharType *str,
+ size_t length) {
+ return GenericStringRef(str, SizeType(length));
+}
+
+#if RAPIDJSON_HAS_STDSTRING
+//! Mark a string object as constant string
+/*! Mark a string object (e.g. \c std::string) as a "string literal".
+ This function can be used to avoid copying a string to be referenced as a
+ value in a JSON GenericValue object, if the string's lifetime is known
+ to be valid long enough.
+
+ \tparam CharType character type of the string
+ \param str Constant string, lifetime assumed to be longer than the use of
+ the string in e.g. a GenericValue \return GenericStringRef string reference
+ object \relatesalso GenericStringRef \note Requires the definition of the
+ preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
+*/
+template
+inline GenericStringRef StringRef(
+ const std::basic_string &str) {
+ return GenericStringRef(str.data(), SizeType(str.size()));
+}
+#endif
+
+///////////////////////////////////////////////////////////////////////////////
+// GenericValue type traits
+namespace internal {
+
+template
+struct IsGenericValueImpl : FalseType {};
+
+// select candidates according to nested encoding and allocator types
+template
+struct IsGenericValueImpl::Type,
+ typename Void::Type>
+ : IsBaseOf<
+ GenericValue,
+ T>::Type {};
+
+// helper to match arbitrary GenericValue instantiations, including derived
+// classes
+template
+struct IsGenericValue : IsGenericValueImpl::Type {};
+
+} // namespace internal
+
+///////////////////////////////////////////////////////////////////////////////
+// TypeHelper
+
+namespace internal {
+
+template
+struct TypeHelper {};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType &v) { return v.IsBool(); }
+ static bool Get(const ValueType &v) { return v.GetBool(); }
+ static ValueType &Set(ValueType &v, bool data) { return v.SetBool(data); }
+ static ValueType &Set(ValueType &v, bool data,
+ typename ValueType::AllocatorType &) {
+ return v.SetBool(data);
+ }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType &v) { return v.IsInt(); }
+ static int Get(const ValueType &v) { return v.GetInt(); }
+ static ValueType &Set(ValueType &v, int data) { return v.SetInt(data); }
+ static ValueType &Set(ValueType &v, int data,
+ typename ValueType::AllocatorType &) {
+ return v.SetInt(data);
+ }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType &v) { return v.IsUint(); }
+ static unsigned Get(const ValueType &v) { return v.GetUint(); }
+ static ValueType &Set(ValueType &v, unsigned data) { return v.SetUint(data); }
+ static ValueType &Set(ValueType &v, unsigned data,
+ typename ValueType::AllocatorType &) {
+ return v.SetUint(data);
+ }
+};
+
+#ifdef _MSC_VER
+RAPIDJSON_STATIC_ASSERT(sizeof(long) == sizeof(int));
+template
+struct TypeHelper {
+ static bool Is(const ValueType &v) { return v.IsInt(); }
+ static long Get(const ValueType &v) { return v.GetInt(); }
+ static ValueType &Set(ValueType &v, long data) { return v.SetInt(data); }
+ static ValueType &Set(ValueType &v, long data,
+ typename ValueType::AllocatorType &) {
+ return v.SetInt(data);
+ }
+};
+
+RAPIDJSON_STATIC_ASSERT(sizeof(unsigned long) == sizeof(unsigned));
+template
+struct TypeHelper {
+ static bool Is(const ValueType &v) { return v.IsUint(); }
+ static unsigned long Get(const ValueType &v) { return v.GetUint(); }
+ static ValueType &Set(ValueType &v, unsigned long data) {
+ return v.SetUint(data);
+ }
+ static ValueType &Set(ValueType &v, unsigned long data,
+ typename ValueType::AllocatorType &) {
+ return v.SetUint(data);
+ }
+};
+#endif
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType &v) { return v.IsInt64(); }
+ static int64_t Get(const ValueType &v) { return v.GetInt64(); }
+ static ValueType &Set(ValueType &v, int64_t data) { return v.SetInt64(data); }
+ static ValueType &Set(ValueType &v, int64_t data,
+ typename ValueType::AllocatorType &) {
+ return v.SetInt64(data);
+ }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType &v) { return v.IsUint64(); }
+ static uint64_t Get(const ValueType &v) { return v.GetUint64(); }
+ static ValueType &Set(ValueType &v, uint64_t data) {
+ return v.SetUint64(data);
+ }
+ static ValueType &Set(ValueType &v, uint64_t data,
+ typename ValueType::AllocatorType &) {
+ return v.SetUint64(data);
+ }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType &v) { return v.IsDouble(); }
+ static double Get(const ValueType &v) { return v.GetDouble(); }
+ static ValueType &Set(ValueType &v, double data) { return v.SetDouble(data); }
+ static ValueType &Set(ValueType &v, double data,
+ typename ValueType::AllocatorType &) {
+ return v.SetDouble(data);
+ }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType &v) { return v.IsFloat(); }
+ static float Get(const ValueType &v) { return v.GetFloat(); }
+ static ValueType &Set(ValueType &v, float data) { return v.SetFloat(data); }
+ static ValueType &Set(ValueType &v, float data,
+ typename ValueType::AllocatorType &) {
+ return v.SetFloat(data);
+ }
+};
+
+template
+struct TypeHelper {
+ typedef const typename ValueType::Ch *StringType;
+ static bool Is(const ValueType &v) { return v.IsString(); }
+ static StringType Get(const ValueType &v) { return v.GetString(); }
+ static ValueType &Set(ValueType &v, const StringType data) {
+ return v.SetString(typename ValueType::StringRefType(data));
+ }
+ static ValueType &Set(ValueType &v, const StringType data,
+ typename ValueType::AllocatorType &a) {
+ return v.SetString(data, a);
+ }
+};
+
+#if RAPIDJSON_HAS_STDSTRING
+template
+struct TypeHelper> {
+ typedef std::basic_string StringType;
+ static bool Is(const ValueType &v) { return v.IsString(); }
+ static StringType Get(const ValueType &v) {
+ return StringType(v.GetString(), v.GetStringLength());
+ }
+ static ValueType &Set(ValueType &v, const StringType &data,
+ typename ValueType::AllocatorType &a) {
+ return v.SetString(data, a);
+ }
+};
+#endif
+
+template
+struct TypeHelper {
+ typedef typename ValueType::Array ArrayType;
+ static bool Is(const ValueType &v) { return v.IsArray(); }
+ static ArrayType Get(ValueType &v) { return v.GetArray(); }
+ static ValueType &Set(ValueType &v, ArrayType data) { return v = data; }
+ static ValueType &Set(ValueType &v, ArrayType data,
+ typename ValueType::AllocatorType &) {
+ return v = data;
+ }
+};
+
+template
+struct TypeHelper {
+ typedef typename ValueType::ConstArray ArrayType;
+ static bool Is(const ValueType &v) { return v.IsArray(); }
+ static ArrayType Get(const ValueType &v) { return v.GetArray(); }
+};
+
+template
+struct TypeHelper {
+ typedef typename ValueType::Object ObjectType;
+ static bool Is(const ValueType &v) { return v.IsObject(); }
+ static ObjectType Get(ValueType &v) { return v.GetObject(); }
+ static ValueType &Set(ValueType &v, ObjectType data) { return v = data; }
+ static ValueType &Set(ValueType &v, ObjectType data,
+ typename ValueType::AllocatorType &) {
+ return v = data;
+ }
+};
+
+template
+struct TypeHelper {
+ typedef typename ValueType::ConstObject ObjectType;
+ static bool Is(const ValueType &v) { return v.IsObject(); }
+ static ObjectType Get(const ValueType &v) { return v.GetObject(); }
+};
+
+} // namespace internal
+
+// Forward declarations
+template
+class GenericArray;
+template
+class GenericObject;
+
+///////////////////////////////////////////////////////////////////////////////
+// GenericValue
+
+//! Represents a JSON value. Use Value for UTF8 encoding and default allocator.
+/*!
+ A JSON value can be one of 7 types. This class is a variant type supporting
+ these types.
+
+ Use the Value if UTF8 and default allocator
+
+ \tparam Encoding Encoding of the value. (Even non-string values need to
+ have the same encoding in a document) \tparam Allocator Allocator type for
+ allocating memory of object, array and string.
+*/
+template >
+class GenericValue {
+ public:
+ //! Name-value pair in an object.
+ typedef GenericMember Member;
+ typedef Encoding EncodingType; //!< Encoding type from template parameter.
+ typedef Allocator AllocatorType; //!< Allocator type from template parameter.
+ typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
+ typedef GenericStringRef
+ StringRefType; //!< Reference to a constant string
+ typedef typename GenericMemberIterator::Iterator
+ MemberIterator; //!< Member iterator for iterating in object.
+ typedef typename GenericMemberIterator::Iterator
+ ConstMemberIterator; //!< Constant member iterator for iterating in
+ //!< object.
+ typedef GenericValue
+ *ValueIterator; //!< Value iterator for iterating in array.
+ typedef const GenericValue
+ *ConstValueIterator; //!< Constant value iterator for iterating in array.
+ typedef GenericValue
+ ValueType; //!< Value type of itself.
+ typedef GenericArray Array;
+ typedef GenericArray ConstArray;
+ typedef GenericObject Object;
+ typedef GenericObject ConstObject;
+
+ //!@name Constructors and destructor.
+ //@{
+
+ //! Default constructor creates a null value.
+ GenericValue() RAPIDJSON_NOEXCEPT : data_() { data_.f.flags = kNullFlag; }
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ //! Move constructor in C++11
+ GenericValue(GenericValue &&rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_) {
+ rhs.data_.f.flags = kNullFlag; // give up contents
+ }
+#endif
+
+ private:
+ //! Copy constructor is not permitted.
+ GenericValue(const GenericValue &rhs);
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ //! Moving from a GenericDocument is not permitted.
+ template
+ GenericValue(GenericDocument &&rhs);
+
+ //! Move assignment from a GenericDocument is not permitted.
+ template
+ GenericValue &operator=(
+ GenericDocument &&rhs);
+#endif
+
+ public:
+ //! Constructor with JSON value type.
+ /*! This creates a Value of specified type with default content.
+ \param type Type of the value.
+ \note Default content for number is zero.
+ */
+ explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() {
+ static const uint16_t defaultFlags[] = {
+ kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag,
+ kArrayFlag, kShortStringFlag, kNumberAnyFlag};
+ RAPIDJSON_NOEXCEPT_ASSERT(type >= kNullType && type <= kNumberType);
+ data_.f.flags = defaultFlags[type];
+
+ // Use ShortString to store empty string.
+ if (type == kStringType) data_.ss.SetLength(0);
+ }
+
+ //! Explicit copy constructor (with allocator)
+ /*! Creates a copy of a Value by using the given Allocator
+ \tparam SourceAllocator allocator of \c rhs
+ \param rhs Value to copy from (read-only)
+ \param allocator Allocator for allocating copied elements and buffers.
+ Commonly use GenericDocument::GetAllocator(). \param copyConstStrings Force
+ copying of constant strings (e.g. referencing an in-situ buffer) \see
+ CopyFrom()
+ */
+ template
+ GenericValue(const GenericValue &rhs,
+ Allocator &allocator, bool copyConstStrings = false) {
+ switch (rhs.GetType()) {
+ case kObjectType: {
+ SizeType count = rhs.data_.o.size;
+ Member *lm = reinterpret_cast(
+ allocator.Malloc(count * sizeof(Member)));
+ const typename GenericValue::Member *rm =
+ rhs.GetMembersPointer();
+ for (SizeType i = 0; i < count; i++) {
+ new (&lm[i].name)
+ GenericValue(rm[i].name, allocator, copyConstStrings);
+ new (&lm[i].value)
+ GenericValue(rm[i].value, allocator, copyConstStrings);
+ }
+ data_.f.flags = kObjectFlag;
+ data_.o.size = data_.o.capacity = count;
+ SetMembersPointer(lm);
+ } break;
+ case kArrayType: {
+ SizeType count = rhs.data_.a.size;
+ GenericValue *le = reinterpret_cast(
+ allocator.Malloc(count * sizeof(GenericValue)));
+ const GenericValue *re =
+ rhs.GetElementsPointer();
+ for (SizeType i = 0; i < count; i++)
+ new (&le[i]) GenericValue(re[i], allocator, copyConstStrings);
+ data_.f.flags = kArrayFlag;
+ data_.a.size = data_.a.capacity = count;
+ SetElementsPointer(le);
+ } break;
+ case kStringType:
+ if (rhs.data_.f.flags == kConstStringFlag && !copyConstStrings) {
+ data_.f.flags = rhs.data_.f.flags;
+ data_ = *reinterpret_cast(&rhs.data_);
+ } else
+ SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()),
+ allocator);
+ break;
+ default:
+ data_.f.flags = rhs.data_.f.flags;
+ data_ = *reinterpret_cast(&rhs.data_);
+ break;
+ }
+ }
+
+//! Constructor for boolean value.
+/*! \param b Boolean value
+ \note This constructor is limited to \em real boolean values and rejects
+ implicitly converted types like arbitrary pointers. Use an explicit
+ cast to \c bool, if you want to construct a boolean JSON value in such
+ cases.
+ */
+#ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen
+ template
+ explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame)))
+ RAPIDJSON_NOEXCEPT // See #472
+#else
+ explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT
+#endif
+ : data_() {
+ // safe-guard against failing SFINAE
+ RAPIDJSON_STATIC_ASSERT((internal::IsSame::Value));
+ data_.f.flags = b ? kTrueFlag : kFalseFlag;
+ }
+
+ //! Constructor for int value.
+ explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.i64 = i;
+ data_.f.flags =
+ (i >= 0) ? (kNumberIntFlag | kUintFlag | kUint64Flag) : kNumberIntFlag;
+ }
+
+ //! Constructor for unsigned value.
+ explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.u64 = u;
+ data_.f.flags = (u & 0x80000000)
+ ? kNumberUintFlag
+ : (kNumberUintFlag | kIntFlag | kInt64Flag);
+ }
+
+ //! Constructor for int64_t value.
+ explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.i64 = i64;
+ data_.f.flags = kNumberInt64Flag;
+ if (i64 >= 0) {
+ data_.f.flags |= kNumberUint64Flag;
+ if (!(static_cast(i64) &
+ RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
+ data_.f.flags |= kUintFlag;
+ if (!(static_cast(i64) &
+ RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
+ data_.f.flags |= kIntFlag;
+ } else if (i64 >= static_cast(
+ RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
+ data_.f.flags |= kIntFlag;
+ }
+
+ //! Constructor for uint64_t value.
+ explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.u64 = u64;
+ data_.f.flags = kNumberUint64Flag;
+ if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000)))
+ data_.f.flags |= kInt64Flag;
+ if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
+ data_.f.flags |= kUintFlag;
+ if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
+ data_.f.flags |= kIntFlag;
+ }
+
+ //! Constructor for double value.
+ explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.d = d;
+ data_.f.flags = kNumberDoubleFlag;
+ }
+
+ //! Constructor for float value.
+ explicit GenericValue(float f) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.d = static_cast(f);
+ data_.f.flags = kNumberDoubleFlag;
+ }
+
+ //! Constructor for constant string (i.e. do not make a copy of string)
+ GenericValue(const Ch *s, SizeType length) RAPIDJSON_NOEXCEPT : data_() {
+ SetStringRaw(StringRef(s, length));
+ }
+
+ //! Constructor for constant string (i.e. do not make a copy of string)
+ explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_() {
+ SetStringRaw(s);
+ }
+
+ //! Constructor for copy-string (i.e. do make a copy of string)
+ GenericValue(const Ch *s, SizeType length, Allocator &allocator) : data_() {
+ SetStringRaw(StringRef(s, length), allocator);
+ }
+
+ //! Constructor for copy-string (i.e. do make a copy of string)
+ GenericValue(const Ch *s, Allocator &allocator) : data_() {
+ SetStringRaw(StringRef(s), allocator);
+ }
+
+#if RAPIDJSON_HAS_STDSTRING
+ //! Constructor for copy-string from a string object (i.e. do make a copy of
+ //! string)
+ /*! \note Requires the definition of the preprocessor symbol \ref
+ * RAPIDJSON_HAS_STDSTRING.
+ */
+ GenericValue(const std::basic_string &s, Allocator &allocator) : data_() {
+ SetStringRaw(StringRef(s), allocator);
+ }
+#endif
+
+ //! Constructor for Array.
+ /*!
+ \param a An array obtained by \c GetArray().
+ \note \c Array is always pass-by-value.
+ \note the source array is moved into this value and the sourec array
+ becomes empty.
+ */
+ GenericValue(Array a) RAPIDJSON_NOEXCEPT : data_(a.value_.data_) {
+ a.value_.data_ = Data();
+ a.value_.data_.f.flags = kArrayFlag;
+ }
+
+ //! Constructor for Object.
+ /*!
+ \param o An object obtained by \c GetObject().
+ \note \c Object is always pass-by-value.
+ \note the source object is moved into this value and the sourec object
+ becomes empty.
+ */
+ GenericValue(Object o) RAPIDJSON_NOEXCEPT : data_(o.value_.data_) {
+ o.value_.data_ = Data();
+ o.value_.data_.f.flags = kObjectFlag;
+ }
+
+ //! Destructor.
+ /*! Need to destruct elements of array, members of object, or copy-string.
+ */
+ ~GenericValue() {
+ if (Allocator::kNeedFree) { // Shortcut by Allocator's trait
+ switch (data_.f.flags) {
+ case kArrayFlag: {
+ GenericValue *e = GetElementsPointer();
+ for (GenericValue *v = e; v != e + data_.a.size; ++v)
+ v->~GenericValue();
+ Allocator::Free(e);
+ } break;
+
+ case kObjectFlag:
+ for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m)
+ m->~Member();
+ Allocator::Free(GetMembersPointer());
+ break;
+
+ case kCopyStringFlag:
+ Allocator::Free(const_cast(GetStringPointer()));
+ break;
+
+ default:
+ break; // Do nothing for other types.
+ }
+ }
+ }
+
+ //@}
+
+ //!@name Assignment operators
+ //@{
+
+ //! Assignment with move semantics.
+ /*! \param rhs Source of the assignment. It will become a null value after
+ * assignment.
+ */
+ GenericValue &operator=(GenericValue &rhs) RAPIDJSON_NOEXCEPT {
+ if (RAPIDJSON_LIKELY(this != &rhs)) {
+ this->~GenericValue();
+ RawAssign(rhs);
+ }
+ return *this;
+ }
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ //! Move assignment in C++11
+ GenericValue &operator=(GenericValue &&rhs) RAPIDJSON_NOEXCEPT {
+ return *this = rhs.Move();
+ }
+#endif
+
+ //! Assignment of constant string reference (no copy)
+ /*! \param str Constant string reference to be assigned
+ \note This overload is needed to avoid clashes with the generic primitive
+ type assignment overload below. \see GenericStringRef, operator=(T)
+ */
+ GenericValue &operator=(StringRefType str) RAPIDJSON_NOEXCEPT {
+ GenericValue s(str);
+ return *this = s;
+ }
+
+ //! Assignment with primitive types.
+ /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
+ \param value The value to be assigned.
+
+ \note The source type \c T explicitly disallows all pointer types,
+ especially (\c const) \ref Ch*. This helps avoiding implicitly
+ referencing character strings with insufficient lifetime, use
+ \ref SetString(const Ch*, Allocator&) (for copying) or
+ \ref StringRef() (to explicitly mark the pointer as constant) instead.
+ All other pointer types would implicitly convert to \c bool,
+ use \ref SetBool() instead.
+ */
+ template
+ RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer), (GenericValue &))
+ operator=(T value) {
+ GenericValue v(value);
+ return *this = v;
+ }
+
+ //! Deep-copy assignment from Value
+ /*! Assigns a \b copy of the Value to the current Value object
+ \tparam SourceAllocator Allocator type of \c rhs
+ \param rhs Value to copy from (read-only)
+ \param allocator Allocator to use for copying
+ \param copyConstStrings Force copying of constant strings (e.g.
+ referencing an in-situ buffer)
+ */
+ template
+ GenericValue &CopyFrom(const GenericValue &rhs,
+ Allocator &allocator, bool copyConstStrings = false) {
+ RAPIDJSON_ASSERT(static_cast(this) !=
+ static_cast(&rhs));
+ this->~GenericValue();
+ new (this) GenericValue(rhs, allocator, copyConstStrings);
+ return *this;
+ }
+
+ //! Exchange the contents of this value with those of other.
+ /*!
+ \param other Another value.
+ \note Constant complexity.
+ */
+ GenericValue &Swap(GenericValue &other) RAPIDJSON_NOEXCEPT {
+ GenericValue temp;
+ temp.RawAssign(*this);
+ RawAssign(other);
+ other.RawAssign(temp);
+ return *this;
+ }
+
+ //! free-standing swap function helper
+ /*!
+ Helper function to enable support for common swap implementation pattern
+ based on \c std::swap: \code void swap(MyClass& a, MyClass& b) { using
+ std::swap; swap(a.value, b.value);
+ // ...
+ }
+ \endcode
+ \see Swap()
+ */
+ friend inline void swap(GenericValue &a, GenericValue &b) RAPIDJSON_NOEXCEPT {
+ a.Swap(b);
+ }
+
+ //! Prepare Value for move semantics
+ /*! \return *this */
+ GenericValue &Move() RAPIDJSON_NOEXCEPT { return *this; }
+ //@}
+
+ //!@name Equal-to and not-equal-to operators
+ //@{
+ //! Equal-to operator
+ /*!
+ \note If an object contains duplicated named member, comparing equality
+ with any object is always \c false. \note Complexity is quadratic in
+ Object's member number and linear for the rest (number of all values in the
+ subtree and total lengths of all strings).
+ */
+ template
+ bool operator==(const GenericValue &rhs) const {
+ typedef GenericValue RhsType;
+ if (GetType() != rhs.GetType()) return false;
+
+ switch (GetType()) {
+ case kObjectType: // Warning: O(n^2) inner-loop
+ if (data_.o.size != rhs.data_.o.size) return false;
+ for (ConstMemberIterator lhsMemberItr = MemberBegin();
+ lhsMemberItr != MemberEnd(); ++lhsMemberItr) {
+ typename RhsType::ConstMemberIterator rhsMemberItr =
+ rhs.FindMember(lhsMemberItr->name);
+ if (rhsMemberItr == rhs.MemberEnd() ||
+ lhsMemberItr->value != rhsMemberItr->value)
+ return false;
+ }
+ return true;
+
+ case kArrayType:
+ if (data_.a.size != rhs.data_.a.size) return false;
+ for (SizeType i = 0; i < data_.a.size; i++)
+ if ((*this)[i] != rhs[i]) return false;
+ return true;
+
+ case kStringType:
+ return StringEqual(rhs);
+
+ case kNumberType:
+ if (IsDouble() || rhs.IsDouble()) {
+ double a = GetDouble(); // May convert from integer to double.
+ double b = rhs.GetDouble(); // Ditto
+ return a >= b && a <= b; // Prevent -Wfloat-equal
+ } else
+ return data_.n.u64 == rhs.data_.n.u64;
+
+ default:
+ return true;
+ }
+ }
+
+ //! Equal-to operator with const C-string pointer
+ bool operator==(const Ch *rhs) const {
+ return *this == GenericValue(StringRef(rhs));
+ }
+
+#if RAPIDJSON_HAS_STDSTRING
+ //! Equal-to operator with string object
+ /*! \note Requires the definition of the preprocessor symbol \ref
+ * RAPIDJSON_HAS_STDSTRING.
+ */
+ bool operator==(const std::basic_string &rhs) const {
+ return *this == GenericValue(StringRef(rhs));
+ }
+#endif
+
+ //! Equal-to operator with primitive types
+ /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t,
+ * \c double, \c true, \c false
+ */
+ template
+ RAPIDJSON_DISABLEIF_RETURN(
+ (internal::OrExpr, internal::IsGenericValue>),
+ (bool))
+ operator==(const T &rhs) const {
+ return *this == GenericValue(rhs);
+ }
+
+ //! Not-equal-to operator
+ /*! \return !(*this == rhs)
+ */
+ template
+ bool operator!=(const GenericValue &rhs) const {
+ return !(*this == rhs);
+ }
+
+ //! Not-equal-to operator with const C-string pointer
+ bool operator!=(const Ch *rhs) const { return !(*this == rhs); }
+
+ //! Not-equal-to operator with arbitrary types
+ /*! \return !(*this == rhs)
+ */
+ template
+ RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool))
+ operator!=(const T &rhs) const {
+ return !(*this == rhs);
+ }
+
+ //! Equal-to operator with arbitrary types (symmetric version)
+ /*! \return (rhs == lhs)
+ */
+ template
+ friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool))
+ operator==(const T &lhs, const GenericValue &rhs) {
+ return rhs == lhs;
+ }
+
+ //! Not-Equal-to operator with arbitrary types (symmetric version)
+ /*! \return !(rhs == lhs)
+ */
+ template
+ friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool))
+ operator!=(const T &lhs, const GenericValue &rhs) {
+ return !(rhs == lhs);
+ }
+ //@}
+
+ //!@name Type
+ //@{
+
+ Type GetType() const { return static_cast(data_.f.flags & kTypeMask); }
+ bool IsNull() const { return data_.f.flags == kNullFlag; }
+ bool IsFalse() const { return data_.f.flags == kFalseFlag; }
+ bool IsTrue() const { return data_.f.flags == kTrueFlag; }
+ bool IsBool() const { return (data_.f.flags & kBoolFlag) != 0; }
+ bool IsObject() const { return data_.f.flags == kObjectFlag; }
+ bool IsArray() const { return data_.f.flags == kArrayFlag; }
+ bool IsNumber() const { return (data_.f.flags & kNumberFlag) != 0; }
+ bool IsInt() const { return (data_.f.flags & kIntFlag) != 0; }
+ bool IsUint() const { return (data_.f.flags & kUintFlag) != 0; }
+ bool IsInt64() const { return (data_.f.flags & kInt64Flag) != 0; }
+ bool IsUint64() const { return (data_.f.flags & kUint64Flag) != 0; }
+ bool IsDouble() const { return (data_.f.flags & kDoubleFlag) != 0; }
+ bool IsString() const { return (data_.f.flags & kStringFlag) != 0; }
+
+ // Checks whether a number can be losslessly converted to a double.
+ bool IsLosslessDouble() const {
+ if (!IsNumber()) return false;
+ if (IsUint64()) {
+ uint64_t u = GetUint64();
+ volatile double d = static_cast(u);
+ return (d >= 0.0) &&
+ (d <
+ static_cast((std::numeric_limits::max)())) &&
+ (u == static_cast(d));
+ }
+ if (IsInt64()) {
+ int64_t i = GetInt64();
+ volatile double d = static_cast(i);
+ return (d >=
+ static_cast((std::numeric_limits::min)())) &&
+ (d < static_cast((std::numeric_limits::max)())) &&
+ (i == static_cast(d));
+ }
+ return true; // double, int, uint are always lossless
+ }
+
+ // Checks whether a number is a float (possible lossy).
+ bool IsFloat() const {
+ if ((data_.f.flags & kDoubleFlag) == 0) return false;
+ double d = GetDouble();
+ return d >= -3.4028234e38 && d <= 3.4028234e38;
+ }
+ // Checks whether a number can be losslessly converted to a float.
+ bool IsLosslessFloat() const {
+ if (!IsNumber()) return false;
+ double a = GetDouble();
+ if (a < static_cast(-(std::numeric_limits::max)()) ||
+ a > static_cast((std::numeric_limits::max)()))
+ return false;
+ double b = static_cast(static_cast(a));
+ return a >= b && a <= b; // Prevent -Wfloat-equal
+ }
+
+ //@}
+
+ //!@name Null
+ //@{
+
+ GenericValue &SetNull() {
+ this->~GenericValue();
+ new (this) GenericValue();
+ return *this;
+ }
+
+ //@}
+
+ //!@name Bool
+ //@{
+
+ bool GetBool() const {
+ RAPIDJSON_ASSERT(IsBool());
+ return data_.f.flags == kTrueFlag;
+ }
+ //!< Set boolean value
+ /*! \post IsBool() == true */
+ GenericValue &SetBool(bool b) {
+ this->~GenericValue();
+ new (this) GenericValue(b);
+ return *this;
+ }
+
+ //@}
+
+ //!@name Object
+ //@{
+
+ //! Set this value as an empty object.
+ /*! \post IsObject() == true */
+ GenericValue &SetObject() {
+ this->~GenericValue();
+ new (this) GenericValue(kObjectType);
+ return *this;
+ }
+
+ //! Get the number of members in the object.
+ SizeType MemberCount() const {
+ RAPIDJSON_ASSERT(IsObject());
+ return data_.o.size;
+ }
+
+ //! Get the capacity of object.
+ SizeType MemberCapacity() const {
+ RAPIDJSON_ASSERT(IsObject());
+ return data_.o.capacity;
+ }
+
+ //! Check whether the object is empty.
+ bool ObjectEmpty() const {
+ RAPIDJSON_ASSERT(IsObject());
+ return data_.o.size == 0;
+ }
+
+ //! Get a value from an object associated with the name.
+ /*! \pre IsObject() == true
+ \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation
+ with \ref operator[](SizeType)) \note In version 0.1x, if the member is not
+ found, this function returns a null value. This makes issue 7. Since 0.2,
+ if the name is not correct, it will assert. If user is unsure whether a
+ member exists, user should use HasMember() first. A better approach is to
+ use FindMember(). \note Linear time complexity.
+ */
+ template
+ RAPIDJSON_DISABLEIF_RETURN(
+ (internal::NotExpr<
+ internal::IsSame::Type, Ch>>),
+ (GenericValue &))
+ operator[](T *name) {
+ GenericValue n(StringRef(name));
+ return (*this)[n];
+ }
+ template
+ RAPIDJSON_DISABLEIF_RETURN(
+ (internal::NotExpr<
+ internal::IsSame::Type, Ch>>),
+ (const GenericValue &))
+ operator[](T *name) const {
+ return const_cast(*this)[name];
+ }
+
+ //! Get a value from an object associated with the name.
+ /*! \pre IsObject() == true
+ \tparam SourceAllocator Allocator of the \c name value
+
+ \note Compared to \ref operator[](T*), this version is faster because it
+ does not need a StrLen(). And it can also handle strings with embedded null
+ characters.
+
+ \note Linear time complexity.
+ */
+ template
+ GenericValue &operator[](
+ const GenericValue &name) {
+ MemberIterator member = FindMember(name);
+ if (member != MemberEnd())
+ return member->value;
+ else {
+ RAPIDJSON_ASSERT(false); // see above note
+
+ // This will generate -Wexit-time-destructors in clang
+ // static GenericValue NullValue;
+ // return NullValue;
+
+ // Use static buffer and placement-new to prevent destruction
+ static char buffer[sizeof(GenericValue)];
+ return *new (buffer) GenericValue();
+ }
+ }
+ template
+ const GenericValue &operator[](
+ const GenericValue &name) const {
+ return const_cast(*this)[name];
+ }
+
+#if RAPIDJSON_HAS_STDSTRING
+ //! Get a value from an object associated with name (string object).
+ GenericValue &operator[](const std::basic_string &name) {
+ return (*this)[GenericValue(StringRef(name))];
+ }
+ const GenericValue &operator[](const std::basic_string &name) const {
+ return (*this)[GenericValue(StringRef(name))];
+ }
+#endif
+
+ //! Const member iterator
+ /*! \pre IsObject() == true */
+ ConstMemberIterator MemberBegin() const {
+ RAPIDJSON_ASSERT(IsObject());
+ return ConstMemberIterator(GetMembersPointer());
+ }
+ //! Const \em past-the-end member iterator
+ /*! \pre IsObject() == true */
+ ConstMemberIterator MemberEnd() const {
+ RAPIDJSON_ASSERT(IsObject());
+ return ConstMemberIterator(GetMembersPointer() + data_.o.size);
+ }
+ //! Member iterator
+ /*! \pre IsObject() == true */
+ MemberIterator MemberBegin() {
+ RAPIDJSON_ASSERT(IsObject());
+ return MemberIterator(GetMembersPointer());
+ }
+ //! \em Past-the-end member iterator
+ /*! \pre IsObject() == true */
+ MemberIterator MemberEnd() {
+ RAPIDJSON_ASSERT(IsObject());
+ return MemberIterator(GetMembersPointer() + data_.o.size);
+ }
+
+ //! Request the object to have enough capacity to store members.
+ /*! \param newCapacity The capacity that the object at least need to have.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one as used before. Commonly use GenericDocument::GetAllocator(). \return
+ The value itself for fluent API. \note Linear time complexity.
+ */
+ GenericValue &MemberReserve(SizeType newCapacity, Allocator &allocator) {
+ RAPIDJSON_ASSERT(IsObject());
+ if (newCapacity > data_.o.capacity) {
+ SetMembersPointer(reinterpret_cast(allocator.Realloc(
+ GetMembersPointer(), data_.o.capacity * sizeof(Member),
+ newCapacity * sizeof(Member))));
+ data_.o.capacity = newCapacity;
+ }
+ return *this;
+ }
+
+ //! Check whether a member exists in the object.
+ /*!
+ \param name Member name to be searched.
+ \pre IsObject() == true
+ \return Whether a member with that name exists.
+ \note It is better to use FindMember() directly if you need the obtain the
+ value as well. \note Linear time complexity.
+ */
+ bool HasMember(const Ch *name) const {
+ return FindMember(name) != MemberEnd();
+ }
+
+#if RAPIDJSON_HAS_STDSTRING
+ //! Check whether a member exists in the object with string object.
+ /*!
+ \param name Member name to be searched.
+ \pre IsObject() == true
+ \return Whether a member with that name exists.
+ \note It is better to use FindMember() directly if you need the obtain the
+ value as well. \note Linear time complexity.
+ */
+ bool HasMember(const std::basic_string &name) const {
+ return FindMember(name) != MemberEnd();
+ }
+#endif
+
+ //! Check whether a member exists in the object with GenericValue name.
+ /*!
+ This version is faster because it does not need a StrLen(). It can also
+ handle string with null character. \param name Member name to be searched.
+ \pre IsObject() == true
+ \return Whether a member with that name exists.
+ \note It is better to use FindMember() directly if you need the obtain the
+ value as well. \note Linear time complexity.
+ */
+ template
+ bool HasMember(const GenericValue &name) const {
+ return FindMember(name) != MemberEnd();
+ }
+
+ //! Find member by name.
+ /*!
+ \param name Member name to be searched.
+ \pre IsObject() == true
+ \return Iterator to member, if it exists.
+ Otherwise returns \ref MemberEnd().
+
+ \note Earlier versions of Rapidjson returned a \c NULL pointer, in case
+ the requested member doesn't exist. For consistency with e.g.
+ \c std::map, this has been changed to MemberEnd() now.
+ \note Linear time complexity.
+ */
+ MemberIterator FindMember(const Ch *name) {
+ GenericValue n(StringRef(name));
+ return FindMember(n);
+ }
+
+ ConstMemberIterator FindMember(const Ch *name) const {
+ return const_cast(*this).FindMember(name);
+ }
+
+ //! Find member by name.
+ /*!
+ This version is faster because it does not need a StrLen(). It can also
+ handle string with null character. \param name Member name to be searched.
+ \pre IsObject() == true
+ \return Iterator to member, if it exists.
+ Otherwise returns \ref MemberEnd().
+
+ \note Earlier versions of Rapidjson returned a \c NULL pointer, in case
+ the requested member doesn't exist. For consistency with e.g.
+ \c std::map, this has been changed to MemberEnd() now.
+ \note Linear time complexity.
+ */
+ template
+ MemberIterator FindMember(
+ const GenericValue &name) {
+ RAPIDJSON_ASSERT(IsObject());
+ RAPIDJSON_ASSERT(name.IsString());
+ MemberIterator member = MemberBegin();
+ for (; member != MemberEnd(); ++member)
+ if (name.StringEqual(member->name)) break;
+ return member;
+ }
+ template
+ ConstMemberIterator FindMember(
+ const GenericValue &name) const {
+ return const_cast(*this).FindMember(name);
+ }
+
+#if RAPIDJSON_HAS_STDSTRING
+ //! Find member by string object name.
+ /*!
+ \param name Member name to be searched.
+ \pre IsObject() == true
+ \return Iterator to member, if it exists.
+ Otherwise returns \ref MemberEnd().
+ */
+ MemberIterator FindMember(const std::basic_string &name) {
+ return FindMember(GenericValue(StringRef(name)));
+ }
+ ConstMemberIterator FindMember(const std::basic_string &name) const {
+ return FindMember(GenericValue(StringRef(name)));
+ }
+#endif
+
+ //! Add a member (name-value pair) to the object.
+ /*! \param name A string value as name of member.
+ \param value Value of any type.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one as used before. Commonly use GenericDocument::GetAllocator(). \return
+ The value itself for fluent API. \note The ownership of \c name and \c
+ value will be transferred to this object on success. \pre IsObject() &&
+ name.IsString() \post name.IsNull() && value.IsNull() \note Amortized
+ Constant time complexity.
+ */
+ GenericValue &AddMember(GenericValue &name, GenericValue &value,
+ Allocator &allocator) {
+ RAPIDJSON_ASSERT(IsObject());
+ RAPIDJSON_ASSERT(name.IsString());
+
+ ObjectData &o = data_.o;
+ if (o.size >= o.capacity)
+ MemberReserve(o.capacity == 0 ? kDefaultObjectCapacity
+ : (o.capacity + (o.capacity + 1) / 2),
+ allocator);
+ Member *members = GetMembersPointer();
+ members[o.size].name.RawAssign(name);
+ members[o.size].value.RawAssign(value);
+ o.size++;
+ return *this;
+ }
+
+ //! Add a constant string value as member (name-value pair) to the object.
+ /*! \param name A string value as name of member.
+ \param value constant string reference as value of member.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one as used before. Commonly use GenericDocument::GetAllocator(). \return
+ The value itself for fluent API. \pre IsObject() \note This overload is
+ needed to avoid clashes with the generic primitive type
+ AddMember(GenericValue&,T,Allocator&) overload below. \note Amortized
+ Constant time complexity.
+ */
+ GenericValue &AddMember(GenericValue &name, StringRefType value,
+ Allocator &allocator) {
+ GenericValue v(value);
+ return AddMember(name, v, allocator);
+ }
+
+#if RAPIDJSON_HAS_STDSTRING
+ //! Add a string object as member (name-value pair) to the object.
+ /*! \param name A string value as name of member.
+ \param value constant string reference as value of member.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one as used before. Commonly use GenericDocument::GetAllocator(). \return
+ The value itself for fluent API. \pre IsObject() \note This overload is
+ needed to avoid clashes with the generic primitive type
+ AddMember(GenericValue&,T,Allocator&) overload below. \note Amortized
+ Constant time complexity.
+ */
+ GenericValue &AddMember(GenericValue &name, std::basic_string &value,
+ Allocator &allocator) {
+ GenericValue v(value, allocator);
+ return AddMember(name, v, allocator);
+ }
+#endif
+
+ //! Add any primitive value as member (name-value pair) to the object.
+ /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
+ \param name A string value as name of member.
+ \param value Value of primitive type \c T as value of member
+ \param allocator Allocator for reallocating memory. Commonly use
+ GenericDocument::GetAllocator(). \return The value itself for fluent API.
+ \pre IsObject()
+
+ \note The source type \c T explicitly disallows all pointer types,
+ especially (\c const) \ref Ch*. This helps avoiding implicitly
+ referencing character strings with insufficient lifetime, use
+ \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref
+ AddMember(StringRefType, StringRefType, Allocator&).
+ All other pointer types would implicitly convert to \c bool,
+ use an explicit cast instead, if needed.
+ \note Amortized Constant time complexity.
+ */
+ template
+ RAPIDJSON_DISABLEIF_RETURN(
+ (internal::OrExpr, internal::IsGenericValue>),
+ (GenericValue &))
+ AddMember(GenericValue &name, T value, Allocator &allocator) {
+ GenericValue v(value);
+ return AddMember(name, v, allocator);
+ }
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ GenericValue &AddMember(GenericValue &&name, GenericValue &&value,
+ Allocator &allocator) {
+ return AddMember(name, value, allocator);
+ }
+ GenericValue &AddMember(GenericValue &&name, GenericValue &value,
+ Allocator &allocator) {
+ return AddMember(name, value, allocator);
+ }
+ GenericValue &AddMember(GenericValue &name, GenericValue &&value,
+ Allocator &allocator) {
+ return AddMember(name, value, allocator);
+ }
+ GenericValue &AddMember(StringRefType name, GenericValue &&value,
+ Allocator &allocator) {
+ GenericValue n(name);
+ return AddMember(n, value, allocator);
+ }
+#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
+
+ //! Add a member (name-value pair) to the object.
+ /*! \param name A constant string reference as name of member.
+ \param value Value of any type.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one as used before. Commonly use GenericDocument::GetAllocator(). \return
+ The value itself for fluent API. \note The ownership of \c value will be
+ transferred to this object on success. \pre IsObject() \post
+ value.IsNull() \note Amortized Constant time complexity.
+ */
+ GenericValue &AddMember(StringRefType name, GenericValue &value,
+ Allocator &allocator) {
+ GenericValue n(name);
+ return AddMember(n, value, allocator);
+ }
+
+ //! Add a constant string value as member (name-value pair) to the object.
+ /*! \param name A constant string reference as name of member.
+ \param value constant string reference as value of member.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one as used before. Commonly use GenericDocument::GetAllocator(). \return
+ The value itself for fluent API. \pre IsObject() \note This overload is
+ needed to avoid clashes with the generic primitive type
+ AddMember(StringRefType,T,Allocator&) overload below. \note Amortized
+ Constant time complexity.
+ */
+ GenericValue &AddMember(StringRefType name, StringRefType value,
+ Allocator &allocator) {
+ GenericValue v(value);
+ return AddMember(name, v, allocator);
+ }
+
+ //! Add any primitive value as member (name-value pair) to the object.
+ /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
+ \param name A constant string reference as name of member.
+ \param value Value of primitive type \c T as value of member
+ \param allocator Allocator for reallocating memory. Commonly use
+ GenericDocument::GetAllocator(). \return The value itself for fluent API.
+ \pre IsObject()
+
+ \note The source type \c T explicitly disallows all pointer types,
+ especially (\c const) \ref Ch*. This helps avoiding implicitly
+ referencing character strings with insufficient lifetime, use
+ \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref
+ AddMember(StringRefType, StringRefType, Allocator&).
+ All other pointer types would implicitly convert to \c bool,
+ use an explicit cast instead, if needed.
+ \note Amortized Constant time complexity.
+ */
+ template
+ RAPIDJSON_DISABLEIF_RETURN(
+ (internal::OrExpr, internal::IsGenericValue>),
+ (GenericValue &))
+ AddMember(StringRefType name, T value, Allocator &allocator) {
+ GenericValue n(name);
+ return AddMember(n, value, allocator);
+ }
+
+ //! Remove all members in the object.
+ /*! This function do not deallocate memory in the object, i.e. the capacity is
+ unchanged. \note Linear time complexity.
+ */
+ void RemoveAllMembers() {
+ RAPIDJSON_ASSERT(IsObject());
+ for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) m->~Member();
+ data_.o.size = 0;
+ }
+
+ //! Remove a member in object by its name.
+ /*! \param name Name of member to be removed.
+ \return Whether the member existed.
+ \note This function may reorder the object members. Use \ref
+ EraseMember(ConstMemberIterator) if you need to preserve the
+ relative order of the remaining members.
+ \note Linear time complexity.
+ */
+ bool RemoveMember(const Ch *name) {
+ GenericValue n(StringRef(name));
+ return RemoveMember(n);
+ }
+
+#if RAPIDJSON_HAS_STDSTRING
+ bool RemoveMember(const std::basic_string &name) {
+ return RemoveMember(GenericValue(StringRef(name)));
+ }
+#endif
+
+ template
+ bool RemoveMember(const GenericValue &name) {
+ MemberIterator m = FindMember(name);
+ if (m != MemberEnd()) {
+ RemoveMember(m);
+ return true;
+ } else
+ return false;
+ }
+
+ //! Remove a member in object by iterator.
+ /*! \param m member iterator (obtained by FindMember() or MemberBegin()).
+ \return the new iterator after removal.
+ \note This function may reorder the object members. Use \ref
+ EraseMember(ConstMemberIterator) if you need to preserve the
+ relative order of the remaining members.
+ \note Constant time complexity.
+ */
+ MemberIterator RemoveMember(MemberIterator m) {
+ RAPIDJSON_ASSERT(IsObject());
+ RAPIDJSON_ASSERT(data_.o.size > 0);
+ RAPIDJSON_ASSERT(GetMembersPointer() != 0);
+ RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd());
+
+ MemberIterator last(GetMembersPointer() + (data_.o.size - 1));
+ if (data_.o.size > 1 && m != last)
+ *m = *last; // Move the last one to this place
+ else
+ m->~Member(); // Only one left, just destroy
+ --data_.o.size;
+ return m;
+ }
+
+ //! Remove a member from an object by iterator.
+ /*! \param pos iterator to the member to remove
+ \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd()
+ \return Iterator following the removed element.
+ If the iterator \c pos refers to the last element, the \ref
+ MemberEnd() iterator is returned. \note This function preserves the
+ relative order of the remaining object members. If you do not need this,
+ use the more efficient \ref RemoveMember(MemberIterator). \note Linear time
+ complexity.
+ */
+ MemberIterator EraseMember(ConstMemberIterator pos) {
+ return EraseMember(pos, pos + 1);
+ }
+
+ //! Remove members in the range [first, last) from an object.
+ /*! \param first iterator to the first member to remove
+ \param last iterator following the last member to remove
+ \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <=
+ \ref MemberEnd() \return Iterator following the last removed element. \note
+ This function preserves the relative order of the remaining object members.
+ \note Linear time complexity.
+ */
+ MemberIterator EraseMember(ConstMemberIterator first,
+ ConstMemberIterator last) {
+ RAPIDJSON_ASSERT(IsObject());
+ RAPIDJSON_ASSERT(data_.o.size > 0);
+ RAPIDJSON_ASSERT(GetMembersPointer() != 0);
+ RAPIDJSON_ASSERT(first >= MemberBegin());
+ RAPIDJSON_ASSERT(first <= last);
+ RAPIDJSON_ASSERT(last <= MemberEnd());
+
+ MemberIterator pos = MemberBegin() + (first - MemberBegin());
+ for (MemberIterator itr = pos; itr != last; ++itr) itr->~Member();
+ std::memmove(static_cast(&*pos), &*last,
+ static_cast(MemberEnd() - last) * sizeof(Member));
+ data_.o.size -= static_cast(last - first);
+ return pos;
+ }
+
+ //! Erase a member in object by its name.
+ /*! \param name Name of member to be removed.
+ \return Whether the member existed.
+ \note Linear time complexity.
+ */
+ bool EraseMember(const Ch *name) {
+ GenericValue n(StringRef(name));
+ return EraseMember(n);
+ }
+
+#if RAPIDJSON_HAS_STDSTRING
+ bool EraseMember(const std::basic_string &name) {
+ return EraseMember(GenericValue(StringRef(name)));
+ }
+#endif
+
+ template
+ bool EraseMember(const GenericValue &name) {
+ MemberIterator m = FindMember(name);
+ if (m != MemberEnd()) {
+ EraseMember(m);
+ return true;
+ } else
+ return false;
+ }
+
+ Object GetObject() {
+ RAPIDJSON_ASSERT(IsObject());
+ return Object(*this);
+ }
+ ConstObject GetObject() const {
+ RAPIDJSON_ASSERT(IsObject());
+ return ConstObject(*this);
+ }
+
+ //@}
+
+ //!@name Array
+ //@{
+
+ //! Set this value as an empty array.
+ /*! \post IsArray == true */
+ GenericValue &SetArray() {
+ this->~GenericValue();
+ new (this) GenericValue(kArrayType);
+ return *this;
+ }
+
+ //! Get the number of elements in array.
+ SizeType Size() const {
+ RAPIDJSON_ASSERT(IsArray());
+ return data_.a.size;
+ }
+
+ //! Get the capacity of array.
+ SizeType Capacity() const {
+ RAPIDJSON_ASSERT(IsArray());
+ return data_.a.capacity;
+ }
+
+ //! Check whether the array is empty.
+ bool Empty() const {
+ RAPIDJSON_ASSERT(IsArray());
+ return data_.a.size == 0;
+ }
+
+ //! Remove all elements in the array.
+ /*! This function do not deallocate memory in the array, i.e. the capacity is
+ unchanged. \note Linear time complexity.
+ */
+ void Clear() {
+ RAPIDJSON_ASSERT(IsArray());
+ GenericValue *e = GetElementsPointer();
+ for (GenericValue *v = e; v != e + data_.a.size; ++v) v->~GenericValue();
+ data_.a.size = 0;
+ }
+
+ //! Get an element from array by index.
+ /*! \pre IsArray() == true
+ \param index Zero-based index of element.
+ \see operator[](T*)
+ */
+ GenericValue &operator[](SizeType index) {
+ RAPIDJSON_ASSERT(IsArray());
+ RAPIDJSON_ASSERT(index < data_.a.size);
+ return GetElementsPointer()[index];
+ }
+ const GenericValue &operator[](SizeType index) const {
+ return const_cast(*this)[index];
+ }
+
+ //! Element iterator
+ /*! \pre IsArray() == true */
+ ValueIterator Begin() {
+ RAPIDJSON_ASSERT(IsArray());
+ return GetElementsPointer();
+ }
+ //! \em Past-the-end element iterator
+ /*! \pre IsArray() == true */
+ ValueIterator End() {
+ RAPIDJSON_ASSERT(IsArray());
+ return GetElementsPointer() + data_.a.size;
+ }
+ //! Constant element iterator
+ /*! \pre IsArray() == true */
+ ConstValueIterator Begin() const {
+ return const_cast(*this).Begin();
+ }
+ //! Constant \em past-the-end element iterator
+ /*! \pre IsArray() == true */
+ ConstValueIterator End() const {
+ return const_cast(*this).End();
+ }
+
+ //! Request the array to have enough capacity to store elements.
+ /*! \param newCapacity The capacity that the array at least need to have.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one as used before. Commonly use GenericDocument::GetAllocator(). \return
+ The value itself for fluent API. \note Linear time complexity.
+ */
+ GenericValue &Reserve(SizeType newCapacity, Allocator &allocator) {
+ RAPIDJSON_ASSERT(IsArray());
+ if (newCapacity > data_.a.capacity) {
+ SetElementsPointer(reinterpret_cast(allocator.Realloc(
+ GetElementsPointer(), data_.a.capacity * sizeof(GenericValue),
+ newCapacity * sizeof(GenericValue))));
+ data_.a.capacity = newCapacity;
+ }
+ return *this;
+ }
+
+ //! Append a GenericValue at the end of the array.
+ /*! \param value Value to be appended.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one as used before. Commonly use GenericDocument::GetAllocator(). \pre
+ IsArray() == true \post value.IsNull() == true \return The value itself for
+ fluent API. \note The ownership of \c value will be transferred to this
+ array on success. \note If the number of elements to be appended is known,
+ calls Reserve() once first may be more efficient. \note Amortized constant
+ time complexity.
+ */
+ GenericValue &PushBack(GenericValue &value, Allocator &allocator) {
+ RAPIDJSON_ASSERT(IsArray());
+ if (data_.a.size >= data_.a.capacity)
+ Reserve(data_.a.capacity == 0
+ ? kDefaultArrayCapacity
+ : (data_.a.capacity + (data_.a.capacity + 1) / 2),
+ allocator);
+ GetElementsPointer()[data_.a.size++].RawAssign(value);
+ return *this;
+ }
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ GenericValue &PushBack(GenericValue &&value, Allocator &allocator) {
+ return PushBack(value, allocator);
+ }
+#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
+
+ //! Append a constant string reference at the end of the array.
+ /*! \param value Constant string reference to be appended.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one used previously. Commonly use GenericDocument::GetAllocator(). \pre
+ IsArray() == true \return The value itself for fluent API. \note If the
+ number of elements to be appended is known, calls Reserve() once first may
+ be more efficient. \note Amortized constant time complexity. \see
+ GenericStringRef
+ */
+ GenericValue &PushBack(StringRefType value, Allocator &allocator) {
+ return (*this).template PushBack(value, allocator);
+ }
+
+ //! Append a primitive value at the end of the array.
+ /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
+ \param value Value of primitive type T to be appended.
+ \param allocator Allocator for reallocating memory. It must be the same
+ one as used before. Commonly use GenericDocument::GetAllocator(). \pre
+ IsArray() == true \return The value itself for fluent API. \note If the
+ number of elements to be appended is known, calls Reserve() once first may
+ be more efficient.
+
+ \note The source type \c T explicitly disallows all pointer types,
+ especially (\c const) \ref Ch*. This helps avoiding implicitly
+ referencing character strings with insufficient lifetime, use
+ \ref PushBack(GenericValue&, Allocator&) or \ref
+ PushBack(StringRefType, Allocator&).
+ All other pointer types would implicitly convert to \c bool,
+ use an explicit cast instead, if needed.
+ \note Amortized constant time complexity.
+ */
+ template
+ RAPIDJSON_DISABLEIF_RETURN(
+ (internal::OrExpr, internal::IsGenericValue>),
+ (GenericValue &))
+ PushBack(T value, Allocator &allocator) {
+ GenericValue v(value);
+ return PushBack(v, allocator);
+ }
+
+ //! Remove the last element in the array.
+ /*!
+ \note Constant time complexity.
+ */
+ GenericValue &PopBack() {
+ RAPIDJSON_ASSERT(IsArray());
+ RAPIDJSON_ASSERT(!Empty());
+ GetElementsPointer()[--data_.a.size].~GenericValue();
+ return *this;
+ }
+
+ //! Remove an element of array by iterator.
+ /*!
+ \param pos iterator to the element to remove
+ \pre IsArray() == true && \ref Begin() <= \c pos < \ref End()
+ \return Iterator following the removed element. If the iterator pos refers
+ to the last element, the End() iterator is returned. \note Linear time
+ complexity.
+ */
+ ValueIterator Erase(ConstValueIterator pos) { return Erase(pos, pos + 1); }
+
+ //! Remove elements in the range [first, last) of the array.
+ /*!
+ \param first iterator to the first element to remove
+ \param last iterator following the last element to remove
+ \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref
+ End() \return Iterator following the last removed element. \note Linear
+ time complexity.
+ */
+ ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) {
+ RAPIDJSON_ASSERT(IsArray());
+ RAPIDJSON_ASSERT(data_.a.size > 0);
+ RAPIDJSON_ASSERT(GetElementsPointer() != 0);
+ RAPIDJSON_ASSERT(first >= Begin());
+ RAPIDJSON_ASSERT(first <= last);
+ RAPIDJSON_ASSERT(last <= End());
+ ValueIterator pos = Begin() + (first - Begin());
+ for (ValueIterator itr = pos; itr != last; ++itr) itr->~GenericValue();
+ std::memmove(static_cast(pos), last,
+ static_cast(End() - last) * sizeof(GenericValue));
+ data_.a.size -= static_cast(last - first);
+ return pos;
+ }
+
+ Array GetArray() {
+ RAPIDJSON_ASSERT(IsArray());
+ return Array(*this);
+ }
+ ConstArray GetArray() const {
+ RAPIDJSON_ASSERT(IsArray());
+ return ConstArray(*this);
+ }
+
+ //@}
+
+ //!@name Number
+ //@{
+
+ int GetInt() const {
+ RAPIDJSON_ASSERT(data_.f.flags & kIntFlag);
+ return data_.n.i.i;
+ }
+ unsigned GetUint() const {
+ RAPIDJSON_ASSERT(data_.f.flags & kUintFlag);
+ return data_.n.u.u;
+ }
+ int64_t GetInt64() const {
+ RAPIDJSON_ASSERT(data_.f.flags & kInt64Flag);
+ return data_.n.i64;
+ }
+ uint64_t GetUint64() const {
+ RAPIDJSON_ASSERT(data_.f.flags & kUint64Flag);
+ return data_.n.u64;
+ }
+
+ //! Get the value as double type.
+ /*! \note If the value is 64-bit integer type, it may lose precision. Use \c
+ * IsLosslessDouble() to check whether the converison is lossless.
+ */
+ double GetDouble() const {
+ RAPIDJSON_ASSERT(IsNumber());
+ if ((data_.f.flags & kDoubleFlag) != 0)
+ return data_.n.d; // exact type, no conversion.
+ if ((data_.f.flags & kIntFlag) != 0) return data_.n.i.i; // int -> double
+ if ((data_.f.flags & kUintFlag) != 0)
+ return data_.n.u.u; // unsigned -> double
+ if ((data_.f.flags & kInt64Flag) != 0)
+ return static_cast(
+ data_.n.i64); // int64_t -> double (may lose precision)
+ RAPIDJSON_ASSERT((data_.f.flags & kUint64Flag) != 0);
+ return static_cast(
+ data_.n.u64); // uint64_t -> double (may lose precision)
+ }
+
+ //! Get the value as float type.
+ /*! \note If the value is 64-bit integer type, it may lose precision. Use \c
+ * IsLosslessFloat() to check whether the converison is lossless.
+ */
+ float GetFloat() const { return static_cast(GetDouble()); }
+
+ GenericValue &SetInt(int i) {
+ this->~GenericValue();
+ new (this) GenericValue(i);
+ return *this;
+ }
+ GenericValue &SetUint(unsigned u) {
+ this->~GenericValue();
+ new (this) GenericValue(u);
+ return *this;
+ }
+ GenericValue &SetInt64(int64_t i64) {
+ this->~GenericValue();
+ new (this) GenericValue(i64);
+ return *this;
+ }
+ GenericValue &SetUint64(uint64_t u64) {
+ this->~GenericValue();
+ new (this) GenericValue(u64);
+ return *this;
+ }
+ GenericValue &SetDouble(double d) {
+ this->~GenericValue();
+ new (this) GenericValue(d);
+ return *this;
+ }
+ GenericValue &SetFloat(float f) {
+ this->~GenericValue();
+ new (this) GenericValue(static_cast(f));
+ return *this;
+ }
+
+ //@}
+
+ //!@name String
+ //@{
+
+ const Ch *GetString() const {
+ RAPIDJSON_ASSERT(IsString());
+ return (data_.f.flags & kInlineStrFlag) ? data_.ss.str : GetStringPointer();
+ }
+
+ //! Get the length of string.
+ /*! Since rapidjson permits "\\u0000" in the json string,
+ * strlen(v.GetString()) may not equal to v.GetStringLength().
+ */
+ SizeType GetStringLength() const {
+ RAPIDJSON_ASSERT(IsString());
+ return ((data_.f.flags & kInlineStrFlag) ? (data_.ss.GetLength())
+ : data_.s.length);
+ }
+
+ //! Set this value as a string without copying source string.
+ /*! This version has better performance with supplied length, and also support
+ string containing null character. \param s source string pointer. \param
+ length The length of source string, excluding the trailing null terminator.
+ \return The value itself for fluent API.
+ \post IsString() == true && GetString() == s && GetStringLength() ==
+ length \see SetString(StringRefType)
+ */
+ GenericValue &SetString(const Ch *s, SizeType length) {
+ return SetString(StringRef(s, length));
+ }
+
+ //! Set this value as a string without copying source string.
+ /*! \param s source string reference
+ \return The value itself for fluent API.
+ \post IsString() == true && GetString() == s && GetStringLength() ==
+ s.length
+ */
+ GenericValue &SetString(StringRefType s) {
+ this->~GenericValue();
+ SetStringRaw(s);
+ return *this;
+ }
+
+ //! Set this value as a string by copying from source string.
+ /*! This version has better performance with supplied length, and also support
+ string containing null character. \param s source string. \param length The
+ length of source string, excluding the trailing null terminator. \param
+ allocator Allocator for allocating copied buffer. Commonly use
+ GenericDocument::GetAllocator(). \return The value itself for fluent API.
+ \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0
+ && GetStringLength() == length
+ */
+ GenericValue &SetString(const Ch *s, SizeType length, Allocator &allocator) {
+ return SetString(StringRef(s, length), allocator);
+ }
+
+ //! Set this value as a string by copying from source string.
+ /*! \param s source string.
+ \param allocator Allocator for allocating copied buffer. Commonly use
+ GenericDocument::GetAllocator(). \return The value itself for fluent API.
+ \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0
+ && GetStringLength() == length
+ */
+ GenericValue &SetString(const Ch *s, Allocator &allocator) {
+ return SetString(StringRef(s), allocator);
+ }
+
+ //! Set this value as a string by copying from source string.
+ /*! \param s source string reference
+ \param allocator Allocator for allocating copied buffer. Commonly use
+ GenericDocument::GetAllocator(). \return The value itself for fluent API.
+ \post IsString() == true && GetString() != s.s && strcmp(GetString(),s) ==
+ 0 && GetStringLength() == length
+ */
+ GenericValue &SetString(StringRefType s, Allocator &allocator) {
+ this->~GenericValue();
+ SetStringRaw(s, allocator);
+ return *this;
+ }
+
+#if RAPIDJSON_HAS_STDSTRING
+ //! Set this value as a string by copying from source string.
+ /*! \param s source string.
+ \param allocator Allocator for allocating copied buffer. Commonly use
+ GenericDocument::GetAllocator(). \return The value itself for fluent API.
+ \post IsString() == true && GetString() != s.data() &&
+ strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size() \note
+ Requires the definition of the preprocessor symbol \ref
+ RAPIDJSON_HAS_STDSTRING.
+ */
+ GenericValue &SetString(const std::basic_string &s,
+ Allocator &allocator) {
+ return SetString(StringRef(s), allocator);
+ }
+#endif
+
+ //@}
+
+ //!@name Array
+ //@{
+
+ //! Templated version for checking whether this value is type T.
+ /*!
+ \tparam T Either \c bool, \c int, \c unsigned, \c int64_t, \c uint64_t, \c
+ double, \c float, \c const \c char*, \c std::basic_string
+ */
+ template
+ bool Is() const {
+ return internal::TypeHelper::Is(*this);
+ }
+
+ template
+ T Get() const {
+ return internal::TypeHelper::Get(*this);
+ }
+
+ template
+ T Get() {
+ return internal::TypeHelper::Get(*this);
+ }
+
+ template
+ ValueType &Set(const T &data) {
+ return internal::TypeHelper::Set(*this, data);
+ }
+
+ template
+ ValueType &Set(const T &data, AllocatorType &allocator) {
+ return internal::TypeHelper::Set(*this, data, allocator);
+ }
+
+ //@}
+
+ //! Generate events of this value to a Handler.
+ /*! This function adopts the GoF visitor pattern.
+ Typical usage is to output this JSON value as JSON text via Writer, which
+ is a Handler. It can also be used to deep clone this value via
+ GenericDocument, which is also a Handler. \tparam Handler type of handler.
+ \param handler An object implementing concept Handler.
+ */
+ template
+ bool Accept(Handler &handler) const {
+ switch (GetType()) {
+ case kNullType:
+ return handler.Null();
+ case kFalseType:
+ return handler.Bool(false);
+ case kTrueType:
+ return handler.Bool(true);
+
+ case kObjectType:
+ if (RAPIDJSON_UNLIKELY(!handler.StartObject())) return false;
+ for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) {
+ RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of
+ // name by MemberIterator.
+ if (RAPIDJSON_UNLIKELY(
+ !handler.Key(m->name.GetString(), m->name.GetStringLength(),
+ (m->name.data_.f.flags & kCopyFlag) != 0)))
+ return false;
+ if (RAPIDJSON_UNLIKELY(!m->value.Accept(handler))) return false;
+ }
+ return handler.EndObject(data_.o.size);
+
+ case kArrayType:
+ if (RAPIDJSON_UNLIKELY(!handler.StartArray())) return false;
+ for (const GenericValue *v = Begin(); v != End(); ++v)
+ if (RAPIDJSON_UNLIKELY(!v->Accept(handler))) return false;
+ return handler.EndArray(data_.a.size);
+
+ case kStringType:
+ return handler.String(GetString(), GetStringLength(),
+ (data_.f.flags & kCopyFlag) != 0);
+
+ default:
+ RAPIDJSON_ASSERT(GetType() == kNumberType);
+ if (IsDouble())
+ return handler.Double(data_.n.d);
+ else if (IsInt())
+ return handler.Int(data_.n.i.i);
+ else if (IsUint())
+ return handler.Uint(data_.n.u.u);
+ else if (IsInt64())
+ return handler.Int64(data_.n.i64);
+ else
+ return handler.Uint64(data_.n.u64);
+ }
+ }
+
+ private:
+ template
+ friend class GenericValue;
+ template
+ friend class GenericDocument;
+
+ enum {
+ kBoolFlag = 0x0008,
+ kNumberFlag = 0x0010,
+ kIntFlag = 0x0020,
+ kUintFlag = 0x0040,
+ kInt64Flag = 0x0080,
+ kUint64Flag = 0x0100,
+ kDoubleFlag = 0x0200,
+ kStringFlag = 0x0400,
+ kCopyFlag = 0x0800,
+ kInlineStrFlag = 0x1000,
+
+ // Initial flags of different types.
+ kNullFlag = kNullType,
+ kTrueFlag = kTrueType | kBoolFlag,
+ kFalseFlag = kFalseType | kBoolFlag,
+ kNumberIntFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag,
+ kNumberUintFlag =
+ kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag,
+ kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag,
+ kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag,
+ kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag,
+ kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag |
+ kUintFlag | kUint64Flag | kDoubleFlag,
+ kConstStringFlag = kStringType | kStringFlag,
+ kCopyStringFlag = kStringType | kStringFlag | kCopyFlag,
+ kShortStringFlag = kStringType | kStringFlag | kCopyFlag | kInlineStrFlag,
+ kObjectFlag = kObjectType,
+ kArrayFlag = kArrayType,
+
+ kTypeMask = 0x07
+ };
+
+ static const SizeType kDefaultArrayCapacity = 16;
+ static const SizeType kDefaultObjectCapacity = 16;
+
+ struct Flag {
+#if RAPIDJSON_48BITPOINTER_OPTIMIZATION
+ char payload[sizeof(SizeType) * 2 +
+ 6]; // 2 x SizeType + lower 48-bit pointer
+#elif RAPIDJSON_64BIT
+ char payload[sizeof(SizeType) * 2 + sizeof(void *) + 6]; // 6 padding bytes
+#else
+ char payload[sizeof(SizeType) * 2 + sizeof(void *) +
+ 2]; // 2 padding bytes
+#endif
+ uint16_t flags;
+ };
+
+ struct String {
+ SizeType length;
+ SizeType hashcode; //!< reserved
+ const Ch *str;
+ }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
+
+ // implementation detail: ShortString can represent zero-terminated strings up
+ // to MaxSize chars (excluding the terminating zero) and store a value to
+ // determine the length of the contained string in the last character
+ // str[LenPos] by storing "MaxSize - length" there. If the string to store has
+ // the maximal length of MaxSize then str[LenPos] will be 0 and therefore act
+ // as the string terminator as well. For getting the string length back from
+ // that value just use "MaxSize - str[LenPos]". This allows to store 13-chars
+ // strings in 32-bit mode, 21-chars strings in 64-bit mode, 13-chars strings
+ // for RAPIDJSON_48BITPOINTER_OPTIMIZATION=1 inline (for `UTF8`-encoded
+ // strings).
+ struct ShortString {
+ enum {
+ MaxChars = sizeof(static_cast(0)->payload) / sizeof(Ch),
+ MaxSize = MaxChars - 1,
+ LenPos = MaxSize
+ };
+ Ch str[MaxChars];
+
+ inline static bool Usable(SizeType len) { return (MaxSize >= len); }
+ inline void SetLength(SizeType len) {
+ str[LenPos] = static_cast(MaxSize - len);
+ }
+ inline SizeType GetLength() const {
+ return static_cast(MaxSize - str[LenPos]);
+ }
+ }; // at most as many bytes as "String" above => 12 bytes in 32-bit mode, 16
+ // bytes in 64-bit mode
+
+ // By using proper binary layout, retrieval of different integer types do not
+ // need conversions.
+ union Number {
+#if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN
+ struct I {
+ int i;
+ char padding[4];
+ } i;
+ struct U {
+ unsigned u;
+ char padding2[4];
+ } u;
+#else
+ struct I {
+ char padding[4];
+ int i;
+ } i;
+ struct U {
+ char padding2[4];
+ unsigned u;
+ } u;
+#endif
+ int64_t i64;
+ uint64_t u64;
+ double d;
+ }; // 8 bytes
+
+ struct ObjectData {
+ SizeType size;
+ SizeType capacity;
+ Member *members;
+ }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
+
+ struct ArrayData {
+ SizeType size;
+ SizeType capacity;
+ GenericValue *elements;
+ }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
+
+ union Data {
+ String s;
+ ShortString ss;
+ Number n;
+ ObjectData o;
+ ArrayData a;
+ Flag f;
+ }; // 16 bytes in 32-bit mode, 24 bytes in 64-bit mode, 16 bytes in 64-bit
+ // with RAPIDJSON_48BITPOINTER_OPTIMIZATION
+
+ RAPIDJSON_FORCEINLINE const Ch *GetStringPointer() const {
+ return RAPIDJSON_GETPOINTER(Ch, data_.s.str);
+ }
+ RAPIDJSON_FORCEINLINE const Ch *SetStringPointer(const Ch *str) {
+ return RAPIDJSON_SETPOINTER(Ch, data_.s.str, str);
+ }
+ RAPIDJSON_FORCEINLINE GenericValue *GetElementsPointer() const {
+ return RAPIDJSON_GETPOINTER(GenericValue, data_.a.elements);
+ }
+ RAPIDJSON_FORCEINLINE GenericValue *SetElementsPointer(
+ GenericValue *elements) {
+ return RAPIDJSON_SETPOINTER(GenericValue, data_.a.elements, elements);
+ }
+ RAPIDJSON_FORCEINLINE Member *GetMembersPointer() const {
+ return RAPIDJSON_GETPOINTER(Member, data_.o.members);
+ }
+ RAPIDJSON_FORCEINLINE Member *SetMembersPointer(Member *members) {
+ return RAPIDJSON_SETPOINTER(Member, data_.o.members, members);
+ }
+
+ // Initialize this value as array with initial data, without calling
+ // destructor.
+ void SetArrayRaw(GenericValue *values, SizeType count, Allocator &allocator) {
+ data_.f.flags = kArrayFlag;
+ if (count) {
+ GenericValue *e = static_cast