WPILibC++ 2026.2.2
Loading...
Searching...
No Matches
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/CANAPI.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 CAN(CAN&&) = default;
54 CAN& operator=(CAN&&) = default;
55
56 /**
57 * Write a packet to the CAN device with a specific ID. This ID is 10 bits.
58 *
59 * @param data The data to write (8 bytes max)
60 * @param length The data length to write
61 * @param apiId The API ID to write.
62 */
63 void WritePacket(const uint8_t* data, int length, int apiId);
64
65 /**
66 * Write a repeating packet to the CAN device with a specific ID. This ID is
67 * 10 bits. The RoboRIO will automatically repeat the packet at the specified
68 * interval
69 *
70 * @param data The data to write (8 bytes max)
71 * @param length The data length to write
72 * @param apiId The API ID to write.
73 * @param repeatMs The period to repeat the packet at.
74 */
75 void WritePacketRepeating(const uint8_t* data, int length, int apiId,
76 int repeatMs);
77
78 /**
79 * Write an RTR frame to the CAN device with a specific ID. This ID is 10
80 * bits. The length by spec must match what is returned by the responding
81 * device
82 *
83 * @param length The length to request (0 to 8)
84 * @param apiId The API ID to write.
85 */
86 void WriteRTRFrame(int length, int apiId);
87
88 /**
89 * Write a packet to the CAN device with a specific ID. This ID is 10 bits.
90 *
91 * @param data The data to write (8 bytes max)
92 * @param length The data length to write
93 * @param apiId The API ID to write.
94 */
95 int WritePacketNoError(const uint8_t* data, int length, int apiId);
96
97 /**
98 * Write a repeating packet to the CAN device with a specific ID. This ID is
99 * 10 bits. The RoboRIO will automatically repeat the packet at the specified
100 * interval
101 *
102 * @param data The data to write (8 bytes max)
103 * @param length The data length to write
104 * @param apiId The API ID to write.
105 * @param repeatMs The period to repeat the packet at.
106 */
107 int WritePacketRepeatingNoError(const uint8_t* data, int length, int apiId,
108 int repeatMs);
109
110 /**
111 * Write an RTR frame to the CAN device with a specific ID. This ID is 10
112 * bits. The length by spec must match what is returned by the responding
113 * device
114 *
115 * @param length The length to request (0 to 8)
116 * @param apiId The API ID to write.
117 */
118 int WriteRTRFrameNoError(int length, int apiId);
119
120 /**
121 * Stop a repeating packet with a specific ID. This ID is 10 bits.
122 *
123 * @param apiId The API ID to stop repeating
124 */
125 void StopPacketRepeating(int apiId);
126
127 /**
128 * Read a new CAN packet. This will only return properly once per packet
129 * received. Multiple calls without receiving another packet will return
130 * false.
131 *
132 * @param apiId The API ID to read.
133 * @param data Storage for the received data.
134 * @return True if the data is valid, otherwise false.
135 */
136 bool ReadPacketNew(int apiId, CANData* data);
137
138 /**
139 * Read a CAN packet. The will continuously return the last packet received,
140 * without accounting for packet age.
141 *
142 * @param apiId The API ID to read.
143 * @param data Storage for the received data.
144 * @return True if the data is valid, otherwise false.
145 */
146 bool ReadPacketLatest(int apiId, CANData* data);
147
148 /**
149 * Read a CAN packet. The will return the last packet received until the
150 * packet is older then the requested timeout. Then it will return false.
151 *
152 * @param apiId The API ID to read.
153 * @param timeoutMs The timeout time for the packet
154 * @param data Storage for the received data.
155 * @return True if the data is valid, otherwise false.
156 */
157 bool ReadPacketTimeout(int apiId, int timeoutMs, CANData* data);
158
159 /**
160 * Reads the current value of the millisecond-resolution timer that CANData
161 * timestamps are based on
162 *
163 * @return Current value of timer used as a base time for CANData timestamps
164 * in milliseconds
165 */
166 static uint64_t GetTimestampBaseTime();
167
168 /// Team manufacturer.
170
171 /// Team device type.
174
175 private:
177};
178} // 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.
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:172
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:169
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.
A move-only C++ wrapper around a HAL handle.
Definition Types.h:96
HAL_CANDeviceType
The CAN device type.
Definition CANAPITypes.h:22
HAL_CANManufacturer
The CAN manufacturer ID.
Definition CANAPITypes.h:60
@ HAL_CAN_Dev_kMiscellaneous
Miscellaneous.
Definition CANAPITypes.h:44
@ HAL_CAN_Man_kTeamUse
Team use.
Definition CANAPITypes.h:78
Definition CAN.h:11
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