blob: 5409ad056fa4c49cd9dad38c7a4356b6e79f5e59 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2016-2018 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#include <atomic>
9#include <chrono>
10#include <cstdlib>
11#include <cstring>
12#include <limits>
13
14#include <FRC_NetworkCommunication/FRCComm.h>
15#include <FRC_NetworkCommunication/NetCommRPCProxy_Occur.h>
16#include <wpi/SafeThread.h>
17#include <wpi/condition_variable.h>
18#include <wpi/mutex.h>
19#include <wpi/raw_ostream.h>
20
21#include "hal/DriverStation.h"
22
23static_assert(sizeof(int32_t) >= sizeof(int),
24 "FRC_NetworkComm status variable is larger than 32 bits");
25
26struct HAL_JoystickAxesInt {
27 int16_t count;
28 int16_t axes[HAL_kMaxJoystickAxes];
29};
30
31static constexpr int kJoystickPorts = 6;
32
33// Joystick User Data
34static std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes;
35static std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
36static std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons;
37static std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor;
38static std::unique_ptr<HAL_MatchInfo> m_matchInfo;
39
40// Joystick Cached Data
41static std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache;
42static std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache;
43static std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
44static std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
45static std::unique_ptr<HAL_MatchInfo> m_matchInfoCache;
46
47static wpi::mutex m_cacheDataMutex;
48
49// Control word variables
50static HAL_ControlWord m_controlWordCache;
51static std::chrono::steady_clock::time_point m_lastControlWordUpdate;
52static wpi::mutex m_controlWordMutex;
53
54// Message and Data variables
55static wpi::mutex msgMutex;
56
57static void InitializeDriverStationCaches() {
58 m_joystickAxes = std::make_unique<HAL_JoystickAxes[]>(kJoystickPorts);
59 m_joystickPOVs = std::make_unique<HAL_JoystickPOVs[]>(kJoystickPorts);
60 m_joystickButtons = std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);
61 m_joystickDescriptor =
62 std::make_unique<HAL_JoystickDescriptor[]>(kJoystickPorts);
63 m_matchInfo = std::make_unique<HAL_MatchInfo>();
64 m_joystickAxesCache = std::make_unique<HAL_JoystickAxes[]>(kJoystickPorts);
65 m_joystickPOVsCache = std::make_unique<HAL_JoystickPOVs[]>(kJoystickPorts);
66 m_joystickButtonsCache =
67 std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);
68 m_joystickDescriptorCache =
69 std::make_unique<HAL_JoystickDescriptor[]>(kJoystickPorts);
70 m_matchInfoCache = std::make_unique<HAL_MatchInfo>();
71
72 // All joysticks should default to having zero axes, povs and buttons, so
73 // uninitialized memory doesn't get sent to speed controllers.
74 for (unsigned int i = 0; i < kJoystickPorts; i++) {
75 m_joystickAxes[i].count = 0;
76 m_joystickPOVs[i].count = 0;
77 m_joystickButtons[i].count = 0;
78 m_joystickDescriptor[i].isXbox = 0;
79 m_joystickDescriptor[i].type = -1;
80 m_joystickDescriptor[i].name[0] = '\0';
81
82 m_joystickAxesCache[i].count = 0;
83 m_joystickPOVsCache[i].count = 0;
84 m_joystickButtonsCache[i].count = 0;
85 m_joystickDescriptorCache[i].isXbox = 0;
86 m_joystickDescriptorCache[i].type = -1;
87 m_joystickDescriptorCache[i].name[0] = '\0';
88 }
89}
90
91static int32_t HAL_GetJoystickAxesInternal(int32_t joystickNum,
92 HAL_JoystickAxes* axes) {
93 HAL_JoystickAxesInt axesInt;
94
95 int retVal = FRC_NetworkCommunication_getJoystickAxes(
96 joystickNum, reinterpret_cast<JoystickAxes_t*>(&axesInt),
97 HAL_kMaxJoystickAxes);
98
99 // copy integer values to double values
100 axes->count = axesInt.count;
101 // current scaling is -128 to 127, can easily be patched in the future by
102 // changing this function.
103 for (int32_t i = 0; i < axesInt.count; i++) {
104 int8_t value = axesInt.axes[i];
105 if (value < 0) {
106 axes->axes[i] = value / 128.0;
107 } else {
108 axes->axes[i] = value / 127.0;
109 }
110 }
111
112 return retVal;
113}
114
115static int32_t HAL_GetJoystickPOVsInternal(int32_t joystickNum,
116 HAL_JoystickPOVs* povs) {
117 return FRC_NetworkCommunication_getJoystickPOVs(
118 joystickNum, reinterpret_cast<JoystickPOV_t*>(povs),
119 HAL_kMaxJoystickPOVs);
120}
121
122static int32_t HAL_GetJoystickButtonsInternal(int32_t joystickNum,
123 HAL_JoystickButtons* buttons) {
124 return FRC_NetworkCommunication_getJoystickButtons(
125 joystickNum, &buttons->buttons, &buttons->count);
126}
127/**
128 * Retrieve the Joystick Descriptor for particular slot
129 * @param desc [out] descriptor (data transfer object) to fill in. desc is
130 * filled in regardless of success. In other words, if descriptor is not
131 * available, desc is filled in with default values matching the init-values in
132 * Java and C++ Driverstation for when caller requests a too-large joystick
133 * index.
134 *
135 * @return error code reported from Network Comm back-end. Zero is good,
136 * nonzero is bad.
137 */
138static int32_t HAL_GetJoystickDescriptorInternal(int32_t joystickNum,
139 HAL_JoystickDescriptor* desc) {
140 desc->isXbox = 0;
141 desc->type = std::numeric_limits<uint8_t>::max();
142 desc->name[0] = '\0';
143 desc->axisCount =
144 HAL_kMaxJoystickAxes; /* set to the desc->axisTypes's capacity */
145 desc->buttonCount = 0;
146 desc->povCount = 0;
147 int retval = FRC_NetworkCommunication_getJoystickDesc(
148 joystickNum, &desc->isXbox, &desc->type,
149 reinterpret_cast<char*>(&desc->name), &desc->axisCount,
150 reinterpret_cast<uint8_t*>(&desc->axisTypes), &desc->buttonCount,
151 &desc->povCount);
152 /* check the return, if there is an error and the RIOimage predates FRC2017,
153 * then axisCount needs to be cleared */
154 if (retval != 0) {
155 /* set count to zero so downstream code doesn't decode invalid axisTypes. */
156 desc->axisCount = 0;
157 }
158 return retval;
159}
160
161static int32_t HAL_GetControlWordInternal(HAL_ControlWord* controlWord) {
162 std::memset(controlWord, 0, sizeof(HAL_ControlWord));
163 return FRC_NetworkCommunication_getControlWord(
164 reinterpret_cast<ControlWord_t*>(controlWord));
165}
166
167static int32_t HAL_GetMatchInfoInternal(HAL_MatchInfo* info) {
168 MatchType_t matchType = MatchType_t::kMatchType_none;
169 int status = FRC_NetworkCommunication_getMatchInfo(
170 info->eventName, &matchType, &info->matchNumber, &info->replayNumber,
171 info->gameSpecificMessage, &info->gameSpecificMessageSize);
172
173 info->matchType = static_cast<HAL_MatchType>(matchType);
174
175 *(std::end(info->eventName) - 1) = '\0';
176
177 return status;
178}
179
180static void UpdateDriverStationControlWord(bool force,
181 HAL_ControlWord& controlWord) {
182 auto now = std::chrono::steady_clock::now();
183 std::lock_guard<wpi::mutex> lock(m_controlWordMutex);
184 // Update every 50 ms or on force.
185 if ((now - m_lastControlWordUpdate > std::chrono::milliseconds(50)) ||
186 force) {
187 HAL_GetControlWordInternal(&m_controlWordCache);
188 m_lastControlWordUpdate = now;
189 }
190 controlWord = m_controlWordCache;
191}
192
193static void UpdateDriverStationDataCaches() {
194 // Get the status of all of the joysticks, and save to the cache
195 for (uint8_t stick = 0; stick < kJoystickPorts; stick++) {
196 HAL_GetJoystickAxesInternal(stick, &m_joystickAxesCache[stick]);
197 HAL_GetJoystickPOVsInternal(stick, &m_joystickPOVsCache[stick]);
198 HAL_GetJoystickButtonsInternal(stick, &m_joystickButtonsCache[stick]);
199 HAL_GetJoystickDescriptorInternal(stick, &m_joystickDescriptorCache[stick]);
200 }
201 // Grab match specific data
202 HAL_GetMatchInfoInternal(m_matchInfoCache.get());
203
204 // Force a control word update, to make sure the data is the newest.
205 HAL_ControlWord controlWord;
206 UpdateDriverStationControlWord(true, controlWord);
207
208 {
209 // Obtain a lock on the data, swap the cached data into the main data arrays
210 std::lock_guard<wpi::mutex> lock(m_cacheDataMutex);
211
212 m_joystickAxes.swap(m_joystickAxesCache);
213 m_joystickPOVs.swap(m_joystickPOVsCache);
214 m_joystickButtons.swap(m_joystickButtonsCache);
215 m_joystickDescriptor.swap(m_joystickDescriptorCache);
216 m_matchInfo.swap(m_matchInfoCache);
217 }
218}
219
220class DriverStationThread : public wpi::SafeThread {
221 public:
222 void Main() {
223 std::unique_lock<wpi::mutex> lock(m_mutex);
224 while (m_active) {
225 m_cond.wait(lock, [&] { return !m_active || m_notify; });
226 if (!m_active) break;
227 m_notify = false;
228
229 lock.unlock();
230 UpdateDriverStationDataCaches();
231 lock.lock();
232
233 // Notify all threads
234 newDSDataAvailableCounter++;
235 newDSDataAvailableCond.notify_all();
236 }
237
238 // Notify waiters on thread exit
239 newDSDataAvailableCounter++;
240 newDSDataAvailableCond.notify_all();
241 }
242
243 bool m_notify = false;
244 wpi::condition_variable newDSDataAvailableCond;
245 int newDSDataAvailableCounter{0};
246};
247
248class DriverStationThreadOwner
249 : public wpi::SafeThreadOwner<DriverStationThread> {
250 public:
251 void Notify() {
252 auto thr = GetThread();
253 if (!thr) return;
254 thr->m_notify = true;
255 thr->m_cond.notify_one();
256 }
257};
258
259static std::unique_ptr<DriverStationThreadOwner> dsThread = nullptr;
260
261namespace hal {
262namespace init {
263void InitializeFRCDriverStation() {}
264} // namespace init
265} // namespace hal
266
267extern "C" {
268
269int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
270 const char* details, const char* location,
271 const char* callStack, HAL_Bool printMsg) {
272 // Avoid flooding console by keeping track of previous 5 error
273 // messages and only printing again if they're longer than 1 second old.
274 static constexpr int KEEP_MSGS = 5;
275 std::lock_guard<wpi::mutex> lock(msgMutex);
276 static std::string prevMsg[KEEP_MSGS];
277 static std::chrono::time_point<std::chrono::steady_clock>
278 prevMsgTime[KEEP_MSGS];
279 static bool initialized = false;
280 if (!initialized) {
281 for (int i = 0; i < KEEP_MSGS; i++) {
282 prevMsgTime[i] =
283 std::chrono::steady_clock::now() - std::chrono::seconds(2);
284 }
285 initialized = true;
286 }
287
288 auto curTime = std::chrono::steady_clock::now();
289 int i;
290 for (i = 0; i < KEEP_MSGS; ++i) {
291 if (prevMsg[i] == details) break;
292 }
293 int retval = 0;
294 if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) {
295 retval = FRC_NetworkCommunication_sendError(isError, errorCode, isLVCode,
296 details, location, callStack);
297 if (printMsg) {
298 if (location && location[0] != '\0') {
299 wpi::errs() << (isError ? "Error" : "Warning") << " at " << location
300 << ": ";
301 }
302 wpi::errs() << details << "\n";
303 if (callStack && callStack[0] != '\0') {
304 wpi::errs() << callStack << "\n";
305 }
306 }
307 if (i == KEEP_MSGS) {
308 // replace the oldest one
309 i = 0;
310 auto first = prevMsgTime[0];
311 for (int j = 1; j < KEEP_MSGS; ++j) {
312 if (prevMsgTime[j] < first) {
313 first = prevMsgTime[j];
314 i = j;
315 }
316 }
317 prevMsg[i] = details;
318 }
319 prevMsgTime[i] = curTime;
320 }
321 return retval;
322}
323
324int32_t HAL_GetControlWord(HAL_ControlWord* controlWord) {
325 std::memset(controlWord, 0, sizeof(HAL_ControlWord));
326 UpdateDriverStationControlWord(false, *controlWord);
327 return 0;
328}
329
330int32_t HAL_GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes) {
331 std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
332 *axes = m_joystickAxes[joystickNum];
333 return 0;
334}
335
336int32_t HAL_GetJoystickPOVs(int32_t joystickNum, HAL_JoystickPOVs* povs) {
337 std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
338 *povs = m_joystickPOVs[joystickNum];
339 return 0;
340}
341
342int32_t HAL_GetJoystickButtons(int32_t joystickNum,
343 HAL_JoystickButtons* buttons) {
344 std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
345 *buttons = m_joystickButtons[joystickNum];
346 return 0;
347}
348
349int32_t HAL_GetJoystickDescriptor(int32_t joystickNum,
350 HAL_JoystickDescriptor* desc) {
351 std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
352 *desc = m_joystickDescriptor[joystickNum];
353 return 0;
354}
355
356int32_t HAL_GetMatchInfo(HAL_MatchInfo* info) {
357 std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
358 *info = *m_matchInfo;
359 return 0;
360}
361
362HAL_AllianceStationID HAL_GetAllianceStation(int32_t* status) {
363 HAL_AllianceStationID allianceStation;
364 *status = FRC_NetworkCommunication_getAllianceStation(
365 reinterpret_cast<AllianceStationID_t*>(&allianceStation));
366 return allianceStation;
367}
368
369HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) {
370 HAL_JoystickDescriptor joystickDesc;
371 if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
372 return 0;
373 } else {
374 return joystickDesc.isXbox;
375 }
376}
377
378int32_t HAL_GetJoystickType(int32_t joystickNum) {
379 HAL_JoystickDescriptor joystickDesc;
380 if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
381 return -1;
382 } else {
383 return joystickDesc.type;
384 }
385}
386
387char* HAL_GetJoystickName(int32_t joystickNum) {
388 HAL_JoystickDescriptor joystickDesc;
389 if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
390 char* name = static_cast<char*>(std::malloc(1));
391 name[0] = '\0';
392 return name;
393 } else {
394 size_t len = std::strlen(joystickDesc.name);
395 char* name = static_cast<char*>(std::malloc(len + 1));
396 std::strncpy(name, joystickDesc.name, len);
397 name[len] = '\0';
398 return name;
399 }
400}
401
402void HAL_FreeJoystickName(char* name) { std::free(name); }
403
404int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) {
405 HAL_JoystickDescriptor joystickDesc;
406 if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
407 return -1;
408 } else {
409 return joystickDesc.axisTypes[axis];
410 }
411}
412
413int32_t HAL_SetJoystickOutputs(int32_t joystickNum, int64_t outputs,
414 int32_t leftRumble, int32_t rightRumble) {
415 return FRC_NetworkCommunication_setJoystickOutputs(joystickNum, outputs,
416 leftRumble, rightRumble);
417}
418
419double HAL_GetMatchTime(int32_t* status) {
420 float matchTime;
421 *status = FRC_NetworkCommunication_getMatchTime(&matchTime);
422 return matchTime;
423}
424
425void HAL_ObserveUserProgramStarting(void) {
426 FRC_NetworkCommunication_observeUserProgramStarting();
427}
428
429void HAL_ObserveUserProgramDisabled(void) {
430 FRC_NetworkCommunication_observeUserProgramDisabled();
431}
432
433void HAL_ObserveUserProgramAutonomous(void) {
434 FRC_NetworkCommunication_observeUserProgramAutonomous();
435}
436
437void HAL_ObserveUserProgramTeleop(void) {
438 FRC_NetworkCommunication_observeUserProgramTeleop();
439}
440
441void HAL_ObserveUserProgramTest(void) {
442 FRC_NetworkCommunication_observeUserProgramTest();
443}
444
445HAL_Bool HAL_IsNewControlData(void) {
446 // There is a rollover error condition here. At Packet# = n * (uintmax), this
447 // will return false when instead it should return true. However, this at a
448 // 20ms rate occurs once every 2.7 years of DS connected runtime, so not
449 // worth the cycles to check.
450 thread_local int lastCount{-1};
451 if (!dsThread) return false;
452 auto thr = dsThread->GetThread();
453 if (!thr) return false;
454 int currentCount = thr->newDSDataAvailableCounter;
455 if (lastCount == currentCount) return false;
456 lastCount = currentCount;
457 return true;
458}
459
460/**
461 * Waits for the newest DS packet to arrive. Note that this is a blocking call.
462 */
463void HAL_WaitForDSData(void) { HAL_WaitForDSDataTimeout(0); }
464
465/**
466 * Waits for the newest DS packet to arrive. If timeout is <= 0, this will wait
467 * forever. Otherwise, it will wait until either a new packet, or the timeout
468 * time has passed. Returns true on new data, false on timeout.
469 */
470HAL_Bool HAL_WaitForDSDataTimeout(double timeout) {
471 auto timeoutTime =
472 std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
473
474 if (!dsThread) return false;
475 auto thr = dsThread->GetThread();
476 if (!thr) return false;
477 int currentCount = thr->newDSDataAvailableCounter;
478 while (thr->newDSDataAvailableCounter == currentCount) {
479 if (timeout > 0) {
480 auto timedOut =
481 thr->newDSDataAvailableCond.wait_until(thr.GetLock(), timeoutTime);
482 if (timedOut == std::cv_status::timeout) {
483 return false;
484 }
485 } else {
486 thr->newDSDataAvailableCond.wait(thr.GetLock());
487 }
488 }
489 return true;
490}
491
492// Constant number to be used for our occur handle
493constexpr int32_t refNumber = 42;
494
495static void newDataOccur(uint32_t refNum) {
496 // Since we could get other values, require our specific handle
497 // to signal our threads
498 if (refNum != refNumber) return;
499 dsThread->Notify();
500}
501
502/*
503 * Call this to initialize the driver station communication. This will properly
504 * handle multiple calls. However note that this CANNOT be called from a library
505 * that interfaces with LabVIEW.
506 */
507void HAL_InitializeDriverStation(void) {
508 static std::atomic_bool initialized{false};
509 static wpi::mutex initializeMutex;
510 // Initial check, as if it's true initialization has finished
511 if (initialized) return;
512
513 std::lock_guard<wpi::mutex> lock(initializeMutex);
514 // Second check in case another thread was waiting
515 if (initialized) return;
516
517 InitializeDriverStationCaches();
518
519 dsThread = std::make_unique<DriverStationThreadOwner>();
520 dsThread->Start();
521
522 // Set up the occur function internally with NetComm
523 NetCommRPCProxy_SetOccurFuncPointer(newDataOccur);
524 // Set up our occur reference number
525 setNewDataOccurRef(refNumber);
526
527 initialized = true;
528}
529
530/*
531 * Releases the DS Mutex to allow proper shutdown of any threads that are
532 * waiting on it.
533 */
534void HAL_ReleaseDSMutex(void) { newDataOccur(refNumber); }
535
536} // extern "C"