SMACC2
Loading...
Searching...
No Matches
cb_absolute_rotate.cpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#include <cl_nav2z/cl_nav2z.hpp>
23#include <cl_nav2z/common.hpp>
27
28#include <rclcpp/parameter_client.hpp>
29
30namespace cl_nav2z
31{
32using namespace smacc2;
33
35CbAbsoluteRotate::CbAbsoluteRotate(float absoluteGoalAngleDegree, float yaw_goal_tolerance)
36{
37 this->absoluteGoalAngleDegree = absoluteGoalAngleDegree;
38
39 if (yaw_goal_tolerance >= 0)
40 {
41 this->yawGoalTolerance = yaw_goal_tolerance;
42 }
43}
44
46{
47 listener = std::make_shared<tf2_ros::Buffer>(getNode()->get_clock());
48 double goal_angle = this->absoluteGoalAngleDegree;
49
50 RCLCPP_INFO_STREAM(getLogger(), "[CbAbsoluteRotate] Absolute yaw Angle:" << goal_angle);
51
52 CpPlannerSwitcher * plannerSwitcher;
53 this->requiresComponent(plannerSwitcher, ComponentRequirement::SOFT);
54
56 {
57 plannerSwitcher->setPureSpinningPlanner();
58 }
59 else
60 {
61 plannerSwitcher->setDefaultPlanners();
62 }
63
65
67 this->requiresComponent(p, ComponentRequirement::HARD);
68
69 auto referenceFrame = p->getReferenceFrame();
70 auto currentPoseMsg = p->toPoseMsg();
71
72 nav2_msgs::action::NavigateToPose::Goal goal;
73 goal.pose.header.frame_id = referenceFrame;
74 //goal.pose.header.stamp = getNode()->now();
75
76 auto targetAngle = goal_angle * M_PI / 180.0;
77 goal.pose.pose.position = currentPoseMsg.position;
78 tf2::Quaternion q;
79 q.setRPY(0, 0, targetAngle);
80 goal.pose.pose.orientation = tf2::toMsg(q);
81
83 this->requiresComponent(odomTracker_, ComponentRequirement::SOFT);
84
85 if (odomTracker_ != nullptr)
86 {
87 auto pathname = this->getCurrentState()->getName() + " - " + getName();
88 odomTracker_->pushPath(pathname);
89 odomTracker_->setStartPoint(p->toPoseStampedMsg());
90 odomTracker_->setCurrentMotionGoal(goal.pose);
92 }
93
94 CpGoalCheckerSwitcher * goalCheckerSwitcher;
95 this->requiresComponent(goalCheckerSwitcher, ComponentRequirement::HARD);
96
97 goalCheckerSwitcher->setGoalCheckerId("absolute_rotate_goal_checker");
98
99 RCLCPP_INFO_STREAM(
100 getLogger(),
101 "[" << getName() << "] current pose yaw: " << tf2::getYaw(currentPoseMsg.orientation));
102 RCLCPP_INFO_STREAM(
103 getLogger(),
104 "[" << getName() << "] goal pose yaw: " << tf2::getYaw(goal.pose.pose.orientation));
105 this->sendGoal(goal);
106}
107
109{
110 auto log = this->getLogger();
111
112 std::string nodename = "/controller_server";
113
114 auto parameters_client =
115 std::make_shared<rclcpp::AsyncParametersClient>(this->getNode(), nodename);
116
117 RCLCPP_INFO_STREAM(
118 log, "[" << getName() << "] using a parameter client to update some controller parameters: "
119 << nodename << ". Waiting service.");
120 parameters_client->wait_for_service();
121
122 RCLCPP_INFO_STREAM(log, "[" << getName() << "] Service found: " << nodename << ".");
123
124 std::string localPlannerName;
125 std::vector<rclcpp::Parameter> parameters;
126
127 // dynamic_reconfigure::DoubleParameter yaw_goal_tolerance;
128 rclcpp::Parameter yaw_goal_tolerance("goal_checker.yaw_goal_tolerance");
129
130 rclcpp::Parameter max_vel_theta, min_vel_theta;
131
132 bool isRosBasePlanner = !spinningPlanner || *spinningPlanner == SpinningPlanner::Default;
133 bool isPureSpinningPlanner = spinningPlanner && *spinningPlanner == SpinningPlanner::PureSpinning;
134
135 // SELECTING CONTROLLER AND PARAMETERS NAME
136 if (isPureSpinningPlanner)
137 {
138 localPlannerName = "PureSpinningLocalPlanner";
139 max_vel_theta = rclcpp::Parameter(localPlannerName + ".max_angular_z_speed");
140 min_vel_theta = rclcpp::Parameter(localPlannerName + ".min_vel_theta");
141 }
142 else if (isRosBasePlanner)
143 {
144 localPlannerName = "FollowPath";
145 max_vel_theta = rclcpp::Parameter(localPlannerName + ".max_vel_theta");
146 min_vel_theta = rclcpp::Parameter(localPlannerName + ".min_vel_theta");
147 }
148
149 if (!undo)
150 {
152 {
153 // save old yaw tolerance
154 auto fut = parameters_client->get_parameters(
155 {localPlannerName + ".yaw_goal_tolerance"},
156 [&](auto futureParameters)
157 {
158 auto params = futureParameters.get();
159 oldYawTolerance = params[0].as_double();
160 });
161
162 // make synchronous
163 fut.get();
164
165 yaw_goal_tolerance = rclcpp::Parameter("goal_checker.yaw_goal_tolerance", *yawGoalTolerance);
166 parameters.push_back(yaw_goal_tolerance);
167 RCLCPP_INFO_STREAM(
168 getLogger(), "[" << getName()
169 << "] updating yaw tolerance local planner to: " << *yawGoalTolerance
170 << ", from previous value: " << this->oldYawTolerance);
171 }
172
173 if (maxVelTheta)
174 {
175 if (isRosBasePlanner)
176 {
177 // nh.getParam(nodename + "/" + localPlannerName+"/min_vel_theta", oldMinVelTheta);
178 // getCurrentState()->getParam(nodename + "/" + localPlannerName + "/max_vel_theta", oldMaxVelTheta);
179
180 // save old yaw tolerance
181 auto fut = parameters_client->get_parameters(
182 {localPlannerName + ".max_vel_theta"},
183 [&](auto futureParameters)
184 {
185 auto params = futureParameters.get();
186 oldMaxVelTheta = params[0].as_double();
187 });
188
189 // make synchronous
190 fut.get();
191 }
192
193 max_vel_theta = rclcpp::Parameter(localPlannerName + ".max_vel_theta", *maxVelTheta);
194 min_vel_theta = rclcpp::Parameter(localPlannerName + ".min_vel_theta", -*maxVelTheta);
195 parameters.push_back(max_vel_theta);
196 parameters.push_back(min_vel_theta);
197
198 RCLCPP_INFO_STREAM(
199 log, "[" << getName() << "] updating max vel theta local planner to: " << *maxVelTheta
200 << ", from previous value: " << this->oldMaxVelTheta);
201 RCLCPP_INFO_STREAM(
202 log, "[" << getName() << "] updating min vel theta local planner to: " << -*maxVelTheta
203 << ", from previous value: " << this->oldMinVelTheta);
204 }
205 }
206 else
207 {
209 {
210 yaw_goal_tolerance = rclcpp::Parameter("goal_checker.yaw_goal_tolerance", oldYawTolerance);
211 RCLCPP_INFO_STREAM(
212 log, "[" << getName() << "] restoring yaw tolerance local planner from: "
213 << *yawGoalTolerance << " , to previous value: " << this->oldYawTolerance);
214 }
215
216 if (maxVelTheta)
217 {
218 if (isRosBasePlanner)
219 {
220 max_vel_theta = rclcpp::Parameter(localPlannerName + ".max_vel_theta", oldMaxVelTheta);
221 min_vel_theta = rclcpp::Parameter(localPlannerName + ".max_vel_theta", oldMinVelTheta);
222 }
223
224 parameters.push_back(max_vel_theta);
225 parameters.push_back(min_vel_theta);
226 RCLCPP_INFO_STREAM(
227 log, "[" << getName() << "] restoring max vel theta local planner from: " << *maxVelTheta
228 << ", to previous value: " << this->oldMaxVelTheta);
229 RCLCPP_INFO_STREAM(
230 log, "[" << getName() << "] restoring min vel theta local planner from: " << -(*maxVelTheta)
231 << ", to previous value: " << this->oldMinVelTheta);
232 }
233 }
234
235 if (parameters.size())
236 {
237 std::stringstream ss;
238 ss << "Executing asynchronous request. Parameters to update: " << std::endl;
239 for (auto & p : parameters)
240 {
241 ss << p.get_name() << " - " << p.value_to_string() << std::endl;
242 }
243
244 RCLCPP_INFO_STREAM(getLogger(), "[CbAbsoluteRotate] " << ss.str());
245
246 auto futureResults = parameters_client->set_parameters(parameters);
247
248 int i = 0;
249 for (auto & res : futureResults.get())
250 {
251 RCLCPP_INFO_STREAM(
252 getLogger(), "[" << getName() << "] parameter result: " << parameters[i].get_name() << "="
253 << parameters[i].as_string() << ". Result: " << res.successful);
254 i++;
255 }
256
257 RCLCPP_INFO_STREAM(log, "[" << getName() << "] parameters updated");
258 }
259 else
260 {
261 RCLCPP_INFO_STREAM(log, "[" << getName() << "] skipping parameters update");
262 }
263}
264
266{
268 {
269 }
270 else
271 {
272 }
273
275}
276
277} // namespace cl_nav2z
std::optional< float > maxVelTheta
std::optional< SpinningPlanner > spinningPlanner
void updateTemporalBehaviorParameters(bool undo)
std::shared_ptr< tf2_ros::Buffer > listener
std::optional< float > yawGoalTolerance
void sendGoal(nav2_msgs::action::NavigateToPose::Goal &goal)
void setGoalCheckerId(std::string goal_checker_id)
void setPureSpinningPlanner(bool commit=true)
void setDefaultPlanners(bool commit=true)
geometry_msgs::msg::Pose toPoseMsg()
Definition cp_pose.hpp:57
geometry_msgs::msg::PoseStamped toPoseStampedMsg()
Definition cp_pose.hpp:63
const std::string & getReferenceFrame() const
Definition cp_pose.hpp:79
void setCurrentMotionGoal(const geometry_msgs::msg::PoseStamped &pose)
void setWorkingMode(WorkingMode workingMode)
void setStartPoint(const geometry_msgs::msg::PoseStamped &pose)
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
virtual std::string getName()=0