Dixon/src/RobotNode/DixonBrain.cpp
Russell Gilbert c19687d91a Improves Dixon's shutdown and control flow.
Adds signal handling for graceful shutdown via SIGINT and SIGTERM.

Updates the control loop to be interruptible, allowing for a clean exit when a shutdown signal is received.

Changes the node state management to use an enum for clarity and better control the run states (starting, running, stopping, stopped).

The heartbeat is stopped when the node is stopping or stopped.

Also updates the line request to force an inactive state when claiming the pin.

Also updates the version number.
2026-02-12 09:53:29 +00:00

57 lines
No EOL
1.1 KiB
C++

#include "DixonBrain.h"
#include <iostream>
#include <chrono>
#include <thread>
DixonBrain::DixonBrain()
: heart_(),
state_(DixonNodeState::instance())
{
std::cout << "DixonBrain initialised\n";
}
DixonBrain::~DixonBrain()
{
stop();
std::cout << "DixonBrain destroyed\n";
}
void DixonBrain::start()
{
if (state_.getNodeStatus() != NodeStatus::Stopped)
return;
state_.setNodeStatus(NodeStatus::Running);
loopThread_ = std::thread(&DixonBrain::runLoop, this);
std::cout << "DixonBrain started\n";
}
void DixonBrain::stop()
{
if (state_.getNodeStatus() == NodeStatus::Stopped)
return;
state_.setNodeStatus(NodeStatus::Stopping);
if (loopThread_.joinable())
loopThread_.join();
heart_.stop();
state_.setNodeStatus(NodeStatus::Stopped);
std::cout << "DixonBrain stopped\n";
}
void DixonBrain::runLoop()
{
while (state_.getNodeStatus() != NodeStatus::Stopped)
{
heart_.beat();
// TODO: main control logic
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}