SMACC2
Loading...
Searching...
No Matches
cb_yaw_scan.cpp
Go to the documentation of this file.
1// Copyright 2026 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
19
20#include <algorithm>
21
22namespace cl_px4_mr
23{
24
26: amplitude_(params.amplitude),
27 period_(std::max(params.period, 0.1f)),
28 baseHeading_(params.baseHeading)
29{
30}
31
33{
34 if (localPosition_ == nullptr || !localPosition_->isValid())
35 {
36 RCLCPP_ERROR(getLogger(), "CbYawScan: no valid local position at entry - posting failure");
37 this->postPx4Failure();
38 return;
39 }
40
44
45 if (std::isnan(baseHeading_))
46 {
48 }
50
51 // Peak yaw rate of the commanded sweep; PX4's autonomous yaw rate limit is
52 // ~0.8 rad/s by default and the flown sweep clips beyond it.
53 float peakYawRate = amplitude_ * 2.0f * static_cast<float>(M_PI) / period_;
54 if (peakYawRate > 0.8f)
55 {
56 RCLCPP_WARN(
57 getLogger(),
58 "CbYawScan: commanded peak yaw rate %.2f rad/s exceeds typical PX4 limits - the flown "
59 "sweep will clip",
60 peakYawRate);
61 }
62
63 elapsed_ = 0.0f;
64 lastUpdateTime_ = std::chrono::steady_clock::now();
65
66 RCLCPP_INFO(
67 getLogger(),
68 "CbYawScan: scanning around heading=%.2f rad A=%.2f rad T=%.2f s "
69 "(continuous - exits only on state change)",
71
73 active_ = true;
74}
75
77{
78 if (active_.exchange(false))
79 {
80 // Restore the base heading; the offboard keep-alive republishes this until
81 // the next state commands otherwise.
83 RCLCPP_INFO(getLogger(), "CbYawScan: exiting - restoring base heading %.2f rad", baseHeading_);
84 }
85}
86
88{
90
91 if (!active_)
92 {
93 return;
94 }
95
96 auto now = std::chrono::steady_clock::now();
97 double dt = std::chrono::duration<double>(now - lastUpdateTime_).count();
98 lastUpdateTime_ = now;
99
100 elapsed_ += static_cast<float>(dt);
101
102 float phase = 2.0f * static_cast<float>(M_PI) * elapsed_ / period_;
103 float yaw = wrapPi(baseHeading_ + amplitude_ * std::sin(phase));
104
106}
107
108} // namespace cl_px4_mr
CbYawScan(const YawScanParams &params={})
std::chrono::steady_clock::time_point lastUpdateTime_
void onEntry() override
void update() override
void onExit() override
std::atomic< bool > active_
void setPositionNED(float x, float y, float z, float yaw=std::numeric_limits< float >::quiet_NaN())
virtual rclcpp::Logger getLogger() const
float wrapPi(float angle)