CTRE Phoenix 6 C++ 25.0.0-beta-4
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
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 enableSig = HootReplay::GetBoolean("RobotEnable");
35 if (enableSig.status.IsOK()) {
36 frc::sim::DriverStationSim::SetEnabled(enableSig.value);
37 }
38
39 auto robotModeSig = HootReplay::GetString("RobotMode");
40 if (robotModeSig.status.IsOK()) {
41 if (robotModeSig.value == "Autonomous") {
42 frc::sim::DriverStationSim::SetAutonomous(true);
43 frc::sim::DriverStationSim::SetTest(false);
44 } else if (robotModeSig.value == "Test") {
45 frc::sim::DriverStationSim::SetAutonomous(false);
46 frc::sim::DriverStationSim::SetTest(true);
47 } else {
48 frc::sim::DriverStationSim::SetAutonomous(false);
49 frc::sim::DriverStationSim::SetTest(false);
50 }
51 }
52
53 frc::sim::DriverStationSim::NotifyNewData();
54 }
55 }}
56 {}
57
58public:
59 /**
60 * \brief Starts automatically enabling the robot in replay.
61 */
62 void Start()
63 {
64 std::lock_guard<std::mutex> lock{_lck};
65 if (_startCount < UINT32_MAX) {
66 if (_startCount++ == 0) {
67 /* start if we were previously at 0 */
68 _enableNotifier.StartPeriodic(20_ms);
69 }
70 }
71 }
72
73 /**
74 * \brief Stops automatically enabling the robot in replay.
75 * The replay enable will only be stopped when all actuators
76 * have requested to stop the replay enable.
77 */
78 void Stop()
79 {
80 std::lock_guard<std::mutex> lock{_lck};
81 if (_startCount > 0) {
82 if (--_startCount == 0) {
83 /* stop if we are now at 0 */
84 _enableNotifier.Stop();
85 }
86 }
87 }
88};
89
90}
91}
92}
static SignalData< std::string > GetString(std::string_view name)
Gets a string user signal.
Definition HootReplay.hpp:266
static bool IsPlaying()
Gets whether hoot log replay is actively playing.
Definition HootReplay.hpp:114
static SignalData< bool > GetBoolean(std::string_view name)
Gets a boolean user signal.
Definition HootReplay.hpp:206
Definition ReplayAutoEnable.hpp:18
void Start()
Starts automatically enabling the robot in replay.
Definition ReplayAutoEnable.hpp:62
void Stop()
Stops automatically enabling the robot in replay.
Definition ReplayAutoEnable.hpp:78
static ReplayAutoEnable & GetInstance()
Definition ReplayAutoEnable.hpp:20
Definition StatusCodes.h:18