2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-01-01 01:05:57 -07:00
|
|
|
/* Copyright (c) FIRST 2008-2017. 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
|
|
|
|
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
|
|
|
|
2016-11-01 20:12:08 -07:00
|
|
|
#include "HAL/DriverStation.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "HAL/cpp/priority_mutex.h"
|
|
|
|
|
#include "RobotState.h"
|
|
|
|
|
#include "SensorBase.h"
|
2016-11-26 21:22:39 -08:00
|
|
|
#include "llvm/StringRef.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
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
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
class DriverStation : public SensorBase, public RobotStateInterface {
|
|
|
|
|
public:
|
|
|
|
|
enum Alliance { kRed, kBlue, kInvalid };
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual ~DriverStation();
|
2016-05-20 17:30:37 -07:00
|
|
|
static DriverStation& GetInstance();
|
2016-12-20 22:08:24 -08:00
|
|
|
static void ReportError(llvm::StringRef error);
|
|
|
|
|
static void ReportWarning(llvm::StringRef error);
|
|
|
|
|
static void ReportError(bool is_error, int code, llvm::StringRef error,
|
|
|
|
|
llvm::StringRef location, llvm::StringRef stack);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
static const int kJoystickPorts = 6;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
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;
|
|
|
|
|
bool GetStickButton(int stick, int button);
|
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;
|
|
|
|
|
bool IsSysActive() const;
|
2016-05-25 23:09:24 -07:00
|
|
|
bool IsBrownedOut() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
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
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/** Only to be used to tell the Driver Station what code you claim to be
|
2016-05-20 17:30:37 -07:00
|
|
|
* executing for diagnostic purposes only
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param entering If true, starting disabled code; if false, leaving disabled
|
|
|
|
|
* code */
|
|
|
|
|
void InDisabled(bool entering) { m_userInDisabled = entering; }
|
|
|
|
|
/** Only to be used to tell the Driver Station what code you claim to be
|
2016-05-20 17:30:37 -07:00
|
|
|
* executing for diagnostic purposes only
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param entering If true, starting autonomous code; if false, leaving
|
|
|
|
|
* autonomous code */
|
|
|
|
|
void InAutonomous(bool entering) { m_userInAutonomous = entering; }
|
|
|
|
|
/** Only to be used to tell the Driver Station what code you claim to be
|
2016-05-20 17:30:37 -07:00
|
|
|
* executing for diagnostic purposes only
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param entering If true, starting teleop code; if false, leaving teleop
|
|
|
|
|
* code */
|
|
|
|
|
void InOperatorControl(bool entering) { m_userInTeleop = entering; }
|
|
|
|
|
/** Only to be used to tell the Driver Station what code you claim to be
|
2016-05-20 17:30:37 -07:00
|
|
|
* executing for diagnostic purposes only
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param entering If true, starting test code; if false, leaving test code */
|
|
|
|
|
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();
|
2016-11-26 21:22:39 -08:00
|
|
|
void ReportJoystickUnpluggedError(llvm::StringRef message);
|
|
|
|
|
void ReportJoystickUnpluggedWarning(llvm::StringRef 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;
|
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;
|
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
|
|
|
// 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
|
|
|
|
2017-05-11 21:25:22 -07:00
|
|
|
mutable hal::priority_mutex m_joystickDataMutex;
|
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-05-11 21:25:22 -07:00
|
|
|
mutable hal::priority_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
|