Skip to content

Commit

Permalink
[wpilib] Replace MotorController interface with functional interface
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Dec 16, 2023
1 parent ab15dae commit 2cad49d
Show file tree
Hide file tree
Showing 125 changed files with 837 additions and 697 deletions.
37 changes: 20 additions & 17 deletions wpilibc/src/main/native/cpp/drive/DifferentialDrive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@

using namespace frc;

WPI_IGNORE_DEPRECATED
DifferentialDrive::DifferentialDrive(MotorController& leftMotor,
MotorController& rightMotor)
: m_leftMotor(&leftMotor), m_rightMotor(&rightMotor) {
wpi::SendableRegistry::AddChild(this, m_leftMotor);
wpi::SendableRegistry::AddChild(this, m_rightMotor);
: DifferentialDrive{[&](double output) { leftMotor.Set(output); },
[&](double output) { rightMotor.Set(output); }} {}
WPI_UNIGNORE_DEPRECATED

DifferentialDrive::DifferentialDrive(std::function<void(double)> leftMotor,
std::function<void(double)> rightMotor)
: m_leftMotor{std::move(leftMotor)}, m_rightMotor{std::move(rightMotor)} {
// wpi::SendableRegistry::AddChild(this, m_leftMotor);
// wpi::SendableRegistry::AddChild(this, m_rightMotor);
static int instances = 0;
++instances;
wpi::SendableRegistry::AddLW(this, "DifferentialDrive", instances);
Expand All @@ -40,8 +47,8 @@ void DifferentialDrive::ArcadeDrive(double xSpeed, double zRotation,

auto [left, right] = ArcadeDriveIK(xSpeed, zRotation, squareInputs);

m_leftMotor->Set(left * m_maxOutput);
m_rightMotor->Set(right * m_maxOutput);
m_leftMotor(left * m_maxOutput);
m_rightMotor(right * m_maxOutput);

Feed();
}
Expand All @@ -60,8 +67,8 @@ void DifferentialDrive::CurvatureDrive(double xSpeed, double zRotation,

auto [left, right] = CurvatureDriveIK(xSpeed, zRotation, allowTurnInPlace);

m_leftMotor->Set(left * m_maxOutput);
m_rightMotor->Set(right * m_maxOutput);
m_leftMotor(left * m_maxOutput);
m_rightMotor(right * m_maxOutput);

Feed();
}
Expand All @@ -80,8 +87,8 @@ void DifferentialDrive::TankDrive(double leftSpeed, double rightSpeed,

auto [left, right] = TankDriveIK(leftSpeed, rightSpeed, squareInputs);

m_leftMotor->Set(left * m_maxOutput);
m_rightMotor->Set(right * m_maxOutput);
m_leftMotor(left * m_maxOutput);
m_rightMotor(right * m_maxOutput);

Feed();
}
Expand Down Expand Up @@ -157,8 +164,8 @@ DifferentialDrive::WheelSpeeds DifferentialDrive::TankDriveIK(
}

void DifferentialDrive::StopMotor() {
m_leftMotor->StopMotor();
m_rightMotor->StopMotor();
m_leftMotor(0.0);
m_rightMotor(0.0);
Feed();
}

Expand All @@ -170,10 +177,6 @@ void DifferentialDrive::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("DifferentialDrive");
builder.SetActuator(true);
builder.SetSafeState([=, this] { StopMotor(); });
builder.AddDoubleProperty(
"Left Motor Speed", [=, this] { return m_leftMotor->Get(); },
[=, this](double value) { m_leftMotor->Set(value); });
builder.AddDoubleProperty(
"Right Motor Speed", [=, this] { return m_rightMotor->Get(); },
[=, this](double value) { m_rightMotor->Set(value); });
builder.AddDoubleProperty("Left Motor Speed", nullptr, m_leftMotor);
builder.AddDoubleProperty("Right Motor Speed", nullptr, m_rightMotor);
}
62 changes: 34 additions & 28 deletions wpilibc/src/main/native/cpp/drive/MecanumDrive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@

using namespace frc;

WPI_IGNORE_DEPRECATED
MecanumDrive::MecanumDrive(MotorController& frontLeftMotor,
MotorController& rearLeftMotor,
MotorController& frontRightMotor,
MotorController& rearRightMotor)
: m_frontLeftMotor(&frontLeftMotor),
m_rearLeftMotor(&rearLeftMotor),
m_frontRightMotor(&frontRightMotor),
m_rearRightMotor(&rearRightMotor) {
wpi::SendableRegistry::AddChild(this, m_frontLeftMotor);
wpi::SendableRegistry::AddChild(this, m_rearLeftMotor);
wpi::SendableRegistry::AddChild(this, m_frontRightMotor);
wpi::SendableRegistry::AddChild(this, m_rearRightMotor);
: MecanumDrive{[&](double output) { frontLeftMotor.Set(output); },
[&](double output) { rearLeftMotor.Set(output); },
[&](double output) { frontRightMotor.Set(output); },
[&](double output) { rearRightMotor.Set(output); }} {}
WPI_UNIGNORE_DEPRECATED

MecanumDrive::MecanumDrive(std::function<void(double)> frontLeftMotor,
std::function<void(double)> rearLeftMotor,
std::function<void(double)> frontRightMotor,
std::function<void(double)> rearRightMotor)
: m_frontLeftMotor{std::move(frontLeftMotor)},
m_rearLeftMotor{std::move(rearLeftMotor)},
m_frontRightMotor{std::move(frontRightMotor)},
m_rearRightMotor{std::move(rearRightMotor)} {
// wpi::SendableRegistry::AddChild(this, m_frontLeftMotor);
// wpi::SendableRegistry::AddChild(this, m_rearLeftMotor);
// wpi::SendableRegistry::AddChild(this, m_frontRightMotor);
// wpi::SendableRegistry::AddChild(this, m_rearRightMotor);
static int instances = 0;
++instances;
wpi::SendableRegistry::AddLW(this, "MecanumDrive", instances);
Expand All @@ -47,10 +58,10 @@ void MecanumDrive::DriveCartesian(double xSpeed, double ySpeed,
auto [frontLeft, frontRight, rearLeft, rearRight] =
DriveCartesianIK(xSpeed, ySpeed, zRotation, gyroAngle);

m_frontLeftMotor->Set(frontLeft * m_maxOutput);
m_frontRightMotor->Set(frontRight * m_maxOutput);
m_rearLeftMotor->Set(rearLeft * m_maxOutput);
m_rearRightMotor->Set(rearRight * m_maxOutput);
m_frontLeftMotor(frontLeft * m_maxOutput);
m_frontRightMotor(frontRight * m_maxOutput);
m_rearLeftMotor(rearLeft * m_maxOutput);
m_rearRightMotor(rearRight * m_maxOutput);

Feed();
}
Expand All @@ -68,10 +79,10 @@ void MecanumDrive::DrivePolar(double magnitude, Rotation2d angle,
}

void MecanumDrive::StopMotor() {
m_frontLeftMotor->StopMotor();
m_frontRightMotor->StopMotor();
m_rearLeftMotor->StopMotor();
m_rearRightMotor->StopMotor();
m_frontLeftMotor(0.0);
m_frontRightMotor(0.0);
m_rearLeftMotor(0.0);
m_rearRightMotor(0.0);
Feed();
}

Expand Down Expand Up @@ -107,16 +118,11 @@ void MecanumDrive::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("MecanumDrive");
builder.SetActuator(true);
builder.SetSafeState([=, this] { StopMotor(); });
builder.AddDoubleProperty(
"Front Left Motor Speed", [=, this] { return m_frontLeftMotor->Get(); },
[=, this](double value) { m_frontLeftMotor->Set(value); });
builder.AddDoubleProperty(
"Front Right Motor Speed", [=, this] { return m_frontRightMotor->Get(); },
[=, this](double value) { m_frontRightMotor->Set(value); });
builder.AddDoubleProperty(
"Rear Left Motor Speed", [=, this] { return m_rearLeftMotor->Get(); },
[=, this](double value) { m_rearLeftMotor->Set(value); });
builder.AddDoubleProperty(
"Rear Right Motor Speed", [=, this] { return m_rearRightMotor->Get(); },
[=, this](double value) { m_rearRightMotor->Set(value); });
builder.AddDoubleProperty("Front Left Motor Speed", nullptr,
m_frontLeftMotor);
builder.AddDoubleProperty("Front Right Motor Speed", nullptr,
m_frontRightMotor);
builder.AddDoubleProperty("Rear Left Motor Speed", nullptr, m_rearLeftMotor);
builder.AddDoubleProperty("Rear Right Motor Speed", nullptr,
m_rearRightMotor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ using namespace frc;
// Can't use a delegated constructor here because of an MSVC bug.
// https://developercommunity.visualstudio.com/content/problem/583/compiler-bug-with-delegating-a-constructor.html

WPI_IGNORE_DEPRECATED

MotorControllerGroup::MotorControllerGroup(
std::vector<std::reference_wrapper<MotorController>>&& motorControllers)
: m_motorControllers(std::move(motorControllers)) {
Expand Down Expand Up @@ -74,3 +76,5 @@ void MotorControllerGroup::InitSendable(wpi::SendableBuilder& builder) {
"Value", [=, this] { return Get(); },
[=, this](double value) { Set(value); });
}

WPI_UNIGNORE_DEPRECATED
5 changes: 5 additions & 0 deletions wpilibc/src/main/native/cpp/motorcontrol/NidecBrushless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
#include <hal/FRCUsageReporting.h>
#include <wpi/sendable/SendableBuilder.h>
#include <wpi/sendable/SendableRegistry.h>
#include "wpi/deprecated.h"

using namespace frc;

WPI_IGNORE_DEPRECATED

NidecBrushless::NidecBrushless(int pwmChannel, int dioChannel)
: m_dio(dioChannel), m_pwm(pwmChannel) {
wpi::SendableRegistry::AddChild(this, &m_dio);
Expand All @@ -26,6 +29,8 @@ NidecBrushless::NidecBrushless(int pwmChannel, int dioChannel)
wpi::SendableRegistry::AddLW(this, "Nidec Brushless", pwmChannel);
}

WPI_UNIGNORE_DEPRECATED

void NidecBrushless::Set(double speed) {
if (!m_disabled) {
m_speed = speed;
Expand Down
19 changes: 18 additions & 1 deletion wpilibc/src/main/native/cpp/motorcontrol/PWMMotorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
#include <fmt/format.h>
#include <wpi/sendable/SendableBuilder.h>
#include <wpi/sendable/SendableRegistry.h>
#include "wpi/deprecated.h"

using namespace frc;

void PWMMotorController::Set(double speed) {
m_pwm.SetSpeed(m_isInverted ? -speed : speed);
if (m_isInverted) {
speed = -speed;
}
m_pwm.SetSpeed(speed);

for (auto& follower : m_followers) {
follower->Set(speed);
}

Feed();
}

Expand Down Expand Up @@ -48,11 +57,19 @@ void PWMMotorController::EnableDeadbandElimination(bool eliminateDeadband) {
m_pwm.EnableDeadbandElimination(eliminateDeadband);
}

void PWMMotorController::AddFollower(PWMMotorController& follower) {
m_followers.emplace_back(&follower);
}

WPI_IGNORE_DEPRECATED

PWMMotorController::PWMMotorController(std::string_view name, int channel)
: m_pwm(channel, false) {
wpi::SendableRegistry::AddLW(this, name, channel);
}

WPI_UNIGNORE_DEPRECATED

void PWMMotorController::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("Motor Controller");
builder.SetActuator(true);
Expand Down
65 changes: 25 additions & 40 deletions wpilibc/src/main/native/include/frc/drive/DifferentialDrive.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

#pragma once

#include <functional>
#include <string>

#include <wpi/deprecated.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>

Expand All @@ -20,43 +22,9 @@ class MotorController;
* the Kit of Parts drive base, "tank drive", or West Coast Drive.
*
* These drive bases typically have drop-center / skid-steer with two or more
* wheels per side (e.g., 6WD or 8WD). This class takes a MotorController per
* side. For four and six motor drivetrains, construct and pass in
* MotorControllerGroup instances as follows.
*
* Four motor drivetrain:
* @code{.cpp}
* class Robot {
* public:
* frc::PWMVictorSPX m_frontLeft{1};
* frc::PWMVictorSPX m_rearLeft{2};
* frc::MotorControllerGroup m_left{m_frontLeft, m_rearLeft};
*
* frc::PWMVictorSPX m_frontRight{3};
* frc::PWMVictorSPX m_rearRight{4};
* frc::MotorControllerGroup m_right{m_frontRight, m_rearRight};
*
* frc::DifferentialDrive m_drive{m_left, m_right};
* };
* @endcode
*
* Six motor drivetrain:
* @code{.cpp}
* class Robot {
* public:
* frc::PWMVictorSPX m_frontLeft{1};
* frc::PWMVictorSPX m_midLeft{2};
* frc::PWMVictorSPX m_rearLeft{3};
* frc::MotorControllerGroup m_left{m_frontLeft, m_midLeft, m_rearLeft};
*
* frc::PWMVictorSPX m_frontRight{4};
* frc::PWMVictorSPX m_midRight{5};
* frc::PWMVictorSPX m_rearRight{6};
* frc::MotorControllerGroup m_right{m_frontRight, m_midRight, m_rearRight};
*
* frc::DifferentialDrive m_drive{m_left, m_right};
* };
* @endcode
* wheels per side (e.g., 6WD or 8WD). This class takes a setter per side. For
* four and six motor drivetrains, use CAN motor controller followers or
* PWMMotorController::AddFollower().
*
* A differential drive robot has left and right wheels separated by an
* arbitrary width.
Expand Down Expand Up @@ -101,13 +69,30 @@ class DifferentialDrive : public RobotDriveBase,
double right = 0.0;
};

/**
* Construct a DifferentialDrive.
*
* To pass multiple motors per side, use CAN motor controller followers or
* PWMSpeedController::AddFollower(). If a motor needs to be inverted, do so
* before passing it in.
*
* @param leftMotor Left motor.
* @param rightMotor Right motor.
*/
WPI_DEPRECATED("Use DifferentialDrive constructor with function arguments.")
DifferentialDrive(MotorController& leftMotor, MotorController& rightMotor);

/**
* Construct a DifferentialDrive.
*
* To pass multiple motors per side, use a MotorControllerGroup. If a motor
* needs to be inverted, do so before passing it in.
*
* @param leftMotor Left motor setter.
* @param rightMotor Right motor setter.
*/
DifferentialDrive(MotorController& leftMotor, MotorController& rightMotor);
DifferentialDrive(std::function<void(double)> leftMotor,
std::function<void(double)> rightMotor);

~DifferentialDrive() override = default;

Expand Down Expand Up @@ -210,8 +195,8 @@ class DifferentialDrive : public RobotDriveBase,
void InitSendable(wpi::SendableBuilder& builder) override;

private:
MotorController* m_leftMotor;
MotorController* m_rightMotor;
std::function<void(double)> m_leftMotor;
std::function<void(double)> m_rightMotor;
};

} // namespace frc
Loading

0 comments on commit 2cad49d

Please sign in to comment.