|
| 1 | +/** |
| 2 | +Software License Agreement (BSD) |
| 3 | +\file software_lvc.hpp |
| 4 | +\authors Tony Baltovski <[email protected]> |
| 5 | +\copyright Copyright (c) 2025, Clearpath Robotics, Inc., All rights reserved. |
| 6 | +Redistribution and use in source and binary forms, with or without modification, are permitted provided that |
| 7 | +the following conditions are met: |
| 8 | + * Redistributions of source code must retain the above copyright notice, this list of conditions and the |
| 9 | + following disclaimer. |
| 10 | + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the |
| 11 | + following disclaimer in the documentation and/or other materials provided with the distribution. |
| 12 | + * Neither the name of Clearpath Robotics nor the names of its contributors may be used to endorse or promote |
| 13 | + products derived from this software without specific prior written permission. |
| 14 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WAR- |
| 15 | +RANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, IN- |
| 17 | +DIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 18 | +OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 19 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 20 | +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include "clearpath_hardware_interfaces/a300/software_lvc.hpp" |
| 24 | + |
| 25 | + |
| 26 | +a300_slvc::SoftwareLowVoltageCutoff::SoftwareLowVoltageCutoff() : Node("software_low_voltage_cutoff") |
| 27 | +{ |
| 28 | + battery_subscriber_ = this->create_subscription<sensor_msgs::msg::BatteryState>( |
| 29 | + BATTERY_STATE_TOPIC, 10, |
| 30 | + std::bind(&SoftwareLowVoltageCutoff::battery_callback, this, std::placeholders::_1)); |
| 31 | + |
| 32 | + client_ = this->create_client<std_srvs::srv::Empty>("low_battery_service"); |
| 33 | +} |
| 34 | + |
| 35 | +void a300_slvc::SoftwareLowVoltageCutoff::battery_callback(const sensor_msgs::msg::BatteryState::SharedPtr msg) |
| 36 | +{ |
| 37 | + if (!msg->present) |
| 38 | + { |
| 39 | + RCLCPP_WARN_THROTTLE(this->get_logger(), *this->get_clock(), 1000, |
| 40 | + "Battery not present. Ignoring message."); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + float charge_percentage = msg->percentage * 100.0; |
| 45 | + |
| 46 | + if (charge_percentage <= WARNING_THRESHOLD && charge_percentage > CRITICAL_THRESHOLD) |
| 47 | + { |
| 48 | + RCLCPP_WARN_THROTTLE(this->get_logger(), *this->get_clock(), 1000, |
| 49 | + "Battery charge low: %.2f%%", charge_percentage); |
| 50 | + } |
| 51 | + else if (charge_percentage <= CRITICAL_THRESHOLD) |
| 52 | + { |
| 53 | + RCLCPP_ERROR_THROTTLE( |
| 54 | + this->get_logger(), *this->get_clock(), 1000, "Battery critically low: %.2f%%", charge_percentage); |
| 55 | + this->consecutive_low_readings_++; |
| 56 | + |
| 57 | + // Gather many readings before calling the service |
| 58 | + if (this->consecutive_low_readings_ >= CRITICAL_READINGS_REQUIRED) |
| 59 | + { |
| 60 | + RCLCPP_FATAL(this->get_logger(), |
| 61 | + "Battery critically low, calling cmd shutdown service on MCU to power off robot."); |
| 62 | + call_low_battery_service(); |
| 63 | + } |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + this->consecutive_low_readings_ = 0; // Reset counter if charge goes above critical level |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void a300_slvc::SoftwareLowVoltageCutoff::call_low_battery_service() |
| 72 | +{ |
| 73 | + if (!client_->wait_for_service(std::chrono::seconds(1))) |
| 74 | + { |
| 75 | + RCLCPP_ERROR(this->get_logger(), "Low battery service unavailable"); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + auto request = std::make_shared<std_srvs::srv::Empty::Request>(); |
| 80 | + auto future = client_->async_send_request(request); |
| 81 | +} |
| 82 | + |
| 83 | +int main(int argc, char **argv) |
| 84 | +{ |
| 85 | + rclcpp::init(argc, argv); |
| 86 | + rclcpp::spin(std::make_shared<a300_slvc::SoftwareLowVoltageCutoff>()); |
| 87 | + rclcpp::shutdown(); |
| 88 | + return 0; |
| 89 | +} |
0 commit comments