WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
CAN.hpp
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 "wpi/hal/CANAPI.h"
10#include "wpi/hal/Types.hpp"
11
12namespace wpi {
13
14/**
15 * High level class for interfacing with CAN devices conforming to
16 * the standard CAN spec.
17 *
18 * No packets that can be sent gets blocked by the RoboRIO, so all methods
19 * work identically in all robot modes.
20 *
21 * All methods are thread save, however the buffer objects passed in
22 * by the user need to not be modified for the duration of their calls.
23 */
24class CAN {
25 public:
26 /**
27 * Create a new CAN communication interface with the specific device ID.
28 * This uses the team manufacturer and device types.
29 * The device ID is 6 bits (0-63)
30 *
31 * @param busId The bus id
32 * @param deviceId The device id
33 */
34 CAN(int busId, int deviceId);
35
36 /**
37 * Create a new CAN communication interface with a specific device ID,
38 * manufacturer and device type. The device ID is 6 bits, the
39 * manufacturer is 8 bits, and the device type is 5 bits.
40 *
41 * @param busId The bus id
42 * @param deviceId The device ID
43 * @param deviceManufacturer The device manufacturer
44 * @param deviceType The device type
45 */
46 CAN(int busId, int deviceId, int deviceManufacturer, int deviceType);
47
48 CAN(CAN&&) = default;
49 CAN& operator=(CAN&&) = default;
50
51 /**
52 * Write a packet to the CAN device with a specific ID. This ID is 10 bits.
53 *
54 * @param apiId The API ID to write.
55 * @param message the CAN message.
56 */
57 void WritePacket(int apiId, const HAL_CANMessage& message);
58
59 /**
60 * Write a repeating packet to the CAN device with a specific ID. This ID is
61 * 10 bits. The RoboRIO will automatically repeat the packet at the specified
62 * interval
63 *
64 * @param apiId The API ID to write.
65 * @param message the CAN message.
66 * @param repeatMs The period to repeat the packet at.
67 */
68 void WritePacketRepeating(int apiId, const HAL_CANMessage& message,
69 int repeatMs);
70
71 /**
72 * Write an RTR frame to the CAN device with a specific ID. This ID is 10
73 * bits. The length by spec must match what is returned by the responding
74 * device
75 *
76 * @param apiId The API ID to write.
77 * @param message the CAN message.
78 */
79 void WriteRTRFrame(int apiId, const HAL_CANMessage& message);
80
81 /**
82 * Write a packet to the CAN device with a specific ID. This ID is 10 bits.
83 *
84 * @param apiId The API ID to write.
85 * @param message the CAN message.
86 */
87 int WritePacketNoError(int apiId, const HAL_CANMessage& message);
88
89 /**
90 * Write a repeating packet to the CAN device with a specific ID. This ID is
91 * 10 bits. The RoboRIO will automatically repeat the packet at the specified
92 * interval
93 *
94 * @param apiId The API ID to write.
95 * @param message the CAN message.
96 * @param repeatMs The period to repeat the packet at.
97 */
98 int WritePacketRepeatingNoError(int apiId, const HAL_CANMessage& message,
99 int repeatMs);
100
101 /**
102 * Write an RTR frame to the CAN device with a specific ID. This ID is 10
103 * bits. The length by spec must match what is returned by the responding
104 * device
105 *
106 * @param apiId The API ID to write.
107 * @param message the CAN message.
108 */
109 int WriteRTRFrameNoError(int apiId, const HAL_CANMessage& message);
110
111 /**
112 * Stop a repeating packet with a specific ID. This ID is 10 bits.
113 *
114 * @param apiId The API ID to stop repeating
115 */
116 void StopPacketRepeating(int apiId);
117
118 /**
119 * Read a new CAN packet. This will only return properly once per packet
120 * received. Multiple calls without receiving another packet will return
121 * false.
122 *
123 * @param apiId The API ID to read.
124 * @param data Storage for the received data.
125 * @return True if the data is valid, otherwise false.
126 */
127 bool ReadPacketNew(int apiId, HAL_CANReceiveMessage* data);
128
129 /**
130 * Read a CAN packet. The will continuously return the last packet received,
131 * without accounting for packet age.
132 *
133 * @param apiId The API ID to read.
134 * @param data Storage for the received data.
135 * @return True if the data is valid, otherwise false.
136 */
138
139 /**
140 * Read a CAN packet. The will return the last packet received until the
141 * packet is older then the requested timeout. Then it will return false.
142 *
143 * @param apiId The API ID to read.
144 * @param timeoutMs The timeout time for the packet
145 * @param data Storage for the received data.
146 * @return True if the data is valid, otherwise false.
147 */
148 bool ReadPacketTimeout(int apiId, int timeoutMs, HAL_CANReceiveMessage* data);
149
150 /// Team manufacturer.
152
153 /// Team device type.
156
157 private:
159};
160} // namespace wpi
void StopPacketRepeating(int apiId)
Stop a repeating packet with a specific ID.
void WriteRTRFrame(int apiId, const HAL_CANMessage &message)
Write an RTR frame to the CAN device with a specific ID.
void WritePacketRepeating(int apiId, const HAL_CANMessage &message, int repeatMs)
Write a repeating packet to the CAN device with a specific ID.
CAN & operator=(CAN &&)=default
int WriteRTRFrameNoError(int apiId, const HAL_CANMessage &message)
Write an RTR frame to the CAN device with a specific ID.
static constexpr HAL_CANDeviceType kTeamDeviceType
Team device type.
Definition CAN.hpp:154
bool ReadPacketNew(int apiId, HAL_CANReceiveMessage *data)
Read a new CAN packet.
int WritePacketNoError(int apiId, const HAL_CANMessage &message)
Write a packet to the CAN device with a specific ID.
CAN(int busId, int deviceId, int deviceManufacturer, int deviceType)
Create a new CAN communication interface with a specific device ID, manufacturer and device type.
int WritePacketRepeatingNoError(int apiId, const HAL_CANMessage &message, int repeatMs)
Write a repeating packet to the CAN device with a specific ID.
static constexpr HAL_CANManufacturer kTeamManufacturer
Team manufacturer.
Definition CAN.hpp:151
bool ReadPacketTimeout(int apiId, int timeoutMs, HAL_CANReceiveMessage *data)
Read a CAN packet.
void WritePacket(int apiId, const HAL_CANMessage &message)
Write a packet to the CAN device with a specific ID.
CAN(int busId, int deviceId)
Create a new CAN communication interface with the specific device ID.
CAN(CAN &&)=default
bool ReadPacketLatest(int apiId, HAL_CANReceiveMessage *data)
Read a CAN packet.
A move-only C++ wrapper around a HAL handle.
Definition Types.hpp:16
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 CvSource.hpp:15
Definition CANAPITypes.h:139
Definition CANAPITypes.h:148