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{
35
36 t_ = 0.0f;
37 lastUpdateTime_ = std::chrono::steady_clock::now();
38
39 // Command initial position (t=0: x=size, y=0 relative to center)
40 float x = centerX_ + size_;
41 float y = centerY_;
42 float z = -altitude_; // NED
43
44 RCLCPP_INFO(
45 getLogger(), "CbFigureEight: starting figure-8 center=[%.2f, %.2f] alt=%.2f size=%.2f loops=%d",
47
48 trajectorySetpoint_->setPositionNED(x, y, z, 0.0f);
49}
50
52
54{
55 auto now = std::chrono::steady_clock::now();
56 double dt = std::chrono::duration<double>(now - lastUpdateTime_).count();
57 lastUpdateTime_ = now;
58
59 t_ += speed_ * dt;
60
61 // Lemniscate of Bernoulli parametric equations
62 float sinT = std::sin(t_);
63 float cosT = std::cos(t_);
64 float denom = 1.0f + sinT * sinT;
65
66 float localX = size_ * cosT / denom;
67 float localY = size_ * sinT * cosT / denom;
68
69 float x = centerX_ + localX;
70 float y = centerY_ + localY;
71 float z = -altitude_; // NED
72
73 // Compute derivatives for yaw (face direction of travel)
74 float sinT2 = sinT * sinT;
75 float cosT2 = cosT * cosT;
76 float denom2 = denom * denom;
77 float dxdt = size_ * (-sinT * (1.0f + sinT2) - cosT * 2.0f * sinT * cosT) / denom2;
78 float dydt =
79 size_ * ((cosT2 - sinT2) * (1.0f + sinT2) - sinT * cosT * 2.0f * sinT * cosT) / denom2;
80 float yaw = std::atan2(dydt, dxdt);
81
83
84 // Check completion
85 float requiredT = numLoops_ * 2.0f * M_PI;
86 if (t_ >= requiredT)
87 {
88 RCLCPP_INFO(getLogger(), "CbFigureEight: %d loops completed - posting success", numLoops_);
89 this->postSuccessEvent();
90 }
91}
92
93} // 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)
CpTrajectorySetpoint * trajectorySetpoint_
void setPositionNED(float x, float y, float z, float yaw=std::numeric_limits< float >::quiet_NaN())
virtual rclcpp::Logger getLogger() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)