-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e98e56
commit 192c5bc
Showing
7 changed files
with
228 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
|
||
HEALTHCHECK_FILE="/health_status.txt" | ||
|
||
# Function to start the ROS 2 healthcheck node | ||
start_healthcheck_node() { | ||
/ros_entrypoint.sh ros2 run healthcheck_pkg healthcheck_$SLAM_MODE & | ||
} | ||
|
||
if [ ! -f "$HEALTHCHECK_FILE" ]; then | ||
echo "Healthcheck file not found. Starting ROS 2 healthcheck node..." | ||
start_healthcheck_node | ||
# Wait a bit to allow the node to start and write its initial status | ||
sleep 2 | ||
fi | ||
|
||
# Now check the health status | ||
if [ -f "$HEALTHCHECK_FILE" ]; then | ||
status=$(cat "$HEALTHCHECK_FILE") | ||
if [ "$status" == "healthy" ]; then | ||
exit 0 | ||
else | ||
exit 1 | ||
fi | ||
else | ||
echo "Healthcheck file still not found. There may be an issue with the node." | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "rclcpp/rclcpp.hpp" | ||
#include "bond/msg/status.hpp" | ||
#include "fstream" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
#define LOOP_PERIOD 2s | ||
#define MSG_VALID_TIME 5s | ||
|
||
std::chrono::steady_clock::time_point last_msg_time; | ||
|
||
void write_health_status(const std::string& status) | ||
{ | ||
std::ofstream healthFile("/health_status.txt"); | ||
healthFile << status; | ||
healthFile.close(); | ||
} | ||
|
||
void msg_callback(const bond::msg::Status::SharedPtr msg) | ||
{ | ||
write_health_status("healthy"); | ||
last_msg_time = std::chrono::steady_clock::now(); | ||
} | ||
|
||
void healthy_check(const rclcpp::Node::SharedPtr& node) | ||
{ | ||
std::chrono::steady_clock::time_point current_time = std::chrono::steady_clock::now(); | ||
std::chrono::duration<double> elapsed_time = current_time - last_msg_time; | ||
bool is_msg_valid = elapsed_time.count() < MSG_VALID_TIME.count(); | ||
|
||
if (is_msg_valid) | ||
{ | ||
RCLCPP_INFO(node->get_logger(), "Health check: healthy"); | ||
write_health_status("healthy"); | ||
} | ||
else | ||
{ | ||
RCLCPP_WARN(node->get_logger(), "Health check: unhealthy"); | ||
write_health_status("unhealthy"); | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
rclcpp::init(argc, argv); | ||
auto node = rclcpp::Node::make_shared("healthcheck_node"); | ||
auto sub = node->create_subscription<bond::msg::Status>("/bond", rclcpp::SystemDefaultsQoS(), msg_callback); | ||
|
||
while (rclcpp::ok()) | ||
{ | ||
rclcpp::spin_some(node); | ||
healthy_check(node); | ||
std::this_thread::sleep_for(LOOP_PERIOD); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#include "rclcpp/rclcpp.hpp" | ||
#include "nav_msgs/msg/occupancy_grid.hpp" | ||
#include "nav2_msgs/srv/save_map.hpp" | ||
#include <fstream> // Include <fstream> for file operations | ||
#include <thread> // Include <thread> for sleep_for | ||
|
||
using namespace std::chrono_literals; | ||
|
||
#define LOOP_PERIOD 2s | ||
#define MSG_VALID_TIME 5s | ||
#define SAVE_MAP_PERIOD 15s | ||
#define SAVE_MAP_CONNECTION_TIMEOUT 2s | ||
#define SAVE_MAP_VALID_TIME 20s | ||
|
||
class HealthCheckNode : public rclcpp::Node | ||
{ | ||
public: | ||
HealthCheckNode() : Node("healthcheck_node") | ||
{ | ||
rclcpp::QoS qos(1); | ||
qos.transient_local(); | ||
map_subscriber_ = create_subscription<nav_msgs::msg::OccupancyGrid>( | ||
"/map", qos, std::bind(&HealthCheckNode::msgCallback, this, std::placeholders::_1)); | ||
save_map_client_ = create_client<nav2_msgs::srv::SaveMap>("/map_saver/save_map"); | ||
} | ||
|
||
void healthyCheck() | ||
{ | ||
auto current_time = std::chrono::steady_clock::now(); | ||
std::chrono::duration<double> msg_elapsed_time = current_time - last_msg_time; | ||
std::chrono::duration<double> saved_map_elapsed_time = current_time - last_saved_map_time; | ||
bool is_msg_valid = msg_elapsed_time.count() < MSG_VALID_TIME.count(); | ||
bool is_save_map_valid = saved_map_elapsed_time.count() < SAVE_MAP_VALID_TIME.count(); | ||
|
||
if (is_msg_valid && is_save_map_valid) | ||
{ | ||
RCLCPP_INFO(get_logger(), "Health check: healthy"); | ||
writeHealthStatus("healthy"); | ||
} | ||
else | ||
{ | ||
RCLCPP_WARN(get_logger(), "Health check: unhealthy"); | ||
writeHealthStatus("unhealthy"); | ||
} | ||
} | ||
|
||
void saveMapPeriodically() | ||
{ | ||
auto current_time = std::chrono::steady_clock::now(); | ||
std::chrono::duration<double> elapsed_time = current_time - last_saved_map_time; | ||
|
||
if (elapsed_time.count() > SAVE_MAP_PERIOD.count()) | ||
{ | ||
if (save_map_client_->wait_for_service(SAVE_MAP_CONNECTION_TIMEOUT)) | ||
{ | ||
RCLCPP_DEBUG(get_logger(), "Service available"); | ||
auto request = std::make_shared<nav2_msgs::srv::SaveMap::Request>(); | ||
request->free_thresh = 0.15; | ||
request->map_topic = "/map"; | ||
request->map_url = "/maps/map"; | ||
request->map_mode = "trinary"; | ||
request->image_format = "png"; | ||
|
||
auto future = save_map_client_->async_send_request(request); | ||
|
||
if (rclcpp::spin_until_future_complete(shared_from_this(), future, SAVE_MAP_CONNECTION_TIMEOUT) == rclcpp::FutureReturnCode::SUCCESS) | ||
{ | ||
RCLCPP_DEBUG(get_logger(), "Map saved"); | ||
last_saved_map_time = std::chrono::steady_clock::now(); | ||
} | ||
else | ||
{ | ||
RCLCPP_DEBUG(get_logger(), "Service response didn't arrived"); | ||
} | ||
} | ||
else | ||
{ | ||
RCLCPP_DEBUG(get_logger(), "Service unavailable"); | ||
} | ||
} | ||
} | ||
|
||
private: | ||
rclcpp::Subscription<nav_msgs::msg::OccupancyGrid>::SharedPtr map_subscriber_; | ||
rclcpp::Client<nav2_msgs::srv::SaveMap>::SharedPtr save_map_client_; | ||
std::chrono::steady_clock::time_point last_msg_time; | ||
std::chrono::steady_clock::time_point last_saved_map_time; | ||
|
||
void writeHealthStatus(const std::string &status) | ||
{ | ||
std::ofstream healthFile("/health_status.txt"); | ||
healthFile << status; | ||
} | ||
|
||
void msgCallback(const nav_msgs::msg::OccupancyGrid::SharedPtr msg) | ||
{ | ||
RCLCPP_DEBUG(get_logger(), "Msg arrived"); | ||
last_msg_time = std::chrono::steady_clock::now(); | ||
} | ||
}; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
rclcpp::init(argc, argv); | ||
auto node = std::make_shared<HealthCheckNode>(); | ||
|
||
while (rclcpp::ok()) | ||
{ | ||
rclcpp::spin_some(node); | ||
node->healthyCheck(); | ||
node->saveMapPeriodically(); | ||
std::this_thread::sleep_for(LOOP_PERIOD); | ||
} | ||
|
||
return 0; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.