Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions easyfleet_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ find_package(action_msgs REQUIRED)
find_package(rcl_interfaces REQUIRED)
find_package(lifecycle_msgs REQUIRED)
find_package(easyfleet_interfaces REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(pluginlib REQUIRED)
find_package(ament_index_cpp REQUIRED)

add_library(${PROJECT_NAME}
src/deployment.cpp
src/init.cpp
src/link_watchdog.cpp
src/order_history.cpp
src/param_utils.cpp
src/robot_status_publisher.cpp
src/spin_utils.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC
Expand All @@ -38,6 +43,8 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
rcl_interfaces::rcl_interfaces
lifecycle_msgs::lifecycle_msgs
easyfleet_interfaces::easyfleet_interfaces
${geometry_msgs_TARGETS}
${sensor_msgs_TARGETS}
pluginlib::pluginlib
ament_index_cpp::ament_index_cpp
)
Expand All @@ -63,6 +70,8 @@ ament_export_dependencies(
rcl_interfaces
lifecycle_msgs
easyfleet_interfaces
geometry_msgs
sensor_msgs
pluginlib
ament_index_cpp
)
Expand Down
32 changes: 31 additions & 1 deletion easyfleet_core/include/easyfleet_core/action_server_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

#include <atomic>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <type_traits>
#include <utility>
#include <vector>

#include "rclcpp/rclcpp.hpp"
Expand All @@ -30,6 +33,16 @@
namespace easyfleet_core
{

namespace detail
{
/// Whether a goal type has an `order_id` field (EasyFleet's own actions do).
template<typename GoalT, typename = void>
struct has_order_id : std::false_type {};
template<typename GoalT>
struct has_order_id<GoalT, std::void_t<decltype(std::declval<GoalT>().order_id)>>
: std::true_type {};
} // namespace detail

/// @brief Base class that hides the boilerplate of running a ROS 2 action server.
/**
* A node exposes an action by inheriting from `ActionServerBase<ActionT>` and
Expand Down Expand Up @@ -119,6 +132,20 @@ class ActionServerBase
/// `on_execute()`.
void stop();

/// @brief Ask the running goal to stop, and abort a queued one, without
/// disabling goals: `is_shutdown_requested()` is true until the running goal
/// returns. Does not wait. Used, e.g., when the link to the fleet is lost.
void halt_active_goal();

/// @brief Called (from the accepting thread) with every goal accepted to run,
/// and its `order_id` field, empty if the goal has none.
using GoalAcceptedCallback =
std::function<void (const rclcpp_action::GoalUUID & uuid, const std::string & order_id)>;

/// @brief Set the callback called with every accepted goal (nullptr: none).
/// @param callback The callback.
void set_goal_accepted_callback(GoalAcceptedCallback callback);

/// @brief Current value of the "allow_preemption" parameter for this action.
/// @return Current value of the "allow_preemption" parameter for this
/// action.
Expand Down Expand Up @@ -193,7 +220,7 @@ class ActionServerBase
bool is_preempt_requested() const noexcept;

/// @brief Whether the running goal must stop because goals are being
/// disabled or the server is being stopped.
/// disabled, the server is being stopped or `halt_active_goal()` was called.
/// @return Whether the running goal must stop. Long-running
/// `on_execute()` implementations should treat this the same as a
/// preemption request.
Expand Down Expand Up @@ -246,6 +273,9 @@ class ActionServerBase
std::condition_variable idle_cv_;
std::atomic_bool shutting_down_{false};
std::atomic_bool halt_requested_{false};
/// Set by halt_active_goal(), cleared when the running goal returns.
std::atomic_bool goal_halt_requested_{false};
GoalAcceptedCallback goal_accepted_callback_;
std::atomic_bool goals_enabled_{true};
std::atomic_bool preempt_requested_{false};
GoalHandleSharedPtr active_handle_;
Expand Down
34 changes: 33 additions & 1 deletion easyfleet_core/include/easyfleet_core/capability.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
#include "rclcpp_lifecycle/lifecycle_publisher.hpp"

#include "easyfleet_core/action_server_base.hpp"
#include "action_msgs/msg/goal_status_array.hpp"
#include "easyfleet_core/capability_node_base.hpp"
#include "easyfleet_core/link_watchdog.hpp"
#include "easyfleet_core/order_history.hpp"
#include "easyfleet_interfaces/msg/capability_description.hpp"
#include "easyfleet_interfaces/msg/capability_status.hpp"
#include "easyfleet_interfaces/msg/fleet_heartbeat.hpp"
#include "easyfleet_interfaces/srv/get_order.hpp"

namespace easyfleet_core
{
Expand All @@ -44,7 +49,12 @@ namespace easyfleet_core
* `easyfleet_interfaces/CapabilityDescription` on the reliable,
* transient-local `/capabilities` topic, so late subscribers still get it;
* - while active, publishes an `easyfleet_interfaces/CapabilityStatus`
* heartbeat once a second on `/capabilities_status`.
* heartbeat once a second on `/capabilities_status`;
* - keeps its recent orders (`<capability>.order_history`, 20 by default),
* queryable on the `~/get_order` service (e.g. /robot1/navigation/get_order);
* - with `<capability>.link_timeout` > 0 s, watches `/fleet_heartbeat` and,
* when it is not heard for that long and `<capability>.on_link_lost` is
* "cancel", stops the running goal ("continue", the default, only reports it).
*
* `ActionServerT` must publicly inherit `ActionServerBase<SomeActionType>`
* and be constructible as `ActionServerT(LifecycleNode &, const std::string &)`.
Expand Down Expand Up @@ -101,6 +111,12 @@ class Capability : public rclcpp_lifecycle::LifecycleNode, public CapabilityNode
/// @return The resolved, fully-qualified action name.
const std::string & get_resolved_action_name() const noexcept;

/// @brief The recent orders of this capability.
const OrderHistory & get_order_history() const noexcept;

/// @brief This capability's status, as in its heartbeat.
easyfleet_interfaces::msg::CapabilityStatus get_status() const override;

// -- CapabilityNodeBase --
//
// Named *_node() (see capability_node_base.hpp) precisely so these don't
Expand Down Expand Up @@ -153,6 +169,10 @@ class Capability : public rclcpp_lifecycle::LifecycleNode, public CapabilityNode
private:
void publish_heartbeat();
void stop_heartbeat();
void check_link();
void on_get_order(
const std::shared_ptr<easyfleet_interfaces::srv::GetOrder::Request> request,
std::shared_ptr<easyfleet_interfaces::srv::GetOrder::Response> response);

std::string capability_name_;
std::string robot_name_;
Expand All @@ -164,6 +184,18 @@ class Capability : public rclcpp_lifecycle::LifecycleNode, public CapabilityNode
rclcpp_lifecycle::LifecyclePublisher<
easyfleet_interfaces::msg::CapabilityStatus>::SharedPtr status_pub_;
rclcpp::TimerBase::SharedPtr heartbeat_timer_;

std::string order_history_param_;
std::string link_timeout_param_;
std::string on_link_lost_param_;
std::unique_ptr<OrderHistory> order_history_;
rclcpp::Subscription<action_msgs::msg::GoalStatusArray>::SharedPtr goal_status_sub_;
rclcpp::Service<easyfleet_interfaces::srv::GetOrder>::SharedPtr get_order_srv_;

LinkWatchdog link_watchdog_;
bool cancel_on_link_lost_{false};
rclcpp::Subscription<easyfleet_interfaces::msg::FleetHeartbeat>::SharedPtr fleet_heartbeat_sub_;
rclcpp::TimerBase::SharedPtr link_timer_;
};

} // namespace easyfleet_core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "rclcpp/node_interfaces/node_base_interface.hpp"
#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"

#include "easyfleet_interfaces/msg/capability_status.hpp"

namespace easyfleet_core
{

Expand Down Expand Up @@ -89,6 +91,10 @@ class CapabilityNodeBase
/// @return The `robot` identity field published on /capabilities and
/// /capabilities_status.
virtual const std::string & get_robot_name() const = 0;

/// @brief This capability's status, as in its /capabilities_status heartbeat.
/// @return The status.
virtual easyfleet_interfaces::msg::CapabilityStatus get_status() const = 0;
};

} // namespace easyfleet_core
Expand Down
10 changes: 10 additions & 0 deletions easyfleet_core/include/easyfleet_core/deployment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "easyfleet_core/capability_factory.hpp"
#include "easyfleet_core/capability_node_base.hpp"
#include "easyfleet_core/robot_status_publisher.hpp"

namespace easyfleet
{
Expand Down Expand Up @@ -176,6 +177,12 @@ class Deployment
/// @return Whether every capability reached ACTIVE.
bool try_start();

/// @brief The /robot_status publisher, created by a successful start()
/// (nullptr before). See `easyfleet_core::RobotStatusPublisher` for its
/// `robot_status.*` parameters, read from this process's parameters.
/// @return The publisher, or nullptr.
easyfleet_core::RobotStatusPublisher * robot_status_publisher() const noexcept;

/// @brief Adds every capability's node to one shared executor and blocks until
/// SIGINT/SIGTERM (see `spin_until_shutdown()`), then deactivates and
/// cleans up every capability -- in reverse order -- before returning.
Expand Down Expand Up @@ -211,6 +218,9 @@ class Deployment
/// distinct from theirs.
rclcpp::Node::SharedPtr config_node();
rclcpp::Node::SharedPtr config_node_;

/// @brief Last member: destroyed first, while the capabilities it reads exist.
std::unique_ptr<easyfleet_core::RobotStatusPublisher> robot_status_;
};

} // namespace easyfleet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ void ActionServerBase<ActionT>::disable_goals()
halt_requested_.store(false);
}

template<typename ActionT>
void ActionServerBase<ActionT>::halt_active_goal()
{
GoalHandleSharedPtr queued;
{
std::lock_guard<std::mutex> lock(mutex_);
queued = std::move(pending_handle_);
pending_handle_.reset();
if (active_handle_) {
goal_halt_requested_.store(true);
}
}
if (queued) {
settle_unstarted(queued);
}
}

template<typename ActionT>
void ActionServerBase<ActionT>::set_goal_accepted_callback(GoalAcceptedCallback callback)
{
std::lock_guard<std::mutex> lock(mutex_);
goal_accepted_callback_ = std::move(callback);
}

template<typename ActionT>
bool ActionServerBase<ActionT>::goals_enabled() const noexcept
{
Expand Down Expand Up @@ -211,7 +235,7 @@ bool ActionServerBase<ActionT>::is_preempt_requested() const noexcept
template<typename ActionT>
bool ActionServerBase<ActionT>::is_shutdown_requested() const noexcept
{
return shutting_down_.load() || halt_requested_.load();
return shutting_down_.load() || halt_requested_.load() || goal_halt_requested_.load();
}

template<typename ActionT>
Expand Down Expand Up @@ -307,6 +331,19 @@ void ActionServerBase<ActionT>::handle_accepted(GoalHandleSharedPtr goal_handle)
}
}

GoalAcceptedCallback callback;
{
std::lock_guard<std::mutex> lock(mutex_);
callback = goal_accepted_callback_;
}
if (callback) {
std::string order_id;
if constexpr (detail::has_order_id<Goal>::value) {
order_id = goal_handle->get_goal()->order_id;
}
callback(goal_handle->get_goal_id(), order_id);
}

if (bumped) {
RCLCPP_WARN(
node_logging_->get_logger(),
Expand Down Expand Up @@ -400,6 +437,7 @@ void ActionServerBase<ActionT>::worker_loop()
{
std::lock_guard<std::mutex> lock(mutex_);
active_handle_.reset();
goal_halt_requested_.store(false);
}
idle_cv_.notify_all();
}
Expand Down
Loading