Skip to content

Commit

Permalink
setpoint_types: add goto setpoint type
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Stastny authored and bkueng committed Dec 15, 2023
1 parent cacfbaa commit fb1b56d
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions px4_ros2_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(HEADER_FILES
include/px4_ros2/components/wait_for_fmu.hpp
include/px4_ros2/control/peripheral_actuators.hpp
include/px4_ros2/control/setpoint_types/direct_actuators.hpp
include/px4_ros2/control/setpoint_types/goto.hpp
include/px4_ros2/control/setpoint_types/experimental/attitude.hpp
include/px4_ros2/control/setpoint_types/experimental/rates.hpp
include/px4_ros2/control/setpoint_types/experimental/trajectory.hpp
Expand All @@ -56,6 +57,7 @@ add_library(px4_ros2_cpp
src/components/wait_for_fmu.cpp
src/control/peripheral_actuators.cpp
src/control/setpoint_types/direct_actuators.cpp
src/control/setpoint_types/goto.cpp
src/control/setpoint_types/experimental/attitude.cpp
src/control/setpoint_types/experimental/rates.cpp
src/control/setpoint_types/experimental/trajectory.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using namespace std::chrono_literals; // NOLINT
{"/fmu/in/aux_global_position", "VehicleGlobalPosition"}, \
{"/fmu/in/config_control_setpoints", "VehicleControlMode"}, \
{"/fmu/in/config_overrides_request", "ConfigOverrides"}, \
{"/fmu/in/goto_setpoint"}, \
{"/fmu/in/mode_completed"}, \
{"/fmu/in/register_ext_component_request"}, \
{"/fmu/in/trajectory_setpoint"}, \
Expand Down
57 changes: 57 additions & 0 deletions px4_ros2_cpp/include/px4_ros2/control/setpoint_types/goto.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/****************************************************************************
* Copyright (c) 2023 PX4 Development Team.
* SPDX-License-Identifier: BSD-3-Clause
****************************************************************************/

#pragma once

#include <px4_msgs/msg/goto_setpoint.hpp>
#include <Eigen/Eigen>
#include <optional>

#include <px4_ros2/common/setpoint_base.hpp>

namespace px4_ros2
{
/** \ingroup setpoint_types
* @{
*/

class GotoSetpointType : public SetpointBase
{
public:
explicit GotoSetpointType(Context & context);

~GotoSetpointType() override = default;

Configuration getConfiguration() override;

/**
* @brief Go-to setpoint update
*
* Unset optional values are not controlled
*
* @param position [m] NED earth-fixed frame
* @param heading [rad] from North
* @param max_horizontal_speed [m/s] in NE-datum of NED earth-fixed frame
* @param max_vertical_speed [m/s] in D-axis of NED earth-fixed frame
* @param max_heading_rate [rad/s] about D-axis of NED earth-fixed frame
*/
void update(
const Eigen::Vector3f & position,
const std::optional<float> & heading = {},
const std::optional<float> & max_horizontal_speed = {},
const std::optional<float> & max_vertical_speed = {},
const std::optional<float> & max_heading_rate = {}
);

float desiredUpdateRateHz() override {return 30.F;}

private:
rclcpp::Node & _node;
rclcpp::Publisher<px4_msgs::msg::GotoSetpoint>::SharedPtr
_goto_setpoint_pub;
};

/** @}*/
} /* namespace px4_ros2 */
67 changes: 67 additions & 0 deletions px4_ros2_cpp/src/control/setpoint_types/goto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/****************************************************************************
* Copyright (c) 2023 PX4 Development Team.
* SPDX-License-Identifier: BSD-3-Clause
****************************************************************************/

#include <px4_ros2/control/setpoint_types/goto.hpp>


namespace px4_ros2
{

GotoSetpointType::GotoSetpointType(Context & context)
: SetpointBase(context), _node(context.node())
{
_goto_setpoint_pub =
context.node().create_publisher<px4_msgs::msg::GotoSetpoint>(
context.topicNamespacePrefix() + "/fmu/in/goto_setpoint", 1);
}

void GotoSetpointType::update(
const Eigen::Vector3f & position,
const std::optional<float> & heading,
const std::optional<float> & max_horizontal_speed,
const std::optional<float> & max_vertical_speed,
const std::optional<float> & max_heading_rate)
{
onUpdate();

px4_msgs::msg::GotoSetpoint sp{};

// setpoints
sp.position[0] = position(0);
sp.position[1] = position(1);
sp.position[2] = position(2);
sp.heading = heading.value_or(0.F);

// setpoint flags
sp.flag_control_heading = heading.has_value();

// constraints
sp.max_horizontal_speed = max_horizontal_speed.value_or(0.F);
sp.max_vertical_speed = max_vertical_speed.value_or(0.F);
sp.max_heading_rate = max_heading_rate.value_or(0.F);

// constraint flags
sp.flag_set_max_horizontal_speed = max_horizontal_speed.has_value();
sp.flag_set_max_vertical_speed = max_vertical_speed.has_value();
sp.flag_set_max_heading_rate = max_heading_rate.has_value();

sp.timestamp = _node.get_clock()->now().nanoseconds() / 1000;
_goto_setpoint_pub->publish(sp);
}

SetpointBase::Configuration GotoSetpointType::getConfiguration()
{
Configuration config{};
config.control_allocation_enabled = true;
config.rates_enabled = true;
config.attitude_enabled = true;
config.altitude_enabled = true;
config.acceleration_enabled = true;
config.velocity_enabled = true;
config.position_enabled = true;
config.climb_rate_enabled = true;
return config;
}
} // namespace px4_ros2

0 comments on commit fb1b56d

Please sign in to comment.