SMACC2
Loading...
Searching...
No Matches
cb_position_control_free_space.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 <angles/angles.h>
22#include <geometry_msgs/msg/twist.hpp>
24
27
28namespace cl_nav2z
29{
31: targetYaw_(0), k_betta_(1.0), max_angular_yaw_speed_(1.0)
32{
33}
34
36
38{
39 auto nh = this->getNode();
40 cmd_vel_pub_ = nh->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", rclcpp::QoS(1));
41
42 cl_nav2z::Pose * pose;
43
44 this->requiresComponent(pose);
45
46 geometry_msgs::msg::Twist cmd_vel;
47 goalReached_ = false;
48
49 geometry_msgs::msg::Pose currentPose = pose->toPoseMsg();
50
51 rclcpp::Rate loop_rate(10);
52 double countAngle = 0;
53 auto prevyaw = tf2::getYaw(currentPose.orientation);
54
55 // PID controller gains (proportional, integral, and derivative)
56 double kp_linear = 0.5;
57 double ki_linear = 0.0;
58 double kd_linear = 0.1;
59
60 double kp_angular = 0.5;
61 double ki_angular = 0.0;
62 double kd_angular = 0.1;
63
64 while (rclcpp::ok() && !goalReached_)
65 {
66 RCLCPP_INFO_STREAM_THROTTLE(
67 getLogger(), *getNode()->get_clock(), 200,
68 "CbPositionControlFreeSpace, current pose: " << currentPose.position.x << ", "
69 << currentPose.position.y << ", "
70 << tf2::getYaw(currentPose.orientation));
71
72 RCLCPP_INFO_STREAM_THROTTLE(
73 getLogger(), *getNode()->get_clock(), 200,
74 "CbPositionControlFreeSpace, target pose: " << target_pose_.position.x << ", "
75 << target_pose_.position.y << ", "
76 << tf2::getYaw(target_pose_.orientation));
77
78 tf2::Quaternion q;
79 currentPose = pose->toPoseMsg();
80
81 // Here we implement the control logic to calculate the velocity command
82 // based on the received position of the vehicle and the target pose.
83
84 // Calculate the errors in x and y
85 double error_x = target_pose_.position.x - currentPose.position.x;
86 double error_y = target_pose_.position.y - currentPose.position.y;
87
88 // Calculate the distance to the target pose
89 double distance_to_target = std::sqrt(error_x * error_x + error_y * error_y);
90
91 RCLCPP_INFO_STREAM(
92 getLogger(), "[" << getName() << "] distance to target: " << distance_to_target
93 << " ( th: " << threshold_distance_ << ")");
94
95 // Check if the robot has reached the target pose
96 if (distance_to_target < threshold_distance_)
97 {
98 RCLCPP_INFO(getLogger(), "Goal reached!");
99 // Stop the robot by setting the velocity commands to zero
100 geometry_msgs::msg::Twist cmd_vel_msg;
101 cmd_vel_msg.linear.x = 0.0;
102 cmd_vel_msg.angular.z = 0.0;
103 cmd_vel_pub_->publish(cmd_vel_msg);
104 break;
105 }
106 else
107 {
108 // Calculate the desired orientation angle
109 double desired_yaw = std::atan2(error_y, error_x);
110
111 // Calculate the difference between the desired orientation and the
112 // current orientation
113 double yaw_error = desired_yaw - (tf2::getYaw(currentPose.orientation) + M_PI);
114
115 // Ensure the yaw error is within the range [-pi, pi]
116 while (yaw_error > M_PI) yaw_error -= 2 * M_PI;
117 while (yaw_error < -M_PI) yaw_error += 2 * M_PI;
118
119 // Calculate the control signals (velocity commands) using PID controllers
120 double cmd_linear_x = kp_linear * distance_to_target + ki_linear * integral_linear_ +
121 kd_linear * (distance_to_target - prev_error_linear_);
122 double cmd_angular_z = kp_angular * yaw_error + ki_angular * integral_angular_ +
123 kd_angular * (yaw_error - prev_error_angular_);
124
125 if (cmd_linear_x > max_linear_velocity)
126 cmd_linear_x = max_linear_velocity;
127 else if (cmd_linear_x < -max_linear_velocity)
128 cmd_linear_x = -max_linear_velocity;
129
130 if (cmd_angular_z > max_angular_velocity)
131 cmd_angular_z = max_angular_velocity;
132 else if (cmd_angular_z < -max_angular_velocity)
133 cmd_angular_z = -max_angular_velocity;
134
135 // Construct and publish the velocity command message
136 geometry_msgs::msg::Twist cmd_vel_msg;
137
138 cmd_vel_msg.linear.x = cmd_linear_x;
139 cmd_vel_msg.angular.z = cmd_angular_z;
140
141 cmd_vel_pub_->publish(cmd_vel_msg);
142
143 // Update errors and integrals for the next control cycle
144 prev_error_linear_ = distance_to_target;
145 prev_error_angular_ = yaw_error;
146 integral_linear_ += distance_to_target;
147 integral_angular_ += yaw_error;
148
149 // tf2::fromMsg(currentPose.orientation, q);
150 // auto currentYaw = tf2::getYaw(currentPose.orientation);
151 // auto deltaAngle = angles::shortest_angular_distance(prevyaw,
152 // currentYaw); countAngle += deltaAngle;
153
154 // prevyaw = currentYaw;
155 // double angular_error = targetYaw_ - countAngle;
156
157 // auto omega = angular_error * k_betta_;
158 // cmd_vel.linear.x = 0;
159 // cmd_vel.linear.y = 0;
160 // cmd_vel.linear.z = 0;
161 // cmd_vel.angular.z =
162 // std::min(std::max(omega, -fabs(max_angular_yaw_speed_)),
163 // fabs(max_angular_yaw_speed_));
164
165 // RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] delta angle: "
166 // << deltaAngle); RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "]
167 // cummulated angle: " << countAngle); RCLCPP_INFO_STREAM(getLogger(), "["
168 // << getName() << "] k_betta_: " << k_betta_);
169
170 // RCLCPP_INFO_STREAM(
171 // getLogger(), "[" << getName() << "] angular error: " << angular_error
172 // << "("
173 // << yaw_goal_tolerance_rads_ << ")");
174 // RCLCPP_INFO_STREAM(
175 // getLogger(), "[" << getName() << "] command angular speed: " <<
176 // cmd_vel.angular.z);
177
178 // if (fabs(angular_error) < yaw_goal_tolerance_rads_)
179 // {
180 // RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] GOAL REACHED.
181 // Sending stop command."); goalReached_ = true; cmd_vel.linear.x = 0;
182 // cmd_vel.angular.z = 0;
183 // break;
184 // }
185
186 // this->cmd_vel_pub_->publish(cmd_vel);
187
188 loop_rate.sleep();
189 }
190 }
191
192 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] Finished behavior execution");
193
194 this->postSuccessEvent();
195}
196
198
199} // namespace cl_nav2z
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
void requiresComponent(SmaccComponentType *&storage, bool throwExceptionIfNotExist=false)
rclcpp::Publisher< geometry_msgs::msg::Twist >::SharedPtr cmd_vel_pub_