SMACC2
Loading...
Searching...
No Matches
cb_spiral_motion.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>
23#include <geometry_msgs/msg/twist.hpp>
24#include <optional>
26
27namespace cl_nav2z
28{
29
30CbSpiralMotion::CbSpiralMotion(std::optional<CbSpiralMotionOptions> options)
31{
32 if (options)
33 {
34 options_ = *options;
35 }
36 else
37 {
38 // we use default options
40 }
41}
42
44{
45 // An option left empty by the caller takes the struct's default instead of reading a disengaged optional.
46 auto linearVelocity =
48 auto maxLinearVelocity =
50 auto initialAngularVelocity =
52 auto spiralMotionDuration = options_.spiralMotionDuration.value_or(
53 rclcpp::Duration::from_seconds(CbSpiralMotionOptions::kDefaultSpiralMotionSeconds));
55
56 float rate = 20.0f;
57 rclcpp::Rate r(rate);
58 cmdVelPub_ = getNode()->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", rclcpp::QoS(1));
59
60 rclcpp::Duration linearRamp = rclcpp::Duration::from_seconds(spiralMotionDuration.seconds());
61 float linearAceleration =
62 static_cast<float>((maxLinearVelocity - linearVelocity) / linearRamp.seconds());
63 float dt = 1.0f / rate;
64
65 // we know final radious and the constant linear velocity
66 float finalAngularVelocity = maxLinearVelocity / finalRadius;
67
68 float angularAcceleration = static_cast<float>(
69 (initialAngularVelocity - finalAngularVelocity) / spiralMotionDuration.seconds());
70
71 geometry_msgs::msg::Twist cmd_vel;
72
73 cmd_vel.linear.x = linearVelocity;
74 cmd_vel.angular.z = initialAngularVelocity;
75 auto start_time = getNode()->now();
76
77 RCLCPP_INFO_STREAM(
78 getLogger(), "[CbSpiralMotion]: initialAngularVelocity: "
79 << initialAngularVelocity << ", finalAngularVelocity: " << finalAngularVelocity
80 << ", angularAcceleration: " << angularAcceleration);
81 RCLCPP_INFO_STREAM(
82 getLogger(), "[CbSpiralMotion]: linearAceleration: "
83 << linearAceleration << ", maxLinearVelocity: " << maxLinearVelocity);
84
85 bool end_condition = false;
86
87 while (!end_condition)
88 {
89 auto current_time = getNode()->now() - start_time;
90
91 cmd_vel.linear.x += static_cast<double>(linearAceleration) * dt;
92 if (cmd_vel.linear.x > maxLinearVelocity)
93 {
94 cmd_vel.linear.x = maxLinearVelocity;
95 }
96
97 float elapsedTimeFactor =
98 static_cast<float>(current_time.seconds() / spiralMotionDuration.seconds());
99 cmd_vel.angular.z = initialAngularVelocity * (1.0f - elapsedTimeFactor) +
100 finalAngularVelocity * elapsedTimeFactor;
101
102 RCLCPP_INFO(
103 getLogger(), "[CbSpiralMotion] cmd_vel.linear.x = %f, cmd_vel.angular.z = %f",
104 cmd_vel.linear.x, cmd_vel.angular.z);
105
106 cmdVelPub_->publish(cmd_vel);
107 r.sleep();
108
109 auto now = getNode()->now();
110
111 rclcpp::Duration elapsed = now - start_time;
112 RCLCPP_INFO_STREAM(
113 getLogger(), "[CbSpiralMotion] elapsed time: " << elapsed.seconds() << ", total duration: "
114 << spiralMotionDuration.seconds());
115 if (elapsed > spiralMotionDuration)
116 {
117 RCLCPP_INFO_STREAM(getLogger(), "[CbSpiralMotion] spiralMotionDuration reached");
118 end_condition = true;
119 }
120 }
121
122 // asynchronous client behaviors usually post a success event when they are done
123 // that is used in states to transition to the next state
124 this->postSuccessEvent();
125}
126
128
129} // namespace cl_nav2z
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
static constexpr float kDefaultInitialAngularVelocity
std::optional< float > initialAngularVelocity
std::optional< float > linearVelocity
static constexpr float kDefaultMaxLinearVelocity
std::optional< rclcpp::Duration > spiralMotionDuration
std::optional< float > maxLinearVelocity
std::optional< float > finalRadius
static constexpr double kDefaultSpiralMotionSeconds
static constexpr float kDefaultFinalRadius
static constexpr float kDefaultLinearVelocity
CbSpiralMotionOptions options_
rclcpp::Publisher< geometry_msgs::msg::Twist >::SharedPtr cmdVelPub_
CbSpiralMotion(std::optional< CbSpiralMotionOptions > options=std::nullopt)