|
| 1 | +// SPDX-FileCopyrightText: 2022-2026 David Bensoussan |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +#include "dc_measurements/plugins/measurements/battery.hpp" |
| 5 | + |
| 6 | +#include <cmath> |
| 7 | + |
| 8 | +namespace dc_measurements |
| 9 | +{ |
| 10 | + |
| 11 | +namespace |
| 12 | +{ |
| 13 | +constexpr size_t kMaxPendingEvents = 64; |
| 14 | + |
| 15 | +// sensor_msgs/BatteryState leaves most fields optional and signals "unmeasured" with NaN, so a |
| 16 | +// field the hardware doesn't fill is left out of the Record rather than written as null. |
| 17 | +void setIfMeasured(json& data, const std::string& key, float value) |
| 18 | +{ |
| 19 | + if (!std::isnan(value)) |
| 20 | + { |
| 21 | + data[key] = value; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +std::string statusName(uint8_t status) |
| 26 | +{ |
| 27 | + switch (status) |
| 28 | + { |
| 29 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_CHARGING: |
| 30 | + return "charging"; |
| 31 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_DISCHARGING: |
| 32 | + return "discharging"; |
| 33 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_NOT_CHARGING: |
| 34 | + return "not_charging"; |
| 35 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_FULL: |
| 36 | + return "full"; |
| 37 | + default: |
| 38 | + return "unknown"; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +std::string healthName(uint8_t health) |
| 43 | +{ |
| 44 | + switch (health) |
| 45 | + { |
| 46 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_HEALTH_GOOD: |
| 47 | + return "good"; |
| 48 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_HEALTH_OVERHEAT: |
| 49 | + return "overheat"; |
| 50 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_HEALTH_DEAD: |
| 51 | + return "dead"; |
| 52 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_HEALTH_OVERVOLTAGE: |
| 53 | + return "overvoltage"; |
| 54 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_HEALTH_UNSPEC_FAILURE: |
| 55 | + return "unspecified_failure"; |
| 56 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_HEALTH_COLD: |
| 57 | + return "cold"; |
| 58 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE: |
| 59 | + return "watchdog_timer_expire"; |
| 60 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE: |
| 61 | + return "safety_timer_expire"; |
| 62 | + default: |
| 63 | + return "unknown"; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +std::string technologyName(uint8_t technology) |
| 68 | +{ |
| 69 | + switch (technology) |
| 70 | + { |
| 71 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_TECHNOLOGY_NIMH: |
| 72 | + return "nimh"; |
| 73 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_TECHNOLOGY_LION: |
| 74 | + return "lion"; |
| 75 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_TECHNOLOGY_LIPO: |
| 76 | + return "lipo"; |
| 77 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_TECHNOLOGY_LIFE: |
| 78 | + return "life"; |
| 79 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_TECHNOLOGY_NICD: |
| 80 | + return "nicd"; |
| 81 | + case sensor_msgs::msg::BatteryState::POWER_SUPPLY_TECHNOLOGY_LIMN: |
| 82 | + return "limn"; |
| 83 | + default: |
| 84 | + return "unknown"; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void setIfPresent(json& data, const std::string& key, const std::optional<double>& value) |
| 89 | +{ |
| 90 | + if (value) |
| 91 | + { |
| 92 | + data[key] = *value; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +double toSeconds(dc_common::ChargingSession::Duration duration) |
| 97 | +{ |
| 98 | + return std::chrono::duration<double>(duration).count(); |
| 99 | +} |
| 100 | +} // namespace |
| 101 | + |
| 102 | +Battery::Battery() : dc_measurements::Measurement() |
| 103 | +{ |
| 104 | +} |
| 105 | + |
| 106 | +Battery::~Battery() = default; |
| 107 | + |
| 108 | +void Battery::onConfigure() |
| 109 | +{ |
| 110 | + auto node = getNode(); |
| 111 | + battery_topic_ = dc_util::get_str_type_param(node, measurement_name_, "topic", "/battery_state"); |
| 112 | + // sensor_msgs/BatteryState specifies percentage on a 0-1 range; drivers that already publish |
| 113 | + // 0-100 are configured with a scale of 1.0. |
| 114 | + percentage_scale_ = dc_util::get_double_type_param(node, measurement_name_, "percentage_scale", 100.0); |
| 115 | + |
| 116 | + subscription_ = node->create_subscription<sensor_msgs::msg::BatteryState>( |
| 117 | + battery_topic_, rclcpp::SensorDataQoS(), std::bind(&Battery::batteryStateCb, this, std::placeholders::_1)); |
| 118 | +} |
| 119 | + |
| 120 | +void Battery::setValidationSchema() |
| 121 | +{ |
| 122 | + if (enable_validator_) |
| 123 | + { |
| 124 | + validateSchema("dc_measurements", "battery.json"); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +void Battery::batteryStateCb(const sensor_msgs::msg::BatteryState& msg) |
| 129 | +{ |
| 130 | + const auto now = getNode()->get_clock()->now(); |
| 131 | + const auto stamp = dc_common::BatteryCycleAccumulator::TimePoint(std::chrono::nanoseconds(now.nanoseconds())); |
| 132 | + |
| 133 | + const std::lock_guard<std::mutex> lock(mutex_); |
| 134 | + last_msg_ = msg; |
| 135 | + has_sample_ = true; |
| 136 | + |
| 137 | + std::optional<double> percentage; |
| 138 | + if (!std::isnan(msg.percentage)) |
| 139 | + { |
| 140 | + percentage = msg.percentage * percentage_scale_; |
| 141 | + } |
| 142 | + |
| 143 | + const auto update = |
| 144 | + accumulator_.update(percentage, static_cast<dc_common::PowerSupplyStatus>(msg.power_supply_status), stamp); |
| 145 | + |
| 146 | + if (update.started) |
| 147 | + { |
| 148 | + const auto& session = *update.started; |
| 149 | + json event; |
| 150 | + event["event"] = "charge_session_start"; |
| 151 | + event["session_id"] = session.sequence; |
| 152 | + event["discharge_depth_percent"] = session.preceding_discharge_depth; |
| 153 | + setIfPresent(event, "percentage", session.start_percentage); |
| 154 | + pending_events_.emplace_back(std::move(event), now); |
| 155 | + } |
| 156 | + if (update.ended) |
| 157 | + { |
| 158 | + const auto& session = *update.ended; |
| 159 | + json event; |
| 160 | + event["event"] = "charge_session_end"; |
| 161 | + event["session_id"] = session.sequence; |
| 162 | + event["duration_sec"] = toSeconds(session.duration(stamp)); |
| 163 | + setIfPresent(event, "start_percentage", session.start_percentage); |
| 164 | + setIfPresent(event, "end_percentage", session.end_percentage); |
| 165 | + if (session.start_percentage && session.end_percentage) |
| 166 | + { |
| 167 | + event["charged_percent"] = *session.end_percentage - *session.start_percentage; |
| 168 | + } |
| 169 | + pending_events_.emplace_back(std::move(event), now); |
| 170 | + } |
| 171 | + |
| 172 | + // One event leaves per poll, so a pack whose status flaps far faster than the polling interval |
| 173 | + // would otherwise queue without bound. The oldest goes first: the recent boundaries are the |
| 174 | + // ones still worth reporting. |
| 175 | + while (pending_events_.size() > kMaxPendingEvents) |
| 176 | + { |
| 177 | + pending_events_.pop_front(); |
| 178 | + RCLCPP_WARN_STREAM_THROTTLE(logger_, *getNode()->get_clock(), 10000, |
| 179 | + "Measurement " << measurement_name_ |
| 180 | + << ": charging session boundaries are arriving faster than the " |
| 181 | + "polling interval can report them; dropping the oldest."); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +json Battery::sampleRecord() const |
| 186 | +{ |
| 187 | + json data; |
| 188 | + data["event"] = "sample"; |
| 189 | + data["power_supply_status"] = statusName(last_msg_.power_supply_status); |
| 190 | + data["present"] = last_msg_.present; |
| 191 | + |
| 192 | + if (!std::isnan(last_msg_.percentage)) |
| 193 | + { |
| 194 | + data["percentage"] = last_msg_.percentage * percentage_scale_; |
| 195 | + } |
| 196 | + setIfMeasured(data, "voltage", last_msg_.voltage); |
| 197 | + setIfMeasured(data, "current", last_msg_.current); |
| 198 | + setIfMeasured(data, "charge", last_msg_.charge); |
| 199 | + setIfMeasured(data, "capacity", last_msg_.capacity); |
| 200 | + setIfMeasured(data, "design_capacity", last_msg_.design_capacity); |
| 201 | + setIfMeasured(data, "temperature", last_msg_.temperature); |
| 202 | + |
| 203 | + if (last_msg_.power_supply_health != sensor_msgs::msg::BatteryState::POWER_SUPPLY_HEALTH_UNKNOWN) |
| 204 | + { |
| 205 | + data["power_supply_health"] = healthName(last_msg_.power_supply_health); |
| 206 | + } |
| 207 | + if (last_msg_.power_supply_technology != sensor_msgs::msg::BatteryState::POWER_SUPPLY_TECHNOLOGY_UNKNOWN) |
| 208 | + { |
| 209 | + data["power_supply_technology"] = technologyName(last_msg_.power_supply_technology); |
| 210 | + } |
| 211 | + // State of health: what the pack still holds against what it was built to hold. Only the |
| 212 | + // hardware reporting both capacities can answer it. |
| 213 | + if (!std::isnan(last_msg_.capacity) && !std::isnan(last_msg_.design_capacity) && last_msg_.design_capacity > 0.0F) |
| 214 | + { |
| 215 | + data["health_percentage"] = 100.0 * last_msg_.capacity / last_msg_.design_capacity; |
| 216 | + } |
| 217 | + if (!last_msg_.location.empty()) |
| 218 | + { |
| 219 | + data["location"] = last_msg_.location; |
| 220 | + } |
| 221 | + if (!last_msg_.serial_number.empty()) |
| 222 | + { |
| 223 | + data["serial_number"] = last_msg_.serial_number; |
| 224 | + } |
| 225 | + |
| 226 | + data["completed_cycles"] = accumulator_.completedCycles(); |
| 227 | + if (const auto& session = accumulator_.openSession()) |
| 228 | + { |
| 229 | + data["session_id"] = session->sequence; |
| 230 | + } |
| 231 | + return data; |
| 232 | +} |
| 233 | + |
| 234 | +dc_interfaces::msg::StringStamped Battery::collect() |
| 235 | +{ |
| 236 | + auto node = getNode(); |
| 237 | + dc_interfaces::msg::StringStamped msg; |
| 238 | + msg.group_key = group_key_; |
| 239 | + |
| 240 | + const std::lock_guard<std::mutex> lock(mutex_); |
| 241 | + |
| 242 | + // A session boundary takes the poll it lands on: it is a fact about a moment, so it keeps the |
| 243 | + // timestamp of that moment rather than this poll's. |
| 244 | + if (!pending_events_.empty()) |
| 245 | + { |
| 246 | + auto event = std::move(pending_events_.front()); |
| 247 | + pending_events_.pop_front(); |
| 248 | + msg.header.stamp = event.second; |
| 249 | + msg.data = event.first.dump(-1, ' ', true); |
| 250 | + return msg; |
| 251 | + } |
| 252 | + |
| 253 | + // Nothing on the topic yet: report nothing rather than a Record full of absent fields. |
| 254 | + if (!has_sample_) |
| 255 | + { |
| 256 | + return msg; |
| 257 | + } |
| 258 | + |
| 259 | + msg.header.stamp = node->get_clock()->now(); |
| 260 | + msg.data = sampleRecord().dump(-1, ' ', true); |
| 261 | + return msg; |
| 262 | +} |
| 263 | + |
| 264 | +} // namespace dc_measurements |
| 265 | + |
| 266 | +#include "pluginlib/class_list_macros.hpp" |
| 267 | +PLUGINLIB_EXPORT_CLASS(dc_measurements::Battery, dc_core::Measurement) |
0 commit comments