CTRE Phoenix 6 C++ 26.0.0-beta-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 "frc/simulation/DriverStationSim.h"
11#include "frc/Notifier.h"
12#include <mutex>
13
14namespace ctre {
15namespace phoenix6 {
16namespace wpiutils {
17
18class ReplayAutoEnable final {
19public:
21 {
22 static ReplayAutoEnable *replayAutoEnable = new ReplayAutoEnable{};
23 return *replayAutoEnable;
24 }
25
26private:
27 std::mutex _lck;
28 frc::Notifier _enableNotifier;
29 uint32_t _startCount = 0;
30
32 _enableNotifier{[] {
34 auto dsAttachedSig = HootReplay::GetBoolean("DS:IsDSAttached");
35 if (dsAttachedSig.status.IsOK()) {
36 frc::sim::DriverStationSim::SetDsAttached(dsAttachedSig.value);
37 }
38
39 if (frc::sim::DriverStationSim::GetDsAttached()) {
40 auto fmsAttachedSig = HootReplay::GetBoolean("DS:IsFMSAttached");
41 if (fmsAttachedSig.status.IsOK()) {
42 frc::sim::DriverStationSim::SetFmsAttached(fmsAttachedSig.value);
43 }
44
45 auto enableSig = HootReplay::GetBoolean("RobotEnable");
46 if (enableSig.status.IsOK()) {
47 frc::sim::DriverStationSim::SetEnabled(enableSig.value);
48 }
49
50 auto robotModeSig = HootReplay::GetString("RobotMode");
51 if (robotModeSig.status.IsOK()) {
52 if (robotModeSig.value == "Autonomous") {
53 frc::sim::DriverStationSim::SetAutonomous(true);
54 frc::sim::DriverStationSim::SetTest(false);
55 frc::sim::DriverStationSim::SetEStop(false);
56 } else if (robotModeSig.value == "Test") {
57 frc::sim::DriverStationSim::SetAutonomous(false);
58 frc::sim::DriverStationSim::SetTest(true);
59 frc::sim::DriverStationSim::SetEStop(false);
60 } else if (robotModeSig.value == "EStopped") {
61 frc::sim::DriverStationSim::SetAutonomous(false);
62 frc::sim::DriverStationSim::SetTest(false);
63 frc::sim::DriverStationSim::SetEStop(true);
64 } else {
65 frc::sim::DriverStationSim::SetAutonomous(false);
66 frc::sim::DriverStationSim::SetTest(false);
67 frc::sim::DriverStationSim::SetEStop(false);
68 }
69 }
70
71 auto allianceStationSig = HootReplay::GetString("AllianceStation");
72 if (allianceStationSig.status.IsOK()) {
73 if (allianceStationSig.value == "Red 1") {
74 frc::sim::DriverStationSim::SetAllianceStationId(HAL_AllianceStationID_kRed1);
75 } else if (allianceStationSig.value == "Red 2") {
76 frc::sim::DriverStationSim::SetAllianceStationId(HAL_AllianceStationID_kRed2);
77 } else if (allianceStationSig.value == "Red 3") {
78 frc::sim::DriverStationSim::SetAllianceStationId(HAL_AllianceStationID_kRed3);
79 } else if (allianceStationSig.value == "Blue 1") {
80 frc::sim::DriverStationSim::SetAllianceStationId(HAL_AllianceStationID_kBlue1);
81 } else if (allianceStationSig.value == "Blue 2") {
82 frc::sim::DriverStationSim::SetAllianceStationId(HAL_AllianceStationID_kBlue2);
83 } else if (allianceStationSig.value == "Blue 3") {
84 frc::sim::DriverStationSim::SetAllianceStationId(HAL_AllianceStationID_kBlue3);
85 } else {
86 frc::sim::DriverStationSim::SetAllianceStationId(HAL_AllianceStationID_kUnknown);
87 }
88 }
89
90 frc::sim::DriverStationSim::NotifyNewData();
91 }
92 }
93 }}
94 {}
95
96public:
97 /**
98 * \brief Starts automatically enabling the robot in replay.
99 */
100 void Start()
101 {
102 std::lock_guard<std::mutex> lock{_lck};
103 if (_startCount < UINT32_MAX) {
104 if (_startCount++ == 0) {
105 /* start if we were previously at 0 */
106 _enableNotifier.StartPeriodic(10_ms);
107 }
108 }
109 }
110
111 /**
112 * \brief Stops automatically enabling the robot in replay.
113 * The replay enable will only be stopped when all actuators
114 * have requested to stop the replay enable.
115 */
116 void Stop()
117 {
118 std::lock_guard<std::mutex> lock{_lck};
119 if (_startCount > 0) {
120 if (--_startCount == 0) {
121 /* stop if we are now at 0 */
122 _enableNotifier.Stop();
123 }
124 }
125 }
126};
127
128}
129}
130}
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:100
void Stop()
Stops automatically enabling the robot in replay.
Definition ReplayAutoEnable.hpp:116
static ReplayAutoEnable & GetInstance()
Definition ReplayAutoEnable.hpp:20
Definition motor_constants.h:14