2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/RobotBase.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-10-27 08:37:30 -07:00
|
|
|
#ifdef __FRC_ROBORIO__
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-06-05 07:33:37 -07:00
|
|
|
#include <cstdio>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <cameraserver/CameraServerShared.h>
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
#include <hal/HALBase.h>
|
2017-12-07 23:34:29 -08:00
|
|
|
#include <networktables/NetworkTableInstance.h>
|
2020-08-07 09:38:43 -07:00
|
|
|
#include <wpimath/MathShared.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2016-10-20 23:54:04 -07:00
|
|
|
#include "WPILibVersion.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/DriverStation.h"
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/RobotState.h"
|
|
|
|
|
#include "frc/livewindow/LiveWindow.h"
|
|
|
|
|
#include "frc/smartdashboard/SmartDashboard.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-10-27 08:37:30 -07:00
|
|
|
typedef void (*SetCameraServerSharedFP)(frc::CameraServerShared* shared);
|
|
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2018-10-29 12:49:17 -07:00
|
|
|
int frc::RunHALInitialization() {
|
|
|
|
|
if (!HAL_Initialize(500, 0)) {
|
|
|
|
|
wpi::errs() << "FATAL ERROR: HAL could not be initialized\n";
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Language,
|
2019-12-26 22:34:10 -06:00
|
|
|
HALUsageReporting::kLanguage_CPlusPlus, 0, GetWPILibVersion());
|
2018-10-29 12:49:17 -07:00
|
|
|
wpi::outs() << "\n********** Robot program starting **********\n";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-21 21:58:42 -08:00
|
|
|
std::thread::id RobotBase::m_threadId;
|
|
|
|
|
|
2018-04-29 13:29:07 -07:00
|
|
|
namespace {
|
|
|
|
|
class WPILibCameraServerShared : public frc::CameraServerShared {
|
|
|
|
|
public:
|
|
|
|
|
void ReportUsbCamera(int id) override {
|
2018-07-25 19:58:05 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_UsbCamera, id);
|
2018-04-29 13:29:07 -07:00
|
|
|
}
|
|
|
|
|
void ReportAxisCamera(int id) override {
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_AxisCamera, id);
|
|
|
|
|
}
|
|
|
|
|
void ReportVideoServer(int id) override {
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PCVideoServer, id);
|
|
|
|
|
}
|
2018-07-29 12:53:41 -07:00
|
|
|
void SetCameraServerError(const wpi::Twine& error) override {
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_ReportError(err::CameraServerError, error);
|
2018-04-29 13:29:07 -07:00
|
|
|
}
|
2018-07-29 12:53:41 -07:00
|
|
|
void SetVisionRunnerError(const wpi::Twine& error) override {
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_ReportError(-1, error);
|
2018-04-29 13:29:07 -07:00
|
|
|
}
|
2018-07-29 12:53:41 -07:00
|
|
|
void ReportDriverStationError(const wpi::Twine& error) override {
|
2018-04-29 13:29:07 -07:00
|
|
|
DriverStation::ReportError(error);
|
|
|
|
|
}
|
|
|
|
|
std::pair<std::thread::id, bool> GetRobotMainThreadId() const override {
|
|
|
|
|
return std::make_pair(RobotBase::GetThreadId(), true);
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-08-06 23:57:39 -07:00
|
|
|
class WPILibMathShared : public wpi::math::MathShared {
|
|
|
|
|
public:
|
|
|
|
|
void ReportError(const wpi::Twine& error) override {
|
|
|
|
|
DriverStation::ReportError(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReportUsage(wpi::math::MathUsageId id, int count) override {
|
|
|
|
|
switch (id) {
|
|
|
|
|
case wpi::math::MathUsageId::kKinematics_DifferentialDrive:
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Kinematics,
|
|
|
|
|
HALUsageReporting::kKinematics_DifferentialDrive);
|
|
|
|
|
break;
|
|
|
|
|
case wpi::math::MathUsageId::kKinematics_MecanumDrive:
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Kinematics,
|
|
|
|
|
HALUsageReporting::kKinematics_MecanumDrive);
|
|
|
|
|
break;
|
|
|
|
|
case wpi::math::MathUsageId::kKinematics_SwerveDrive:
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Kinematics,
|
|
|
|
|
HALUsageReporting::kKinematics_SwerveDrive);
|
|
|
|
|
break;
|
|
|
|
|
case wpi::math::MathUsageId::kTrajectory_TrapezoidProfile:
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_TrapezoidProfile, count);
|
|
|
|
|
break;
|
|
|
|
|
case wpi::math::MathUsageId::kFilter_Linear:
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_LinearFilter, count);
|
|
|
|
|
break;
|
|
|
|
|
case wpi::math::MathUsageId::kOdometry_DifferentialDrive:
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Odometry,
|
|
|
|
|
HALUsageReporting::kOdometry_DifferentialDrive);
|
|
|
|
|
break;
|
|
|
|
|
case wpi::math::MathUsageId::kOdometry_SwerveDrive:
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Odometry,
|
|
|
|
|
HALUsageReporting::kOdometry_SwerveDrive);
|
|
|
|
|
break;
|
|
|
|
|
case wpi::math::MathUsageId::kOdometry_MecanumDrive:
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Odometry,
|
|
|
|
|
HALUsageReporting::kOdometry_MecanumDrive);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-04-29 13:29:07 -07:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
static void SetupCameraServerShared() {
|
2019-10-27 08:37:30 -07:00
|
|
|
#ifdef __FRC_ROBORIO__
|
|
|
|
|
#ifdef DYNAMIC_CAMERA_SERVER
|
|
|
|
|
#ifdef DYNAMIC_CAMERA_SERVER_DEBUG
|
|
|
|
|
auto cameraServerLib = dlopen("libcameraserverd.so", RTLD_NOW);
|
|
|
|
|
#else
|
|
|
|
|
auto cameraServerLib = dlopen("libcameraserver.so", RTLD_NOW);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (!cameraServerLib) {
|
|
|
|
|
wpi::outs() << "Camera Server Library Not Found\n";
|
|
|
|
|
wpi::outs().flush();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto symbol = dlsym(cameraServerLib, "CameraServer_SetCameraServerShared");
|
|
|
|
|
if (symbol) {
|
|
|
|
|
auto setCameraServerShared = (SetCameraServerSharedFP)symbol;
|
|
|
|
|
setCameraServerShared(new WPILibCameraServerShared{});
|
|
|
|
|
} else {
|
|
|
|
|
wpi::outs() << "Camera Server Shared Symbol Missing\n";
|
|
|
|
|
wpi::outs().flush();
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
CameraServer_SetCameraServerShared(new WPILibCameraServerShared{});
|
|
|
|
|
#endif
|
|
|
|
|
#else
|
|
|
|
|
wpi::outs() << "Not loading CameraServerShared\n";
|
|
|
|
|
wpi::outs().flush();
|
|
|
|
|
#endif
|
2018-04-29 13:29:07 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 23:57:39 -07:00
|
|
|
static void SetupMathShared() {
|
|
|
|
|
wpi::math::MathSharedStore::SetMathShared(
|
|
|
|
|
std::make_unique<WPILibMathShared>());
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RobotBase::IsEnabled() const {
|
|
|
|
|
return m_ds.IsEnabled();
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RobotBase::IsDisabled() const {
|
|
|
|
|
return m_ds.IsDisabled();
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RobotBase::IsAutonomous() const {
|
|
|
|
|
return m_ds.IsAutonomous();
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-08-29 16:32:19 -04:00
|
|
|
bool RobotBase::IsAutonomousEnabled() const {
|
|
|
|
|
return m_ds.IsAutonomousEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RobotBase::IsOperatorControl() const {
|
|
|
|
|
return m_ds.IsOperatorControl();
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-08-29 16:32:19 -04:00
|
|
|
bool RobotBase::IsOperatorControlEnabled() const {
|
|
|
|
|
return m_ds.IsOperatorControlEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RobotBase::IsTest() const {
|
|
|
|
|
return m_ds.IsTest();
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RobotBase::IsNewDataAvailable() const {
|
|
|
|
|
return m_ds.IsNewControlData();
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
std::thread::id RobotBase::GetThreadId() {
|
|
|
|
|
return m_threadId;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
|
2016-12-21 21:58:42 -08:00
|
|
|
m_threadId = std::this_thread::get_id();
|
|
|
|
|
|
2018-04-29 13:29:07 -07:00
|
|
|
SetupCameraServerShared();
|
2020-08-06 23:57:39 -07:00
|
|
|
SetupMathShared();
|
2018-04-29 13:29:07 -07:00
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
auto inst = nt::NetworkTableInstance::GetDefault();
|
|
|
|
|
inst.SetNetworkIdentity("Robot");
|
2019-12-29 18:55:02 -06:00
|
|
|
#ifdef __FRC_ROBORIO__
|
2017-09-02 00:17:43 -07:00
|
|
|
inst.StartServer("/home/lvuser/networktables.ini");
|
2019-12-29 18:55:02 -06:00
|
|
|
#else
|
|
|
|
|
inst.StartServer();
|
|
|
|
|
#endif
|
2015-08-13 23:17:19 -07:00
|
|
|
|
2017-01-01 00:07:22 -08:00
|
|
|
SmartDashboard::init();
|
|
|
|
|
|
2018-12-26 22:59:49 -08:00
|
|
|
if (IsReal()) {
|
|
|
|
|
std::FILE* file = nullptr;
|
|
|
|
|
file = std::fopen("/tmp/frc_versions/FRC_Lib_Version.ini", "w");
|
|
|
|
|
|
|
|
|
|
if (file != nullptr) {
|
|
|
|
|
std::fputs("C++ ", file);
|
|
|
|
|
std::fputs(GetWPILibVersion(), file);
|
|
|
|
|
std::fclose(file);
|
|
|
|
|
}
|
2015-11-15 14:49:50 -08:00
|
|
|
}
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
// First and one-time initialization
|
2017-09-02 00:17:43 -07:00
|
|
|
inst.GetTable("LiveWindow")
|
2017-10-11 23:27:49 -04:00
|
|
|
->GetSubTable(".status")
|
2017-09-02 00:17:43 -07:00
|
|
|
->GetEntry("LW Enabled")
|
|
|
|
|
.SetBoolean(false);
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2021-05-09 18:16:07 -07:00
|
|
|
LiveWindow::GetInstance()->SetEnabled(false);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2018-09-24 00:08:25 -07:00
|
|
|
|
2019-05-31 13:43:32 -07:00
|
|
|
RobotBase::RobotBase(RobotBase&&) noexcept
|
|
|
|
|
: m_ds(DriverStation::GetInstance()) {}
|
2018-09-24 00:08:25 -07:00
|
|
|
|
2020-12-28 00:37:33 -08:00
|
|
|
RobotBase::~RobotBase() = default;
|
2018-11-07 00:02:28 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
RobotBase& RobotBase::operator=(RobotBase&&) noexcept {
|
|
|
|
|
return *this;
|
|
|
|
|
}
|