SMACC2
Loading...
Searching...
No Matches
cb_spiral_pattern.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
18
19namespace cl_px4_mr
20{
21
23: centerX_(params.centerX),
24 centerY_(params.centerY),
25 altitude_(params.altitude),
26 maxRadius_(params.maxRadius),
27 spacing_(params.spacing),
28 speed_(params.speed)
29{
30}
31
33{
34 theta_ = 0.0f;
35 lastUpdateTime_ = std::chrono::steady_clock::now();
36
37 // Initial position is the spiral center
38 float z = -altitude_; // NED: up is negative
39
40 RCLCPP_INFO(
41 getLogger(),
42 "CbSpiralPattern: starting spiral center=[%.2f, %.2f] alt=%.2f maxR=%.2f spacing=%.2f "
43 "speed=%.2f",
45
47}
48
50
52{
54
55 auto now = std::chrono::steady_clock::now();
56 double dt = std::chrono::duration<double>(now - lastUpdateTime_).count();
57 lastUpdateTime_ = now;
58
59 // Archimedean spiral: r(theta) = a * theta
60 // where a = spacing / (2*PI) so each revolution adds 'spacing' meters of radius
61 float a = static_cast<float>(spacing_ / (2.0f * M_PI));
62
63 // Current radius
64 float r = a * theta_;
65
66 // Adaptive angular velocity to maintain constant linear speed
67 // Arc-length speed: ds/dt = sqrt(r^2 + a^2) * dtheta/dt
68 // Solving for dtheta/dt: omega = speed / sqrt(r^2 + a^2)
69 float omega = speed_ / std::sqrt(r * r + a * a);
70
71 // Advance angle
72 theta_ = static_cast<float>(theta_ + omega * dt);
73
74 // Compute new position
75 float r_new = a * theta_;
76 float x = centerX_ + r_new * std::cos(theta_);
77 float y = centerY_ + r_new * std::sin(theta_);
78 float z = -altitude_; // NED
79
80 // Yaw: face direction of travel (derivatives of Archimedean spiral)
81 // dx/dtheta = a*cos(theta) - r*sin(theta)
82 // dy/dtheta = a*sin(theta) + r*cos(theta)
83 float dxdt = a * std::cos(theta_) - r_new * std::sin(theta_);
84 float dydt = a * std::sin(theta_) + r_new * std::cos(theta_);
85 float yaw = std::atan2(dydt, dxdt);
86
88
89 // Check completion
90 if (r_new >= maxRadius_)
91 {
92 RCLCPP_INFO(
93 getLogger(), "CbSpiralPattern: max radius %.2f reached (r=%.2f) - posting success",
94 maxRadius_, r_new);
95 this->postPx4Success();
96 }
97}
98
99} // namespace cl_px4_mr
CbSpiralPattern(const SpiralPatternParams &params)
std::chrono::steady_clock::time_point lastUpdateTime_
void setPositionNED(float x, float y, float z, float yaw=std::numeric_limits< float >::quiet_NaN())
virtual rclcpp::Logger getLogger() const