CTRE Phoenix 6 C++ 26.50.0-alpha-1
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 * For the native Systemcore CAN buses, use #Systemcore instead.
60 *
61 * For the Motioncore CAN buses, use #Motioncore instead.
62 *
63 * \param canbus Name of the CAN bus. Possible CAN bus strings are:
64 * - "can_s0" to "can_s24" for the native Systemcore/Motioncore CAN buses
65 * - CANivore name or serial number
66 * - SocketCAN interface (non-FRC Linux only)
67 * - "*" for any CANivore seen by the program
68 * - empty string (default) to select the default for the system:
69 * - "can_s1" on Systemcore
70 * - "can0" on Linux
71 * - "*" on Windows
72 */
73 constexpr CANBus(std::string_view canbus = "") :
74 _name{canbus}
75 {}
76
77 /**
78 * \brief Creates a new CAN bus with the given name, and loads an associated
79 * hoot file for replay (equivalent to HootReplay#LoadFile).
80 *
81 * Only one hoot log may be replayed at a time. As a result, only one
82 * CAN bus should be constructed with a hoot file.
83 *
84 * When using relative paths, the file path is typically relative
85 * to the top-level folder of the robot project.
86 *
87 * For the native Systemcore CAN buses, use #Systemcore instead.
88 *
89 * For the Motioncore CAN buses, use #Motioncore instead.
90 *
91 * \param canbus Name of the CAN bus. Possible CAN bus strings are:
92 * - "can_s0" to "can_s24" for the native Systemcore/Motioncore CAN buses
93 * - CANivore name or serial number
94 * - SocketCAN interface (non-FRC Linux only)
95 * - "*" for any CANivore seen by the program
96 * - empty string (default) to select the default for the system:
97 * - "can_s1" on Systemcore
98 * - "can0" on Linux
99 * - "*" on Windows
100 * \param hootFilepath Path and name of the hoot file to load
101 */
102 CANBus(std::string_view canbus, char const *hootFilepath) :
103 CANBus{canbus}
104 {
105 HootReplay::LoadFile(hootFilepath);
106 }
107
108 /**
109 * \brief Creates a new CAN bus for one of the native Systemcore buses.
110 *
111 * For the Motioncore CAN buses, use #Motioncore instead.
112 *
113 * \param canbus Index of the Systemcore bus, from 0-4
114 * \returns A native Systemcore CAN bus
115 */
116 static constexpr CANBus Systemcore(int canbus)
117 {
118 if (canbus < 0 || canbus > 4) [[unlikely]] {
119 throw std::invalid_argument{"Systemcore CAN bus index must be within 0-4."};
120 }
121 switch (canbus) {
122 case 0: return {"can_s0"};
123 case 1: return {"can_s1"};
124 case 2: return {"can_s2"};
125 case 3: return {"can_s3"};
126 case 4: return {"can_s4"};
127 default: return {};
128 }
129 }
130
131 /**
132 * \brief Creates a new CAN bus for one of the native Systemcore buses,
133 * and loads an associated hoot file for replay (equivalent to
134 * HootReplay#LoadFile).
135 *
136 * Only one hoot log may be replayed at a time. As a result, only one
137 * CAN bus should be constructed with a hoot file.
138 *
139 * When using relative paths, the file path is typically relative
140 * to the top-level folder of the robot project.
141 *
142 * For the Motioncore CAN buses, use #Motioncore instead.
143 *
144 * \param canbus Index of the Systemcore bus, from 0-4
145 * \param hootFilepath Path and name of the hoot file to load
146 * \returns A native Systemcore CAN bus
147 */
148 static CANBus Systemcore(int canbus, char const *hootFilepath)
149 {
150 auto retval = Systemcore(canbus);
151 HootReplay::LoadFile(hootFilepath);
152 return retval;
153 }
154
155 /**
156 * \brief Creates a new CAN bus for one of the Motioncore ports.
157 *
158 * \param port The Motioncore port, from 0-19
159 * \returns A Motioncore CAN bus
160 */
161 static constexpr CANBus Motioncore(int port)
162 {
163 if (port < 0 || port > 19) [[unlikely]] {
164 throw std::invalid_argument{"Motioncore port must be within 0-19."};
165 }
166 switch (port) {
167 case 0: return {"can_s5"};
168 case 1: return {"can_s6"};
169 case 2: return {"can_s7"};
170 case 3: return {"can_s8"};
171 case 4: return {"can_s9"};
172 case 5: return {"can_s10"};
173 case 6: return {"can_s11"};
174 case 7: return {"can_s12"};
175 case 8: return {"can_s13"};
176 case 9: return {"can_s14"};
177 case 10: return {"can_s15"};
178 case 11: return {"can_s16"};
179 case 12: return {"can_s17"};
180 case 13: return {"can_s18"};
181 case 14: return {"can_s19"};
182 case 15: return {"can_s20"};
183 case 16: return {"can_s21"};
184 case 17: return {"can_s22"};
185 case 18: return {"can_s23"};
186 case 19: return {"can_s24"};
187 default: return {};
188 }
189 }
190
191 /**
192 * \brief Creates a new CAN bus for one of the Motioncore ports,
193 * and loads an associated hoot file for replay (equivalent to
194 * HootReplay#LoadFile).
195 *
196 * Only one hoot log may be replayed at a time. As a result, only one
197 * CAN bus should be constructed with a hoot file.
198 *
199 * When using relative paths, the file path is typically relative
200 * to the top-level folder of the robot project.
201 *
202 * \param port The Motioncore port, from 0-19
203 * \param hootFilepath Path and name of the hoot file to load
204 * \returns A Motioncore CAN bus
205 */
206 static CANBus Motioncore(int port, char const *hootFilepath)
207 {
208 auto retval = Motioncore(port);
209 HootReplay::LoadFile(hootFilepath);
210 return retval;
211 }
212
213 /**
214 * \brief Get the name used to construct this CAN bus.
215 *
216 * \returns Name of the CAN bus
217 */
218 constexpr std::string_view GetName() const
219 {
220 return _name;
221 }
222
223 /**
224 * \brief Gets whether the CAN bus is a CAN FD network.
225 *
226 * \returns True if the CAN bus is CAN FD
227 */
228 bool IsNetworkFD() const;
229 /**
230 * \brief Gets the status of the CAN bus, including the
231 * bus utilization and the error counters.
232 *
233 * This can block for up to 0.001 seconds (1 ms).
234 *
235 * \returns Status of the CAN bus
236 */
238
239 friend std::ostream &operator<<(std::ostream &os, CANBus const &canbus);
240
241 constexpr friend bool operator==(CANBus const &, CANBus const &) = default;
242 constexpr friend auto operator<=>(CANBus const &, CANBus const &) = default;
243};
244
245}
246}
247
248template <>
249struct std::hash<ctre::phoenix6::CANBus> {
250 size_t operator()(ctre::phoenix6::CANBus const &canbus) const noexcept
251 {
252 return std::hash<std::string_view>{}(canbus.GetName());
253 }
254};
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:102
CANBusStatus GetStatus() const
Gets the status of the CAN bus, including the bus utilization and the error counters.
static constexpr CANBus Motioncore(int port)
Creates a new CAN bus for one of the Motioncore ports.
Definition CANBus.hpp:161
static constexpr CANBus Systemcore(int canbus)
Creates a new CAN bus for one of the native Systemcore buses.
Definition CANBus.hpp:116
constexpr friend bool operator==(CANBus const &, CANBus const &)=default
static CANBus Systemcore(int canbus, char const *hootFilepath)
Creates a new CAN bus for one of the native Systemcore buses, and loads an associated hoot file for r...
Definition CANBus.hpp:148
constexpr CANBus(std::string_view canbus="")
Creates a new CAN bus with the given name.
Definition CANBus.hpp:73
friend std::ostream & operator<<(std::ostream &os, CANBus const &canbus)
constexpr friend auto operator<=>(CANBus const &, CANBus const &)=default
static CANBus Motioncore(int port, char const *hootFilepath)
Creates a new CAN bus for one of the Motioncore ports, and loads an associated hoot file for replay (...
Definition CANBus.hpp:206
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:218
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:28
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14
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
size_t operator()(ctre::phoenix6::CANBus const &canbus) const noexcept
Definition CANBus.hpp:250