SMACC2
Loading...
Searching...
No Matches
cb_takeoff.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
21
22#include <chrono>
23#include <thread>
24
25namespace cl_px4_mr
26{
27
28CbTakeOff::CbTakeOff(float targetAltitude) : targetAltitude_(targetAltitude) {}
29
31{
32 // 1. Enable offboard keepalive heartbeat
34
35 // 2. Hold current position to begin streaming setpoints
37
38 // 3. Wait for PX4 to see the setpoint stream (needs >2Hz for 1 second)
39 RCLCPP_INFO(getLogger(), "CbTakeOff: streaming setpoints before offboard switch...");
40 std::this_thread::sleep_for(std::chrono::seconds(2));
41
42 // 4. Switch to offboard mode
43 RCLCPP_INFO(getLogger(), "CbTakeOff: switching to offboard mode");
45
46 // 5. Small delay for mode switch to take effect
47 std::this_thread::sleep_for(std::chrono::milliseconds(500));
48
49 // 6. Command target altitude (NED: up is negative Z)
50 float currentX = localPosition_->getX();
51 float currentY = localPosition_->getY();
52 float targetZ = -targetAltitude_;
53 float currentHeading = localPosition_->getHeading();
54
55 RCLCPP_INFO(
56 getLogger(), "CbTakeOff: commanding altitude %.2f m (NED z=%.2f)", targetAltitude_, targetZ);
57 trajectorySetpoint_->setPositionNED(currentX, currentY, targetZ, currentHeading);
58
59 // 7. Set goal checker for target altitude
60 goalChecker_->setGoal(currentX, currentY, targetZ, 0.5f, 0.3f);
61}
62
64
66{
67 RCLCPP_INFO(getLogger(), "CbTakeOff: target altitude reached - posting success");
68 this->postPx4Success();
69}
70
72{
73 if (goalChecker_ != nullptr)
74 {
77 }
78 else
79 {
80 RCLCPP_WARN(getLogger(), "CbTakeOff: completion component missing, no completion signal wired");
81 }
82}
83
84} // namespace cl_px4_mr
void wireCompletionSignals() override
void onEntry() override
void onExit() override
CbTakeOff(float targetAltitude=5.0f)
void setGoal(float x, float y, float z, float xy_tolerance=0.5f, float z_tolerance=0.3f)
smacc2::SmaccSignal< void()> onGoalReached_
void setPositionNED(float x, float y, float z, float yaw=std::numeric_limits< float >::quiet_NaN())
virtual rclcpp::Logger getLogger() const
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)