39#include <unordered_map>
43#include "rev/CANSparkMaxFrames.h"
48static const char* frc_deviceType_text[15] = {
54 "Accelerometer Sensor",
58 "Pneumatics Controller",
66static const char* frc_manufacturer_text[10] = {
67 "Broadcast",
"NI",
"LM",
"DEKA",
"CTRE",
68 "REV",
"Grapple",
"MindSensors",
"TeamUse",
"Unknown",
71static inline const char* GetFRCDeviceTypeText(uint32_t index) {
72 if (index > 11 && index < 31) {
74 }
else if (index > 31) {
76 }
else if (index == 31) {
81 return frc_deviceType_text[index];
84static inline const char* GetFRCManufacturerText(uint32_t index) {
85 index = (index > 9) ? 9 : index;
86 return frc_manufacturer_text[index];
100 this->deviceTypeId = frame.fields.deviceType;
101 this->manufacturerId = frame.fields.manufacturer;
102 this->canId = frame.fields.deviceNumber;
107 return std::string(GetFRCManufacturerText(
manufacturerId)) +
" " +
143 explicit CANBusScanner(
int buffersize = 256,
int threadIntervalMs = 10);
151 void RegisterDevice(std::string name, std::vector<uint32_t> validIds,
152 int32_t maxFramePeriodMs = 100);
155 class CANScanElement {
160 explicit CANScanElement(uint64_t timeoutMs = 1000);
161 void UpdateLastSeen();
162 bool IsActive()
const;
165 class CANScanCollection {
167 CANScanCollection(std::string name, uint32_t arbId, uint64_t timeoutMs)
168 : name(name), timeout(timeoutMs), arbId(arbId) {}
170 std::vector<int> ActiveDevices()
const {
171 std::vector<int> result;
172 for (
const auto& [devKey, devValue] : devices) {
173 if (devValue.IsActive()) {
174 result.push_back(devKey);
180 void AddOrUpdateDevice(
int id) {
181 if (devices.find(
id) != devices.end()) {
182 devices[id] = CANScanElement(timeout);
184 devices[id].UpdateLastSeen();
187 std::string Name()
const {
return name; }
188 uint32_t ArbId()
const {
return arbId; }
194 std::unordered_map<int, CANScanElement> devices;
201 std::unordered_map<uint32_t, std::shared_ptr<CANScanCollection> >
203 std::vector<std::shared_ptr<CANScanCollection> > m_registeredList;
205 int m_streamBufferSize;
206 uint32_t m_streamHandle;
207 int m_threadInterval;
209 std::thread m_thread;
210 std::atomic_bool m_stopThread;
211 std::atomic_bool m_running;
212 std::string m_lastError;
Definition: CANDeviceScanner.h:139
~CANBusScanner()
Definition: CANDeviceScanner.cpp:58
bool Start()
Definition: CANDeviceScanner.cpp:64
std::vector< CANScanIdentifier > CANBusScan()
Definition: CANDeviceScanner.cpp:147
bool Running()
Definition: CANDeviceScanner.cpp:90
void Stop()
Definition: CANDeviceScanner.cpp:82
void RegisterDevice(std::string name, std::vector< uint32_t > validIds, int32_t maxFramePeriodMs=100)
Definition: CANDeviceScanner.cpp:131
std::string LastError()
Definition: CANDeviceScanner.cpp:92
CANBusScanner(int buffersize=256, int threadIntervalMs=10)
Definition: CANDeviceScanner.cpp:51
bool operator<(const CANScanIdentifier &lhs, const CANScanIdentifier &rhs)
Definition: CANDeviceScanner.h:115
bool operator==(const CANScanIdentifier &lhs, const CANScanIdentifier &rhs)
Definition: CANDeviceScanner.h:131
bool operator>(const CANScanIdentifier &lhs, const CANScanIdentifier &rhs)
Definition: CANDeviceScanner.h:123
Definition: SparkLowLevel.cpp:40
Definition: CANDeviceScanner.h:89
frc_deviceType_t deviceTypeId
Definition: CANDeviceScanner.h:90
uint16_t canId
Definition: CANDeviceScanner.h:92
uint32_t uniqueId
Definition: CANDeviceScanner.h:93
CANScanIdentifier(uint32_t arbid, std::string name, uint32_t uniqueId=0)
Definition: CANDeviceScanner.h:95
std::string name
Definition: CANDeviceScanner.h:94
frc_manufacturer_t manufacturerId
Definition: CANDeviceScanner.h:91
std::string Name()
Definition: CANDeviceScanner.h:105