SMACC2
Loading...
Searching...
No Matches
cb_sine_altitude_cruise.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: groundSpeed_(params.groundSpeed),
27 amplitude_(params.amplitude),
28 wavelength_(std::max(params.wavelength, 0.1f)),
29 heading_(params.heading)
30{
31}
32
34{
35 if (localPosition_ == nullptr || !localPosition_->isValid())
36 {
37 RCLCPP_ERROR(
38 getLogger(), "CbSineAltitudeCruise: no valid local position at entry - posting failure");
39 this->postPx4Failure();
40 return;
41 }
42
46
47 if (std::isnan(heading_))
48 {
50 }
52
53 // Peak vertical speed of the commanded path; PX4 defaults limit climb/descent
54 // to ~2-3 m/s and the flown sine flattens beyond that.
55 float peakVerticalSpeed =
56 amplitude_ * 2.0f * static_cast<float>(M_PI) * groundSpeed_ / wavelength_;
57 if (peakVerticalSpeed > 2.0f)
58 {
59 RCLCPP_WARN(
60 getLogger(),
61 "CbSineAltitudeCruise: commanded peak vertical speed %.2f m/s exceeds typical PX4 limits - "
62 "the flown path will flatten",
63 peakVerticalSpeed);
64 }
65
66 arcLength_ = 0.0f;
69 lastUpdateTime_ = std::chrono::steady_clock::now();
70
71 RCLCPP_INFO(
72 getLogger(),
73 "CbSineAltitudeCruise: cruising heading=%.2f rad v=%.2f m/s A=%.2f m lambda=%.2f m "
74 "(continuous - exits only on state change)",
76
78 active_ = true;
79}
80
82{
83 if (active_.exchange(false))
84 {
85 // Settle at the entry altitude at the last commanded ground position; the
86 // offboard keep-alive republishes this until the next state commands otherwise.
88 RCLCPP_INFO(
89 getLogger(), "CbSineAltitudeCruise: exiting - settling at base altitude (NED z=%.2f)",
90 baseZ_);
91 }
92}
93
95{
97
98 if (!active_)
99 {
100 return;
101 }
102
103 auto now = std::chrono::steady_clock::now();
104 double dt = std::chrono::duration<double>(now - lastUpdateTime_).count();
105 lastUpdateTime_ = now;
106
107 arcLength_ += groundSpeed_ * static_cast<float>(dt);
108
109 float x = startX_ + arcLength_ * std::cos(heading_);
110 float y = startY_ + arcLength_ * std::sin(heading_);
111 float phase = 2.0f * static_cast<float>(M_PI) * arcLength_ / wavelength_;
112 float z = baseZ_ - amplitude_ * std::sin(phase); // NED: up is negative
113
114 lastCmdX_ = x;
115 lastCmdY_ = y;
117}
118
119} // namespace cl_px4_mr
CbSineAltitudeCruise(const SineAltitudeCruiseParams &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
float wrapPi(float angle)