1 #ifndef CONTROLS_NODES_CONTROLSROS2WRAPPER_H
2 #define CONTROLS_NODES_CONTROLSROS2WRAPPER_H
6 #include <utils/concurrentqueue.h>
8 #include <yaml-cpp/yaml.h>
11 #include <bb_controls_msgs/action/locomotion.hpp>
12 #include <bb_controls_msgs/msg/controller_status.hpp>
13 #include <bb_controls_msgs/msg/thrusters.hpp>
14 #include <bb_controls_msgs/srv/controller.hpp>
15 #include <bb_controls_msgs/srv/encircle_traj.hpp>
16 #include <bb_controls_msgs/srv/spline_traj.hpp>
17 #include <bb_planner_msgs/srv/get_plan_through_poses.hpp>
18 #include <bb_sensor_msgs/msg/batteries_with_id.hpp>
19 #include <boost/uuid/uuid.hpp>
20 #include <boost/uuid/uuid_generators.hpp>
21 #include <boost/uuid/uuid_io.hpp>
24 #include <rclcpp_action/rclcpp_action.hpp>
25 #include <rclcpp_action/server.hpp>
26 #include <std_msgs/msg/bool.hpp>
28 #include <string_view>
30 #include <unordered_map>
72 std::string body_frame_tf_name =
"base_link", std::string world_frame_tf_name =
"map");
198 static inline const std::unordered_map<std::string_view, double Params::*>
DOUBLE_PARAM_MAP = {
296 static inline const std::unordered_map<std::string_view, bool Params::*>
BOOL_PARAM_MAP = {
325 rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr
world_sub_;
326 rclcpp::Subscription<bb_sensor_msgs::msg::BatteriesWithId>::SharedPtr
battery_sub_;
331 rclcpp::Publisher<bb_controls_msgs::msg::ControllerStatus>::SharedPtr
pub_status_;
398 rcl_interfaces::msg::SetParametersResult
parameters_callback(
const std::vector<rclcpp::Parameter>& parameters);
449 std::shared_ptr<bb_controls_msgs::srv::Controller::Response> res);
458 std::shared_ptr<bb_controls_msgs::srv::SplineTraj::Response> res);
467 std::shared_ptr<bb_controls_msgs::srv::EncircleTraj::Response> res);
477 rclcpp_action::GoalResponse
poly_handle_goal(
const rclcpp_action::GoalUUID& uuid,
478 std::shared_ptr<const Locomotion::Goal> goal);
487 rclcpp_action::CancelResponse
poly_handle_cancel(
const std::shared_ptr<GoalHandleLocomotion> goal_handle);
ROS2 node that wraps the Vehicle control stack and exposes its interface to the rest of the system.
Definition: ControlsROS2Wrapper.h:61
void debug_publish_loop()
Drains the debug queue and publishes all pending messages.
Definition: ControlsROS2Wrapper.cpp:170
rclcpp::Subscription< geometry_msgs::msg::Twist >::SharedPtr teleop_force_sub_
Definition: ControlsROS2Wrapper.h:327
rclcpp::Publisher< bb_controls_msgs::msg::ThrusterForces >::SharedPtr pub_thruster_forces_
Definition: ControlsROS2Wrapper.h:335
static const std::unordered_map< std::string_view, double Params::* > DOUBLE_PARAM_MAP
Maps ROS parameter names to their corresponding Params member pointers.
Definition: ControlsROS2Wrapper.h:198
~ControlsROS2Wrapper() override
Definition: ControlsROS2Wrapper.cpp:104
void sub_callback_teleop_force(const geometry_msgs::msg::Twist::ConstSharedPtr &msg)
Teleop subscriber callback — allocates the raw force directly if the controller is disabled.
Definition: ControlsROS2Wrapper.cpp:230
rclcpp::TimerBase::SharedPtr debug_publish_timer_
Definition: ControlsROS2Wrapper.h:338
const std::string world_frame_id_
TF frame_id of the world frame.
Definition: ControlsROS2Wrapper.h:319
rclcpp_action::GoalResponse poly_handle_goal(const rclcpp_action::GoalUUID &uuid, std::shared_ptr< const Locomotion::Goal > goal)
Action server goal handler — rejects goals when the controller is disabled.
Definition: ControlsROS2Wrapper.cpp:317
rclcpp_action::CancelResponse poly_handle_cancel(const std::shared_ptr< GoalHandleLocomotion > goal_handle)
Action server cancel handler — always accepts cancel requests.
Definition: ControlsROS2Wrapper.cpp:327
std::shared_ptr< GoalHandleLocomotion > current_action_goal_
Definition: ControlsROS2Wrapper.h:349
rclcpp::Subscription< nav_msgs::msg::Odometry >::SharedPtr world_sub_
Definition: ControlsROS2Wrapper.h:325
std::mutex action_goal_mutex_
Definition: ControlsROS2Wrapper.h:348
void apply_config_to_vehicle()
Applies config_ to the Vehicle's dynamics model, controller gains, and limits.
Definition: ControlsROS2Wrapper.cpp:540
static constexpr double MIN_DEPTH_CLAMP
Minimum depth value clamp to avoid near-zero depth noise (m)
Definition: ControlsROS2Wrapper.h:307
void sub_callback_battery_voltage(const bb_sensor_msgs::msg::BatteriesWithId::ConstSharedPtr &msg)
Battery subscriber callback — forwards the minimum cell voltage to the Vehicle.
Definition: ControlsROS2Wrapper.cpp:222
static constexpr int CONTROL_LOOP_RATE
Control loop frequency in Hz.
Definition: ControlsROS2Wrapper.h:304
rclcpp::Service< bb_controls_msgs::srv::SplineTraj >::SharedPtr spline_traj_srv_
Definition: ControlsROS2Wrapper.h:344
rclcpp::Service< bb_controls_msgs::srv::EncircleTraj >::SharedPtr encircle_traj_srv_
Definition: ControlsROS2Wrapper.h:345
Params config_
Local snapshot of ROS parameters, pushed to Vehicle subsystems on change.
Definition: ControlsROS2Wrapper.h:360
rclcpp_action::ServerGoalHandle< Locomotion > GoalHandleLocomotion
Definition: ControlsROS2Wrapper.h:83
rclcpp::Publisher< geometry_msgs::msg::WrenchStamped >::SharedPtr pub_controller_force_
Definition: ControlsROS2Wrapper.h:330
std::once_flag control_loop_started_
Flag for starting control loop.
Definition: ControlsROS2Wrapper.h:313
void load_params_from_server()
Reads all declared ROS parameters into config_.
Definition: ControlsROS2Wrapper.cpp:530
rcl_interfaces::msg::SetParametersResult parameters_callback(const std::vector< rclcpp::Parameter > ¶meters)
Callback invoked by the ROS parameter system when any parameter changes.
Definition: ControlsROS2Wrapper.cpp:602
void poly_execute_action(const std::shared_ptr< GoalHandleLocomotion > goal_handle)
Executes a Locomotion action: plans trajectory, waits for completion, checks tolerances.
Definition: ControlsROS2Wrapper.cpp:352
ControlsROS2Wrapper & operator=(ControlsROS2Wrapper &&)=delete
rclcpp::Publisher< geometry_msgs::msg::WrenchStamped >::SharedPtr pub_allocator_force_
Definition: ControlsROS2Wrapper.h:334
OnSetParametersCallbackHandle::SharedPtr callback_handle_
Definition: ControlsROS2Wrapper.h:357
rclcpp::CallbackGroup::SharedPtr actions_cb_group_
Definition: ControlsROS2Wrapper.h:365
std::thread action_execute_thread_
Thread running action execute method; joined before destruction.
Definition: ControlsROS2Wrapper.h:355
void control_loop()
Executes one iteration of the 20 Hz control loop.
Definition: ControlsROS2Wrapper.cpp:116
std::atomic< bool > shutdown_requested_
Signals the action execution thread to exit its loop.
Definition: ControlsROS2Wrapper.h:352
moodycamel::ConcurrentQueue< DebugMessage > debug_queue_
Lock-free queue through which the control loop passes debug messages to the publish timer.
Definition: ControlsROS2Wrapper.h:341
rclcpp::CallbackGroup::SharedPtr odom_cb_group_
Definition: ControlsROS2Wrapper.h:365
rclcpp::TimerBase::SharedPtr control_loop_timer_
Definition: ControlsROS2Wrapper.h:337
ControlsROS2Wrapper & operator=(const ControlsROS2Wrapper &)=delete
rclcpp::CallbackGroup::SharedPtr aux_cb_group_
Definition: ControlsROS2Wrapper.h:366
ControlsROS2Wrapper(ControlsROS2Wrapper &&)=delete
ControlsROS2Wrapper(const ControlsROS2Wrapper &)=delete
void declare_controller_params()
Registers all entries in DOUBLE_PARAM_MAP / BOOL_PARAM_MAP with the ROS parameter server.
Definition: ControlsROS2Wrapper.cpp:520
void srv_callback_trajectory_xyz(const std::shared_ptr< bb_controls_msgs::srv::SplineTraj::Request > req, std::shared_ptr< bb_controls_msgs::srv::SplineTraj::Response > res)
Service callback to request a straight XYZ or multi-waypoint spline trajectory.
Definition: ControlsROS2Wrapper.cpp:260
static constexpr double POSITION_JUMP_THRESHOLD
Position jump threshold for outlier rejection (m)
Definition: ControlsROS2Wrapper.h:310
rclcpp::Publisher< bb_controls_msgs::msg::ControllerStatus >::SharedPtr pub_status_
Definition: ControlsROS2Wrapper.h:331
const std::string vehicle_frame_id_
TF child_frame_id of the vehicle body frame.
Definition: ControlsROS2Wrapper.h:316
void poly_handle_accepted(const std::shared_ptr< GoalHandleLocomotion > goal_handle)
Action server accepted handler — preempts any active goal and starts a new execute thread.
Definition: ControlsROS2Wrapper.cpp:332
rclcpp::Subscription< bb_sensor_msgs::msg::BatteriesWithId >::SharedPtr battery_sub_
Definition: ControlsROS2Wrapper.h:326
void srv_callback_encircle_traj(const std::shared_ptr< bb_controls_msgs::srv::EncircleTraj::Request > req, std::shared_ptr< bb_controls_msgs::srv::EncircleTraj::Response > res)
Service callback to request a circular arc (encircle) trajectory.
Definition: ControlsROS2Wrapper.cpp:287
void srv_callback_controller(const std::shared_ptr< bb_controls_msgs::srv::Controller::Request > req, std::shared_ptr< bb_controls_msgs::srv::Controller::Response > res)
Service callback to enable (stationkeep) or disable the controller.
Definition: ControlsROS2Wrapper.cpp:248
rclcpp::Publisher< bb_controls_msgs::msg::Thrusters >::SharedPtr pub_thruster_commands_
Definition: ControlsROS2Wrapper.h:333
rclcpp_action::Server< Locomotion >::SharedPtr poly_action_server_
Definition: ControlsROS2Wrapper.h:347
rclcpp::Service< bb_controls_msgs::srv::Controller >::SharedPtr controller_srv_
Definition: ControlsROS2Wrapper.h:343
void sub_callback_world_position(const nav_msgs::msg::Odometry::ConstSharedPtr &m)
Odometry subscriber callback — updates vehicle state and starts the control loop.
Definition: ControlsROS2Wrapper.cpp:195
rclcpp::CallbackGroup::SharedPtr control_loop_cb_group_
Definition: ControlsROS2Wrapper.h:365
void publish_controller_status(const Vehicle::ControllerStatus &status)
Publishes a ControllerStatus message from the given status snapshot.
Definition: ControlsROS2Wrapper.cpp:156
bb_controls_msgs::action::Locomotion Locomotion
Definition: ControlsROS2Wrapper.h:82
rclcpp::Publisher< geometry_msgs::msg::PoseArray >::SharedPtr pub_traj_viz_
Definition: ControlsROS2Wrapper.h:332
ControlsROS2Wrapper(const std::string &name, std::unique_ptr< Vehicle > robot, std::string body_frame_tf_name="base_link", std::string world_frame_tf_name="map")
Constructs the node, initialises all ROS interfaces, and loads parameters.
Definition: ControlsROS2Wrapper.cpp:20
double prev_time_
ROS timestamp of the previous control loop iteration.
Definition: ControlsROS2Wrapper.h:363
rclcpp::CallbackGroup::SharedPtr services_cb_group_
Definition: ControlsROS2Wrapper.h:365
std::unique_ptr< Vehicle > auv_
Encapsulated vehicle control stack.
Definition: ControlsROS2Wrapper.h:322
static const std::unordered_map< std::string_view, bool Params::* > BOOL_PARAM_MAP
Definition: ControlsROS2Wrapper.h:296
Internal representation of all ROS-declared controller and dynamics parameters.
Definition: ControlsROS2Wrapper.h:92
double ma_x
Definition: ControlsROS2Wrapper.h:167
double kd_x_surface
Definition: ControlsROS2Wrapper.h:124
double ki_roll
Definition: ControlsROS2Wrapper.h:114
double axis_gain_roll
Definition: ControlsROS2Wrapper.h:101
double ki_yaw_surface
Definition: ControlsROS2Wrapper.h:138
double rgb_x
Definition: ControlsROS2Wrapper.h:188
double dnl_yaw
Definition: ControlsROS2Wrapper.h:184
double rbb_y
Definition: ControlsROS2Wrapper.h:186
double ki_yaw
Definition: ControlsROS2Wrapper.h:120
double max_yaw_jerk
Definition: ControlsROS2Wrapper.h:149
double max_xy_vel
Definition: ControlsROS2Wrapper.h:141
double kd_x
Definition: ControlsROS2Wrapper.h:106
double dl_z
Definition: ControlsROS2Wrapper.h:175
double dnl_roll
Definition: ControlsROS2Wrapper.h:182
double kp_z_surface
Definition: ControlsROS2Wrapper.h:128
double kd_roll_surface
Definition: ControlsROS2Wrapper.h:133
double rbb_z
Definition: ControlsROS2Wrapper.h:187
double axis_gain_z
Definition: ControlsROS2Wrapper.h:100
double ki_y
Definition: ControlsROS2Wrapper.h:108
double kp_yaw
Definition: ControlsROS2Wrapper.h:119
double buoyancy_factor
Definition: ControlsROS2Wrapper.h:160
bool buoyancy_adaptive
Definition: ControlsROS2Wrapper.h:152
double kd_z_surface
Definition: ControlsROS2Wrapper.h:130
double kd_y
Definition: ControlsROS2Wrapper.h:109
double rgb_z
Definition: ControlsROS2Wrapper.h:190
double ki_x_surface
Definition: ControlsROS2Wrapper.h:123
double ma_yaw
Definition: ControlsROS2Wrapper.h:172
double kd_yaw
Definition: ControlsROS2Wrapper.h:121
double i_xy
Definition: ControlsROS2Wrapper.h:164
double kd_pitch
Definition: ControlsROS2Wrapper.h:118
double kd_roll
Definition: ControlsROS2Wrapper.h:115
double rgb_y
Definition: ControlsROS2Wrapper.h:189
double cg_offset_y
Definition: ControlsROS2Wrapper.h:192
double kd_y_surface
Definition: ControlsROS2Wrapper.h:127
double fb_gain
Definition: ControlsROS2Wrapper.h:94
double buoyancy_gain
Definition: ControlsROS2Wrapper.h:95
double ki_roll_surface
Definition: ControlsROS2Wrapper.h:132
double kd_yaw_surface
Definition: ControlsROS2Wrapper.h:139
double dl_yaw
Definition: ControlsROS2Wrapper.h:178
double ki_x
Definition: ControlsROS2Wrapper.h:105
double kp_roll
Definition: ControlsROS2Wrapper.h:113
double ma_y
Definition: ControlsROS2Wrapper.h:168
bool surface_gain_scheduling
Definition: ControlsROS2Wrapper.h:96
double rbb_x
Definition: ControlsROS2Wrapper.h:185
double i_yz
Definition: ControlsROS2Wrapper.h:166
double dnl_pitch
Definition: ControlsROS2Wrapper.h:183
double dnl_x
Definition: ControlsROS2Wrapper.h:179
double max_xy_jerk
Definition: ControlsROS2Wrapper.h:143
double max_xy_acc
Definition: ControlsROS2Wrapper.h:142
double buoyancy_adaptive_pos_thresh
Definition: ControlsROS2Wrapper.h:155
double dl_roll
Definition: ControlsROS2Wrapper.h:176
double ki_z_surface
Definition: ControlsROS2Wrapper.h:129
double kp_x
Definition: ControlsROS2Wrapper.h:104
double max_z_jerk
Definition: ControlsROS2Wrapper.h:146
double cg_offset_z
Definition: ControlsROS2Wrapper.h:193
double ki_y_surface
Definition: ControlsROS2Wrapper.h:126
double ki_pitch_surface
Definition: ControlsROS2Wrapper.h:135
double max_z_vel
Definition: ControlsROS2Wrapper.h:144
double ki_pitch
Definition: ControlsROS2Wrapper.h:117
double kp_z
Definition: ControlsROS2Wrapper.h:110
double kd_z
Definition: ControlsROS2Wrapper.h:112
double dnl_z
Definition: ControlsROS2Wrapper.h:181
double dl_pitch
Definition: ControlsROS2Wrapper.h:177
double kp_y
Definition: ControlsROS2Wrapper.h:107
double cg_offset_x
Definition: ControlsROS2Wrapper.h:191
double ki_z
Definition: ControlsROS2Wrapper.h:111
double kp_y_surface
Definition: ControlsROS2Wrapper.h:125
double axis_gain_yaw
Definition: ControlsROS2Wrapper.h:103
double kp_x_surface
Definition: ControlsROS2Wrapper.h:122
double slow_down_gain
Definition: ControlsROS2Wrapper.h:140
double max_yaw_vel
Definition: ControlsROS2Wrapper.h:147
double mass
Definition: ControlsROS2Wrapper.h:159
double max_yaw_acc
Definition: ControlsROS2Wrapper.h:148
double kp_pitch_surface
Definition: ControlsROS2Wrapper.h:134
double i_zz
Definition: ControlsROS2Wrapper.h:163
double i_xx
Definition: ControlsROS2Wrapper.h:161
double i_xz
Definition: ControlsROS2Wrapper.h:165
double i_yy
Definition: ControlsROS2Wrapper.h:162
double axis_gain_x
Definition: ControlsROS2Wrapper.h:98
double dl_x
Definition: ControlsROS2Wrapper.h:173
double ff_gain
Definition: ControlsROS2Wrapper.h:93
double ma_roll
Definition: ControlsROS2Wrapper.h:170
double kd_pitch_surface
Definition: ControlsROS2Wrapper.h:136
double surface_threshold
Definition: ControlsROS2Wrapper.h:97
double ma_z
Definition: ControlsROS2Wrapper.h:169
double buoyancy_adaptive_vel_thresh
Definition: ControlsROS2Wrapper.h:156
double dl_y
Definition: ControlsROS2Wrapper.h:174
double axis_gain_pitch
Definition: ControlsROS2Wrapper.h:102
double ma_pitch
Definition: ControlsROS2Wrapper.h:171
double buoyancy_adaptive_max_scale
Definition: ControlsROS2Wrapper.h:153
double kp_roll_surface
Definition: ControlsROS2Wrapper.h:131
double max_z_acc
Definition: ControlsROS2Wrapper.h:145
double buoyancy_adaptive_clamp_pct
Definition: ControlsROS2Wrapper.h:154
double kp_pitch
Definition: ControlsROS2Wrapper.h:116
double dnl_y
Definition: ControlsROS2Wrapper.h:180
double axis_gain_y
Definition: ControlsROS2Wrapper.h:99
double kp_yaw_surface
Definition: ControlsROS2Wrapper.h:137
Snapshot of controller status exposed to external callers.
Definition: Vehicle.h:51