WPILibC++ 2024.3.2
CAN.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <hal/CANAPITypes.h>
10
11namespace frc {
12struct CANData {
13 /** Contents of the CAN packet. */
14 uint8_t data[8];
15 /** Length of packet in bytes. */
16 int32_t length;
17 /** CAN frame timestamp in milliseconds. */
18 uint64_t timestamp;
19};
20
21/**
22 * High level class for interfacing with CAN devices conforming to
23 * the standard CAN spec.
24 *
25 * No packets that can be sent gets blocked by the RoboRIO, so all methods
26 * work identically in all robot modes.
27 *
28 * All methods are thread save, however the buffer objects passed in
29 * by the user need to not be modified for the duration of their calls.
30 */
31class CAN {
32 public:
33 /**
34 * Create a new CAN communication interface with the specific device ID.
35 * This uses the team manufacturer and device types.
36 * The device ID is 6 bits (0-63)
37 *
38 * @param deviceId The device id
39 */
40 explicit CAN(int deviceId);
41
42 /**
43 * Create a new CAN communication interface with a specific device ID,
44 * manufacturer and device type. The device ID is 6 bits, the
45 * manufacturer is 8 bits, and the device type is 5 bits.
46 *
47 * @param deviceId The device ID
48 * @param deviceManufacturer The device manufacturer
49 * @param deviceType The device type
50 */
51 CAN(int deviceId, int deviceManufacturer, int deviceType);
52
53 /**
54 * Closes the CAN communication.
55 */
57
58 CAN(CAN&&) = default;
59 CAN& operator=(CAN&&) = default;
60
61 /**
62 * Write a packet to the CAN device with a specific ID. This ID is 10 bits.
63 *
64 * @param data The data to write (8 bytes max)
65 * @param length The data length to write
66 * @param apiId The API ID to write.
67 */
68 void WritePacket(const uint8_t* data, int length, int apiId);
69
70 /**
71 * Write a repeating packet to the CAN device with a specific ID. This ID is
72 * 10 bits. The RoboRIO will automatically repeat the packet at the specified
73 * interval
74 *
75 * @param data The data to write (8 bytes max)
76 * @param length The data length to write
77 * @param apiId The API ID to write.
78 * @param repeatMs The period to repeat the packet at.
79 */
80 void WritePacketRepeating(const uint8_t* data, int length, int apiId,
81 int repeatMs);
82
83 /**
84 * Write an RTR frame to the CAN device with a specific ID. This ID is 10
85 * bits. The length by spec must match what is returned by the responding
86 * device
87 *
88 * @param length The length to request (0 to 8)
89 * @param apiId The API ID to write.
90 */
91 void WriteRTRFrame(int length, int apiId);
92
93 /**
94 * Write a packet to the CAN device with a specific ID. This ID is 10 bits.
95 *
96 * @param data The data to write (8 bytes max)
97 * @param length The data length to write
98 * @param apiId The API ID to write.
99 */
100 int WritePacketNoError(const uint8_t* data, int length, int apiId);
101
102 /**
103 * Write a repeating packet to the CAN device with a specific ID. This ID is
104 * 10 bits. The RoboRIO will automatically repeat the packet at the specified
105 * interval
106 *
107 * @param data The data to write (8 bytes max)
108 * @param length The data length to write
109 * @param apiId The API ID to write.
110 * @param repeatMs The period to repeat the packet at.
111 */
112 int WritePacketRepeatingNoError(const uint8_t* data, int length, int apiId,
113 int repeatMs);
114
115 /**
116 * Write an RTR frame to the CAN device with a specific ID. This ID is 10
117 * bits. The length by spec must match what is returned by the responding
118 * device
119 *
120 * @param length The length to request (0 to 8)
121 * @param apiId The API ID to write.
122 */
123 int WriteRTRFrameNoError(int length, int apiId);
124
125 /**
126 * Stop a repeating packet with a specific ID. This ID is 10 bits.
127 *
128 * @param apiId The API ID to stop repeating
129 */
130 void StopPacketRepeating(int apiId);
131
132 /**
133 * Read a new CAN packet. This will only return properly once per packet
134 * received. Multiple calls without receiving another packet will return
135 * false.
136 *
137 * @param apiId The API ID to read.
138 * @param data Storage for the received data.
139 * @return True if the data is valid, otherwise false.
140 */
141 bool ReadPacketNew(int apiId, CANData* data);
142
143 /**
144 * Read a CAN packet. The will continuously return the last packet received,
145 * without accounting for packet age.
146 *
147 * @param apiId The API ID to read.
148 * @param data Storage for the received data.
149 * @return True if the data is valid, otherwise false.
150 */
151 bool ReadPacketLatest(int apiId, CANData* data);
152
153 /**
154 * Read a CAN packet. The will return the last packet received until the
155 * packet is older then the requested timeout. Then it will return false.
156 *
157 * @param apiId The API ID to read.
158 * @param timeoutMs The timeout time for the packet
159 * @param data Storage for the received data.
160 * @return True if the data is valid, otherwise false.
161 */
162 bool ReadPacketTimeout(int apiId, int timeoutMs, CANData* data);
163
164 /**
165 * Reads the current value of the millisecond-resolution timer that CANData
166 * timestamps are based on
167 *
168 * @return Current value of timer used as a base time for CANData timestamps
169 * in milliseconds
170 */
171 static uint64_t GetTimestampBaseTime();
172
173 /// Team manufacturer.
175
176 /// Team device type.
179
180 private:
182};
183} // namespace frc
High level class for interfacing with CAN devices conforming to the standard CAN spec.
Definition: CAN.h:31
bool ReadPacketNew(int apiId, CANData *data)
Read a new CAN packet.
~CAN()
Closes the CAN communication.
int WritePacketRepeatingNoError(const uint8_t *data, int length, int apiId, int repeatMs)
Write a repeating packet to the CAN device with a specific ID.
bool ReadPacketTimeout(int apiId, int timeoutMs, CANData *data)
Read a CAN packet.
bool ReadPacketLatest(int apiId, CANData *data)
Read a CAN packet.
CAN(CAN &&)=default
CAN(int deviceId, int deviceManufacturer, int deviceType)
Create a new CAN communication interface with a specific device ID, manufacturer and device type.
CAN & operator=(CAN &&)=default
static constexpr HAL_CANDeviceType kTeamDeviceType
Team device type.
Definition: CAN.h:177
static uint64_t GetTimestampBaseTime()
Reads the current value of the millisecond-resolution timer that CANData timestamps are based on.
static constexpr HAL_CANManufacturer kTeamManufacturer
Team manufacturer.
Definition: CAN.h:174
int WriteRTRFrameNoError(int length, int apiId)
Write an RTR frame to the CAN device with a specific ID.
void StopPacketRepeating(int apiId)
Stop a repeating packet with a specific ID.
int WritePacketNoError(const uint8_t *data, int length, int apiId)
Write a packet to the CAN device with a specific ID.
CAN(int deviceId)
Create a new CAN communication interface with the specific device ID.
void WritePacketRepeating(const uint8_t *data, int length, int apiId, int repeatMs)
Write a repeating packet to the CAN device with a specific ID.
void WriteRTRFrame(int length, int apiId)
Write an RTR frame to the CAN device with a specific ID.
void WritePacket(const uint8_t *data, int length, int apiId)
Write a packet to the CAN device with a specific ID.
HAL_CANDeviceType
The CAN device type.
Definition: CANAPITypes.h:22
HAL_CANManufacturer
The CAN manufacturer ID.
Definition: CANAPITypes.h:56
@ HAL_CAN_Dev_kMiscellaneous
Miscellaneous.
Definition: CANAPITypes.h:44
@ HAL_CAN_Man_kTeamUse
Team use.
Definition: CANAPITypes.h:74
Definition: AprilTagPoseEstimator.h:15
Definition: CAN.h:12
uint8_t data[8]
Contents of the CAN packet.
Definition: CAN.h:14
uint64_t timestamp
CAN frame timestamp in milliseconds.
Definition: CAN.h:18
int32_t length
Length of packet in bytes.
Definition: CAN.h:16