WPILibC++  2020.3.2
CAN.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <stdint.h>
11 
12 #include <hal/CANAPITypes.h>
13 #include <wpi/ArrayRef.h>
14 
15 #include "frc/ErrorBase.h"
16 
17 namespace frc {
18 struct CANData {
19  uint8_t data[8];
20  int32_t length;
21  uint64_t timestamp;
22 };
23 
34 class CAN : public ErrorBase {
35  public:
43  explicit CAN(int deviceId);
44 
54  CAN(int deviceId, int deviceManufacturer, int deviceType);
55 
59  ~CAN() override;
60 
61  CAN(CAN&&) = default;
62  CAN& operator=(CAN&&) = default;
63 
71  void WritePacket(const uint8_t* data, int length, int apiId);
72 
83  void WritePacketRepeating(const uint8_t* data, int length, int apiId,
84  int repeatMs);
85 
94  void WriteRTRFrame(int length, int apiId);
95 
101  void StopPacketRepeating(int apiId);
102 
112  bool ReadPacketNew(int apiId, CANData* data);
113 
122  bool ReadPacketLatest(int apiId, CANData* data);
123 
133  bool ReadPacketTimeout(int apiId, int timeoutMs, CANData* data);
134 
135  static constexpr HAL_CANManufacturer kTeamManufacturer = HAL_CAN_Man_kTeamUse;
136  static constexpr HAL_CANDeviceType kTeamDeviceType =
137  HAL_CAN_Dev_kMiscellaneous;
138 
139  private:
140  hal::Handle<HAL_CANHandle> m_handle;
141 };
142 } // namespace frc
frc::CAN
High level class for interfacing with CAN devices conforming to the standard CAN spec.
Definition: CAN.h:34
frc::CAN::CAN
CAN(int deviceId)
Create a new CAN communication interface with the specific device ID.
frc::CAN::~CAN
~CAN() override
Closes the CAN communication.
frc::CAN::ReadPacketTimeout
bool ReadPacketTimeout(int apiId, int timeoutMs, CANData *data)
Read a CAN packet.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::CAN::StopPacketRepeating
void StopPacketRepeating(int apiId)
Stop a repeating packet with a specific ID.
frc::CAN::WriteRTRFrame
void WriteRTRFrame(int length, int apiId)
Write an RTR frame to the CAN device with a specific ID.
frc::CAN::WritePacket
void WritePacket(const uint8_t *data, int length, int apiId)
Write a packet to the CAN device with a specific ID.
frc::CAN::WritePacketRepeating
void WritePacketRepeating(const uint8_t *data, int length, int apiId, int repeatMs)
Write a repeating packet to the CAN device with a specific ID.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::CAN::ReadPacketLatest
bool ReadPacketLatest(int apiId, CANData *data)
Read a CAN packet.
frc::CANData
Definition: CAN.h:18
frc::CAN::ReadPacketNew
bool ReadPacketNew(int apiId, CANData *data)
Read a new CAN packet.