1717#include " networktables/RawTopic.h"
1818#include " networktables/IntegerTopic.h"
1919#include " networktables/StringArrayTopic.h"
20+ #include " networktables/BooleanTopic.h"
2021
2122#define NUM_CAN_BUSES 5
2223
@@ -27,20 +28,16 @@ static constexpr uint32_t manufacturerMask = 0x00FF0000;
2728static constexpr uint32_t ctreFilter = 0x00040000 ;
2829static constexpr uint32_t revFilter = 0x00050000 ;
2930
30- struct SendVersionStore {
31- uint8_t ctrepcm : 1 ;
32- uint8_t ctrepdh : 1 ;
33- uint8_t revpdh : 1 ;
34- uint8_t revph : 1 ;
35- uint8_t reserved : 4 ;
36- };
31+ constexpr uint32_t revPhVersionPacketMask = 0x09052600 ;
32+ constexpr uint32_t revPhAnyPacketMask = 0x09050000 ;
33+ constexpr uint32_t revPdhVersionPacketMask = 0x08052600 ;
3734
3835struct CanState {
3936 int socketHandle{-1 };
4037 nt::IntegerPublisher deviceIdPublisher;
4138 std::array<nt::RawPublisher, 4 > framePublishers;
4239 nt::StringArrayPublisher versionPublisher;
43- std::array<SendVersionStore, 64 > sentVersions{ 0 , 0 , 0 , 0 , 0 } ;
40+ std::unordered_map< uint32_t , std::string > sentVersions;
4441 unsigned busId{0 };
4542
4643 ~CanState () {
@@ -49,9 +46,9 @@ struct CanState {
4946 }
5047 }
5148
52- void maybeSendRevVersionRequest ( uint8_t deviceId, bool isPower );
49+ void maybeSendVersionRequest ( uint32_t canId );
5350
54- void handleRevVersionFrame (const canfd_frame& frame, bool isPower );
51+ void handleRevVersionFrame (const canfd_frame& frame);
5552
5653 void handleCanFrame (const canfd_frame& frame);
5754 void handlePowerFrame (const canfd_frame& frame);
@@ -60,68 +57,57 @@ struct CanState {
6057 wpi::uv::Loop& loop);
6158};
6259
63- void CanState::maybeSendRevVersionRequest (uint8_t deviceId, bool isPower) {
64- if (deviceId > 63 ) {
65- return ;
66- }
67-
68- if (isPower && sentVersions[deviceId].revpdh ) {
69- return ;
70- } else if (!isPower && sentVersions[deviceId].revph ) {
60+ void CanState::maybeSendVersionRequest (uint32_t canId) {
61+ auto & sent = sentVersions[canId];
62+ if (!sent.empty ()) {
7163 return ;
7264 }
7365
74- constexpr uint32_t pneumaticsRequest = 0x09052600 ;
75- constexpr uint32_t powerRequest = 0x08052600 ;
76-
7766 canfd_frame frame;
7867 memset (&frame, 0 , sizeof (frame));
79- frame.can_id = deviceId | CAN_EFF_FLAG | CAN_RTR_FLAG;
80- frame.can_id |= isPower ? powerRequest : pneumaticsRequest;
68+ frame.can_id = canId | CAN_EFF_FLAG | CAN_RTR_FLAG;
8169 frame.len = 8 ;
8270
71+ printf (" Requesting %x version frame\n " , canId);
72+
8373 send (socketHandle, &frame, CAN_MTU, 0 );
8474}
8575
86- void CanState::handleRevVersionFrame (const canfd_frame& frame, bool isPower ) {
76+ void CanState::handleRevVersionFrame (const canfd_frame& frame) {
8777 if (frame.len < 8 ) {
8878 return ;
8979 }
9080
91- uint8_t year = frame.data [0 ];
81+ uint8_t year = frame.data [2 ];
9282 uint8_t minor = frame.data [1 ];
93- uint8_t fix = frame.data [2 ];
94-
95- uint8_t deviceId = (frame.can_id & 0x3F );
83+ uint8_t fix = frame.data [0 ];
9684
97- uint16_t deviceBusCombined = busId << 8 | deviceId ;
85+ uint32_t frameId = frame. can_id & CAN_EFF_MASK ;
9886
9987 char buf[32 ];
10088 snprintf (buf, sizeof (buf), " %u.%u.%u" , year, minor, fix);
10189
10290 std::array<std::string, 3 > sendData;
103- sendData[0 ] = std::to_string (deviceBusCombined);
104- sendData[1 ] = isPower ? " RevPH" : " RevPDH" ;
91+ sendData[0 ] = std::to_string (frameId);
92+ sendData[1 ] = ((frame.can_id & deviceTypeMask) == pneumaticsFilter)
93+ ? " Rev PH"
94+ : " Rev PDH" ;
10595 sendData[2 ] = buf;
10696
97+ fmt::print (" Setting {:x} {} {}\n " , frameId, sendData[1 ], sendData[2 ]);
98+
10799 versionPublisher.Set (sendData);
108100
109- if (isPower) {
110- sentVersions[deviceId].revpdh = 1 ;
111- } else {
112- sentVersions[deviceId].revph = 1 ;
113- }
101+ sentVersions[frameId] = buf;
114102}
115103
116104void CanState::handlePneumaticsFrame (const canfd_frame& frame) {
117105 // The only thing we're doing with pneumatics is version receiving.
118- constexpr uint32_t revPhVersionPacketMask = 0x09052600 ;
119- constexpr uint32_t revPhAnyPacketMask = 0x09050000 ;
120106
121107 if ((frame.can_id & revPhVersionPacketMask) == revPhVersionPacketMask) {
122- handleRevVersionFrame (frame, false );
108+ handleRevVersionFrame (frame);
123109 } else if ((frame.can_id & revPhAnyPacketMask) == revPhAnyPacketMask) {
124- maybeSendRevVersionRequest ( frame.can_id & 0x3F , false );
110+ maybeSendVersionRequest (revPhVersionPacketMask | ( frame.can_id & 0x3F ) );
125111 }
126112}
127113
@@ -152,14 +138,14 @@ void CanState::handlePowerFrame(const canfd_frame& frame) {
152138 // Rev Frame
153139 if (apiId == 0x98 ) {
154140 // Version frame
155- handleRevVersionFrame (frame, true );
141+ handleRevVersionFrame (frame);
156142 return ;
157143 } else if (apiId < 0x60 || apiId > 0x63 ) {
158144 // Not valid
159145 return ;
160146 }
161147
162- maybeSendRevVersionRequest (frame. can_id & 0x3F , true );
148+ maybeSendVersionRequest (revPdhVersionPacketMask | (deviceId & 0x3F ) );
163149
164150 frameNum = apiId - 0x60 ;
165151 } else {
@@ -217,7 +203,7 @@ bool CanState::startUvLoop(unsigned bus, const nt::NetworkTableInstance& ntInst,
217203 ntInst.GetIntegerTopic (" /pd/" + busIdStr + " /deviceid" ).Publish ();
218204
219205 versionPublisher =
220- ntInst.GetStringArrayTopic (" /Netcomm/Reporting/LibVersionStr " )
206+ ntInst.GetStringArrayTopic (" /Netcomm/Reporting/UserVersionStr " )
221207 .Publish (options);
222208
223209 socketHandle =
@@ -293,7 +279,7 @@ bool CanState::startUvLoop(unsigned bus, const nt::NetworkTableInstance& ntInst,
293279}
294280
295281int main () {
296- printf (" Starting PowerDistributionDaemon \n " );
282+ printf (" Starting KitCanDaemon \n " );
297283 printf (" \t Build Hash: %s\n " , MRC_GetGitHash ());
298284 printf (" \t Build Timestamp: %s\n " , MRC_GetBuildTimestamp ());
299285
0 commit comments