[hal, wpilib] Rewrite CAN APIs (#7798)

This commit is contained in:
Thad House
2025-02-25 19:07:01 -08:00
committed by GitHub
parent b39744b562
commit baa20fa239
107 changed files with 1447 additions and 1379 deletions

View File

@@ -9,14 +9,6 @@
#include <hal/CANAPI.h>
namespace frc {
struct CANData {
/** Contents of the CAN packet. */
uint8_t data[8];
/** Length of packet in bytes. */
int32_t length;
/** CAN frame timestamp in milliseconds. */
uint64_t timestamp;
};
/**
* High level class for interfacing with CAN devices conforming to
@@ -35,20 +27,22 @@ class CAN {
* This uses the team manufacturer and device types.
* The device ID is 6 bits (0-63)
*
* @param busId The bus id
* @param deviceId The device id
*/
explicit CAN(int deviceId);
CAN(int busId, int deviceId);
/**
* Create a new CAN communication interface with a specific device ID,
* manufacturer and device type. The device ID is 6 bits, the
* manufacturer is 8 bits, and the device type is 5 bits.
*
* @param busId The bus id
* @param deviceId The device ID
* @param deviceManufacturer The device manufacturer
* @param deviceType The device type
*/
CAN(int deviceId, int deviceManufacturer, int deviceType);
CAN(int busId, int deviceId, int deviceManufacturer, int deviceType);
CAN(CAN&&) = default;
CAN& operator=(CAN&&) = default;
@@ -56,23 +50,21 @@ class CAN {
/**
* Write a packet to the CAN device with a specific ID. This ID is 10 bits.
*
* @param data The data to write (8 bytes max)
* @param length The data length to write
* @param apiId The API ID to write.
* @param message the CAN message.
*/
void WritePacket(const uint8_t* data, int length, int apiId);
void WritePacket(int apiId, const HAL_CANMessage& message);
/**
* Write a repeating packet to the CAN device with a specific ID. This ID is
* 10 bits. The RoboRIO will automatically repeat the packet at the specified
* interval
*
* @param data The data to write (8 bytes max)
* @param length The data length to write
* @param apiId The API ID to write.
* @param message the CAN message.
* @param repeatMs The period to repeat the packet at.
*/
void WritePacketRepeating(const uint8_t* data, int length, int apiId,
void WritePacketRepeating(int apiId, const HAL_CANMessage& message,
int repeatMs);
/**
@@ -80,31 +72,29 @@ class CAN {
* bits. The length by spec must match what is returned by the responding
* device
*
* @param length The length to request (0 to 8)
* @param apiId The API ID to write.
* @param message the CAN message.
*/
void WriteRTRFrame(int length, int apiId);
void WriteRTRFrame(int apiId, const HAL_CANMessage& message);
/**
* Write a packet to the CAN device with a specific ID. This ID is 10 bits.
*
* @param data The data to write (8 bytes max)
* @param length The data length to write
* @param apiId The API ID to write.
* @param message the CAN message.
*/
int WritePacketNoError(const uint8_t* data, int length, int apiId);
int WritePacketNoError(int apiId, const HAL_CANMessage& message);
/**
* Write a repeating packet to the CAN device with a specific ID. This ID is
* 10 bits. The RoboRIO will automatically repeat the packet at the specified
* interval
*
* @param data The data to write (8 bytes max)
* @param length The data length to write
* @param apiId The API ID to write.
* @param message the CAN message.
* @param repeatMs The period to repeat the packet at.
*/
int WritePacketRepeatingNoError(const uint8_t* data, int length, int apiId,
int WritePacketRepeatingNoError(int apiId, const HAL_CANMessage& message,
int repeatMs);
/**
@@ -112,10 +102,10 @@ class CAN {
* bits. The length by spec must match what is returned by the responding
* device
*
* @param length The length to request (0 to 8)
* @param apiId The API ID to write.
* @param message the CAN message.
*/
int WriteRTRFrameNoError(int length, int apiId);
int WriteRTRFrameNoError(int apiId, const HAL_CANMessage& message);
/**
* Stop a repeating packet with a specific ID. This ID is 10 bits.
@@ -133,7 +123,7 @@ class CAN {
* @param data Storage for the received data.
* @return True if the data is valid, otherwise false.
*/
bool ReadPacketNew(int apiId, CANData* data);
bool ReadPacketNew(int apiId, HAL_CANReceiveMessage* data);
/**
* Read a CAN packet. The will continuously return the last packet received,
@@ -143,7 +133,7 @@ class CAN {
* @param data Storage for the received data.
* @return True if the data is valid, otherwise false.
*/
bool ReadPacketLatest(int apiId, CANData* data);
bool ReadPacketLatest(int apiId, HAL_CANReceiveMessage* data);
/**
* Read a CAN packet. The will return the last packet received until the
@@ -154,16 +144,7 @@ class CAN {
* @param data Storage for the received data.
* @return True if the data is valid, otherwise false.
*/
bool ReadPacketTimeout(int apiId, int timeoutMs, CANData* data);
/**
* Reads the current value of the millisecond-resolution timer that CANData
* timestamps are based on
*
* @return Current value of timer used as a base time for CANData timestamps
* in milliseconds
*/
static uint64_t GetTimestampBaseTime();
bool ReadPacketTimeout(int apiId, int timeoutMs, HAL_CANReceiveMessage* data);
/// Team manufacturer.
static constexpr HAL_CANManufacturer kTeamManufacturer = HAL_CAN_Man_kTeamUse;

View File

@@ -37,17 +37,19 @@ class Compressor : public wpi::Sendable,
/**
* Constructs a compressor for a specified module and type.
*
* @param busId The bus ID.
* @param module The module ID to use.
* @param moduleType The module type to use.
*/
Compressor(int module, PneumaticsModuleType moduleType);
Compressor(int busId, int module, PneumaticsModuleType moduleType);
/**
* Constructs a compressor for a default module and specified type.
*
* @param busId The bus ID.
* @param moduleType The module type to use.
*/
explicit Compressor(PneumaticsModuleType moduleType);
Compressor(int busId, PneumaticsModuleType moduleType);
~Compressor() override;

View File

@@ -41,23 +41,25 @@ class DoubleSolenoid : public wpi::Sendable,
* Constructs a double solenoid for a specified module of a specific module
* type.
*
* @param busId The bus ID.
* @param module The module of the solenoid module to use.
* @param moduleType The module type to use.
* @param forwardChannel The forward channel on the module to control.
* @param reverseChannel The reverse channel on the module to control.
*/
DoubleSolenoid(int module, PneumaticsModuleType moduleType,
DoubleSolenoid(int busId, int module, PneumaticsModuleType moduleType,
int forwardChannel, int reverseChannel);
/**
* Constructs a double solenoid for a default module of a specific module
* type.
*
* @param busId The bus ID.
* @param moduleType The module type to use.
* @param forwardChannel The forward channel on the module to control.
* @param reverseChannel The reverse channel on the module to control.
*/
DoubleSolenoid(PneumaticsModuleType moduleType, int forwardChannel,
DoubleSolenoid(int busId, PneumaticsModuleType moduleType, int forwardChannel,
int reverseChannel);
~DoubleSolenoid() override;

View File

@@ -17,15 +17,20 @@ namespace frc {
/** Module class for controlling a REV Robotics Pneumatic Hub. */
class PneumaticHub : public PneumaticsBase {
public:
/** Constructs a PneumaticHub with the default ID (1). */
PneumaticHub();
/**
* Constructs a PneumaticHub with the default ID (1).
*
* @param busId The bus ID.
*/
explicit PneumaticHub(int busId);
/**
* Constructs a PneumaticHub.
*
* @param busId The bus ID.
* @param module module number to construct
*/
explicit PneumaticHub(int module);
PneumaticHub(int busId, int module);
~PneumaticHub() override = default;
@@ -296,17 +301,17 @@ class PneumaticHub : public PneumaticsBase {
class DataStore;
friend class DataStore;
friend class PneumaticsBase;
PneumaticHub(HAL_REVPHHandle handle, int module);
PneumaticHub(int busId, HAL_REVPHHandle handle, int module);
static std::shared_ptr<PneumaticsBase> GetForModule(int module);
static std::shared_ptr<PneumaticsBase> GetForModule(int busId, int module);
std::shared_ptr<DataStore> m_dataStore;
HAL_REVPHHandle m_handle;
int m_module;
static wpi::mutex m_handleLock;
static std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<DataStore>>>
m_handleMap;
static std::weak_ptr<DataStore>& GetDataStore(int module);
static std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<DataStore>>[]>
m_handleMaps;
static std::weak_ptr<DataStore>& GetDataStore(int busId, int module);
};
} // namespace frc

View File

@@ -267,12 +267,13 @@ class PneumaticsBase {
/**
* For internal use to get a module for a specific type.
*
* @param busId The bus ID.
* @param module module number
* @param moduleType module type
* @return module
*/
static std::shared_ptr<PneumaticsBase> GetForType(
int module, PneumaticsModuleType moduleType);
int busId, int module, PneumaticsModuleType moduleType);
/**
* For internal use to get the default for a specific type.

View File

@@ -17,15 +17,20 @@ namespace frc {
* Module. */
class PneumaticsControlModule : public PneumaticsBase {
public:
/** Constructs a PneumaticsControlModule with the default ID (0). */
PneumaticsControlModule();
/**
* Constructs a PneumaticsControlModule with the default ID (0).
*
* @param busId The bus ID.
*/
explicit PneumaticsControlModule(int busId);
/**
* Constructs a PneumaticsControlModule.
*
* @param busId The bus ID.
* @param module module number to construct
*/
explicit PneumaticsControlModule(int module);
PneumaticsControlModule(int busId, int module);
~PneumaticsControlModule() override = default;
@@ -196,17 +201,17 @@ class PneumaticsControlModule : public PneumaticsBase {
class DataStore;
friend class DataStore;
friend class PneumaticsBase;
PneumaticsControlModule(HAL_CTREPCMHandle handle, int module);
PneumaticsControlModule(int busId, HAL_CTREPCMHandle handle, int module);
static std::shared_ptr<PneumaticsBase> GetForModule(int module);
static std::shared_ptr<PneumaticsBase> GetForModule(int busId, int module);
std::shared_ptr<DataStore> m_dataStore;
HAL_CTREPCMHandle m_handle;
int m_module;
static wpi::mutex m_handleLock;
static std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<DataStore>>>
m_handleMap;
static std::weak_ptr<DataStore>& GetDataStore(int module);
static std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<DataStore>>[]>
m_handleMaps;
static std::weak_ptr<DataStore>& GetDataStore(int busId, int module);
};
} // namespace frc

View File

@@ -38,16 +38,19 @@ class PowerDistribution : public wpi::Sendable,
*
* Detects the connected PDP/PDH using the default CAN ID (0 for CTRE and 1
* for REV).
*
* @param busId The bus ID.
*/
PowerDistribution();
explicit PowerDistribution(int busId);
/**
* Constructs a PowerDistribution object.
*
* @param busId The bus ID.
* @param module The CAN ID of the PDP/PDH
* @param moduleType The type of module
*/
PowerDistribution(int module, ModuleType moduleType);
PowerDistribution(int busId, int module, ModuleType moduleType);
PowerDistribution(PowerDistribution&&) = default;
PowerDistribution& operator=(PowerDistribution&&) = default;

View File

@@ -336,9 +336,10 @@ class RobotController {
/**
* Get the current status of the CAN bus.
*
* @param busId The bus ID.
* @return The status of the CAN bus
*/
static CANStatus GetCANStatus();
static CANStatus GetCANStatus(int busId);
private:
static std::function<uint64_t()> m_timeSource;

View File

@@ -28,19 +28,21 @@ class Solenoid : public wpi::Sendable, public wpi::SendableHelper<Solenoid> {
/**
* Constructs a solenoid for a specified module and type.
*
* @param busId The bus ID.
* @param module The module ID to use.
* @param moduleType The module type to use.
* @param channel The channel the solenoid is on.
*/
Solenoid(int module, PneumaticsModuleType moduleType, int channel);
Solenoid(int busId, int module, PneumaticsModuleType moduleType, int channel);
/**
* Constructs a solenoid for a default module and specified type.
*
* @param busId The bus ID.
* @param moduleType The module type to use.
* @param channel The channel the solenoid is on.
*/
Solenoid(PneumaticsModuleType moduleType, int channel);
Solenoid(int busId, PneumaticsModuleType moduleType, int channel);
~Solenoid() override;