Decision Module  1.0
ICRA2020 AI Challenge Northwestern Polytechnical University Aoxiang Team Strategy Code
my_robot.h
Go to the documentation of this file.
1 //
2 // Created by kehan on 2020/2/28.
3 //
4 
5 #ifndef ROBORTS_ROBORTS_DECISION_BLACKBOARD_RMROBOT_H_
6 #define ROBORTS_ROBORTS_DECISION_BLACKBOARD_RMROBOT_H_
7 
8 #include <vector>
9 #include <thread>
10 #include <mutex>
11 #include <geometry_msgs/PoseStamped.h>
12 #include <std_msgs/Bool.h>
13 
14 #include <roborts_msgs/ArmorsDetected.h>
15 #include <roborts_msgs/RobotStatus.h>
16 
17 #include <actionlib/client/simple_action_client.h>
18 #include <tf/tf.h>
19 #include <tf/transform_listener.h>
20 #include <ros/ros.h>
21 #include <roborts_msgs/RobotHeat.h>
22 #include <roborts_msgs/RobotDamage.h>
23 #include <roborts_msgs/ShootInfo.h>
24 
25 #include "blackboard_common.h"
26 #include "blackboard_raw.h"
27 #include "../executor/gimbal_executor.h"
28 #include "../executor/chassis_executor.h"
29 #include "../../roborts_common/math/math.h"
30 
31 namespace roborts_decision {
32 
33 class MyRobot {
34 public:
35  explicit MyRobot(RobotId id, const ros::NodeHandle &nh = ros::NodeHandle("~"));
36 
37  virtual ~MyRobot();
38 
39  bool operator==(const MyRobot &rhs) const;
40  bool operator!=(const MyRobot &rhs) const;
41 
42  /****************定义MyRobot获取赛场信息和自身属性方法********************/
43  void update(); // 每拍进行的更新
44 
45  RobotId GetId() const;
46 
47  // 获取机器人编号: red1/2, blue1/2
48  RobotType getRobotType() const;
49  //设定机器人编号
50  void setRobotType(RobotType robot_type);
51  //将机器人编号数据类型由enum转换成string
52  std::string robotTypeToString(const RobotType robot_type);
53 
54  //获取当前血量
55  int getHp() const;
56 
57  // 获取当前热量
58  int getCurrentHeat() const;
59 
60  // 获取当前弹丸数
61  int getRemainingProjectiles() const;
62 
63  // 是否存活
64  bool isSurvival() const;
65  void setIsSurvival(bool is_survival);
66 
67  // 是否可以移动、射击
68  bool isNoMove() const;
69  bool isNoShoot() const;
70 
71  // 是否被攻击
72  bool isShot() const;
73 
74  // 获取被攻击的装甲板编号
75  const std::vector<ArmorId> &getArmorsUnderAttack() const;
76 
77  // 获取视线范围内的装甲板
78  const roborts_msgs::ArmorsDetected &getArmorsInEyes() const;
79 
80  // 获取底盘在地图的姿态,世界坐标
81  const geometry_msgs::PoseStamped &getChassisMapPose() const;
82 
83  // OK = 0, Error = 1, FAILURE >= 2
84  uint32_t getStatusCode();
85 
86  // 获取底盘odom坐标系下姿态
87  const geometry_msgs::PoseStamped &getChassisOdomPose();
88 
89  // 获取云台世界坐标系下姿态
90  const geometry_msgs::PoseStamped &getGimbalMapPose();
91 
92  // 获取云台Odom坐标系下姿态
93  const geometry_msgs::PoseStamped &getGimbalOdomPose();
94 
95  roborts_common::Point2D getMyRobotPoint() const;
96  // 获取当前目标
97  // const geometry_msgs::PoseStamped &GetCurrentGoal() const;
98 
99  // 获取当前行为状态
101  void setCurrentBehavior(MyRobotBehavior current_behavior);
102 
103  double getEffectiveShootDistance() const;
104 
105  double getState() const;
106 
107  /**********************通讯方法******************************/
108  // 获取下一个跑位点
109  const roborts_common::Point2D &getNextPoint() const;
110 
111  // 设置下一个跑位点
112  void setNextPoint(const roborts_common::Point2D &nextPoint);
113 
114  // 获取信号量
115  Signal getSignal() const;
116 
117  /****************定义MyRobot底盘动作方法********************/
118  //实现机器人旋转前进
119  // 只需传跑位点,转速在内部确定
120  void spinForword(const double &tarX, const double &tarY);
121 
122  // 实现机器人原地旋转
123  void spinInPlace(const double &angularVelocity);
124 
125  //实现机器人原地静止
126  void stayStill();
127 
133  void keepDirectionMove(double tar_x, double tar_y, double direction);
134  double getSafeDirection(const geometry_msgs::PoseStamped &pose_enemy); // 获得相对地方最安全的姿态偏角
135 
136  // 避障跑 此为空函数不推荐使用
137  [[deprecated("当前跑位默认使用避障,暂无不适用避障的接口")]]
138  void avoidanceMove(double x, double y);
139 
140  // 获取特定装甲板的朝向角度,范围 -PI - PI
141  double GetArmorTowards(ArmorId armor_id);
142 
143  /****************定义MyRobot云台及射击方法********************/
144  // TODO:实现机器人以某种速度射击一定数量的子弹(返回是否在一拍内射击了指定数量的子弹)(不满足射击条件自动停止射击并返回false)
145  bool shoot(double speed, int number = data::MAX);
146  int shootBulletsMaxNumber(double speed) const; // TODO:返回一拍内该速度射击的最大子弹数量
147 
148  // 将枪口朝向某个坐标点
149  double TowardWPosShoot(double tar_x, double tar_y, double tar_z);
150 
151  //获取需要攻击的目标
152  int GetShootEnemyId();
153  void SetShootEnemyId(const int id);
154 
155  // 获取一帧射击的子弹数
156  int GetShootNulletsNumber();
157  void SetShootNulletsNumber(const int num_bullets);
158 
159  // 设置我方机器人射击参数,用于状态估计(getState)
160  void SetHomeShootParameter();
161 
162  // 设置我方机器人移动参数,用于状态估计(getState)
163  void SetHomeMoveParameter();
164 
165 
166 
167 private:
168  // 获取底盘/云台控制器指针
169  std::shared_ptr<ChassisExecutor> getPChassisExecutor();
170  std::shared_ptr<GimbalExecutor> getPGimbalExecutor();
171 
172  //设置云台位置
173  void setGimbalOdomPose(const double &gimbal_goal_map_pitch, const double &gimbal_goal_map_yaw);
174 
175  // 原学长写的射击函数,提供宝贵指导作用
176  roborts_msgs::GimbalAngle shootAimed(geometry_msgs::Point32 &target);
177 
178  //暂时废弃shoot函数
179  void shoot();
180 
181  // 与其他机器人通讯(不保证对方一定会接收)
182  void setSignal(Signal signal);
183 
184  // TODO: 射击时转轮和实际射击速度的关系
185  double changeFrictionWheelSpeedToShootSpeed(double frictionWhellSpeed);
186 
187  // ros消息回调函数
188  void ArmorsUnderAttackCallback(const roborts_msgs::RobotDamage::ConstPtr &msg);
189  void HeatCallback(const roborts_msgs::RobotHeat::ConstPtr &msg);
190  void RobotStatusCallback(const roborts_msgs::RobotStatus::ConstPtr &msg);
191  void ArmorsInEyesCallback(const roborts_msgs::ArmorsDetected::ConstPtr &msg);
192  void RemainingProjectilesCallback(const roborts_msgs::ShootInfo::ConstPtr &msg);
193 
194  void ChassisMapPoseCallback(const geometry_msgs::PoseStamped::ConstPtr &msg);
195  // void UpdateChassisMapPose();
196  void UpdateChassisOdomPose();
197  void UpdateGimbalMapPose();
198  void UpdateGimbalOdomPose();
199 
200  ros::NodeHandle nh_;
201 
202  ros::Subscriber remaining_projectiles_sub_;
203  ros::Subscriber armors_under_attack_sub_;
204  ros::Subscriber heat_sub_;
205  ros::Subscriber armors_in_eyes_sub_;
206  ros::Subscriber robot_status_sub_;
207  ros::Subscriber robot_map_pose_sub_;
208 
209  std::shared_ptr<tf::TransformListener> tf_ptr_;
210  std::shared_ptr<std::thread> tf_thread_ptr_;
211 
212  std::shared_ptr<std::thread> p_ros_spin_thread_;
213 
216 
218  int max_hp_;
220 
221  bool no_move_;
222  bool no_shoot_;
223 
224  // 状态评估的系数
225  constexpr static double kBlood = 0.9;
226  constexpr static double kBullet = 0.8;
227  constexpr static double kHeat = 0.1;
228  double tNoShoot = 0;
229  double tNoMove = 0;
230 
231 
232  // TODO: 子弹数,不知为何要加TODO
234 
236 
238 
240 
241  std::vector<ArmorId> armors_under_attack_;
242  roborts_msgs::ArmorsDetected armors_in_eyes_;
243 
244  geometry_msgs::PoseStamped chassis_map_pose_;
245  geometry_msgs::PoseStamped chassis_odom_pose_;
246 
247  geometry_msgs::PoseStamped gimbal_map_pose_;
248  geometry_msgs::PoseStamped gimbal_odom_pose_;
249 
250  // geometry_msgs::PoseStamped current_goal_;
252 
253  std::shared_ptr<ChassisExecutor> p_chassis_executor_;
254  std::shared_ptr<GimbalExecutor> p_gimbal_executor_;
255 
256  ros::Publisher shoot_pub_;
257 
258 private:
259  // 策略数据
260  roborts_common::Point2D nextPoint_; // 下一个跑位点
261 
262  roborts_decision::Signal signal_; // 通讯信号量(求救、进攻等,其他机器人会在满足自己的动作的基础上查看同伴的情况)
263 };
264 
265 }
266 
267 #endif //ROBORTS_ROBORTS_DECISION_BLACKBOARD_RMROBOT_H_
roborts_decision::MyRobot::getMyRobotPoint
roborts_common::Point2D getMyRobotPoint() const
Definition: my_robot.cpp:564
roborts_decision::MyRobot::getPChassisExecutor
std::shared_ptr< ChassisExecutor > getPChassisExecutor()
Definition: my_robot.cpp:261
roborts_decision::MyRobot::tNoMove
double tNoMove
Definition: my_robot.h:229
roborts_decision::MyRobot::ArmorsUnderAttackCallback
void ArmorsUnderAttackCallback(const roborts_msgs::RobotDamage::ConstPtr &msg)
Definition: my_robot.cpp:89
roborts_decision::MyRobot::isShot
bool isShot() const
Definition: my_robot.cpp:257
roborts_decision::MyRobot::current_heat_
int current_heat_
Definition: my_robot.h:219
roborts_decision::MyRobot::getPGimbalExecutor
std::shared_ptr< GimbalExecutor > getPGimbalExecutor()
Definition: my_robot.cpp:265
roborts_decision::MyRobot::armors_in_eyes_
roborts_msgs::ArmorsDetected armors_in_eyes_
Definition: my_robot.h:242
roborts_decision::MyRobot::kHeat
constexpr static double kHeat
Definition: my_robot.h:227
roborts_decision::MyRobot::avoidanceMove
void avoidanceMove(double x, double y)
Definition: my_robot.cpp:491
roborts_decision::MyRobot::getChassisMapPose
const geometry_msgs::PoseStamped & getChassisMapPose() const
Definition: my_robot.cpp:181
roborts_decision::MyRobot::GetShootEnemyId
int GetShootEnemyId()
Definition: my_robot.cpp:597
roborts_decision::MyRobot::UpdateGimbalOdomPose
void UpdateGimbalOdomPose()
Definition: my_robot.cpp:324
roborts_decision::MyRobot::getGimbalOdomPose
const geometry_msgs::PoseStamped & getGimbalOdomPose()
Definition: my_robot.cpp:193
roborts_decision::MyRobot::getCurrentBehavior
MyRobotBehavior getCurrentBehavior() const
Definition: my_robot.cpp:201
roborts_decision::MyRobot::shoot_bullets_this_time_
int shoot_bullets_this_time_
Definition: my_robot.h:239
roborts_decision::MyRobot::HeatCallback
void HeatCallback(const roborts_msgs::RobotHeat::ConstPtr &msg)
Definition: my_robot.cpp:93
roborts_decision::MyRobot::isSurvival
bool isSurvival() const
Definition: my_robot.cpp:161
roborts_decision::MyRobot::getRobotType
RobotType getRobotType() const
Definition: my_robot.cpp:117
roborts_decision::MyRobot::setCurrentBehavior
void setCurrentBehavior(MyRobotBehavior current_behavior)
Definition: my_robot.cpp:205
roborts_decision::MyRobot::getSafeDirection
double getSafeDirection(const geometry_msgs::PoseStamped &pose_enemy)
Definition: my_robot.cpp:494
roborts_decision::MyRobot::max_hp_
int max_hp_
Definition: my_robot.h:218
roborts_decision::MyRobot::operator!=
bool operator!=(const MyRobot &rhs) const
Definition: my_robot.cpp:213
roborts_decision::MyRobot::signal_
roborts_decision::Signal signal_
Definition: my_robot.h:262
roborts_decision::MyRobot::p_gimbal_executor_
std::shared_ptr< GimbalExecutor > p_gimbal_executor_
Definition: my_robot.h:254
roborts_decision::MyRobot::getEffectiveShootDistance
double getEffectiveShootDistance() const
Definition: my_robot.cpp:506
roborts_decision::MyRobot::UpdateGimbalMapPose
void UpdateGimbalMapPose()
Definition: my_robot.cpp:302
roborts_decision::MyRobot::getChassisOdomPose
const geometry_msgs::PoseStamped & getChassisOdomPose()
Definition: my_robot.cpp:185
roborts_decision::MyRobot::TowardWPosShoot
double TowardWPosShoot(double tar_x, double tar_y, double tar_z)
Definition: my_robot.cpp:391
roborts_decision::MyRobot::stayStill
void stayStill()
Definition: my_robot.cpp:486
roborts_decision::MyRobot::nh_
ros::NodeHandle nh_
Definition: my_robot.h:200
roborts_decision::MyRobot::chassis_odom_pose_
geometry_msgs::PoseStamped chassis_odom_pose_
Definition: my_robot.h:245
blackboard_raw.h
roborts_decision::MyRobot::gimbal_map_pose_
geometry_msgs::PoseStamped gimbal_map_pose_
Definition: my_robot.h:247
roborts_decision::MyRobot::kBullet
constexpr static double kBullet
Definition: my_robot.h:226
roborts_decision::ArmorId
ArmorId
Definition: blackboard_common.h:54
roborts_decision::MyRobot::getNextPoint
const roborts_common::Point2D & getNextPoint() const
Definition: my_robot.cpp:510
roborts_decision::MyRobot::p_ros_spin_thread_
std::shared_ptr< std::thread > p_ros_spin_thread_
Definition: my_robot.h:212
roborts_decision::MyRobot::setRobotType
void setRobotType(RobotType robot_type)
Definition: my_robot.cpp:145
roborts_decision::MyRobot::setNextPoint
void setNextPoint(const roborts_common::Point2D &nextPoint)
Definition: my_robot.cpp:514
blackboard_common.h
roborts_decision::MyRobot::getRemainingProjectiles
int getRemainingProjectiles() const
Definition: my_robot.cpp:157
roborts_decision::MyRobot::getStatusCode
uint32_t getStatusCode()
Definition: my_robot.cpp:269
roborts_decision::MyRobot::shoot_enemy_id_
int shoot_enemy_id_
Definition: my_robot.h:237
roborts_decision::MyRobot::remaining_hp_
int remaining_hp_
Definition: my_robot.h:217
roborts_decision::MyRobot::remaining_projectiles_
int remaining_projectiles_
Definition: my_robot.h:233
roborts_decision::data::MAX
constexpr static int MAX
最大数
Definition: blackboard_common.h:26
roborts_decision::MyRobot::~MyRobot
virtual ~MyRobot()
roborts_decision::MyRobot::is_survival_
bool is_survival_
Definition: my_robot.h:235
roborts_decision::MyRobot::armors_in_eyes_sub_
ros::Subscriber armors_in_eyes_sub_
Definition: my_robot.h:205
roborts_decision::MyRobot::SetShootEnemyId
void SetShootEnemyId(const int id)
Definition: my_robot.cpp:600
roborts_decision::MyRobot::operator==
bool operator==(const MyRobot &rhs) const
Definition: my_robot.cpp:209
roborts_decision::MyRobot::nextPoint_
roborts_common::Point2D nextPoint_
Definition: my_robot.h:260
roborts_decision::MyRobot::id_
RobotId id_
Definition: my_robot.h:214
roborts_decision::MyRobot::armors_under_attack_sub_
ros::Subscriber armors_under_attack_sub_
Definition: my_robot.h:203
roborts_decision::MyRobot::GetShootNulletsNumber
int GetShootNulletsNumber()
Definition: my_robot.cpp:603
roborts_decision::MyRobot::shootAimed
roborts_msgs::GimbalAngle shootAimed(geometry_msgs::Point32 &target)
Definition: my_robot.cpp:430
roborts_decision::MyRobot::robot_type_
RobotType robot_type_
Definition: my_robot.h:215
roborts_decision::MyRobot::no_shoot_
bool no_shoot_
Definition: my_robot.h:222
roborts_decision::MyRobot::getArmorsUnderAttack
const std::vector< ArmorId > & getArmorsUnderAttack() const
Definition: my_robot.cpp:173
roborts_decision::MyRobot::changeFrictionWheelSpeedToShootSpeed
double changeFrictionWheelSpeedToShootSpeed(double frictionWhellSpeed)
Definition: my_robot.cpp:575
roborts_decision::MyRobot::heat_sub_
ros::Subscriber heat_sub_
Definition: my_robot.h:204
roborts_decision::MyRobot::isNoShoot
bool isNoShoot() const
Definition: my_robot.cpp:249
roborts_decision::MyRobot::getSignal
Signal getSignal() const
Definition: my_robot.cpp:518
roborts_decision::MyRobot::robot_map_pose_sub_
ros::Subscriber robot_map_pose_sub_
Definition: my_robot.h:207
roborts_decision::Signal
Signal
Signal for interaction between robot.
Definition: blackboard_common.h:119
roborts_decision::MyRobot::isNoMove
bool isNoMove() const
Definition: my_robot.cpp:245
roborts_decision::MyRobot::tf_thread_ptr_
std::shared_ptr< std::thread > tf_thread_ptr_
Definition: my_robot.h:210
roborts_decision::MyRobot::RemainingProjectilesCallback
void RemainingProjectilesCallback(const roborts_msgs::ShootInfo::ConstPtr &msg)
Definition: my_robot.cpp:109
roborts_decision::MyRobot::setSignal
void setSignal(Signal signal)
Definition: my_robot.cpp:522
roborts_decision::MyRobot::setGimbalOdomPose
void setGimbalOdomPose(const double &gimbal_goal_map_pitch, const double &gimbal_goal_map_yaw)
Definition: my_robot.cpp:459
roborts_decision
Definition: behavior_test_node.h:14
roborts_decision::MyRobot::SetHomeShootParameter
void SetHomeShootParameter()
Definition: my_robot.cpp:620
roborts_decision::MyRobot::shoot
void shoot()
Definition: my_robot.cpp:273
roborts_decision::MyRobot::SetHomeMoveParameter
void SetHomeMoveParameter()
Definition: my_robot.cpp:624
roborts_decision::MyRobot::getState
double getState() const
Definition: my_robot.cpp:612
roborts_decision::MyRobot::gimbal_odom_pose_
geometry_msgs::PoseStamped gimbal_odom_pose_
Definition: my_robot.h:248
roborts_decision::MyRobot::UpdateChassisOdomPose
void UpdateChassisOdomPose()
Definition: my_robot.cpp:279
roborts_decision::MyRobot::remaining_projectiles_sub_
ros::Subscriber remaining_projectiles_sub_
Definition: my_robot.h:202
roborts_decision::MyRobot::chassis_map_pose_
geometry_msgs::PoseStamped chassis_map_pose_
Definition: my_robot.h:244
roborts_decision::MyRobot::spinInPlace
void spinInPlace(const double &angularVelocity)
Definition: my_robot.cpp:350
roborts_decision::MyRobot
Definition: my_robot.h:33
roborts_decision::RobotId
RobotId
Definition: blackboard_common.h:89
roborts_decision::MyRobot::MyRobot
MyRobot(RobotId id, const ros::NodeHandle &nh=ros::NodeHandle("~"))
Definition: my_robot.cpp:11
roborts_decision::MyRobot::current_behavior_
MyRobotBehavior current_behavior_
Definition: my_robot.h:251
roborts_decision::MyRobot::keepDirectionMove
void keepDirectionMove(double tar_x, double tar_y, double direction)
Definition: my_robot.cpp:378
roborts_decision::MyRobot::SetShootNulletsNumber
void SetShootNulletsNumber(const int num_bullets)
Definition: my_robot.cpp:606
roborts_decision::MyRobot::ArmorsInEyesCallback
void ArmorsInEyesCallback(const roborts_msgs::ArmorsDetected::ConstPtr &msg)
Definition: my_robot.cpp:105
roborts_decision::MyRobot::shootBulletsMaxNumber
int shootBulletsMaxNumber(double speed) const
Definition: my_robot.cpp:570
roborts_decision::MyRobot::tNoShoot
double tNoShoot
Definition: my_robot.h:228
roborts_decision::MyRobot::update
void update()
Definition: my_robot.cpp:526
roborts_decision::MyRobot::robot_status_sub_
ros::Subscriber robot_status_sub_
Definition: my_robot.h:206
roborts_decision::MyRobot::getArmorsInEyes
const roborts_msgs::ArmorsDetected & getArmorsInEyes() const
Definition: my_robot.cpp:177
roborts_decision::MyRobot::RobotStatusCallback
void RobotStatusCallback(const roborts_msgs::RobotStatus::ConstPtr &msg)
Definition: my_robot.cpp:97
roborts_decision::MyRobot::ChassisMapPoseCallback
void ChassisMapPoseCallback(const geometry_msgs::PoseStamped::ConstPtr &msg)
Definition: my_robot.cpp:241
roborts_decision::MyRobot::robotTypeToString
std::string robotTypeToString(const RobotType robot_type)
Definition: my_robot.cpp:121
roborts_decision::MyRobotBehavior
MyRobotBehavior
Definition: blackboard_common.h:47
roborts_decision::MyRobot::GetArmorTowards
double GetArmorTowards(ArmorId armor_id)
Definition: my_robot.cpp:578
roborts_decision::MyRobot::getCurrentHeat
int getCurrentHeat() const
Definition: my_robot.cpp:153
roborts_decision::MyRobot::spinForword
void spinForword(const double &tarX, const double &tarY)
Definition: my_robot.cpp:360
roborts_decision::MyRobot::no_move_
bool no_move_
Definition: my_robot.h:221
roborts_decision::MyRobot::shoot_pub_
ros::Publisher shoot_pub_
Definition: my_robot.h:256
roborts_decision::MyRobot::getGimbalMapPose
const geometry_msgs::PoseStamped & getGimbalMapPose()
Definition: my_robot.cpp:189
roborts_decision::MyRobot::getHp
int getHp() const
Definition: my_robot.cpp:149
roborts_decision::MyRobot::GetId
RobotId GetId() const
Definition: my_robot.cpp:113
roborts_decision::MyRobot::tf_ptr_
std::shared_ptr< tf::TransformListener > tf_ptr_
Definition: my_robot.h:209
roborts_decision::MyRobot::setIsSurvival
void setIsSurvival(bool is_survival)
Definition: my_robot.cpp:165
roborts_decision::MyRobot::armors_under_attack_
std::vector< ArmorId > armors_under_attack_
Definition: my_robot.h:241
roborts_decision::MyRobot::kBlood
constexpr static double kBlood
Definition: my_robot.h:225
roborts_decision::MyRobot::p_chassis_executor_
std::shared_ptr< ChassisExecutor > p_chassis_executor_
Definition: my_robot.h:253
roborts_decision::RobotType
RobotType
Definition: blackboard_common.h:81