SMACC2
Loading...
Searching...
No Matches
cp_vehicle_command.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
16
17namespace cl_px4_mr
18{
19
21
23
25{
26 auto node = this->getNode();
27 publisher_ = node->create_publisher<px4_msgs::msg::VehicleCommand>(
28 "/fmu/in/vehicle_command", rclcpp::QoS(1).best_effort());
29 RCLCPP_INFO(getLogger(), "CpVehicleCommand: publisher created on /fmu/in/vehicle_command");
30}
31
33 uint32_t command, float param1, float param2, float param3, float param4, double param5,
34 double param6, float param7)
35{
36 auto node = this->getNode();
37 px4_msgs::msg::VehicleCommand msg;
38 msg.timestamp = node->get_clock()->now().nanoseconds() / 1000;
39 msg.command = command;
40 msg.param1 = param1;
41 msg.param2 = param2;
42 msg.param3 = param3;
43 msg.param4 = param4;
44 msg.param5 = param5;
45 msg.param6 = param6;
46 msg.param7 = param7;
47 msg.target_system = 1;
48 msg.target_component = 1;
49 msg.source_system = 1;
50 msg.source_component = 1;
51 msg.from_external = true;
52 publisher_->publish(msg);
53 RCLCPP_INFO(
54 getLogger(), "CpVehicleCommand: sent command %u (p1=%.1f p2=%.1f p7=%.1f)", command, param1,
55 param2, param7);
56}
57
59{
60 sendCommand(px4_msgs::msg::VehicleCommand::VEHICLE_CMD_COMPONENT_ARM_DISARM, 1.0f);
61}
62
64{
65 // param2 = 21196 bypasses pre-arm checks (useful for SITL without RC/GCS)
66 sendCommand(px4_msgs::msg::VehicleCommand::VEHICLE_CMD_COMPONENT_ARM_DISARM, 1.0f, 21196.0f);
67}
68
70{
71 sendCommand(px4_msgs::msg::VehicleCommand::VEHICLE_CMD_COMPONENT_ARM_DISARM, 0.0f);
72}
73
75{
76 sendCommand(px4_msgs::msg::VehicleCommand::VEHICLE_CMD_DO_SET_MODE, 1.0f, 6.0f);
77}
78
79void CpVehicleCommand::land() { sendCommand(px4_msgs::msg::VehicleCommand::VEHICLE_CMD_NAV_LAND); }
80
81void CpVehicleCommand::takeoff(float altitude)
82{
84 px4_msgs::msg::VehicleCommand::VEHICLE_CMD_NAV_TAKEOFF, 0.0f, 0.0f, 0.0f, 0.0f, 0.0, 0.0,
85 altitude);
86}
87
88} // namespace cl_px4_mr
void sendCommand(uint32_t command, float param1=0.0f, float param2=0.0f, float param3=0.0f, float param4=0.0f, double param5=0.0, double param6=0.0, float param7=0.0f)
rclcpp::Publisher< px4_msgs::msg::VehicleCommand >::SharedPtr publisher_
rclcpp::Logger getLogger() const
rclcpp::Node::SharedPtr getNode()