CTRE Phoenix 6 C++ 26.0.0-beta-1
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 <units/time.h>
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 uint64_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(units::second_t maxLatencySeconds = 500_ms) = 0;
96
97 /**
98 * \brief Optimizes the device's bus utilization by reducing the update frequencies of its status signals.
99 *
100 * All status signals that have not been explicitly given an update frequency using
101 * BaseStatusSignal#SetUpdateFrequency will be slowed down. Note that if other status
102 * signals in the same status frame have been given an update frequency, the update
103 * frequency will be honored for the entire frame.
104 *
105 * This function only needs to be called once on this device in the robot program. Additionally, this
106 * method does not necessarily need to be called after setting the update frequencies of other signals.
107 *
108 * To restore the default status update frequencies, call ResetSignalFrequencies.
109 * Alternatively, remove this method call, redeploy the robot application, and power-cycle
110 * the device on the bus. The user can also override individual status update frequencies
111 * using BaseStatusSignal#SetUpdateFrequency.
112 *
113 * \param optimizedFreqHz The update frequency to apply to the optimized status signals. A frequency
114 * of 0 Hz will turn off the signals. Otherwise, the minimum supported signal
115 * frequency is 4 Hz (default).
116 * \param timeoutSeconds Maximum amount of time to wait for each status frame when performing the action
117 * \returns Status code of the first failed update frequency set call, or OK if all succeeded
118 */
119 virtual ctre::phoenix::StatusCode OptimizeBusUtilization(units::frequency::hertz_t optimizedFreqHz = 4_Hz, units::time::second_t timeoutSeconds = 100_ms) = 0;
120
121 /**
122 * \brief Resets the update frequencies of all the device's status signals to the defaults.
123 *
124 * This restores the default update frequency of all status signals, including status signals
125 * explicitly given an update frequency using BaseStatusSignal#SetUpdateFrequency and status
126 * signals optimized out using OptimizeBusUtilization.
127 *
128 * \param timeoutSeconds Maximum amount of time to wait for each status frame when performing the action
129 * \returns Status code of the first failed update frequency set call, or OK if all succeeded
130 */
131 virtual ctre::phoenix::StatusCode ResetSignalFrequencies(units::time::second_t timeoutSeconds = 100_ms) = 0;
132};
133
134}
135}
136}
137}
Class for getting information about an available CAN bus.
Definition CANBus.hpp:19
Contains everything common between Phoenix 6 devices.
Definition CommonDevice.hpp:23
virtual ctre::phoenix::StatusCode OptimizeBusUtilization(units::frequency::hertz_t optimizedFreqHz=4_Hz, units::time::second_t timeoutSeconds=100_ms)=0
Optimizes the device's bus utilization by reducing the update frequencies of its status signals.
virtual ctre::phoenix::StatusCode ResetSignalFrequencies(units::time::second_t timeoutSeconds=100_ms)=0
Resets the update frequencies of all the device's status signals to the defaults.
virtual uint64_t GetDeviceHash() const =0
Gets a number unique for this device's hardware type and ID.
virtual bool IsConnected(units::second_t maxLatencySeconds=500_ms)=0
Returns whether the device is still connected to the robot.
virtual std::shared_ptr< controls::ControlRequest const > GetAppliedControl() const =0
Get the latest applied control.
virtual std::function< bool()> GetResetOccurredChecker() const =0
virtual std::shared_ptr< controls::ControlRequest > GetAppliedControl()=0
Get the latest applied control.
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
Definition motor_constants.h:14