2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
#include <array>
|
2016-05-20 17:30:37 -07:00
|
|
|
#include <atomic>
|
2016-09-05 13:55:31 -07:00
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2016-11-01 20:12:08 -07:00
|
|
|
#include <thread>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/DriverStation.h>
|
2017-12-01 21:31:35 -08:00
|
|
|
#include <llvm/Twine.h>
|
2018-01-18 21:54:33 -08:00
|
|
|
#include <support/condition_variable.h>
|
2017-12-10 21:52:49 -08:00
|
|
|
#include <support/deprecated.h>
|
2017-11-13 09:51:48 -08:00
|
|
|
#include <support/mutex.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
#include "ErrorBase.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "RobotState.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2017-11-09 19:59:29 -08:00
|
|
|
struct MatchInfoData;
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Provide access to the network communication data to / from the Driver
|
|
|
|
|
* Station.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2017-12-04 23:28:33 -08:00
|
|
|
class DriverStation : public ErrorBase, public RobotStateInterface {
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
|
|
|
|
enum Alliance { kRed, kBlue, kInvalid };
|
2017-11-09 19:59:29 -08:00
|
|
|
enum MatchType { kNone, kPractice, kQualification, kElimination };
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
~DriverStation() override;
|
2016-05-20 17:30:37 -07:00
|
|
|
static DriverStation& GetInstance();
|
2017-12-01 21:31:35 -08:00
|
|
|
static void ReportError(const llvm::Twine& error);
|
|
|
|
|
static void ReportWarning(const llvm::Twine& error);
|
|
|
|
|
static void ReportError(bool isError, int code, const llvm::Twine& error,
|
|
|
|
|
const llvm::Twine& location,
|
|
|
|
|
const llvm::Twine& stack);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-11-19 19:04:28 -08:00
|
|
|
static constexpr int kJoystickPorts = 6;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
bool GetStickButton(int stick, int button);
|
|
|
|
|
bool GetStickButtonPressed(int stick, int button);
|
|
|
|
|
bool GetStickButtonReleased(int stick, int button);
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double GetStickAxis(int stick, int axis);
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetStickPOV(int stick, int pov);
|
|
|
|
|
int GetStickButtons(int stick) const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetStickAxisCount(int stick) const;
|
|
|
|
|
int GetStickPOVCount(int stick) const;
|
|
|
|
|
int GetStickButtonCount(int stick) const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
bool GetJoystickIsXbox(int stick) const;
|
|
|
|
|
int GetJoystickType(int stick) const;
|
|
|
|
|
std::string GetJoystickName(int stick) const;
|
|
|
|
|
int GetJoystickAxisType(int stick, int axis) const;
|
2015-06-19 17:23:54 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool IsEnabled() const override;
|
|
|
|
|
bool IsDisabled() const override;
|
|
|
|
|
bool IsAutonomous() const override;
|
|
|
|
|
bool IsOperatorControl() const override;
|
|
|
|
|
bool IsTest() const override;
|
|
|
|
|
bool IsDSAttached() const;
|
|
|
|
|
bool IsNewControlData() const;
|
|
|
|
|
bool IsFMSAttached() const;
|
2017-12-10 21:52:49 -08:00
|
|
|
WPI_DEPRECATED("Use RobotController static class method")
|
2015-06-25 15:07:55 -04:00
|
|
|
bool IsSysActive() const;
|
2017-12-10 21:52:49 -08:00
|
|
|
WPI_DEPRECATED("Use RobotController static class method")
|
2016-05-25 23:09:24 -07:00
|
|
|
bool IsBrownedOut() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-11-09 19:59:29 -08:00
|
|
|
std::string GetGameSpecificMessage() const;
|
|
|
|
|
std::string GetEventName() const;
|
|
|
|
|
MatchType GetMatchType() const;
|
|
|
|
|
int GetMatchNumber() const;
|
|
|
|
|
int GetReplayNumber() const;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Alliance GetAlliance() const;
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetLocation() const;
|
2015-06-25 15:07:55 -04:00
|
|
|
void WaitForData();
|
2016-10-09 11:46:01 -07:00
|
|
|
bool WaitForData(double timeout);
|
2015-06-25 15:07:55 -04:00
|
|
|
double GetMatchTime() const;
|
2016-11-20 07:25:03 -08:00
|
|
|
double GetBatteryVoltage() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-11-16 00:33:51 -08:00
|
|
|
/**
|
|
|
|
|
* Only to be used to tell the Driver Station what code you claim to be
|
|
|
|
|
* executing for diagnostic purposes only.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param entering If true, starting disabled code; if false, leaving disabled
|
2017-11-16 00:33:51 -08:00
|
|
|
* code.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void InDisabled(bool entering) { m_userInDisabled = entering; }
|
2017-11-16 00:33:51 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only to be used to tell the Driver Station what code you claim to be
|
|
|
|
|
* executing for diagnostic purposes only.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param entering If true, starting autonomous code; if false, leaving
|
2017-11-16 00:33:51 -08:00
|
|
|
* autonomous code.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void InAutonomous(bool entering) { m_userInAutonomous = entering; }
|
2017-11-16 00:33:51 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only to be used to tell the Driver Station what code you claim to be
|
|
|
|
|
* executing for diagnostic purposes only.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param entering If true, starting teleop code; if false, leaving teleop
|
2017-11-16 00:33:51 -08:00
|
|
|
* code.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void InOperatorControl(bool entering) { m_userInTeleop = entering; }
|
2017-11-16 00:33:51 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only to be used to tell the Driver Station what code you claim to be
|
|
|
|
|
* executing for diagnostic purposes only.
|
|
|
|
|
*
|
|
|
|
|
* @param entering If true, starting test code; if false, leaving test code.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void InTest(bool entering) { m_userInTest = entering; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
|
|
|
|
void GetData();
|
|
|
|
|
|
|
|
|
|
private:
|
2016-07-14 20:50:38 -07:00
|
|
|
DriverStation();
|
2017-11-16 00:33:51 -08:00
|
|
|
|
2017-12-01 21:31:35 -08:00
|
|
|
void ReportJoystickUnpluggedError(const llvm::Twine& message);
|
|
|
|
|
void ReportJoystickUnpluggedWarning(const llvm::Twine& message);
|
2015-06-25 15:07:55 -04:00
|
|
|
void Run();
|
2016-07-15 13:39:26 -07:00
|
|
|
void UpdateControlWord(bool force, HAL_ControlWord& controlWord) const;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-10-24 20:32:43 -07:00
|
|
|
// Joystick User Data
|
2016-07-09 00:24:26 -07:00
|
|
|
std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes;
|
|
|
|
|
std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
|
|
|
|
|
std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons;
|
|
|
|
|
std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor;
|
2017-11-09 19:59:29 -08:00
|
|
|
std::unique_ptr<MatchInfoData> m_matchInfo;
|
Implements locking in C++ DriverStation, and adds double buffering to DS Thread (#25)
Currently, about 5ms of every 20ms loop the DS thread would hold
the mutex, while grabbing data. During this time, and call to grab
joystick data would be blocked. This change grabs the joystick data
to a cache, and then grabs the mutex and moves the data references
around. This is much more efficient then the old code, and gives
teams more of their teleop loop time back for use.
Another major change this does is use preallocated arrays when entering
the JNI. Previously every JNI DS call would allocate an entire new array.
With a GC'd language where those arrays go on the heap, thats a problem,
and creates tons of garbage. That garbage is no longer created anymore,
as all arrays and byte buffers sent to JNI in the DS are preallocated.
In addition, GetJoystickName was always returning joystick 0 data, which
this fixes.
2016-05-21 00:41:15 -07:00
|
|
|
|
2016-10-24 20:32:43 -07:00
|
|
|
// Joystick Cached Data
|
2016-07-09 00:24:26 -07:00
|
|
|
std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache;
|
|
|
|
|
std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache;
|
|
|
|
|
std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
|
|
|
|
|
std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
|
2017-11-09 19:59:29 -08:00
|
|
|
std::unique_ptr<MatchInfoData> m_matchInfoCache;
|
Implements locking in C++ DriverStation, and adds double buffering to DS Thread (#25)
Currently, about 5ms of every 20ms loop the DS thread would hold
the mutex, while grabbing data. During this time, and call to grab
joystick data would be blocked. This change grabs the joystick data
to a cache, and then grabs the mutex and moves the data references
around. This is much more efficient then the old code, and gives
teams more of their teleop loop time back for use.
Another major change this does is use preallocated arrays when entering
the JNI. Previously every JNI DS call would allocate an entire new array.
With a GC'd language where those arrays go on the heap, thats a problem,
and creates tons of garbage. That garbage is no longer created anymore,
as all arrays and byte buffers sent to JNI in the DS are preallocated.
In addition, GetJoystickName was always returning joystick 0 data, which
this fixes.
2016-05-21 00:41:15 -07:00
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
// Joystick button rising/falling edge flags
|
|
|
|
|
std::array<uint32_t, kJoystickPorts> m_joystickButtonsPressed;
|
|
|
|
|
std::array<uint32_t, kJoystickPorts> m_joystickButtonsReleased;
|
|
|
|
|
|
2016-10-24 20:32:43 -07:00
|
|
|
// Internal Driver Station thread
|
2016-11-01 20:12:08 -07:00
|
|
|
std::thread m_dsThread;
|
2014-08-14 00:07:02 -07:00
|
|
|
std::atomic<bool> m_isRunning{false};
|
2016-10-24 20:32:43 -07:00
|
|
|
|
2018-01-18 21:54:33 -08:00
|
|
|
wpi::mutex m_waitForDataMutex;
|
|
|
|
|
wpi::condition_variable m_waitForDataCond;
|
|
|
|
|
int m_waitForDataCounter;
|
|
|
|
|
|
2017-11-13 09:51:48 -08:00
|
|
|
mutable wpi::mutex m_cacheDataMutex;
|
2016-10-24 20:32:43 -07:00
|
|
|
|
|
|
|
|
// Robot state status variables
|
2015-06-24 01:06:29 -07:00
|
|
|
bool m_userInDisabled = false;
|
|
|
|
|
bool m_userInAutonomous = false;
|
|
|
|
|
bool m_userInTeleop = false;
|
|
|
|
|
bool m_userInTest = false;
|
2016-10-24 20:32:43 -07:00
|
|
|
|
|
|
|
|
// Control word variables
|
2016-07-15 13:39:26 -07:00
|
|
|
mutable HAL_ControlWord m_controlWordCache;
|
|
|
|
|
mutable std::chrono::steady_clock::time_point m_lastControlWordUpdate;
|
2017-11-13 09:51:48 -08:00
|
|
|
mutable wpi::mutex m_controlWordMutex;
|
2016-10-24 20:32:43 -07:00
|
|
|
|
2015-06-24 01:06:29 -07:00
|
|
|
double m_nextMessageTime = 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|