SMACC
Loading...
Searching...
No Matches
planner_switcher.cpp
Go to the documentation of this file.
1/*****************************************************************************************************************
2 * ReelRobotix Inc. - Software License Agreement Copyright (c) 2018
3 * Authors: Pablo Inigo Blasco, Brett Aldrich
4 *
5 ******************************************************************************************************************/
8
9namespace cl_move_base_z
10{
11
13{
14}
15
17{
18 auto client_ = dynamic_cast<ClMoveBaseZ *>(owner_);
19 ros::NodeHandle nh(client_->name_);
20 dynrecofSub_ = nh.subscribe<dynamic_reconfigure::Config>("/move_base/parameter_updates", 1, boost::bind(&PlannerSwitcher::dynreconfCallback, this, _1));
21}
22
24{
25 ROS_INFO("[PlannerSwitcher] Planner Switcher: Trying to set BackwardPlanner");
26 desired_global_planner_ = "undo_path_global_planner/UndoPathGlobalPlanner";
27 desired_local_planner_ = "backward_local_planner/BackwardLocalPlanner";
29}
30
32{
33 ROS_INFO("[PlannerSwitcher] Planner Switcher: Trying to set BackwardPlanner");
34 desired_global_planner_ = "undo_path_global_planner/UndoPathGlobalPlanner";
35 desired_local_planner_ = "pure_spinning_local_planner/PureSpinningLocalPlanner";
37}
38
40{
41 ROS_INFO("[PlannerSwitcher] Planner Switcher: Trying to set BackwardPlanner");
42 desired_global_planner_ = "backward_global_planner/BackwardGlobalPlanner";
43 desired_local_planner_ = "backward_local_planner/BackwardLocalPlanner";
45}
46
48{
49 ROS_INFO("[PlannerSwitcher] Planner Switcher: Trying to set ForwardPlanner");
50 desired_global_planner_ = "forward_global_planner/ForwardGlobalPlanner";
51 desired_local_planner_ = "forward_local_planner/ForwardLocalPlanner";
53}
54
55
57{
58 ROS_INFO("[PlannerSwitcher] Planner Switcher: Trying to set PureSpinningPlanner");
59 desired_global_planner_ = "forward_global_planner/ForwardGlobalPlanner";
60 desired_local_planner_ = "pure_spinning_local_planner/PureSpinningLocalPlanner";
62}
63
65{
66 desired_global_planner_ = "navfn/NavfnROS";
67 desired_local_planner_ = "base_local_planner/TrajectoryPlannerROS";
69}
70
71void PlannerSwitcher::updatePlanners(bool subscribecallback)
72{
73 ROS_INFO_STREAM("[PlannerSwitcher] Setting global planner: " << desired_global_planner_);
74 ROS_INFO_STREAM("[PlannerSwitcher] Setting local planner: " << desired_local_planner_);
75
76 dynamic_reconfigure::ReconfigureRequest srv_req;
77 dynamic_reconfigure::ReconfigureResponse srv_resp;
78 dynamic_reconfigure::StrParameter local_planner, global_planner;
79 dynamic_reconfigure::Config conf;
80
81 local_planner.name = "base_local_planner";
82 local_planner.value = desired_local_planner_;
83 conf.strs.push_back(local_planner);
84
85 global_planner.name = "base_global_planner";
86 global_planner.value = desired_global_planner_;
87 conf.strs.push_back(global_planner);
88
89 srv_req.config = conf;
90 ROS_INFO("setting values of the dynamic reconfigure server");
91 bool error;
92 do
93 {
94 bool res = ros::service::call("/move_base/set_parameters", srv_req, srv_resp);
95 ros::spinOnce();
96 ros::Duration(0.5).sleep();
97
98 error = false;
99 auto baselocalPlannerIt = std::find_if(srv_resp.config.strs.begin(), srv_resp.config.strs.end(), [](auto sp){return sp.name == "base_local_planner";});
100 auto baseglobalPlannerIt = std::find_if(srv_resp.config.strs.begin(), srv_resp.config.strs.end(), [](auto sp){return sp.name == "base_global_planner";});
101
102 auto updatedLocalPlanner = baselocalPlannerIt->value;
103 auto updatedGlobalPlanner = baseglobalPlannerIt->value;
104 ROS_INFO_STREAM("[PlannerSwitcher] Selected base local planner: " << updatedLocalPlanner);
105 ROS_INFO_STREAM("[PlannerSwitcher] Selected base global planner: " << updatedGlobalPlanner);
106
107 if(updatedGlobalPlanner != desired_global_planner_ || updatedLocalPlanner!= desired_local_planner_)
108 {
109 ROS_WARN("[PlannerSwitcher] Planners not correctly configured. Waiting 1 second and retrying...");
110 ROS_INFO_STREAM("[PlannerSwitcher] Response: " << srv_resp);
111 error = true;
112 }
113
114 }while(error);
115
116}
117
118void PlannerSwitcher::dynreconfCallback(const dynamic_reconfigure::Config::ConstPtr &configuration_update)
119{
120 auto gp = std::find_if(configuration_update->strs.begin(), configuration_update->strs.begin(),
121 [&](const dynamic_reconfigure::StrParameter &p) {
122 return p.name == "base_global_planner" && p.value == desired_global_planner_;
123 });
124
125 auto lp = std::find_if(configuration_update->strs.begin(), configuration_update->strs.begin(),
126 [&](const dynamic_reconfigure::StrParameter &p) {
127 return p.name == "base_local_planner" && p.value == desired_local_planner_;
128 });
129
130 if (gp == configuration_update->strs.end() || lp == configuration_update->strs.end())
131 {
132 ROS_INFO("[PlannerSwitcher] After planner update it is noticed an incorrect move_base planner configuration. Resending request.");
134 updatePlanners(false);
135 }
136 else
137 {
138 ROS_INFO("[PlannerSwitcher] Planners correctly configured according to parameter update callback");
140 }
141}
142}; // namespace cl_move_base_z
void updatePlanners(bool subscribecallback=true)
virtual void onInitialize() override
void dynreconfCallback(const dynamic_reconfigure::Config::ConstPtr &configuration_update)
ISmaccClient * owner_
Definition: component.h:57