SMACC2
Loading...
Searching...
No Matches
cb_figure_eight.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
17
18namespace cl_px4_mr
19{
20
22: centerX_(params.centerX),
23 centerY_(params.centerY),
24 altitude_(params.altitude),
25 size_(params.size),
26 speed_(params.speed),
27 numLoops_(params.numLoops)
28{
29}
30
32{
33 t_ = 0.0f;
34 lastUpdateTime_ = std::chrono::steady_clock::now();
35
36 // Command initial position (t=0: x=size, y=0 relative to center, rotated by heading_)
37 float x = centerX_ + size_ * std::cos(heading_);
38 float y = centerY_ + size_ * std::sin(heading_);
39 float z = -altitude_; // NED
40
41 RCLCPP_INFO(
42 getLogger(),
43 "CbFigureEight: starting figure-8 center=[%.2f, %.2f] alt=%.2f size=%.2f axis=%.2f rad "
44 "loops=%d",
46
48}
49
51
53{
55
56 auto now = std::chrono::steady_clock::now();
57 double dt = std::chrono::duration<double>(now - lastUpdateTime_).count();
58 lastUpdateTime_ = now;
59
60 t_ = static_cast<float>(t_ + speed_ * dt);
61
62 // Lemniscate of Bernoulli parametric equations
63 float sinT = std::sin(t_);
64 float cosT = std::cos(t_);
65 float denom = 1.0f + sinT * sinT;
66
67 float localX = size_ * cosT / denom;
68 float localY = size_ * sinT * cosT / denom;
69
70 // rotate the lemniscate so its lobe axis lies along heading_
71 const float ch = std::cos(heading_);
72 const float sh = std::sin(heading_);
73 float x = centerX_ + localX * ch - localY * sh;
74 float y = centerY_ + localX * sh + localY * ch;
75 float z = -altitude_; // NED
76
77 // Compute derivatives for yaw (face direction of travel)
78 float sinT2 = sinT * sinT;
79 float cosT2 = cosT * cosT;
80 float denom2 = denom * denom;
81 float dxdt = size_ * (-sinT * (1.0f + sinT2) - cosT * 2.0f * sinT * cosT) / denom2;
82 float dydt =
83 size_ * ((cosT2 - sinT2) * (1.0f + sinT2) - sinT * cosT * 2.0f * sinT * cosT) / denom2;
84 float yaw = std::atan2(dydt, dxdt) + heading_;
85
87
88 // Check completion
89 float requiredT = static_cast<float>(static_cast<float>(numLoops_) * 2.0f * M_PI);
90 if (t_ >= requiredT)
91 {
92 RCLCPP_INFO(getLogger(), "CbFigureEight: %d loops completed - posting success", numLoops_);
93 this->postPx4Success();
94 }
95}
96
97} // namespace cl_px4_mr
CbFigureEight(const FigureEightParams &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