ros2_control - rolling
Loading...
Searching...
No Matches
battery_state_broadcaster.hpp
1// Copyright (c) 2025, b-robotized Group
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
15#ifndef BATTERY_STATE_BROADCASTER__BATTERY_STATE_BROADCASTER_HPP_
16#define BATTERY_STATE_BROADCASTER__BATTERY_STATE_BROADCASTER_HPP_
17
18#include <cmath>
19#include <cstdint>
20#include <memory>
21#include <string>
22#include <unordered_map>
23#include <vector>
24
25#include "controller_interface/controller_interface.hpp"
26#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
27#include "rclcpp_lifecycle/state.hpp"
28#include "realtime_tools/realtime_buffer.hpp"
29#include "realtime_tools/realtime_publisher.hpp"
30
31#include <battery_state_broadcaster/battery_state_broadcaster_parameters.hpp>
32#include "control_msgs/msg/battery_state_array.hpp"
33#include "sensor_msgs/msg/battery_state.hpp"
34
35namespace battery_state_broadcaster
36{
67{
68public:
70
72
74
75 controller_interface::CallbackReturn on_init() override;
76
77 controller_interface::CallbackReturn on_configure(
78 const rclcpp_lifecycle::State & previous_state) override;
79
80 controller_interface::CallbackReturn on_activate(
81 const rclcpp_lifecycle::State & previous_state) override;
82
83 controller_interface::CallbackReturn on_deactivate(
84 const rclcpp_lifecycle::State & previous_state) override;
85
86 controller_interface::return_type update(
87 const rclcpp::Time & time, const rclcpp::Duration & period) override;
88
89protected:
91 {
92 float voltage_sum = 0.0f;
93 float temperature_sum = 0.0f;
94 float current_sum = 0.0f;
95 float charge_sum = 0.0f;
96 float percentage_sum = 0.0f;
97 float capacity_sum = 0.0f;
98 float design_capacity_sum = 0.0f;
99 };
100
102 {
103 int temperature_cnt = 0;
104 int current_cnt = 0;
105 int percentage_cnt = 0;
106 };
107
108 battery_state_broadcaster::Params params_;
109
110 std::vector<std::string> batteries_;
111
114
115private:
116 std::shared_ptr<realtime_tools::RealtimePublisher<sensor_msgs::msg::BatteryState>>
117 battery_state_realtime_publisher_;
118 std::shared_ptr<realtime_tools::RealtimePublisher<control_msgs::msg::BatteryStateArray>>
119 raw_battery_states_realtime_publisher_;
120
121 std::shared_ptr<battery_state_broadcaster::ParamListener> param_listener_;
122 std::shared_ptr<rclcpp::Publisher<sensor_msgs::msg::BatteryState>> battery_state_publisher_;
123 std::shared_ptr<rclcpp::Publisher<control_msgs::msg::BatteryStateArray>>
124 raw_battery_states_publisher_;
125 sensor_msgs::msg::BatteryState battery_state_msg_;
126 control_msgs::msg::BatteryStateArray raw_battery_states_msg_;
127};
128
129} // namespace battery_state_broadcaster
130
131#endif // BATTERY_STATE_BROADCASTER__BATTERY_STATE_BROADCASTER_HPP_
Battery State Broadcaster for all or some state in a ros2_control system.
Definition battery_state_broadcaster.hpp:67
controller_interface::return_type update(const rclcpp::Time &time, const rclcpp::Duration &period) override
Control step update. Command interfaces are updated based on on reference inputs and current states.
Definition battery_state_broadcaster.cpp:322
controller_interface::CallbackReturn on_init() override
Extending interface with initialization method which is individual for each controller.
Definition battery_state_broadcaster.cpp:32
controller_interface::InterfaceConfiguration state_interface_configuration() const override
Get configuration for controller's required state interfaces.
Definition battery_state_broadcaster.cpp:188
controller_interface::InterfaceConfiguration command_interface_configuration() const override
Get configuration for controller's required command interfaces.
Definition battery_state_broadcaster.cpp:181
Definition controller_interface.hpp:27
Configuring what command/state interfaces to claim.
Definition controller_interface_base.hpp:72