SMACC2
Loading...
Searching...
No Matches
smacc2_client_library
cl_px4_mr
src
cl_px4_mr
client_behaviors
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
15
#include <
cl_px4_mr/client_behaviors/cb_spiral_pattern.hpp
>
16
#include <
cl_px4_mr/components/cp_trajectory_setpoint.hpp
>
17
#include <
cl_px4_mr/components/cp_vehicle_local_position.hpp
>
18
19
namespace
cl_px4_mr
20
{
21
22
CbSpiralPattern::CbSpiralPattern
(
const
SpiralPatternParams
& params)
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
32
void
CbSpiralPattern::onEntry
()
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"
,
44
centerX_
,
centerY_
,
altitude_
,
maxRadius_
,
spacing_
,
speed_
);
45
46
trajectorySetpoint_
->
setPositionNED
(
centerX_
,
centerY_
, z, 0.0f);
47
}
48
49
void
CbSpiralPattern::onExit
() {}
50
51
void
CbSpiralPattern::update
()
52
{
53
CbPx4ClientBehaviorBase::update
();
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
87
trajectorySetpoint_
->
setPositionNED
(x, y, z, yaw);
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
cb_spiral_pattern.hpp
cl_px4_mr::CbPx4ClientBehaviorBase::update
void update() override
Definition
cb_px4_client_behavior_base.hpp:92
cl_px4_mr::CbPx4ClientBehaviorBase::postPx4Success
void postPx4Success()
Definition
cb_px4_client_behavior_base.hpp:121
cl_px4_mr::CbPx4ClientBehaviorBase::trajectorySetpoint_
CpTrajectorySetpoint * trajectorySetpoint_
Definition
cb_px4_client_behavior_base.hpp:139
cl_px4_mr::CbSpiralPattern::spacing_
float spacing_
Definition
cb_spiral_pattern.hpp:53
cl_px4_mr::CbSpiralPattern::altitude_
float altitude_
Definition
cb_spiral_pattern.hpp:51
cl_px4_mr::CbSpiralPattern::CbSpiralPattern
CbSpiralPattern(const SpiralPatternParams ¶ms)
Definition
cb_spiral_pattern.cpp:22
cl_px4_mr::CbSpiralPattern::maxRadius_
float maxRadius_
Definition
cb_spiral_pattern.hpp:52
cl_px4_mr::CbSpiralPattern::onExit
void onExit() override
Definition
cb_spiral_pattern.cpp:49
cl_px4_mr::CbSpiralPattern::lastUpdateTime_
std::chrono::steady_clock::time_point lastUpdateTime_
Definition
cb_spiral_pattern.hpp:57
cl_px4_mr::CbSpiralPattern::theta_
float theta_
Definition
cb_spiral_pattern.hpp:56
cl_px4_mr::CbSpiralPattern::centerX_
float centerX_
Definition
cb_spiral_pattern.hpp:49
cl_px4_mr::CbSpiralPattern::onEntry
void onEntry() override
Definition
cb_spiral_pattern.cpp:32
cl_px4_mr::CbSpiralPattern::speed_
float speed_
Definition
cb_spiral_pattern.hpp:54
cl_px4_mr::CbSpiralPattern::update
void update() override
Definition
cb_spiral_pattern.cpp:51
cl_px4_mr::CbSpiralPattern::centerY_
float centerY_
Definition
cb_spiral_pattern.hpp:50
cl_px4_mr::CpTrajectorySetpoint::setPositionNED
void setPositionNED(float x, float y, float z, float yaw=std::numeric_limits< float >::quiet_NaN())
Definition
cp_trajectory_setpoint.cpp:46
smacc2::ISmaccClientBehavior::getLogger
virtual rclcpp::Logger getLogger() const
Definition
smacc_client_behavior_base.cpp:60
cp_trajectory_setpoint.hpp
cp_vehicle_local_position.hpp
cl_px4_mr
Definition
cl_px4_mr.hpp:31
cl_px4_mr::SpiralPatternParams
Definition
cb_spiral_pattern.hpp:30
Generated by
1.12.0