Skip to content

Commit 968fe0f

Browse files
author
Yara Shahin
committed
add compatibility for ipa320 broadcaster
1 parent 67045a9 commit 968fe0f

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

battery_state_broadcaster/src/battery_state_broadcaster.cpp

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,27 @@ controller_interface::CallbackReturn BatteryStateBroadcaster::on_configure(
5050
const rclcpp_lifecycle::State & /*previous_state*/)
5151
{
5252
params_ = param_listener_->get_params();
53-
state_joints_ = params_.state_joints;
54-
battery_presence_.resize(state_joints_.size(), false);
53+
if (!params_.sensor_name.empty())
54+
{
55+
if (params_.state_joints.size() > 0)
56+
{
57+
RCLCPP_ERROR(
58+
get_node()->get_logger(),
59+
"You cannot use both 'sensor_name' and 'state_joints' parameters. Please use only "
60+
"'state_joints' going forward.");
61+
return controller_interface::CallbackReturn::ERROR;
62+
}
63+
RCLCPP_WARN(
64+
get_node()->get_logger(),
65+
"The 'sensor_name' parameter is deprecated and will be removed in future releases. Please "
66+
"use 'state_joints' parameter instead.");
67+
state_joints_ = {params_.sensor_name};
68+
}
69+
else
70+
{
71+
state_joints_ = params_.state_joints;
72+
}
73+
battery_presence_.resize(params_.state_joints.size(), false);
5574

5675
try
5776
{
@@ -86,8 +105,8 @@ controller_interface::CallbackReturn BatteryStateBroadcaster::on_configure(
86105

87106
raw_battery_states_realtime_publisher_->lock();
88107
auto & msg = raw_battery_states_realtime_publisher_->msg_;
89-
msg.battery_states.reserve(state_joints_.size());
90-
for (size_t i = 0; i < state_joints_.size(); ++i)
108+
msg.battery_states.reserve(params_.state_joints.size());
109+
for (size_t i = 0; i < params_.state_joints.size(); ++i)
91110
{
92111
sensor_msgs::msg::BatteryState battery;
93112
battery.location.reserve(MAX_LENGTH);
@@ -97,7 +116,7 @@ controller_interface::CallbackReturn BatteryStateBroadcaster::on_configure(
97116
raw_battery_states_realtime_publisher_->unlock();
98117

99118
// Get count of enabled joints for each interface
100-
for (size_t i = 0; i < state_joints_.size(); ++i)
119+
for (size_t i = 0; i < params_.state_joints.size(); ++i)
101120
{
102121
const auto & interfaces = params_.interfaces.state_joints_map.at(params_.state_joints.at(i));
103122
const auto & battery_properties = params_.state_joints_map.at(params_.state_joints.at(i));
@@ -152,8 +171,15 @@ BatteryStateBroadcaster::state_interface_configuration() const
152171

153172
state_interfaces_config.type = controller_interface::interface_configuration_type::INDIVIDUAL;
154173

155-
state_interfaces_config.names.reserve(state_joints_.size() * 8);
156-
for (const auto & joint : state_joints_)
174+
if (!params_.sensor_name.empty())
175+
{
176+
state_interfaces_config.names.reserve(1);
177+
state_interfaces_config.names.push_back(params_.sensor_name + "/voltage");
178+
return state_interfaces_config;
179+
}
180+
181+
state_interfaces_config.names.reserve(params_.state_joints.size() * 8);
182+
for (const auto & joint : params_.state_joints)
157183
{
158184
const auto & interfaces = params_.interfaces.state_joints_map.at(joint);
159185
state_interfaces_config.names.push_back(joint + "/battery_voltage");
@@ -210,12 +236,12 @@ controller_interface::CallbackReturn BatteryStateBroadcaster::on_activate(
210236

211237
// handle individual battery states initializations
212238
auto & raw_battery_states_msg = raw_battery_states_realtime_publisher_->msg_;
213-
for (size_t i = 0; i < state_joints_.size(); ++i)
239+
for (size_t i = 0; i < params_.state_joints.size(); ++i)
214240
{
215241
auto & battery_state = raw_battery_states_msg.battery_states[i];
216242
const auto & battery_properties = params_.state_joints_map.at(params_.state_joints.at(i));
217243

218-
battery_state.header.frame_id = state_joints_[i];
244+
battery_state.header.frame_id = params_.state_joints[i];
219245
battery_state.voltage = kUninitializedValue;
220246
battery_state.temperature = kUninitializedValue;
221247
battery_state.current = kUninitializedValue;
@@ -242,6 +268,12 @@ controller_interface::CallbackReturn BatteryStateBroadcaster::on_activate(
242268
combined_serial_number += battery_state.serial_number + ", ";
243269
}
244270

271+
if (!params_.sensor_name.empty())
272+
{
273+
sums_.design_capacity_sum = static_cast<float>(params_.design_capacity);
274+
combined_power_supply_technology = static_cast<uint8_t>(params_.power_supply_technology);
275+
}
276+
245277
// handle aggregate battery state initialization
246278
auto & battery_state_msg = battery_state_realtime_publisher_->msg_;
247279

@@ -285,7 +317,7 @@ controller_interface::return_type BatteryStateBroadcaster::update(
285317
if (raw_battery_states_realtime_publisher_ && raw_battery_states_realtime_publisher_->trylock())
286318
{
287319
auto & raw_battery_states_msg = raw_battery_states_realtime_publisher_->msg_;
288-
for (size_t i = 0; i < state_joints_.size(); ++i)
320+
for (size_t i = 0; i < params_.state_joints.size(); ++i)
289321
{
290322
const auto & interfaces = params_.interfaces.state_joints_map.at(params_.state_joints.at(i));
291323

@@ -385,6 +417,12 @@ controller_interface::return_type BatteryStateBroadcaster::update(
385417
raw_battery_states_realtime_publisher_->unlockAndPublish();
386418
}
387419

420+
if (!params_.sensor_name.empty())
421+
{
422+
sums_.voltage_sum =
423+
static_cast<float>(state_interfaces_[0].get_optional<double>().value_or(kUninitializedValue));
424+
}
425+
388426
if (battery_state_realtime_publisher_ && battery_state_realtime_publisher_->trylock())
389427
{
390428
auto & battery_state_msg = battery_state_realtime_publisher_->msg_;

battery_state_broadcaster/src/battery_state_broadcaster.yaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ battery_state_broadcaster:
1818
description: "List of joints from which battery state interfaces will be read.",
1919
read_only: true,
2020
validation: {
21-
unique<>: null,
22-
size_gt<>: [0]
21+
unique<>: null
2322
}
2423
}
2524
interfaces:
@@ -105,3 +104,24 @@ battery_state_broadcaster:
105104
description: "Serial number of the battery.",
106105
read_only: true,
107106
}
107+
sensor_name: {
108+
type: string,
109+
default_value: "",
110+
description: "Sensor name of the battery. If provided, the 'voltage' state interface of this sensor will be used to populate the voltage field in the BatteryState message. If this parameter is used, the state joints and interfaces parameters are ignored.",
111+
read_only: true,
112+
}
113+
design_capacity: {
114+
type: double,
115+
default_value: .nan,
116+
description: "Design capacity of the battery [Ah] for the sensor_name mode (If unmeasured NaN).",
117+
read_only: true,
118+
}
119+
power_supply_technology: {
120+
type: int,
121+
default_value: 0,
122+
description: "Battery chemistry type as an enum for the sensor_name mode. If unmeasured, the technology is set to unknown.",
123+
read_only: true,
124+
validation: {
125+
bounds<>: [0, 8]
126+
}
127+
}

0 commit comments

Comments
 (0)