diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp index 8d2d6d2bd4..9a94d3d026 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp @@ -21,10 +21,9 @@ Command::~Command() { CommandScheduler::GetInstance().Cancel(this); } -Command::Command(const Command& rhs) : ErrorBase(rhs) {} +Command::Command(const Command& rhs) = default; Command& Command::operator=(const Command& rhs) { - ErrorBase::operator=(rhs); m_isGrouped = false; return *this; } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandGroupBase.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandGroupBase.cpp index c09a7dd113..a59fc0307c 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandGroupBase.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandGroupBase.cpp @@ -4,19 +4,15 @@ #include "frc2/command/CommandGroupBase.h" -#include - using namespace frc2; bool CommandGroupBase::RequireUngrouped(Command& command) { if (command.IsGrouped()) { - wpi_setGlobalWPIErrorWithContext( - CommandIllegalUse, + throw FRC_MakeError( + frc::err::CommandIllegalUse, "Commands cannot be added to more than one CommandGroup"); - return false; - } else { - return true; } + return true; } bool CommandGroupBase::RequireUngrouped( @@ -26,8 +22,8 @@ bool CommandGroupBase::RequireUngrouped( allUngrouped &= !command.get()->IsGrouped(); } if (!allUngrouped) { - wpi_setGlobalWPIErrorWithContext( - CommandIllegalUse, + throw FRC_MakeError( + frc::err::CommandIllegalUse, "Commands cannot be added to more than one CommandGroup"); } return allUngrouped; @@ -40,8 +36,8 @@ bool CommandGroupBase::RequireUngrouped( allUngrouped &= !command->IsGrouped(); } if (!allUngrouped) { - wpi_setGlobalWPIErrorWithContext( - CommandIllegalUse, + throw FRC_MakeError( + frc::err::CommandIllegalUse, "Commands cannot be added to more than one CommandGroup"); } return allUngrouped; diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp index 57e33a222a..f08c21fd22 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -112,9 +111,9 @@ void CommandScheduler::Schedule(bool interruptible, Command* command) { } if (command->IsGrouped()) { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "A command that is part of a command group " - "cannot be independently scheduled"); + throw FRC_MakeError(frc::err::CommandIllegalUse, + "A command that is part of a command group " + "cannot be independently scheduled"); return; } if (m_impl->disabled || diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelCommandGroup.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelCommandGroup.cpp index c243ed5429..d6169d7574 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelCommandGroup.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelCommandGroup.cpp @@ -65,9 +65,9 @@ void ParallelCommandGroup::AddCommands( } if (isRunning) { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "Commands cannot be added to a CommandGroup " - "while the group is running"); + throw FRC_MakeError(frc::err::CommandIllegalUse, + "Commands cannot be added to a CommandGroup " + "while the group is running"); } for (auto&& command : commands) { @@ -77,10 +77,9 @@ void ParallelCommandGroup::AddCommands( m_runWhenDisabled &= command->RunsWhenDisabled(); m_commands.emplace_back(std::move(command), false); } else { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "Multiple commands in a parallel group cannot " - "require the same subsystems"); - return; + throw FRC_MakeError(frc::err::CommandIllegalUse, + "Multiple commands in a parallel group cannot " + "require the same subsystems"); } } } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelDeadlineGroup.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelDeadlineGroup.cpp index d544a6b905..16104e2513 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelDeadlineGroup.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelDeadlineGroup.cpp @@ -60,9 +60,9 @@ void ParallelDeadlineGroup::AddCommands( } if (!m_finished) { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "Commands cannot be added to a CommandGroup " - "while the group is running"); + throw FRC_MakeError(frc::err::CommandIllegalUse, + "Commands cannot be added to a CommandGroup " + "while the group is running"); } for (auto&& command : commands) { @@ -72,10 +72,9 @@ void ParallelDeadlineGroup::AddCommands( m_runWhenDisabled &= command->RunsWhenDisabled(); m_commands.emplace_back(std::move(command), false); } else { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "Multiple commands in a parallel group cannot " - "require the same subsystems"); - return; + throw FRC_MakeError(frc::err::CommandIllegalUse, + "Multiple commands in a parallel group cannot " + "require the same subsystems"); } } } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelRaceGroup.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelRaceGroup.cpp index 3bee59845c..38e0494b79 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelRaceGroup.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/ParallelRaceGroup.cpp @@ -50,9 +50,9 @@ void ParallelRaceGroup::AddCommands( } if (isRunning) { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "Commands cannot be added to a CommandGroup " - "while the group is running"); + throw FRC_MakeError(frc::err::CommandIllegalUse, + "Commands cannot be added to a CommandGroup " + "while the group is running"); } for (auto&& command : commands) { @@ -62,10 +62,9 @@ void ParallelRaceGroup::AddCommands( m_runWhenDisabled &= command->RunsWhenDisabled(); m_commands.emplace_back(std::move(command)); } else { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "Multiple commands in a parallel group cannot " - "require the same subsystems"); - return; + throw FRC_MakeError(frc::err::CommandIllegalUse, + "Multiple commands in a parallel group cannot " + "require the same subsystems"); } } } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/SequentialCommandGroup.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/SequentialCommandGroup.cpp index 766d0cefa4..a02870724a 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/SequentialCommandGroup.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/SequentialCommandGroup.cpp @@ -60,9 +60,9 @@ void SequentialCommandGroup::AddCommands( } if (m_currentCommandIndex != invalid_index) { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "Commands cannot be added to a CommandGroup " - "while the group is running"); + throw FRC_MakeError(frc::err::CommandIllegalUse, + "Commands cannot be added to a CommandGroup " + "while the group is running"); } for (auto&& command : commands) { diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h index bc5edb59fe..e1177ad710 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -46,10 +45,10 @@ class ProxyScheduleCommand; * @see CommandScheduler * @see CommandHelper */ -class Command : public frc::ErrorBase { +class Command { public: Command() = default; - ~Command() override; + virtual ~Command(); Command(const Command&); Command& operator=(const Command&); diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/CommandScheduler.h b/wpilibNewCommands/src/main/native/include/frc2/command/CommandScheduler.h index f9f5f37984..4719524ded 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/CommandScheduler.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/CommandScheduler.h @@ -8,8 +8,7 @@ #include #include -#include -#include +#include #include #include #include @@ -29,7 +28,6 @@ class Subsystem; * methods to be called and for their default commands to be scheduled. */ class CommandScheduler final : public frc::Sendable, - public frc::ErrorBase, public frc::SendableHelper { public: /** @@ -182,14 +180,12 @@ class CommandScheduler final : public frc::Sendable, Command, std::remove_reference_t>>> void SetDefaultCommand(Subsystem* subsystem, T&& defaultCommand) { if (!defaultCommand.HasRequirement(subsystem)) { - wpi_setWPIErrorWithContext( - CommandIllegalUse, "Default commands must require their subsystem!"); - return; + throw FRC_MakeError(frc::err::CommandIllegalUse, + "Default commands must require their subsystem!"); } if (defaultCommand.IsFinished()) { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "Default commands should not end!"); - return; + throw FRC_MakeError(frc::err::CommandIllegalUse, + "Default commands should not end!"); } SetDefaultCommandImpl(subsystem, std::make_unique>( diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/SequentialCommandGroup.h b/wpilibNewCommands/src/main/native/include/frc2/command/SequentialCommandGroup.h index 843e096cc7..253abb28c1 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/SequentialCommandGroup.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/SequentialCommandGroup.h @@ -15,8 +15,6 @@ #include #include -#include -#include #include #include "frc2/command/CommandGroupBase.h" diff --git a/wpilibNewCommands/src/test/native/cpp/frc2/command/CommandRequirementsTest.cpp b/wpilibNewCommands/src/test/native/cpp/frc2/command/CommandRequirementsTest.cpp index ad6a09b91e..cf673d8bc6 100644 --- a/wpilibNewCommands/src/test/native/cpp/frc2/command/CommandRequirementsTest.cpp +++ b/wpilibNewCommands/src/test/native/cpp/frc2/command/CommandRequirementsTest.cpp @@ -2,6 +2,8 @@ // 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. +#include + #include "CommandTestBase.h" #include "frc2/command/CommandScheduler.h" #include "frc2/command/ConditionalCommand.h" @@ -71,11 +73,9 @@ TEST_F(CommandRequirementsTest, RequirementUninterruptibleTest) { TEST_F(CommandRequirementsTest, DefaultCommandRequirementErrorTest) { TestSubsystem requirement1; - ErrorConfirmer confirmer("require"); MockCommand command1; - requirement1.SetDefaultCommand(std::move(command1)); - - EXPECT_TRUE(requirement1.GetDefaultCommand() == nullptr); + ASSERT_THROW(requirement1.SetDefaultCommand(std::move(command1)), + frc::RuntimeError); } diff --git a/wpilibNewCommands/src/test/native/cpp/frc2/command/CommandTestBase.h b/wpilibNewCommands/src/test/native/cpp/frc2/command/CommandTestBase.h index 07b1b97b5e..a3890f6ed6 100644 --- a/wpilibNewCommands/src/test/native/cpp/frc2/command/CommandTestBase.h +++ b/wpilibNewCommands/src/test/native/cpp/frc2/command/CommandTestBase.h @@ -9,7 +9,6 @@ #include -#include "ErrorConfirmer.h" #include "frc2/command/CommandGroupBase.h" #include "frc2/command/CommandScheduler.h" #include "frc2/command/SetUtilities.h" diff --git a/wpilibNewCommands/src/test/native/cpp/frc2/command/ErrorConfirmer.cpp b/wpilibNewCommands/src/test/native/cpp/frc2/command/ErrorConfirmer.cpp deleted file mode 100644 index 68f6a42534..0000000000 --- a/wpilibNewCommands/src/test/native/cpp/frc2/command/ErrorConfirmer.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// 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. - -#include "ErrorConfirmer.h" - -#include - -ErrorConfirmer* ErrorConfirmer::instance; - -int32_t ErrorConfirmer::HandleError(HAL_Bool isError, int32_t errorCode, - HAL_Bool isLVCode, const char* details, - const char* location, const char* callStack, - HAL_Bool printMsg) { - if (std::regex_search(details, std::regex(instance->m_msg))) { - instance->ConfirmError(); - } - return 1; -} diff --git a/wpilibNewCommands/src/test/native/cpp/frc2/command/ErrorConfirmer.h b/wpilibNewCommands/src/test/native/cpp/frc2/command/ErrorConfirmer.h deleted file mode 100644 index e503ab95a9..0000000000 --- a/wpilibNewCommands/src/test/native/cpp/frc2/command/ErrorConfirmer.h +++ /dev/null @@ -1,38 +0,0 @@ -// 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. - -#pragma once - -#include - -#include "gmock/gmock.h" - -class ErrorConfirmer { - public: - explicit ErrorConfirmer(const char* msg) : m_msg(msg) { - if (instance != nullptr) { - return; - } - HALSIM_SetSendError(HandleError); - EXPECT_CALL(*this, ConfirmError()); - instance = this; - } - - ~ErrorConfirmer() { - HALSIM_SetSendError(nullptr); - instance = nullptr; - } - - MOCK_METHOD0(ConfirmError, void()); - - const char* m_msg; - - static int32_t HandleError(HAL_Bool isError, int32_t errorCode, - HAL_Bool isLVCode, const char* details, - const char* location, const char* callStack, - HAL_Bool printMsg); - - private: - static ErrorConfirmer* instance; -}; diff --git a/wpilibOldCommands/src/main/native/cpp/commands/Command.cpp b/wpilibOldCommands/src/main/native/cpp/commands/Command.cpp index b1a7554069..c2e9822ce9 100644 --- a/wpilibOldCommands/src/main/native/cpp/commands/Command.cpp +++ b/wpilibOldCommands/src/main/native/cpp/commands/Command.cpp @@ -6,9 +6,9 @@ #include +#include "frc/Errors.h" #include "frc/RobotState.h" #include "frc/Timer.h" -#include "frc/WPIErrors.h" #include "frc/commands/CommandGroup.h" #include "frc/commands/Scheduler.h" #include "frc/livewindow/LiveWindow.h" @@ -32,7 +32,7 @@ Command::Command(Subsystem& subsystem) : Command("", -1.0) { Command::Command(const wpi::Twine& name, double timeout) { // We use -1.0 to indicate no timeout. if (timeout < 0.0 && timeout != -1.0) { - wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0"); + throw FRC_MakeError(err::ParameterOutOfRange, "timeout < 0.0"); } m_timeout = timeout; @@ -77,15 +77,15 @@ void Command::Requires(Subsystem* subsystem) { if (subsystem != nullptr) { m_requirements.insert(subsystem); } else { - wpi_setWPIErrorWithContext(NullParameter, "subsystem"); + throw FRC_MakeError(err::NullParameter, "subsystem"); } } void Command::Start() { LockChanges(); if (m_parent != nullptr) { - wpi_setWPIErrorWithContext( - CommandIllegalUse, + throw FRC_MakeError( + err::CommandIllegalUse, "Can not start a command that is part of a command group"); } @@ -115,8 +115,8 @@ bool Command::Run() { void Command::Cancel() { if (m_parent != nullptr) { - wpi_setWPIErrorWithContext( - CommandIllegalUse, + throw FRC_MakeError( + err::CommandIllegalUse, "Can not cancel a command that is part of a command group"); } @@ -173,7 +173,7 @@ int Command::GetID() const { void Command::SetTimeout(double timeout) { if (timeout < 0.0) { - wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0"); + throw FRC_MakeError(err::ParameterOutOfRange, "timeout < 0.0"); } else { m_timeout = timeout; } @@ -185,21 +185,22 @@ bool Command::IsTimedOut() const { bool Command::AssertUnlocked(const std::string& message) { if (m_locked) { - std::string buf = - message + " after being started or being added to a command group"; - wpi_setWPIErrorWithContext(CommandIllegalUse, buf); - return false; + throw FRC_MakeError( + err::CommandIllegalUse, + message + + wpi::Twine{ + " after being started or being added to a command group"}); } return true; } void Command::SetParent(CommandGroup* parent) { if (parent == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "parent"); + throw FRC_MakeError(err::NullParameter, "parent"); } else if (m_parent != nullptr) { - wpi_setWPIErrorWithContext(CommandIllegalUse, - "Can not give command to a command group after " - "already being put in a command group"); + throw FRC_MakeError(err::CommandIllegalUse, + "Can not give command to a command group after " + "already being put in a command group"); } else { LockChanges(); m_parent = parent; diff --git a/wpilibOldCommands/src/main/native/cpp/commands/CommandGroup.cpp b/wpilibOldCommands/src/main/native/cpp/commands/CommandGroup.cpp index ff8ee7c985..f743724889 100644 --- a/wpilibOldCommands/src/main/native/cpp/commands/CommandGroup.cpp +++ b/wpilibOldCommands/src/main/native/cpp/commands/CommandGroup.cpp @@ -4,16 +4,15 @@ #include "frc/commands/CommandGroup.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" using namespace frc; CommandGroup::CommandGroup(const wpi::Twine& name) : Command(name) {} void CommandGroup::AddSequential(Command* command) { - if (command == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "command"); - return; + if (!command) { + throw FRC_MakeError(err::NullParameter, "command"); } if (!AssertUnlocked("Cannot add new command to command group")) { return; @@ -31,16 +30,14 @@ void CommandGroup::AddSequential(Command* command) { } void CommandGroup::AddSequential(Command* command, double timeout) { - if (command == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "command"); - return; + if (!command) { + throw FRC_MakeError(err::NullParameter, "command"); } if (!AssertUnlocked("Cannot add new command to command group")) { return; } if (timeout < 0.0) { - wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0"); - return; + throw FRC_MakeError(err::ParameterOutOfRange, "timeout < 0.0"); } m_commands.emplace_back(command, CommandGroupEntry::kSequence_InSequence, @@ -56,8 +53,8 @@ void CommandGroup::AddSequential(Command* command, double timeout) { } void CommandGroup::AddParallel(Command* command) { - if (command == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "command"); + if (!command) { + throw FRC_MakeError(err::NullParameter, "command"); return; } if (!AssertUnlocked("Cannot add new command to command group")) { @@ -76,16 +73,14 @@ void CommandGroup::AddParallel(Command* command) { } void CommandGroup::AddParallel(Command* command, double timeout) { - if (command == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "command"); - return; + if (!command) { + throw FRC_MakeError(err::NullParameter, "command"); } if (!AssertUnlocked("Cannot add new command to command group")) { return; } if (timeout < 0.0) { - wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0"); - return; + throw FRC_MakeError(err::ParameterOutOfRange, "timeout < 0.0"); } m_commands.emplace_back(command, CommandGroupEntry::kSequence_BranchChild, diff --git a/wpilibOldCommands/src/main/native/cpp/commands/Scheduler.cpp b/wpilibOldCommands/src/main/native/cpp/commands/Scheduler.cpp index 8b3803ea80..42bebb821e 100644 --- a/wpilibOldCommands/src/main/native/cpp/commands/Scheduler.cpp +++ b/wpilibOldCommands/src/main/native/cpp/commands/Scheduler.cpp @@ -13,7 +13,7 @@ #include #include -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/buttons/ButtonScheduler.h" #include "frc/commands/Command.h" #include "frc/commands/Subsystem.h" @@ -64,9 +64,8 @@ void Scheduler::AddButton(ButtonScheduler* button) { } void Scheduler::RegisterSubsystem(Subsystem* subsystem) { - if (subsystem == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "subsystem"); - return; + if (!subsystem) { + throw FRC_MakeError(err::NullParameter, "subsystem"); } m_impl->subsystems.insert(subsystem); } @@ -109,8 +108,8 @@ void Scheduler::Run() { for (auto& addition : m_impl->additions) { // Check to make sure no adding during adding if (m_impl->adding) { - wpi_setWPIErrorWithContext(IncompatibleState, - "Can not start command from cancel method"); + FRC_ReportError(warn::IncompatibleState, + "Can not start command from cancel method"); } else { m_impl->ProcessCommandAddition(addition); } @@ -122,8 +121,8 @@ void Scheduler::Run() { for (auto& subsystem : m_impl->subsystems) { if (subsystem->GetCurrentCommand() == nullptr) { if (m_impl->adding) { - wpi_setWPIErrorWithContext(IncompatibleState, - "Can not start command from cancel method"); + FRC_ReportError(warn::IncompatibleState, + "Can not start command from cancel method"); } else { m_impl->ProcessCommandAddition(subsystem->GetDefaultCommand()); } @@ -133,9 +132,8 @@ void Scheduler::Run() { } void Scheduler::Remove(Command* command) { - if (command == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "command"); - return; + if (!command) { + throw FRC_MakeError(err::NullParameter, "command"); } m_impl->Remove(command); diff --git a/wpilibOldCommands/src/main/native/cpp/commands/Subsystem.cpp b/wpilibOldCommands/src/main/native/cpp/commands/Subsystem.cpp index 3026af85e4..2ef307c610 100644 --- a/wpilibOldCommands/src/main/native/cpp/commands/Subsystem.cpp +++ b/wpilibOldCommands/src/main/native/cpp/commands/Subsystem.cpp @@ -4,7 +4,7 @@ #include "frc/commands/Subsystem.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/commands/Command.h" #include "frc/commands/Scheduler.h" #include "frc/livewindow/LiveWindow.h" @@ -24,9 +24,8 @@ void Subsystem::SetDefaultCommand(Command* command) { } else { const auto& reqs = command->GetRequirements(); if (std::find(reqs.begin(), reqs.end(), this) == reqs.end()) { - wpi_setWPIErrorWithContext( - CommandIllegalUse, "A default command must require the subsystem"); - return; + throw FRC_MakeError(err::CommandIllegalUse, + "A default command must require the subsystem"); } m_defaultCommand = command; diff --git a/wpilibOldCommands/src/main/native/include/frc/commands/Command.h b/wpilibOldCommands/src/main/native/include/frc/commands/Command.h index 167d2bf9c4..3863f85836 100644 --- a/wpilibOldCommands/src/main/native/include/frc/commands/Command.h +++ b/wpilibOldCommands/src/main/native/include/frc/commands/Command.h @@ -10,7 +10,6 @@ #include #include -#include "frc/ErrorBase.h" #include "frc/commands/Subsystem.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -43,9 +42,7 @@ class CommandGroup; * @see CommandGroup * @see Subsystem */ -class Command : public ErrorBase, - public Sendable, - public SendableHelper { +class Command : public Sendable, public SendableHelper { friend class CommandGroup; friend class Scheduler; diff --git a/wpilibOldCommands/src/main/native/include/frc/commands/Scheduler.h b/wpilibOldCommands/src/main/native/include/frc/commands/Scheduler.h index 0bdd4b8465..83372dc278 100644 --- a/wpilibOldCommands/src/main/native/include/frc/commands/Scheduler.h +++ b/wpilibOldCommands/src/main/native/include/frc/commands/Scheduler.h @@ -6,7 +6,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -16,9 +15,7 @@ class ButtonScheduler; class Command; class Subsystem; -class Scheduler : public ErrorBase, - public Sendable, - public SendableHelper { +class Scheduler : public Sendable, public SendableHelper { public: /** * Returns the Scheduler, creating it if one does not exist. diff --git a/wpilibOldCommands/src/main/native/include/frc/commands/Subsystem.h b/wpilibOldCommands/src/main/native/include/frc/commands/Subsystem.h index a2f9e5f250..fe3a997861 100644 --- a/wpilibOldCommands/src/main/native/include/frc/commands/Subsystem.h +++ b/wpilibOldCommands/src/main/native/include/frc/commands/Subsystem.h @@ -10,7 +10,6 @@ #include #include -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -18,9 +17,7 @@ namespace frc { class Command; -class Subsystem : public ErrorBase, - public Sendable, - public SendableHelper { +class Subsystem : public Sendable, public SendableHelper { friend class Scheduler; public: diff --git a/wpilibc/src/main/native/cpp/AddressableLED.cpp b/wpilibc/src/main/native/cpp/AddressableLED.cpp index 335982f395..db297ffe46 100644 --- a/wpilibc/src/main/native/cpp/AddressableLED.cpp +++ b/wpilibc/src/main/native/cpp/AddressableLED.cpp @@ -10,7 +10,7 @@ #include #include -#include "frc/WPIErrors.h" +#include "frc/Errors.h" using namespace frc; @@ -18,13 +18,13 @@ AddressableLED::AddressableLED(int port) { int32_t status = 0; m_pwmHandle = HAL_InitializePWMPort(HAL_GetPort(port), &status); - wpi_setHALErrorWithRange(status, 0, HAL_GetNumPWMChannels(), port); + FRC_CheckErrorStatus(status, "Port " + wpi::Twine{port}); if (m_pwmHandle == HAL_kInvalidHandle) { return; } m_handle = HAL_InitializeAddressableLED(m_pwmHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Port " + wpi::Twine{port}); if (m_handle == HAL_kInvalidHandle) { HAL_FreePWMPort(m_pwmHandle, &status); } @@ -36,12 +36,13 @@ AddressableLED::~AddressableLED() { HAL_FreeAddressableLED(m_handle); int32_t status = 0; HAL_FreePWMPort(m_pwmHandle, &status); + FRC_ReportError(status, "FreePWM"); } void AddressableLED::SetLength(int length) { int32_t status = 0; HAL_SetAddressableLEDLength(m_handle, length, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "length " + wpi::Twine{length}); } static_assert(sizeof(AddressableLED::LEDData) == sizeof(HAL_AddressableLEDData), @@ -51,14 +52,14 @@ void AddressableLED::SetData(wpi::ArrayRef ledData) { int32_t status = 0; HAL_WriteAddressableLEDData(m_handle, ledData.begin(), ledData.size(), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetData"); } void AddressableLED::SetData(std::initializer_list ledData) { int32_t status = 0; HAL_WriteAddressableLEDData(m_handle, ledData.begin(), ledData.size(), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetData"); } void AddressableLED::SetBitTiming(units::nanosecond_t lowTime0, @@ -69,25 +70,25 @@ void AddressableLED::SetBitTiming(units::nanosecond_t lowTime0, HAL_SetAddressableLEDBitTiming( m_handle, lowTime0.to(), highTime0.to(), lowTime1.to(), highTime1.to(), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetBitTiming"); } void AddressableLED::SetSyncTime(units::microsecond_t syncTime) { int32_t status = 0; HAL_SetAddressableLEDSyncTime(m_handle, syncTime.to(), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetSyncTime"); } void AddressableLED::Start() { int32_t status = 0; HAL_StartAddressableLEDOutput(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Start"); } void AddressableLED::Stop() { int32_t status = 0; HAL_StopAddressableLEDOutput(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Stop"); } void AddressableLED::LEDData::SetHSV(int h, int s, int v) { diff --git a/wpilibc/src/main/native/cpp/AnalogAccelerometer.cpp b/wpilibc/src/main/native/cpp/AnalogAccelerometer.cpp index e0e3ef71ac..d27048b6f0 100644 --- a/wpilibc/src/main/native/cpp/AnalogAccelerometer.cpp +++ b/wpilibc/src/main/native/cpp/AnalogAccelerometer.cpp @@ -7,7 +7,7 @@ #include #include "frc/Base.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -20,20 +20,18 @@ AnalogAccelerometer::AnalogAccelerometer(int channel) AnalogAccelerometer::AnalogAccelerometer(AnalogInput* channel) : m_analogInput(channel, NullDeleter()) { - if (channel == nullptr) { - wpi_setWPIError(NullParameter); - } else { - InitAccelerometer(); + if (!channel) { + throw FRC_MakeError(err::NullParameter, "channel"); } + InitAccelerometer(); } AnalogAccelerometer::AnalogAccelerometer(std::shared_ptr channel) : m_analogInput(channel) { - if (channel == nullptr) { - wpi_setWPIError(NullParameter); - } else { - InitAccelerometer(); + if (!channel) { + throw FRC_MakeError(err::NullParameter, "channel"); } + InitAccelerometer(); } double AnalogAccelerometer::GetAcceleration() const { diff --git a/wpilibc/src/main/native/cpp/AnalogGyro.cpp b/wpilibc/src/main/native/cpp/AnalogGyro.cpp index 86e5b461f6..504c27384b 100644 --- a/wpilibc/src/main/native/cpp/AnalogGyro.cpp +++ b/wpilibc/src/main/native/cpp/AnalogGyro.cpp @@ -13,8 +13,8 @@ #include "frc/AnalogInput.h" #include "frc/Base.h" +#include "frc/Errors.h" #include "frc/Timer.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -31,12 +31,11 @@ AnalogGyro::AnalogGyro(AnalogInput* channel) AnalogGyro::AnalogGyro(std::shared_ptr channel) : m_analog(channel) { - if (channel == nullptr) { - wpi_setWPIError(NullParameter); - } else { - InitGyro(); - Calibrate(); + if (!channel) { + throw FRC_MakeError(err::NullParameter, "channel"); } + InitGyro(); + Calibrate(); } AnalogGyro::AnalogGyro(int channel, int center, double offset) @@ -47,20 +46,15 @@ AnalogGyro::AnalogGyro(int channel, int center, double offset) AnalogGyro::AnalogGyro(std::shared_ptr channel, int center, double offset) : m_analog(channel) { - if (channel == nullptr) { - wpi_setWPIError(NullParameter); - } else { - InitGyro(); - int32_t status = 0; - HAL_SetAnalogGyroParameters(m_gyroHandle, kDefaultVoltsPerDegreePerSecond, - offset, center, &status); - if (status != 0) { - wpi_setHALError(status); - m_gyroHandle = HAL_kInvalidHandle; - return; - } - Reset(); + if (!channel) { + throw FRC_MakeError(err::NullParameter, "channel"); } + InitGyro(); + int32_t status = 0; + HAL_SetAnalogGyroParameters(m_gyroHandle, kDefaultVoltsPerDegreePerSecond, + offset, center, &status); + FRC_CheckErrorStatus(status, "SetAnalogGyroParameters"); + Reset(); } AnalogGyro::~AnalogGyro() { @@ -68,42 +62,30 @@ AnalogGyro::~AnalogGyro() { } double AnalogGyro::GetAngle() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double value = HAL_GetAnalogGyroAngle(m_gyroHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetAngle"); return value; } double AnalogGyro::GetRate() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double value = HAL_GetAnalogGyroRate(m_gyroHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetRate"); return value; } int AnalogGyro::GetCenter() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int value = HAL_GetAnalogGyroCenter(m_gyroHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetCenter"); return value; } double AnalogGyro::GetOffset() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double value = HAL_GetAnalogGyroOffset(m_gyroHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetOffset"); return value; } @@ -111,57 +93,35 @@ void AnalogGyro::SetSensitivity(double voltsPerDegreePerSecond) { int32_t status = 0; HAL_SetAnalogGyroVoltsPerDegreePerSecond(m_gyroHandle, voltsPerDegreePerSecond, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetSensitivity"); } void AnalogGyro::SetDeadband(double volts) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAnalogGyroDeadband(m_gyroHandle, volts, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetDeadband"); } void AnalogGyro::Reset() { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_ResetAnalogGyro(m_gyroHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Reset"); } void AnalogGyro::InitGyro() { - if (StatusIsFatal()) { - return; - } if (m_gyroHandle == HAL_kInvalidHandle) { int32_t status = 0; m_gyroHandle = HAL_InitializeAnalogGyro(m_analog->m_port, &status); if (status == PARAMETER_OUT_OF_RANGE) { - wpi_setWPIErrorWithContext(ParameterOutOfRange, - " channel (must be accumulator channel)"); - m_analog = nullptr; - m_gyroHandle = HAL_kInvalidHandle; - return; - } - if (status != 0) { - wpi_setHALError(status); - m_analog = nullptr; - m_gyroHandle = HAL_kInvalidHandle; - return; + throw FRC_MakeError(err::ParameterOutOfRange, + "channel must be accumulator channel"); } + FRC_CheckErrorStatus(status, "InitializeAnalogGyro"); } int32_t status = 0; HAL_SetupAnalogGyro(m_gyroHandle, &status); - if (status != 0) { - wpi_setHALError(status); - m_analog = nullptr; - m_gyroHandle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "SetupAnalogGyro"); HAL_Report(HALUsageReporting::kResourceType_Gyro, m_analog->GetChannel() + 1); @@ -170,12 +130,9 @@ void AnalogGyro::InitGyro() { } void AnalogGyro::Calibrate() { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_CalibrateAnalogGyro(m_gyroHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Calibrate"); } std::shared_ptr AnalogGyro::GetAnalogInput() const { diff --git a/wpilibc/src/main/native/cpp/AnalogInput.cpp b/wpilibc/src/main/native/cpp/AnalogInput.cpp index 9c6d0c2deb..e9191d8b92 100644 --- a/wpilibc/src/main/native/cpp/AnalogInput.cpp +++ b/wpilibc/src/main/native/cpp/AnalogInput.cpp @@ -10,9 +10,9 @@ #include #include +#include "frc/Errors.h" #include "frc/SensorUtil.h" #include "frc/Timer.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -20,9 +20,8 @@ using namespace frc; AnalogInput::AnalogInput(int channel) { if (!SensorUtil::CheckAnalogInputChannel(channel)) { - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, - "Analog Input " + wpi::Twine(channel)); - return; + throw FRC_MakeError(err::ChannelIndexOutOfRange, + "Analog Input " + wpi::Twine{channel}); } m_channel = channel; @@ -30,12 +29,7 @@ AnalogInput::AnalogInput(int channel) { HAL_PortHandle port = HAL_GetPort(channel); int32_t status = 0; m_port = HAL_InitializeAnalogInputPort(port, &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumAnalogInputs(), channel); - m_channel = std::numeric_limits::max(); - m_port = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{channel}); HAL_Report(HALUsageReporting::kResourceType_AnalogChannel, channel + 1); @@ -47,210 +41,151 @@ AnalogInput::~AnalogInput() { } int AnalogInput::GetValue() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int value = HAL_GetAnalogValue(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return value; } int AnalogInput::GetAverageValue() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int value = HAL_GetAnalogAverageValue(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return value; } double AnalogInput::GetVoltage() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double voltage = HAL_GetAnalogVoltage(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return voltage; } double AnalogInput::GetAverageVoltage() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double voltage = HAL_GetAnalogAverageVoltage(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return voltage; } int AnalogInput::GetChannel() const { - if (StatusIsFatal()) { - return 0; - } return m_channel; } void AnalogInput::SetAverageBits(int bits) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAnalogAverageBits(m_port, bits, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); } int AnalogInput::GetAverageBits() const { int32_t status = 0; int averageBits = HAL_GetAnalogAverageBits(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return averageBits; } void AnalogInput::SetOversampleBits(int bits) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAnalogOversampleBits(m_port, bits, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); } int AnalogInput::GetOversampleBits() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int oversampleBits = HAL_GetAnalogOversampleBits(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return oversampleBits; } int AnalogInput::GetLSBWeight() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int lsbWeight = HAL_GetAnalogLSBWeight(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return lsbWeight; } int AnalogInput::GetOffset() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int offset = HAL_GetAnalogOffset(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return offset; } bool AnalogInput::IsAccumulatorChannel() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; bool isAccum = HAL_IsAccumulatorChannel(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return isAccum; } void AnalogInput::InitAccumulator() { - if (StatusIsFatal()) { - return; - } m_accumulatorOffset = 0; int32_t status = 0; HAL_InitAccumulator(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); } void AnalogInput::SetAccumulatorInitialValue(int64_t initialValue) { - if (StatusIsFatal()) { - return; - } m_accumulatorOffset = initialValue; } void AnalogInput::ResetAccumulator() { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_ResetAccumulator(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); - if (!StatusIsFatal()) { - // Wait until the next sample, so the next call to GetAccumulator*() - // won't have old values. - const double sampleTime = 1.0 / GetSampleRate(); - const double overSamples = 1 << GetOversampleBits(); - const double averageSamples = 1 << GetAverageBits(); - Wait(sampleTime * overSamples * averageSamples); - } + // Wait until the next sample, so the next call to GetAccumulator*() + // won't have old values. + const double sampleTime = 1.0 / GetSampleRate(); + const double overSamples = 1 << GetOversampleBits(); + const double averageSamples = 1 << GetAverageBits(); + Wait(sampleTime * overSamples * averageSamples); } void AnalogInput::SetAccumulatorCenter(int center) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAccumulatorCenter(m_port, center, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); } void AnalogInput::SetAccumulatorDeadband(int deadband) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAccumulatorDeadband(m_port, deadband, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); } int64_t AnalogInput::GetAccumulatorValue() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int64_t value = HAL_GetAccumulatorValue(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return value + m_accumulatorOffset; } int64_t AnalogInput::GetAccumulatorCount() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int64_t count = HAL_GetAccumulatorCount(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); return count; } void AnalogInput::GetAccumulatorOutput(int64_t& value, int64_t& count) const { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_GetAccumulatorOutput(m_port, &value, &count, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Analog Input " + wpi::Twine{m_channel}); value += m_accumulatorOffset; } void AnalogInput::SetSampleRate(double samplesPerSecond) { int32_t status = 0; HAL_SetAnalogSampleRate(samplesPerSecond, &status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "SetSampleRate"); } double AnalogInput::GetSampleRate() { int32_t status = 0; double sampleRate = HAL_GetAnalogSampleRate(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetSampleRate"); return sampleRate; } diff --git a/wpilibc/src/main/native/cpp/AnalogOutput.cpp b/wpilibc/src/main/native/cpp/AnalogOutput.cpp index 1c3e2b061a..1c86d17234 100644 --- a/wpilibc/src/main/native/cpp/AnalogOutput.cpp +++ b/wpilibc/src/main/native/cpp/AnalogOutput.cpp @@ -12,8 +12,8 @@ #include #include +#include "frc/Errors.h" #include "frc/SensorUtil.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -21,11 +21,8 @@ using namespace frc; AnalogOutput::AnalogOutput(int channel) { if (!SensorUtil::CheckAnalogOutputChannel(channel)) { - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, - "analog output " + wpi::Twine(channel)); - m_channel = std::numeric_limits::max(); - m_port = HAL_kInvalidHandle; - return; + throw FRC_MakeError(err::ChannelIndexOutOfRange, + "analog output " + wpi::Twine(channel)); } m_channel = channel; @@ -33,12 +30,7 @@ AnalogOutput::AnalogOutput(int channel) { HAL_PortHandle port = HAL_GetPort(m_channel); int32_t status = 0; m_port = HAL_InitializeAnalogOutputPort(port, &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumAnalogOutputs(), channel); - m_channel = std::numeric_limits::max(); - m_port = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "analog output " + wpi::Twine(channel)); HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel + 1); SendableRegistry::GetInstance().AddLW(this, "AnalogOutput", m_channel); @@ -51,16 +43,13 @@ AnalogOutput::~AnalogOutput() { void AnalogOutput::SetVoltage(double voltage) { int32_t status = 0; HAL_SetAnalogOutput(m_port, voltage, &status); - - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetVoltage"); } double AnalogOutput::GetVoltage() const { int32_t status = 0; double voltage = HAL_GetAnalogOutput(m_port, &status); - - wpi_setHALError(status); - + FRC_CheckErrorStatus(status, "GetVoltage"); return voltage; } diff --git a/wpilibc/src/main/native/cpp/AnalogTrigger.cpp b/wpilibc/src/main/native/cpp/AnalogTrigger.cpp index 6b547b3e1e..2d19555297 100644 --- a/wpilibc/src/main/native/cpp/AnalogTrigger.cpp +++ b/wpilibc/src/main/native/cpp/AnalogTrigger.cpp @@ -11,7 +11,7 @@ #include "frc/AnalogInput.h" #include "frc/Base.h" #include "frc/DutyCycle.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableRegistry.h" using namespace frc; @@ -26,11 +26,7 @@ AnalogTrigger::AnalogTrigger(AnalogInput* input) { m_analogInput = input; int32_t status = 0; m_trigger = HAL_InitializeAnalogTrigger(input->m_port, &status); - if (status != 0) { - wpi_setHALError(status); - m_trigger = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "InitializeAnalogTrigger"); int index = GetIndex(); HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1); @@ -41,11 +37,7 @@ AnalogTrigger::AnalogTrigger(DutyCycle* input) { m_dutyCycle = input; int32_t status = 0; m_trigger = HAL_InitializeAnalogTriggerDutyCycle(input->m_handle, &status); - if (status != 0) { - wpi_setHALError(status); - m_trigger = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "InitializeAnalogTriggerDutyCycle"); int index = GetIndex(); HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1); @@ -55,6 +47,7 @@ AnalogTrigger::AnalogTrigger(DutyCycle* input) { AnalogTrigger::~AnalogTrigger() { int32_t status = 0; HAL_CleanAnalogTrigger(m_trigger, &status); + FRC_ReportError(status, "CleanAnalogTrigger"); if (m_ownsAnalog) { delete m_analogInput; @@ -62,16 +55,13 @@ AnalogTrigger::~AnalogTrigger() { } AnalogTrigger::AnalogTrigger(AnalogTrigger&& rhs) - : ErrorBase(std::move(rhs)), - SendableHelper(std::move(rhs)), - m_trigger(std::move(rhs.m_trigger)) { + : SendableHelper(std::move(rhs)), m_trigger(std::move(rhs.m_trigger)) { std::swap(m_analogInput, rhs.m_analogInput); std::swap(m_dutyCycle, rhs.m_dutyCycle); std::swap(m_ownsAnalog, rhs.m_ownsAnalog); } AnalogTrigger& AnalogTrigger::operator=(AnalogTrigger&& rhs) { - ErrorBase::operator=(std::move(rhs)); SendableHelper::operator=(std::move(rhs)); m_trigger = std::move(rhs.m_trigger); @@ -83,85 +73,58 @@ AnalogTrigger& AnalogTrigger::operator=(AnalogTrigger&& rhs) { } void AnalogTrigger::SetLimitsVoltage(double lower, double upper) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAnalogTriggerLimitsVoltage(m_trigger, lower, upper, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetLimitsVoltage"); } void AnalogTrigger::SetLimitsDutyCycle(double lower, double upper) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAnalogTriggerLimitsDutyCycle(m_trigger, lower, upper, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetLimitsDutyCycle"); } void AnalogTrigger::SetLimitsRaw(int lower, int upper) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAnalogTriggerLimitsRaw(m_trigger, lower, upper, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetLimitsRaw"); } void AnalogTrigger::SetAveraged(bool useAveragedValue) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAnalogTriggerAveraged(m_trigger, useAveragedValue, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetAveraged"); } void AnalogTrigger::SetFiltered(bool useFilteredValue) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetAnalogTriggerFiltered(m_trigger, useFilteredValue, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetFiltered"); } int AnalogTrigger::GetIndex() const { - if (StatusIsFatal()) { - return -1; - } int32_t status = 0; auto ret = HAL_GetAnalogTriggerFPGAIndex(m_trigger, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetIndex"); return ret; } bool AnalogTrigger::GetInWindow() { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; bool result = HAL_GetAnalogTriggerInWindow(m_trigger, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetInWindow"); return result; } bool AnalogTrigger::GetTriggerState() { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; bool result = HAL_GetAnalogTriggerTriggerState(m_trigger, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetTriggerState"); return result; } std::shared_ptr AnalogTrigger::CreateOutput( AnalogTriggerType type) const { - if (StatusIsFatal()) { - return nullptr; - } return std::shared_ptr( new AnalogTriggerOutput(*this, type), NullDeleter()); } diff --git a/wpilibc/src/main/native/cpp/AnalogTriggerOutput.cpp b/wpilibc/src/main/native/cpp/AnalogTriggerOutput.cpp index a9b8fb4b7f..d9f8ec7942 100644 --- a/wpilibc/src/main/native/cpp/AnalogTriggerOutput.cpp +++ b/wpilibc/src/main/native/cpp/AnalogTriggerOutput.cpp @@ -7,7 +7,7 @@ #include #include "frc/AnalogTrigger.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" using namespace frc; @@ -16,7 +16,7 @@ bool AnalogTriggerOutput::Get() const { bool result = HAL_GetAnalogTriggerOutput( m_trigger->m_trigger, static_cast(m_outputType), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Get"); return result; } diff --git a/wpilibc/src/main/native/cpp/BuiltInAccelerometer.cpp b/wpilibc/src/main/native/cpp/BuiltInAccelerometer.cpp index a482cf5535..4b8b51d63f 100644 --- a/wpilibc/src/main/native/cpp/BuiltInAccelerometer.cpp +++ b/wpilibc/src/main/native/cpp/BuiltInAccelerometer.cpp @@ -7,7 +7,7 @@ #include #include -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -23,8 +23,8 @@ BuiltInAccelerometer::BuiltInAccelerometer(Range range) { void BuiltInAccelerometer::SetRange(Range range) { if (range == kRange_16G) { - wpi_setWPIErrorWithContext( - ParameterOutOfRange, "16G range not supported (use k2G, k4G, or k8G)"); + throw FRC_MakeError(err::ParameterOutOfRange, + "16G range not supported (use k2G, k4G, or k8G)"); } HAL_SetAccelerometerActive(false); diff --git a/wpilibc/src/main/native/cpp/CAN.cpp b/wpilibc/src/main/native/cpp/CAN.cpp index ee2003bd7a..7c58475afa 100644 --- a/wpilibc/src/main/native/cpp/CAN.cpp +++ b/wpilibc/src/main/native/cpp/CAN.cpp @@ -11,17 +11,15 @@ #include #include +#include "frc/Errors.h" + using namespace frc; CAN::CAN(int deviceId) { int32_t status = 0; m_handle = HAL_InitializeCAN(kTeamManufacturer, deviceId, kTeamDeviceType, &status); - if (status != 0) { - wpi_setHALError(status); - m_handle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "device id " + wpi::Twine{deviceId}); HAL_Report(HALUsageReporting::kResourceType_CAN, deviceId + 1); } @@ -31,19 +29,14 @@ CAN::CAN(int deviceId, int deviceManufacturer, int deviceType) { m_handle = HAL_InitializeCAN( static_cast(deviceManufacturer), deviceId, static_cast(deviceType), &status); - if (status != 0) { - wpi_setHALError(status); - m_handle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "device id " + wpi::Twine{deviceId} + " mfg " + + wpi::Twine{deviceManufacturer} + " type " + + wpi::Twine{deviceType}); HAL_Report(HALUsageReporting::kResourceType_CAN, deviceId + 1); } CAN::~CAN() { - if (StatusIsFatal()) { - return; - } if (m_handle != HAL_kInvalidHandle) { HAL_CleanCAN(m_handle); m_handle = HAL_kInvalidHandle; @@ -53,20 +46,20 @@ CAN::~CAN() { void CAN::WritePacket(const uint8_t* data, int length, int apiId) { int32_t status = 0; HAL_WriteCANPacket(m_handle, data, length, apiId, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "WritePacket"); } void CAN::WritePacketRepeating(const uint8_t* data, int length, int apiId, int repeatMs) { int32_t status = 0; HAL_WriteCANPacketRepeating(m_handle, data, length, apiId, repeatMs, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "WritePacketRepeating"); } void CAN::WriteRTRFrame(int length, int apiId) { int32_t status = 0; HAL_WriteCANRTRFrame(m_handle, length, apiId, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "WriteRTRFrame"); } int CAN::WritePacketNoError(const uint8_t* data, int length, int apiId) { @@ -91,7 +84,7 @@ int CAN::WriteRTRFrameNoError(int length, int apiId) { void CAN::StopPacketRepeating(int apiId) { int32_t status = 0; HAL_StopCANPacketRepeating(m_handle, apiId, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "StopPacketRepeating"); } bool CAN::ReadPacketNew(int apiId, CANData* data) { @@ -102,7 +95,7 @@ bool CAN::ReadPacketNew(int apiId, CANData* data) { return false; } if (status != 0) { - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ReadPacketNew"); return false; } else { return true; @@ -117,7 +110,7 @@ bool CAN::ReadPacketLatest(int apiId, CANData* data) { return false; } if (status != 0) { - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ReadPacketLatest"); return false; } else { return true; @@ -133,7 +126,7 @@ bool CAN::ReadPacketTimeout(int apiId, int timeoutMs, CANData* data) { return false; } if (status != 0) { - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ReadPacketTimeout"); return false; } else { return true; diff --git a/wpilibc/src/main/native/cpp/Compressor.cpp b/wpilibc/src/main/native/cpp/Compressor.cpp index 88e5eb6415..d0654866af 100644 --- a/wpilibc/src/main/native/cpp/Compressor.cpp +++ b/wpilibc/src/main/native/cpp/Compressor.cpp @@ -9,7 +9,7 @@ #include #include -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -18,10 +18,7 @@ using namespace frc; Compressor::Compressor(int pcmID) : m_module(pcmID) { int32_t status = 0; m_compressorHandle = HAL_InitializeCompressor(m_module, &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumPCMModules(), pcmID); - return; - } + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); SetClosedLoopControl(true); HAL_Report(HALUsageReporting::kResourceType_Compressor, pcmID + 1); @@ -29,204 +26,96 @@ Compressor::Compressor(int pcmID) : m_module(pcmID) { } void Compressor::Start() { - if (StatusIsFatal()) { - return; - } SetClosedLoopControl(true); } void Compressor::Stop() { - if (StatusIsFatal()) { - return; - } SetClosedLoopControl(false); } bool Compressor::Enabled() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; - bool value; - - value = HAL_GetCompressor(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + bool value = HAL_GetCompressor(m_compressorHandle, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } bool Compressor::GetPressureSwitchValue() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; - bool value; - - value = HAL_GetCompressorPressureSwitch(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + bool value = HAL_GetCompressorPressureSwitch(m_compressorHandle, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } double Compressor::GetCompressorCurrent() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; - double value; - - value = HAL_GetCompressorCurrent(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + double value = HAL_GetCompressorCurrent(m_compressorHandle, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } void Compressor::SetClosedLoopControl(bool on) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; - HAL_SetCompressorClosedLoopControl(m_compressorHandle, on, &status); - - if (status) { - wpi_setWPIError(Timeout); - } + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); } bool Compressor::GetClosedLoopControl() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; - bool value; - - value = HAL_GetCompressorClosedLoopControl(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + bool value = HAL_GetCompressorClosedLoopControl(m_compressorHandle, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } bool Compressor::GetCompressorCurrentTooHighFault() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; - bool value; - - value = HAL_GetCompressorCurrentTooHighFault(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + bool value = + HAL_GetCompressorCurrentTooHighFault(m_compressorHandle, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } bool Compressor::GetCompressorCurrentTooHighStickyFault() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; - bool value; - - value = + bool value = HAL_GetCompressorCurrentTooHighStickyFault(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } bool Compressor::GetCompressorShortedStickyFault() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; - bool value; - - value = HAL_GetCompressorShortedStickyFault(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + bool value = HAL_GetCompressorShortedStickyFault(m_compressorHandle, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } bool Compressor::GetCompressorShortedFault() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; - bool value; - - value = HAL_GetCompressorShortedFault(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + bool value = HAL_GetCompressorShortedFault(m_compressorHandle, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } bool Compressor::GetCompressorNotConnectedStickyFault() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; - bool value; - - value = HAL_GetCompressorNotConnectedStickyFault(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + bool value = + HAL_GetCompressorNotConnectedStickyFault(m_compressorHandle, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } bool Compressor::GetCompressorNotConnectedFault() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; - bool value; - - value = HAL_GetCompressorNotConnectedFault(m_compressorHandle, &status); - - if (status) { - wpi_setWPIError(Timeout); - } - + bool value = HAL_GetCompressorNotConnectedFault(m_compressorHandle, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); return value; } void Compressor::ClearAllPCMStickyFaults() { - if (StatusIsFatal()) { - return; - } int32_t status = 0; - HAL_ClearAllPCMStickyFaults(m_module, &status); - - if (status) { - wpi_setWPIError(Timeout); - } + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{m_module}); } int Compressor::GetModule() const { diff --git a/wpilibc/src/main/native/cpp/Counter.cpp b/wpilibc/src/main/native/cpp/Counter.cpp index e1d9d2920e..02b1df3ba8 100644 --- a/wpilibc/src/main/native/cpp/Counter.cpp +++ b/wpilibc/src/main/native/cpp/Counter.cpp @@ -12,7 +12,7 @@ #include "frc/AnalogTrigger.h" #include "frc/Base.h" #include "frc/DigitalInput.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -22,7 +22,7 @@ Counter::Counter(Mode mode) { int32_t status = 0; m_counter = HAL_InitializeCounter(static_cast(mode), &m_index, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "InitializeCounter"); SetMaxPeriod(0.5); @@ -64,10 +64,8 @@ Counter::Counter(EncodingType encodingType, std::shared_ptr downSource, bool inverted) : Counter(kExternalDirection) { if (encodingType != k1X && encodingType != k2X) { - wpi_setWPIErrorWithContext( - ParameterOutOfRange, - "Counter only supports 1X and 2X quadrature decoding."); - return; + throw FRC_MakeError(err::ParameterOutOfRange, + "Counter only supports 1X and 2X quadrature decoding."); } SetUpSource(upSource); SetDownSource(downSource); @@ -81,22 +79,23 @@ Counter::Counter(EncodingType encodingType, HAL_SetCounterAverageSize(m_counter, 2, &status); } - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Counter constructor"); SetDownSourceEdge(inverted, true); } Counter::~Counter() { - SetUpdateWhenEmpty(true); + try { + SetUpdateWhenEmpty(true); + } catch (const RuntimeError& e) { + e.Report(); + } int32_t status = 0; HAL_FreeCounter(m_counter, &status); - wpi_setHALError(status); + FRC_ReportError(status, "Counter destructor"); } void Counter::SetUpSource(int channel) { - if (StatusIsFatal()) { - return; - } SetUpSource(std::make_shared(channel)); SendableRegistry::GetInstance().AddChild(this, m_upSource.get()); } @@ -110,9 +109,6 @@ void Counter::SetUpSource(AnalogTrigger* analogTrigger, void Counter::SetUpSource(std::shared_ptr analogTrigger, AnalogTriggerType triggerType) { - if (StatusIsFatal()) { - return; - } SetUpSource(analogTrigger->CreateOutput(triggerType)); } @@ -122,20 +118,13 @@ void Counter::SetUpSource(DigitalSource* source) { } void Counter::SetUpSource(std::shared_ptr source) { - if (StatusIsFatal()) { - return; - } m_upSource = source; - if (m_upSource->StatusIsFatal()) { - CloneError(*m_upSource); - } else { - int32_t status = 0; - HAL_SetCounterUpSource(m_counter, source->GetPortHandleForRouting(), - static_cast( - source->GetAnalogTriggerTypeForRouting()), - &status); - wpi_setHALError(status); - } + int32_t status = 0; + HAL_SetCounterUpSource(m_counter, source->GetPortHandleForRouting(), + static_cast( + source->GetAnalogTriggerTypeForRouting()), + &status); + FRC_CheckErrorStatus(status, "SetUpSource"); } void Counter::SetUpSource(DigitalSource& source) { @@ -144,33 +133,24 @@ void Counter::SetUpSource(DigitalSource& source) { } void Counter::SetUpSourceEdge(bool risingEdge, bool fallingEdge) { - if (StatusIsFatal()) { - return; - } if (m_upSource == nullptr) { - wpi_setWPIErrorWithContext( - NullParameter, + throw FRC_MakeError( + err::NullParameter, "Must set non-nullptr UpSource before setting UpSourceEdge"); } int32_t status = 0; HAL_SetCounterUpSourceEdge(m_counter, risingEdge, fallingEdge, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetUpSourceEdge"); } void Counter::ClearUpSource() { - if (StatusIsFatal()) { - return; - } m_upSource.reset(); int32_t status = 0; HAL_ClearCounterUpSource(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ClearUpSource"); } void Counter::SetDownSource(int channel) { - if (StatusIsFatal()) { - return; - } SetDownSource(std::make_shared(channel)); SendableRegistry::GetInstance().AddChild(this, m_downSource.get()); } @@ -184,9 +164,6 @@ void Counter::SetDownSource(AnalogTrigger* analogTrigger, void Counter::SetDownSource(std::shared_ptr analogTrigger, AnalogTriggerType triggerType) { - if (StatusIsFatal()) { - return; - } SetDownSource(analogTrigger->CreateOutput(triggerType)); } @@ -201,106 +178,82 @@ void Counter::SetDownSource(DigitalSource& source) { } void Counter::SetDownSource(std::shared_ptr source) { - if (StatusIsFatal()) { - return; - } m_downSource = source; - if (m_downSource->StatusIsFatal()) { - CloneError(*m_downSource); - } else { - int32_t status = 0; - HAL_SetCounterDownSource(m_counter, source->GetPortHandleForRouting(), - static_cast( - source->GetAnalogTriggerTypeForRouting()), - &status); - wpi_setHALError(status); - } + int32_t status = 0; + HAL_SetCounterDownSource(m_counter, source->GetPortHandleForRouting(), + static_cast( + source->GetAnalogTriggerTypeForRouting()), + &status); + FRC_CheckErrorStatus(status, "SetDownSource"); } void Counter::SetDownSourceEdge(bool risingEdge, bool fallingEdge) { - if (StatusIsFatal()) { - return; - } if (m_downSource == nullptr) { - wpi_setWPIErrorWithContext( - NullParameter, + throw FRC_MakeError( + err::NullParameter, "Must set non-nullptr DownSource before setting DownSourceEdge"); } int32_t status = 0; HAL_SetCounterDownSourceEdge(m_counter, risingEdge, fallingEdge, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetDownSourceEdge"); } void Counter::ClearDownSource() { - if (StatusIsFatal()) { - return; - } m_downSource.reset(); int32_t status = 0; HAL_ClearCounterDownSource(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ClearDownSource"); } void Counter::SetUpDownCounterMode() { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetCounterUpDownMode(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetUpDownCounterMode"); } void Counter::SetExternalDirectionMode() { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetCounterExternalDirectionMode(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetExternalDirectionMode"); } void Counter::SetSemiPeriodMode(bool highSemiPeriod) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetCounterSemiPeriodMode(m_counter, highSemiPeriod, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus( + status, + "SetSemiPeriodMode to " + wpi::Twine{highSemiPeriod ? "true" : "false"}); } void Counter::SetPulseLengthMode(double threshold) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetCounterPulseLengthMode(m_counter, threshold, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetPulseLengthMode"); } void Counter::SetReverseDirection(bool reverseDirection) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetCounterReverseDirection(m_counter, reverseDirection, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, + "SetReverseDirection to " + + wpi::Twine{reverseDirection ? "true" : "false"}); } void Counter::SetSamplesToAverage(int samplesToAverage) { if (samplesToAverage < 1 || samplesToAverage > 127) { - wpi_setWPIErrorWithContext( - ParameterOutOfRange, - "Average counter values must be between 1 and 127"); + throw FRC_MakeError(err::ParameterOutOfRange, + "Average counter values must be between 1 and 127"); } int32_t status = 0; HAL_SetCounterSamplesToAverage(m_counter, samplesToAverage, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus( + status, "SetSamplesToAverage to " + wpi::Twine{samplesToAverage}); } int Counter::GetSamplesToAverage() const { int32_t status = 0; int samples = HAL_GetCounterSamplesToAverage(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetSamplesToAverage"); return samples; } @@ -309,69 +262,48 @@ int Counter::GetFPGAIndex() const { } int Counter::Get() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int value = HAL_GetCounter(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Get"); return value; } void Counter::Reset() { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_ResetCounter(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Reset"); } double Counter::GetPeriod() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double value = HAL_GetCounterPeriod(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetPeriod"); return value; } void Counter::SetMaxPeriod(double maxPeriod) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetCounterMaxPeriod(m_counter, maxPeriod, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetMaxPeriod"); } void Counter::SetUpdateWhenEmpty(bool enabled) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetCounterUpdateWhenEmpty(m_counter, enabled, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetUpdateWhenEmpty"); } bool Counter::GetStopped() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; bool value = HAL_GetCounterStopped(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetStopped"); return value; } bool Counter::GetDirection() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; bool value = HAL_GetCounterDirection(m_counter, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetDirection"); return value; } diff --git a/wpilibc/src/main/native/cpp/DMA.cpp b/wpilibc/src/main/native/cpp/DMA.cpp index 87164f9a94..6c99e14d39 100644 --- a/wpilibc/src/main/native/cpp/DMA.cpp +++ b/wpilibc/src/main/native/cpp/DMA.cpp @@ -4,21 +4,22 @@ #include "frc/DMA.h" -#include -#include -#include -#include -#include - #include #include +#include "frc/AnalogInput.h" +#include "frc/Counter.h" +#include "frc/DigitalSource.h" +#include "frc/DutyCycle.h" +#include "frc/Encoder.h" +#include "frc/Errors.h" + using namespace frc; DMA::DMA() { int32_t status = 0; dmaHandle = HAL_InitializeDMA(&status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "InitializeDMA"); } DMA::~DMA() { @@ -28,68 +29,68 @@ DMA::~DMA() { void DMA::SetPause(bool pause) { int32_t status = 0; HAL_SetDMAPause(dmaHandle, pause, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "SetPause"); } void DMA::SetRate(int cycles) { int32_t status = 0; HAL_SetDMARate(dmaHandle, cycles, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "SetRate"); } void DMA::AddEncoder(const Encoder* encoder) { int32_t status = 0; HAL_AddDMAEncoder(dmaHandle, encoder->m_encoder, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "AddEncoder"); } void DMA::AddEncoderPeriod(const Encoder* encoder) { int32_t status = 0; HAL_AddDMAEncoderPeriod(dmaHandle, encoder->m_encoder, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "AddEncoderPeriod"); } void DMA::AddCounter(const Counter* counter) { int32_t status = 0; HAL_AddDMACounter(dmaHandle, counter->m_counter, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "AddCounter"); } void DMA::AddCounterPeriod(const Counter* counter) { int32_t status = 0; HAL_AddDMACounterPeriod(dmaHandle, counter->m_counter, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "AddCounterPeriod"); } void DMA::AddDigitalSource(const DigitalSource* digitalSource) { int32_t status = 0; HAL_AddDMADigitalSource(dmaHandle, digitalSource->GetPortHandleForRouting(), &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "AddDigitalSource"); } void DMA::AddDutyCycle(const DutyCycle* dutyCycle) { int32_t status = 0; HAL_AddDMADutyCycle(dmaHandle, dutyCycle->m_handle, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "AddDutyCycle"); } void DMA::AddAnalogInput(const AnalogInput* analogInput) { int32_t status = 0; HAL_AddDMAAnalogInput(dmaHandle, analogInput->m_port, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "AddAnalogInput"); } void DMA::AddAveragedAnalogInput(const AnalogInput* analogInput) { int32_t status = 0; HAL_AddDMAAveragedAnalogInput(dmaHandle, analogInput->m_port, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "AddAveragedAnalogInput"); } void DMA::AddAnalogAccumulator(const AnalogInput* analogInput) { int32_t status = 0; HAL_AddDMAAnalogAccumulator(dmaHandle, analogInput->m_port, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "AddAnalogAccumulator"); } void DMA::SetExternalTrigger(DigitalSource* source, bool rising, bool falling) { @@ -98,17 +99,17 @@ void DMA::SetExternalTrigger(DigitalSource* source, bool rising, bool falling) { static_cast( source->GetAnalogTriggerTypeForRouting()), rising, falling, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "SetExternalTrigger"); } void DMA::StartDMA(int queueDepth) { int32_t status = 0; HAL_StartDMA(dmaHandle, queueDepth, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "StartDMA"); } void DMA::StopDMA() { int32_t status = 0; HAL_StopDMA(dmaHandle, &status); - wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); + FRC_CheckErrorStatus(status, "StopDMA"); } diff --git a/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp b/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp index 0ff4ed93aa..9fae85dbd7 100644 --- a/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp +++ b/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp @@ -14,9 +14,9 @@ #include "frc/Counter.h" #include "frc/Encoder.h" +#include "frc/Errors.h" #include "frc/SensorUtil.h" #include "frc/Utility.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableRegistry.h" using namespace frc; @@ -29,7 +29,7 @@ DigitalGlitchFilter::DigitalGlitchFilter() { std::scoped_lock lock(m_mutex); auto index = std::find(m_filterAllocated.begin(), m_filterAllocated.end(), false); - wpi_assert(index != m_filterAllocated.end()); + FRC_Assert(index != m_filterAllocated.end()); m_channelIndex = std::distance(m_filterAllocated.begin(), index); *index = true; @@ -48,12 +48,11 @@ DigitalGlitchFilter::~DigitalGlitchFilter() { } DigitalGlitchFilter::DigitalGlitchFilter(DigitalGlitchFilter&& rhs) - : ErrorBase(std::move(rhs)), SendableHelper(std::move(rhs)) { + : SendableHelper(std::move(rhs)) { std::swap(m_channelIndex, rhs.m_channelIndex); } DigitalGlitchFilter& DigitalGlitchFilter::operator=(DigitalGlitchFilter&& rhs) { - ErrorBase::operator=(std::move(rhs)); SendableHelper::operator=(std::move(rhs)); std::swap(m_channelIndex, rhs.m_channelIndex); @@ -71,35 +70,31 @@ void DigitalGlitchFilter::DoAdd(DigitalSource* input, int requestedIndex) { if (input) { // We don't support GlitchFilters on AnalogTriggers. if (input->IsAnalogTrigger()) { - wpi_setErrorWithContext( + throw FRC_MakeError( -1, "Analog Triggers not supported for DigitalGlitchFilters"); - return; } int32_t status = 0; HAL_SetFilterSelect(input->GetPortHandleForRouting(), requestedIndex, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, + "requested index " + wpi::Twine{requestedIndex}); // Validate that we set it correctly. int actualIndex = HAL_GetFilterSelect(input->GetPortHandleForRouting(), &status); - wpi_assertEqual(actualIndex, requestedIndex); + FRC_CheckErrorStatus(status, + "requested index " + wpi::Twine{requestedIndex}); + FRC_Assert(actualIndex == requestedIndex); } } void DigitalGlitchFilter::Add(Encoder* input) { Add(input->m_aSource.get()); - if (StatusIsFatal()) { - return; - } Add(input->m_bSource.get()); } void DigitalGlitchFilter::Add(Counter* input) { Add(input->m_upSource.get()); - if (StatusIsFatal()) { - return; - } Add(input->m_downSource.get()); } @@ -109,24 +104,18 @@ void DigitalGlitchFilter::Remove(DigitalSource* input) { void DigitalGlitchFilter::Remove(Encoder* input) { Remove(input->m_aSource.get()); - if (StatusIsFatal()) { - return; - } Remove(input->m_bSource.get()); } void DigitalGlitchFilter::Remove(Counter* input) { Remove(input->m_upSource.get()); - if (StatusIsFatal()) { - return; - } Remove(input->m_downSource.get()); } void DigitalGlitchFilter::SetPeriodCycles(int fpgaCycles) { int32_t status = 0; HAL_SetFilterPeriod(m_channelIndex, fpgaCycles, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Channel " + wpi::Twine{m_channelIndex}); } void DigitalGlitchFilter::SetPeriodNanoSeconds(uint64_t nanoseconds) { @@ -134,25 +123,20 @@ void DigitalGlitchFilter::SetPeriodNanoSeconds(uint64_t nanoseconds) { int fpgaCycles = nanoseconds * HAL_GetSystemClockTicksPerMicrosecond() / 4 / 1000; HAL_SetFilterPeriod(m_channelIndex, fpgaCycles, &status); - - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Channel " + wpi::Twine{m_channelIndex}); } int DigitalGlitchFilter::GetPeriodCycles() { int32_t status = 0; int fpgaCycles = HAL_GetFilterPeriod(m_channelIndex, &status); - - wpi_setHALError(status); - + FRC_CheckErrorStatus(status, "Channel " + wpi::Twine{m_channelIndex}); return fpgaCycles; } uint64_t DigitalGlitchFilter::GetPeriodNanoSeconds() { int32_t status = 0; int fpgaCycles = HAL_GetFilterPeriod(m_channelIndex, &status); - - wpi_setHALError(status); - + FRC_CheckErrorStatus(status, "Channel " + wpi::Twine{m_channelIndex}); return static_cast(fpgaCycles) * 1000L / static_cast(HAL_GetSystemClockTicksPerMicrosecond() / 4); } diff --git a/wpilibc/src/main/native/cpp/DigitalInput.cpp b/wpilibc/src/main/native/cpp/DigitalInput.cpp index 1751053950..936067ad0b 100644 --- a/wpilibc/src/main/native/cpp/DigitalInput.cpp +++ b/wpilibc/src/main/native/cpp/DigitalInput.cpp @@ -11,8 +11,8 @@ #include #include +#include "frc/Errors.h" #include "frc/SensorUtil.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -20,40 +20,27 @@ using namespace frc; DigitalInput::DigitalInput(int channel) { if (!SensorUtil::CheckDigitalChannel(channel)) { - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, - "Digital Channel " + wpi::Twine(channel)); - m_channel = std::numeric_limits::max(); - return; + throw FRC_MakeError(err::ChannelIndexOutOfRange, + "Digital Channel " + wpi::Twine{channel}); } m_channel = channel; int32_t status = 0; m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), true, &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumDigitalChannels(), channel); - m_handle = HAL_kInvalidHandle; - m_channel = std::numeric_limits::max(); - return; - } + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{channel}); HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel + 1); SendableRegistry::GetInstance().AddLW(this, "DigitalInput", channel); } DigitalInput::~DigitalInput() { - if (StatusIsFatal()) { - return; - } HAL_FreeDIOPort(m_handle); } bool DigitalInput::Get() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; bool value = HAL_GetDIO(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Get"); return value; } diff --git a/wpilibc/src/main/native/cpp/DigitalOutput.cpp b/wpilibc/src/main/native/cpp/DigitalOutput.cpp index c8d3c13a8e..41c5e4e1ec 100644 --- a/wpilibc/src/main/native/cpp/DigitalOutput.cpp +++ b/wpilibc/src/main/native/cpp/DigitalOutput.cpp @@ -11,8 +11,8 @@ #include #include +#include "frc/Errors.h" #include "frc/SensorUtil.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -21,54 +21,40 @@ using namespace frc; DigitalOutput::DigitalOutput(int channel) { m_pwmGenerator = HAL_kInvalidHandle; if (!SensorUtil::CheckDigitalChannel(channel)) { - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, - "Digital Channel " + wpi::Twine(channel)); - m_channel = std::numeric_limits::max(); - return; + throw FRC_MakeError(err::ChannelIndexOutOfRange, + "Digital Channel " + wpi::Twine{channel}); } m_channel = channel; int32_t status = 0; m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), false, &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumDigitalChannels(), channel); - m_channel = std::numeric_limits::max(); - m_handle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{channel}); HAL_Report(HALUsageReporting::kResourceType_DigitalOutput, channel + 1); SendableRegistry::GetInstance().AddLW(this, "DigitalOutput", channel); } DigitalOutput::~DigitalOutput() { - if (StatusIsFatal()) { - return; - } // Disable the PWM in case it was running. - DisablePWM(); + try { + DisablePWM(); + } catch (const RuntimeError& e) { + e.Report(); + } HAL_FreeDIOPort(m_handle); } void DigitalOutput::Set(bool value) { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; HAL_SetDIO(m_handle, value, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); } bool DigitalOutput::Get() const { - if (StatusIsFatal()) { - return false; - } - int32_t status = 0; bool val = HAL_GetDIO(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); return val; } @@ -89,34 +75,22 @@ int DigitalOutput::GetChannel() const { } void DigitalOutput::Pulse(double length) { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; HAL_Pulse(m_handle, length, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); } bool DigitalOutput::IsPulsing() const { - if (StatusIsFatal()) { - return false; - } - int32_t status = 0; bool value = HAL_IsPulsing(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); return value; } void DigitalOutput::SetPWMRate(double rate) { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; HAL_SetDigitalPWMRate(rate, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); } void DigitalOutput::EnablePWM(double initialDutyCycle) { @@ -126,29 +100,17 @@ void DigitalOutput::EnablePWM(double initialDutyCycle) { int32_t status = 0; - if (StatusIsFatal()) { - return; - } m_pwmGenerator = HAL_AllocateDigitalPWM(&status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); - if (StatusIsFatal()) { - return; - } HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, initialDutyCycle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); - if (StatusIsFatal()) { - return; - } HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, m_channel, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); } void DigitalOutput::DisablePWM() { - if (StatusIsFatal()) { - return; - } if (m_pwmGenerator == HAL_kInvalidHandle) { return; } @@ -158,22 +120,18 @@ void DigitalOutput::DisablePWM() { // Disable the output by routing to a dead bit. HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, SensorUtil::kDigitalChannels, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); HAL_FreeDigitalPWM(m_pwmGenerator, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); m_pwmGenerator = HAL_kInvalidHandle; } void DigitalOutput::UpdateDutyCycle(double dutyCycle) { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, dutyCycle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Digital Channel " + wpi::Twine{m_channel}); } void DigitalOutput::SetSimDevice(HAL_SimDeviceHandle device) { diff --git a/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp b/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp index 815e3a076e..b0d7cbfd6a 100644 --- a/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp +++ b/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp @@ -11,8 +11,8 @@ #include #include +#include "frc/Errors.h" #include "frc/SensorUtil.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -28,42 +28,31 @@ DoubleSolenoid::DoubleSolenoid(int moduleNumber, int forwardChannel, m_forwardChannel(forwardChannel), m_reverseChannel(reverseChannel) { if (!SensorUtil::CheckSolenoidModule(m_moduleNumber)) { - wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, - "Solenoid Module " + wpi::Twine(m_moduleNumber)); - return; + throw FRC_MakeError(err::ModuleIndexOutOfRange, + "Solenoid Module " + wpi::Twine{m_moduleNumber}); } if (!SensorUtil::CheckSolenoidChannel(m_forwardChannel)) { - wpi_setWPIErrorWithContext( - ChannelIndexOutOfRange, - "Solenoid Channel " + wpi::Twine(m_forwardChannel)); - return; + throw FRC_MakeError(err::ChannelIndexOutOfRange, + "Solenoid Channel " + wpi::Twine{m_forwardChannel}); } if (!SensorUtil::CheckSolenoidChannel(m_reverseChannel)) { - wpi_setWPIErrorWithContext( - ChannelIndexOutOfRange, - "Solenoid Channel " + wpi::Twine(m_reverseChannel)); - return; + throw FRC_MakeError(err::ChannelIndexOutOfRange, + "Solenoid Channel " + wpi::Twine{m_reverseChannel}); } int32_t status = 0; m_forwardHandle = HAL_InitializeSolenoidPort( HAL_GetPortWithModule(moduleNumber, m_forwardChannel), &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumSolenoidChannels(), - forwardChannel); - m_forwardHandle = HAL_kInvalidHandle; - m_reverseHandle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "Solenoid Module " + wpi::Twine{m_moduleNumber} + + " Channel " + wpi::Twine{m_forwardChannel}); m_reverseHandle = HAL_InitializeSolenoidPort( HAL_GetPortWithModule(moduleNumber, m_reverseChannel), &status); if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumSolenoidChannels(), - reverseChannel); // free forward solenoid HAL_FreeSolenoidPort(m_forwardHandle); - m_forwardHandle = HAL_kInvalidHandle; - m_reverseHandle = HAL_kInvalidHandle; + FRC_CheckErrorStatus(status, "Solenoid Module " + + wpi::Twine{m_moduleNumber} + " Channel " + + wpi::Twine{m_reverseChannel}); return; } @@ -85,10 +74,6 @@ DoubleSolenoid::~DoubleSolenoid() { } void DoubleSolenoid::Set(Value value) { - if (StatusIsFatal()) { - return; - } - bool forward = false; bool reverse = false; @@ -112,22 +97,26 @@ void DoubleSolenoid::Set(Value value) { int rstatus = 0; HAL_SetSolenoid(m_reverseHandle, reverse, &rstatus); - wpi_setHALError(fstatus); - wpi_setHALError(rstatus); + FRC_CheckErrorStatus(fstatus, "Solenoid Module " + + wpi::Twine{m_moduleNumber} + " Channel " + + wpi::Twine{m_forwardChannel}); + FRC_CheckErrorStatus(rstatus, "Solenoid Module " + + wpi::Twine{m_moduleNumber} + " Channel " + + wpi::Twine{m_reverseChannel}); } DoubleSolenoid::Value DoubleSolenoid::Get() const { - if (StatusIsFatal()) { - return kOff; - } - int fstatus = 0; int rstatus = 0; bool valueForward = HAL_GetSolenoid(m_forwardHandle, &fstatus); bool valueReverse = HAL_GetSolenoid(m_reverseHandle, &rstatus); - wpi_setHALError(fstatus); - wpi_setHALError(rstatus); + FRC_CheckErrorStatus(fstatus, "Solenoid Module " + + wpi::Twine{m_moduleNumber} + " Channel " + + wpi::Twine{m_forwardChannel}); + FRC_CheckErrorStatus(rstatus, "Solenoid Module " + + wpi::Twine{m_moduleNumber} + " Channel " + + wpi::Twine{m_reverseChannel}); if (valueForward) { return kForward; diff --git a/wpilibc/src/main/native/cpp/DriverStation.cpp b/wpilibc/src/main/native/cpp/DriverStation.cpp index 2cce5f6c53..1e1f77397f 100644 --- a/wpilibc/src/main/native/cpp/DriverStation.cpp +++ b/wpilibc/src/main/native/cpp/DriverStation.cpp @@ -17,9 +17,9 @@ #include #include +#include "frc/Errors.h" #include "frc/MotorSafety.h" #include "frc/Timer.h" -#include "frc/WPIErrors.h" namespace frc { // A simple class which caches the previous value written to an NT entry @@ -140,7 +140,8 @@ void DriverStation::ReportError(bool isError, int32_t code, bool DriverStation::GetStickButton(int stick, int button) { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return false; } if (button <= 0) { @@ -163,7 +164,8 @@ bool DriverStation::GetStickButton(int stick, int button) { bool DriverStation::GetStickButtonPressed(int stick, int button) { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return false; } if (button <= 0) { @@ -192,7 +194,8 @@ bool DriverStation::GetStickButtonPressed(int stick, int button) { bool DriverStation::GetStickButtonReleased(int stick, int button) { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return false; } if (button <= 0) { @@ -221,11 +224,13 @@ bool DriverStation::GetStickButtonReleased(int stick, int button) { double DriverStation::GetStickAxis(int stick, int axis) { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return 0.0; } if (axis < 0 || axis >= HAL_kMaxJoystickAxes) { - wpi_setWPIError(BadJoystickAxis); + FRC_ReportError(warn::BadJoystickAxis, + "axis " + wpi::Twine{axis} + " out of range"); return 0.0; } @@ -243,11 +248,13 @@ double DriverStation::GetStickAxis(int stick, int axis) { int DriverStation::GetStickPOV(int stick, int pov) { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return -1; } if (pov < 0 || pov >= HAL_kMaxJoystickPOVs) { - wpi_setWPIError(BadJoystickAxis); + FRC_ReportError(warn::BadJoystickAxis, + "POV " + wpi::Twine{pov} + " out of range"); return -1; } @@ -265,7 +272,8 @@ int DriverStation::GetStickPOV(int stick, int pov) { int DriverStation::GetStickButtons(int stick) const { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return 0; } @@ -277,7 +285,8 @@ int DriverStation::GetStickButtons(int stick) const { int DriverStation::GetStickAxisCount(int stick) const { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return 0; } @@ -289,7 +298,8 @@ int DriverStation::GetStickAxisCount(int stick) const { int DriverStation::GetStickPOVCount(int stick) const { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return 0; } @@ -301,7 +311,8 @@ int DriverStation::GetStickPOVCount(int stick) const { int DriverStation::GetStickButtonCount(int stick) const { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return 0; } @@ -313,7 +324,8 @@ int DriverStation::GetStickButtonCount(int stick) const { bool DriverStation::GetJoystickIsXbox(int stick) const { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return false; } @@ -325,7 +337,8 @@ bool DriverStation::GetJoystickIsXbox(int stick) const { int DriverStation::GetJoystickType(int stick) const { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return -1; } @@ -337,7 +350,8 @@ int DriverStation::GetJoystickType(int stick) const { std::string DriverStation::GetJoystickName(int stick) const { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); } HAL_JoystickDescriptor descriptor; @@ -348,7 +362,8 @@ std::string DriverStation::GetJoystickName(int stick) const { int DriverStation::GetJoystickAxisType(int stick, int axis) const { if (stick < 0 || stick >= kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + FRC_ReportError(warn::BadJoystickIndex, + "stick " + wpi::Twine{stick} + " out of range"); return -1; } @@ -537,7 +552,7 @@ double DriverStation::GetMatchTime() const { double DriverStation::GetBatteryVoltage() const { int32_t status = 0; double voltage = HAL_GetVinVoltage(&status); - wpi_setErrorWithContext(status, "getVinVoltage"); + FRC_CheckErrorStatus(status, "getVinVoltage"); return voltage; } diff --git a/wpilibc/src/main/native/cpp/DutyCycle.cpp b/wpilibc/src/main/native/cpp/DutyCycle.cpp index 6ffcd9713d..f4dbe14085 100644 --- a/wpilibc/src/main/native/cpp/DutyCycle.cpp +++ b/wpilibc/src/main/native/cpp/DutyCycle.cpp @@ -9,18 +9,17 @@ #include "frc/Base.h" #include "frc/DigitalSource.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableBuilder.h" using namespace frc; DutyCycle::DutyCycle(DigitalSource* source) : m_source{source, NullDeleter()} { - if (m_source == nullptr) { - wpi_setWPIError(NullParameter); - } else { - InitDutyCycle(); + if (!m_source) { + throw FRC_MakeError(err::NullParameter, "source"); } + InitDutyCycle(); } DutyCycle::DutyCycle(DigitalSource& source) @@ -30,11 +29,10 @@ DutyCycle::DutyCycle(DigitalSource& source) DutyCycle::DutyCycle(std::shared_ptr source) : m_source{std::move(source)} { - if (m_source == nullptr) { - wpi_setWPIError(NullParameter); - } else { - InitDutyCycle(); + if (!m_source) { + throw FRC_MakeError(err::NullParameter, "source"); } + InitDutyCycle(); } DutyCycle::~DutyCycle() { @@ -48,7 +46,7 @@ void DutyCycle::InitDutyCycle() { static_cast( m_source->GetAnalogTriggerTypeForRouting()), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "InitDutyCycle"); int index = GetFPGAIndex(); HAL_Report(HALUsageReporting::kResourceType_DutyCycle, index + 1); SendableRegistry::GetInstance().AddLW(this, "Duty Cycle", index); @@ -57,35 +55,35 @@ void DutyCycle::InitDutyCycle() { int DutyCycle::GetFPGAIndex() const { int32_t status = 0; auto retVal = HAL_GetDutyCycleFPGAIndex(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetFPGAIndex"); return retVal; } int DutyCycle::GetFrequency() const { int32_t status = 0; auto retVal = HAL_GetDutyCycleFrequency(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetFrequency"); return retVal; } double DutyCycle::GetOutput() const { int32_t status = 0; auto retVal = HAL_GetDutyCycleOutput(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetOutput"); return retVal; } unsigned int DutyCycle::GetOutputRaw() const { int32_t status = 0; auto retVal = HAL_GetDutyCycleOutputRaw(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetOutputRaw"); return retVal; } unsigned int DutyCycle::GetOutputScaleFactor() const { int32_t status = 0; auto retVal = HAL_GetDutyCycleOutputScaleFactor(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetOutputScaleFactor"); return retVal; } diff --git a/wpilibc/src/main/native/cpp/Encoder.cpp b/wpilibc/src/main/native/cpp/Encoder.cpp index 2d7373940b..ce3f5eb47a 100644 --- a/wpilibc/src/main/native/cpp/Encoder.cpp +++ b/wpilibc/src/main/native/cpp/Encoder.cpp @@ -11,7 +11,7 @@ #include "frc/Base.h" #include "frc/DigitalInput.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -31,11 +31,13 @@ Encoder::Encoder(DigitalSource* aSource, DigitalSource* bSource, bool reverseDirection, EncodingType encodingType) : m_aSource(aSource, NullDeleter()), m_bSource(bSource, NullDeleter()) { - if (m_aSource == nullptr || m_bSource == nullptr) { - wpi_setWPIError(NullParameter); - } else { - InitEncoder(reverseDirection, encodingType); + if (!m_aSource) { + throw FRC_MakeError(err::NullParameter, "aSource"); } + if (!m_bSource) { + throw FRC_MakeError(err::NullParameter, "bSource"); + } + InitEncoder(reverseDirection, encodingType); } Encoder::Encoder(DigitalSource& aSource, DigitalSource& bSource, @@ -49,167 +51,130 @@ Encoder::Encoder(std::shared_ptr aSource, std::shared_ptr bSource, bool reverseDirection, EncodingType encodingType) : m_aSource(std::move(aSource)), m_bSource(std::move(bSource)) { - if (m_aSource == nullptr || m_bSource == nullptr) { - wpi_setWPIError(NullParameter); - } else { - InitEncoder(reverseDirection, encodingType); + if (!m_aSource) { + throw FRC_MakeError(err::NullParameter, "aSource"); } + if (!m_bSource) { + throw FRC_MakeError(err::NullParameter, "bSource"); + } + InitEncoder(reverseDirection, encodingType); } Encoder::~Encoder() { int32_t status = 0; HAL_FreeEncoder(m_encoder, &status); - wpi_setHALError(status); + FRC_ReportError(status, "FreeEncoder"); } int Encoder::Get() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int value = HAL_GetEncoder(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Get"); return value; } void Encoder::Reset() { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_ResetEncoder(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Reset"); } double Encoder::GetPeriod() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double value = HAL_GetEncoderPeriod(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetPeriod"); return value; } void Encoder::SetMaxPeriod(double maxPeriod) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetEncoderMaxPeriod(m_encoder, maxPeriod, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetMaxPeriod"); } bool Encoder::GetStopped() const { - if (StatusIsFatal()) { - return true; - } int32_t status = 0; bool value = HAL_GetEncoderStopped(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetStopped"); return value; } bool Encoder::GetDirection() const { - if (StatusIsFatal()) { - return false; - } int32_t status = 0; bool value = HAL_GetEncoderDirection(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetDirection"); return value; } int Encoder::GetRaw() const { - if (StatusIsFatal()) { - return 0; - } int32_t status = 0; int value = HAL_GetEncoderRaw(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetRaw"); return value; } int Encoder::GetEncodingScale() const { int32_t status = 0; int val = HAL_GetEncoderEncodingScale(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetEncodingScale"); return val; } double Encoder::GetDistance() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double value = HAL_GetEncoderDistance(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetDistance"); return value; } double Encoder::GetRate() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double value = HAL_GetEncoderRate(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetRate"); return value; } void Encoder::SetMinRate(double minRate) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetEncoderMinRate(m_encoder, minRate, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetMinRate"); } void Encoder::SetDistancePerPulse(double distancePerPulse) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetEncoderDistancePerPulse(m_encoder, distancePerPulse, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetDistancePerPulse"); } double Encoder::GetDistancePerPulse() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double distancePerPulse = HAL_GetEncoderDistancePerPulse(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetDistancePerPulse"); return distancePerPulse; } void Encoder::SetReverseDirection(bool reverseDirection) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetEncoderReverseDirection(m_encoder, reverseDirection, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetReverseDirection"); } void Encoder::SetSamplesToAverage(int samplesToAverage) { if (samplesToAverage < 1 || samplesToAverage > 127) { - wpi_setWPIErrorWithContext( - ParameterOutOfRange, - "Average counter values must be between 1 and 127"); - return; + throw FRC_MakeError( + err::ParameterOutOfRange, + "Average counter values must be between 1 and 127, got " + + wpi::Twine{samplesToAverage}); } int32_t status = 0; HAL_SetEncoderSamplesToAverage(m_encoder, samplesToAverage, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetSamplesToAverage"); } int Encoder::GetSamplesToAverage() const { int32_t status = 0; int result = HAL_GetEncoderSamplesToAverage(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetSamplesToAverage"); return result; } @@ -228,7 +193,7 @@ void Encoder::SetIndexSource(const DigitalSource& source, source.GetAnalogTriggerTypeForRouting()), static_cast(type), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetIndexSource"); } void Encoder::SetSimDevice(HAL_SimDeviceHandle device) { @@ -238,14 +203,14 @@ void Encoder::SetSimDevice(HAL_SimDeviceHandle device) { int Encoder::GetFPGAIndex() const { int32_t status = 0; int val = HAL_GetEncoderFPGAIndex(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetFPGAIndex"); return val; } void Encoder::InitSendable(SendableBuilder& builder) { int32_t status = 0; HAL_EncoderEncodingType type = HAL_GetEncoderEncodingType(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetEncodingType"); if (type == HAL_EncoderEncodingType::HAL_Encoder_k4X) { builder.SetSmartDashboardType("Quadrature Encoder"); } else { @@ -271,7 +236,7 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) { m_bSource->GetAnalogTriggerTypeForRouting()), reverseDirection, static_cast(encodingType), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "InitEncoder"); HAL_Report(HALUsageReporting::kResourceType_Encoder, GetFPGAIndex() + 1, encodingType); @@ -280,11 +245,8 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) { } double Encoder::DecodingScaleFactor() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double val = HAL_GetEncoderDecodingScaleFactor(m_encoder, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "DecodingScaleFactor"); return val; } diff --git a/wpilibc/src/main/native/cpp/Error.cpp b/wpilibc/src/main/native/cpp/Error.cpp deleted file mode 100644 index 383bde3830..0000000000 --- a/wpilibc/src/main/native/cpp/Error.cpp +++ /dev/null @@ -1,108 +0,0 @@ -// 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. - -#include "frc/Error.h" - -#include -#include - -#include "frc/Base.h" -#include "frc/DriverStation.h" -#include "frc/Timer.h" - -using namespace frc; - -Error::Error(Code code, const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, int lineNumber, - const ErrorBase* originatingObject) { - Set(code, contextMessage, filename, function, lineNumber, originatingObject); -} - -bool Error::operator<(const Error& rhs) const { - if (m_code < rhs.m_code) { - return true; - } else if (m_message < rhs.m_message) { - return true; - } else if (m_filename < rhs.m_filename) { - return true; - } else if (m_function < rhs.m_function) { - return true; - } else if (m_lineNumber < rhs.m_lineNumber) { - return true; - } else if (m_originatingObject < rhs.m_originatingObject) { - return true; - } else if (m_timestamp < rhs.m_timestamp) { - return true; - } else { - return false; - } -} - -Error::Code Error::GetCode() const { - return m_code; -} - -std::string Error::GetMessage() const { - return m_message; -} - -std::string Error::GetFilename() const { - return m_filename; -} - -std::string Error::GetFunction() const { - return m_function; -} - -int Error::GetLineNumber() const { - return m_lineNumber; -} - -const ErrorBase* Error::GetOriginatingObject() const { - return m_originatingObject; -} - -double Error::GetTimestamp() const { - return m_timestamp; -} - -void Error::Set(Code code, const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber, const ErrorBase* originatingObject) { - bool report = true; - - if (code == m_code && GetTime() - m_timestamp < 1) { - report = false; - } - - m_code = code; - m_message = contextMessage.str(); - m_filename = filename; - m_function = function; - m_lineNumber = lineNumber; - m_originatingObject = originatingObject; - - if (report) { - m_timestamp = GetTime(); - Report(); - } -} - -void Error::Report() { - DriverStation::ReportError( - true, m_code, m_message, - m_function + wpi::Twine(" [") + wpi::sys::path::filename(m_filename) + - wpi::Twine(':') + wpi::Twine(m_lineNumber) + wpi::Twine(']'), - wpi::GetStackTrace(4)); -} - -void Error::Clear() { - m_code = 0; - m_message = ""; - m_filename = ""; - m_function = ""; - m_lineNumber = 0; - m_originatingObject = nullptr; - m_timestamp = 0.0; -} diff --git a/wpilibc/src/main/native/cpp/ErrorBase.cpp b/wpilibc/src/main/native/cpp/ErrorBase.cpp deleted file mode 100644 index e6c3c9c241..0000000000 --- a/wpilibc/src/main/native/cpp/ErrorBase.cpp +++ /dev/null @@ -1,198 +0,0 @@ -// 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. - -#include "frc/ErrorBase.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "frc/Base.h" - -using namespace frc; - -namespace { -struct GlobalErrors { - wpi::mutex mutex; - std::set errors; - const Error* lastError{nullptr}; - - static GlobalErrors& GetInstance(); - static void Insert(const Error& error); - static void Insert(Error&& error); -}; -} // namespace - -GlobalErrors& GlobalErrors::GetInstance() { - static GlobalErrors inst; - return inst; -} - -void GlobalErrors::Insert(const Error& error) { - GlobalErrors& inst = GetInstance(); - std::scoped_lock lock(inst.mutex); - inst.lastError = &(*inst.errors.insert(error).first); -} - -void GlobalErrors::Insert(Error&& error) { - GlobalErrors& inst = GetInstance(); - std::scoped_lock lock(inst.mutex); - inst.lastError = &(*inst.errors.insert(std::move(error)).first); -} - -ErrorBase::ErrorBase() { - HAL_Initialize(500, 0); -} - -Error& ErrorBase::GetError() { - return m_error; -} - -const Error& ErrorBase::GetError() const { - return m_error; -} - -void ErrorBase::ClearError() const { - m_error.Clear(); -} - -void ErrorBase::SetErrnoError(const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const { - wpi::SmallString<128> buf; - wpi::raw_svector_ostream err(buf); - int errNo = errno; - if (errNo == 0) { - err << "OK: "; - } else { - err << std::strerror(errNo) << " (" << wpi::format_hex(errNo, 10, true) - << "): "; - } - - // Set the current error information for this object. - m_error.Set(-1, err.str() + contextMessage, filename, function, lineNumber, - this); - - // Update the global error if there is not one already set. - GlobalErrors::Insert(m_error); -} - -void ErrorBase::SetImaqError(int success, const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const { - // If there was an error - if (success <= 0) { - // Set the current error information for this object. - m_error.Set(success, wpi::Twine(success) + ": " + contextMessage, filename, - function, lineNumber, this); - - // Update the global error if there is not one already set. - GlobalErrors::Insert(m_error); - } -} - -void ErrorBase::SetError(Error::Code code, const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const { - // If there was an error - if (code != 0) { - // Set the current error information for this object. - m_error.Set(code, contextMessage, filename, function, lineNumber, this); - - // Update the global error if there is not one already set. - GlobalErrors::Insert(m_error); - } -} - -void ErrorBase::SetErrorRange(Error::Code code, int32_t minRange, - int32_t maxRange, int32_t requestedValue, - const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const { - // If there was an error - if (code != 0) { - // Set the current error information for this object. - m_error.Set(code, - contextMessage + ", Minimum Value: " + wpi::Twine(minRange) + - ", MaximumValue: " + wpi::Twine(maxRange) + - ", Requested Value: " + wpi::Twine(requestedValue), - filename, function, lineNumber, this); - - // Update the global error if there is not one already set. - GlobalErrors::Insert(m_error); - } -} - -void ErrorBase::SetWPIError(const wpi::Twine& errorMessage, Error::Code code, - const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const { - // Set the current error information for this object. - m_error.Set(code, errorMessage + ": " + contextMessage, filename, function, - lineNumber, this); - - // Update the global error if there is not one already set. - GlobalErrors::Insert(m_error); -} - -void ErrorBase::CloneError(const ErrorBase& rhs) const { - m_error = rhs.GetError(); -} - -bool ErrorBase::StatusIsFatal() const { - return m_error.GetCode() < 0; -} - -void ErrorBase::SetGlobalError(Error::Code code, - const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) { - // If there was an error - if (code != 0) { - // Set the current error information for this object. - GlobalErrors::Insert( - Error(code, contextMessage, filename, function, lineNumber, nullptr)); - } -} - -void ErrorBase::SetGlobalWPIError(const wpi::Twine& errorMessage, - const wpi::Twine& contextMessage, - wpi::StringRef filename, - wpi::StringRef function, int lineNumber) { - GlobalErrors::Insert(Error(-1, errorMessage + ": " + contextMessage, filename, - function, lineNumber, nullptr)); -} - -Error ErrorBase::GetGlobalError() { - auto& inst = GlobalErrors::GetInstance(); - std::scoped_lock mutex(inst.mutex); - if (!inst.lastError) { - return {}; - } - return *inst.lastError; -} - -std::vector ErrorBase::GetGlobalErrors() { - auto& inst = GlobalErrors::GetInstance(); - std::scoped_lock mutex(inst.mutex); - std::vector rv; - for (auto&& error : inst.errors) { - rv.push_back(error); - } - return rv; -} - -void ErrorBase::ClearGlobalErrors() { - auto& inst = GlobalErrors::GetInstance(); - std::scoped_lock mutex(inst.mutex); - inst.errors.clear(); - inst.lastError = nullptr; -} diff --git a/wpilibc/src/main/native/cpp/Errors.cpp b/wpilibc/src/main/native/cpp/Errors.cpp new file mode 100644 index 0000000000..56c55922a4 --- /dev/null +++ b/wpilibc/src/main/native/cpp/Errors.cpp @@ -0,0 +1,78 @@ +// 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. + +#include "frc/Errors.h" + +#include + +#include +#include +#include +#include +#include + +using namespace frc; + +RuntimeError::RuntimeError(int32_t code, const wpi::Twine& message, + const wpi::Twine& loc, wpi::StringRef stack) + : runtime_error{message.str()}, m_data{std::make_shared()} { + m_data->code = code; + m_data->loc = loc.str(); + m_data->stack = stack; +} + +RuntimeError::RuntimeError(int32_t code, const wpi::Twine& message, + const char* fileName, int lineNumber, + const char* funcName, wpi::StringRef stack) + : RuntimeError{code, message, + funcName + wpi::Twine{" ["} + + wpi::sys::path::filename(fileName) + ":" + + wpi::Twine{lineNumber} + "]", + stack} {} + +void RuntimeError::Report() const { + HAL_SendError(m_data->code < 0, m_data->code, 0, what(), m_data->loc.c_str(), + m_data->stack.c_str(), 1); +} + +const char* frc::GetErrorMessage(int32_t code) { + using namespace err; + using namespace warn; + switch (code) { +#define S(label, offset, message) \ + case label: \ + return message; +#include "frc/WPIErrors.mac" +#include "frc/WPIWarnings.mac" +#undef S + default: + return HAL_GetErrorMessage(code); + } +} + +void frc::ReportError(int32_t status, const wpi::Twine& message, + const char* fileName, int lineNumber, + const char* funcName) { + if (status == 0) { + return; + } + const char* statusMessage = GetErrorMessage(status); + auto stack = wpi::GetStackTrace(2); + wpi::SmallString<128> buf; + HAL_SendError(status < 0, status, 0, + (statusMessage + wpi::Twine{": "} + message) + .toNullTerminatedStringRef(buf) + .data(), + funcName, stack.c_str(), 1); +} + +RuntimeError frc::MakeError(int32_t status, const wpi::Twine& message, + const char* fileName, int lineNumber, + const char* funcName) { + const char* statusMessage = GetErrorMessage(status); + auto stack = wpi::GetStackTrace(2); + return RuntimeError{status, statusMessage + wpi::Twine{": "} + message, + fileName, lineNumber, + funcName, stack}; +} diff --git a/wpilibc/src/main/native/cpp/GenericHID.cpp b/wpilibc/src/main/native/cpp/GenericHID.cpp index d606b372f8..423fb351ef 100644 --- a/wpilibc/src/main/native/cpp/GenericHID.cpp +++ b/wpilibc/src/main/native/cpp/GenericHID.cpp @@ -7,13 +7,14 @@ #include #include "frc/DriverStation.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" using namespace frc; GenericHID::GenericHID(int port) : m_ds(&DriverStation::GetInstance()) { - if (port >= DriverStation::kJoystickPorts) { - wpi_setWPIError(BadJoystickIndex); + if (port < 0 || port >= DriverStation::kJoystickPorts) { + throw FRC_MakeError(warn::BadJoystickIndex, + "port " + wpi::Twine{port} + "out of range"); } m_port = port; } diff --git a/wpilibc/src/main/native/cpp/I2C.cpp b/wpilibc/src/main/native/cpp/I2C.cpp index df44730801..439471efd4 100644 --- a/wpilibc/src/main/native/cpp/I2C.cpp +++ b/wpilibc/src/main/native/cpp/I2C.cpp @@ -9,7 +9,7 @@ #include #include -#include "frc/WPIErrors.h" +#include "frc/Errors.h" using namespace frc; @@ -17,7 +17,7 @@ I2C::I2C(Port port, int deviceAddress) : m_port(static_cast(port)), m_deviceAddress(deviceAddress) { int32_t status = 0; HAL_InitializeI2C(m_port, &status); - // wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Port " + wpi::Twine{static_cast(port)}); HAL_Report(HALUsageReporting::kResourceType_I2C, deviceAddress); } @@ -31,7 +31,6 @@ bool I2C::Transaction(uint8_t* dataToSend, int sendSize, uint8_t* dataReceived, int32_t status = 0; status = HAL_TransactionI2C(m_port, m_deviceAddress, dataToSend, sendSize, dataReceived, receiveSize); - // wpi_setHALError(status); return status < 0; } @@ -56,12 +55,10 @@ bool I2C::WriteBulk(uint8_t* data, int count) { bool I2C::Read(int registerAddress, int count, uint8_t* buffer) { if (count < 1) { - wpi_setWPIErrorWithContext(ParameterOutOfRange, "count"); - return true; + throw FRC_MakeError(err::ParameterOutOfRange, "count " + wpi::Twine{count}); } - if (buffer == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "buffer"); - return true; + if (!buffer) { + throw FRC_MakeError(err::NullParameter, "buffer"); } uint8_t regAddr = registerAddress; return Transaction(®Addr, sizeof(regAddr), buffer, count); @@ -69,12 +66,10 @@ bool I2C::Read(int registerAddress, int count, uint8_t* buffer) { bool I2C::ReadOnly(int count, uint8_t* buffer) { if (count < 1) { - wpi_setWPIErrorWithContext(ParameterOutOfRange, "count"); - return true; + throw FRC_MakeError(err::ParameterOutOfRange, "count " + wpi::Twine{count}); } - if (buffer == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "buffer"); - return true; + if (!buffer) { + throw FRC_MakeError(err::NullParameter, "buffer"); } return HAL_ReadI2C(m_port, m_deviceAddress, buffer, count) < 0; } diff --git a/wpilibc/src/main/native/cpp/InterruptableSensorBase.cpp b/wpilibc/src/main/native/cpp/InterruptableSensorBase.cpp index 1dda99dcc5..334fd3c1d3 100644 --- a/wpilibc/src/main/native/cpp/InterruptableSensorBase.cpp +++ b/wpilibc/src/main/native/cpp/InterruptableSensorBase.cpp @@ -4,8 +4,8 @@ #include "frc/InterruptableSensorBase.h" +#include "frc/Errors.h" #include "frc/Utility.h" -#include "frc/WPIErrors.h" using namespace frc; @@ -20,15 +20,8 @@ InterruptableSensorBase::~InterruptableSensorBase() { void InterruptableSensorBase::RequestInterrupts( HAL_InterruptHandlerFunction handler, void* param) { - if (StatusIsFatal()) { - return; - } - - wpi_assert(m_interrupt == HAL_kInvalidHandle); + FRC_Assert(m_interrupt == HAL_kInvalidHandle); AllocateInterrupts(false); - if (StatusIsFatal()) { - return; // if allocate failed, out of interrupts - } int32_t status = 0; HAL_RequestInterrupts( @@ -37,19 +30,12 @@ void InterruptableSensorBase::RequestInterrupts( &status); SetUpSourceEdge(true, false); HAL_AttachInterruptHandler(m_interrupt, handler, param, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "AttachInterruptHandler"); } void InterruptableSensorBase::RequestInterrupts(InterruptEventHandler handler) { - if (StatusIsFatal()) { - return; - } - - wpi_assert(m_interrupt == HAL_kInvalidHandle); + FRC_Assert(m_interrupt == HAL_kInvalidHandle); AllocateInterrupts(false); - if (StatusIsFatal()) { - return; // if allocate failed, out of interrupts - } m_interruptHandler = std::make_unique(std::move(handler)); @@ -74,34 +60,24 @@ void InterruptableSensorBase::RequestInterrupts(InterruptEventHandler handler) { (*self)(res); }, m_interruptHandler.get(), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "AttachInterruptHandler"); } void InterruptableSensorBase::RequestInterrupts() { - if (StatusIsFatal()) { - return; - } - - wpi_assert(m_interrupt == HAL_kInvalidHandle); + FRC_Assert(m_interrupt == HAL_kInvalidHandle); AllocateInterrupts(true); - if (StatusIsFatal()) { - return; // if allocate failed, out of interrupts - } int32_t status = 0; HAL_RequestInterrupts( m_interrupt, GetPortHandleForRouting(), static_cast(GetAnalogTriggerTypeForRouting()), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "RequestInterrupts"); SetUpSourceEdge(true, false); } void InterruptableSensorBase::CancelInterrupts() { - if (StatusIsFatal()) { - return; - } - wpi_assert(m_interrupt != HAL_kInvalidHandle); + FRC_Assert(m_interrupt != HAL_kInvalidHandle); int32_t status = 0; HAL_CleanInterrupts(m_interrupt, &status); // Ignore status, as an invalid handle just needs to be ignored. @@ -111,15 +87,12 @@ void InterruptableSensorBase::CancelInterrupts() { InterruptableSensorBase::WaitResult InterruptableSensorBase::WaitForInterrupt( double timeout, bool ignorePrevious) { - if (StatusIsFatal()) { - return InterruptableSensorBase::kTimeout; - } - wpi_assert(m_interrupt != HAL_kInvalidHandle); + FRC_Assert(m_interrupt != HAL_kInvalidHandle); int32_t status = 0; int result; result = HAL_WaitForInterrupt(m_interrupt, timeout, ignorePrevious, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "WaitForInterrupt"); // Rising edge result is the interrupt bit set in the byte 0xFF // Falling edge result is the interrupt bit set in the byte 0xFF00 @@ -131,69 +104,53 @@ InterruptableSensorBase::WaitResult InterruptableSensorBase::WaitForInterrupt( } void InterruptableSensorBase::EnableInterrupts() { - if (StatusIsFatal()) { - return; - } - wpi_assert(m_interrupt != HAL_kInvalidHandle); + FRC_Assert(m_interrupt != HAL_kInvalidHandle); int32_t status = 0; HAL_EnableInterrupts(m_interrupt, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "EnableInterrupts"); } void InterruptableSensorBase::DisableInterrupts() { - if (StatusIsFatal()) { - return; - } - wpi_assert(m_interrupt != HAL_kInvalidHandle); + FRC_Assert(m_interrupt != HAL_kInvalidHandle); int32_t status = 0; HAL_DisableInterrupts(m_interrupt, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "DisableInterrupts"); } double InterruptableSensorBase::ReadRisingTimestamp() { - if (StatusIsFatal()) { - return 0.0; - } - wpi_assert(m_interrupt != HAL_kInvalidHandle); + FRC_Assert(m_interrupt != HAL_kInvalidHandle); int32_t status = 0; int64_t timestamp = HAL_ReadInterruptRisingTimestamp(m_interrupt, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ReadRisingTimestamp"); return timestamp * 1e-6; } double InterruptableSensorBase::ReadFallingTimestamp() { - if (StatusIsFatal()) { - return 0.0; - } - wpi_assert(m_interrupt != HAL_kInvalidHandle); + FRC_Assert(m_interrupt != HAL_kInvalidHandle); int32_t status = 0; int64_t timestamp = HAL_ReadInterruptFallingTimestamp(m_interrupt, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ReadFallingTimestamp"); return timestamp * 1e-6; } void InterruptableSensorBase::SetUpSourceEdge(bool risingEdge, bool fallingEdge) { - if (StatusIsFatal()) { - return; - } if (m_interrupt == HAL_kInvalidHandle) { - wpi_setWPIErrorWithContext( - NullParameter, + throw FRC_MakeError( + err::NullParameter, "You must call RequestInterrupts before SetUpSourceEdge"); - return; } if (m_interrupt != HAL_kInvalidHandle) { int32_t status = 0; HAL_SetInterruptUpSourceEdge(m_interrupt, risingEdge, fallingEdge, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetUpSourceEdge"); } } void InterruptableSensorBase::AllocateInterrupts(bool watcher) { - wpi_assert(m_interrupt == HAL_kInvalidHandle); + FRC_Assert(m_interrupt == HAL_kInvalidHandle); // Expects the calling leaf class to allocate an interrupt index. int32_t status = 0; m_interrupt = HAL_InitializeInterrupts(watcher, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "AllocateInterrupts"); } diff --git a/wpilibc/src/main/native/cpp/MotorSafety.cpp b/wpilibc/src/main/native/cpp/MotorSafety.cpp index 1d0a5d3b21..ed76bf1240 100644 --- a/wpilibc/src/main/native/cpp/MotorSafety.cpp +++ b/wpilibc/src/main/native/cpp/MotorSafety.cpp @@ -12,7 +12,7 @@ #include #include "frc/DriverStation.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" using namespace frc; @@ -30,16 +30,13 @@ MotorSafety::~MotorSafety() { } MotorSafety::MotorSafety(MotorSafety&& rhs) - : ErrorBase(std::move(rhs)), - m_expiration(std::move(rhs.m_expiration)), + : m_expiration(std::move(rhs.m_expiration)), m_enabled(std::move(rhs.m_enabled)), m_stopTime(std::move(rhs.m_stopTime)) {} MotorSafety& MotorSafety::operator=(MotorSafety&& rhs) { std::scoped_lock lock(m_thisMutex, rhs.m_thisMutex); - ErrorBase::operator=(std::move(rhs)); - m_expiration = std::move(rhs.m_expiration); m_enabled = std::move(rhs.m_enabled); m_stopTime = std::move(rhs.m_stopTime); @@ -97,7 +94,7 @@ void MotorSafety::Check() { wpi::raw_svector_ostream desc(buf); GetDescription(desc); desc << "... Output not updated often enough."; - wpi_setWPIErrorWithContext(Timeout, desc.str()); + FRC_ReportError(err::Timeout, desc.str()); StopMotor(); } } diff --git a/wpilibc/src/main/native/cpp/Notifier.cpp b/wpilibc/src/main/native/cpp/Notifier.cpp index 72f8a1d08c..c65a68ce2a 100644 --- a/wpilibc/src/main/native/cpp/Notifier.cpp +++ b/wpilibc/src/main/native/cpp/Notifier.cpp @@ -11,20 +11,20 @@ #include #include +#include "frc/Errors.h" #include "frc/Timer.h" #include "frc/Utility.h" -#include "frc/WPIErrors.h" using namespace frc; Notifier::Notifier(std::function handler) { - if (handler == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "handler must not be nullptr"); + if (!handler) { + throw FRC_MakeError(err::NullParameter, "handler"); } m_handler = handler; int32_t status = 0; m_notifier = HAL_InitializeNotifier(&status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "InitializeNotifier"); m_thread = std::thread([=] { for (;;) { @@ -60,13 +60,13 @@ Notifier::Notifier(std::function handler) { } Notifier::Notifier(int priority, std::function handler) { - if (handler == nullptr) { - wpi_setWPIErrorWithContext(NullParameter, "handler must not be nullptr"); + if (!handler) { + throw FRC_MakeError(err::NullParameter, "handler"); } m_handler = handler; int32_t status = 0; m_notifier = HAL_InitializeNotifier(&status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "InitializeNotifier"); m_thread = std::thread([=] { int32_t status = 0; @@ -107,7 +107,7 @@ Notifier::~Notifier() { // atomically set handle to 0, then clean HAL_NotifierHandle handle = m_notifier.exchange(0); HAL_StopNotifier(handle, &status); - wpi_setHALError(status); + FRC_ReportError(status, "StopNotifier"); // Join the thread to ensure the handler has exited. if (m_thread.joinable()) { @@ -118,8 +118,7 @@ Notifier::~Notifier() { } Notifier::Notifier(Notifier&& rhs) - : ErrorBase(std::move(rhs)), - m_thread(std::move(rhs.m_thread)), + : m_thread(std::move(rhs.m_thread)), m_notifier(rhs.m_notifier.load()), m_handler(std::move(rhs.m_handler)), m_expirationTime(std::move(rhs.m_expirationTime)), @@ -129,8 +128,6 @@ Notifier::Notifier(Notifier&& rhs) } Notifier& Notifier::operator=(Notifier&& rhs) { - ErrorBase::operator=(std::move(rhs)); - m_thread = std::move(rhs.m_thread); m_notifier = rhs.m_notifier.load(); rhs.m_notifier = HAL_kInvalidHandle; @@ -183,7 +180,7 @@ void Notifier::Stop() { m_periodic = false; int32_t status = 0; HAL_CancelNotifierAlarm(m_notifier, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "CancelNotifierAlarm"); } void Notifier::UpdateAlarm(uint64_t triggerTime) { @@ -194,7 +191,7 @@ void Notifier::UpdateAlarm(uint64_t triggerTime) { return; } HAL_UpdateNotifierAlarm(notifier, triggerTime, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "UpdateNotifierAlarm"); } void Notifier::UpdateAlarm() { diff --git a/wpilibc/src/main/native/cpp/PWM.cpp b/wpilibc/src/main/native/cpp/PWM.cpp index 2f4e1fe17f..d9c0a27454 100644 --- a/wpilibc/src/main/native/cpp/PWM.cpp +++ b/wpilibc/src/main/native/cpp/PWM.cpp @@ -11,9 +11,9 @@ #include #include +#include "frc/Errors.h" #include "frc/SensorUtil.h" #include "frc/Utility.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -21,27 +21,22 @@ using namespace frc; PWM::PWM(int channel, bool registerSendable) { if (!SensorUtil::CheckPWMChannel(channel)) { - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, - "PWM Channel " + wpi::Twine(channel)); + throw FRC_MakeError(err::ChannelIndexOutOfRange, + "PWM Channel " + wpi::Twine{channel}); return; } int32_t status = 0; m_handle = HAL_InitializePWMPort(HAL_GetPort(channel), &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumPWMChannels(), channel); - m_channel = std::numeric_limits::max(); - m_handle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "PWM Channel " + wpi::Twine{channel}); m_channel = channel; HAL_SetPWMDisabled(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetPWMDisabled"); status = 0; HAL_SetPWMEliminateDeadband(m_handle, false, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetPWMEliminateDeadband"); HAL_Report(HALUsageReporting::kResourceType_PWM, channel + 1); if (registerSendable) { @@ -53,88 +48,59 @@ PWM::~PWM() { int32_t status = 0; HAL_SetPWMDisabled(m_handle, &status); - wpi_setHALError(status); + FRC_ReportError(status, "SetPWMDisabled"); HAL_FreePWMPort(m_handle, &status); - wpi_setHALError(status); + FRC_ReportError(status, "FreePWM"); } void PWM::SetRaw(uint16_t value) { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; HAL_SetPWMRaw(m_handle, value, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetRaw"); } uint16_t PWM::GetRaw() const { - if (StatusIsFatal()) { - return 0; - } - int32_t status = 0; uint16_t value = HAL_GetPWMRaw(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetRaw"); return value; } void PWM::SetPosition(double pos) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetPWMPosition(m_handle, pos, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetPosition"); } double PWM::GetPosition() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double position = HAL_GetPWMPosition(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetPosition"); return position; } void PWM::SetSpeed(double speed) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetPWMSpeed(m_handle, speed, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetSpeed"); } double PWM::GetSpeed() const { - if (StatusIsFatal()) { - return 0.0; - } int32_t status = 0; double speed = HAL_GetPWMSpeed(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetSpeed"); return speed; } void PWM::SetDisabled() { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; - HAL_SetPWMDisabled(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetDisabled"); } void PWM::SetPeriodMultiplier(PeriodMultiplier mult) { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; switch (mult) { @@ -153,49 +119,35 @@ void PWM::SetPeriodMultiplier(PeriodMultiplier mult) { wpi_assert(false); } - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetPeriodMultiplier"); } void PWM::SetZeroLatch() { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; - HAL_LatchPWMZero(m_handle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetZeroLatch"); } void PWM::EnableDeadbandElimination(bool eliminateDeadband) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetPWMEliminateDeadband(m_handle, eliminateDeadband, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "EnableDeadbandElimination"); } void PWM::SetBounds(double max, double deadbandMax, double center, double deadbandMin, double min) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetPWMConfig(m_handle, max, deadbandMax, center, deadbandMin, min, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetBounds"); } void PWM::SetRawBounds(int max, int deadbandMax, int center, int deadbandMin, int min) { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetRawBounds"); } void PWM::GetRawBounds(int* max, int* deadbandMax, int* center, @@ -203,7 +155,7 @@ void PWM::GetRawBounds(int* max, int* deadbandMax, int* center, int32_t status = 0; HAL_GetPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetRawBounds"); } int PWM::GetChannel() const { diff --git a/wpilibc/src/main/native/cpp/PowerDistributionPanel.cpp b/wpilibc/src/main/native/cpp/PowerDistributionPanel.cpp index b955f7f2ae..bb95c7bf0c 100644 --- a/wpilibc/src/main/native/cpp/PowerDistributionPanel.cpp +++ b/wpilibc/src/main/native/cpp/PowerDistributionPanel.cpp @@ -7,11 +7,9 @@ #include #include #include -#include -#include +#include "frc/Errors.h" #include "frc/SensorUtil.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -25,10 +23,7 @@ PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {} PowerDistributionPanel::PowerDistributionPanel(int module) : m_module(module) { int32_t status = 0; m_handle = HAL_InitializePDP(module, &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumPDPModules(), module); - return; - } + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{module}); HAL_Report(HALUsageReporting::kResourceType_PDP, module + 1); SendableRegistry::GetInstance().AddLW(this, "PowerDistributionPanel", module); @@ -36,25 +31,15 @@ PowerDistributionPanel::PowerDistributionPanel(int module) : m_module(module) { double PowerDistributionPanel::GetVoltage() const { int32_t status = 0; - double voltage = HAL_GetPDPVoltage(m_handle, &status); - - if (status) { - wpi_setWPIErrorWithContext(Timeout, ""); - } - + FRC_CheckErrorStatus(status, "GetVoltage"); return voltage; } double PowerDistributionPanel::GetTemperature() const { int32_t status = 0; - double temperature = HAL_GetPDPTemperature(m_handle, &status); - - if (status) { - wpi_setWPIErrorWithContext(Timeout, ""); - } - + FRC_CheckErrorStatus(status, "GetTemperature"); return temperature; } @@ -62,75 +47,48 @@ double PowerDistributionPanel::GetCurrent(int channel) const { int32_t status = 0; if (!SensorUtil::CheckPDPChannel(channel)) { - wpi::SmallString<32> str; - wpi::raw_svector_ostream buf(str); - buf << "PDP Channel " << channel; - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); + FRC_ReportError(err::ChannelIndexOutOfRange, + "Channel " + wpi::Twine{channel}); + return 0; } double current = HAL_GetPDPChannelCurrent(m_handle, channel, &status); - - if (status) { - wpi_setWPIErrorWithContext(Timeout, ""); - } + FRC_CheckErrorStatus(status, "Channel " + wpi::Twine{channel}); return current; } double PowerDistributionPanel::GetTotalCurrent() const { int32_t status = 0; - double current = HAL_GetPDPTotalCurrent(m_handle, &status); - - if (status) { - wpi_setWPIErrorWithContext(Timeout, ""); - } - + FRC_CheckErrorStatus(status, "GetTotalCurrent"); return current; } double PowerDistributionPanel::GetTotalPower() const { int32_t status = 0; - double power = HAL_GetPDPTotalPower(m_handle, &status); - - if (status) { - wpi_setWPIErrorWithContext(Timeout, ""); - } - + FRC_CheckErrorStatus(status, "GetTotalPower"); return power; } double PowerDistributionPanel::GetTotalEnergy() const { int32_t status = 0; - double energy = HAL_GetPDPTotalEnergy(m_handle, &status); - - if (status) { - wpi_setWPIErrorWithContext(Timeout, ""); - } - + FRC_CheckErrorStatus(status, "GetTotalEnergy"); return energy; } void PowerDistributionPanel::ResetTotalEnergy() { int32_t status = 0; - HAL_ResetPDPTotalEnergy(m_handle, &status); - - if (status) { - wpi_setWPIErrorWithContext(Timeout, ""); - } + FRC_CheckErrorStatus(status, "ResetTotalEnergy"); } void PowerDistributionPanel::ClearStickyFaults() { int32_t status = 0; - HAL_ClearPDPStickyFaults(m_handle, &status); - - if (status) { - wpi_setWPIErrorWithContext(Timeout, ""); - } + FRC_CheckErrorStatus(status, "ClearStickyFaults"); } int PowerDistributionPanel::GetModule() const { diff --git a/wpilibc/src/main/native/cpp/Preferences.cpp b/wpilibc/src/main/native/cpp/Preferences.cpp index 8fa43a51db..8526c943b5 100644 --- a/wpilibc/src/main/native/cpp/Preferences.cpp +++ b/wpilibc/src/main/native/cpp/Preferences.cpp @@ -10,8 +10,6 @@ #include #include -#include "frc/WPIErrors.h" - using namespace frc; // The Preferences table name diff --git a/wpilibc/src/main/native/cpp/Relay.cpp b/wpilibc/src/main/native/cpp/Relay.cpp index 395b1eb617..66c63c3516 100644 --- a/wpilibc/src/main/native/cpp/Relay.cpp +++ b/wpilibc/src/main/native/cpp/Relay.cpp @@ -12,8 +12,8 @@ #include #include +#include "frc/Errors.h" #include "frc/SensorUtil.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -22,8 +22,8 @@ using namespace frc; Relay::Relay(int channel, Relay::Direction direction) : m_channel(channel), m_direction(direction) { if (!SensorUtil::CheckRelayChannel(m_channel)) { - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, - "Relay Channel " + wpi::Twine(m_channel)); + throw FRC_MakeError(err::ChannelIndexOutOfRange, + "Relay Channel " + wpi::Twine{m_channel}); return; } @@ -32,45 +32,24 @@ Relay::Relay(int channel, Relay::Direction direction) if (m_direction == kBothDirections || m_direction == kForwardOnly) { int32_t status = 0; m_forwardHandle = HAL_InitializeRelayPort(portHandle, true, &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumRelayChannels(), channel); - m_forwardHandle = HAL_kInvalidHandle; - m_reverseHandle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "Relay Channel " + wpi::Twine{m_channel}); HAL_Report(HALUsageReporting::kResourceType_Relay, m_channel + 1); } if (m_direction == kBothDirections || m_direction == kReverseOnly) { int32_t status = 0; m_reverseHandle = HAL_InitializeRelayPort(portHandle, false, &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumRelayChannels(), channel); - m_forwardHandle = HAL_kInvalidHandle; - m_reverseHandle = HAL_kInvalidHandle; - return; - } - + FRC_CheckErrorStatus(status, "Relay Channel " + wpi::Twine{m_channel}); HAL_Report(HALUsageReporting::kResourceType_Relay, m_channel + 128); } int32_t status = 0; if (m_forwardHandle != HAL_kInvalidHandle) { HAL_SetRelay(m_forwardHandle, false, &status); - if (status != 0) { - wpi_setHALError(status); - m_forwardHandle = HAL_kInvalidHandle; - m_reverseHandle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "Relay Channel " + wpi::Twine{m_channel}); } if (m_reverseHandle != HAL_kInvalidHandle) { HAL_SetRelay(m_reverseHandle, false, &status); - if (status != 0) { - wpi_setHALError(status); - m_forwardHandle = HAL_kInvalidHandle; - m_reverseHandle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "Relay Channel " + wpi::Twine{m_channel}); } SendableRegistry::GetInstance().AddLW(this, "Relay", m_channel); @@ -90,10 +69,6 @@ Relay::~Relay() { } void Relay::Set(Relay::Value value) { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; switch (value) { @@ -115,7 +90,7 @@ void Relay::Set(Relay::Value value) { break; case kForward: if (m_direction == kReverseOnly) { - wpi_setWPIError(IncompatibleMode); + FRC_ReportError(err::IncompatibleMode, "setting forward"); break; } if (m_direction == kBothDirections || m_direction == kForwardOnly) { @@ -127,7 +102,7 @@ void Relay::Set(Relay::Value value) { break; case kReverse: if (m_direction == kForwardOnly) { - wpi_setWPIError(IncompatibleMode); + FRC_ReportError(err::IncompatibleMode, "setting reverse"); break; } if (m_direction == kBothDirections) { @@ -139,7 +114,7 @@ void Relay::Set(Relay::Value value) { break; } - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Set"); } Relay::Value Relay::Get() const { @@ -173,7 +148,7 @@ Relay::Value Relay::Get() const { } } - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Get"); } int Relay::GetChannel() const { diff --git a/wpilibc/src/main/native/cpp/Resource.cpp b/wpilibc/src/main/native/cpp/Resource.cpp index dd16112d98..e41a43c778 100644 --- a/wpilibc/src/main/native/cpp/Resource.cpp +++ b/wpilibc/src/main/native/cpp/Resource.cpp @@ -4,8 +4,7 @@ #include "frc/Resource.h" -#include "frc/ErrorBase.h" -#include "frc/WPIErrors.h" +#include "frc/Errors.h" using namespace frc; @@ -31,19 +30,16 @@ uint32_t Resource::Allocate(const std::string& resourceDesc) { return i; } } - wpi_setWPIErrorWithContext(NoAvailableResources, resourceDesc); - return std::numeric_limits::max(); + throw FRC_MakeError(err::NoAvailableResources, resourceDesc); } uint32_t Resource::Allocate(uint32_t index, const std::string& resourceDesc) { std::scoped_lock lock(m_allocateMutex); if (index >= m_isAllocated.size()) { - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc); - return std::numeric_limits::max(); + throw FRC_MakeError(err::ChannelIndexOutOfRange, resourceDesc); } if (m_isAllocated[index]) { - wpi_setWPIErrorWithContext(ResourceAlreadyAllocated, resourceDesc); - return std::numeric_limits::max(); + throw FRC_MakeError(err::ResourceAlreadyAllocated, resourceDesc); } m_isAllocated[index] = true; return index; @@ -55,12 +51,10 @@ void Resource::Free(uint32_t index) { return; } if (index >= m_isAllocated.size()) { - wpi_setWPIError(NotAllocated); - return; + throw FRC_MakeError(err::NotAllocated, "index " + wpi::Twine{index}); } if (!m_isAllocated[index]) { - wpi_setWPIError(NotAllocated); - return; + throw FRC_MakeError(err::NotAllocated, "index " + wpi::Twine{index}); } m_isAllocated[index] = false; } diff --git a/wpilibc/src/main/native/cpp/RobotController.cpp b/wpilibc/src/main/native/cpp/RobotController.cpp index ac49cfa225..88da69b7d0 100644 --- a/wpilibc/src/main/native/cpp/RobotController.cpp +++ b/wpilibc/src/main/native/cpp/RobotController.cpp @@ -8,156 +8,154 @@ #include #include -#include "frc/ErrorBase.h" +#include "frc/Errors.h" using namespace frc; int RobotController::GetFPGAVersion() { int32_t status = 0; int version = HAL_GetFPGAVersion(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetFPGAVersion"); return version; } int64_t RobotController::GetFPGARevision() { int32_t status = 0; int64_t revision = HAL_GetFPGARevision(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetFPGARevision"); return revision; } uint64_t RobotController::GetFPGATime() { int32_t status = 0; uint64_t time = HAL_GetFPGATime(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetFPGATime"); return time; } bool RobotController::GetUserButton() { int32_t status = 0; - bool value = HAL_GetFPGAButton(&status); - wpi_setGlobalError(status); - + FRC_CheckErrorStatus(status, "GetUserButton"); return value; } units::volt_t RobotController::GetBatteryVoltage() { int32_t status = 0; double retVal = HAL_GetVinVoltage(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetBatteryVoltage"); return units::volt_t{retVal}; } bool RobotController::IsSysActive() { int32_t status = 0; bool retVal = HAL_GetSystemActive(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "IsSysActive"); return retVal; } bool RobotController::IsBrownedOut() { int32_t status = 0; bool retVal = HAL_GetBrownedOut(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "IsBrownedOut"); return retVal; } double RobotController::GetInputVoltage() { int32_t status = 0; double retVal = HAL_GetVinVoltage(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetInputVoltage"); return retVal; } double RobotController::GetInputCurrent() { int32_t status = 0; double retVal = HAL_GetVinCurrent(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetInputCurrent"); return retVal; } double RobotController::GetVoltage3V3() { int32_t status = 0; double retVal = HAL_GetUserVoltage3V3(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetVoltage3V3"); return retVal; } double RobotController::GetCurrent3V3() { int32_t status = 0; double retVal = HAL_GetUserCurrent3V3(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetCurrent3V3"); return retVal; } bool RobotController::GetEnabled3V3() { int32_t status = 0; bool retVal = HAL_GetUserActive3V3(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetEnabled3V3"); return retVal; } int RobotController::GetFaultCount3V3() { int32_t status = 0; int retVal = HAL_GetUserCurrentFaults3V3(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetFaultCount3V3"); return retVal; } double RobotController::GetVoltage5V() { int32_t status = 0; double retVal = HAL_GetUserVoltage5V(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetVoltage5V"); return retVal; } double RobotController::GetCurrent5V() { int32_t status = 0; double retVal = HAL_GetUserCurrent5V(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetCurrent5V"); return retVal; } bool RobotController::GetEnabled5V() { int32_t status = 0; bool retVal = HAL_GetUserActive5V(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetEnabled5V"); return retVal; } int RobotController::GetFaultCount5V() { int32_t status = 0; int retVal = HAL_GetUserCurrentFaults5V(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetFaultCount5V"); return retVal; } double RobotController::GetVoltage6V() { int32_t status = 0; double retVal = HAL_GetUserVoltage6V(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetVoltage6V"); return retVal; } double RobotController::GetCurrent6V() { int32_t status = 0; double retVal = HAL_GetUserCurrent6V(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetCurrent6V"); return retVal; } bool RobotController::GetEnabled6V() { int32_t status = 0; bool retVal = HAL_GetUserActive6V(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetEnabled6V"); return retVal; } int RobotController::GetFaultCount6V() { int32_t status = 0; int retVal = HAL_GetUserCurrentFaults6V(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetFaultCount6V"); return retVal; } @@ -170,10 +168,7 @@ CANStatus RobotController::GetCANStatus() { uint32_t transmitErrorCount = 0; HAL_CAN_GetCANStatus(&percentBusUtilization, &busOffCount, &txFullCount, &receiveErrorCount, &transmitErrorCount, &status); - if (status != 0) { - wpi_setGlobalHALError(status); - return {}; - } + FRC_CheckErrorStatus(status, "GetCANStatus"); return {percentBusUtilization, static_cast(busOffCount), static_cast(txFullCount), static_cast(receiveErrorCount), static_cast(transmitErrorCount)}; diff --git a/wpilibc/src/main/native/cpp/SPI.cpp b/wpilibc/src/main/native/cpp/SPI.cpp index 12ebb172d1..9392723b6d 100644 --- a/wpilibc/src/main/native/cpp/SPI.cpp +++ b/wpilibc/src/main/native/cpp/SPI.cpp @@ -14,8 +14,8 @@ #include #include "frc/DigitalSource.h" +#include "frc/Errors.h" #include "frc/Notifier.h" -#include "frc/WPIErrors.h" using namespace frc; @@ -77,9 +77,7 @@ void SPI::Accumulator::Update() { // get amount of data available int32_t numToRead = HAL_ReadSPIAutoReceivedData(m_port, m_buf, 0, 0, &status); - if (status != 0) { - return; // error reading - } + FRC_CheckErrorStatus(status, "ReadSPIAutoReceivedData"); // only get whole responses; +1 is for timestamp numToRead -= numToRead % m_xferSize; @@ -93,9 +91,7 @@ void SPI::Accumulator::Update() { // read buffered data HAL_ReadSPIAutoReceivedData(m_port, m_buf, numToRead, 0, &status); - if (status != 0) { - return; // error reading - } + FRC_CheckErrorStatus(status, "ReadSPIAutoReceivedData"); // loop over all responses for (int32_t off = 0; off < numToRead; off += m_xferSize) { @@ -162,7 +158,7 @@ void SPI::Accumulator::Update() { SPI::SPI(Port port) : m_port(static_cast(port)) { int32_t status = 0; HAL_InitializeSPI(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "InitializeSPI"); HAL_Report(HALUsageReporting::kResourceType_SPI, static_cast(port) + 1); @@ -219,13 +215,13 @@ void SPI::SetClockActiveHigh() { void SPI::SetChipSelectActiveHigh() { int32_t status = 0; HAL_SetSPIChipSelectActiveHigh(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetChipSelectActiveHigh"); } void SPI::SetChipSelectActiveLow() { int32_t status = 0; HAL_SetSPIChipSelectActiveLow(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetChipSelectActiveLow"); } int SPI::Write(uint8_t* data, int size) { @@ -255,26 +251,26 @@ int SPI::Transaction(uint8_t* dataToSend, uint8_t* dataReceived, int size) { void SPI::InitAuto(int bufferSize) { int32_t status = 0; HAL_InitSPIAuto(m_port, bufferSize, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "InitAuto"); } void SPI::FreeAuto() { int32_t status = 0; HAL_FreeSPIAuto(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "FreeAuto"); } void SPI::SetAutoTransmitData(wpi::ArrayRef dataToSend, int zeroSize) { int32_t status = 0; HAL_SetSPIAutoTransmitData(m_port, dataToSend.data(), dataToSend.size(), zeroSize, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetAutoTransmitData"); } void SPI::StartAutoRate(units::second_t period) { int32_t status = 0; HAL_StartSPIAutoRate(m_port, period.to(), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "StartAutoRate"); } void SPI::StartAutoRate(double period) { @@ -287,19 +283,19 @@ void SPI::StartAutoTrigger(DigitalSource& source, bool rising, bool falling) { static_cast( source.GetAnalogTriggerTypeForRouting()), rising, falling, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "StartAutoTrigger"); } void SPI::StopAuto() { int32_t status = 0; HAL_StopSPIAuto(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "StopAuto"); } void SPI::ForceAutoRead() { int32_t status = 0; HAL_ForceSPIAutoRead(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ForceAutoRead"); } int SPI::ReadAutoReceivedData(uint32_t* buffer, int numToRead, @@ -307,7 +303,7 @@ int SPI::ReadAutoReceivedData(uint32_t* buffer, int numToRead, int32_t status = 0; int32_t val = HAL_ReadSPIAutoReceivedData(m_port, buffer, numToRead, timeout.to(), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ReadAutoReceivedData"); return val; } @@ -318,7 +314,7 @@ int SPI::ReadAutoReceivedData(uint32_t* buffer, int numToRead, double timeout) { int SPI::GetAutoDroppedCount() { int32_t status = 0; int32_t val = HAL_GetSPIAutoDroppedCount(m_port, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetAutoDroppedCount"); return val; } @@ -327,7 +323,7 @@ void SPI::ConfigureAutoStall(HAL_SPIPort port, int csToSclkTicks, int32_t status = 0; HAL_ConfigureSPIAutoStall(m_port, csToSclkTicks, stallTicks, pow2BytesPerRead, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "ConfigureAutoStall"); } void SPI::InitAccumulator(units::second_t period, int cmd, int xferSize, diff --git a/wpilibc/src/main/native/cpp/SerialPort.cpp b/wpilibc/src/main/native/cpp/SerialPort.cpp index 70c7fb52d1..6640286477 100644 --- a/wpilibc/src/main/native/cpp/SerialPort.cpp +++ b/wpilibc/src/main/native/cpp/SerialPort.cpp @@ -9,6 +9,8 @@ #include #include +#include "frc/Errors.h" + using namespace frc; SerialPort::SerialPort(int baudRate, Port port, int dataBits, @@ -18,19 +20,17 @@ SerialPort::SerialPort(int baudRate, Port port, int dataBits, m_portHandle = HAL_InitializeSerialPort(static_cast(port), &status); - wpi_setHALError(status); - // Don't continue if initialization failed - if (status < 0) { - return; - } + FRC_CheckErrorStatus(status, "Port " + wpi::Twine{static_cast(port)}); HAL_SetSerialBaudRate(m_portHandle, baudRate, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetSerialBaudRate " + wpi::Twine{baudRate}); HAL_SetSerialDataBits(m_portHandle, dataBits, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetSerialDataBits " + wpi::Twine{dataBits}); HAL_SetSerialParity(m_portHandle, parity, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus( + status, "SetSerialParity " + wpi::Twine{static_cast(parity)}); HAL_SetSerialStopBits(m_portHandle, stopBits, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus( + status, "SetSerialStopBits " + wpi::Twine{static_cast(stopBits)}); // Set the default timeout to 5 seconds. SetTimeout(5.0); @@ -54,19 +54,17 @@ SerialPort::SerialPort(int baudRate, const wpi::Twine& portName, Port port, m_portHandle = HAL_InitializeSerialPortDirect( static_cast(port), portNameC, &status); - wpi_setHALError(status); - // Don't continue if initialization failed - if (status < 0) { - return; - } + FRC_CheckErrorStatus(status, "Port " + wpi::Twine{static_cast(port)}); HAL_SetSerialBaudRate(m_portHandle, baudRate, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetSerialBaudRate " + wpi::Twine{baudRate}); HAL_SetSerialDataBits(m_portHandle, dataBits, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetSerialDataBits " + wpi::Twine{dataBits}); HAL_SetSerialParity(m_portHandle, parity, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus( + status, "SetSerialParity " + wpi::Twine{static_cast(parity)}); HAL_SetSerialStopBits(m_portHandle, stopBits, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus( + status, "SetSerialStopBits " + wpi::Twine{static_cast(stopBits)}); // Set the default timeout to 5 seconds. SetTimeout(5.0); @@ -83,38 +81,40 @@ SerialPort::SerialPort(int baudRate, const wpi::Twine& portName, Port port, SerialPort::~SerialPort() { int32_t status = 0; HAL_CloseSerial(m_portHandle, &status); - wpi_setHALError(status); + FRC_ReportError(status, "CloseSerial"); } void SerialPort::SetFlowControl(SerialPort::FlowControl flowControl) { int32_t status = 0; HAL_SetSerialFlowControl(m_portHandle, flowControl, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus( + status, "SetFlowControl " + wpi::Twine{static_cast(flowControl)}); } void SerialPort::EnableTermination(char terminator) { int32_t status = 0; HAL_EnableSerialTermination(m_portHandle, terminator, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus( + status, "EnableTermination " + wpi::Twine{static_cast(terminator)}); } void SerialPort::DisableTermination() { int32_t status = 0; HAL_DisableSerialTermination(m_portHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "DisableTermination"); } int SerialPort::GetBytesReceived() { int32_t status = 0; int retVal = HAL_GetSerialBytesReceived(m_portHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "GetBytesReceived"); return retVal; } int SerialPort::Read(char* buffer, int count) { int32_t status = 0; int retVal = HAL_ReadSerial(m_portHandle, buffer, count, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Read"); return retVal; } @@ -126,42 +126,43 @@ int SerialPort::Write(wpi::StringRef buffer) { int32_t status = 0; int retVal = HAL_WriteSerial(m_portHandle, buffer.data(), buffer.size(), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Write"); return retVal; } void SerialPort::SetTimeout(double timeout) { int32_t status = 0; HAL_SetSerialTimeout(m_portHandle, timeout, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetTimeout"); } void SerialPort::SetReadBufferSize(int size) { int32_t status = 0; HAL_SetSerialReadBufferSize(m_portHandle, size, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetReadBufferSize " + wpi::Twine{size}); } void SerialPort::SetWriteBufferSize(int size) { int32_t status = 0; HAL_SetSerialWriteBufferSize(m_portHandle, size, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetWriteBufferSize " + wpi::Twine{size}); } void SerialPort::SetWriteBufferMode(SerialPort::WriteBufferMode mode) { int32_t status = 0; HAL_SetSerialWriteMode(m_portHandle, mode, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus( + status, "SetWriteBufferMode " + wpi::Twine{static_cast(mode)}); } void SerialPort::Flush() { int32_t status = 0; HAL_FlushSerial(m_portHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Flush"); } void SerialPort::Reset() { int32_t status = 0; HAL_ClearSerial(m_portHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Reset"); } diff --git a/wpilibc/src/main/native/cpp/Solenoid.cpp b/wpilibc/src/main/native/cpp/Solenoid.cpp index ec54e15df4..1ead9fd130 100644 --- a/wpilibc/src/main/native/cpp/Solenoid.cpp +++ b/wpilibc/src/main/native/cpp/Solenoid.cpp @@ -11,8 +11,8 @@ #include #include +#include "frc/Errors.h" #include "frc/SensorUtil.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -24,24 +24,19 @@ Solenoid::Solenoid(int channel) Solenoid::Solenoid(int moduleNumber, int channel) : SolenoidBase(moduleNumber), m_channel(channel) { if (!SensorUtil::CheckSolenoidModule(m_moduleNumber)) { - wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, - "Solenoid Module " + wpi::Twine(m_moduleNumber)); - return; + throw FRC_MakeError(err::ModuleIndexOutOfRange, + "Solenoid Module " + wpi::Twine{m_moduleNumber}); } if (!SensorUtil::CheckSolenoidChannel(m_channel)) { - wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, - "Solenoid Channel " + wpi::Twine(m_channel)); - return; + throw FRC_MakeError(err::ChannelIndexOutOfRange, + "Solenoid Channel " + wpi::Twine{m_channel}); } int32_t status = 0; m_solenoidHandle = HAL_InitializeSolenoidPort( HAL_GetPortWithModule(moduleNumber, channel), &status); - if (status != 0) { - wpi_setHALErrorWithRange(status, 0, HAL_GetNumSolenoidChannels(), channel); - m_solenoidHandle = HAL_kInvalidHandle; - return; - } + FRC_CheckErrorStatus(status, "Solenoid Module " + wpi::Twine{m_moduleNumber} + + " Channel " + wpi::Twine{m_channel}); HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_channel + 1, m_moduleNumber + 1); @@ -54,23 +49,15 @@ Solenoid::~Solenoid() { } void Solenoid::Set(bool on) { - if (StatusIsFatal()) { - return; - } - int32_t status = 0; HAL_SetSolenoid(m_solenoidHandle, on, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Set"); } bool Solenoid::Get() const { - if (StatusIsFatal()) { - return false; - } - int32_t status = 0; bool value = HAL_GetSolenoid(m_solenoidHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "Get"); return value; } @@ -90,21 +77,15 @@ bool Solenoid::IsBlackListed() const { void Solenoid::SetPulseDuration(double durationSeconds) { int32_t durationMS = durationSeconds * 1000; - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_SetOneShotDuration(m_solenoidHandle, durationMS, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "SetPulseDuration"); } void Solenoid::StartPulse() { - if (StatusIsFatal()) { - return; - } int32_t status = 0; HAL_FireOneShot(m_solenoidHandle, &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "StartPulse"); } void Solenoid::InitSendable(SendableBuilder& builder) { diff --git a/wpilibc/src/main/native/cpp/SolenoidBase.cpp b/wpilibc/src/main/native/cpp/SolenoidBase.cpp index c8b64f4b6a..e4598e1ea5 100644 --- a/wpilibc/src/main/native/cpp/SolenoidBase.cpp +++ b/wpilibc/src/main/native/cpp/SolenoidBase.cpp @@ -7,13 +7,15 @@ #include #include +#include "frc/Errors.h" + using namespace frc; int SolenoidBase::GetAll(int module) { int value = 0; int32_t status = 0; value = HAL_GetAllSolenoids(module, &status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{module}); return value; } @@ -23,7 +25,9 @@ int SolenoidBase::GetAll() const { int SolenoidBase::GetPCMSolenoidBlackList(int module) { int32_t status = 0; - return HAL_GetPCMSolenoidBlackList(module, &status); + int rv = HAL_GetPCMSolenoidBlackList(module, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{module}); + return rv; } int SolenoidBase::GetPCMSolenoidBlackList() const { @@ -32,7 +36,9 @@ int SolenoidBase::GetPCMSolenoidBlackList() const { bool SolenoidBase::GetPCMSolenoidVoltageStickyFault(int module) { int32_t status = 0; - return HAL_GetPCMSolenoidVoltageStickyFault(module, &status); + bool rv = HAL_GetPCMSolenoidVoltageStickyFault(module, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{module}); + return rv; } bool SolenoidBase::GetPCMSolenoidVoltageStickyFault() const { @@ -41,7 +47,9 @@ bool SolenoidBase::GetPCMSolenoidVoltageStickyFault() const { bool SolenoidBase::GetPCMSolenoidVoltageFault(int module) { int32_t status = 0; - return HAL_GetPCMSolenoidVoltageFault(module, &status); + bool rv = HAL_GetPCMSolenoidVoltageFault(module, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{module}); + return rv; } bool SolenoidBase::GetPCMSolenoidVoltageFault() const { @@ -50,7 +58,8 @@ bool SolenoidBase::GetPCMSolenoidVoltageFault() const { void SolenoidBase::ClearAllPCMStickyFaults(int module) { int32_t status = 0; - return HAL_ClearAllPCMStickyFaults(module, &status); + HAL_ClearAllPCMStickyFaults(module, &status); + FRC_CheckErrorStatus(status, "Module " + wpi::Twine{module}); } void SolenoidBase::ClearAllPCMStickyFaults() { diff --git a/wpilibc/src/main/native/cpp/Threads.cpp b/wpilibc/src/main/native/cpp/Threads.cpp index 1b1650aff4..3ecab5569a 100644 --- a/wpilibc/src/main/native/cpp/Threads.cpp +++ b/wpilibc/src/main/native/cpp/Threads.cpp @@ -7,7 +7,7 @@ #include #include -#include "frc/ErrorBase.h" +#include "frc/Errors.h" namespace frc { @@ -16,7 +16,7 @@ int GetThreadPriority(std::thread& thread, bool* isRealTime) { HAL_Bool rt = false; auto native = thread.native_handle(); auto ret = HAL_GetThreadPriority(&native, &rt, &status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetThreadPriority"); *isRealTime = rt; return ret; } @@ -25,7 +25,7 @@ int GetCurrentThreadPriority(bool* isRealTime) { int32_t status = 0; HAL_Bool rt = false; auto ret = HAL_GetCurrentThreadPriority(&rt, &status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "GetCurrentThreadPriority"); *isRealTime = rt; return ret; } @@ -34,14 +34,14 @@ bool SetThreadPriority(std::thread& thread, bool realTime, int priority) { int32_t status = 0; auto native = thread.native_handle(); auto ret = HAL_SetThreadPriority(&native, realTime, priority, &status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "SetThreadPriority"); return ret; } bool SetCurrentThreadPriority(bool realTime, int priority) { int32_t status = 0; auto ret = HAL_SetCurrentThreadPriority(realTime, priority, &status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "SetCurrentThreadPriority"); return ret; } diff --git a/wpilibc/src/main/native/cpp/TimedRobot.cpp b/wpilibc/src/main/native/cpp/TimedRobot.cpp index 2704d51010..0326859411 100644 --- a/wpilibc/src/main/native/cpp/TimedRobot.cpp +++ b/wpilibc/src/main/native/cpp/TimedRobot.cpp @@ -12,9 +12,9 @@ #include #include +#include "frc/Errors.h" #include "frc/Timer.h" #include "frc/Utility.h" -#include "frc/WPIErrors.h" using namespace frc; @@ -39,7 +39,7 @@ void TimedRobot::StartCompetition() { HAL_UpdateNotifierAlarm( m_notifier, static_cast(callback.expirationTime * 1e6), &status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "UpdateNotifierAlarm"); uint64_t curTime = HAL_WaitForNotifierAlarm(m_notifier, &status); if (curTime == 0 || status != 0) { @@ -81,7 +81,7 @@ TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) { int32_t status = 0; m_notifier = HAL_InitializeNotifier(&status); - wpi_setHALError(status); + FRC_CheckErrorStatus(status, "InitializeNotifier"); HAL_SetNotifierName(m_notifier, "TimedRobot", &status); HAL_Report(HALUsageReporting::kResourceType_Framework, @@ -92,7 +92,7 @@ TimedRobot::~TimedRobot() { int32_t status = 0; HAL_StopNotifier(m_notifier, &status); - wpi_setHALError(status); + FRC_ReportError(status, "StopNotifier"); HAL_CleanNotifier(m_notifier, &status); } diff --git a/wpilibc/src/main/native/cpp/Ultrasonic.cpp b/wpilibc/src/main/native/cpp/Ultrasonic.cpp index 140c66ae5b..2389eda1e0 100644 --- a/wpilibc/src/main/native/cpp/Ultrasonic.cpp +++ b/wpilibc/src/main/native/cpp/Ultrasonic.cpp @@ -12,9 +12,9 @@ #include "frc/Counter.h" #include "frc/DigitalInput.h" #include "frc/DigitalOutput.h" +#include "frc/Errors.h" #include "frc/Timer.h" #include "frc/Utility.h" -#include "frc/WPIErrors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -40,9 +40,11 @@ Ultrasonic::Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel) : m_pingChannel(pingChannel, NullDeleter()), m_echoChannel(echoChannel, NullDeleter()), m_counter(m_echoChannel) { - if (pingChannel == nullptr || echoChannel == nullptr) { - wpi_setWPIError(NullParameter); - return; + if (!pingChannel) { + throw FRC_MakeError(err::NullParameter, "pingChannel"); + } + if (!echoChannel) { + throw FRC_MakeError(err::NullParameter, "echoChannel"); } Initialize(); } diff --git a/wpilibc/src/main/native/cpp/Watchdog.cpp b/wpilibc/src/main/native/cpp/Watchdog.cpp index e93457f33f..5ba4273343 100644 --- a/wpilibc/src/main/native/cpp/Watchdog.cpp +++ b/wpilibc/src/main/native/cpp/Watchdog.cpp @@ -14,6 +14,7 @@ #include #include "frc/DriverStation.h" +#include "frc/Errors.h" #include "frc2/Timer.h" using namespace frc; @@ -47,7 +48,7 @@ class Watchdog::Impl { Watchdog::Impl::Impl() { int32_t status = 0; m_notifier = HAL_InitializeNotifier(&status); - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "starting watchdog notifier"); HAL_SetNotifierName(m_notifier, "Watchdog", &status); m_thread = std::thread([=] { Main(); }); @@ -58,7 +59,7 @@ Watchdog::Impl::~Impl() { // atomically set handle to 0, then clean HAL_NotifierHandle handle = m_notifier.exchange(0); HAL_StopNotifier(handle, &status); - wpi_setGlobalHALError(status); + FRC_ReportError(status, "stopping watchdog notifier"); // Join the thread to ensure the handler has exited. if (m_thread.joinable()) { @@ -84,7 +85,7 @@ void Watchdog::Impl::UpdateAlarm() { 1e6), &status); } - wpi_setGlobalHALError(status); + FRC_CheckErrorStatus(status, "updating watchdog notifier alarm"); } void Watchdog::Impl::Main() { @@ -141,7 +142,11 @@ Watchdog::Watchdog(units::second_t timeout, std::function callback) : m_timeout(timeout), m_callback(std::move(callback)), m_impl(GetImpl()) {} Watchdog::~Watchdog() { - Disable(); + try { + Disable(); + } catch (const RuntimeError& e) { + e.Report(); + } } Watchdog::Watchdog(Watchdog&& rhs) { diff --git a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardContainer.cpp b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardContainer.cpp index 84738bcde4..dc01b9f44a 100644 --- a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardContainer.cpp +++ b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardContainer.cpp @@ -7,6 +7,7 @@ #include #include +#include "frc/Errors.h" #include "frc/shuffleboard/ComplexWidget.h" #include "frc/shuffleboard/ShuffleboardComponent.h" #include "frc/shuffleboard/ShuffleboardLayout.h" @@ -56,8 +57,8 @@ ShuffleboardLayout& ShuffleboardContainer::GetLayout(const wpi::Twine& title) { wpi::SmallVector storage; auto titleRef = title.toStringRef(storage); if (m_layouts.count(titleRef) == 0) { - wpi_setWPIErrorWithContext( - InvalidParameter, "No layout with the given title has been defined"); + throw FRC_MakeError(err::InvalidParameter, + "No layout with the given title has been defined"); } return *m_layouts[titleRef]; } diff --git a/wpilibc/src/main/native/cpp/smartdashboard/SmartDashboard.cpp b/wpilibc/src/main/native/cpp/smartdashboard/SmartDashboard.cpp index 35feb9de11..3cbd29ffd7 100644 --- a/wpilibc/src/main/native/cpp/smartdashboard/SmartDashboard.cpp +++ b/wpilibc/src/main/native/cpp/smartdashboard/SmartDashboard.cpp @@ -10,7 +10,7 @@ #include #include -#include "frc/WPIErrors.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableRegistry.h" using namespace frc; @@ -85,9 +85,8 @@ nt::NetworkTableEntry SmartDashboard::GetEntry(wpi::StringRef key) { } void SmartDashboard::PutData(wpi::StringRef key, Sendable* data) { - if (data == nullptr) { - wpi_setGlobalWPIErrorWithContext(NullParameter, "value"); - return; + if (!data) { + throw FRC_MakeError(err::NullParameter, "value"); } auto& inst = Singleton::GetInstance(); std::scoped_lock lock(inst.tablesToDataMutex); @@ -103,9 +102,8 @@ void SmartDashboard::PutData(wpi::StringRef key, Sendable* data) { } void SmartDashboard::PutData(Sendable* value) { - if (value == nullptr) { - wpi_setGlobalWPIErrorWithContext(NullParameter, "value"); - return; + if (!value) { + throw FRC_MakeError(err::NullParameter, "value"); } auto name = SendableRegistry::GetInstance().GetName(value); if (!name.empty()) { @@ -118,8 +116,7 @@ Sendable* SmartDashboard::GetData(wpi::StringRef key) { std::scoped_lock lock(inst.tablesToDataMutex); auto it = inst.tablesToData.find(key); if (it == inst.tablesToData.end()) { - wpi_setGlobalWPIErrorWithContext(SmartDashboardMissingKey, key); - return nullptr; + throw FRC_MakeError(err::SmartDashboardMissingKey, key); } return SendableRegistry::GetInstance().GetSendable(it->getValue()); } diff --git a/wpilibc/src/main/native/cppcs/RobotBase.cpp b/wpilibc/src/main/native/cppcs/RobotBase.cpp index b112901e52..7a1ef244bd 100644 --- a/wpilibc/src/main/native/cppcs/RobotBase.cpp +++ b/wpilibc/src/main/native/cppcs/RobotBase.cpp @@ -18,9 +18,9 @@ #include "WPILibVersion.h" #include "frc/DriverStation.h" +#include "frc/Errors.h" #include "frc/RobotState.h" #include "frc/Utility.h" -#include "frc/WPIErrors.h" #include "frc/livewindow/LiveWindow.h" #include "frc/smartdashboard/SmartDashboard.h" @@ -54,10 +54,10 @@ class WPILibCameraServerShared : public frc::CameraServerShared { HAL_Report(HALUsageReporting::kResourceType_PCVideoServer, id); } void SetCameraServerError(const wpi::Twine& error) override { - wpi_setGlobalWPIErrorWithContext(CameraServerError, error); + FRC_ReportError(err::CameraServerError, error); } void SetVisionRunnerError(const wpi::Twine& error) override { - wpi_setGlobalErrorWithContext(-1, error); + FRC_ReportError(-1, error); } void ReportDriverStationError(const wpi::Twine& error) override { DriverStation::ReportError(error); diff --git a/wpilibc/src/main/native/include/frc/ADXL345_I2C.h b/wpilibc/src/main/native/include/frc/ADXL345_I2C.h index 7820751442..feedd4c67d 100644 --- a/wpilibc/src/main/native/include/frc/ADXL345_I2C.h +++ b/wpilibc/src/main/native/include/frc/ADXL345_I2C.h @@ -6,7 +6,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/I2C.h" #include "frc/interfaces/Accelerometer.h" #include "frc/smartdashboard/Sendable.h" @@ -23,8 +22,7 @@ class SendableBuilder; * an I2C bus. This class assumes the default (not alternate) sensor address of * 0x1D (7-bit address). */ -class ADXL345_I2C : public ErrorBase, - public Accelerometer, +class ADXL345_I2C : public Accelerometer, public Sendable, public SendableHelper { public: diff --git a/wpilibc/src/main/native/include/frc/ADXL345_SPI.h b/wpilibc/src/main/native/include/frc/ADXL345_SPI.h index ee16518760..bc6122343d 100644 --- a/wpilibc/src/main/native/include/frc/ADXL345_SPI.h +++ b/wpilibc/src/main/native/include/frc/ADXL345_SPI.h @@ -6,7 +6,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/SPI.h" #include "frc/interfaces/Accelerometer.h" #include "frc/smartdashboard/Sendable.h" @@ -20,8 +19,7 @@ namespace frc { * This class allows access to an Analog Devices ADXL345 3-axis accelerometer * via SPI. This class assumes the sensor is wired in 4-wire SPI mode. */ -class ADXL345_SPI : public ErrorBase, - public Accelerometer, +class ADXL345_SPI : public Accelerometer, public Sendable, public SendableHelper { public: diff --git a/wpilibc/src/main/native/include/frc/ADXL362.h b/wpilibc/src/main/native/include/frc/ADXL362.h index 39734e1d57..ca9bc9129c 100644 --- a/wpilibc/src/main/native/include/frc/ADXL362.h +++ b/wpilibc/src/main/native/include/frc/ADXL362.h @@ -6,7 +6,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/SPI.h" #include "frc/interfaces/Accelerometer.h" #include "frc/smartdashboard/Sendable.h" @@ -21,8 +20,7 @@ class SendableBuilder; * * This class allows access to an Analog Devices ADXL362 3-axis accelerometer. */ -class ADXL362 : public ErrorBase, - public Accelerometer, +class ADXL362 : public Accelerometer, public Sendable, public SendableHelper { public: diff --git a/wpilibc/src/main/native/include/frc/ADXRS450_Gyro.h b/wpilibc/src/main/native/include/frc/ADXRS450_Gyro.h index 556bb49a2e..69b6492f72 100644 --- a/wpilibc/src/main/native/include/frc/ADXRS450_Gyro.h +++ b/wpilibc/src/main/native/include/frc/ADXRS450_Gyro.h @@ -8,7 +8,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/SPI.h" #include "frc/interfaces/Gyro.h" #include "frc/smartdashboard/Sendable.h" @@ -30,7 +29,6 @@ namespace frc { * Only one instance of an ADXRS Gyro is supported. */ class ADXRS450_Gyro : public Gyro, - public ErrorBase, public Sendable, public SendableHelper { public: diff --git a/wpilibc/src/main/native/include/frc/AddressableLED.h b/wpilibc/src/main/native/include/frc/AddressableLED.h index d0b5428055..2cdff76bf2 100644 --- a/wpilibc/src/main/native/include/frc/AddressableLED.h +++ b/wpilibc/src/main/native/include/frc/AddressableLED.h @@ -11,7 +11,6 @@ #include #include -#include "frc/ErrorBase.h" #include "util/Color.h" #include "util/Color8Bit.h" @@ -22,7 +21,7 @@ namespace frc { * *

Only 1 LED driver is currently supported by the roboRIO. */ -class AddressableLED : public ErrorBase { +class AddressableLED { public: class LEDData : public HAL_AddressableLEDData { public: @@ -86,7 +85,7 @@ class AddressableLED : public ErrorBase { */ explicit AddressableLED(int port); - ~AddressableLED() override; + ~AddressableLED(); /** * Sets the length of the LED strip. diff --git a/wpilibc/src/main/native/include/frc/AnalogAccelerometer.h b/wpilibc/src/main/native/include/frc/AnalogAccelerometer.h index f157cd5d6a..acc5c5f8d1 100644 --- a/wpilibc/src/main/native/include/frc/AnalogAccelerometer.h +++ b/wpilibc/src/main/native/include/frc/AnalogAccelerometer.h @@ -7,7 +7,6 @@ #include #include "frc/AnalogInput.h" -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -22,8 +21,7 @@ class SendableBuilder; * sensors have multiple axis and can be treated as multiple devices. Each is * calibrated by finding the center value over a period of time. */ -class AnalogAccelerometer : public ErrorBase, - public Sendable, +class AnalogAccelerometer : public Sendable, public SendableHelper { public: /** diff --git a/wpilibc/src/main/native/include/frc/AnalogEncoder.h b/wpilibc/src/main/native/include/frc/AnalogEncoder.h index 40438e6460..1ac341412b 100644 --- a/wpilibc/src/main/native/include/frc/AnalogEncoder.h +++ b/wpilibc/src/main/native/include/frc/AnalogEncoder.h @@ -12,7 +12,6 @@ #include "frc/AnalogTrigger.h" #include "frc/Counter.h" -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -22,9 +21,7 @@ class AnalogInput; /** * Class for supporting continuous analog encoders, such as the US Digital MA3. */ -class AnalogEncoder : public ErrorBase, - public Sendable, - public SendableHelper { +class AnalogEncoder : public Sendable, public SendableHelper { public: /** * Construct a new AnalogEncoder attached to a specific AnalogInput. diff --git a/wpilibc/src/main/native/include/frc/AnalogGyro.h b/wpilibc/src/main/native/include/frc/AnalogGyro.h index e2646cec5a..ef36ab0126 100644 --- a/wpilibc/src/main/native/include/frc/AnalogGyro.h +++ b/wpilibc/src/main/native/include/frc/AnalogGyro.h @@ -8,7 +8,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/interfaces/Gyro.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -31,7 +30,6 @@ class AnalogInput; * This class is for gyro sensors that connect to an analog input. */ class AnalogGyro : public Gyro, - public ErrorBase, public Sendable, public SendableHelper { public: diff --git a/wpilibc/src/main/native/include/frc/AnalogInput.h b/wpilibc/src/main/native/include/frc/AnalogInput.h index 7a9ebeeb6d..6b1828225e 100644 --- a/wpilibc/src/main/native/include/frc/AnalogInput.h +++ b/wpilibc/src/main/native/include/frc/AnalogInput.h @@ -8,7 +8,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -30,9 +29,7 @@ class DMASample; * are divided by the number of samples to retain the resolution, but get more * stable values. */ -class AnalogInput : public ErrorBase, - public Sendable, - public SendableHelper { +class AnalogInput : public Sendable, public SendableHelper { friend class AnalogTrigger; friend class AnalogGyro; friend class DMA; diff --git a/wpilibc/src/main/native/include/frc/AnalogOutput.h b/wpilibc/src/main/native/include/frc/AnalogOutput.h index 6120059e2b..f93511a903 100644 --- a/wpilibc/src/main/native/include/frc/AnalogOutput.h +++ b/wpilibc/src/main/native/include/frc/AnalogOutput.h @@ -6,7 +6,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -17,9 +16,7 @@ class SendableBuilder; /** * MXP analog output class. */ -class AnalogOutput : public ErrorBase, - public Sendable, - public SendableHelper { +class AnalogOutput : public Sendable, public SendableHelper { public: /** * Construct an analog output on the given channel. diff --git a/wpilibc/src/main/native/include/frc/AnalogPotentiometer.h b/wpilibc/src/main/native/include/frc/AnalogPotentiometer.h index c6034e06ce..8c470bbf45 100644 --- a/wpilibc/src/main/native/include/frc/AnalogPotentiometer.h +++ b/wpilibc/src/main/native/include/frc/AnalogPotentiometer.h @@ -7,7 +7,6 @@ #include #include "frc/AnalogInput.h" -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -21,8 +20,7 @@ class SendableBuilder; * units you choose, by way of the scaling and offset constants passed to the * constructor. */ -class AnalogPotentiometer : public ErrorBase, - public Sendable, +class AnalogPotentiometer : public Sendable, public SendableHelper { public: /** diff --git a/wpilibc/src/main/native/include/frc/AnalogTrigger.h b/wpilibc/src/main/native/include/frc/AnalogTrigger.h index 6cb6ec0959..6fb41cf0c2 100644 --- a/wpilibc/src/main/native/include/frc/AnalogTrigger.h +++ b/wpilibc/src/main/native/include/frc/AnalogTrigger.h @@ -9,7 +9,6 @@ #include #include "frc/AnalogTriggerOutput.h" -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -19,9 +18,7 @@ class AnalogInput; class DutyCycle; class SendableBuilder; -class AnalogTrigger : public ErrorBase, - public Sendable, - public SendableHelper { +class AnalogTrigger : public Sendable, public SendableHelper { friend class AnalogTriggerOutput; public: diff --git a/wpilibc/src/main/native/include/frc/BuiltInAccelerometer.h b/wpilibc/src/main/native/include/frc/BuiltInAccelerometer.h index 8a55a978f1..517b7ae483 100644 --- a/wpilibc/src/main/native/include/frc/BuiltInAccelerometer.h +++ b/wpilibc/src/main/native/include/frc/BuiltInAccelerometer.h @@ -4,7 +4,6 @@ #pragma once -#include "frc/ErrorBase.h" #include "frc/interfaces/Accelerometer.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -18,8 +17,7 @@ class SendableBuilder; * * This class allows access to the roboRIO's internal accelerometer. */ -class BuiltInAccelerometer : public ErrorBase, - public Accelerometer, +class BuiltInAccelerometer : public Accelerometer, public Sendable, public SendableHelper { public: diff --git a/wpilibc/src/main/native/include/frc/CAN.h b/wpilibc/src/main/native/include/frc/CAN.h index 495e782992..4c9b9bd917 100644 --- a/wpilibc/src/main/native/include/frc/CAN.h +++ b/wpilibc/src/main/native/include/frc/CAN.h @@ -8,8 +8,6 @@ #include -#include "frc/ErrorBase.h" - namespace frc { struct CANData { uint8_t data[8]; @@ -27,7 +25,7 @@ struct CANData { * All methods are thread save, however the buffer objects passed in * by the user need to not be modified for the duration of their calls. */ -class CAN : public ErrorBase { +class CAN { public: /** * Create a new CAN communication interface with the specific device ID. @@ -52,7 +50,7 @@ class CAN : public ErrorBase { /** * Closes the CAN communication. */ - ~CAN() override; + ~CAN(); CAN(CAN&&) = default; CAN& operator=(CAN&&) = default; diff --git a/wpilibc/src/main/native/include/frc/Compressor.h b/wpilibc/src/main/native/include/frc/Compressor.h index 96b1be5738..e83ba0f611 100644 --- a/wpilibc/src/main/native/include/frc/Compressor.h +++ b/wpilibc/src/main/native/include/frc/Compressor.h @@ -6,7 +6,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/SensorUtil.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -30,9 +29,7 @@ class SendableBuilder; * loop control. You can only turn off closed loop control, thereby stopping * the compressor from operating. */ -class Compressor : public ErrorBase, - public Sendable, - public SendableHelper { +class Compressor : public Sendable, public SendableHelper { public: /** * Constructor. The default PCM ID is 0. diff --git a/wpilibc/src/main/native/include/frc/Counter.h b/wpilibc/src/main/native/include/frc/Counter.h index dc0f4179e0..c15814e293 100644 --- a/wpilibc/src/main/native/include/frc/Counter.h +++ b/wpilibc/src/main/native/include/frc/Counter.h @@ -10,7 +10,6 @@ #include "frc/AnalogTrigger.h" #include "frc/CounterBase.h" -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -31,8 +30,7 @@ class DMASample; * All counters will immediately start counting - Reset() them if you need them * to be zeroed before use. */ -class Counter : public ErrorBase, - public CounterBase, +class Counter : public CounterBase, public Sendable, public SendableHelper { friend class DMA; diff --git a/wpilibc/src/main/native/include/frc/DMA.h b/wpilibc/src/main/native/include/frc/DMA.h index 7646fa8b81..72c3829215 100644 --- a/wpilibc/src/main/native/include/frc/DMA.h +++ b/wpilibc/src/main/native/include/frc/DMA.h @@ -6,8 +6,6 @@ #include -#include "frc/ErrorBase.h" - namespace frc { class Encoder; class Counter; @@ -16,12 +14,12 @@ class DutyCycle; class AnalogInput; class DMASample; -class DMA : public ErrorBase { +class DMA { friend class DMASample; public: DMA(); - ~DMA() override; + ~DMA(); DMA& operator=(DMA&& other) = default; DMA(DMA&& other) = default; diff --git a/wpilibc/src/main/native/include/frc/DigitalGlitchFilter.h b/wpilibc/src/main/native/include/frc/DigitalGlitchFilter.h index 3fce2d083f..e75ea3923b 100644 --- a/wpilibc/src/main/native/include/frc/DigitalGlitchFilter.h +++ b/wpilibc/src/main/native/include/frc/DigitalGlitchFilter.h @@ -11,7 +11,6 @@ #include #include "frc/DigitalSource.h" -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -27,8 +26,7 @@ class Counter; * filter. The filter lets the user configure the time that an input must remain * high or low before it is classified as high or low. */ -class DigitalGlitchFilter : public ErrorBase, - public Sendable, +class DigitalGlitchFilter : public Sendable, public SendableHelper { public: DigitalGlitchFilter(); diff --git a/wpilibc/src/main/native/include/frc/DriverStation.h b/wpilibc/src/main/native/include/frc/DriverStation.h index ba521c7be0..91e36a6503 100644 --- a/wpilibc/src/main/native/include/frc/DriverStation.h +++ b/wpilibc/src/main/native/include/frc/DriverStation.h @@ -15,8 +15,6 @@ #include #include -#include "frc/ErrorBase.h" - namespace frc { class MatchDataSender; @@ -25,12 +23,12 @@ class MatchDataSender; * Provide access to the network communication data to / from the Driver * Station. */ -class DriverStation : public ErrorBase { +class DriverStation { public: enum Alliance { kRed, kBlue, kInvalid }; enum MatchType { kNone, kPractice, kQualification, kElimination }; - ~DriverStation() override; + ~DriverStation(); DriverStation(const DriverStation&) = delete; DriverStation& operator=(const DriverStation&) = delete; diff --git a/wpilibc/src/main/native/include/frc/DutyCycle.h b/wpilibc/src/main/native/include/frc/DutyCycle.h index 8b02e875bf..0cdf8c3019 100644 --- a/wpilibc/src/main/native/include/frc/DutyCycle.h +++ b/wpilibc/src/main/native/include/frc/DutyCycle.h @@ -8,7 +8,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -29,9 +28,7 @@ class DMASample; * order to implement rollover checking. * */ -class DutyCycle : public ErrorBase, - public Sendable, - public SendableHelper { +class DutyCycle : public Sendable, public SendableHelper { friend class AnalogTrigger; friend class DMA; friend class DMASample; diff --git a/wpilibc/src/main/native/include/frc/DutyCycleEncoder.h b/wpilibc/src/main/native/include/frc/DutyCycleEncoder.h index 87789f2fc2..fc1d0108ea 100644 --- a/wpilibc/src/main/native/include/frc/DutyCycleEncoder.h +++ b/wpilibc/src/main/native/include/frc/DutyCycleEncoder.h @@ -12,7 +12,6 @@ #include "frc/AnalogTrigger.h" #include "frc/Counter.h" -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -25,8 +24,7 @@ class DigitalSource; * PWM Output, the CTRE Mag Encoder, the Rev Hex Encoder, and the AM Mag * Encoder. */ -class DutyCycleEncoder : public ErrorBase, - public Sendable, +class DutyCycleEncoder : public Sendable, public SendableHelper { public: /** diff --git a/wpilibc/src/main/native/include/frc/Encoder.h b/wpilibc/src/main/native/include/frc/Encoder.h index db96187d6a..50f8cbf152 100644 --- a/wpilibc/src/main/native/include/frc/Encoder.h +++ b/wpilibc/src/main/native/include/frc/Encoder.h @@ -10,7 +10,6 @@ #include "frc/Counter.h" #include "frc/CounterBase.h" -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -37,8 +36,7 @@ class DMASample; * All encoders will immediately start counting - Reset() them if you need them * to be zeroed before use. */ -class Encoder : public ErrorBase, - public CounterBase, +class Encoder : public CounterBase, public Sendable, public SendableHelper { friend class DMA; diff --git a/wpilibc/src/main/native/include/frc/Error.h b/wpilibc/src/main/native/include/frc/Error.h deleted file mode 100644 index 812d1491bb..0000000000 --- a/wpilibc/src/main/native/include/frc/Error.h +++ /dev/null @@ -1,63 +0,0 @@ -// 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. - -#pragma once - -#include - -#include -#include - -#ifdef _WIN32 -#pragma push_macro("GetMessage") -#undef GetMessage -#endif - -namespace frc { - -class ErrorBase; - -/** - * Error object represents a library error. - */ -class Error { - public: - using Code = int; - - Error() = default; - Error(Code code, const wpi::Twine& contextMessage, wpi::StringRef filename, - wpi::StringRef function, int lineNumber, - const ErrorBase* originatingObject); - - bool operator<(const Error& rhs) const; - - Code GetCode() const; - std::string GetMessage() const; - std::string GetFilename() const; - std::string GetFunction() const; - int GetLineNumber() const; - const ErrorBase* GetOriginatingObject() const; - double GetTimestamp() const; - void Clear(); - void Set(Code code, const wpi::Twine& contextMessage, wpi::StringRef filename, - wpi::StringRef function, int lineNumber, - const ErrorBase* originatingObject); - - private: - void Report(); - - Code m_code = 0; - std::string m_message; - std::string m_filename; - std::string m_function; - int m_lineNumber = 0; - const ErrorBase* m_originatingObject = nullptr; - double m_timestamp = 0.0; -}; - -} // namespace frc - -#ifdef _WIN32 -#pragma pop_macro("GetMessage") -#endif diff --git a/wpilibc/src/main/native/include/frc/ErrorBase.h b/wpilibc/src/main/native/include/frc/ErrorBase.h deleted file mode 100644 index f9e8b00bf4..0000000000 --- a/wpilibc/src/main/native/include/frc/ErrorBase.h +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -#pragma once - -#include - -#include -#include - -#include "frc/Error.h" - -// Forward declared manually to avoid needing to pull in entire HAL header. -extern "C" const char* HAL_GetErrorMessage(int32_t code); - -#define wpi_setErrnoErrorWithContext(context) \ - this->SetErrnoError((context), __FILE__, __FUNCTION__, __LINE__) -#define wpi_setErrnoError() wpi_setErrnoErrorWithContext("") -#define wpi_setImaqErrorWithContext(code, context) \ - do { \ - if ((code) != 0) \ - this->SetImaqError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ - } while (0) -#define wpi_setErrorWithContext(code, context) \ - do { \ - if ((code) != 0) \ - this->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ - } while (0) -#define wpi_setErrorWithContextRange(code, min, max, req, context) \ - do { \ - if ((code) != 0) \ - this->SetErrorRange((code), (min), (max), (req), (context), __FILE__, \ - __FUNCTION__, __LINE__); \ - } while (0) - -#define wpi_setHALError(code) \ - do { \ - if ((code) != 0) \ - this->SetError((code), HAL_GetErrorMessage(code), __FILE__, \ - __FUNCTION__, __LINE__); \ - } while (0) - -#define wpi_setHALErrorWithRange(code, min, max, req) \ - do { \ - if ((code) != 0) \ - this->SetErrorRange((code), (min), (max), (req), \ - HAL_GetErrorMessage(code), __FILE__, __FUNCTION__, \ - __LINE__); \ - } while (0) - -#define wpi_setError(code) wpi_setErrorWithContext(code, "") -#define wpi_setStaticErrorWithContext(object, code, context) \ - do { \ - if ((code) != 0) \ - object->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ - } while (0) -#define wpi_setStaticError(object, code) \ - wpi_setStaticErrorWithContext(object, code, "") - -#define wpi_setGlobalErrorWithContext(code, context) \ - do { \ - if ((code) != 0) \ - ::frc::ErrorBase::SetGlobalError((code), (context), __FILE__, \ - __FUNCTION__, __LINE__); \ - } while (0) - -#define wpi_setGlobalHALError(code) \ - do { \ - if ((code) != 0) \ - ::frc::ErrorBase::SetGlobalError((code), HAL_GetErrorMessage(code), \ - __FILE__, __FUNCTION__, __LINE__); \ - } while (0) - -#define wpi_setGlobalError(code) wpi_setGlobalErrorWithContext(code, "") -#define wpi_setWPIErrorWithContext(error, context) \ - this->SetWPIError(wpi_error_s_##error(), wpi_error_value_##error(), \ - (context), __FILE__, __FUNCTION__, __LINE__) -#define wpi_setWPIError(error) (wpi_setWPIErrorWithContext(error, "")) -#define wpi_setStaticWPIErrorWithContext(object, error, context) \ - object->SetWPIError(wpi_error_s_##error(), (context), __FILE__, \ - __FUNCTION__, __LINE__) -#define wpi_setStaticWPIError(object, error) \ - wpi_setStaticWPIErrorWithContext(object, error, "") -#define wpi_setGlobalWPIErrorWithContext(error, context) \ - ::frc::ErrorBase::SetGlobalWPIError(wpi_error_s_##error(), (context), \ - __FILE__, __FUNCTION__, __LINE__) -#define wpi_setGlobalWPIError(error) wpi_setGlobalWPIErrorWithContext(error, "") - -namespace frc { - -/** - * Base class for most objects. - * - * ErrorBase is the base class for most objects since it holds the generated - * error for that object. In addition, there is a single instance of a global - * error object. - */ -class ErrorBase { - // TODO: Consider initializing instance variables and cleanup in destructor - public: - ErrorBase(); - virtual ~ErrorBase() = default; - - ErrorBase(const ErrorBase&) = default; - ErrorBase& operator=(const ErrorBase&) = default; - ErrorBase(ErrorBase&&) = default; - ErrorBase& operator=(ErrorBase&&) = default; - - /** - * @brief Retrieve the current error. - * - * Get the current error information associated with this sensor. - */ - virtual Error& GetError(); - - /** - * @brief Retrieve the current error. - * - * Get the current error information associated with this sensor. - */ - virtual const Error& GetError() const; - - /** - * @brief Clear the current error information associated with this sensor. - */ - virtual void ClearError() const; - - /** - * @brief Set error information associated with a C library call that set an - * error to the "errno" global variable. - * - * @param contextMessage A custom message from the code that set the error. - * @param filename Filename of the error source - * @param function Function of the error source - * @param lineNumber Line number of the error source - */ - virtual void SetErrnoError(const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const; - - /** - * @brief Set the current error information associated from the nivision Imaq - * API. - * - * @param success The return from the function - * @param contextMessage A custom message from the code that set the error. - * @param filename Filename of the error source - * @param function Function of the error source - * @param lineNumber Line number of the error source - */ - virtual void SetImaqError(int success, const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const; - - /** - * @brief Set the current error information associated with this sensor. - * - * @param code The error code - * @param contextMessage A custom message from the code that set the error. - * @param filename Filename of the error source - * @param function Function of the error source - * @param lineNumber Line number of the error source - */ - virtual void SetError(Error::Code code, const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const; - - /** - * @brief Set the current error information associated with this sensor. - * Range versions use for initialization code. - * - * @param code The error code - * @param minRange The minimum allowed allocation range - * @param maxRange The maximum allowed allocation range - * @param requestedValue The requested value to allocate - * @param contextMessage A custom message from the code that set the error. - * @param filename Filename of the error source - * @param function Function of the error source - * @param lineNumber Line number of the error source - */ - virtual void SetErrorRange(Error::Code code, int32_t minRange, - int32_t maxRange, int32_t requestedValue, - const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const; - - /** - * @brief Set the current error information associated with this sensor. - * - * @param errorMessage The error message from WPIErrors.h - * @param contextMessage A custom message from the code that set the error. - * @param filename Filename of the error source - * @param function Function of the error source - * @param lineNumber Line number of the error source - */ - virtual void SetWPIError(const wpi::Twine& errorMessage, Error::Code code, - const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber) const; - - virtual void CloneError(const ErrorBase& rhs) const; - - /** - * @brief Check if the current error code represents a fatal error. - * - * @return true if the current error is fatal. - */ - virtual bool StatusIsFatal() const; - - static void SetGlobalError(Error::Code code, const wpi::Twine& contextMessage, - wpi::StringRef filename, wpi::StringRef function, - int lineNumber); - - static void SetGlobalWPIError(const wpi::Twine& errorMessage, - const wpi::Twine& contextMessage, - wpi::StringRef filename, - wpi::StringRef function, int lineNumber); - - /** - * Retrieve the last global error. - */ - static Error GetGlobalError(); - - /** - * Retrieve all global errors. - */ - static std::vector GetGlobalErrors(); - - /** - * Clear global errors. - */ - void ClearGlobalErrors(); - - protected: - mutable Error m_error; -}; - -} // namespace frc diff --git a/wpilibc/src/main/native/include/frc/Errors.h b/wpilibc/src/main/native/include/frc/Errors.h new file mode 100644 index 0000000000..9b16072298 --- /dev/null +++ b/wpilibc/src/main/native/include/frc/Errors.h @@ -0,0 +1,139 @@ +// 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. + +#pragma once + +#include + +#include +#include +#include + +#include + +namespace frc { + +/** + * Runtime error exception. + */ +class RuntimeError : public std::runtime_error { + public: + RuntimeError(int32_t code, const wpi::Twine& message, const wpi::Twine& loc, + wpi::StringRef stack); + RuntimeError(int32_t code, const wpi::Twine& message, const char* fileName, + int lineNumber, const char* funcName, wpi::StringRef stack); + + int32_t code() const noexcept { return m_data->code; } + const char* loc() const noexcept { return m_data->loc.c_str(); } + const char* stack() const noexcept { return m_data->stack.c_str(); } + + /** + * Reports error to Driver Station (using HAL_SendError). + */ + void Report() const; + + private: + struct Data { + int32_t code; + std::string loc; + std::string stack; + }; + std::shared_ptr m_data; +}; + +/** + * Gets error message string for an error code. + */ +const char* GetErrorMessage(int32_t code); + +/** + * Reports an error to the driver station (using HAL_SendError). + * Generally the FRC_ReportError wrapper macro should be used instead. + * + * @param status error code + * @param message error message details + * @param fileName source file name + * @param lineNumber source line number + * @param funcName source function name + */ +void ReportError(int32_t status, const wpi::Twine& message, + const char* fileName, int lineNumber, const char* funcName); + +/** + * Makes a runtime error exception object. This object should be thrown + * by the caller. Generally the FRC_MakeError wrapper macro should be used + * instead. + * + * @param status error code + * @param message error message details + * @param fileName source file name + * @param lineNumber source line number + * @param funcName source function name + * @return runtime error object + */ +[[nodiscard]] RuntimeError MakeError(int32_t status, const wpi::Twine& message, + const char* fileName, int lineNumber, + const char* funcName); + +namespace err { +#define S(label, offset, message) inline constexpr int label = offset; +#include "frc/WPIErrors.mac" +#undef S +} // namespace err + +namespace warn { +#define S(label, offset, message) inline constexpr int label = offset; +#include "frc/WPIWarnings.mac" +#undef S +} // namespace warn +} // namespace frc + +/** + * Reports an error to the driver station (using HAL_SendError). + * + * @param status error code + * @param message error message details + */ +#define FRC_ReportError(status, message) \ + do { \ + if ((status) != 0) { \ + ::frc::ReportError(status, message, __FILE__, __LINE__, __FUNCTION__); \ + } \ + } while (0) + +/** + * Makes a runtime error exception object. This object should be thrown + * by the caller. + * + * @param status error code + * @param message error message details + * @return runtime error object + */ +#define FRC_MakeError(status, message) \ + ::frc::MakeError(status, message, __FILE__, __LINE__, __FUNCTION__) + +/** + * Checks a status code and depending on its value, either throws a + * RuntimeError exception, calls ReportError, or does nothing (if no error). + * + * @param status error code + * @param message error message details + */ +#define FRC_CheckErrorStatus(status, message) \ + do { \ + if ((status) < 0) { \ + throw FRC_MakeError(status, message); \ + } else if ((status) > 0) { \ + FRC_ReportError(status, message); \ + } \ + } while (0) + +#define FRC_AssertMessage(condition, message) \ + do { \ + if (!(condition)) { \ + throw FRC_MakeError(err::AssertionFailure, message); \ + } \ + } while (0) + +#define FRC_Assert(condition) FRC_AssertMessage(condition, #condition) diff --git a/wpilibc/src/main/native/include/frc/GenericHID.h b/wpilibc/src/main/native/include/frc/GenericHID.h index e441204034..880782b1d9 100644 --- a/wpilibc/src/main/native/include/frc/GenericHID.h +++ b/wpilibc/src/main/native/include/frc/GenericHID.h @@ -8,8 +8,6 @@ #include -#include "frc/ErrorBase.h" - namespace frc { class DriverStation; @@ -17,7 +15,7 @@ class DriverStation; /** * GenericHID Interface. */ -class GenericHID : public ErrorBase { +class GenericHID { public: enum RumbleType { kLeftRumble, kRightRumble }; @@ -44,7 +42,7 @@ class GenericHID : public ErrorBase { enum JoystickHand { kLeftHand = 0, kRightHand = 1 }; explicit GenericHID(int port); - ~GenericHID() override = default; + virtual ~GenericHID() = default; GenericHID(GenericHID&&) = default; GenericHID& operator=(GenericHID&&) = default; diff --git a/wpilibc/src/main/native/include/frc/I2C.h b/wpilibc/src/main/native/include/frc/I2C.h index f62aaf0731..5031673160 100644 --- a/wpilibc/src/main/native/include/frc/I2C.h +++ b/wpilibc/src/main/native/include/frc/I2C.h @@ -8,8 +8,6 @@ #include -#include "frc/ErrorBase.h" - namespace frc { /** @@ -18,7 +16,7 @@ namespace frc { * This class is intended to be used by sensor (and other I2C device) drivers. * It probably should not be used directly. */ -class I2C : public ErrorBase { +class I2C { public: enum Port { kOnboard = 0, kMXP }; @@ -30,7 +28,7 @@ class I2C : public ErrorBase { */ I2C(Port port, int deviceAddress); - ~I2C() override; + ~I2C(); I2C(I2C&&) = default; I2C& operator=(I2C&&) = default; diff --git a/wpilibc/src/main/native/include/frc/InterruptableSensorBase.h b/wpilibc/src/main/native/include/frc/InterruptableSensorBase.h index 6e361ab26d..cefc2d2d0c 100644 --- a/wpilibc/src/main/native/include/frc/InterruptableSensorBase.h +++ b/wpilibc/src/main/native/include/frc/InterruptableSensorBase.h @@ -10,11 +10,10 @@ #include #include "frc/AnalogTriggerType.h" -#include "frc/ErrorBase.h" namespace frc { -class InterruptableSensorBase : public ErrorBase { +class InterruptableSensorBase { public: enum WaitResult { kTimeout = 0x0, @@ -35,7 +34,7 @@ class InterruptableSensorBase : public ErrorBase { /** * Free the resources for an interrupt event. */ - ~InterruptableSensorBase() override; + virtual ~InterruptableSensorBase(); InterruptableSensorBase(InterruptableSensorBase&&) = default; InterruptableSensorBase& operator=(InterruptableSensorBase&&) = default; diff --git a/wpilibc/src/main/native/include/frc/MotorSafety.h b/wpilibc/src/main/native/include/frc/MotorSafety.h index b23a29c7ef..0f56b42f40 100644 --- a/wpilibc/src/main/native/include/frc/MotorSafety.h +++ b/wpilibc/src/main/native/include/frc/MotorSafety.h @@ -6,7 +6,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/Timer.h" namespace wpi { @@ -21,10 +20,10 @@ namespace frc { * * The subclass should call Feed() whenever the motor value is updated. */ -class MotorSafety : public ErrorBase { +class MotorSafety { public: MotorSafety(); - ~MotorSafety() override; + virtual ~MotorSafety(); MotorSafety(MotorSafety&& rhs); MotorSafety& operator=(MotorSafety&& rhs); diff --git a/wpilibc/src/main/native/include/frc/Notifier.h b/wpilibc/src/main/native/include/frc/Notifier.h index df8c37ca53..fee783b937 100644 --- a/wpilibc/src/main/native/include/frc/Notifier.h +++ b/wpilibc/src/main/native/include/frc/Notifier.h @@ -18,11 +18,9 @@ #include #include -#include "frc/ErrorBase.h" - namespace frc { -class Notifier : public ErrorBase { +class Notifier { public: /** * Create a Notifier for timer event notification. @@ -63,7 +61,7 @@ class Notifier : public ErrorBase { /** * Free the resources for a timer event. */ - ~Notifier() override; + ~Notifier(); Notifier(Notifier&& rhs); Notifier& operator=(Notifier&& rhs); diff --git a/wpilibc/src/main/native/include/frc/PWM.h b/wpilibc/src/main/native/include/frc/PWM.h index 869b6453b9..65de64ba55 100644 --- a/wpilibc/src/main/native/include/frc/PWM.h +++ b/wpilibc/src/main/native/include/frc/PWM.h @@ -8,7 +8,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -33,7 +32,7 @@ class SendableBuilder; * - 1 = minimum pulse width (currently 0.5ms) * - 0 = disabled (i.e. PWM output is held low) */ -class PWM : public ErrorBase, public Sendable, public SendableHelper { +class PWM : public Sendable, public SendableHelper { public: friend class AddressableLED; /** diff --git a/wpilibc/src/main/native/include/frc/PowerDistributionPanel.h b/wpilibc/src/main/native/include/frc/PowerDistributionPanel.h index d40b988989..ba285bc08f 100644 --- a/wpilibc/src/main/native/include/frc/PowerDistributionPanel.h +++ b/wpilibc/src/main/native/include/frc/PowerDistributionPanel.h @@ -6,7 +6,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -18,8 +17,7 @@ class SendableBuilder; * Class for getting voltage, current, temperature, power and energy from the * CAN PDP. */ -class PowerDistributionPanel : public ErrorBase, - public Sendable, +class PowerDistributionPanel : public Sendable, public SendableHelper { public: PowerDistributionPanel(); diff --git a/wpilibc/src/main/native/include/frc/Preferences.h b/wpilibc/src/main/native/include/frc/Preferences.h index 0d192b7aba..7ecf25a008 100644 --- a/wpilibc/src/main/native/include/frc/Preferences.h +++ b/wpilibc/src/main/native/include/frc/Preferences.h @@ -12,8 +12,6 @@ #include -#include "frc/ErrorBase.h" - namespace frc { /** @@ -30,7 +28,7 @@ namespace frc { * This will also interact with {@link NetworkTable} by creating a table called * "Preferences" with all the key-value pairs. */ -class Preferences : public ErrorBase { +class Preferences { public: /** * Get the one and only {@link Preferences} object. @@ -226,7 +224,7 @@ class Preferences : public ErrorBase { protected: Preferences(); - ~Preferences() override; + ~Preferences(); Preferences(Preferences&&) = default; Preferences& operator=(Preferences&&) = default; diff --git a/wpilibc/src/main/native/include/frc/Relay.h b/wpilibc/src/main/native/include/frc/Relay.h index 14a59a8012..b623701649 100644 --- a/wpilibc/src/main/native/include/frc/Relay.h +++ b/wpilibc/src/main/native/include/frc/Relay.h @@ -9,7 +9,6 @@ #include #include -#include "frc/ErrorBase.h" #include "frc/MotorSafety.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" diff --git a/wpilibc/src/main/native/include/frc/Resource.h b/wpilibc/src/main/native/include/frc/Resource.h index 6880d65265..4109cc461c 100644 --- a/wpilibc/src/main/native/include/frc/Resource.h +++ b/wpilibc/src/main/native/include/frc/Resource.h @@ -12,8 +12,6 @@ #include -#include "frc/ErrorBase.h" - namespace frc { /** @@ -26,9 +24,9 @@ namespace frc { * resources; it just tracks which indices were marked in use by Allocate and * not yet freed by Free. */ -class Resource : public ErrorBase { +class Resource { public: - ~Resource() override = default; + virtual ~Resource() = default; /** * Factory method to create a Resource allocation-tracker *if* needed. diff --git a/wpilibc/src/main/native/include/frc/RobotBase.h b/wpilibc/src/main/native/include/frc/RobotBase.h index ffc59eec50..c477438639 100644 --- a/wpilibc/src/main/native/include/frc/RobotBase.h +++ b/wpilibc/src/main/native/include/frc/RobotBase.h @@ -14,6 +14,7 @@ #include #include "frc/Base.h" +#include "frc/Errors.h" namespace frc { @@ -25,12 +26,17 @@ namespace impl { template void RunRobot(wpi::mutex& m, Robot** robot) { - static Robot theRobot; - { - std::scoped_lock lock{m}; - *robot = &theRobot; + try { + static Robot theRobot; + { + std::scoped_lock lock{m}; + *robot = &theRobot; + } + theRobot.StartCompetition(); + } catch (const frc::RuntimeError& e) { + e.Report(); + throw; } - theRobot.StartCompetition(); } } // namespace impl diff --git a/wpilibc/src/main/native/include/frc/SPI.h b/wpilibc/src/main/native/include/frc/SPI.h index 59fe0db418..be9d6ef8f3 100644 --- a/wpilibc/src/main/native/include/frc/SPI.h +++ b/wpilibc/src/main/native/include/frc/SPI.h @@ -13,8 +13,6 @@ #include #include -#include "frc/ErrorBase.h" - namespace frc { class DigitalSource; @@ -26,7 +24,7 @@ class DigitalSource; * It probably should not be used directly. * */ -class SPI : public ErrorBase { +class SPI { public: enum Port { kOnboardCS0 = 0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP }; @@ -37,7 +35,7 @@ class SPI : public ErrorBase { */ explicit SPI(Port port); - ~SPI() override; + ~SPI(); SPI(SPI&&) = default; SPI& operator=(SPI&&) = default; diff --git a/wpilibc/src/main/native/include/frc/SerialPort.h b/wpilibc/src/main/native/include/frc/SerialPort.h index 9bbd9961ca..b11cb64dd7 100644 --- a/wpilibc/src/main/native/include/frc/SerialPort.h +++ b/wpilibc/src/main/native/include/frc/SerialPort.h @@ -11,8 +11,6 @@ #include #include -#include "frc/ErrorBase.h" - namespace frc { /** @@ -27,7 +25,7 @@ namespace frc { * and the NI-VISA Programmer's Reference Manual here: * http://www.ni.com/pdf/manuals/370132c.pdf */ -class SerialPort : public ErrorBase { +class SerialPort { public: enum Parity { kParity_None = 0, @@ -88,7 +86,7 @@ class SerialPort : public ErrorBase { int dataBits = 8, Parity parity = kParity_None, StopBits stopBits = kStopBits_One); - ~SerialPort() override; + ~SerialPort(); SerialPort(SerialPort&& rhs) = default; SerialPort& operator=(SerialPort&& rhs) = default; diff --git a/wpilibc/src/main/native/include/frc/SolenoidBase.h b/wpilibc/src/main/native/include/frc/SolenoidBase.h index c0acc4334f..82121fbe1b 100644 --- a/wpilibc/src/main/native/include/frc/SolenoidBase.h +++ b/wpilibc/src/main/native/include/frc/SolenoidBase.h @@ -4,16 +4,16 @@ #pragma once -#include "frc/ErrorBase.h" - namespace frc { /** * SolenoidBase class is the common base class for the Solenoid and * DoubleSolenoid classes. */ -class SolenoidBase : public ErrorBase { +class SolenoidBase { public: + virtual ~SolenoidBase() = default; + /** * Get the CAN ID of the module this solenoid is connected to. * diff --git a/wpilibc/src/main/native/include/frc/TimedRobot.h b/wpilibc/src/main/native/include/frc/TimedRobot.h index 817e6317b3..56221e765d 100644 --- a/wpilibc/src/main/native/include/frc/TimedRobot.h +++ b/wpilibc/src/main/native/include/frc/TimedRobot.h @@ -14,7 +14,6 @@ #include #include -#include "frc/ErrorBase.h" #include "frc/IterativeRobotBase.h" #include "frc2/Timer.h" @@ -29,7 +28,7 @@ namespace frc { * Periodic() functions from the base class are called on an interval by a * Notifier instance. */ -class TimedRobot : public IterativeRobotBase, public ErrorBase { +class TimedRobot : public IterativeRobotBase { public: static constexpr units::second_t kDefaultPeriod = 20_ms; diff --git a/wpilibc/src/main/native/include/frc/Ultrasonic.h b/wpilibc/src/main/native/include/frc/Ultrasonic.h index 78afc97be5..adb86b54bc 100644 --- a/wpilibc/src/main/native/include/frc/Ultrasonic.h +++ b/wpilibc/src/main/native/include/frc/Ultrasonic.h @@ -12,7 +12,6 @@ #include #include "frc/Counter.h" -#include "frc/ErrorBase.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -33,9 +32,7 @@ class DigitalOutput; * received. The time that the line is high determines the round trip distance * (time of flight). */ -class Ultrasonic : public ErrorBase, - public Sendable, - public SendableHelper { +class Ultrasonic : public Sendable, public SendableHelper { public: /** * Create an instance of the Ultrasonic Sensor. diff --git a/wpilibc/src/main/native/include/frc/WPIErrors.h b/wpilibc/src/main/native/include/frc/WPIErrors.mac similarity index 55% rename from wpilibc/src/main/native/include/frc/WPIErrors.h rename to wpilibc/src/main/native/include/frc/WPIErrors.mac index 3ff8dd23d4..f159ccd848 100644 --- a/wpilibc/src/main/native/include/frc/WPIErrors.h +++ b/wpilibc/src/main/native/include/frc/WPIErrors.mac @@ -2,18 +2,9 @@ // 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. -#pragma once - -#include - -#define S(label, offset, message) \ - constexpr inline const char* wpi_error_s_##label() { return message; } \ - constexpr inline int wpi_error_value_##label() { return offset; } - -// Fatal errors S(ModuleIndexOutOfRange, -1, "Allocating module that is out of range or not found") -S(ChannelIndexOutOfRange, -1, "Allocating channel that is out of range") +S(ChannelIndexOutOfRange, -45, "Allocating channel that is out of range") S(NotAllocated, -2, "Attempting to free unallocated resource") S(ResourceAlreadyAllocated, -3, "Attempted to reuse an allocated resource") S(NoAvailableResources, -4, "No available resources to allocate") @@ -26,13 +17,13 @@ S(IncompatibleMode, -9, "The object is in an incompatible mode") S(AnalogTriggerLimitOrderError, -10, "AnalogTrigger limits error. Lower limit > Upper Limit") S(AnalogTriggerPulseOutputError, -11, - "Attempted to read AnalogTrigger pulse output.") + "Attempted to read AnalogTrigger pulse output") S(TaskError, -12, "Task can't be started") -S(TaskIDError, -13, "Task error: Invalid ID.") -S(TaskDeletedError, -14, "Task error: Task already deleted.") -S(TaskOptionsError, -15, "Task error: Invalid options.") -S(TaskMemoryError, -16, "Task can't be started due to insufficient memory.") -S(TaskPriorityError, -17, "Task error: Invalid priority [1-255].") +S(TaskIDError, -13, "Task error: Invalid ID") +S(TaskDeletedError, -14, "Task error: Task already deleted") +S(TaskOptionsError, -15, "Task error: Invalid options") +S(TaskMemoryError, -16, "Task can't be started due to insufficient memory") +S(TaskPriorityError, -17, "Task error: Invalid priority [1-255]") S(DriveUninitialized, -18, "RobotDrive not initialized for the C interface") S(CompressorNonMatching, -19, "Compressor slot/channel doesn't match previous instance") @@ -41,19 +32,19 @@ S(CompressorUndefined, -21, "Using compressor functions without defining compressor") S(InconsistentArrayValueAdded, -22, "When packing data into an array to the dashboard, not all values added were " - "of the same type.") + "of the same type") S(MismatchedComplexTypeClose, -23, "When packing data to the dashboard, a Close for a complex type was called " - "without a matching Open.") + "without a matching Open") S(DashboardDataOverflow, -24, "When packing data to the dashboard, too much data was packed and the buffer " - "overflowed.") + "overflowed") S(DashboardDataCollision, -25, - "The same buffer was used for packing data and for printing.") -S(EnhancedIOMissing, -26, "IO is not attached or Enhanced IO is not enabled.") + "The same buffer was used for packing data and for printing") +S(EnhancedIOMissing, -26, "IO is not attached or Enhanced IO is not enabled") S(LineNotOutput, -27, - "Cannot SetDigitalOutput for a line not configured for output.") -S(ParameterOutOfRange, -28, "A parameter is out of range.") + "Cannot SetDigitalOutput for a line not configured for output") +S(ParameterOutOfRange, -28, "A parameter is out of range") S(SPIClockRateTooLow, -29, "SPI clock rate was below the minimum supported") S(JaguarVersionError, -30, "Jaguar firmware version error") S(JaguarMessageNotFound, -31, "Jaguar message not found") @@ -62,31 +53,9 @@ S(NetworkTablesBufferFull, -41, "Buffer full writing to NetworkTables socket") S(NetworkTablesWrongType, -42, "The wrong type was read from the NetworkTables entry") S(NetworkTablesCorrupt, -43, "NetworkTables data stream is corrupt") -S(SmartDashboardMissingKey, -43, "SmartDashboard data does not exist") +S(SmartDashboardMissingKey, -44, "SmartDashboard data does not exist") S(CommandIllegalUse, -50, "Illegal use of Command") S(UnsupportedInSimulation, -80, "Unsupported in simulation") S(CameraServerError, -90, "CameraServer error") S(InvalidParameter, -100, "Invalid parameter value") - -// Warnings -S(SampleRateTooHigh, 1, "Analog module sample rate is too high") -S(VoltageOutOfRange, 2, - "Voltage to convert to raw value is out of range [-10; 10]") -S(CompressorTaskError, 3, "Compressor task won't start") -S(LoopTimingError, 4, "Digital module loop timing is not the expected value") -S(NonBinaryDigitalValue, 5, "Digital output value is not 0 or 1") -S(IncorrectBatteryChannel, 6, - "Battery measurement channel is not correct value") -S(BadJoystickIndex, 7, "Joystick index is out of range, should be 0-3") -S(BadJoystickAxis, 8, "Joystick axis or POV is out of range") -S(InvalidMotorIndex, 9, "Motor index is out of range, should be 0-3") -S(DriverStationTaskError, 10, "Driver Station task won't start") -S(EnhancedIOPWMPeriodOutOfRange, 11, - "Driver Station Enhanced IO PWM Output period out of range.") -S(SPIWriteNoMOSI, 12, "Cannot write to SPI port with no MOSI output") -S(SPIReadNoMISO, 13, "Cannot read from SPI port with no MISO input") -S(SPIReadNoData, 14, "No data available to read from SPI") -S(IncompatibleState, 15, - "Incompatible State: The operation cannot be completed") - -#undef S +S(AssertionFailure, -110, "Assertion failed") diff --git a/wpilibc/src/main/native/include/frc/WPIWarnings.mac b/wpilibc/src/main/native/include/frc/WPIWarnings.mac new file mode 100644 index 0000000000..8e39ac1d6c --- /dev/null +++ b/wpilibc/src/main/native/include/frc/WPIWarnings.mac @@ -0,0 +1,23 @@ +// 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. + +S(SampleRateTooHigh, 1, "Analog module sample rate is too high") +S(VoltageOutOfRange, 2, + "Voltage to convert to raw value is out of range [-10; 10]") +S(CompressorTaskError, 3, "Compressor task won't start") +S(LoopTimingError, 4, "Digital module loop timing is not the expected value") +S(NonBinaryDigitalValue, 5, "Digital output value is not 0 or 1") +S(IncorrectBatteryChannel, 6, + "Battery measurement channel is not correct value") +S(BadJoystickIndex, 7, "Joystick index is out of range, should be 0-3") +S(BadJoystickAxis, 8, "Joystick axis or POV is out of range") +S(InvalidMotorIndex, 9, "Motor index is out of range, should be 0-3") +S(DriverStationTaskError, 10, "Driver Station task won't start") +S(EnhancedIOPWMPeriodOutOfRange, 11, + "Driver Station Enhanced IO PWM Output period out of range") +S(SPIWriteNoMOSI, 12, "Cannot write to SPI port with no MOSI output") +S(SPIReadNoMISO, 13, "Cannot read from SPI port with no MISO input") +S(SPIReadNoData, 14, "No data available to read from SPI") +S(IncompatibleState, 15, + "Incompatible State: The operation cannot be completed") diff --git a/wpilibc/src/main/native/include/frc/motorcontrol/NidecBrushless.h b/wpilibc/src/main/native/include/frc/motorcontrol/NidecBrushless.h index f92e0f3759..112cbd13b4 100644 --- a/wpilibc/src/main/native/include/frc/motorcontrol/NidecBrushless.h +++ b/wpilibc/src/main/native/include/frc/motorcontrol/NidecBrushless.h @@ -5,7 +5,6 @@ #pragma once #include "frc/DigitalOutput.h" -#include "frc/ErrorBase.h" #include "frc/MotorSafety.h" #include "frc/PWM.h" #include "frc/motorcontrol/MotorController.h" diff --git a/wpilibc/src/main/native/include/frc/romi/RomiGyro.h b/wpilibc/src/main/native/include/frc/romi/RomiGyro.h index eb33efc559..7523448a0f 100644 --- a/wpilibc/src/main/native/include/frc/romi/RomiGyro.h +++ b/wpilibc/src/main/native/include/frc/romi/RomiGyro.h @@ -6,7 +6,6 @@ #include -#include "frc/ErrorBase.h" #include "frc/interfaces/Gyro.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" @@ -19,10 +18,7 @@ namespace frc { * This class is for the Romi onboard gyro, and will only work in * simulation/Romi mode. Only one instance of a RomiGyro is supported. */ -class RomiGyro : public Gyro, - public ErrorBase, - public Sendable, - public SendableHelper { +class RomiGyro : public Gyro, public Sendable, public SendableHelper { public: RomiGyro(); diff --git a/wpilibc/src/main/native/include/frc/shuffleboard/ShuffleboardContainer.h b/wpilibc/src/main/native/include/frc/shuffleboard/ShuffleboardContainer.h index a25c81fe89..33b9fe713f 100644 --- a/wpilibc/src/main/native/include/frc/shuffleboard/ShuffleboardContainer.h +++ b/wpilibc/src/main/native/include/frc/shuffleboard/ShuffleboardContainer.h @@ -16,8 +16,6 @@ #include #include -#include "frc/ErrorBase.h" -#include "frc/WPIErrors.h" #include "frc/shuffleboard/BuiltInLayouts.h" #include "frc/shuffleboard/LayoutType.h" #include "frc/shuffleboard/ShuffleboardComponentBase.h" @@ -38,8 +36,7 @@ class SimpleWidget; /** * Common interface for objects that can contain shuffleboard components. */ -class ShuffleboardContainer : public virtual ShuffleboardValue, - public ErrorBase { +class ShuffleboardContainer : public virtual ShuffleboardValue { public: explicit ShuffleboardContainer(const wpi::Twine& title); diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SmartDashboard.h b/wpilibc/src/main/native/include/frc/smartdashboard/SmartDashboard.h index 1dd0a1efbf..6a6ee299cf 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SmartDashboard.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SmartDashboard.h @@ -11,16 +11,13 @@ #include #include -#include "frc/ErrorBase.h" #include "frc/smartdashboard/ListenerExecutor.h" #include "frc/smartdashboard/Sendable.h" #include "frc/smartdashboard/SendableHelper.h" namespace frc { -class SmartDashboard : public ErrorBase, - public Sendable, - public SendableHelper { +class SmartDashboard : public Sendable, public SendableHelper { public: static void init(); diff --git a/wpilibcIntegrationTests/src/main/native/cpp/PowerDistributionPanelTest.cpp b/wpilibcIntegrationTests/src/main/native/cpp/PowerDistributionPanelTest.cpp index 06d12586c8..f213d56d9c 100644 --- a/wpilibcIntegrationTests/src/main/native/cpp/PowerDistributionPanelTest.cpp +++ b/wpilibcIntegrationTests/src/main/native/cpp/PowerDistributionPanelTest.cpp @@ -47,10 +47,8 @@ TEST_F(PowerDistributionPanelTest, CheckRepeatedCalls) { for (int i = 0; i < 50; i++) { for (int j = 0; j < numChannels; j++) { m_pdp->GetCurrent(j); - ASSERT_TRUE(m_pdp->GetError().GetCode() == 0); } m_pdp->GetVoltage(); - ASSERT_TRUE(m_pdp->GetError().GetCode() == 0); } std::this_thread::sleep_for(std::chrono::milliseconds(20)); }