1616#include " networktables/NetworkTableInstance.h"
1717#include " networktables/RawTopic.h"
1818#include " networktables/IntegerTopic.h"
19- #include " networktables/StringArrayTopic.h"
20- #include " networktables/BooleanTopic.h"
2119
22- #define NUM_CAN_BUSES 5
20+ #define NUM_CAN_BUSES 2
2321
2422static constexpr uint32_t deviceTypeMask = 0x3F000000 ;
2523static constexpr uint32_t powerDistributionFilter = 0x08000000 ;
26- static constexpr uint32_t pneumaticsFilter = 0x09000000 ;
27- static constexpr uint32_t manufacturerMask = 0x00FF0000 ;
28- static constexpr uint32_t ctreFilter = 0x00040000 ;
29- static constexpr uint32_t revFilter = 0x00050000 ;
30-
31- constexpr uint32_t revPhVersionPacketMask = 0x09052600 ;
32- constexpr uint32_t revPhAnyPacketMask = 0x09050000 ;
33- constexpr uint32_t revPdhVersionPacketMask = 0x08052600 ;
3424
3525struct CanState {
36- wpi::mutex sendVersionsMutex;
3726 int socketHandle{-1 };
3827 nt::IntegerPublisher deviceIdPublisher;
3928 std::array<nt::RawPublisher, 4 > framePublishers;
40- nt::StringArrayPublisher versionPublisher;
41- std::unordered_map<uint32_t , std::string> sentVersions;
4229 unsigned busId{0 };
4330
4431 ~CanState () {
@@ -47,85 +34,12 @@ struct CanState {
4734 }
4835 }
4936
50- void maybeSendVersionRequest (uint32_t canId);
51-
52- void handleRevVersionFrame (const canfd_frame& frame);
53- void sendVersions ();
54-
5537 void handleCanFrame (const canfd_frame& frame);
5638 void handlePowerFrame (const canfd_frame& frame);
57- void handlePneumaticsFrame (const canfd_frame& frame);
5839 bool startUvLoop (unsigned bus, const nt::NetworkTableInstance& ntInst,
5940 wpi::uv::Loop& loop);
6041};
6142
62- void CanState::sendVersions () {
63- std::scoped_lock lock{sendVersionsMutex};
64-
65- for (auto && versions : sentVersions) {
66- if (versions.first == 0 || versions.second .empty ()) {
67- continue ;
68- }
69-
70- uint32_t frameId = versions.first ;
71-
72- std::array<std::string, 3 > sendData;
73- sendData[0 ] = std::to_string (frameId);
74- sendData[1 ] = ((frameId & deviceTypeMask) == pneumaticsFilter)
75- ? " Rev PH"
76- : " Rev PDH" ;
77- sendData[2 ] = versions.second ;
78-
79- fmt::print (" Setting {:x} {} {}\n " , frameId, sendData[1 ], sendData[2 ]);
80-
81- versionPublisher.Set (sendData);
82- }
83- }
84-
85- void CanState::maybeSendVersionRequest (uint32_t canId) {
86- auto & sent = sentVersions[canId];
87- if (!sent.empty ()) {
88- return ;
89- }
90-
91- canfd_frame frame;
92- memset (&frame, 0 , sizeof (frame));
93- frame.can_id = canId | CAN_EFF_FLAG | CAN_RTR_FLAG;
94- frame.len = 8 ;
95-
96- printf (" Requesting %x version frame\n " , canId);
97-
98- send (socketHandle, &frame, CAN_MTU, 0 );
99- }
100-
101- void CanState::handleRevVersionFrame (const canfd_frame& frame) {
102- if (frame.len < 8 ) {
103- return ;
104- }
105-
106- uint8_t year = frame.data [2 ];
107- uint8_t minor = frame.data [1 ];
108- uint8_t fix = frame.data [0 ];
109-
110- uint32_t frameId = frame.can_id & CAN_EFF_MASK;
111-
112- char buf[32 ];
113- snprintf (buf, sizeof (buf), " %u.%u.%u" , year, minor, fix);
114-
115- std::scoped_lock lock{sendVersionsMutex};
116- sentVersions[frameId] = buf;
117- }
118-
119- void CanState::handlePneumaticsFrame (const canfd_frame& frame) {
120- // The only thing we're doing with pneumatics is version receiving.
121-
122- if ((frame.can_id & revPhVersionPacketMask) == revPhVersionPacketMask) {
123- handleRevVersionFrame (frame);
124- } else if ((frame.can_id & revPhAnyPacketMask) == revPhAnyPacketMask) {
125- maybeSendVersionRequest (revPhVersionPacketMask | (frame.can_id & 0x3F ));
126- }
127- }
128-
12943void CanState::handleCanFrame (const canfd_frame& frame) {
13044 // Can't support FD frames
13145 if (frame.flags & CANFD_FDF) {
@@ -138,8 +52,6 @@ void CanState::handleCanFrame(const canfd_frame& frame) {
13852
13953 if (maskedDeviceType == powerDistributionFilter) {
14054 handlePowerFrame (frame);
141- } else if (maskedDeviceType == pneumaticsFilter) {
142- handlePneumaticsFrame (frame);
14355 }
14456}
14557
@@ -151,30 +63,21 @@ void CanState::handlePowerFrame(const canfd_frame& frame) {
15163
15264 if (frame.can_id & 0x10000 ) {
15365 // Rev Frame
154- if (apiId == 0x98 ) {
155- // Version frame
156- handleRevVersionFrame (frame);
157- return ;
158- } else if (apiId < 0x60 || apiId > 0x63 ) {
66+ if (apiId < 0x60 || apiId > 0x63 ) {
15967 // Not valid
16068 return ;
16169 }
16270
163- maybeSendVersionRequest (revPdhVersionPacketMask | (deviceId & 0x3F ));
164-
16571 frameNum = apiId - 0x60 ;
16672 } else {
16773 // CTRE frame
168-
16974 if (apiId == 0x5D ) {
17075 // Special case
17176 frameNum = 3 ;
77+ } else if (apiId < 0x50 || apiId > 0x52 ) {
78+ // Not valid
79+ return ;
17280 } else {
173- if (apiId < 0x50 || apiId > 0x52 ) {
174- // Not valid
175- return ;
176- }
177-
17881 frameNum = apiId - 0x50 ;
17982 }
18083 }
@@ -194,7 +97,7 @@ void CanState::handlePowerFrame(const canfd_frame& frame) {
19497
19598bool CanState::startUvLoop (unsigned bus, const nt::NetworkTableInstance& ntInst,
19699 wpi::uv::Loop& loop) {
197- if (busId >= NUM_CAN_BUSES) {
100+ if (bus >= NUM_CAN_BUSES) {
198101 return false ;
199102 }
200103
@@ -217,25 +120,20 @@ bool CanState::startUvLoop(unsigned bus, const nt::NetworkTableInstance& ntInst,
217120 deviceIdPublisher =
218121 ntInst.GetIntegerTopic (" /pd/" + busIdStr + " /deviceid" ).Publish ();
219122
220- versionPublisher =
221- ntInst.GetStringArrayTopic (" /Netcomm/Reporting/UserVersionStr" )
222- .Publish (options);
223-
224123 socketHandle =
225124 socket (PF_CAN, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, CAN_RAW);
226125
227126 if (socketHandle == -1 ) {
228127 return false ;
229128 }
230129
231- // Filter to PD or pneumatics device type.
130+ // Filter to PD device type.
232131 // Both mfg types have the "4" bit set. They just
233132 // differ on the 1 bit. So a single filter can be used,
234133 // ignoring that bit.
235- // Same thing for 8 vs 9 for the device type
236134 struct can_filter filter {
237135 .can_id = 0x08040000 | CAN_EFF_FLAG,
238- .can_mask = 0x1EFE0000 | CAN_EFF_FLAG,
136+ .can_mask = 0x1FFE0000 | CAN_EFF_FLAG,
239137 };
240138
241139 if (setsockopt (socketHandle, SOL_CAN_RAW, CAN_RAW_FILTER, &filter,
@@ -294,7 +192,7 @@ bool CanState::startUvLoop(unsigned bus, const nt::NetworkTableInstance& ntInst,
294192}
295193
296194int main () {
297- printf (" Starting KitCanDaemon \n " );
195+ printf (" Starting PowerDistributionDaemon \n " );
298196 printf (" \t Build Hash: %s\n " , MRC_GetGitHash ());
299197 printf (" \t Build Timestamp: %s\n " , MRC_GetBuildTimestamp ());
300198
@@ -310,11 +208,7 @@ int main() {
310208
311209 auto ntInst = nt::NetworkTableInstance::Create ();
312210 ntInst.SetServer ({" localhost" }, 6810 );
313- ntInst.StartClient (" KitCanDaemon" );
314-
315- nt::IntegerSubscriber requestSubscriber =
316- ntInst.GetIntegerTopic (" /Netcomm/Reporting/RequestVersions" )
317- .Subscribe (0 );
211+ ntInst.StartClient (" PowerDistributionDaemon" );
318212
319213 wpi::EventLoopRunner loopRunner;
320214
@@ -328,14 +222,6 @@ int main() {
328222 }
329223 });
330224
331- NT_Listener requestListener =
332- ntInst.AddListener (requestSubscriber, NT_EVENT_VALUE_REMOTE,
333- [&states, &loopRunner](const nt::Event& event) {
334- for (size_t i = 0 ; i < states.size (); i++) {
335- states[i].sendVersions ();
336- }
337- });
338-
339225 if (!success) {
340226 loopRunner.Stop ();
341227 return -1 ;
@@ -349,7 +235,6 @@ int main() {
349235 (void )getchar ();
350236#endif
351237 }
352- ntInst.RemoveListener (requestListener);
353238 ntInst.StopClient ();
354239 nt::NetworkTableInstance::Destroy (ntInst);
355240
0 commit comments