CTRE Phoenix 6 C++ 25.0.0-beta-4
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
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 {
46 robotMode = "Disabled";
47 }
48 SignalLogger::WriteString("RobotMode", robotMode);
49 }}
50 {}
51
52public:
53 /**
54 * \brief Starts feeding the enable signal to CTRE actuators.
55 */
56 void Start()
57 {
58 std::lock_guard<std::mutex> lock{_lck};
59 if (_startCount < UINT32_MAX) {
60 if (_startCount++ == 0) {
61 /* start if we were previously at 0 */
62 _enableNotifier.StartPeriodic(20_ms);
63 }
64 }
65 }
66
67 /**
68 * \brief Stops feeding the enable signal to CTRE actuators.
69 * The enable signal will only be stopped when all actuators
70 * have requested to stop the enable signal.
71 */
72 void Stop()
73 {
74 std::lock_guard<std::mutex> lock{_lck};
75 if (_startCount > 0) {
76 if (--_startCount == 0) {
77 /* stop if we are now at 0 */
78 _enableNotifier.Stop();
79 }
80 }
81 }
82};
83
84}
85}
86}
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:151
Definition AutoFeedEnable.hpp:19
void Start()
Starts feeding the enable signal to CTRE actuators.
Definition AutoFeedEnable.hpp:56
static AutoFeedEnable & GetInstance()
Definition AutoFeedEnable.hpp:21
void Stop()
Stops feeding the enable signal to CTRE actuators.
Definition AutoFeedEnable.hpp:72
CTREXPORT void FeedEnable(int timeoutMs)
Feed the robot enable.
Definition StatusCodes.h:18