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