Skip to content

Commit

Permalink
[wombat] move motorvoltagecontroller into voltage controller. (#39)
Browse files Browse the repository at this point in the history
Resolves #36
  • Loading branch information
spacey-sooty authored Dec 31, 2023
1 parent 74baf54 commit d773686
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
13 changes: 7 additions & 6 deletions wombat/src/main/cpp/utils/VoltageController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ units::volt_t VoltageController::GetEstimatedRealVoltage() const {
return units::math::min(units::math::max(-vb, GetVoltage()), vb);
}

MotorVoltageController::MotorVoltageController(frc::MotorController *MotorController) : _MotorController(MotorController) {}
VoltageController::VoltageController(frc::MotorController *MotorController) : _MotorController(MotorController)
{}

void MotorVoltageController::SetVoltage(units::volt_t voltage) {
void VoltageController::SetVoltage(units::volt_t voltage) {
_MotorController->Set(voltage / GetBusVoltage());
}

units::volt_t MotorVoltageController::GetVoltage() const {
units::volt_t VoltageController::GetVoltage() const {
return _MotorController->Get() * GetBusVoltage();
}

units::volt_t MotorVoltageController::GetBusVoltage() const {
units::volt_t VoltageController::GetBusVoltage() const {
return frc::RobotController::GetInputVoltage() * 1_V;
}

void MotorVoltageController::SetInverted(bool invert) {
void VoltageController::SetInverted(bool invert) {
_MotorController->SetInverted(invert);
}

bool MotorVoltageController::GetInverted() const {
bool VoltageController::GetInverted() const {
return frc::RobotController::GetInputVoltage();
}
54 changes: 20 additions & 34 deletions wombat/src/main/include/utils/VoltageController.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,46 @@ namespace wom {
*/
class VoltageController {
public:
VoltageController(frc::MotorController *MotorController);
/**
* Set the voltage of the output.
*/
virtual void SetVoltage(units::volt_t voltage) = 0;
void SetVoltage(units::volt_t voltage);
/**
* Get the voltage of the output.
*/
virtual units::volt_t GetVoltage() const = 0;
units::volt_t GetVoltage() const;

/**
* Set the output as inverted.
*/
virtual void SetInverted(bool invert) = 0;
void SetInverted(bool invert);
/**
* Get whether the output is inverted
*/
virtual bool GetInverted() const = 0;
bool GetInverted() const;

/**
* Get the estimated real voltage of the output, based on the controller voltage.
* Get the estimated real voltage of the output, based on the controller voltage.
*/
units::volt_t GetEstimatedRealVoltage() const;
};

/**
* The MotorVoltageController is an adapter for an frc::MotorController to
* a VoltageController.
*/
class MotorVoltageController : public VoltageController {
public:
MotorVoltageController(frc::MotorController *MotorController);

void SetVoltage(units::volt_t voltage) override;
units::volt_t GetVoltage() const override;

void SetInverted(bool invert) override;
bool GetInverted() const override;

units::volt_t GetBusVoltage() const;

/**
* Create a MotorVoltageController with a given frc::MotorController
* subclass. Please note that this creates an unsafe pointer (will never dealloc)
*/
template<typename T, typename ...Args>
static MotorVoltageController Of(Args& ...args) {
T *t = new T(args...); // Be warned, does not deallocate!
return MotorVoltageController{t};
}

template<typename ...Args>
static MotorVoltageController Group(Args& ...args) {
return Of<frc::MotorControllerGroup>(args...);
}
/**
* Create a MotorVoltageController with a given frc::MotorController
* subclass. Please note that this creates an unsafe pointer (will never dealloc)
*/
template<typename T, typename ...Args>
static VoltageController Of(Args& ...args) {
T *t = new T(args...); // Be warned, does not deallocate!
return VoltageController{t};
}

template<typename ...Args>
static VoltageController Group(Args& ...args) {
return Of<frc::MotorControllerGroup>(args...);
}

private:
frc::MotorController *_MotorController;
Expand Down

0 comments on commit d773686

Please sign in to comment.