SMACC2
Loading...
Searching...
No Matches
cb_position_control_free_space.cpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft 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{
30using namespace smacc2;
32
34
36{
37 auto nh = this->getNode();
38 cmd_vel_pub_ = nh->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", rclcpp::QoS(1));
39
40 cl_nav2z::CpPose * pose = nullptr;
41
42 this->requiresComponent(pose, ComponentRequirement::HARD);
43
44 geometry_msgs::msg::Twist cmd_vel;
45 goalReached_ = false;
46
47 geometry_msgs::msg::Pose currentPose = pose->toPoseMsg();
48
49 rclcpp::Rate loop_rate(10);
50
51 // PID controller gains (proportional, integral, and derivative)
52 double kp_linear = 0.5;
53 double ki_linear = 0.0;
54 double kd_linear = 0.1;
55
56 double kp_angular = 0.5;
57 double ki_angular = 0.0;
58 double kd_angular = 0.1;
59
60 // also ends when the state is exited: the loop used to block that exit until the goal was reached
61 while (rclcpp::ok() && !goalReached_ && !this->isShutdownRequested())
62 {
63 RCLCPP_INFO_STREAM_THROTTLE(
64 getLogger(), *getNode()->get_clock(), 200,
65 "CbPositionControlFreeSpace, current pose: " << currentPose.position.x << ", "
66 << currentPose.position.y << ", "
67 << tf2::getYaw(currentPose.orientation));
68
69 RCLCPP_INFO_STREAM_THROTTLE(
70 getLogger(), *getNode()->get_clock(), 200,
71 "CbPositionControlFreeSpace, target pose: " << target_pose_.position.x << ", "
72 << target_pose_.position.y << ", "
73 << tf2::getYaw(target_pose_.orientation));
74
75 tf2::Quaternion q;
76 currentPose = pose->toPoseMsg();
77
78 // Here we implement the control logic to calculate the velocity command
79 // based on the received position of the vehicle and the target pose.
80
81 // Calculate the errors in x and y
82 double error_x = target_pose_.position.x - currentPose.position.x;
83 double error_y = target_pose_.position.y - currentPose.position.y;
84
85 // Calculate the distance to the target pose
86 double distance_to_target = std::sqrt(error_x * error_x + error_y * error_y);
87
88 RCLCPP_INFO_STREAM(
89 getLogger(), "[" << getName() << "] distance to target: " << distance_to_target
90 << " ( th: " << threshold_distance_ << ")");
91
92 // Check if the robot has reached the target pose
93 if (distance_to_target < threshold_distance_)
94 {
95 RCLCPP_INFO(getLogger(), "Goal reached!");
96 goalReached_ = true;
97 }
98 else
99 {
100 // Calculate the desired orientation angle
101 double desired_yaw = std::atan2(error_y, error_x);
102
103 // Calculate the difference between the desired orientation and the
104 // current orientation
105 // the robot's front is steered to the target; with pi added here it turned its back and drove away
106 double yaw_error = desired_yaw - tf2::getYaw(currentPose.orientation);
107
108 // Ensure the yaw error is within the range [-pi, pi]
109 while (yaw_error > M_PI) yaw_error -= 2 * M_PI;
110 while (yaw_error < -M_PI) yaw_error += 2 * M_PI;
111
112 // Calculate the control signals (velocity commands) using PID controllers
113 double cmd_linear_x = kp_linear * distance_to_target + ki_linear * integral_linear_ +
114 kd_linear * (distance_to_target - prev_error_linear_);
115 double cmd_angular_z = kp_angular * yaw_error + ki_angular * integral_angular_ +
116 kd_angular * (yaw_error - prev_error_angular_);
117
118 if (cmd_linear_x > max_linear_velocity)
119 cmd_linear_x = max_linear_velocity;
120 else if (cmd_linear_x < -max_linear_velocity)
121 cmd_linear_x = -max_linear_velocity;
122
123 if (cmd_angular_z > max_angular_velocity)
124 cmd_angular_z = max_angular_velocity;
125 else if (cmd_angular_z < -max_angular_velocity)
126 cmd_angular_z = -max_angular_velocity;
127
128 // Construct and publish the velocity command message
129 geometry_msgs::msg::Twist cmd_vel_msg;
130
131 cmd_vel_msg.linear.x = cmd_linear_x;
132 cmd_vel_msg.angular.z = cmd_angular_z;
133
134 cmd_vel_pub_->publish(cmd_vel_msg);
135
136 // Update errors and integrals for the next control cycle
137 prev_error_linear_ = distance_to_target;
138 prev_error_angular_ = yaw_error;
139 integral_linear_ += distance_to_target;
140 integral_angular_ += yaw_error;
141
142 loop_rate.sleep();
143 }
144 }
145
146 // stop the robot, whether the goal was reached or the state was exited
147 if (rclcpp::ok()) cmd_vel_pub_->publish(geometry_msgs::msg::Twist());
148
149 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] Finished behavior execution");
150
151 if (goalReached_) this->postSuccessEvent();
152}
153
155
156} // namespace cl_nav2z
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...
rclcpp::Publisher< geometry_msgs::msg::Twist >::SharedPtr cmd_vel_pub_