mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
[hal, wpilib] Remove analog output (#7696)
This commit is contained in:
@@ -1,61 +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/AnalogOutput.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <hal/AnalogOutput.h>
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
#include <hal/HALBase.h>
|
||||
#include <hal/Ports.h>
|
||||
#include <wpi/StackTrace.h>
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
#include <wpi/sendable/SendableRegistry.h>
|
||||
|
||||
#include "frc/Errors.h"
|
||||
#include "frc/SensorUtil.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
AnalogOutput::AnalogOutput(int channel) {
|
||||
if (!SensorUtil::CheckAnalogOutputChannel(channel)) {
|
||||
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
|
||||
}
|
||||
|
||||
m_channel = channel;
|
||||
|
||||
HAL_PortHandle port = HAL_GetPort(m_channel);
|
||||
int32_t status = 0;
|
||||
std::string stackTrace = wpi::GetStackTrace(1);
|
||||
m_port = HAL_InitializeAnalogOutputPort(port, stackTrace.c_str(), &status);
|
||||
FRC_CheckErrorStatus(status, "Channel {}", channel);
|
||||
|
||||
HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel + 1);
|
||||
wpi::SendableRegistry::AddLW(this, "AnalogOutput", m_channel);
|
||||
}
|
||||
|
||||
void AnalogOutput::SetVoltage(double voltage) {
|
||||
int32_t status = 0;
|
||||
HAL_SetAnalogOutput(m_port, voltage, &status);
|
||||
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
||||
}
|
||||
|
||||
double AnalogOutput::GetVoltage() const {
|
||||
int32_t status = 0;
|
||||
double voltage = HAL_GetAnalogOutput(m_port, &status);
|
||||
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
||||
return voltage;
|
||||
}
|
||||
|
||||
int AnalogOutput::GetChannel() const {
|
||||
return m_channel;
|
||||
}
|
||||
|
||||
void AnalogOutput::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Analog Output");
|
||||
builder.AddDoubleProperty(
|
||||
"Value", [=, this] { return GetVoltage(); },
|
||||
[=, this](double value) { SetVoltage(value); });
|
||||
}
|
||||
@@ -5,7 +5,6 @@
|
||||
#include "frc/SensorUtil.h"
|
||||
|
||||
#include <hal/AnalogInput.h>
|
||||
#include <hal/AnalogOutput.h>
|
||||
#include <hal/DIO.h>
|
||||
#include <hal/PWM.h>
|
||||
#include <hal/Ports.h>
|
||||
@@ -32,10 +31,6 @@ bool SensorUtil::CheckAnalogInputChannel(int channel) {
|
||||
return HAL_CheckAnalogInputChannel(channel);
|
||||
}
|
||||
|
||||
bool SensorUtil::CheckAnalogOutputChannel(int channel) {
|
||||
return HAL_CheckAnalogOutputChannel(channel);
|
||||
}
|
||||
|
||||
int SensorUtil::GetNumDigitalChannels() {
|
||||
return HAL_GetNumDigitalChannels();
|
||||
}
|
||||
@@ -44,10 +39,6 @@ int SensorUtil::GetNumAnalogInputs() {
|
||||
return HAL_GetNumAnalogInputs();
|
||||
}
|
||||
|
||||
int SensorUtil::GetNumAnalogOuputs() {
|
||||
return HAL_GetNumAnalogOutputs();
|
||||
}
|
||||
|
||||
int SensorUtil::GetNumPwmChannels() {
|
||||
return HAL_GetNumPWMChannels();
|
||||
}
|
||||
|
||||
@@ -1,57 +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/simulation/AnalogOutputSim.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <hal/simulation/AnalogOutData.h>
|
||||
|
||||
#include "frc/AnalogOutput.h"
|
||||
|
||||
using namespace frc;
|
||||
using namespace frc::sim;
|
||||
|
||||
AnalogOutputSim::AnalogOutputSim(const AnalogOutput& analogOutput)
|
||||
: m_index{analogOutput.GetChannel()} {}
|
||||
|
||||
AnalogOutputSim::AnalogOutputSim(int channel) : m_index{channel} {}
|
||||
|
||||
std::unique_ptr<CallbackStore> AnalogOutputSim::RegisterVoltageCallback(
|
||||
NotifyCallback callback, bool initialNotify) {
|
||||
auto store = std::make_unique<CallbackStore>(
|
||||
m_index, -1, callback, &HALSIM_CancelAnalogOutVoltageCallback);
|
||||
store->SetUid(HALSIM_RegisterAnalogOutVoltageCallback(
|
||||
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
||||
return store;
|
||||
}
|
||||
|
||||
double AnalogOutputSim::GetVoltage() const {
|
||||
return HALSIM_GetAnalogOutVoltage(m_index);
|
||||
}
|
||||
|
||||
void AnalogOutputSim::SetVoltage(double voltage) {
|
||||
HALSIM_SetAnalogOutVoltage(m_index, voltage);
|
||||
}
|
||||
|
||||
std::unique_ptr<CallbackStore> AnalogOutputSim::RegisterInitializedCallback(
|
||||
NotifyCallback callback, bool initialNotify) {
|
||||
auto store = std::make_unique<CallbackStore>(
|
||||
m_index, -1, callback, &HALSIM_CancelAnalogOutInitializedCallback);
|
||||
store->SetUid(HALSIM_RegisterAnalogOutInitializedCallback(
|
||||
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
||||
return store;
|
||||
}
|
||||
|
||||
bool AnalogOutputSim::GetInitialized() const {
|
||||
return HALSIM_GetAnalogOutInitialized(m_index);
|
||||
}
|
||||
|
||||
void AnalogOutputSim::SetInitialized(bool initialized) {
|
||||
HALSIM_SetAnalogOutInitialized(m_index, initialized);
|
||||
}
|
||||
|
||||
void AnalogOutputSim::ResetData() {
|
||||
HALSIM_ResetAnalogOutData(m_index);
|
||||
}
|
||||
@@ -1,60 +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 <hal/AnalogOutput.h>
|
||||
#include <hal/Types.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* MXP analog output class.
|
||||
*/
|
||||
class AnalogOutput : public wpi::Sendable,
|
||||
public wpi::SendableHelper<AnalogOutput> {
|
||||
public:
|
||||
/**
|
||||
* Construct an analog output on the given channel.
|
||||
*
|
||||
* All analog outputs are located on the MXP port.
|
||||
*
|
||||
* @param channel The channel number on the roboRIO to represent.
|
||||
*/
|
||||
explicit AnalogOutput(int channel);
|
||||
|
||||
AnalogOutput(AnalogOutput&&) = default;
|
||||
AnalogOutput& operator=(AnalogOutput&&) = default;
|
||||
|
||||
~AnalogOutput() override = default;
|
||||
|
||||
/**
|
||||
* Set the value of the analog output.
|
||||
*
|
||||
* @param voltage The output value in Volts, from 0.0 to +5.0.
|
||||
*/
|
||||
void SetVoltage(double voltage);
|
||||
|
||||
/**
|
||||
* Get the voltage of the analog output.
|
||||
*
|
||||
* @return The value in Volts, from 0.0 to +5.0.
|
||||
*/
|
||||
double GetVoltage() const;
|
||||
|
||||
/**
|
||||
* Get the channel of this AnalogOutput.
|
||||
*/
|
||||
int GetChannel() const;
|
||||
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
protected:
|
||||
int m_channel;
|
||||
hal::Handle<HAL_AnalogOutputHandle, HAL_FreeAnalogOutputPort> m_port;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -58,19 +58,8 @@ class SensorUtil final {
|
||||
*/
|
||||
static bool CheckAnalogInputChannel(int channel);
|
||||
|
||||
/**
|
||||
* Check that the analog output number is valid.
|
||||
*
|
||||
* Verify that the analog output number is one of the legal channel numbers.
|
||||
* Channel numbers are 0-based.
|
||||
*
|
||||
* @return Analog channel is valid
|
||||
*/
|
||||
static bool CheckAnalogOutputChannel(int channel);
|
||||
|
||||
static int GetNumDigitalChannels();
|
||||
static int GetNumAnalogInputs();
|
||||
static int GetNumAnalogOuputs();
|
||||
static int GetNumPwmChannels();
|
||||
};
|
||||
|
||||
|
||||
@@ -1,95 +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 <memory>
|
||||
|
||||
#include "frc/simulation/CallbackStore.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class AnalogOutput;
|
||||
|
||||
namespace sim {
|
||||
|
||||
/**
|
||||
* Class to control a simulated analog output.
|
||||
*/
|
||||
class AnalogOutputSim {
|
||||
public:
|
||||
/**
|
||||
* Constructs from an AnalogOutput object.
|
||||
*
|
||||
* @param analogOutput AnalogOutput to simulate
|
||||
*/
|
||||
explicit AnalogOutputSim(const AnalogOutput& analogOutput);
|
||||
|
||||
/**
|
||||
* Constructs from an analog output channel number.
|
||||
*
|
||||
* @param channel Channel number
|
||||
*/
|
||||
explicit AnalogOutputSim(int channel);
|
||||
|
||||
/**
|
||||
* Register a callback to be run whenever the voltage changes.
|
||||
*
|
||||
* @param callback the callback
|
||||
* @param initialNotify whether to call the callback with the initial state
|
||||
* @return the CallbackStore object associated with this callback
|
||||
*/
|
||||
[[nodiscard]]
|
||||
std::unique_ptr<CallbackStore> RegisterVoltageCallback(
|
||||
NotifyCallback callback, bool initialNotify);
|
||||
|
||||
/**
|
||||
* Read the analog output voltage.
|
||||
*
|
||||
* @return the voltage on this analog output
|
||||
*/
|
||||
double GetVoltage() const;
|
||||
|
||||
/**
|
||||
* Set the analog output voltage.
|
||||
*
|
||||
* @param voltage the new voltage on this analog output
|
||||
*/
|
||||
void SetVoltage(double voltage);
|
||||
|
||||
/**
|
||||
* Register a callback to be run when this analog output is initialized.
|
||||
*
|
||||
* @param callback the callback
|
||||
* @param initialNotify whether to run the callback with the initial state
|
||||
* @return the CallbackStore object associated with this callback
|
||||
*/
|
||||
[[nodiscard]]
|
||||
std::unique_ptr<CallbackStore> RegisterInitializedCallback(
|
||||
NotifyCallback callback, bool initialNotify);
|
||||
|
||||
/**
|
||||
* Check whether this analog output has been initialized.
|
||||
*
|
||||
* @return true if initialized
|
||||
*/
|
||||
bool GetInitialized() const;
|
||||
|
||||
/**
|
||||
* Define whether this analog output has been initialized.
|
||||
*
|
||||
* @param initialized whether this object is initialized
|
||||
*/
|
||||
void SetInitialized(bool initialized);
|
||||
|
||||
/**
|
||||
* Reset all simulation data on this object.
|
||||
*/
|
||||
void ResetData();
|
||||
|
||||
private:
|
||||
int m_index;
|
||||
};
|
||||
} // namespace sim
|
||||
} // namespace frc
|
||||
@@ -1,89 +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/simulation/AnalogOutputSim.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <hal/HAL.h>
|
||||
|
||||
#include "callback_helpers/TestCallbackHelpers.h"
|
||||
#include "frc/AnalogOutput.h"
|
||||
|
||||
namespace frc::sim {
|
||||
|
||||
TEST(AnalogOutputSimTest, Initialize) {
|
||||
HAL_Initialize(500, 0);
|
||||
|
||||
AnalogOutputSim outputSim{0};
|
||||
EXPECT_FALSE(outputSim.GetInitialized());
|
||||
|
||||
BooleanCallback callback;
|
||||
|
||||
auto cb =
|
||||
outputSim.RegisterInitializedCallback(callback.GetCallback(), false);
|
||||
AnalogOutput output(0);
|
||||
EXPECT_TRUE(outputSim.GetInitialized());
|
||||
|
||||
EXPECT_TRUE(callback.WasTriggered());
|
||||
EXPECT_TRUE(callback.GetLastValue());
|
||||
}
|
||||
|
||||
TEST(AnalogOutputSimTest, SetCallback) {
|
||||
HAL_Initialize(500, 0);
|
||||
|
||||
AnalogOutput output{0};
|
||||
output.SetVoltage(0.5);
|
||||
|
||||
AnalogOutputSim outputSim(output);
|
||||
|
||||
DoubleCallback voltageCallback;
|
||||
|
||||
auto cb =
|
||||
outputSim.RegisterVoltageCallback(voltageCallback.GetCallback(), false);
|
||||
|
||||
EXPECT_FALSE(voltageCallback.WasTriggered());
|
||||
|
||||
for (int i = 0; i < 50; ++i) {
|
||||
double voltage = i * .1;
|
||||
voltageCallback.Reset();
|
||||
|
||||
output.SetVoltage(0);
|
||||
|
||||
EXPECT_EQ(0, output.GetVoltage());
|
||||
EXPECT_EQ(0, outputSim.GetVoltage());
|
||||
|
||||
// 0 -> 0 isn't a change, so callback not called
|
||||
if (i > 2) {
|
||||
EXPECT_TRUE(voltageCallback.WasTriggered()) << " on " << i;
|
||||
EXPECT_NEAR(voltageCallback.GetLastValue(), 0, 0.001) << " on " << i;
|
||||
}
|
||||
|
||||
voltageCallback.Reset();
|
||||
|
||||
output.SetVoltage(voltage);
|
||||
|
||||
EXPECT_EQ(voltage, output.GetVoltage());
|
||||
EXPECT_EQ(voltage, outputSim.GetVoltage());
|
||||
|
||||
// 0 -> 0 isn't a change, so callback not called
|
||||
if (i != 0) {
|
||||
EXPECT_TRUE(voltageCallback.WasTriggered());
|
||||
EXPECT_NEAR(voltageCallback.GetLastValue(), voltage, 0.001);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(AnalogOutputSimTest, Reset) {
|
||||
HAL_Initialize(500, 0);
|
||||
|
||||
AnalogOutputSim outputSim{0};
|
||||
|
||||
AnalogOutput output{0};
|
||||
output.SetVoltage(1.2);
|
||||
|
||||
outputSim.ResetData();
|
||||
EXPECT_EQ(0, output.GetVoltage());
|
||||
EXPECT_EQ(0, outputSim.GetVoltage());
|
||||
}
|
||||
} // namespace frc::sim
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "frc/simulation/AddressableLEDSim.h"
|
||||
#include "frc/simulation/AnalogGyroSim.h"
|
||||
#include "frc/simulation/AnalogInputSim.h"
|
||||
#include "frc/simulation/AnalogOutputSim.h"
|
||||
#include "frc/simulation/AnalogTriggerSim.h"
|
||||
#include "frc/simulation/BuiltInAccelerometerSim.h"
|
||||
#include "frc/simulation/CTREPCMSim.h"
|
||||
@@ -31,7 +30,6 @@ TEST(SimInitializationTest, AllInitialize) {
|
||||
BuiltInAccelerometerSim biacsim;
|
||||
AnalogGyroSim agsim{0};
|
||||
AnalogInputSim aisim{0};
|
||||
AnalogOutputSim aosim{0};
|
||||
EXPECT_THROW(AnalogTriggerSim::CreateForChannel(0), std::out_of_range);
|
||||
EXPECT_THROW(DigitalPWMSim::CreateForChannel(0), std::out_of_range);
|
||||
DIOSim diosim{0};
|
||||
|
||||
Reference in New Issue
Block a user