CTRE Phoenix 6 C++ 26.70.0-alpha-2
Loading...
Searching...
No Matches
CommonDevice.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 <functional>
12#include <memory>
13#include <wpi/units/time.hpp>
14
15namespace ctre {
16namespace phoenix6 {
17namespace hardware {
18namespace traits {
19
20/**
21 * Contains everything common between Phoenix 6 devices.
22 */
24public:
25 virtual ~CommonDevice() = default;
26
27 /**
28 * \returns The device ID of this device [0,62]
29 */
30 virtual int GetDeviceID() const = 0;
31
32 /**
33 * \returns The network this device is on
34 */
35 virtual CANBus GetNetwork() const = 0;
36
37 /**
38 * \brief Gets a number unique for this device's hardware type and ID.
39 * This number is not unique across networks.
40 *
41 * \details This can be used to easily reference hardware devices on
42 * the same network in collections such as maps.
43 *
44 * \returns Hash of this device
45 */
46 virtual uint32_t GetDeviceHash() const = 0;
47
48 /**
49 * \brief Get the latest applied control.
50 * Caller can cast this to the derived class if they know its type. Otherwise,
51 * use controls#ControlRequest#GetControlInfo to get info out of it.
52 *
53 * \details This returns a shared pointer to avoid becoming a dangling pointer
54 * due to parallel operations changing the underlying data. Make sure
55 * to save the shared_ptr to a variable before chaining function calls,
56 * otherwise the data may be freed early.
57 *
58 * \returns Latest applied control
59 */
60 virtual std::shared_ptr<controls::ControlRequest const> GetAppliedControl() const = 0;
61
62 /**
63 * \brief Get the latest applied control.
64 * Caller can cast this to the derived class if they know its type. Otherwise,
65 * use controls#ControlRequest#GetControlInfo to get info out of it.
66 *
67 * \details This returns a shared pointer to avoid becoming a dangling pointer
68 * due to parallel operations changing the underlying data. Make sure
69 * to save the shared_ptr to a variable before chaining function calls,
70 * otherwise the data may be freed early.
71 *
72 * \returns Latest applied control
73 */
74 virtual std::shared_ptr<controls::ControlRequest> GetAppliedControl() = 0;
75
76 /**
77 * \returns true if device has reset since the previous call of this routine
78 */
79 virtual bool HasResetOccurred() = 0;
80
81 /**
82 * \returns A function that checks for device resets
83 */
84 virtual std::function<bool()> GetResetOccurredChecker() const = 0;
85
86 /**
87 * \brief Returns whether the device is still connected to the robot.
88 * This is equivalent to refreshing and checking the latency of the
89 * Version status signal.
90 *
91 * \param maxLatencySeconds The maximum latency of the Version status signal
92 * before the device is reported as disconnected
93 * \returns true if the device is connected
94 */
95 virtual bool IsConnected(wpi::units::second_t maxLatencySeconds = 500_ms) = 0;
96
97 /**
98 * \brief Gets a function that checks whether the device is still connected to
99 * the robot. This is equivalent to refreshing and checking the latency of a
100 * copy of the Version status signal.
101 *
102 * \param maxLatencySeconds The maximum latency of the Version status signal
103 * before the device is reported as disconnected
104 * \returns A function that checks whether the device is still connected
105 */
106 virtual std::function<bool()> GetIsConnectedChecker(wpi::units::second_t maxLatencySeconds = 500_ms) const = 0;
107
108 /**
109 * \brief Optimizes the device's bus utilization by reducing the update frequencies of its status signals.
110 *
111 * All status signals that have not been explicitly given an update frequency using
112 * BaseStatusSignal#SetUpdateFrequency will be slowed down. Note that if other status
113 * signals in the same status frame have been given an update frequency, the update
114 * frequency will be honored for the entire frame.
115 *
116 * This function only needs to be called once on this device in the robot program. Additionally, this
117 * method does not necessarily need to be called after setting the update frequencies of other signals.
118 *
119 * To restore the default status update frequencies, call ResetSignalFrequencies.
120 * Alternatively, remove this method call, redeploy the robot application, and power-cycle
121 * the device on the bus. The user can also override individual status update frequencies
122 * using BaseStatusSignal#SetUpdateFrequency.
123 *
124 * \param optimizedFreqHz The update frequency to apply to the optimized status signals. A frequency
125 * of 0 Hz will turn off the signals. Otherwise, the minimum supported signal
126 * frequency is 4 Hz (default).
127 * \param timeoutSeconds Maximum amount of time to wait for each status frame when performing the action
128 * \returns Status code of the first failed update frequency set call, or OK if all succeeded
129 */
130 virtual ctre::phoenix::StatusCode OptimizeBusUtilization(wpi::units::hertz_t optimizedFreqHz = 4_Hz, wpi::units::second_t timeoutSeconds = 100_ms) = 0;
131
132 /**
133 * \brief Resets the update frequencies of all the device's status signals to the defaults.
134 *
135 * This restores the default update frequency of all status signals, including status signals
136 * explicitly given an update frequency using BaseStatusSignal#SetUpdateFrequency and status
137 * signals optimized out using OptimizeBusUtilization.
138 *
139 * \param timeoutSeconds Maximum amount of time to wait for each status frame when performing the action
140 * \returns Status code of the first failed update frequency set call, or OK if all succeeded
141 */
142 virtual ctre::phoenix::StatusCode ResetSignalFrequencies(wpi::units::second_t timeoutSeconds = 100_ms) = 0;
143
144 /**
145 * \returns String representation of this device
146 */
147 virtual std::string ToString() const = 0;
148};
149
150}
151}
152}
153}
Class for getting information about an available CAN bus.
Definition CANBus.hpp:142
Contains everything common between Phoenix 6 devices.
Definition CommonDevice.hpp:23
virtual std::function< bool()> GetIsConnectedChecker(wpi::units::second_t maxLatencySeconds=500_ms) const =0
Gets a function that checks whether the device is still connected to the robot.
virtual uint32_t GetDeviceHash() const =0
Gets a number unique for this device's hardware type and ID.
virtual ctre::phoenix::StatusCode OptimizeBusUtilization(wpi::units::hertz_t optimizedFreqHz=4_Hz, wpi::units::second_t timeoutSeconds=100_ms)=0
Optimizes the device's bus utilization by reducing the update frequencies of its status signals.
virtual std::shared_ptr< controls::ControlRequest const > GetAppliedControl() const =0
Get the latest applied control.
virtual ctre::phoenix::StatusCode ResetSignalFrequencies(wpi::units::second_t timeoutSeconds=100_ms)=0
Resets the update frequencies of all the device's status signals to the defaults.
virtual bool IsConnected(wpi::units::second_t maxLatencySeconds=500_ms)=0
Returns whether the device is still connected to the robot.
virtual std::function< bool()> GetResetOccurredChecker() const =0
virtual std::shared_ptr< controls::ControlRequest > GetAppliedControl()=0
Get the latest applied control.
virtual std::string ToString() const =0
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
Definition CommonTalonWithExternalMotor.hpp:20
Definition ExternalFeedbackConfigs.hpp:17
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14