SMACC2
Loading...
Searching...
No Matches
cb_orbit_location.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
18
19namespace cl_px4_mr
20{
21
23 float centerX, float centerY, float altitude, float radius, float angularVelocity, int numOrbits)
24: centerX_(centerX),
25 centerY_(centerY),
26 altitude_(altitude),
27 radius_(radius),
28 angularVelocity_(angularVelocity),
29 numOrbits_(numOrbits)
30{
31}
32
34{
35 // Compute starting angle from current position relative to center
36 float dx = localPosition_->getX() - centerX_;
37 float dy = localPosition_->getY() - centerY_;
38 startAngle_ = std::atan2(dy, dx);
40 lastUpdateTime_ = std::chrono::steady_clock::now();
41
42 // Command initial orbit position
43 float x = centerX_ + radius_ * std::cos(currentAngle_);
44 float y = centerY_ + radius_ * std::sin(currentAngle_);
45 float z = -altitude_; // NED: up is negative
46 float yaw = currentAngle_ + M_PI; // Face toward center
47
48 RCLCPP_INFO(
49 getLogger(), "CbOrbitLocation: starting orbit center=[%.2f, %.2f] r=%.2f alt=%.2f orbits=%d",
51
53}
54
56
58{
60
61 auto now = std::chrono::steady_clock::now();
62 double dt = std::chrono::duration<double>(now - lastUpdateTime_).count();
63 lastUpdateTime_ = now;
64
65 // Advance angle
67
68 // Compute new position on circle
69 float x = centerX_ + radius_ * std::cos(currentAngle_);
70 float y = centerY_ + radius_ * std::sin(currentAngle_);
71 float z = -altitude_; // NED
72 float yaw = currentAngle_ + M_PI; // Face toward center
73
75
76 // Check if we've completed the required number of orbits
77 float totalAngle = currentAngle_ - startAngle_;
78 float requiredAngle = numOrbits_ * 2.0f * M_PI;
79
80 if (totalAngle >= requiredAngle)
81 {
82 RCLCPP_INFO(getLogger(), "CbOrbitLocation: %d orbits completed - posting success", numOrbits_);
83 this->postPx4Success();
84 }
85}
86
87} // namespace cl_px4_mr
std::chrono::steady_clock::time_point lastUpdateTime_
CbOrbitLocation(float centerX, float centerY, float altitude, float radius=5.0f, float angularVelocity=0.5f, int numOrbits=3)
void setPositionNED(float x, float y, float z, float yaw=std::numeric_limits< float >::quiet_NaN())
virtual rclcpp::Logger getLogger() const