PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
battery.hpp
1 /****************************************************************************
2  * Copyright (c) 2023-2024 PX4 Development Team.
3  * SPDX-License-Identifier: BSD-3-Clause
4  ****************************************************************************/
5 
6 #pragma once
7 
8 #include <Eigen/Eigen>
9 #include <px4_msgs/msg/battery_status.hpp>
10 #include <px4_ros2/common/context.hpp>
11 #include <px4_ros2/utils/subscription.hpp>
12 #include <px4_ros2/utils/message_version.hpp>
13 
14 namespace px4_ros2
15 {
25 class Battery : public Subscription<px4_msgs::msg::BatteryStatus>
26 {
27 public:
28  explicit Battery(Context & context)
30  "fmu/out/battery_status" + px4_ros2::getMessageNameVersion<px4_msgs::msg::BatteryStatus>()) {}
31 
37  float voltageV() const
38  {
39  return last().voltage_v;
40  }
41 
47  float currentA() const
48  {
49  return last().current_a;
50  }
51 
57  float dischargedCapacityMah() const
58  {
59  return last().discharged_mah;
60  }
61 
67  float remaningFraction() const
68  {
69  return last().remaining;
70  }
71 
77  uint8_t cellCount() const
78  {
79  return last().cell_count;
80  }
81 
87  Eigen::VectorXf cellVoltagesV() const
88  {
89  const px4_msgs::msg::BatteryStatus & battery = last();
90  const uint8_t cell_count = battery.cell_count;
91 
92  if (cell_count == 0) {
93  RCLCPP_WARN_THROTTLE(
94  _node.get_logger(),
95  *_node.get_clock(), 1000, "Failed to retrieve battery cell voltage: cell count unknown.");
96  return Eigen::VectorXf();
97  }
98 
99  Eigen::VectorXf cell_voltages(cell_count);
100  for (uint8_t i = 0; i < cell_count; ++i) {
101  cell_voltages(i) = battery.voltage_cell_v[i];
102  }
103 
104  return cell_voltages;
105  }
106 
107 };
108 
110 } /* namespace px4_ros2 */
Provides access to the vehicle's battery status.
Definition: battery.hpp:26
uint8_t cellCount() const
Get the vehicle's battery cell count.
Definition: battery.hpp:77
float dischargedCapacityMah() const
Get the vehicle's battery cumulative discharged capacity.
Definition: battery.hpp:57
float currentA() const
Get the vehicle's battery current.
Definition: battery.hpp:47
float voltageV() const
Get the vehicle's battery voltage.
Definition: battery.hpp:37
float remaningFraction() const
Get the vehicle's battery remaining charge as a fraction.
Definition: battery.hpp:67
Eigen::VectorXf cellVoltagesV() const
Get the voltages of each cell in the vehicle's battery.
Definition: battery.hpp:87
Definition: context.hpp:20
Provides a subscription to arbitrary ROS topics.
Definition: subscription.hpp:23
const px4_msgs::msg::BatteryStatus & last() const
Get the last-received message.
Definition: subscription.hpp:58