CTRE Phoenix 6 C++ 26.70.0-alpha-2
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
10#include <wpi/hardware/bus/CANPort.hpp>
11#include <wpi/units/dimensionless.hpp>
12#include <iosfwd>
13#include <stdexcept>
14#include <string>
15
16namespace ctre {
17namespace phoenix6 {
18
19/**
20 * \brief Possible states of the CAN bus.
21 */
22struct CANState {
23 /** \brief Unique integer value of this CANState. */
24 int value;
25
26 /**
27 * \brief The controller is in normal operation.
28 */
29 static constexpr int ErrorActive = 0;
30 /**
31 * \brief The controller has detected errors and is approaching the Error Passive state.
32 */
33 static constexpr int ErrorWarning = 1;
34 /**
35 * \brief The controller is in the "Error Passive" state and has detected many errors.
36 */
37 static constexpr int ErrorPassive = 2;
38 /**
39 * \brief The controller is in the "Bus Off" state after detecting too many errors.
40 */
41 static constexpr int BusOff = 3;
42 /**
43 * \brief The controller has been manually stopped.
44 */
45 static constexpr int Stopped = 4;
46 /**
47 * \brief The controller has entered low-power sleep mode.
48 */
49 static constexpr int Sleeping = 5;
50
51 constexpr CANState(int value) :
53 {}
54
55 constexpr CANState() :
56 value{-1}
57 {}
58
59 constexpr bool operator==(CANState other) const
60 {
61 return value == other.value;
62 }
63 constexpr auto operator<=>(CANState other) const
64 {
65 return value <=> other.value;
66 }
67
68 /**
69 * \brief Gets the string representation of this enum
70 *
71 * \returns String representation of this enum
72 */
73 constexpr std::string_view ToString() const
74 {
75 switch (value) {
76 case CANState::ErrorActive: return "ErrorActive";
77 case CANState::ErrorWarning: return "ErrorWarning";
78 case CANState::ErrorPassive: return "ErrorPassive";
79 case CANState::BusOff: return "BusOff";
80 case CANState::Stopped: return "Stopped";
81 case CANState::Sleeping: return "Sleeping";
82 default: return "Invalid Value";
83 }
84 }
85
86 friend std::ostream &operator<<(std::ostream &os, CANState state);
87};
88
89/**
90 * \brief Contains status information about a CAN bus.
91 */
93 /**
94 * \brief Status code response of getting the data
95 */
97
98 /**
99 * \brief CAN bus utilization, from 0.0 to 1.0
100 */
101 wpi::units::unit_t<wpi::units::scalar, float> BusUtilization;
102 /**
103 * \brief Bus error count
104 */
106 /**
107 * \brief Arbitration lost count
108 */
110 /**
111 * \brief Restart count
112 */
113 uint32_t RestartCount;
114 /**
115 * \brief Bus off count
116 */
117 uint32_t BusOffCount;
118 /**
119 * \brief Transmit buffer full count
120 */
121 uint32_t TxFullCount;
122 /**
123 * \brief Receive Error Counter (REC)
124 */
125 uint16_t REC;
126 /**
127 * \brief Transmit Error Counter (TEC)
128 */
129 uint16_t TEC;
130 /**
131 * \brief CAN state
132 */
134
135 friend std::ostream &operator<<(std::ostream &os, CANBusStatus const &status);
136 std::string ToString() const;
137};
138
139/**
140 * \brief Class for getting information about an available CAN bus.
141 */
142class CANBus {
143private:
144 std::string_view _name;
145
146 static constexpr std::string_view GetCANPortName(wpi::CANPort canPort)
147 {
148 switch (canPort) {
149 case wpi::CANPort::CAN_S0: return "can_s0";
150 case wpi::CANPort::CAN_S1: return "can_s1";
151 case wpi::CANPort::CAN_S2: return "can_s2";
152 case wpi::CANPort::CAN_S3: return "can_s3";
153 case wpi::CANPort::CAN_S4: return "can_s4";
154 case wpi::CANPort::CAN_D0: return "can_d0";
155 case wpi::CANPort::CAN_D1: return "can_d1";
156 case wpi::CANPort::CAN_D2: return "can_d2";
157 case wpi::CANPort::CAN_D3: return "can_d3";
158 case wpi::CANPort::CAN_D4: return "can_d4";
159 case wpi::CANPort::CAN_D5: return "can_d5";
160 case wpi::CANPort::CAN_D6: return "can_d6";
161 case wpi::CANPort::CAN_D7: return "can_d7";
162 case wpi::CANPort::CAN_D8: return "can_d8";
163 case wpi::CANPort::CAN_D9: return "can_d9";
164 case wpi::CANPort::CAN_D10: return "can_d10";
165 case wpi::CANPort::CAN_D11: return "can_d11";
166 case wpi::CANPort::CAN_D12: return "can_d12";
167 case wpi::CANPort::CAN_D13: return "can_d13";
168 case wpi::CANPort::CAN_D14: return "can_d14";
169 case wpi::CANPort::CAN_D15: return "can_d15";
170 case wpi::CANPort::CAN_D16: return "can_d16";
171 case wpi::CANPort::CAN_D17: return "can_d17";
172 case wpi::CANPort::CAN_D18: return "can_d18";
173 case wpi::CANPort::CAN_D19: return "can_d19";
174 default: throw std::invalid_argument{"Unexpected Systemcore CAN port " + std::to_string((int)canPort)};
175 }
176 }
177
178public:
179 /**
180 * \brief Creates a new CAN bus with the given name.
181 *
182 * For the native Systemcore/Motioncore CAN buses, use #CANBus(wpi::CANPort) instead.
183 *
184 * \param canbus Name of the CAN bus. Possible CAN bus strings are:
185 * - "can_s0" to "can_s4" for the native Systemcore CAN buses
186 * - "can_d0" to "can_d19" for the Motioncore CAN buses
187 * - CANivore name or serial number
188 * - SocketCAN interface (non-FRC Linux only)
189 * - "*" for any CANivore seen by the program
190 * - empty string (default) to select the default for the system:
191 * - "can_s2" on Systemcore
192 * - "can0" on Linux
193 * - "*" on Windows
194 */
195 constexpr CANBus(std::string_view canbus = "") :
196 _name{canbus}
197 {}
198
199 /**
200 * \brief Creates a new CAN bus for one of the native Systemcore/Motioncore buses.
201 *
202 * For the CANivore and non-FRC SocketCAN interfaces, use #CANBus(std::string_view)
203 * instead.
204 *
205 * \param canPort The Systemcore/Motioncore CAN port
206 */
207 constexpr CANBus(wpi::CANPort canPort) :
208 CANBus{GetCANPortName(canPort)}
209 {}
210
211 /**
212 * \brief Get the name used to construct this CAN bus.
213 *
214 * \returns Name of the CAN bus
215 */
216 constexpr std::string_view GetName() const
217 {
218 return _name;
219 }
220
221 /**
222 * \brief Gets whether the CAN bus is a CAN FD network.
223 *
224 * \returns True if the CAN bus is CAN FD
225 */
226 bool IsNetworkFD() const;
227 /**
228 * \brief Gets the status of the CAN bus, including the
229 * bus utilization and the error counters.
230 *
231 * This can block for up to 0.001 seconds (1 ms).
232 *
233 * \returns Status of the CAN bus
234 */
236
237 friend std::ostream &operator<<(std::ostream &os, CANBus const &canbus);
238
239 constexpr friend bool operator==(CANBus const &, CANBus const &) = default;
240 constexpr friend auto operator<=>(CANBus const &, CANBus const &) = default;
241};
242
243}
244}
245
246template <>
247struct std::hash<ctre::phoenix6::CANBus> {
248 size_t operator()(ctre::phoenix6::CANBus const &canbus) const noexcept
249 {
250 return std::hash<std::string_view>{}(canbus.GetName());
251 }
252};
253
254#include <wpi/util/struct/Struct.hpp>
255
256/** \brief WPILib Struct implementation for ctre#phoenix6#CANBusStatus. */
257template <>
258struct wpi::util::Struct<ctre::phoenix6::CANBusStatus> final {
259 static constexpr std::string_view GetTypeName() { return "ctre_CANBusStatus"; }
260 static constexpr size_t GetSize()
261 {
262 return wpi::util::GetStructSize<int32_t>() +
263 wpi::util::GetStructSize<float>() +
264 5 * wpi::util::GetStructSize<uint32_t>() +
265 2 * wpi::util::GetStructSize<uint16_t>() +
266 wpi::util::GetStructSize<int32_t>();
267 }
268 static constexpr std::string_view GetSchema()
269 {
270 return "int32 Status;"
271 "float BusUtilization;"
272 "uint32 BusErrorCount;"
273 "uint32 ArbitrationLostCount;"
274 "uint32 RestartCount;"
275 "uint32 BusOffCount;"
276 "uint32 TxFullCount;"
277 "uint16 REC;"
278 "uint16 TEC;"
279 "enum{ErrorActive=0,ErrorWarning=1,ErrorPassive=2,BusOff=3,Stopped=4,Sleeping=5} int32 State";
280 }
281
282 static ctre::phoenix6::CANBusStatus Unpack(std::span<uint8_t const> data);
283 static void Pack(std::span<uint8_t> data, ctre::phoenix6::CANBusStatus const &value);
284};
285
286static_assert(wpi::util::StructSerializable<ctre::phoenix6::CANBusStatus>);
its the or e mails sent from or on behalf of the Company are free of trojan timebombs or other harmful components Some jurisdictions do not allow the exclusion of certain types of warranties or limitations on applicable statutory rights of a so some or all of the above exclusions and limitations may not apply to You But in such a case the exclusions and limitations set forth in this section shall be applied to the greatest extent enforceable under applicable law To the extent any warranty exists under law that cannot be the Company shall be solely responsible for such warranty Severability and Waiver Severability If any provision of this Agreement is held to be unenforceable or such provision will be changed and interpreted to accomplish the objectives of such provision to the greatest extent possible under applicable law and the remaining provisions will continue in full force and effect Waiver Except as provided the failure to exercise a right or to require performance of an obligation under this Agreement shall not affect a party s ability to exercise such right or require such performance at any time thereafter nor shall the waiver of a breach constitute waiver of any subsequent breach Product Claims The Company does not make any warranties concerning the Software United States Legal Compliance You represent and warrant or that has been designated by the United States government as a terrorist supporting at its sole to modify or replace this Agreement at any time If a revision is material we will provide at least days notice prior to any new terms taking effect What constitutes a material change will be determined at the sole discretion of the Company By continuing to access or use the Software after any revisions become You agree to be bound by the revised terms If You do not agree to the new You are no longer authorized to use the Software Governing Law The laws of the State of United States of excluding its conflicts of law shall govern this Agreement and your use of the Software Your use of the Software may also be subject to other state
Definition CTRE_LICENSE.txt:308
Class for getting information about an available CAN bus.
Definition CANBus.hpp:142
CANBusStatus GetStatus() const
Gets the status of the CAN bus, including the bus utilization and the error counters.
constexpr friend bool operator==(CANBus const &, CANBus const &)=default
constexpr CANBus(wpi::CANPort canPort)
Creates a new CAN bus for one of the native Systemcore/Motioncore buses.
Definition CANBus.hpp:207
constexpr CANBus(std::string_view canbus="")
Creates a new CAN bus with the given name.
Definition CANBus.hpp:195
friend std::ostream & operator<<(std::ostream &os, CANBus const &canbus)
constexpr friend auto operator<=>(CANBus const &, CANBus const &)=default
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:216
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:92
uint32_t ArbitrationLostCount
Arbitration lost count.
Definition CANBus.hpp:109
ctre::phoenix::StatusCode Status
Status code response of getting the data.
Definition CANBus.hpp:96
uint32_t RestartCount
Restart count.
Definition CANBus.hpp:113
CANState State
CAN state.
Definition CANBus.hpp:133
uint32_t BusErrorCount
Bus error count.
Definition CANBus.hpp:105
uint32_t BusOffCount
Bus off count.
Definition CANBus.hpp:117
uint16_t REC
Receive Error Counter (REC).
Definition CANBus.hpp:125
uint32_t TxFullCount
Transmit buffer full count.
Definition CANBus.hpp:121
uint16_t TEC
Transmit Error Counter (TEC).
Definition CANBus.hpp:129
friend std::ostream & operator<<(std::ostream &os, CANBusStatus const &status)
std::string ToString() const
wpi::units::unit_t< wpi::units::scalar, float > BusUtilization
CAN bus utilization, from 0.0 to 1.0.
Definition CANBus.hpp:101
Possible states of the CAN bus.
Definition CANBus.hpp:22
static constexpr int BusOff
The controller is in the "Bus Off" state after detecting too many errors.
Definition CANBus.hpp:41
static constexpr int Stopped
The controller has been manually stopped.
Definition CANBus.hpp:45
constexpr CANState()
Definition CANBus.hpp:55
constexpr auto operator<=>(CANState other) const
Definition CANBus.hpp:63
friend std::ostream & operator<<(std::ostream &os, CANState state)
constexpr std::string_view ToString() const
Gets the string representation of this enum.
Definition CANBus.hpp:73
constexpr CANState(int value)
Definition CANBus.hpp:51
static constexpr int ErrorPassive
The controller is in the "Error Passive" state and has detected many errors.
Definition CANBus.hpp:37
static constexpr int ErrorActive
The controller is in normal operation.
Definition CANBus.hpp:29
static constexpr int ErrorWarning
The controller has detected errors and is approaching the Error Passive state.
Definition CANBus.hpp:33
constexpr bool operator==(CANState other) const
Definition CANBus.hpp:59
static constexpr int Sleeping
The controller has entered low-power sleep mode.
Definition CANBus.hpp:49
int value
Unique integer value of this CANState.
Definition CANBus.hpp:24
size_t operator()(ctre::phoenix6::CANBus const &canbus) const noexcept
Definition CANBus.hpp:248
static constexpr std::string_view GetTypeName()
Definition CANBus.hpp:259
static constexpr std::string_view GetSchema()
Definition CANBus.hpp:268
static ctre::phoenix6::CANBusStatus Unpack(std::span< uint8_t const > data)
static constexpr size_t GetSize()
Definition CANBus.hpp:260
static void Pack(std::span< uint8_t > data, ctre::phoenix6::CANBusStatus const &value)