Protective Stop
Loading...
Searching...
No Matches
hardware_backend.hpp
1// SPDX-FileCopyrightText: 2026 Polymath Robotics, Inc.
2// SPDX-License-Identifier: Apache-2.0
3#pragma once
4
5#include <atomic>
6#include <memory>
7#include <mutex>
8#include <string>
9#include <thread>
10
11#include "protective_stop_machine/backend.hpp"
12
13namespace protective_stop_machine
14{
15
18{
20 std::string device_url{"http://127.0.0.1"};
22 std::string admin_user{"admin"};
24 std::string admin_pass;
26 double poll_hz{5.0};
28 double http_timeout_s{2.0};
29};
30
36{
37public:
39 explicit HardwareMachineBackend(const HardwareConfig & config);
40 ~HardwareMachineBackend() override;
41
42 bool start() override;
43 void stop() override;
44 MachineSnapshot snapshot() const override;
45 bool configure(const MachineTiming & timing, std::string & error) override;
46
47 const char * name() const override
48 {
49 return "hardware";
50 }
51
54 static void parse_state(const std::string & body, MachineSnapshot & out_snapshot);
55
56private:
57 void poll_loop();
60 // NOLINTNEXTLINE(runtime/int)
61 bool http_get(const std::string & path, std::string & body, long & status);
62 // NOLINTNEXTLINE(runtime/int)
63 bool http_post(const std::string & path, const std::string & json, std::string & body, long & status);
64
65 HardwareConfig config_;
66 std::thread poll_thread_;
67 std::atomic<bool> running_{false};
68 mutable std::mutex snapshot_mutex_;
69 MachineSnapshot latest_snapshot_;
70};
71
72} // namespace protective_stop_machine
HardwareMachineBackend(const HardwareConfig &config)
Copies config; no I/O until start().
const char * name() const override
Backend kind, "software" or "hardware".
Definition hardware_backend.hpp:47
void stop() override
Stops servicing and leaves the machine in its SAFE state.
MachineSnapshot snapshot() const override
Thread-safe copy of the latest state.
static void parse_state(const std::string &body, MachineSnapshot &out_snapshot)
Maps a /state.json body onto a snapshot.
bool configure(const MachineTiming &timing, std::string &error) override
Applies an already safety-validated timing change.
bool start() override
Begins servicing: software binds UDP and runs the machine loop, hardware starts polling.
Backend seam the node depends on, implemented by SoftwareMachineBackend (hosts pstop_c) and HardwareM...
Definition backend.hpp:113
Deployment values for the hardware backend.
Definition hardware_backend.hpp:18
double http_timeout_s
HTTP request timeout, in seconds.
Definition hardware_backend.hpp:28
std::string device_url
ESP32 machn admin/state URL.
Definition hardware_backend.hpp:20
std::string admin_user
Admin API user name.
Definition hardware_backend.hpp:22
double poll_hz
/state.json polling rate, in hertz.
Definition hardware_backend.hpp:26
std::string admin_pass
Read from PSTOP_MACHINE_ADMIN_PASS, never a param file.
Definition hardware_backend.hpp:24
One immutable read of machine state, mapped straight onto the ROS messages.
Definition backend.hpp:64
Runtime-settable timing envelope.
Definition backend.hpp:100