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: centerX_(params.centerX),
24 centerY_(params.centerY),
25 altitude_(params.altitude),
26 radius_(params.radius),
27 angularVelocity_(params.angularVelocity),
28 numOrbits_(params.numOrbits)
29{
30}
31
33{
34 // Compute starting angle from current position relative to center
35 float dx = localPosition_->getX() - centerX_;
36 float dy = localPosition_->getY() - centerY_;
37 startAngle_ = std::atan2(dy, dx);
39 lastUpdateTime_ = std::chrono::steady_clock::now();
40
41 // Command initial orbit position
42 float x = centerX_ + radius_ * std::cos(currentAngle_);
43 float y = centerY_ + radius_ * std::sin(currentAngle_);
44 float z = -altitude_; // NED: up is negative
45 float yaw = static_cast<float>(currentAngle_ + M_PI); // Face toward center
46
47 RCLCPP_INFO(
48 getLogger(), "CbOrbitLocation: starting orbit center=[%.2f, %.2f] r=%.2f alt=%.2f orbits=%d",
50
52}
53
55
57{
59
60 auto now = std::chrono::steady_clock::now();
61 double dt = std::chrono::duration<double>(now - lastUpdateTime_).count();
62 lastUpdateTime_ = now;
63
64 // Advance angle
65 currentAngle_ = static_cast<float>(currentAngle_ + angularVelocity_ * dt);
66
67 // Compute new position on circle
68 float x = centerX_ + radius_ * std::cos(currentAngle_);
69 float y = centerY_ + radius_ * std::sin(currentAngle_);
70 float z = -altitude_; // NED
71 float yaw = static_cast<float>(currentAngle_ + M_PI); // Face toward center
72
74
75 // Check if we've completed the required number of orbits
76 float totalAngle = currentAngle_ - startAngle_;
77 float requiredAngle = static_cast<float>(static_cast<float>(numOrbits_) * 2.0f * M_PI);
78
79 if (totalAngle >= requiredAngle)
80 {
81 RCLCPP_INFO(getLogger(), "CbOrbitLocation: %d orbits completed - posting success", numOrbits_);
82 this->postPx4Success();
83 }
84}
85
86} // namespace cl_px4_mr
CbOrbitLocation(const OrbitLocationParams &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