Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 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 "frc/RobotBase.h" |
| 9 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 10 | #ifdef __FRC_ROBORIO__ |
| 11 | #include <dlfcn.h> |
| 12 | #endif |
| 13 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 14 | #include <cstdio> |
| 15 | |
| 16 | #include <cameraserver/CameraServerShared.h> |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame^] | 17 | #include <hal/FRCUsageReporting.h> |
| 18 | #include <hal/HALBase.h> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 19 | #include <networktables/NetworkTableInstance.h> |
| 20 | |
| 21 | #include "WPILibVersion.h" |
| 22 | #include "frc/DriverStation.h" |
| 23 | #include "frc/RobotState.h" |
| 24 | #include "frc/Utility.h" |
| 25 | #include "frc/WPIErrors.h" |
| 26 | #include "frc/livewindow/LiveWindow.h" |
| 27 | #include "frc/smartdashboard/SmartDashboard.h" |
| 28 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 29 | typedef void (*SetCameraServerSharedFP)(frc::CameraServerShared* shared); |
| 30 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 31 | using namespace frc; |
| 32 | |
| 33 | int frc::RunHALInitialization() { |
| 34 | if (!HAL_Initialize(500, 0)) { |
| 35 | wpi::errs() << "FATAL ERROR: HAL could not be initialized\n"; |
| 36 | return -1; |
| 37 | } |
| 38 | HAL_Report(HALUsageReporting::kResourceType_Language, |
| 39 | HALUsageReporting::kLanguage_CPlusPlus); |
| 40 | wpi::outs() << "\n********** Robot program starting **********\n"; |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | std::thread::id RobotBase::m_threadId; |
| 45 | |
| 46 | namespace { |
| 47 | class WPILibCameraServerShared : public frc::CameraServerShared { |
| 48 | public: |
| 49 | void ReportUsbCamera(int id) override { |
| 50 | HAL_Report(HALUsageReporting::kResourceType_UsbCamera, id); |
| 51 | } |
| 52 | void ReportAxisCamera(int id) override { |
| 53 | HAL_Report(HALUsageReporting::kResourceType_AxisCamera, id); |
| 54 | } |
| 55 | void ReportVideoServer(int id) override { |
| 56 | HAL_Report(HALUsageReporting::kResourceType_PCVideoServer, id); |
| 57 | } |
| 58 | void SetCameraServerError(const wpi::Twine& error) override { |
| 59 | wpi_setGlobalWPIErrorWithContext(CameraServerError, error); |
| 60 | } |
| 61 | void SetVisionRunnerError(const wpi::Twine& error) override { |
| 62 | wpi_setGlobalErrorWithContext(-1, error); |
| 63 | } |
| 64 | void ReportDriverStationError(const wpi::Twine& error) override { |
| 65 | DriverStation::ReportError(error); |
| 66 | } |
| 67 | std::pair<std::thread::id, bool> GetRobotMainThreadId() const override { |
| 68 | return std::make_pair(RobotBase::GetThreadId(), true); |
| 69 | } |
| 70 | }; |
| 71 | } // namespace |
| 72 | |
| 73 | static void SetupCameraServerShared() { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 74 | #ifdef __FRC_ROBORIO__ |
| 75 | #ifdef DYNAMIC_CAMERA_SERVER |
| 76 | #ifdef DYNAMIC_CAMERA_SERVER_DEBUG |
| 77 | auto cameraServerLib = dlopen("libcameraserverd.so", RTLD_NOW); |
| 78 | #else |
| 79 | auto cameraServerLib = dlopen("libcameraserver.so", RTLD_NOW); |
| 80 | #endif |
| 81 | |
| 82 | if (!cameraServerLib) { |
| 83 | wpi::outs() << "Camera Server Library Not Found\n"; |
| 84 | wpi::outs().flush(); |
| 85 | return; |
| 86 | } |
| 87 | auto symbol = dlsym(cameraServerLib, "CameraServer_SetCameraServerShared"); |
| 88 | if (symbol) { |
| 89 | auto setCameraServerShared = (SetCameraServerSharedFP)symbol; |
| 90 | setCameraServerShared(new WPILibCameraServerShared{}); |
| 91 | wpi::outs() << "Set Camera Server Shared\n"; |
| 92 | wpi::outs().flush(); |
| 93 | } else { |
| 94 | wpi::outs() << "Camera Server Shared Symbol Missing\n"; |
| 95 | wpi::outs().flush(); |
| 96 | } |
| 97 | #else |
| 98 | CameraServer_SetCameraServerShared(new WPILibCameraServerShared{}); |
| 99 | #endif |
| 100 | #else |
| 101 | wpi::outs() << "Not loading CameraServerShared\n"; |
| 102 | wpi::outs().flush(); |
| 103 | #endif |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool RobotBase::IsEnabled() const { return m_ds.IsEnabled(); } |
| 107 | |
| 108 | bool RobotBase::IsDisabled() const { return m_ds.IsDisabled(); } |
| 109 | |
| 110 | bool RobotBase::IsAutonomous() const { return m_ds.IsAutonomous(); } |
| 111 | |
| 112 | bool RobotBase::IsOperatorControl() const { return m_ds.IsOperatorControl(); } |
| 113 | |
| 114 | bool RobotBase::IsTest() const { return m_ds.IsTest(); } |
| 115 | |
| 116 | bool RobotBase::IsNewDataAvailable() const { return m_ds.IsNewControlData(); } |
| 117 | |
| 118 | std::thread::id RobotBase::GetThreadId() { return m_threadId; } |
| 119 | |
| 120 | RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) { |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 121 | m_threadId = std::this_thread::get_id(); |
| 122 | |
| 123 | SetupCameraServerShared(); |
| 124 | |
| 125 | auto inst = nt::NetworkTableInstance::GetDefault(); |
| 126 | inst.SetNetworkIdentity("Robot"); |
| 127 | inst.StartServer("/home/lvuser/networktables.ini"); |
| 128 | |
| 129 | SmartDashboard::init(); |
| 130 | |
| 131 | if (IsReal()) { |
| 132 | std::FILE* file = nullptr; |
| 133 | file = std::fopen("/tmp/frc_versions/FRC_Lib_Version.ini", "w"); |
| 134 | |
| 135 | if (file != nullptr) { |
| 136 | std::fputs("C++ ", file); |
| 137 | std::fputs(GetWPILibVersion(), file); |
| 138 | std::fclose(file); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // First and one-time initialization |
| 143 | inst.GetTable("LiveWindow") |
| 144 | ->GetSubTable(".status") |
| 145 | ->GetEntry("LW Enabled") |
| 146 | .SetBoolean(false); |
| 147 | |
| 148 | LiveWindow::GetInstance()->SetEnabled(false); |
| 149 | } |
| 150 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 151 | RobotBase::RobotBase(RobotBase&&) noexcept |
| 152 | : m_ds(DriverStation::GetInstance()) {} |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 153 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 154 | RobotBase::~RobotBase() {} |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 155 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 156 | RobotBase& RobotBase::operator=(RobotBase&&) noexcept { return *this; } |