CTRE Phoenix 6 C++ 24.50.0-alpha-2
CANBus.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 <string>
12
13namespace ctre {
14namespace phoenix6 {
15
16/**
17 * \brief Class for getting information about an available CAN bus.
18 */
19class CANBus {
20public:
21 /**
22 * \brief Contains status information about a CAN bus.
23 */
24 struct CANBusStatus {
25 /**
26 * \brief Status code response of getting the data
27 */
29
30 /**
31 * \brief CAN bus utilization, from 0.0 to 1.0
32 */
34 /**
35 * \brief Bus off count
36 */
37 uint32_t BusOffCount;
38 /**
39 * \brief Transmit buffer full count
40 */
41 uint32_t TxFullCount;
42 /**
43 * \brief Receive Error Counter (REC)
44 */
45 uint32_t REC;
46 /**
47 * \brief Transmit Error Counter (TEC)
48 */
49 uint32_t TEC;
50 };
51
52private:
53 std::string_view _name;
54
55public:
56 /**
57 * Creates a new CAN bus with the given name.
58 *
59 * \param canbus Name of the CAN bus. Possible CAN bus strings are:
60 * - "rio" for the native roboRIO CAN bus
61 * - CANivore name or serial number
62 * - SocketCAN interface (non-FRC Linux only)
63 * - "*" for any CANivore seen by the program
64 * - empty string (default) to select the default for the system:
65 * - "rio" on roboRIO
66 * - "can0" on Linux
67 * - "*" on Windows
68 */
69 constexpr CANBus(std::string_view canbus = "") :
70 _name{canbus}
71 {}
72
73 /**
74 * Creates a new CAN bus with the given name, and loads an associated
75 * hoot file for replay (equivalent to HootReplay#LoadFile).
76 * <p>
77 * Only one hoot log may be replayed at a time. As a result, only one
78 * CAN bus should be constructed with a hoot file.
79 * <p>
80 * When using relative paths, the file path is typically relative
81 * to the top-level folder of the robot project.
82 *
83 * \param canbus Name of the CAN bus. Possible CAN bus strings are:
84 * - "rio" for the native roboRIO CAN bus
85 * - CANivore name or serial number
86 * - SocketCAN interface (non-FRC Linux only)
87 * - "*" for any CANivore seen by the program
88 * - empty string (default) to select the default for the system:
89 * - "rio" on roboRIO
90 * - "can0" on Linux
91 * - "*" on Windows
92 * \param hootFilepath Path and name of the hoot file to load
93 */
94 CANBus(std::string_view canbus, char const *hootFilepath) :
95 CANBus{canbus}
96 {
97 HootReplay::LoadFile(hootFilepath);
98 }
99
100 /**
101 * Get the name used to construct this CAN bus.
102 *
103 * \returns Name of the CAN bus
104 */
105 constexpr std::string_view GetName() const
106 {
107 return _name;
108 }
109
110 /**
111 * \brief Gets whether the CAN bus is a CAN FD network.
112 *
113 * \returns True if the CAN bus is CAN FD
114 */
116 /**
117 * \brief Gets the status of the CAN bus, including the
118 * bus utilization and the error counters.
119 *
120 * \returns Status of the CAN bus
121 */
123
124 /**
125 * \brief Gets whether the CAN bus is a CAN FD network.
126 *
127 * \deprecated The CANBus static methods are deprecated
128 * for removal in 2026. Construct a new CANBus instance
129 * and call #IsNetworkFD() instead.
130 *
131 * \param canbus Name of the CAN bus
132 * \returns True if the CAN bus is CAN FD
133 */
134 [[deprecated("The CANBus static methods are deprecated for removal in 2026."
135 "Construct a new CANBus instance and call IsNetworkFD() instead.")]]
136 static bool IsNetworkFD(std::string const &canbus)
137 {
138 return CANBus{canbus}.IsNetworkFD();
139 }
140 /**
141 * \brief Gets the status of the CAN bus, including the
142 * bus utilization and the error counters.
143 *
144 * \deprecated The CANBus static methods are deprecated
145 * for removal in 2026. Construct a new CANBus instance
146 * and call #GetStatus() instead.
147 *
148 * \param canbus Name of the CAN bus
149 * \returns Status of the CAN bus
150 */
151 [[deprecated("The CANBus static methods are deprecated for removal in 2026."
152 "Construct a new CANBus instance and call GetStatus() instead.")]]
153 static CANBusStatus GetStatus(std::string const &canbus)
154 {
155 return CANBus{canbus}.GetStatus();
156 }
157};
158
159}
160}
Class for getting information about an available CAN bus.
Definition: CANBus.hpp:19
CANBus(std::string_view canbus, char const *hootFilepath)
Creates a new CAN bus with the given name, and loads an associated hoot file for replay (equivalent t...
Definition: CANBus.hpp:94
static bool IsNetworkFD(std::string const &canbus)
Gets whether the CAN bus is a CAN FD network.
Definition: CANBus.hpp:136
static CANBusStatus GetStatus(std::string const &canbus)
Gets the status of the CAN bus, including the bus utilization and the error counters.
Definition: CANBus.hpp:153
CANBusStatus GetStatus()
Gets the status of the CAN bus, including the bus utilization and the error counters.
constexpr CANBus(std::string_view canbus="")
Creates a new CAN bus with the given name.
Definition: CANBus.hpp:69
bool IsNetworkFD()
Gets whether the CAN bus is a CAN FD network.
constexpr std::string_view GetName() const
Get the name used to construct this CAN bus.
Definition: CANBus.hpp:105
static ctre::phoenix::StatusCode LoadFile(char const *filepath)
Loads the given file and starts signal log replay.
Status codes reported by APIs, including OK, warnings, and errors.
Definition: StatusCodes.h:27
Represents the state of one swerve module.
Definition: StatusCodes.h:18
Contains status information about a CAN bus.
Definition: CANBus.hpp:24
uint32_t TxFullCount
Transmit buffer full count.
Definition: CANBus.hpp:41
ctre::phoenix::StatusCode Status
Status code response of getting the data.
Definition: CANBus.hpp:28
float BusUtilization
CAN bus utilization, from 0.0 to 1.0.
Definition: CANBus.hpp:33
uint32_t BusOffCount
Bus off count.
Definition: CANBus.hpp:37
uint32_t REC
Receive Error Counter (REC)
Definition: CANBus.hpp:45
uint32_t TEC
Transmit Error Counter (TEC)
Definition: CANBus.hpp:49