CTRE Phoenix 6 C++ 26.0.0-beta-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 roboRIO CAN bus, use #RoboRIO instead.
60 *
61 * \param canbus Name of the CAN bus. Possible CAN bus strings are:
62 * - "rio" for the native roboRIO CAN bus
63 * - CANivore name or serial number
64 * - SocketCAN interface (non-FRC Linux only)
65 * - "*" for any CANivore seen by the program
66 * - empty string (default) to select the default for the system:
67 * - "rio" on roboRIO
68 * - "can0" on Linux
69 * - "*" on Windows
70 */
71 constexpr CANBus(std::string_view canbus = "") :
72 _name{canbus}
73 {}
74
75 /**
76 * \brief Creates a new CAN bus with the given name, and loads an associated
77 * hoot file for replay (equivalent to HootReplay#LoadFile).
78 *
79 * Only one hoot log may be replayed at a time. As a result, only one
80 * CAN bus should be constructed with a hoot file.
81 *
82 * When using relative paths, the file path is typically relative
83 * to the top-level folder of the robot project.
84 *
85 * For the native roboRIO CAN bus, use #RoboRIO instead.
86 *
87 * \param canbus Name of the CAN bus. Possible CAN bus strings are:
88 * - "rio" for the native roboRIO CAN bus
89 * - CANivore name or serial number
90 * - SocketCAN interface (non-FRC Linux only)
91 * - "*" for any CANivore seen by the program
92 * - empty string (default) to select the default for the system:
93 * - "rio" on roboRIO
94 * - "can0" on Linux
95 * - "*" on Windows
96 * \param hootFilepath Path and name of the hoot file to load
97 */
98 CANBus(std::string_view canbus, char const *hootFilepath) :
99 CANBus{canbus}
100 {
101 HootReplay::LoadFile(hootFilepath);
102 }
103
104 /**
105 * \brief Creates a new CAN bus for the native roboRIO bus.
106 *
107 * \returns The native roboRIO CAN bus
108 */
109 static constexpr CANBus RoboRIO()
110 {
111 return CANBus{"rio"};
112 }
113
114 /**
115 * \brief Creates a new CAN bus for the native roboRIO bus, and
116 * loads an associated hoot file for replay (equivalent to
117 * HootReplay#LoadFile).
118 *
119 * Only one hoot log may be replayed at a time. As a result, only one
120 * CAN bus should be constructed with a hoot file.
121 *
122 * When using relative paths, the file path is typically relative
123 * to the top-level folder of the robot project.
124 *
125 * \param hootFilepath Path and name of the hoot file to load
126 * \returns The native roboRIO CAN bus
127 */
128 static CANBus RoboRIO(char const *hootFilepath)
129 {
130 HootReplay::LoadFile(hootFilepath);
131 return RoboRIO();
132 }
133
134 /**
135 * \brief Get the name used to construct this CAN bus.
136 *
137 * \returns Name of the CAN bus
138 */
139 constexpr std::string_view GetName() const
140 {
141 return _name;
142 }
143
144 /**
145 * \brief Gets whether the CAN bus is a CAN FD network.
146 *
147 * \returns True if the CAN bus is CAN FD
148 */
149 bool IsNetworkFD() const;
150 /**
151 * \brief Gets the status of the CAN bus, including the
152 * bus utilization and the error counters.
153 *
154 * This can block for up to 0.001 seconds (1 ms).
155 *
156 * \returns Status of the CAN bus
157 */
159
160 friend std::ostream &operator<<(std::ostream &os, CANBus const &canbus);
161};
162
163}
164}
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:98
CANBusStatus GetStatus() const
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:71
static CANBus RoboRIO(char const *hootFilepath)
Creates a new CAN bus for the native roboRIO bus, and loads an associated hoot file for replay (equiv...
Definition CANBus.hpp:128
friend std::ostream & operator<<(std::ostream &os, CANBus const &canbus)
static constexpr CANBus RoboRIO()
Creates a new CAN bus for the native roboRIO bus.
Definition CANBus.hpp:109
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:139
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 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