CTRE Phoenix 6 C++ 26.50.0-alpha-1
Loading...
Searching...
No Matches
AutoFeedEnable.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
11#include <wpi/driverstation/MatchState.hpp>
12#include <wpi/driverstation/RobotState.hpp>
13#include <wpi/system/Notifier.hpp>
14#include <mutex>
15
16namespace ctre {
17namespace phoenix6 {
18namespace wpiutils {
19
20class AutoFeedEnable final {
21public:
22 static AutoFeedEnable &GetInstance()
23 {
24 static AutoFeedEnable &autoFeedEnable = *new AutoFeedEnable{};
25 return autoFeedEnable;
26 }
27
28private:
29 std::mutex _lck;
30 wpi::Notifier _enableNotifier;
31 uint32_t _startCount = 0;
32
34 _enableNotifier{[] {
35 std::string_view robotMode;
36 if (wpi::RobotState::IsEnabled()) {
38
39 switch (wpi::RobotState::GetRobotMode()) {
40 case wpi::RobotMode::UNKNOWN:
41 default:
42 robotMode = "Unknown";
43 break;
44 case wpi::RobotMode::AUTONOMOUS:
45 robotMode = "Autonomous";
46 break;
47 case wpi::RobotMode::TELEOPERATED:
48 robotMode = "Teleop";
49 break;
50 case wpi::RobotMode::UTILITY:
51 robotMode = "Utility";
52 break;
53 }
54 } else {
56
57 if (wpi::RobotState::IsEStopped()) {
58 robotMode = "EStopped";
59 } else {
60 robotMode = "Disabled";
61 }
62 }
63
64 std::string_view allianceStation{};
65 {
66 auto const alliance = wpi::MatchState::GetAlliance();
67 auto const location = wpi::MatchState::GetLocation();
68 if (alliance && location) {
69 switch (*alliance) {
70 case wpi::Alliance::RED:
71 switch (*location) {
72 case 1:
73 allianceStation = "Red 1";
74 break;
75 case 2:
76 allianceStation = "Red 2";
77 break;
78 case 3:
79 allianceStation = "Red 3";
80 break;
81 default:
82 break;
83 }
84 break;
85 case wpi::Alliance::BLUE:
86 switch (*location) {
87 case 1:
88 allianceStation = "Blue 1";
89 break;
90 case 2:
91 allianceStation = "Blue 2";
92 break;
93 case 3:
94 allianceStation = "Blue 3";
95 break;
96 default:
97 break;
98 }
99 break;
100 default:
101 break;
102 }
103 }
104 }
105
106 SignalLogger::WriteString("RobotMode", robotMode);
107 SignalLogger::WriteString("AllianceStation", allianceStation);
108 SignalLogger::WriteBoolean("DS:IsDSAttached", wpi::RobotState::IsDSAttached());
109 SignalLogger::WriteBoolean("DS:IsFMSAttached", wpi::RobotState::IsFMSAttached());
110 }}
111 {}
112
113public:
114 /**
115 * \brief Starts feeding the enable signal to CTRE actuators.
116 */
117 void Start()
118 {
119 std::lock_guard<std::mutex> lock{_lck};
120 if (_startCount < UINT32_MAX) {
121 if (_startCount++ == 0) {
122 /* start if we were previously at 0 */
123 _enableNotifier.StartPeriodic(10_ms);
124 }
125 }
126 }
127
128 /**
129 * \brief Stops feeding the enable signal to CTRE actuators.
130 * The enable signal will only be stopped when all actuators
131 * have requested to stop the enable signal.
132 */
133 void Stop()
134 {
135 std::lock_guard<std::mutex> lock{_lck};
136 if (_startCount > 0) {
137 if (--_startCount == 0) {
138 /* stop if we are now at 0 */
139 _enableNotifier.Stop();
140 }
141 }
142 }
143};
144
145}
146}
147}
static ctre::phoenix::StatusCode WriteBoolean(std::string_view name, bool value, wpi::units::second_t latencySeconds=0_s)
Writes the boolean to the log file.
Definition SignalLogger.hpp:333
static ctre::phoenix::StatusCode WriteString(std::string_view name, std::string_view value, wpi::units::second_t latencySeconds=0_s)
Writes the string to the log file.
Definition SignalLogger.hpp:388
Definition AutoFeedEnable.hpp:20
void Start()
Starts feeding the enable signal to CTRE actuators.
Definition AutoFeedEnable.hpp:117
static AutoFeedEnable & GetInstance()
Definition AutoFeedEnable.hpp:22
void Stop()
Stops feeding the enable signal to CTRE actuators.
Definition AutoFeedEnable.hpp:133
Definition CallbackHelper.hpp:13
Definition ExternalFeedbackConfigs.hpp:16
CTREXPORT void FeedEnable(int timeoutMs)
Feed the robot enable.
Definition motor_constants.h:14