Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SW-642] Make it obvious when spot is running out of battery #252

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spot_driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ install(
DESTINATION lib/${PROJECT_NAME}
RENAME spot_ros2
)
install(
PROGRAMS
spot_driver/spot_alerts.py
DESTINATION lib/${PROJECT_NAME}
RENAME spot_alerts
)
# Install Libraries
install(
TARGETS
Expand Down
9 changes: 9 additions & 0 deletions spot_driver/launch/spot_driver.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ def launch_setup(context: LaunchContext, ld: LaunchDescription) -> None:
)
ld.add_action(robot_state_publisher)

spot_alert_node = launch_ros.actions.Node(
package="spot_driver",
executable="spot_alerts",
name="spot_alerts",
output="screen",
namespace=spot_name,
)
ld.add_action(spot_alert_node)

rviz = IncludeLaunchDescription(
PythonLaunchDescriptionSource([FindPackageShare(THIS_PACKAGE), "/launch", "/rviz.launch.py"]),
launch_arguments={
Expand Down
52 changes: 52 additions & 0 deletions spot_driver/spot_driver/spot_alerts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
from tkinter import messagebox
from typing import Any, List, Optional

import bdai_ros2_wrappers.process as ros_process
from bdai_ros2_wrappers.node import Node
from rclpy.parameter import Parameter

from spot_msgs.msg import ( # type: ignore
BatteryStateArray,
)


class SpotAlerts(Node):
"""Monitors spot states and will generate a pop-up message to alert the user of warnings"""

def __init__(self, **kwargs: Any) -> None:
super().__init__("spot_alerts", **kwargs)
# Subscribers #
self.battery_states = self.create_subscription(
BatteryStateArray, "status/battery_states", self.battery_callback, 1
)

# Parameters #
self.declare_parameter("low_battery", False)
tcappellari-bdai marked this conversation as resolved.
Show resolved Hide resolved

def battery_callback(self, battery_array_msg: BatteryStateArray) -> None:
"""A callback function to check the battery percentage and send a pop-up alert when <= 10%
Args:
battery_array_msg: BatteryStateArray message received from the publisher containing BatteryState messages
Returns:
None
"""
for battery_msg in battery_array_msg.battery_states:
if battery_msg.charge_percentage <= 10 and not self.get_parameter("low_battery").value:
tcappellari-bdai marked this conversation as resolved.
Show resolved Hide resolved
low_battery_param = Parameter("low_battery", Parameter.Type.BOOL, True)
self.set_parameters([low_battery_param])
messagebox.showwarning(
title="Warning: Low Battery {}".format(battery_msg.identifier),
message=(
"Battery is at {} %. Approximately {} minutes remaining.\n Please charge your Spot soon."
).format(battery_msg.charge_percentage, round(battery_msg.estimated_runtime.sec / 60)),
)


@ros_process.main(prebaked=False)
tcappellari-bdai marked this conversation as resolved.
Show resolved Hide resolved
def main(args: Optional[List[str]] = None) -> None:
ros_process.spin(SpotAlerts)


if __name__ == "__main__":
main()
Loading