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{
37
40
41 // 1. Enable offboard keepalive heartbeat
43
44 // 2. Hold current position to begin streaming setpoints
46
47 // 3. Wait for PX4 to see the setpoint stream (needs >2Hz for 1 second)
48 RCLCPP_INFO(getLogger(), "CbTakeOff: streaming setpoints before offboard switch...");
49 std::this_thread::sleep_for(std::chrono::seconds(2));
50
51 // 4. Switch to offboard mode
52 RCLCPP_INFO(getLogger(), "CbTakeOff: switching to offboard mode");
54
55 // 5. Small delay for mode switch to take effect
56 std::this_thread::sleep_for(std::chrono::milliseconds(500));
57
58 // 6. Command target altitude (NED: up is negative Z)
59 float currentX = localPosition_->getX();
60 float currentY = localPosition_->getY();
61 float targetZ = -targetAltitude_;
62 float currentHeading = localPosition_->getHeading();
63
64 RCLCPP_INFO(
65 getLogger(), "CbTakeOff: commanding altitude %.2f m (NED z=%.2f)", targetAltitude_, targetZ);
66 trajectorySetpoint_->setPositionNED(currentX, currentY, targetZ, currentHeading);
67
68 // 7. Set goal checker for target altitude
69 goalChecker_->setGoal(currentX, currentY, targetZ, 0.5f, 0.3f);
70}
71
73
75{
76 RCLCPP_INFO(getLogger(), "CbTakeOff: target altitude reached - posting success");
77 this->postSuccessEvent();
78}
79
80} // namespace cl_px4_mr
void onEntry() override
void onExit() override
CpVehicleLocalPosition * localPosition_
CpGoalChecker * goalChecker_
CpOffboardKeepAlive * offboardKeepAlive_
CpVehicleCommand * vehicleCommand_
CbTakeOff(float targetAltitude=5.0f)
CpTrajectorySetpoint * trajectorySetpoint_
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
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)