CTRE Phoenix 6 C++ 25.0.0-beta-4
Loading...
Searching...
No Matches
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 * \brief 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 * \brief Creates a new CAN bus with the given name, and loads an associated
75 * hoot file for replay (equivalent to HootReplay#LoadFile).
76 *
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 *
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 * \brief 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 */
115 bool IsNetworkFD() const;
116 /**
117 * \brief Gets the status of the CAN bus, including the
118 * bus utilization and the error counters.
119 *
120 * This can block for up to 0.001 seconds (1 ms).
121 *
122 * \returns Status of the CAN bus
123 */
125
126 /**
127 * \brief Gets whether the CAN bus is a CAN FD network.
128 *
129 * \deprecated The CANBus static methods are deprecated
130 * for removal in 2026. Construct a new CANBus instance
131 * and call #IsNetworkFD() instead.
132 *
133 * \param canbus Name of the CAN bus
134 * \returns True if the CAN bus is CAN FD
135 */
136 [[deprecated("The CANBus static methods are deprecated for removal in 2026."
137 "Construct a new CANBus instance and call IsNetworkFD() instead.")]]
138 static bool IsNetworkFD(std::string const &canbus)
139 {
140 return CANBus{canbus}.IsNetworkFD();
141 }
142 /**
143 * \brief Gets the status of the CAN bus, including the
144 * bus utilization and the error counters.
145 *
146 * \deprecated The CANBus static methods are deprecated
147 * for removal in 2026. Construct a new CANBus instance
148 * and call #GetStatus() instead.
149 *
150 * \param canbus Name of the CAN bus
151 * \returns Status of the CAN bus
152 */
153 [[deprecated("The CANBus static methods are deprecated for removal in 2026."
154 "Construct a new CANBus instance and call GetStatus() instead.")]]
155 static CANBusStatus GetStatus(std::string const &canbus)
156 {
157 return CANBus{canbus}.GetStatus();
158 }
159};
160
161}
162}
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
CANBusStatus GetStatus() const
Gets the status of the CAN bus, including the bus utilization and the error counters.
static bool IsNetworkFD(std::string const &canbus)
Gets whether the CAN bus is a CAN FD network.
Definition CANBus.hpp:138
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:155
constexpr CANBus(std::string_view canbus="")
Creates a new CAN bus with the given name.
Definition CANBus.hpp:69
bool IsNetworkFD() const
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
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