CTRE Phoenix 6 C++ 26.50.0-alpha-1
Loading...
Searching...
No Matches
ReplayAutoEnable.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
10#include <wpi/simulation/DriverStationSim.hpp>
11#include <wpi/system/Notifier.hpp>
12#include <mutex>
13
14namespace ctre {
15namespace phoenix6 {
16namespace wpiutils {
17
18class ReplayAutoEnable final {
19public:
20 static ReplayAutoEnable &GetInstance()
21 {
22 static ReplayAutoEnable &replayAutoEnable = *new ReplayAutoEnable{};
23 return replayAutoEnable;
24 }
25
26private:
27 std::mutex _lck;
28 wpi::Notifier _enableNotifier;
29 uint32_t _startCount = 0;
30
32 _enableNotifier{[] {
34 auto dsAttachedSig = HootReplay::GetBoolean("DS:IsDSAttached");
35 if (dsAttachedSig.status.IsOK()) {
36 wpi::sim::DriverStationSim::SetDsAttached(dsAttachedSig.value);
37 }
38
39 if (wpi::sim::DriverStationSim::GetDsAttached()) {
40 auto fmsAttachedSig = HootReplay::GetBoolean("DS:IsFMSAttached");
41 if (fmsAttachedSig.status.IsOK()) {
42 wpi::sim::DriverStationSim::SetFmsAttached(fmsAttachedSig.value);
43 }
44
45 auto enableSig = HootReplay::GetBoolean("RobotEnable");
46 if (enableSig.status.IsOK()) {
47 wpi::sim::DriverStationSim::SetEnabled(enableSig.value);
48 }
49
50 auto robotModeSig = HootReplay::GetString("RobotMode");
51 if (robotModeSig.status.IsOK()) {
52 if (robotModeSig.value == "Autonomous") {
53 wpi::sim::DriverStationSim::SetRobotMode(HAL_ROBOT_MODE_AUTONOMOUS);
54 wpi::sim::DriverStationSim::SetEStop(false);
55 } else if (robotModeSig.value == "Teleop") {
56 wpi::sim::DriverStationSim::SetRobotMode(HAL_ROBOT_MODE_TELEOPERATED);
57 wpi::sim::DriverStationSim::SetEStop(false);
58 } else if (robotModeSig.value == "Utility") {
59 wpi::sim::DriverStationSim::SetRobotMode(HAL_ROBOT_MODE_UTILITY);
60 wpi::sim::DriverStationSim::SetEStop(false);
61 } else if (robotModeSig.value == "EStopped") {
62 wpi::sim::DriverStationSim::SetEStop(true);
63 } else {
64 wpi::sim::DriverStationSim::SetRobotMode(HAL_ROBOT_MODE_UNKNOWN);
65 wpi::sim::DriverStationSim::SetEStop(false);
66 }
67 }
68
69 auto allianceStationSig = HootReplay::GetString("AllianceStation");
70 if (allianceStationSig.status.IsOK()) {
71 if (allianceStationSig.value == "Red 1") {
72 wpi::sim::DriverStationSim::SetAllianceStationId(HAL_ALLIANCE_STATION_RED_1);
73 } else if (allianceStationSig.value == "Red 2") {
74 wpi::sim::DriverStationSim::SetAllianceStationId(HAL_ALLIANCE_STATION_RED_2);
75 } else if (allianceStationSig.value == "Red 3") {
76 wpi::sim::DriverStationSim::SetAllianceStationId(HAL_ALLIANCE_STATION_RED_3);
77 } else if (allianceStationSig.value == "Blue 1") {
78 wpi::sim::DriverStationSim::SetAllianceStationId(HAL_ALLIANCE_STATION_BLUE_1);
79 } else if (allianceStationSig.value == "Blue 2") {
80 wpi::sim::DriverStationSim::SetAllianceStationId(HAL_ALLIANCE_STATION_BLUE_2);
81 } else if (allianceStationSig.value == "Blue 3") {
82 wpi::sim::DriverStationSim::SetAllianceStationId(HAL_ALLIANCE_STATION_BLUE_3);
83 } else {
84 wpi::sim::DriverStationSim::SetAllianceStationId(HAL_ALLIANCE_STATION_UNKNOWN);
85 }
86 }
87
88 wpi::sim::DriverStationSim::NotifyNewData();
89 }
90 }
91 }}
92 {}
93
94public:
95 /**
96 * \brief Starts automatically enabling the robot in replay.
97 */
98 void Start()
99 {
100 std::lock_guard<std::mutex> lock{_lck};
101 if (_startCount < UINT32_MAX) {
102 if (_startCount++ == 0) {
103 /* start if we were previously at 0 */
104 _enableNotifier.StartPeriodic(10_ms);
105 }
106 }
107 }
108
109 /**
110 * \brief Stops automatically enabling the robot in replay.
111 * The replay enable will only be stopped when all actuators
112 * have requested to stop the replay enable.
113 */
114 void Stop()
115 {
116 std::lock_guard<std::mutex> lock{_lck};
117 if (_startCount > 0) {
118 if (--_startCount == 0) {
119 /* stop if we are now at 0 */
120 _enableNotifier.Stop();
121 }
122 }
123 }
124};
125
126}
127}
128}
static bool IsPlaying()
Gets whether hoot log replay is actively playing.
Definition HootReplay.hpp:127
static SignalMeasurement< std::string > GetString(std::string_view name)
Gets a string user signal.
Definition HootReplay.hpp:378
static SignalMeasurement< bool > GetBoolean(std::string_view name)
Gets a boolean user signal.
Definition HootReplay.hpp:317
Definition ReplayAutoEnable.hpp:18
void Start()
Starts automatically enabling the robot in replay.
Definition ReplayAutoEnable.hpp:98
void Stop()
Stops automatically enabling the robot in replay.
Definition ReplayAutoEnable.hpp:114
static ReplayAutoEnable & GetInstance()
Definition ReplayAutoEnable.hpp:20
Definition CallbackHelper.hpp:13
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14