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