Protective Stop
Loading...
Searching...
No Matches
backend.hpp
1// SPDX-FileCopyrightText: 2026 Polymath Robotics, Inc.
2// SPDX-License-Identifier: Apache-2.0
3#pragma once
4
5#include <cstdint>
6#include <string>
7#include <vector>
8
9namespace protective_stop_machine
10{
11
13enum class MachineState : uint8_t
14{
16 ACTIVE = 0,
18 DEACTIVATED = 1,
20 UNSTABLE = 2
21};
22
25{
27 std::string device_id;
29 uint8_t bond_state{0};
31 bool in_use{false};
33 bool stop_only{false};
35 uint32_t reply_age_ms{0};
37 uint32_t loop_rtt_ms{0};
39 uint32_t disco_rtt_ms{0};
41 uint32_t rebonds{0};
42};
43
46{
48 bool applicable{false};
50 bool run{false};
52 bool relay_stop{true};
54 bool fault_a{false};
56 bool fault_b{false};
58 uint32_t mismatch{0};
59};
60
64{
66 bool reachable{false};
68 bool running{false};
70 bool need_stop{false};
72 std::string status_reason;
76 std::vector<RemoteInfo> remotes;
78 uint32_t active_remotes{0};
84 uint32_t bonds_refused{0};
85
87 MachineState state() const
88 {
89 if (!reachable) {
90 return MachineState::UNSTABLE;
91 }
92 return running ? MachineState::ACTIVE : MachineState::DEACTIVATED;
93 }
94};
95
100{
102 uint64_t heartbeat_ms{400};
104 uint16_t max_missed{3};
106 uint64_t min_stop_ms{500};
107};
108
113{
114public:
115 virtual ~IMachineBackend() = default;
116
120 virtual bool start() = 0;
121
125 virtual void stop() = 0;
126
128 virtual MachineSnapshot snapshot() const = 0;
129
134 virtual bool configure(const MachineTiming & timing, std::string & error) = 0;
135
137 virtual const char * name() const = 0;
138};
139
140} // namespace protective_stop_machine
Backend seam the node depends on, implemented by SoftwareMachineBackend (hosts pstop_c) and HardwareM...
Definition backend.hpp:113
virtual MachineSnapshot snapshot() const =0
Thread-safe copy of the latest state.
virtual bool configure(const MachineTiming &timing, std::string &error)=0
Applies an already safety-validated timing change.
virtual void stop()=0
Stops servicing and leaves the machine in its SAFE state.
virtual bool start()=0
Begins servicing: software binds UDP and runs the machine loop, hardware starts polling.
virtual const char * name() const =0
Backend kind, "software" or "hardware".
One immutable read of machine state, mapped straight onto the ROS messages.
Definition backend.hpp:64
std::string status_reason
Human-readable cause of the current state.
Definition backend.hpp:72
bool need_stop
Machine awaits a STOP before it can be re-armed.
Definition backend.hpp:70
std::vector< RemoteInfo > remotes
Known remotes.
Definition backend.hpp:76
bool reachable
Backend can reach the machine.
Definition backend.hpp:66
RelayInfo relay
Relay state.
Definition backend.hpp:74
uint32_t heartbeat_timeout_stops
Stops caused by a remote missing its heartbeats, since the backend started.
Definition backend.hpp:81
MachineState state() const
State derived from reachable and running.
Definition backend.hpp:87
bool running
Machine is cleared to run.
Definition backend.hpp:68
uint32_t bonds_refused
Bonds refused by the allow/denylist, since the backend started.
Definition backend.hpp:84
uint32_t active_remotes
Number of bonded remotes.
Definition backend.hpp:78
Runtime-settable timing envelope.
Definition backend.hpp:100
uint16_t max_missed
Heartbeats that may be missed before the machine stops.
Definition backend.hpp:104
uint64_t min_stop_ms
Minimum time between STOP and OK, in milliseconds.
Definition backend.hpp:106
uint64_t heartbeat_ms
Heartbeat interval, in milliseconds.
Definition backend.hpp:102
Relay and fault state of a hardware machine.
Definition backend.hpp:46
bool fault_b
Fault flag of channel B.
Definition backend.hpp:56
uint32_t mismatch
Count of A/B channel mismatches.
Definition backend.hpp:58
bool relay_stop
Stop relay asserted.
Definition backend.hpp:52
bool applicable
Set by the hardware backend; the software backend has no relays.
Definition backend.hpp:48
bool run
Run relay energized.
Definition backend.hpp:50
bool fault_a
Fault flag of channel A.
Definition backend.hpp:54
State of one remote as seen by the machine.
Definition backend.hpp:25
uint32_t loop_rtt_ms
Loop round-trip time, in milliseconds.
Definition backend.hpp:37
uint32_t disco_rtt_ms
Discovery round-trip time, in milliseconds.
Definition backend.hpp:39
std::string device_id
8 hex digits, e.g. "01d7eed0".
Definition backend.hpp:27
uint32_t reply_age_ms
Milliseconds since the last reply from the remote.
Definition backend.hpp:35
bool stop_only
Remote may stop but not re-arm.
Definition backend.hpp:33
uint32_t rebonds
Number of times the remote has re-bonded.
Definition backend.hpp:41
uint8_t bond_state
0 empty, 1 connecting, 2 bonded, 3 stopped.
Definition backend.hpp:29
bool in_use
Owns the current arming cycle.
Definition backend.hpp:31