blob: bc316f4d326b0d906fa9a22209c355463e741eb2 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2008-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 "frc/RobotBase.h"
9
10#include <cstdio>
11
12#include <cameraserver/CameraServerShared.h>
13#include <cscore.h>
14#include <hal/HAL.h>
15#include <networktables/NetworkTableInstance.h>
16
17#include "WPILibVersion.h"
18#include "frc/DriverStation.h"
19#include "frc/RobotState.h"
20#include "frc/Utility.h"
21#include "frc/WPIErrors.h"
22#include "frc/livewindow/LiveWindow.h"
23#include "frc/smartdashboard/SmartDashboard.h"
24
25using namespace frc;
26
27int frc::RunHALInitialization() {
28 if (!HAL_Initialize(500, 0)) {
29 wpi::errs() << "FATAL ERROR: HAL could not be initialized\n";
30 return -1;
31 }
32 HAL_Report(HALUsageReporting::kResourceType_Language,
33 HALUsageReporting::kLanguage_CPlusPlus);
34 wpi::outs() << "\n********** Robot program starting **********\n";
35 return 0;
36}
37
38std::thread::id RobotBase::m_threadId;
39
40namespace {
41class WPILibCameraServerShared : public frc::CameraServerShared {
42 public:
43 void ReportUsbCamera(int id) override {
44 HAL_Report(HALUsageReporting::kResourceType_UsbCamera, id);
45 }
46 void ReportAxisCamera(int id) override {
47 HAL_Report(HALUsageReporting::kResourceType_AxisCamera, id);
48 }
49 void ReportVideoServer(int id) override {
50 HAL_Report(HALUsageReporting::kResourceType_PCVideoServer, id);
51 }
52 void SetCameraServerError(const wpi::Twine& error) override {
53 wpi_setGlobalWPIErrorWithContext(CameraServerError, error);
54 }
55 void SetVisionRunnerError(const wpi::Twine& error) override {
56 wpi_setGlobalErrorWithContext(-1, error);
57 }
58 void ReportDriverStationError(const wpi::Twine& error) override {
59 DriverStation::ReportError(error);
60 }
61 std::pair<std::thread::id, bool> GetRobotMainThreadId() const override {
62 return std::make_pair(RobotBase::GetThreadId(), true);
63 }
64};
65} // namespace
66
67static void SetupCameraServerShared() {
68 SetCameraServerShared(std::make_unique<WPILibCameraServerShared>());
69}
70
71bool RobotBase::IsEnabled() const { return m_ds.IsEnabled(); }
72
73bool RobotBase::IsDisabled() const { return m_ds.IsDisabled(); }
74
75bool RobotBase::IsAutonomous() const { return m_ds.IsAutonomous(); }
76
77bool RobotBase::IsOperatorControl() const { return m_ds.IsOperatorControl(); }
78
79bool RobotBase::IsTest() const { return m_ds.IsTest(); }
80
81bool RobotBase::IsNewDataAvailable() const { return m_ds.IsNewControlData(); }
82
83std::thread::id RobotBase::GetThreadId() { return m_threadId; }
84
85RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
86 if (!HAL_Initialize(500, 0)) {
87 wpi::errs() << "FATAL ERROR: HAL could not be initialized\n";
88 wpi::errs().flush();
89 std::terminate();
90 }
91 m_threadId = std::this_thread::get_id();
92
93 SetupCameraServerShared();
94
95 auto inst = nt::NetworkTableInstance::GetDefault();
96 inst.SetNetworkIdentity("Robot");
97 inst.StartServer("/home/lvuser/networktables.ini");
98
99 SmartDashboard::init();
100
101 if (IsReal()) {
102 std::FILE* file = nullptr;
103 file = std::fopen("/tmp/frc_versions/FRC_Lib_Version.ini", "w");
104
105 if (file != nullptr) {
106 std::fputs("C++ ", file);
107 std::fputs(GetWPILibVersion(), file);
108 std::fclose(file);
109 }
110 }
111
112 // First and one-time initialization
113 inst.GetTable("LiveWindow")
114 ->GetSubTable(".status")
115 ->GetEntry("LW Enabled")
116 .SetBoolean(false);
117
118 LiveWindow::GetInstance()->SetEnabled(false);
119}
120
121RobotBase::RobotBase(RobotBase&&) : m_ds(DriverStation::GetInstance()) {}
122
123RobotBase::~RobotBase() { cs::Shutdown(); }
124
125RobotBase& RobotBase::operator=(RobotBase&&) { return *this; }