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